# -*- coding: utf-8 -*-
from setuptools import setup

package_dir = \
{'': 'src'}

packages = \
['betterdicts']

package_data = \
{'': ['*']}

setup_kwargs = {
    'name': 'betterdicts',
    'version': '0.5.0',
    'description': 'Better dictionary types.',
    'long_description': '# betterdicts\n\nA collection of dictionary and dictionary-like utility classes.\n\nThe base type is `betterdict`, which only seeks to improve on the built-in\n`dict` type by providing some extra functionality.\n\nOn top of this several other useful dictionaries have been built:\n\n- `stack_dict`: a multi-levelled dict whose state can be pushed and popped like\n  a stack.\n- `attr_dict` and `jsdict`: ergonomic dictionaries where attribute access\n  (`xs.key`) can be used in place of the usual square bracket syntax\n  (`xs["key"]`).\n- `persistent_dict`: a dict which automatically saves itself to disk with every\n  change, so it retains its state between script execution.\n- `dynamic_dict` and `cache_dict`: dictionaries that represent or act like a\n  memoized function.\n- `number_dict`: a dict on which arithmetic can be performed. Think of it as a\n  simple dict version of a NumPy array.\n\n\n## betterdict\n\n``` python\nfrom betterdicts import betterdict\n```\n\nIntended to work just like a dict, but with extra functionality and convenience.\n\nMost of these things can be achieved with simple generator expressions or\nsimilar, but having need of them constantly it got to a boiling point.\n\n### Increased Functional Friendliness\n\n`obj.update()` and `obj.clear()` now return `obj` instead of `None`, so they are\nno longer _as_ useless.\n\n```python-console\n>>> d = betterdict()\n>>> d.update(a=1).update(zip(\'abc\', [7,4,2]))\n{\'a\': 7, \'b\': 4, \'c\': 2}\n```\n\nThere\'s also `betterdict.insert(key, value, [missing])` as an alternative to\n`obj[key] = value`. It returns the previous value (if present).\n\n``` python-console\n>>> d.insert(\'b\', 69)\n4\n>>> d.insert(\'d\', 0)\n>>> d.insert(\'e\', 0, \'default return value goes here\')\n\'default return value goes here\'\n>>> d\n{\'a\': 7, \'b\': 69, \'c\': 2, \'d\': 0, \'e\': 0}\n```\n\nPython being a statement-oriented language rather than expression-oriented can\nsometimes get in the way when you want to do simple operations inside of a list\ncomprehension or test. These methods are intended to temper this problem.\n\n``` python-console\n>>> d = betterdict()\n>>> [d.insert(len(w), w) for w in \'fare thee well great heart\'.split()]\n[None, \'fare\', \'thee\', None, \'great\']\n>>> d\n{4: \'well\', 5: \'heart\'}\n```\n\nNote that `del obj[x]` (a good example of Python\'s statement fanaticism) already\nhas an expression alternative in `dict.pop(x, [default])`.\n\nThe annoyance of statements led to the infamous walrus wart`^W`operator being\nadded after several bikeshed bonfires, finally admitting that\nexpression-oriented language design is _objectively superior_. [TODO: add\ntongue-in-cheek emoji here.]\n\n### Combining and Collecting\n  \nOften you\'re collecting things into dictionaries where the key you\'re interested\nin is not unique. Or simiarly, if given a stream of keyed data, you usually need\nsome extra logic to handle identical keys.\n\nOne way to handle this is to write out some loop and add some\ntedious-but-mandatory `if` branch in the loop. These loops start to feel like\nChinese water torture when they number in the thousands.\n\nThe default behavior of `dict` if you just give it an iterator is to discard\n(overwrite). That default behavior has been kept, but alternatives have been\nprovided.\n\n- `betterdict.combine(func, it, [initial=...])`\n\nThis works like the built-in `reduce(func, it, [initial])`. It works well when\nyou\'re dealing with simple data like numbers and you just want a single answer.\n\n``` python-console\n>>> betterdict.combine(int.__add__, [(\'a\',1), (\'a\',5), (\'b\',2), (\'a\',7)])\n{\'a\': 13, \'b\': 2}\n```\n\nIt\'s short-hand for two kinds of loops:\n\n``` python-console\n# with a specified initial value\nfor k, v in source:\n  d[k] = func(d.get(k, initial), v)\n\n# without\nfor k, v in source:\n  if k in d:\n    d[k] = func(d[k], v)\n  else:\n    d[k] = v\n```\n\nThis _can_ be used to put values into collections like in a functional language, as the following example:\n\n``` python-console\n>>> betterdict.combine(lambda x, y: x + (y,), [(\'a\',1), (\'a\',5), (\'b\',2), (\'a\',7)], initial=())\n{\'a\': (1, 5, 7), \'b\': (2,)}\n```\n\nBut it\'s very inefficient and painful to use with mutable containers like lists.\nThis is where `collect()` comes in:\n\n- `betterdict.collect(type, it, [add_action=...])`\n\nThis will return a dictionary-of-collections, where the collection is created\nwith `type` and added with `add_action` (which defaults to being auto-detected\nas something like `list.append`, `set.add`, etc.).\n\n``` python-console\n>>> betterdict.collect(list, [(\'a\',1), (\'a\',5), (\'b\',2), (\'a\',7)])\n{\'a\': [1, 5, 7], \'b\': [2]}\n```\n\nA more realistic example:\n\n``` python-console\n>>> some_stream_of_words = open(\'README.md\', \'r\', encoding=\'utf-8\').read().split()\n>>> l2w = betterdict.collect(set, ((len(w), w) for w in some_stream_of_words))\n>>> l2w[2]\n{\'{}\', \'in\', \'of\', \'be\', \'up\', \'(a\', \'-1\', \'is\', \'no\', \'4,\', \'6}\', \'to\', \'if\', \'as\', \'v)\', \'1,\', \'38\', \'"\\\'\', \'6:\', \'As\', \'-c\', \'2,\', \'w)\', \'8:\', \'or\', \'v:\', \'2}\', \'2:\', \'on\', \'10\', \'==\', \'x,\', \'7,\', \'0}\', \'-l\', \'4}\', "\',", \'k:\', \'do\', \'So\', \'it\', \'4:\', \'d:\', \'9,\', \'3)\', \'at\', \'7:\', \'Or\', \'3:\', \'40\', \'5:\', \'##\', \'ad\', \'^D\', \'an\', \'20\', \'9:\', \'ls\', "T\'", \'p2\', \'y:\', \'1}\', \'It\', \'To\', \'so\', \'1)\', \'17\', \'0)\', \'by\', \'k,\', \'3,\', \'//\', \'x:\', \'0,\', \'26\', \'5,\', \'+=\', \'p1\', \'me\', \'1:\'}\n```\n\nThese functions are magic in that they can work both as class methods and as\ninstance methods. The instance methods `obj.collect(...)` and `obj.combine(...)`\nworks kind of like a combining or collecting `obj.update()`. The only exception\nis that `obj.combine()` works immutably by returning a new dictionary as if it\nwas a binary operator.\n\nSee `help(betterdict.combine)` and `help(betterdict.collect)` for more\ninformation.\n\n### Filtering and Mapping\n\n`.map_keys(f)`, `.map_values(f)`, `.map_pairs(f)`, `.filter_keys(p)`,\n`.filter_values(p)`, `.filter_pairs(p)` all do the somewhat obvious thing.\n\n``` python-console\n>>> q = betterdict(enumerate("I\'m nothing but heart"))\n>>> q.filter_keys(lambda x: 4 < x < 10)\n{5: \'o\', 6: \'t\', 7: \'h\', 8: \'i\', 9: \'n\'}\n>>> q.filter_values(str.isupper)\n{0: \'I\'}\n```\n\nThough it should be noted that unlike `.update` they do not modify the\ndictionary in-place as they\'re targeting more functional programming.\n\n`map_pairs(f)` and `filter_pairs(f)` take a function of two arguments.\n\n``` python-console\n>>> q.map_pairs(lambda x, y: (x, x * y))\n{0: \'\', 1: "\'", 2: \'mm\', 3: \'   \', 4: \'nnnn\', 5: \'\', 6: \'t\', 7: \'hh\', 8: \'iii\', 9: \'nnnn\', 10: \'\', 11: \' \', 12: \'bb\', 13: \'uuu\', 14: \'tttt\', 15: \'\', 16: \'h\', 17: \'ee\', 18: \'aaa\', 19: \'rrrr\', 20: \'\'}\n```\n\nSince `map_keys()` and `map_pairs()` might map two values to the same key, they\nalso take a function and initial value as optional arguments to do a reduction,\nsimilar to how the built-in `reduce()` works. (Refer to `.combine()`.)\n\n``` python-console\n>>> q.map_keys(lambda x: x%2, str.__add__)\n{0: \'Imntigbthat\', 1: "\' ohn u er"} \n```\n\nThere\'s also `.filter([keys=f], [values=g])` and `.map([keys=f], [values=g])`\nwhich can be used to filter/map both keys and values with two different\nfunctions in one step:\n\n``` python-console\n>>> q.filter(lambda k: 4 < k < 10, lambda v: v in \'aeiouy\')\n{5: \'o\', 8: \'i\'}\n```\n\n### Inversion\n\nFlipping those arrows.\n\n```python-console\n>>> q = betterdict(enumerate(\'divebar\'))\n>>> q\n{0: \'d\', 1: \'i\', 2: \'v\', 3: \'e\', 4: \'b\', 5: \'a\', 6: \'r\'}\n>>> q.invert()\n{\'d\': 0, \'i\': 1, \'v\': 2, \'e\': 3, \'b\': 4, \'a\': 5, \'r\': 6}\n>>> q == q.invert().invert()\n```\n\nNote that `q.invert()` does _not_ modify `q`, but returns a new dictionary.\n(Functional friendliness where it makes sense.)\n\nMost of the time the map is not expected to be injective (one-to-one) though,\nand there are two ways of handling that:\n\n- you can either _collect_ values into a container like a list, or\n\n``` python-console\n>>> q = betterdict(enumerate(\'syzygy\'))\n>>> q.invert()\n{\'s\': 0, \'y\': 5, \'z\': 2, \'g\': 4}\n>>> q.invert_and_collect(list)\n{\'s\': [0], \'y\': [1, 3, 5], \'z\': [2], \'g\': [4]}\n>>> q.invert_and_collect(set)\n{\'s\': {0}, \'y\': {1, 3, 5}, \'z\': {2}, \'g\': {4}}\n```\n\n- you can _combine_ values repeatedly until you get a single answer.\n\n```python-console\n>>> q.invert_and_combine(int.__mul__)\n{\'s\': 0, \'y\': 15, \'z\': 2, \'g\': 4}\n>>> q.invert_and_combine(lambda x, y: x+[y], [])\n{\'s\': [0], \'y\': [1, 3, 5], \'z\': [2], \'g\': [4]}\n```\n\nAs seen above, collecting and combining are sometimes just special cases of a\nmore general operation (folding, repeated applicaitino of a monoid, etc). More\nfunctional languages usually only has one operation for this, but I chose to\nseparate them because they have very a different feel in in Python, with very\ndifferent performance characteristics.\n\n\n## jsdict (ad hoc use only)\n\n``` python\nfrom betterdicts import jsdict, njsdict, rjsdict\n```\n\n`jsdict` is a `betterdict`s which works like JavaScript object, where keys and\nattributes are the same. This is accomplished with zero overhead.\n\n``` python-console\n>>> d = jsdict()\n>>> d[\'hello\'] = 1\n>>> d.filepath = \'/\'\n>>> d\n{\'hello\': 1, \'filepath\': \'/\'}\n>>> d.hello, d[\'filepath\']\n(1, \'/\')\n```\n\nFor _obvious reasons_, this is a touch insane. JavaScript was never accused of\ngood design, and bringing it to Python where conventions are different will lead\nto awful things:\n\n``` python-console\n>>> d = jsdict(clear=0)\n>>> d.clear()\nTraceback (most recent call last):\n  File "<stdin>", line 1, in <module>\nTypeError: \'int\' object is not callable\n```\n\nThus these are for _ad hoc_ use only, i.e. in one-off scripts or when doging\nwork directly in the REPL.\n\nRefer to `attr_dict` below for a slightly safer alternative.\n\nBut sometimes this kind of convenience is just too good to pass up. You\'ll know\nthis in your heart too, if you\'ve ever dealt with loading configuration from\nsome source that gives you a `dict`, and then having to type out `[\'...\']` so\nmany times you actually get angry.\n\n``` python-console\n>>> import json\n>>> config = json.loads(\'{"lazy": "sarah", "active": false}\', object_pairs_hook=jsdict)\n>>> config.active\nFalse\n>>> config\n{\'lazy\': \'sarah\', \'active\': False}\n```\n\nI get equally angry whenever a source gives me some object of a godforsaken\nabstract `ConfigurationProxyHelper` class which is just a glorified `dict`\nwithout any of the functionality or compatibility.\n\n- `rjsdict` is a `jsdict` where `obj.key` for a missing key automatically\n  returns and inserts a new `rjsdict` instance. Think of it as a recursive\n  `jsdict`. It\'s useful for when you\'re building some hierarchical structure:\n\n``` python-console\n >>> config = rjsdict()\n>>> config.lights = \'off\'\n>>> config.player.health = 40\n>>> config.player.name = \'Mr. T\'\n>>> config\n{\'lights\': \'off\', \'player\': {\'health\': 40, \'name\': \'Mr. T\'}}\n```\n\n- `njsdict` is a `jsdict` where `obj.key` evaluates to `None` instead of raising\n  `AttributeError`. (`obj` is not modified.)\n\n### attr_dict\n\n``` python\nfrom betterdicts import attr_dict\n```\n\nInspired by `jsdict` above, it works in much the same way (though different\nmechanism). It is provided as a slightly safer alternative, because it doesn\'t\noverwrite extant attributes:\n\n``` python-console\n>>> q = attr_dict(a=10)\n>>> q.a\n10\n>>> q.b = 20\n>>> q\n{\'a\': 10, \'b\': 20}\n>>> q.pop = -1\n>>> q\n{\'a\': 10, \'b\': 20, \'pop\': -1}\n>>> q.pop\n<built-in method pop of attr_dict object at 0x7fc85a580c40>\n```\n\nBut note it is still has the glow of insanity, which will inspire nothing but\nanger, fear, and frustration.\n\n## dynamic_dict\n\n``` python\nfrom betterdicts import dynamic_dict, cache_dict\n```\n\nHow many times have you been frustrated by the fact that the standard\n`collections.defaultdict()` calls its factory function without providing the key\ncontext?\n\nYeah, me too.\n\n\n`dynamic_dict(f)` is equivalent to a `betterdict` but if a missing key is requested\nit is first created with `f(key)` and inserted.\n\n``` python-console\n>>> d = dynamic_dict(hex)\n>>> d[16]\n\'0x10\'\n>>> d[100]\n\'0x64\'\n>>> d\n{16: \'0x10\', 100: \'0x64\'}\n```\n\n### cache_dict\n\nHow many times have you wanted concrete access to the function cache when using\nsomething like `functools.cache`?\n\nYeah, me too.\n\nIntended to be used as a decorator on a function, this will turn the function\ninto a callable dictionary. The dictionary is its own cache and can freely be\ninspected, modified, etc.\n\n``` python\n@cache_dict\ndef heavy_bite(n):\n  "Calculates a heavy bite."\n  if n < 1: return 1\n  print(f\'calculating heavy bite {n}...\')\n  return heavy_bite(n // 3) * heavy_bite(n - 3 + (-n % 3)) + heavy_bite(n - 1)\n```\n\n``` python-console\n>>> heavy_bite(10)\ncalculating heavy bite 10...\ncalculating heavy bite 3...\ncalculating heavy bite 1...\ncalculating heavy bite 2...\ncalculating heavy bite 9...\ncalculating heavy bite 6...\ncalculating heavy bite 5...\ncalculating heavy bite 4...\ncalculating heavy bite 8...\ncalculating heavy bite 7...\n2880\n>>> heavy_bite(10)\n2880\n>>> del heavy_bite[10]\n>>> heavy_bite(10)\ncalculating heavy bite 10...\n2880\n>>> heavy_bite.__doc__\n\'Calculates a heavy bite.\'\n```\n\nNOTE: per now it only caches on its first argument. The rest are just passed\nthrough (in case of a cache miss). Ideally I want a way to specify an argument\nsignature with indication of which ones constitutes the key.\n\n## number_dict\n\nActs like a `collections.Counter()` with arithmetic support like a number.\n\nThis is sort of a poor man\'s `numpy-dict`.\n\n``` python-console\n>>> q = number_dict(range(5))\n>>> q\n{0: 1, 1: 1, 2: 1, 3: 1, 4: 1}\n>>> q[1] += 5\n>>> q[4] += 1\n>>> q[9]\n0\n>>> q[9] = 9\n>>> (q+1)**2\n{0: 4, 1: 49, 2: 4, 3: 4, 4: 9, 9: 100}\n>>> 1 / q\n{0: 1.0, 1: 0.16666666666666666, 2: 1.0, 3: 1.0, 4: 0.5, 9: 0.1111111111111111}\n```\n\n## persistent_dict\n\nThe _simplest possible_ persistent state exposed as a `betterdict`. Also\nintended for ad hoc use.\n\nThis is for when you need something really simple and magic to store some flat\ndata between script invocations, without the extra management of a database or\nfile formats.\n\n``` bash\n[franksh@moso ~]$ python -iq -c \'from betterdicts import persistent_dict\'\n>>> p = persistent_dict()\n>>> p\n{}\n>>> p[\'foo\'] = 17\n>>> p\n{\'foo\': 17}\n>>> persistent_dict()\n{\'foo\': 17}\n>>> p[\'bar\'] = [1,2]\n>>> persistent_dict()\n{\'foo\': 17, \'bar\': [1, 2]}\n>>> ^D\n[franksh@moso ~]$ ls -l cache.pickle \n-rw-r--r-- 1 franksh franksh 38 Aug 26 17:18 cache.pickle\n[franksh@moso ~]$ python -iq -c \'from betterdicts import persistent_dict\'\n>>> persistent_dict()\n{\'foo\': 17, \'bar\': [1, 2]}\n>>> \n```\n\nIt defaults to loading and saving from `./cache.pickle` in whatever the current\nworking directory happens to be, as seen above.\n\n_Any change made directly[^1] to the dictionary causes it to save itself to disk\nas a pickle file._ So obviously if you\'re building up some initial data quickly,\nyou want to use another dictionary first, and then convert it later with\n`persistent_dict([filename], data)`. This will automatically both load and save.\n\nTo use a custom filename use `persisent_dict([filename])`:\n\n``` python-console\n[franksh@moso ~]$ python -iq -c \'from betterdicts import persistent_dict\'\n>>> p1 = persistent_dict(\'foo.p\', dict(a=1,b=22,c=333))\n>>> p2 = persistent_dict(\'bar.p\', p1.invert())\nnot pers ({1: \'a\', 22: \'b\', 333: \'c\'},) {}\n>>> persistent_dict(\'foo.p\')\n{\'a\': 1, \'b\': 22, \'c\': 333}\n>>> persistent_dict(\'bar.p\')\n{1: \'a\', 22: \'b\', 333: \'c\'}\n>>> del p2[22]\n>>> p2 == persistent_dict(\'bar.p\')\nTrue\n```\n\n*Warning:* Two separate `persistent_dict()` objects bound to the same file will\nnot automatically stay in sync, instead they will keep overwriting each other\'s\ndata!\n\n## stack_dict\n\nStack dicts emulate how scopes or namespaces work. It allows you to repeatedly\nsave the state of the dictonary (`push_stack()`) and later retore it\n(`pop_stack()`).\n\n``` python-console\n>>> from betterdicts import stack_dict\n>>> q=stack_dict(a=1,b=2)\n>>> q\n{\'a\': 1, \'b\': 2}\n>>> q.push_stack()\n>>> q\n{\'a\': 1, \'b\': 2}\n>>> q[\'c\'] = 7\n>>> del q[\'a\']\n>>> q\n{\'b\': 2, \'c\': 7}\n>>> q.pop_stack()\n>>> q\n{\'a\': 1, \'b\': 2}\n>>> q.push_stack(hello=0, world=-1)  # push_stack works like update()\n>>> q.push_stack(hello=1000, world=1000)\n>>> q\n{\'a\': 1, \'b\': 2, \'hello\': 1000, \'world\': 1000}\n>>> q.pop_stack(); q\n{\'a\': 1, \'b\': 2, \'hello\': 0, \'world\': -1}\n>>> q.pop_stack(); q\n{\'a\': 1, \'b\': 2}\n```\n\nStack dicts can also be used with `with`-blocks:\n\n``` python-console\n>>> from betterdicts import stack_dict\n>>> q = stack_dict({\'a\': 1, \'b\': 2})\n>>> with q:\n...   q[\'c\'] = 10\n...   print(q)\n... \n{\'a\': 1, \'b\': 2, \'c\': 10}\n>>> q  # q is reset to its previous state after the `with`.\n{\'a\': 1, \'b\': 2}\n```\n\n[^1]: "deep" changes, like modifying a mutable object in the dictionary are not detected\n',
    'author': 'Frank S. Hestvik',
    'author_email': 'tristesse@gmail.com',
    'maintainer': 'None',
    'maintainer_email': 'None',
    'url': 'https://gitlab.com/franksh/betterdicts',
    'package_dir': package_dir,
    'packages': packages,
    'package_data': package_data,
    'python_requires': '>=3.8,<4.0',
}


setup(**setup_kwargs)
