Skip to main content

A collection of classes that allow for easier usage of common packages.

Project description

Vys Documentation

DB Schema documentation:

SqLite Syntax

from Vys import models

cfg = models.DbCredentials(database='test.db')

class Employee(models.LiteSchema):
    def __init__(self, *args, **kwargs):
        super().__init__(db_credentials=cfg, table='employee')

Create the schema parameters

class Employee(models.LiteSchema):
    def __init__(self, *args, **kwargs):
        super().__init__(db_credentials=cfg, table='employee')
        self.add_params({
            'name': {
                'dbtype': 'varchar(255)',
                'type': str
            },
            'manager': {
                'dbtype': 'varchar(9)'
            }
        })
        self.init_schema(args, kwargs)

IDs for SqLite are added automatically.

Dbtype and type is different purely for validation purposes - type is for schema validation, dbtype corresponds to the data type in the database.

Query

user = User
x = user.find(
    query={
        'user': 'connor.white'
    },
    fetch=['perms']
)
# Returns [{'ID': 'ShlLuRWaa', 'createdAt': '2020-09-04 11:32:44.132863', 'updatedAt': '2020-09-04 11:32:44.132863', 'manager': None, 'name': 'tefrstf'}

x = user.find_one(
    query={
        'user': 'connor.white'
    },
    fetch=['perms']
)
# Returns {'ID': 'ShlLuRWaa', 'createdAt': '2020-09-04 11:32:44.132863', 'updatedAt': '2020-09-04 11:32:44.132863', 'manager': None, 'name': 'tefrstf'}

user.populate(
    query={'name': 'tefrstf'} 
)
print(user)

# OR

user.populate(
    name='newname'
)

# Returns # Returns {'ID': 'ShlLuRWaa', 'createdAt': '2020-09-04 11:32:44.132863', 'updatedAt': '2020-09-04 11:32:44.132863', 'manager': None, 'name': 'tefrstf'}

user.populate_many(
    query={'name': 'tefrstf'} 
)
print(user)
# Returns {'createdAt': ['2020-09-04 11:32:44.132863', '2020-09-04 11:33:02.841748', '2020-09-04 11:33:37.348974', '2020-09-04 11:37:57.689165', '2020-09-04 11:38:11.541987', '2020-09-04 11:39:46.465859', '2020-09-04 11:40:28.353423'], 'updatedAt': ['2020-09-04 11:32:44.132863', '2020-09-04 11:33:02.841748', '2020-09-04 11:33:37.348974', '2020-09-04 11:37:57.689165', '2020-09-04 11:38:11.541987', '2020-09-04 11:39:46.465859', '2020-09-04 11:40:28.353423'], 'name': ['tefrstf', 'tefrstf', 'tefrstf', 'tefrstf', 'tefrstf', 'tefrstf', 'tefrstf'], 'manager': [None, None, None, None, None, None, None], 'ID': ['ShlLuRWaa', 'jkkVrB06P', 'LXCBlqUhN', 'tLCrf5EzW', '8EJzjBtwO', 'TrPxwtTXo', 'wRTuXYKe1']}

The fetch parameter is not required here, excluding it will return all columns. Find will return multiple, find_one only one.

Create

User.create({
    'user': 'new',
    'password': 'hffgv'
})

user = User('create', name='name', password='hffgv')

The id will be done automatically, as will the "CreatedAt" and "UpdatedAt" Fields. This will insert a new row into the db.

Update

User.find_and_update(
    query={
        'perms': 'user'
    },
    update={
        'perms': 'silly'
    }
)

############################################

User.populate({
    'name': 'tefrstf'
})

mark.update({
    'name': 'testing123'
})

# OR

mark.update(
    name='testing123'
)

Find and update will update all instances - as the "find" function returns a python dict, you can input them directly into the query section here. Using the .update function on a class that has been populated will only update that row.

Adding a foreign key to a table.

options = {
    'column': 'manager',
    'table': 'managers',
    'foreign column': 'ID'
}


class Employee(models.LiteSchema):
    def __init__(self, *args, **kwargs):
        super().__init__(db_credentials=cfg, table='employee', f_keys=options)
        self.add_params({
            'name': {
                'dbtype': 'varchar(255)',
                'type': str
            },
            'manager': {
                'dbtype': 'varchar(9)'
            }
        })
        self.expand_param(
            param='manager',
            fetch='ID, name'
        )
        self.init_schema(args, kwargs)

This snippet creates a column linked to a foreign key. In the "options" dict, the keys should be self explanatory. The expand_param function expands the return - so instead of returning the ID of the field, it will return info from the column in which that key is present in the other table.

Creating the virtual population.

class Manager(models.LiteSchema):
    def __init__(self, db_credentials=cfg, table='managers'):
        super().__init__(db_credentials, table)
        self.add_params({
            'name': {
                'dbtype': 'varchar(255)',
                'type': str
            }
        })
        self.add_virtual({
            'table': 'employee',
            'foreign_column': 'manager',
            'field': 'ID'
        })

The dict takes 3 arguments. "Table" is the target table where the foreign key is located. "Foreign column" is the column where the key is located. "Field" is the local column where the key is located.

Populating the virtual field.

manager = Manager()

manager.populate_new(name='connor')
manager.populate_virtual()

print(manager.virtual)

# Returns [{'ID': 'o0l5i2nIr', 'createdAt': '2020-08-31 20:51:08.422624', 'updatedAt': '2020-09-02 08:43:51.600846', 'manager': 'w47mlx4df', 'name': 'mark'}]

print(manager.virtual.o0l5i2nIr)

# Returns {'ID': 'o0l5i2nIr', 'createdAt': '2020-08-31 20:51:08.422624', 'updatedAt': '2020-09-02 08:43:51.600846', 'manager': 'w47mlx4df', 'name': 'mark'}

Datetime manipulation

DatetimeGenerator

datetime_object = DatetimeGenerator(
    datetime_input=datetime.strptime('2000-08-31 10:22:08', '%Y-%m-%d %H:%M:%S'),
    format='iso',
    str_format='%Y-%m-%d'
)
datetime_object.add(hours=9, days=12)

Datetime input can be any datetime object. Format can be either "datetime", "iso", "unix", or "string".

Leaving datetime input blank will generate the current time. Leaving the format blank defaults to datetime. Str_format should only be used when the format is put as string.

You can add or subtract values of time with the add and subtract methods.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Vys-0.2.2.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Vys-0.2.2-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file Vys-0.2.2.tar.gz.

File metadata

  • Download URL: Vys-0.2.2.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for Vys-0.2.2.tar.gz
Algorithm Hash digest
SHA256 682d48f3a7ed67cde2ec40688c2a354182a6552555ea5ed597d5aa8fb6bf0203
MD5 cb828de9ab4df36b43a8175ed85120b6
BLAKE2b-256 8ab7bf23c1dd5c2f95a5db2041880f2c27652b61d2758ee18c5e65cf165cf471

See more details on using hashes here.

File details

Details for the file Vys-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: Vys-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for Vys-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fc0bb5d267c45b95ab09db616e6fcffbd8726cc4044df07459fa012e75147f35
MD5 035e3e4a7105f68837b2e75e934347df
BLAKE2b-256 b3e97d4ce0ba4f4e0a47c36bce910c1a47232f0d3240a606a8a3d6c3f0e049a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page