Name | data JSON |
Version |
0.4
JSON |
| download |
home_page | http://github.com/mbr/data |
Summary | Work with unicode/non-unicode data from files or strings uniformly. |
upload_time | 2015-06-10 22:53:59 |
maintainer | None |
docs_url | https://pythonhosted.org/data/ |
author | Marc Brinkmann |
requires_python | None |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
|
coveralls test coverage |
No coveralls.
|
data
====
``data`` is a small Python module that allows you to treat input in a singular
way and leave it up to the caller to supply a byte-string, a unicode object, a
file-like or a filename.
.. code-block:: python
>>> open('helloworld.txt', 'w').write('hello, world from a file')
>>> from data import Data as I
>>> a = I(u'hello, world')
>>> b = I(file='helloworld.txt')
>>> c = I(open('helloworld.txt'))
>>> print unicode(a)
hello, world
>>> print unicode(b)
hello, world from a file
>>> print unicode(c)
hello, world from a file
This can be made even more convenient using the ``data`` decorator:
.. code-block:: python
>>> from data.decorators import data
>>> @data('buf')
... def parse_buffer(buf, magic_mode=False):
... return 'buf passed in as ' + repr(buf)
...
>>> parse_buffer('hello')
"buf passed in as Data(data='hello', encoding='utf8')"
>>> rv = parse_buffer(open('helloworld.txt'))
>>> assert 'file=' in rv
Fitting in
----------
All instances support methods like ``read`` or ``__str__`` that make it easy to
fit it into existing APIs:
.. code-block:: python
>>> d = I('some data')
>>> d.read(4)
u'some'
>>> d.read(4)
u' dat'
>>> d.read(4)
u'a'
>>> e = I(u'more data')
>>> str(e)
'more data'
Note how ``read`` returns unicode. Additionally, ``readb`` is available:
.. code-block:: python
>>> f = I(u'I am \xdcnicode.')
>>> f.readb()
'I am \xc3\x9cnicode.'
Every ``data`` object has an encoding attribute which is used for converting
from and to unicode.
.. code-block:: python
>>> g = I(u'I am \xdcnicode.', encoding='latin1')
>>> g.readb()
'I am \xdcnicode.'
Iteration and line reading are also supported:
.. code-block:: python
>>> h = I('I am\nof many\nlines')
>>> h.readline()
u'I am\n'
>>> h.readlines()
[u'of many\n', u'lines']
>>> i = I('line one\nline two\n')
>>> list(iter(i))
[u'line one\n', u'line two\n']
Extras
------
save_to
~~~~~~~
Some useful convenience methods are available:
.. code-block:: python
>>> j = I('example')
>>> j.save_to('example.txt')
The ``save_to`` method will use the most efficient way possible to save the
data to a file (``copyfileobj`` or ``write()``). It can also be passed a
file-like object:
.. code-block:: python
>>> k = I('example2')
>>> with open('example2.txt', 'wb') as out:
... k.save_to(out)
...
temp_saved
~~~~~~~~~~
If you need the output inside a secure temporary file, ``temp_saved`` is
available:
.. code-block:: python
>>> l = I('goes into tmp')
>>> with l.temp_saved() as tmp:
... print tmp.name.startswith('/tmp/tmp')
... print l.read()
...
True
goes into tmp
``temp_saved`` functions almost identically to ``tempfile.NamedTemporaryFile``,
with one difference: There is no ``delete`` argument. The file is removed only
when the context manager exits.
Where it is useful
------------------
``data`` can be used on both sides of an API, either while passing values in:
.. code-block:: python
>>> import json
>>> from data import Data as I
>>> m = I('{"this": "json"}')
>>> json.load(m)
{u'this': u'json'}
or when getting values passed (see the data decorator example above). If
necessary, you can also support APIs that allow users to pass in filenames:
.. code-block:: python
>>> class Parser(object):
... @data('input')
... def parse(self, input, parser_opt=False):
... return input
... def parse_file(self, input_file, *args, **kwargs):
... return self.parse(I(file=input_file), *args, **kwargs)
...
>>> p = Parser()
>>> p.parse_file('/dev/urandom')
Data(file='/dev/urandom', encoding='utf8')
See the documentation at http://pythonhosted.org/data for an API reference.
Python 2 and 3
--------------
``data`` works the same on Python 2 and 3 thanks to `six
<https://pypi.python.org/pypi/six>`_, a few compatibility functions and a
testsuite.
Python 3 is supported from 3.3 onwards, Python 2 from 2.6.
Raw data
{
"_id": null,
"home_page": "http://github.com/mbr/data",
"name": "data",
"maintainer": null,
"docs_url": "https://pythonhosted.org/data/",
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Marc Brinkmann",
"author_email": "git@marcbrinkmann.de",
"download_url": "https://files.pythonhosted.org/packages/ed/e9/623be82fac4250fc614741f5b1ead83d339794f94b19ac8665b6ea12ee05/data-0.4.tar.gz",
"platform": "UNKNOWN",
"description": "data\n====\n\n``data`` is a small Python module that allows you to treat input in a singular\nway and leave it up to the caller to supply a byte-string, a unicode object, a\nfile-like or a filename.\n\n.. code-block:: python\n\n >>> open('helloworld.txt', 'w').write('hello, world from a file')\n\n >>> from data import Data as I\n >>> a = I(u'hello, world')\n >>> b = I(file='helloworld.txt')\n >>> c = I(open('helloworld.txt'))\n\n >>> print unicode(a)\n hello, world\n >>> print unicode(b)\n hello, world from a file\n >>> print unicode(c)\n hello, world from a file\n\nThis can be made even more convenient using the ``data`` decorator:\n\n.. code-block:: python\n\n >>> from data.decorators import data\n\n >>> @data('buf')\n ... def parse_buffer(buf, magic_mode=False):\n ... return 'buf passed in as ' + repr(buf)\n ...\n\n >>> parse_buffer('hello')\n \"buf passed in as Data(data='hello', encoding='utf8')\"\n\n >>> rv = parse_buffer(open('helloworld.txt'))\n >>> assert 'file=' in rv\n\n\nFitting in\n----------\n\nAll instances support methods like ``read`` or ``__str__`` that make it easy to\nfit it into existing APIs:\n\n.. code-block:: python\n\n >>> d = I('some data')\n >>> d.read(4)\n u'some'\n >>> d.read(4)\n u' dat'\n >>> d.read(4)\n u'a'\n >>> e = I(u'more data')\n >>> str(e)\n 'more data'\n\nNote how ``read`` returns unicode. Additionally, ``readb`` is available:\n\n.. code-block:: python\n\n >>> f = I(u'I am \\xdcnicode.')\n >>> f.readb()\n 'I am \\xc3\\x9cnicode.'\n\nEvery ``data`` object has an encoding attribute which is used for converting\nfrom and to unicode.\n\n.. code-block:: python\n\n >>> g = I(u'I am \\xdcnicode.', encoding='latin1')\n >>> g.readb()\n 'I am \\xdcnicode.'\n\nIteration and line reading are also supported:\n\n.. code-block:: python\n\n >>> h = I('I am\\nof many\\nlines')\n >>> h.readline()\n u'I am\\n'\n >>> h.readlines()\n [u'of many\\n', u'lines']\n\n >>> i = I('line one\\nline two\\n')\n >>> list(iter(i))\n [u'line one\\n', u'line two\\n']\n\n\nExtras\n------\n\nsave_to\n~~~~~~~\n\nSome useful convenience methods are available:\n\n.. code-block:: python\n\n >>> j = I('example')\n >>> j.save_to('example.txt')\n\nThe ``save_to`` method will use the most efficient way possible to save the\ndata to a file (``copyfileobj`` or ``write()``). It can also be passed a\nfile-like object:\n\n.. code-block:: python\n\n >>> k = I('example2')\n >>> with open('example2.txt', 'wb') as out:\n ... k.save_to(out)\n ...\n\n\ntemp_saved\n~~~~~~~~~~\n\nIf you need the output inside a secure temporary file, ``temp_saved`` is\navailable:\n\n.. code-block:: python\n\n >>> l = I('goes into tmp')\n >>> with l.temp_saved() as tmp:\n ... print tmp.name.startswith('/tmp/tmp')\n ... print l.read()\n ...\n True\n goes into tmp\n\n``temp_saved`` functions almost identically to ``tempfile.NamedTemporaryFile``,\nwith one difference: There is no ``delete`` argument. The file is removed only\nwhen the context manager exits.\n\n\nWhere it is useful\n------------------\n\n``data`` can be used on both sides of an API, either while passing values in:\n\n.. code-block:: python\n\n >>> import json\n >>> from data import Data as I\n\n >>> m = I('{\"this\": \"json\"}')\n >>> json.load(m)\n {u'this': u'json'}\n\nor when getting values passed (see the data decorator example above). If\nnecessary, you can also support APIs that allow users to pass in filenames:\n\n.. code-block:: python\n\n >>> class Parser(object):\n ... @data('input')\n ... def parse(self, input, parser_opt=False):\n ... return input\n ... def parse_file(self, input_file, *args, **kwargs):\n ... return self.parse(I(file=input_file), *args, **kwargs)\n ...\n\n >>> p = Parser()\n >>> p.parse_file('/dev/urandom')\n Data(file='/dev/urandom', encoding='utf8')\n\n\nSee the documentation at http://pythonhosted.org/data for an API reference.\n\n\nPython 2 and 3\n--------------\n\n``data`` works the same on Python 2 and 3 thanks to `six\n<https://pypi.python.org/pypi/six>`_, a few compatibility functions and a\ntestsuite.\n\nPython 3 is supported from 3.3 onwards, Python 2 from 2.6.",
"bugtrack_url": null,
"license": "MIT",
"summary": "Work with unicode/non-unicode data from files or strings uniformly.",
"version": "0.4",
"project_urls": {
"Download": "UNKNOWN",
"Homepage": "http://github.com/mbr/data"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ede9623be82fac4250fc614741f5b1ead83d339794f94b19ac8665b6ea12ee05",
"md5": "e8b34bc3806cae346a7c54aad77b4fef",
"sha256": "2726a65da1af31e2345b6bba81ae4cee87dddf17f7c62f5c63ba7327a8480667"
},
"downloads": -1,
"filename": "data-0.4.tar.gz",
"has_sig": false,
"md5_digest": "e8b34bc3806cae346a7c54aad77b4fef",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7042,
"upload_time": "2015-06-10T22:53:59",
"upload_time_iso_8601": "2015-06-10T22:53:59.635372Z",
"url": "https://files.pythonhosted.org/packages/ed/e9/623be82fac4250fc614741f5b1ead83d339794f94b19ac8665b6ea12ee05/data-0.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2015-06-10 22:53:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mbr",
"github_project": "data",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "data"
}