datalink
datalink is 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 of sqlalchemy and dataset, and all SQL
schemas supported in sqlalchemy are supported in datalink.
Using datalink.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.
Installation
datalink is available on the Python Package Index.
pip install datalink
Tutorial
A full tutorial is avaiable here.
Creating datalinks
If you've ever used NameTuple from the collections module, you know
how to use datalink.
Let's make a template for our data and an instance of that template,
backed up to a file-based sqlite database.
Our SQL table will have the columns a, b, and c with the defaults
set in my_data_fields.
import datalink
my_data_fields = {'a': 0, 'b': 'a string', 'c': []} # default entries
MyClass = 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 the data property 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.data
DEBUG | 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 data
Now let's modify some elements and see what happens. They are accessible
as properties in A as determined by the defaults we gave the factory.
A.a = 5
A.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 the
update method.
A.update(a=1000, c=[1,2,3])
A.data
DEBUG | 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 data
As we can see, each new instance of MyClass receives 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.id
del A
C = 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 identification
Users 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.
Metadata
Release files for datalink 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| datalink-0.1.3.tar.gz | 11.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| datalink-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 33.9 kB
Release files / datalink-0.1.3.tar.gz
| Download URL | datalink-0.1.3.tar.gz |
|---|---|
| Size | 11.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d233b1a19c560335da784c9e5a5c9a2de7b1e4ae0fcf468e81476624f102ace1
|
|
BLAKE2b-256 checksum How to use checksums |
783327b19779e5dacd8868309ac8dcd97b9a1fa0d67406c3bc943dca983d84f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/42.0.1.post20191125 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.5
|
Release files / datalink-0.1.3-py3-none-any.whl
| Download URL | datalink-0.1.3-py3-none-any.whl |
|---|---|
| Size | 22.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
24d154cf3050edc17a8b3e987f091d80770f9fcdf61bbb4c4deb26b1047ee617
|
|
BLAKE2b-256 checksum How to use checksums |
8e3c7a70ccd8cd10dd9ac0627876aaa090d26bdd40096da9a45b0ae144313f3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/42.0.1.post20191125 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.5
|
Security for datalink 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.