Tree @52c3784 (Download .tar.gz)
README.rst @52c3784 — view markup · raw · history · blame
Python Thesaurus and ThesaurusCfg
Copyright (c) 2012-2019 Dave Cinege. All rights reserved.
See the end of this file for further copyright and license information
$ python thesauruscfg_sample.py
Then review thesauruscfg_sample.py and thesauruscfg_sample.cfg and you will get an idea what Thesaurus and ThesaurusCfg are all about.
At the momement there is no email list, wiki, etc.
Thesaurus is a mapping data type with key recursion and attribute aliasing. It is a subclass of dict() and compatible as a dictionary replacement baring where key path recursion may take place.
Thesaurus prefers to be imported and called as thes() in the same way you would use dict(). Thesaurus likes to think of itself as a Python data type primative that should be used along side of dict(), similar to the relationship between list() and tuple(); they are overlappingly similar but serve different purposes and diverge to incompatibilities. Thesaurus is currently content with not having it's own data type tokens. Hopefully so are you.
ThesaurusExtended is a subclass of Thesaurus providing additional usablity methods.
ThesaurusCfg is a subclass of ThesaurusExtended providing a configuration file parser and per key data coercion methods.
The Thesaurus family works with Python 2.6+ to 3.8+.
Currect State of Code - 2019-11-13
Thesaurus first came about in December 2012. It has gone through a total of maybe 6 complete re-writes. In mid 2019 I think I finally got it right with the implemintation of Keypath's and also the creation of ThesaurusCfg, something I've been wishing I've had to use for myself for at least the last 4 years.
The current version you will find here has recently had the recent addition of Keypaths and while it is quite usable might still have a few bugs to iron out.
Additonally parts should be cleaned and reimplemented now that Keypath's have been implemented. If you are reviewing the code, this will likely stand out. With that said, please remember that at this stage few things have been done by accident. There are certainly section that can be cleaned for readablity, however they are this way, on purpose, for performance.
ThesaurusCfg is quite new. It works. I have a version of it frozen in production code. But it's not had the maturity of Thesaurus in practise and requires a more thorough re-write.
I'm putting this code out now to gain feedback to finalize for a proper release. As such, both modules are very subject to change at this time. Specifically I have the following decisions to make:
Finalize name conventions. Use set_path() or setpath()?
Finalize specialtiy method names, Are merge(), mesh() and screen() good names that make sense? I am struggling with the ThesaurusExtended method names myself.
Resolve Thesaurus's schizophrenia :: v = t['a.b.c'] # This recurses t['a.b.c'] = v # This does not. Use set_path()
I want to be comfortable this feels naturale to others or make changes.
Review how I do recursive copy/deepcopys.
Decide how to properly handle copying ThesaurusCfg coercion methods.
Copyright and License Information
Copyright (c) 2012 - 2019 Dave Cinege. All rights reserved.
See the file "LICENSE" for information on the history of this software, terms & conditions for usage, and a DISCLAIMER OF ALL WARRANTIES.
BELOW IS OLD/IN PROGRESS - NOT COHERENT
- What is attribute alaising?
Attribute alaising refering to redirecting the methods __getattr__() and__setattr__() to __getitem__() and __setitem__() respectivly.
The effect is to syntatically reference keys like attributes.
For example: .. code::python t['k'] t.k
are treated the same.
Thesaurus also provides a special alias
What is key recursion?
For example: .. code:: python t = thes() t.set_path('k._k.__k.___k', ','Hello') t['k']['_k']['__k']['___k'] t.k._k.__k.___k
Becasue of the nature pof Thesaurus, keys shodul be of trype str()
, difference being the latter will recurse or 'walk the key directory tree' until
What is a keypath? A keypath is a It may be represented by a string with path elements seperated by dots. t['k']
Nested dictionarys which I refer to as dictionary trees (aka nested dictionaries.)
from thesaurus import thes from thesaurus import thesext as thes
# OLD README Thesaurus is a pure dictionary subclass which allows calling keys as if they are class attributes and will search through nested objects recursively when __getitem__ is called.
You will notice that the code is very compact. However I have found that this has completely changed the way I program in Python. I've re-written some exiting programs using Thesaurus, and often realized 15-30% code reduction. Additionally I find the new code much easier to read.
If you find yourself programing with nested dictionaries often, fighting to generate output or command lines for external programs, or wish you had a dictionary that could act (sort of) like a class, Thesaurus may be for you.