{ "info": { "author": "", "author_email": "", "bugtrack_url": null, "classifiers": [], "description": "# parserutils\n\n[![Build Status](https://travis-ci.org/consbio/parserutils.png?branch=master)](https://travis-ci.org/consbio/parserutils)[![Coverage Status](https://coveralls.io/repos/github/consbio/parserutils/badge.svg?branch=master)](https://coveralls.io/github/consbio/parserutils?branch=master)\n\nThis is a library of utility functions designed to make a developer's life easier.\n\nThe functions in this library are written to be both performant and Pythonic, as well as compatible with Python 2.7 through 3.6.\nThey are both documented and covered thoroughly by unit tests that fully describe and prove their behavior.\n\nIn general, my philosophy is that utility functions should be fast and handle edge cases so the caller doesn't have to take all kinds of precautions or do type checking on results.\nThus, in this library, if None will break a function it is simply returned as is; if there's nothing to do for a value, the result is returned without processing; otherwise, values are either processed successfully or a standard exception is returned.\n\nBut this is just a starting point. I welcome feedback and requests for additional functionality.\n\n\n## Installation\nInstall with `pip install parserutils`.\n\n## Usage\n\nHere's what you can do with `dict` objects and other collections.\n```python\nfrom parserutils import collections\n\ncollections.accumulate_items([('key', 'val1'), ('key', 'val2'), ('key', 'val3')]) # {'key': ['val1', 'val2', 'val3']}\ncollections.accumulate_items(\n [('key1', 'val1'), ('key2', 'val2'), ('key3', 'val3')], reduce_each=True # {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}\n)\n\ncollections.setdefaults({}, 'a.b') # {'a': {'b': None}}\ncollections.setdefaults({}, ['a.b', 'a.c']) # {'a': {'b': None, 'c': None}}\ncollections.setdefaults({}, {'a.b': 'bbb', 'a.c': 'ccc'}) # {'a': {'b': 'bbb', 'c': 'ccc'}}\n\ncollections.filter_empty(x for x in (None, [], ['a'], '', {'b'}, 'c')) # [['a'], {'b'}, 'c']\ncollections.flatten_items(x for x in ('abc', ['a', 'b', 'c'], ('d', 'e'))) # ['abc', 'a', 'b', 'c', 'd', 'e']\n\ncollections.remove_duplicates('abcdefabc') # 'abcdef'\ncollections.remove_duplicates('abcdefabc', in_reverse=True) # 'defabc'\ncollections.remove_duplicates(['a', 'b', 'c', 'a']) # ['a', 'b', 'c']\ncollections.remove_duplicates(('a', 'b', 'c', 'a'), in_reverse=True) # ('b', 'c', 'a')\ncollections.remove_duplicates(x for x in 'abca') # ['a', 'b', 'c']\ncollections.remove_duplicates((x for x in 'abca'), in_reverse=True) # ['b', 'c', 'a']\ncollections.remove_duplicates((set(x) for x in 'abca'), is_hashable=True) # [{'a'}, {'b'}, {'c'}]\n\ncollections.rindex('aba', 'a') # 2\ncollections.rindex(['a', 'b', 'a'], 'a') # 2\ncollections.rindex(('a', 'b', 'a'), 'a') # 2\ncollections.rindex('xyz', 'a') # ValueError\ncollections.rindex([x for x in 'xyz'], 'a') # ValueError\n\ncollections.rfind('aba', 'a') # 2\ncollections.rfind(['a', 'b', 'a'], 'a') # 2\ncollections.rfind(('a', 'b', 'a'), 'a') # 2\ncollections.rindex('xyz', 'a') # -1\ncollections.rfind([x for x in 'xyz'], 'a') # -1\n\ncollections.reduce_value(['abc']) # 'abc'\ncollections.reduce_value(('abc',)) # 'abc'\ncollections.reduce_value({'abc'}) # 'abc'\ncollections.reduce_value('abc') # 'abc'\ncollections.reduce_value({'a': 'aaa'}) # {'a': 'aaa'}\ncollections.reduce_value([{'a': 'aaa'}]) # {'a': 'aaa'}\ncollections.reduce_value(['a', 'b', 'c']) # ['a', 'b', 'c']\n\ncollections.wrap_value(['abc']) # ['abc']\ncollections.wrap_value(('abc',)) # ('abc',)\ncollections.wrap_value('abc') # ['abc']\ncollections.wrap_value(x for x in 'abc') # ['a', 'b', 'c']\ncollections.wrap_value({'a': 'aaa'}) # [{'a': 'aaa'}]\ncollections.wrap_value(['a', 'b', 'c']) # ['a', 'b', 'c']\n```\n\nHere's a little bit about dates and numbers.\n```python\nfrom parserutils import dates\nfrom parserutils import numbers\n\n# Leverages dateutil in general, but also handles milliseconds and provides defaults\n\ndates.parse_dates(None, default='today') # Today (default behavior)\ndates.parse_dates(None, default=None) # Returns None\ndates.parse_dates('nope', default=None) # Returns None\ndates.parse_dates(0) # 1970\ndates.parse_dates('') # Behaves as described in dateutil library\n\n# Reliably handles all the usual cases\n\nnumbers.is_number(0) # Integer: True\nnumbers.is_number(1.1) # Float: True\nnumbers.is_number('2.2') # String: True\nnumbers.is_number(False) # Boolean: False by default\nnumbers.is_number(False, if_bool=True) # Boolean: True if you need it to\nnumbers.is_number(float('inf')) # Infinite: False\nnumbers.is_number(float('nan')) # NaN: False\n```\n\nHere's something about string and URL parsing helpers.\n```python\nfrom parserutils import strings\nfrom parserutils import urls\n\n# These string conversions are written to be fast and reliable\n\nstrings.camel_to_constant('toConstant') # TO_CONSTANT\nstrings.camel_to_constant('XMLConstant') # XML_CONSTANT\nstrings.camel_to_constant('withNumbers1And2') # WITH_NUMBERS1_AND2\n\nstrings.camel_to_snake('toSnake') # to_snake\nstrings.camel_to_snake('withXMLAbbreviation') # with_xml_abbreviation\nstrings.camel_to_snake('withNumbers3And4') # with_numbers3_and4\n\nstrings.snake_to_camel('from_snake') # fromSnake\nstrings.snake_to_camel('_leading_and_trailing_') # leadingAndTrailing\nstrings.snake_to_camel('extra___underscores') # extraUnderscores\n\nstrings.find_all('ab??ca??bc??', '??') # [2, 6, 10]\nstrings.find_all('ab??ca??bc??', '??', reverse=True) # [10, 6, 2]\nstrings.find_all('ab??ca??bc??', '??', limit=2, reverse=True) # [10, 6]\nstrings.find_all('ab??ca??bc??', '??', start=4) # [6, 10]\nstrings.find_all('ab??ca??bc??', '??', end=8) # [2, 6]\nstrings.find_all('ab??ca??bc??', '??', start=4, end=8) # [6]\n\nstrings.splitany('ab:ca:bc', ',') # Same as 'ab:ca:bc'.split(':')\nstrings.splitany('ab:ca:bc', ',', 1) # Same as 'ab:ca:bc'.split(':', 1)\nstrings.splitany('ab|ca:bc', '|:') # ['ab', 'ca', 'bc']\nstrings.splitany('ab|ca:bc', ':|', 1) # ['ab', 'ca:bc']\nstrings.splitany('0<=3<5', ['<', '<=']) # ['0', '3', '5']\nstrings.splitany('0<=3<5', ['<', '<='], 1) # ['0', '3<5']\n\nstrings.to_ascii_equivalent('smart quotes, etc.') # Replaces with ascii quotes, etc.\n\n# URL manipulation leverages urllib, but spares you the extra code\n\nurls.get_base_url('http://www.params.com?a=aaa') # 'http://www.params.com/'\nurls.get_base_url('http://www.path.com/test') # 'http://www.path.com/'\nurls.get_base_url('http://www.path.com/test', include_path=True) # 'http://www.path.com/test/'\nurls.get_base_url('http://www.params.com/test?a=aaa', True) # 'http://www.params.com/test/'\n\nurls.update_url_params('http://www.params.com?a=aaa', a='aaa') # 'http://www.params.com?a=aaa'\nurls.update_url_params('http://www.params.com?a=aaa', a='xxx') # 'http://www.params.com?a=xxx'\nurls.update_url_params('http://www.params.com', b='bbb') # 'http://www.params.com?b=bbb'\nurls.update_url_params('http://www.params.com', c=['c', 'cc']) # 'http://www.params.com?c=c&c=cc'\n\n# Helpers to parse urls to and from parts: parses path as list and params as dict\nurls.url_to_parts('http://www.params.com/test/path?a=aaa') # SplitResult(..., path=['test', 'path'], query={'a': 'aaa'})\nurls.parts_to_url(\n {'netloc': 'www.params.com', 'query': {'a': 'aaa'} # 'http://www.params.com?a=aaa'\n)\nurls.parts_to_url(\n urls.url_to_parts('http://www.params.com/test/path?a=aaa') # 'http://www.params.com/test/path?a=aaa'\n)\n```\n\nFinally, XML parsing is also supported, using the cElementTree and defusedxml libraries for performance and security\n```python\nfrom parserutils import elements\n\n# First convert an XML string to an Element object\nxml_string = 'onetwoyuck'\nxml_element = elements.get_element(xml_string)\n\n\n# Update the XML string and print it back out\nelements.set_element_text(xml_element, 'parent/child', 'child text')\nelements.set_element_attributes(xml_element, childHas='child attribute')\nelements.remove_element(xml_element, 'parent/uglyChild')\nelements.element_to_string(xml_element)\n\n\n# Conversion from string to Element, to dict, and then back to string\nconverted = elements.element_to_dict(xml_string, recurse=True)\nreverted = elements.dict_to_element(converted)\nreverted = elements.get_element(converted)\nxml_string == elements.element_to_string(converted)\n\n\n# Conversion to flattened dict object\nroot, obj = elements.element_to_object(converted)\nobj == {'root': {'parent': {'child': ['one', 'two'], 'uglyChild': 'yuck'}}}\n\n\n# Read in an XML file and write it elsewhere\nwith open('/path/to/file.xml', 'wb') as xml:\n xml_from_file = elements.get_element(xml)\n elements.write_element(xml_from_file, '/path/to/updated/file.xml')\n\n\n# Write a local file from a remote location (via URL)\nxml_from_web = elements.get_remote_element('http://en.wikipedia.org/wiki/XML')\nelements.write_element(xml_from_web, '/path/to/new/file.xml')\n\n\n# Read content at a local file path to a string\nxml_from_path = elements.get_remote_element('/path/to/file.xml')\nelements.element_to_string(xml_from_path)\n```\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/consbio/parserutils", "keywords": "parser,parsing,utils,utilities,collections,dates,elements,numbers,strings,url,xml", "license": "BSD", "maintainer": "", "maintainer_email": "", "name": "parserutils", "package_url": "https://pypi.org/project/parserutils/", "platform": "", "project_url": "https://pypi.org/project/parserutils/", "project_urls": { "Homepage": "https://github.com/consbio/parserutils" }, "release_url": "https://pypi.org/project/parserutils/1.1.2/", "requires_dist": [ "defusedxml (>=0.4.1)", "python-dateutil (>=2.4.2)", "six (>=1.9.0)" ], "requires_python": "", "summary": "A collection of performant parsing utilities", "version": "1.1.2" }, "last_serial": 3934215, "releases": { "0.9.8": [ { "comment_text": "", "digests": { "md5": "fbb58618a7614354e2cd6c176156472e", "sha256": "e80609945d50c8f6f59462e193e02c131ab3d02587d62ffea6dea591538156c7" }, "downloads": -1, "filename": "parserutils-0.9.8-py3-none-any.whl", "has_sig": false, "md5_digest": "fbb58618a7614354e2cd6c176156472e", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 39887, "upload_time": "2017-07-18T20:30:41", "url": "https://files.pythonhosted.org/packages/b3/4e/da064f17740fff48966707606799dab40e44cfbba8a95b53c6231dbcf419/parserutils-0.9.8-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cd49c22cf186924069f869ce21e7627f", "sha256": "8e3defffeb3f460c6224301b5483beeeeaa8426d510789d730fc62eff3c56440" }, "downloads": -1, "filename": "parserutils-0.9.8.tar.gz", "has_sig": false, "md5_digest": "cd49c22cf186924069f869ce21e7627f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 34796, "upload_time": "2017-07-18T20:30:43", "url": "https://files.pythonhosted.org/packages/15/fb/f1297f09286dfb281fd936a25d0a6fbe101796ac0371b766b58edbbaddee/parserutils-0.9.8.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "f09d44c223c726769968e47034f1bd88", "sha256": "9cd73338bb9d6194b48a04b52934728d3b412efeaf00b30d8b58a2a11ed5ecb7" }, "downloads": -1, "filename": "parserutils-1.0.1-py3.6.egg", "has_sig": false, "md5_digest": "f09d44c223c726769968e47034f1bd88", "packagetype": "bdist_egg", "python_version": "3.6", "requires_python": null, "size": 92431, "upload_time": "2018-06-01T22:58:13", "url": "https://files.pythonhosted.org/packages/4d/9d/1176b09bff889d63cfa182e804733fc6fb6ad300a52c7736ce2a41165625/parserutils-1.0.1-py3.6.egg" }, { "comment_text": "", "digests": { "md5": "6f29a0f846bf9503ac85aa588201dd36", "sha256": "82a111ab7963682a6d2eed20f683e8ed28b7f23b14184c5f2db0cb638dc3d62a" }, "downloads": -1, "filename": "parserutils-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "6f29a0f846bf9503ac85aa588201dd36", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 40074, "upload_time": "2017-07-31T20:32:46", "url": "https://files.pythonhosted.org/packages/4c/f4/cc197c059972ea92ce1c4ab48d0d96c17c6666202249d13a45edc63cb096/parserutils-1.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "2c23765a78de9b3200d004b815431276", "sha256": "4a169e6c9814e9348043cf26fb678beb78f83dc2d26c19ed8664a0553709e7fe" }, "downloads": -1, "filename": "parserutils-1.0.1.tar.gz", "has_sig": false, "md5_digest": "2c23765a78de9b3200d004b815431276", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 34942, "upload_time": "2017-07-31T20:32:48", "url": "https://files.pythonhosted.org/packages/0f/50/e92d8756443c69be3c0e2807c63dae716c31008faf71b528a1fc6dc40e20/parserutils-1.0.1.tar.gz" } ], "1.1.2": [ { "comment_text": "", "digests": { "md5": "b5974cc8a9ec2bd04c86b26bb8a5f0e9", "sha256": "e291ebd89b487265d18f739df4b7c1e7dbd5ec9b5955c402a12cca285b5418a8" }, "downloads": -1, "filename": "parserutils-1.1.2-py3-none-any.whl", "has_sig": false, "md5_digest": "b5974cc8a9ec2bd04c86b26bb8a5f0e9", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 42177, "upload_time": "2018-06-05T23:16:39", "url": "https://files.pythonhosted.org/packages/fe/6a/0f6392f437f7da7aa33a9a3ba023db9c4f504699143d3027c7c6b60453de/parserutils-1.1.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "61cc0b40e0280e82622e1c3fcd9b36fd", "sha256": "24c969485e886868355249dae521e776a31cae3a0e2f0ab75a5c082d5b7e6407" }, "downloads": -1, "filename": "parserutils-1.1.2.tar.gz", "has_sig": false, "md5_digest": "61cc0b40e0280e82622e1c3fcd9b36fd", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 40953, "upload_time": "2018-06-05T23:16:41", "url": "https://files.pythonhosted.org/packages/e8/7f/2f6cc2d60e82ffe472afba1ba9481fcd607ae5464f3cd47b074d179a140d/parserutils-1.1.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b5974cc8a9ec2bd04c86b26bb8a5f0e9", "sha256": "e291ebd89b487265d18f739df4b7c1e7dbd5ec9b5955c402a12cca285b5418a8" }, "downloads": -1, "filename": "parserutils-1.1.2-py3-none-any.whl", "has_sig": false, "md5_digest": "b5974cc8a9ec2bd04c86b26bb8a5f0e9", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 42177, "upload_time": "2018-06-05T23:16:39", "url": "https://files.pythonhosted.org/packages/fe/6a/0f6392f437f7da7aa33a9a3ba023db9c4f504699143d3027c7c6b60453de/parserutils-1.1.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "61cc0b40e0280e82622e1c3fcd9b36fd", "sha256": "24c969485e886868355249dae521e776a31cae3a0e2f0ab75a5c082d5b7e6407" }, "downloads": -1, "filename": "parserutils-1.1.2.tar.gz", "has_sig": false, "md5_digest": "61cc0b40e0280e82622e1c3fcd9b36fd", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 40953, "upload_time": "2018-06-05T23:16:41", "url": "https://files.pythonhosted.org/packages/e8/7f/2f6cc2d60e82ffe472afba1ba9481fcd607ae5464f3cd47b074d179a140d/parserutils-1.1.2.tar.gz" } ] }