Package: picorm
picormPicorm is a small ORM with limited functions for multiple database engines for pet-projects, mostly for simple CRUD operations in cases, when you don't want to introduce a solid ORM like SQLAlchemy or whatever, but still want to keep storage-related stuff out of your code.Getting StartedInstallationTo install frompypi:pip install picormSimple usagefrompicormimportFileStorageasStorage# import one of storage implementationsstorage=Storage('path_to_database_file')# create storage object# every collection that you need to store must inherit from storage's Table classclassUsers(storage.Table):def__init__(self,fields={}):defaults=OrderedDict([('key','users')])# specify table namefork,vinfields.items():defaults[k]=vsuper().__init__(defaults)storage.create('users',OrderedDict([# specify table name and desired fields to be stored('key',storage.types['int']),('id',storage.types['int']),('name',storage.types['str']),('type',storage.types['str']),]))# every query on storage.users will return object of this class if not specified otherwiseclassUser(self.Record):def__init__(self,fields={}):# default values for fieldsdefaults=OrderedDict([('key',-1),('id',-1),('name',':null'),('type',':null'),])fork,vinfields.items():defaults[k]=vsuper().__init__(defaults)# that's it, you can add your own methods and propertiesself.User=User# attach classes to table objectusers=Users()# create table objectusers.Record=users.User# make every query return object of class UserStorage queryingnew_user=users.User({'id':42,'name':'foo'})# create record, you can do it this way or through storage.users.Userusers.add(new_user)# place it in storagefound_user=users.find_one({'name':'foo'})# get storage Record - an object of class Userfound_user.get('id')# get record's field valuefound_user.set({'name':'bar'})# change desired fieldsstorage.disconnect()# don't forget to disconnect from storage if you're planning to switch enginesReturn different subclasses of table recordIn those cases, when you need queries to return various subclasses of table record, you can simply introduce them after User definition in example:classUsers(storage.Table):def__init__(self,fields={}):# ...classPlayer(User):# an example of subclassing table Recordtype='player'def__init__(self,fields):overrides={'type':Player.type}merged={**fields,**overrides}super().__init__(merged)# ...self.Player=Player# attach class to table object# create a type -> class map to return objects of different subclasses of table recordself.type_class_map={Player.type:Player,User.type:User,':null':User,# "default" class to wrap record}# make every query return object of either Player or User class based on "type" field valueusers.Record=lambdafields:users.type_class_map[fields['type']](fields)Table methodsmethodargumentsreturnscreateself, name, schema, logNonefindself, selectorlist of Record objectsfind_oneself, selectorRecord object or Nonefind_maxself, keyRecord object or Noneaddself, recordNoneremoveself, recordNoneRecord operationsmethodargumentsreturnssetself, fieldsNonegetself, keyRecord's field valueSupported enginesFile storageSQLite storageDevelopmentEnvironment setupInstall Python 3.9+InstallvirtualenvpipinstallvirtualenvClone this projectFrom project directory, runvirtualenv.envNote: This will create a virtual environment using the Python version
thatvirtualenvwas run with (which will be the version it was installed
with). To use a specific Python version, run:virtualenv--python=<path_to_other_python_version>.env# For example, this might look likevirtualenv--python=/usr/bin/python3.6.envAssuming you are using thebashshell, run:source.env/bin/activateFor other shells, see the otheractivate.*scripts in the.env/bin/directory. If you are on Windows, run:.env\Scripts\activate.batInstall all of the required packages usingpipinstall-rrequirements.txtTestingThis project uses tox -> pytest. To trigger tests either runtoxorpytestoptionally with -vv and -s flags for verbosity and prints.Packaging moduleRun the following command to package picorm module:python-mpipinstall--upgradebuild
python-mbuildGenerated archive and .whl package will be placed indistdirectory.ContributionsPR are always welcome!