dol


Namedol JSON
Version 0.1.21 PyPI version JSON
download
home_pagehttps://github.com/i2mint/dol
SummaryBase builtin tools make and transform data object layers (dols).
upload_time2021-06-15 20:24:48
maintainer
docs_urlNone
authorOtoSense
requires_python
licensemit
keywords storage interface
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # dol
Base builtin tools make and transform data object layers (dols).

The package is light-weight: Pure python; not third-party dependencies.

Why the name?
- because it's short
- because it's cute
- because it reminds one of "russian dolls" (one way to think of wrappers)
- because we can come up with an acronym the contains "Data Object" in it. 

To install:	```pip install dol```

# Examples

By store we mean key-value store. This could be files in a filesystem, objects in s3, or a database. Where and
how the content is stored should be specified, but StoreInterface offers a dict-like interface to this.

    __getitem__ calls: _id_of_key			                    _obj_of_data
    __setitem__ calls: _id_of_key		        _data_of_obj
    __delitem__ calls: _id_of_key
    __iter__    calls:	            _key_of_id

```pydocstring
>>> from dol import Store
```

A Store can be instantiated with no arguments. By default it will make a dict and wrap that.

```pydocstring
>>> # Default store: no key or value conversion ################################################
>>> s = Store()
>>> s['foo'] = 33
>>> s['bar'] = 65
>>> assert list(s.items()) == [('foo', 33), ('bar', 65)]
>>> assert list(s.store.items()) == [('foo', 33), ('bar', 65)]  # see that the store contains the same thing
```

Now let's make stores that have a key and value conversion layer 
input keys will be upper cased, and output keys lower cased 
input values (assumed int) will be converted to ascii string, and visa versa 

```pydocstring
>>>
>>> def test_store(s):
...     s['foo'] = 33  # write 33 to 'foo'
...     assert 'foo' in s  # __contains__ works
...     assert 'no_such_key' not in s  # __nin__ works
...     s['bar'] = 65  # write 65 to 'bar'
...     assert len(s) == 2  # there are indeed two elements
...     assert list(s) == ['foo', 'bar']  # these are the keys
...     assert list(s.keys()) == ['foo', 'bar']  # the keys() method works!
...     assert list(s.values()) == [33, 65]  # the values() method works!
...     assert list(s.items()) == [('foo', 33), ('bar', 65)]  # these are the items
...     assert list(s.store.items()) == [('FOO', '!'), ('BAR', 'A')]  # but note the internal representation
...     assert s.get('foo') == 33  # the get method works
...     assert s.get('no_such_key', 'something') == 'something'  # return a default value
...     del(s['foo'])  # you can delete an item given its key
...     assert len(s) == 1  # see, only one item left!
...     assert list(s.items()) == [('bar', 65)]  # here it is
>>>
```

We can introduce this conversion layer in several ways. 

Here are few... 

## by subclassing
```pydocstring
>>> # by subclassing ###############################################################################
>>> class MyStore(Store):
...     def _id_of_key(self, k):
...         return k.upper()
...     def _key_of_id(self, _id):
...         return _id.lower()
...     def _data_of_obj(self, obj):
...         return chr(obj)
...     def _obj_of_data(self, data):
...         return ord(data)
>>> s = MyStore(store=dict())  # note that you don't need to specify dict(), since it's the default
>>> test_store(s)
>>>
```

## by assigning functions to converters

```pydocstring
>>> # by assigning functions to converters ##########################################################
>>> class MyStore(Store):
...     def __init__(self, store, _id_of_key, _key_of_id, _data_of_obj, _obj_of_data):
...         super().__init__(store)
...         self._id_of_key = _id_of_key
...         self._key_of_id = _key_of_id
...         self._data_of_obj = _data_of_obj
...         self._obj_of_data = _obj_of_data
...
>>> s = MyStore(dict(),
...             _id_of_key=lambda k: k.upper(),
...             _key_of_id=lambda _id: _id.lower(),
...             _data_of_obj=lambda obj: chr(obj),
...             _obj_of_data=lambda data: ord(data))
>>> test_store(s)
>>>
```

