Package: datalink
datalinkdatalinkis a python module that lets you interact with entries of SQL
data as if you were simply using dictionaries.The module is built on top ofsqlalchemyanddataset, and all SQL
schemas supported insqlalchemyare supported indatalink.Usingdatalink.factory, the user creates a new class, each instance of
which is linked to a single entry of data stored in an SQL database.
Pushing changes to this SQL entry is as simple as modifying the
instance's attributes.Loading, saving, unique identification, and database management all
take place behind the scenes, so the user doesn't need to worry about
databases at all.Installationdatalinkis available on the Python Package Index.pip install datalinkTutorialA full tutorial is avaiablehere.Creating datalinksIf you've ever usedNameTuplefrom thecollectionsmodule, you know
how to usedatalink.Let's make a template for our data and an instance of that template,
backed up to a file-basedsqlitedatabase.Our SQL table will have the columnsa,b, andcwith the defaults
set inmy_data_fields.importdatalinkmy_data_fields={'a':0,'b':'a string','c':[]}# default entriesMyClass=datalink.factory('MyClass','my_table',my_data_fields,database='/tmp/my.db')A=MyClass()INFO | db created at: sqlite:////tmp/my.db
INFO | created table my_table
DEBUG | Creating new database entry with id ce267917-6c2e-46d1-bacc-c52affba2d2d.We can also instantiate data in the instance declaration.
We can then use thedataproperty to expose a dict containing a map of
the column name to value to make sure it has been correctly set.B=MyClass(a=100,b='a new string')B.dataDEBUG | Creating new database entry with id e2bd9f8c-af49-4188-96c2-b6f4ea771188.

{'a': 100,
 'b': 'a new string',
 'c': [],
 'id': 'e2bd9f8c-af49-4188-96c2-b6f4ea771188'}Modifying dataNow let's modify some elements and see what happens. They are accessible
as properties inAas determined by the defaults we gave the factory.A.a=5A.c.append(None)print(A.a,A.c)DEBUG | Updating existing database entry for id ce267917-6c2e-46d1-bacc-c52affba2d2d.
DEBUG | Updating existing database entry for id ce267917-6c2e-46d1-bacc-c52affba2d2d.
5 [None]Multiple updates can be pushed to the SQL database at once with theupdatemethod.A.update(a=1000,c=[1,2,3])A.dataDEBUG | Updating existing database entry for id ce267917-6c2e-46d1-bacc-c52affba2d2d.

{'a': 1000,
 'b': 'a string',
 'c': [1, 2, 3],
 'id': 'ce267917-6c2e-46d1-bacc-c52affba2d2d'}Loading dataAs we can see, each new instance ofMyClassreceives a Unique
Universal Identifier (UUID), that can be used to easily fetch rows from
the database as a positional argument.print(A.id)id_to_load=A.iddelAC=MyClass(id_to_load)C.data'ce267917-6c2e-46d1-bacc-c52affba2d2d'

DEBUG | Loaded data corresponding to ID: ce267917-6c2e-46d1-bacc-c52affba2d2d

{'a': 1000,
 'b': 'a string',
 'c': [1, 2, 3],
 'id': 'ce267917-6c2e-46d1-bacc-c52affba2d2d'}User-specified identificationUsers can also specify their own identifiers in the construction of new
instances of MyClass to replace the uuid.This can be useful in design patterns that want to store easily
accessible configuration or metadata.D=MyClass('myid',a=200,b='some other string',c=[4,5,6])DEBUG | Creating new database entry with id myid.