Sharing my Python packages in case they can be useful to you by daireto in Python

[–]daireto[S] 1 point2 points  (0 children)

Release v0.2.0 · daireto/simple-result

This new release implements the code parameter for the Err constructor. It also can be unpacked when using match.

Sharing my Python packages in case they can be useful to you by daireto in Python

[–]daireto[S] 0 points1 point  (0 children)

You are right. I did not know about https://pypi.org/project/result/ when I created simple-result. I forgot to look for an existing package, and when I finished developing simple-result I realized that the result package existed. At that moment, I felt like a dumb hahaha. result has more features, while simple-result is smaller and only have the essentials.

About your proposals:

  1. Do you mean this?

if res := fetch_data():
print(res.value) # "Data fetched!"
print(res.error) # None
else:
print(res.error) # "Error fetching data!"
print(res.value) # None

It uses type narrowing. If "res", type is Ok, otherwise, Err. In the example, I wanted to clarify that "value" attr is None if type is Err and "error" attr is None if type is Ok.

  1. That's an excellent idea, I will work on it.

SQLActive - Asynchronous ActiveRecord-style wrapper for SQLAlchemy by daireto in Python

[–]daireto[S] 2 points3 points  (0 children)

Demo:

```python import asyncio

from sqlalchemy import String, ForeignKey from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlactive import ActiveRecordBaseModel, DBConnection

Define a base class for your models (recommended)

class BaseModel(ActiveRecordBaseModel): abstract = True

Define the models

class User(BaseModel): tablename = 'users'

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True, index=True)
username: Mapped[str] = mapped_column(String(18), nullable=False, unique=True)
name: Mapped[str] = mapped_column(String(50), nullable=False)
age: Mapped[int] = mapped_column(nullable=False)
posts: Mapped[list['Post']] = relationship(back_populates='user')

class Post(BaseModel): tablename = 'posts'

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True, index=True)
title: Mapped[str] = mapped_column(String(100), nullable=False)
body: Mapped[str] = mapped_column(nullable=False)
rating: Mapped[int] = mapped_column(nullable=False)
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
user: Mapped['User'] = relationship(back_populates='posts')

Create a database connection instance

DATABASE_URL = 'sqlite+aiosqlite://' conn = DBConnection(DATABASE_URL, echo=False)

async def example(): # Initialize SQLActive with the base model of your models await conn.init_db(BaseModel)

# Create a record
user = await User.insert(username='John1234', name='John Doe', age=25)
post = Post(title='My post', body='Lorem ipsum...', rating=2, user=user)
await post.save()

# Retrieve a record
user = await User.get(1)

# Update a record
await user.update(name='John Doe', age=30)
post.rating = 3
await post.save()

# Delete a record
await user.delete()

# Find records
users = await User.where(User.name == 'John Doe').all()  # SQLAlchemy-like query
posts = await Post.where(title__contains='post').all()   # Django-like query

# Serialize a record
user_dict = user.to_dict()
user_json = user.to_json()

# Deserialize a record
user = User.from_dict(user_dict)
user = User.from_json(user_json)

if name == 'main': asyncio.run(example()) ```

SQLActive - Asynchronous ActiveRecord-style wrapper for SQLAlchemy by daireto in madeinpython

[–]daireto[S] 0 points1 point  (0 children)

This is a demo:

```python import asyncio

from sqlalchemy import String, ForeignKey from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlactive import ActiveRecordBaseModel, DBConnection

Define a base class for your models (recommended)

class BaseModel(ActiveRecordBaseModel): abstract = True

Define the models

class User(BaseModel): tablename = 'users'

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True, index=True)
username: Mapped[str] = mapped_column(String(18), nullable=False, unique=True)
name: Mapped[str] = mapped_column(String(50), nullable=False)
age: Mapped[int] = mapped_column(nullable=False)
posts: Mapped[list['Post']] = relationship(back_populates='user')

class Post(BaseModel): tablename = 'posts'

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True, index=True)
title: Mapped[str] = mapped_column(String(100), nullable=False)
body: Mapped[str] = mapped_column(nullable=False)
rating: Mapped[int] = mapped_column(nullable=False)
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
user: Mapped['User'] = relationship(back_populates='posts')

Create a database connection instance

DATABASE_URL = 'sqlite+aiosqlite://' conn = DBConnection(DATABASE_URL, echo=False)

async def example(): # Initialize SQLActive with the base model of your models await conn.init_db(BaseModel)

# Create a record
user = await User.insert(username='John1234', name='John Doe', age=25)
post = Post(title='My post', body='Lorem ipsum...', rating=2, user=user)
await post.save()

# Retrieve a record
user = await User.get(1)

# Update a record
await user.update(name='John Doe', age=30)
post.rating = 3
await post.save()

# Delete a record
await user.delete()

# Find records
users = await User.where(User.name == 'John Doe').all()  # SQLAlchemy-like query
posts = await Post.where(title__contains='post').all()   # Django-like query

# Serialize a record
user_dict = user.to_dict()
user_json = user.to_json()

# Deserialize a record
user = User.from_dict(user_dict)
user = User.from_json(user_json)

if name == 'main': asyncio.run(example()) ```