## using a Mixin class

```pydocstring
>>> # using a Mixin class #############################################################################
>>> class Mixin:
...     def _id_of_key(self, k):
...         return k.upper()
...     def _key_of_id(self, _id):
...         return _id.lower()
...     def _data_of_obj(self, obj):
...         return chr(obj)
...     def _obj_of_data(self, data):
...         return ord(data)
...
>>> class MyStore(Mixin, Store):  # note that the Mixin must come before Store in the mro
...     pass
...
>>> s = MyStore()  # no dict()? No, because default anyway
>>> test_store(s)
```

## adding wrapper methods to an already made Store instance

```pydocstring
>>> # adding wrapper methods to an already made Store instance #########################################
>>> s = Store(dict())
>>> s._id_of_key=lambda k: k.upper()
>>> s._key_of_id=lambda _id: _id.lower()
>>> s._data_of_obj=lambda obj: chr(obj)
>>> s._obj_of_data=lambda data: ord(data)
>>> test_store(s)
```
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/i2mint/dol",
    "name": "dol",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "storage,interface",
    "author": "OtoSense",
    "author_email": "thorwhalen1@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/20/b3/92e6b3e8a548101184e12f03e70f485bae55bea148be64162ccb643ebbf5/dol-0.1.21.tar.gz",
    "platform": "any",
    "description": "# dol\nBase builtin tools make and transform data object layers (dols).\n\nThe package is light-weight: Pure python; not third-party dependencies.\n\nWhy the name?\n- because it's short\n- because it's cute\n- because it reminds one of \"russian dolls\" (one way to think of wrappers)\n- because we can come up with an acronym the contains \"Data Object\" in it. \n\nTo install:\t```pip install dol```\n\n# Examples\n\nBy store we mean key-value store. This could be files in a filesystem, objects in s3, or a database. Where and\nhow the content is stored should be specified, but StoreInterface offers a dict-like interface to this.\n\n    __getitem__ calls: _id_of_key\t\t\t                    _obj_of_data\n    __setitem__ calls: _id_of_key\t\t        _data_of_obj\n    __delitem__ calls: _id_of_key\n    __iter__    calls:\t            _key_of_id\n\n```pydocstring\n>>> from dol import Store\n```\n\nA Store can be instantiated with no arguments. By default it will make a dict and wrap that.\n\n```pydocstring\n>>> # Default store: no key or value conversion ################################################\n>>> s = Store()\n>>> s['foo'] = 33\n>>> s['bar'] = 65\n>>> assert list(s.items()) == [('foo', 33), ('bar', 65)]\n>>> assert list(s.store.items()) == [('foo', 33), ('bar', 65)]  # see that the store contains the same thing\n```\n\nNow let's make stores that have a key and value conversion layer \ninput keys will be upper cased, and output keys lower cased \ninput values (assumed int) will be converted to ascii string, and visa versa \n\n```pydocstring\n>>>\n>>> def test_store(s):\n...     s['foo'] = 33  # write 33 to 'foo'\n...     assert 'foo' in s  # __contains__ works\n...     assert 'no_such_key' not in s  # __nin__ works\n...     s['bar'] = 65  # write 65 to 'bar'\n...     assert len(s) == 2  # there are indeed two elements\n...     assert list(s) == ['foo', 'bar']  # these are the keys\n...     assert list(s.keys()) == ['foo', 'bar']  # the keys() method works!\n...     assert list(s.values()) == [33, 65]  # the values() method works!\n...     assert list(s.items()) == [('foo', 33), ('bar', 65)]  # these are the items\n...     assert list(s.store.items()) == [('FOO', '!'), ('BAR', 'A')]  # but note the internal representation\n...     assert s.get('foo') == 33  # the get method works\n...     assert s.get('no_such_key', 'something') == 'something'  # return a default value\n...     del(s['foo'])  # you can delete an item given its key\n...     assert len(s) == 1  # see, only one item left!\n...     assert list(s.items()) == [('bar', 65)]  # here it is\n>>>\n```\n\nWe can introduce this conversion layer in several ways. \n\nHere are few... \n\n## by subclassing\n```pydocstring\n>>> # by subclassing ###############################################################################\n>>> class MyStore(Store):\n...     def _id_of_key(self, k):\n...         return k.upper()\n...     def _key_of_id(self, _id):\n...         return _id.lower()\n...     def _data_of_obj(self, obj):\n...         return chr(obj)\n...     def _obj_of_data(self, data):\n...         return ord(data)\n>>> s = MyStore(store=dict())  # note that you don't need to specify dict(), since it's the default\n>>> test_store(s)\n>>>\n```\n\n## by assigning functions to converters\n\n```pydocstring\n>>> # by assigning functions to converters ##########################################################\n>>> class MyStore(Store):\n...     def __init__(self, store, _id_of_key, _key_of_id, _data_of_obj, _obj_of_data):\n...         super().__init__(store)\n...         self._id_of_key = _id_of_key\n...         self._key_of_id = _key_of_id\n...         self._data_of_obj = _data_of_obj\n...         self._obj_of_data = _obj_of_data\n...\n>>> s = MyStore(dict(),\n...             _id_of_key=lambda k: k.upper(),\n...             _key_of_id=lambda _id: _id.lower(),\n...             _data_of_obj=lambda obj: chr(obj),\n...             _obj_of_data=lambda data: ord(data))\n>>> test_store(s)\n>>>\n```\n\n## using a Mixin class\n\n```pydocstring\n>>> # using a Mixin class #############################################################################\n>>> class Mixin:\n...     def _id_of_key(self, k):\n...         return k.upper()\n...     def _key_of_id(self, _id):\n...         return _id.lower()\n...     def _data_of_obj(self, obj):\n...         return chr(obj)\n...     def _obj_of_data(self, data):\n...         return ord(data)\n...\n>>> class MyStore(Mixin, Store):  # note that the Mixin must come before Store in the mro\n...     pass\n...\n>>> s = MyStore()  # no dict()? No, because default anyway\n>>> test_store(s)\n```\n\n## adding wrapper methods to an already made Store instance\n\n```pydocstring\n>>> # adding wrapper methods to an already made Store instance #########################################\n>>> s = Store(dict())\n>>> s._id_of_key=lambda k: k.upper()\n>>> s._key_of_id=lambda _id: _id.lower()\n>>> s._data_of_obj=lambda obj: chr(obj)\n>>> s._obj_of_data=lambda data: ord(data)\n>>> test_store(s)\n```",
    "bugtrack_url": null,
    "license": "mit",
    "summary": "Base builtin tools make and transform data object layers (dols).",
    "version": "0.1.21",
    "split_keywords": [
        "storage",
        "interface"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "dc58265800447358bd95de9f805c200e",
                "sha256": "dea7d2fc10e17e58cf9fa03671d820da4d3f7a0fb287ffd67d046c510d764e34"
            },
            "downloads": -1,
            "filename": "dol-0.1.21.tar.gz",
            "has_sig": false,
            "md5_digest": "dc58265800447358bd95de9f805c200e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 107480,
            "upload_time": "2021-06-15T20:24:48",
            "upload_time_iso_8601": "2021-06-15T20:24:48.137736Z",
            "url": "https://files.pythonhosted.org/packages/20/b3/92e6b3e8a548101184e12f03e70f485bae55bea148be64162ccb643ebbf5/dol-0.1.21.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-06-15 20:24:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": null,
    "github_project": "i2mint",
    "error": "Could not fetch GitHub repository",
    "lcname": "dol"
}
        
Elapsed time: 0.33598s