A package for assisting linking attributes.
Project description
AttrLinker
What it is
It is an Attribute Linker for class instances. It links a certain attribute name for the class instances to a certain other attribute name before being returned as a value for given attribute name.
How does it work?
It works using property. To be more specific, it links an attribute name on the class with property, to its target attribute, which is passed through a converter and then returned as the attribute value.
What is Presets
To help ease linking stuff, because preparing converters is often tedious.
What is this for?
IMHO, The best use case for this is in dynamically allocated data, periodically updated data, an example is for an app that periodically fetches data from some server about something, and on it's response, there's a lot of entries, which is needed to be updated on its attributes. You could use property for this, but over a certain point, it will become to repetitive. This is the problem this module strives to solve.
Reminders
- Please note that an implementation of the module may or may not work across different versions.
Examples
1. Dynamically Allocated Data
from attrLinker.linkMethod import MultiDictionary, FormattedText
from attrLinker.preparedLink import PreparedLink
from attrLinker import LinkedClass
@LinkedClass
class User:
# linkMap pair => {destination_attribute: source_key, ...}
# enableSetter set to True to be able to assign value back to it.
__LINKS__ = [PreparedLink(MultiDictionary, 'userData', linkMap={'ID':'id', 'name':'name', 'onlineTime':'online_time', 'status':'status'}, enableSetter=True),
PreparedLink(FormattedText, 'userData', 'nameTag', formattableText="{Name}#{id}")]
def __init__(self):
self.userData = {}
def fetchData(self):
if len(self.userData):
self.userData = {'id':1, 'name':'Foo', 'online_time':0, 'status': 'idle'}
else:
self.userData['status']='online'
self.userData['online_time']+=1
>>> testuser = User()
>>> testuser.fetchData()
>>> testuser.ID
1
>>> testuser.name
'Foo'
>>> testuser.onlineTime
0
>>> testuser.status
'idle'
>>> testuser.nameTag
'Foo#1'
>>> testuser.userData # Checking the data directly
{'id': 1, 'name': 'Foo', 'online_time': 0, 'status': 'idle'}
>>> testuser.onlineTime+=10 # Change it remotely from the assigned attribute (only if enableSetter is True)
>>> testuser.userData
{'id': 1, 'name': 'Foo', 'online_time': 10, 'status': 'idle'}
>>> testuser.fetchData() # Simulating new data fetched from a server on the internet
>>> testuser.onlineTime
11
>>> testuser.status
'online'
>>> testuser.userData # Checking the data directly
{'id': 1, 'name': 'Foo', 'online_time': 11, 'status': 'online'}
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
Built Distribution
File details
Details for the file AttrLinker-0.0.6.tar.gz
.
File metadata
- Download URL: AttrLinker-0.0.6.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d6a85f3ecf6a9acad9fe985eb15a0fb48637214e07ee38b5184a16b70b29272 |
|
MD5 | 3737dd9dd5ee0cf929910c6dab55fe86 |
|
BLAKE2b-256 | af1b935868c1c8ed2887ab06a52f63eb8d00a7a61eba2f7844858616a5b2a50f |
File details
Details for the file AttrLinker-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: AttrLinker-0.0.6-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8a79762e016b04353089bfefb5f037567b8ab2c6c52417b2344f198be82d88f |
|
MD5 | ed74267dda80bd309793067aa81b3121 |
|
BLAKE2b-256 | 72b72a4e3502e30d601745174b3ad1e22fcb363a23ee3c532ad71cdc5174e2b1 |