{ "info": { "author": "Deniz Bozyigit", "author_email": "deniz195@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "# Unitdoc\n\nUnitdoc is a Python library for dealing with data objects which describe physical objects with units and easy serialization. Let's look at an example. First, import unitdoc and create the registry that you will use in your application:\n```python\nfrom unitdoc import UnitDocRegistry\n\nudr = UnitDocRegistry()\n```\n\nLet's create a class that represents a battery\n```python\nimport attr\n\n@udr.serialize() \n@attr.s()\nclass Battery(object):\n name = attr.ib()\n\n weight = udr.attrib(default='45g')\n volume = udr.attrib(default='16ml', default_unit='ml')\n capacity = udr.attrib(default='3.0Ah')\n voltage = udr.attrib(default='3.6V', description ='Average voltage')\n```\n\nWe can create an instance of `Battery`, which will be a normal `attr` object (see [attrs library](https://github.com/python-attrs/attrs)), which features e.g. a nice `repr` function:\n```python\na_battery = Battery(name = 'battery', weight='43g')\nprint(a_battery)\n# outputs: Battery(name='battery', weight=, volume=, capacity=, voltage=)\n```\n\n\nWe can use the attributes of the battery in any operation that are allowed by the pint package:\n```python\nenergy = (a_battery.capacity * a_battery.voltage).to('Wh')\nenergy_density = (energy / a_battery.weight).to('Wh/kg')\nprint(f'{energy} @ {energy_density}')\n# outputs: 10.8 Wh @ 251.2 Wh / kg\n```\n\nNow let's save and reload our battery object:\n```python\n# look at serialized form\nprint(a_battery.serialize())\n\n# outputs:\nname: battery\nweight: !unit 43 g\nvolume: !unit 16 ml\ncapacity: !unit 3 Ah\nvoltage: !unit 3.6 V\n```\n\nThis can be easily saved to a file and reloaded again:\n```python\nfn = 'a_battery.yaml'\n# save to yaml file\nwith open(fn, 'w') as f:\n f.write(a_battery.serialize())\n\n# load from yaml file\nwith open(fn, 'r') as f:\n a_loaded_battery = Battery.deserialize(f.read())\n\nassert a_battery == a_loaded_battery \n```\n\n## More features\nUnitdoc facilitates certain operations, which can improve your code. \n\nIf you specify `default_unit` in an attribute, quantities are automatically normalized to that unit:\n```python\na_battery = Battery(name = 'battery', volume='15903 mm^3')\nprint(a_battery.volume)\n# outputs: 15.9 ml\n```\n\nIf a `default_unit` is specified, any incompatible unit will raise an exception:\n```python\nfrom unitdoc import DimensionalityError\n\ntry:\n a_battery = Battery(name = 'battery', volume='42 g')\nexcept DimensionalityError as e:\n print(e)\n# outputs: Cannot convert from 'gram' ([mass]) to 'milliliter' ([length] ** 3)\n```\n\nYou can retrieve description of parameters, for e.g. data representation code\n```python\nfrom unitdoc import get_attr_description\nprint(get_attr_description(a_battery.__class__, 'voltage'))\n# outputs: Average voltage\n```\n\n\n\n## Installation\n\nUse the package manager [pip](https://pip.pypa.io/en/stable/) to install unitdoc:\n\n```bash\npip install unitdoc\n```\n\nAlternatively, install the latest version from git:\n```bash\ngit clone https://github.com/deniz195/unitdoc\npython unitdoc/setup.py install --user\n```\n\n## Related packages\nUnitdoc is based on the following amazing packages:\n\n- [pint](https://pint.readthedocs.io/) deals with the units\n- [ruamel.yamls](https://yaml.readthedocs.io/en/latest/) deals with (de)serializing from semi-structured data (nested dictionaries)\n- [attrs](https://github.com/python-attrs/attrs) deals with the boilerplate of data classes\n- [cattr](https://github.com/Tinche/cattrs) deals with the unstructuring and restructuring of classes for (de)serialization\n\nThe UnitDocRegistry creates registries/converters/parsers for each package and aggregates them. You can leverage the features of each package:\n\nUse unit registry from pint:\n```python\nq = udr.ureg('1000gram').to('kg')\nprint(q)\n# outputs: 1 kg\n```\n\nUse yaml parser from ruaml.yaml:\n```python\nq_yaml = udr.yaml.dump(dict(weight=q))\nprint(q_yaml)\n# outputs: weight: !unit 1 kg\n```\n\nUse cattr converter:\n```python\n@udr.serialize() \n@attr.s()\nclass Thing(object):\n weight = udr.attrib(default='45g', description ='Total weight')\n\na_thing = Thing()\na_thing_dict = udr.cattr.unstructure(a_thing)\n\nassert type(a_thing_dict) == dict\nprint(a_thing_dict['weight'])\n# output: 45 g\n```\n\n## Restrictions\nGiven the restrictions of the attrs package, updating attributes safely requires certain precautions. E.g. given the `Battery` class from above the following is possible but not desirable\n```python\na_battery = Battery(name = 'battery')\na_battery.volume = 99\ntype(a_battery.volume)\n# outputs: int\n```\nThis is not desirable, because unit check and normalizatin is not performed. \n\nAn good way to avoid this (and other problems) is to use keyword only (`kw_only=True`) and frozen (`frozen=True`) `attr` objects. \n```python\n@udr.serialize() \n@attr.s(kw_only=True, frozen=True)\nclass BetterBattery(object):\n name = attr.ib()\n\n weight = udr.attrib(default='45g')\n volume = udr.attrib(default='16ml', default_unit='ml')\n capacity = udr.attrib(default='3.0Ah')\n voltage = udr.attrib(default='3.6V', description ='Average voltage')\n```\n\nThe keyword only restriction, will not allow the creation of objects from positional parameters, so that the following line fails with a Type error:\n```python\na_battery = BetterBattery('battery', '42g', '16ml') \n```\nThis is good, because positional arguments can be dangerous when data model changes over time. The following line creates a new object and is stable if the class changes\n```python\na_battery = BetterBattery(name='battery', weight='42g', volume='16ml') \n```\n\nThe frozen instance restriction does not allow to mutate an object, so that this line will fail with a `FrozenInstanceError`\n```python\na_battery.volume = 99 \n```\nTo update values, you can use the attr.evolve function, which creates a new object with the updated value\n```python\na_battery = attr.evolve(a_battery, volume='12cm^3')\n```\nIn this case unit conversion and checks are performed as expected.\n\nWhile unitdoc works with regular `attr` classes (`@attr.s()`), we strongly recommend using `@attr.s(kw_only=True, frozen=True)`.\n\n\n\n## Contributing\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n## License\n[MIT](https://choosealicense.com/licenses/mit/)\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/deniz195/unitdoc", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "unitdoc", "package_url": "https://pypi.org/project/unitdoc/", "platform": "", "project_url": "https://pypi.org/project/unitdoc/", "project_urls": { "Homepage": "https://github.com/deniz195/unitdoc" }, "release_url": "https://pypi.org/project/unitdoc/0.2.1/", "requires_dist": [ "attrs", "cattrs", "attr-descriptions (>=0.1.3)", "ruamel.yaml", "pint-mtools" ], "requires_python": "", "summary": "A framework that enables classes that describe physical objects with units and easy serialization.", "version": "0.2.1" }, "last_serial": 5694953, "releases": { "0.2.1": [ { "comment_text": "", "digests": { "md5": "621e1614b444ed4af7b361b1c0f33d5e", "sha256": "65936f806d50c419eb90e1b3a8f28b033912dd85806159a0dcb86ea5527f5713" }, "downloads": -1, "filename": "unitdoc-0.2.1-py3-none-any.whl", "has_sig": false, "md5_digest": "621e1614b444ed4af7b361b1c0f33d5e", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7502, "upload_time": "2019-08-18T15:31:44", "url": "https://files.pythonhosted.org/packages/b9/74/c87b9c4fec7e05919f8d33e5dc0be4deb70212efcb307e86b80f2055d32e/unitdoc-0.2.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6276d6d041bbf8b175bab0a06dacd5b4", "sha256": "5ee65f54f4f2475dd38af2cadbc2030a566054f813cc78d03634d44f339ebadc" }, "downloads": -1, "filename": "unitdoc-0.2.1.tar.gz", "has_sig": false, "md5_digest": "6276d6d041bbf8b175bab0a06dacd5b4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8519, "upload_time": "2019-08-18T15:31:46", "url": "https://files.pythonhosted.org/packages/74/19/2e2150b8074afd1b3343999b5773f991fdbd8106cfc96521044fa7307eee/unitdoc-0.2.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "621e1614b444ed4af7b361b1c0f33d5e", "sha256": "65936f806d50c419eb90e1b3a8f28b033912dd85806159a0dcb86ea5527f5713" }, "downloads": -1, "filename": "unitdoc-0.2.1-py3-none-any.whl", "has_sig": false, "md5_digest": "621e1614b444ed4af7b361b1c0f33d5e", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7502, "upload_time": "2019-08-18T15:31:44", "url": "https://files.pythonhosted.org/packages/b9/74/c87b9c4fec7e05919f8d33e5dc0be4deb70212efcb307e86b80f2055d32e/unitdoc-0.2.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6276d6d041bbf8b175bab0a06dacd5b4", "sha256": "5ee65f54f4f2475dd38af2cadbc2030a566054f813cc78d03634d44f339ebadc" }, "downloads": -1, "filename": "unitdoc-0.2.1.tar.gz", "has_sig": false, "md5_digest": "6276d6d041bbf8b175bab0a06dacd5b4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8519, "upload_time": "2019-08-18T15:31:46", "url": "https://files.pythonhosted.org/packages/74/19/2e2150b8074afd1b3343999b5773f991fdbd8106cfc96521044fa7307eee/unitdoc-0.2.1.tar.gz" } ] }