Skip to main content

PyDBRepo

Is a simple implementation of the Repository pattern to access data in python, providing extensibility flexibility and builtin tools to manage databases with this pattern.

Supported databases

  • SQLite
  • MySQL
  • PostgreSQL
  • MongoDB
  • Amazon QLDB

Requirements

  • Python >= 3.7

Postgres

  • psychopg2-binary

Mysql

  • mysql-connector-python

MongoDB

  • pymongo
  • dnspython

Amazon QLDB

  • pyqldb

Examples

Entity usage

Entity model

This class brings the build it in methods:

  • to_dict: Will take all properties of the created class and will convert it into a dict instance.
  • from_dict: This will take a dict instance and will set the values of every key into a model property with the same name.
  • from_record: It takes an ordered Iterable object with the name of the fields that will be loaded into the model, and a tuple with the corresponding values

Entity models will be used with simple class properties or can be used with the Field descriptor of the package

Example with simple properties
from pydbrepo import Entity

class Model(Entity):
    id = None
    name = None

model = Model.from_dict({"id": 1, "name": "some"})
# Model({"id": 1, "name": "some"})

print(model.id) # => 1
print(model.name) # => some
Example with property decorators
from pydbrepo import Entity

class Model(Entity):
    def __init__(self):
        super().__init__()
        
        self.id = None
        self.name = None
        
    @property
    def id(self):
        return self._id
    
    @id.setter
    def id(self, value):
        self._id = value
        
    @property
    def name(self):
        return self._name
    
    @name.setter
    def name(self, value):
        self._name = value

model = Model.from_dict({"id": 1, "name": "some"})
# Model({"id": 1, "name": "some"})

print(model.id) # => 1
print(model.name) # => some
Example with Field descriptor
from pydbrepo import Entity, Field, named_fields

@named_fields
class Model(Entity):
    id = Field(type_=int)
    name = Field(type_=str)

model = Model.from_dict({"id": 1, "name": "some"})
# Model({"id": 1, "name": "some"})

print(model.id) # => 1
print(model.name) # => some
Example of casting values with Field descriptor
from uuid import UUID
from pydbrepo import Entity, Field, named_fields

@named_fields
class Model(Entity):
    id = Field(type_=(UUID, str), cast_to=UUID, cast_if=str)
    name = Field(type_=str)

model = Model.from_dict({"id": '10620c02-d80e-4950-b0a2-34a5f2d34ae5', "name": "some"})
# Model({"id": UUID('10620c02-d80e-4950-b0a2-34a5f2d34ae5'), "name": "some"})

print(model.id) # => 10620c02-d80e-4950-b0a2-34a5f2d34ae5
print(model.name) # => some
Example of casting from a callback function
from datetime import date, datetime
from pydbrepo import Entity, Field, named_fields

def cast_epoch(value):
    if isinstance(value, date):
        return int(value.strftime("%s"))
    
    if isinstance(value, datetime):
        return int(value.timestamp())

@named_fields
class Model(Entity):
    name = Field(type_=str)
    epoch = Field(type_=(int, date, datetime), cast_to=cast_epoch, cast_if=(date, datetime))

model = Model.from_dict({"name": "some", "epoch": datetime.now()})
# Model({"name": "some", "epoch": 1231231231})

print(model.name) # => some
print(model.epoch) # => 1231231231
Example of iterable fields and casting with Field descriptor
from pydbrepo import Entity, Field, named_fields

@named_fields
class Item(Entity):
    name = Field(type_=str)
    price = Field(type_=float)

@named_fields
class Model(Entity):
    id = Field(type_=int)
    name = Field(type_=str)
    items = Field(type_=list, cast_items_to=Item)

model = Model.from_dict({
    "id": 1, 
    "name": "some", 
    "items": [
        {"name": "some", "price": 5.99},
        {"name": "nothing", "price": 6.99},
    ]
})
# Model({"id": 1, "name": "some", "items": [Item({"name": "some", "price": 5.99}), Item({"name": "nothing", "price": 6.99})]})

print(model.id) # => 1
print(model.name) # => some
print(model.items) # => [Item({"name": "some", "price": 5.99}), Item({"name": "nothing", "price": 6.99})]
print(model.items[0].price) # => 5.99

Metadata

Release files for pydbrepo 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydbrepo 0.8.0
File Size Uploaded
pydbrepo-0.8.0.tar.gz 23.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydbrepo 0.8.0
File Interpreter ABI Platform
pydbrepo-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 64.1 kB

Release files / pydbrepo-0.8.0.tar.gz

Download URL pydbrepo-0.8.0.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
55f8cb4e6b903ac1d20bea8953b6ed8fc7d149986a9f75c7ae06a1337899cd08
BLAKE2b-256 checksum
How to use checksums
448e119c43f9e984c8b7968d7702981f7270ddfb681e07b28d24f54b713c11ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0 CPython/3.8.12 Linux/5.8.0-1041-azure

Release files / pydbrepo-0.8.0-py3-none-any.whl

Download URL pydbrepo-0.8.0-py3-none-any.whl
Size 40.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8798bdb8fc362c6ab48f3f1e1e265bf1dad6b2489fd2044d32bd580fe572aeb
BLAKE2b-256 checksum
How to use checksums
1b25f62851c65c3c94d74ece98829f766e69ce99f9056934bc37486891050dd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0 CPython/3.8.12 Linux/5.8.0-1041-azure

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.2.0

2 release files

0.1.17

2 release files

0.1.16

2 release files

0.1.15

2 release files

0.1.14

2 release files

0.1.13

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page