Commit 2cc629c6 authored by nimrod's avatar nimrod
Browse files

- More doctests.

- Updated TODO list.
parent d4f711a6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -78,7 +78,7 @@ at: https://www.shore.co.il/git/.
TODO
----

- Add unit tests of filters using doctest.
- Fix unicode strings in Python 2.7 only doctests in :code:`load_json`.
- Fix combining dictionaries test.
- Fix Travis CI test on Python 3.2 (https://travis-ci.org/adarnimrod/template/jobs/187388235).
- Release on tagged commits to PyPI in Travis CI
+37 −0
Original line number Diff line number Diff line
#!/usr/bin/env python
from __future__ import (absolute_import, division,
                        print_function, unicode_literals)


def to_yaml(value):
@@ -20,16 +22,51 @@ def to_yaml(value):


def to_json(value):
    '''
    Converts given data structure to JSON form.
    Examples:

    >>> to_json([1,2,3])
    '[1, 2, 3]'
    >>> to_json({'b':2})
    '{"b": 2}'
    >>> to_json(2)
    '2'
    >>> to_json({1: {'a': [1,2,3]}})
    '{"1": {"a": [1, 2, 3]}}'
    '''
    from json import dumps
    return dumps(value)


def from_json(value):
    '''
    Returns native data structure from the given JSON string.
    Examples:

    >>> from_json('[1, 2, 3]')
    [1, 2, 3]
    >>> from_json('"a"')
    u'a'
    >>> from_json('{"1": {"a": [1, 2, 3]}}')
    {u'1': {u'a': [1, 2, 3]}}
    '''
    from json import loads
    return loads(value)


def from_yaml(value):
    '''
    Returns native data structure from the given YAML string.
    Examples:

    >>> from_yaml('a')
    'a'
    >>> from_yaml('[1, 2, 3]')
    [1, 2, 3]
    >>> from_yaml('{"1": {"a": [1, 2, 3]}}')
    {'1': {'a': [1, 2, 3]}}
    '''
    from yaml import safe_load
    return safe_load(value)