From d5f9b7c477e9d86639fe274cc0ac1c3d7883908d Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Mon, 23 Jan 2017 20:55:45 +0200 Subject: [PATCH] - Fixed issues with difference between Python2 and Python3 regarding Unicode strings. - Fixed combine filter tests. - Updated TODO list accordingly. --- README.rst | 2 -- setup.py | 2 +- template/filters.py | 25 +++++++++++++------------ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/README.rst b/README.rst index 26b3581..8f157c1 100644 --- a/README.rst +++ b/README.rst @@ -79,8 +79,6 @@ at: https://www.shore.co.il/git/. TODO ---- -- 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 (https://docs.travis-ci.com/user/deployment/pypi/ and diff --git a/setup.py b/setup.py index 68c110a..7abfd3b 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ setup( ], keywords='config configuration jinja template environment', packages=find_packages(), - install_requires=['Jinja2', 'PyYAML'], + install_requires=['Jinja2', 'PyYAML', 'six'], extras_require={ 'dev': ['tox'], }, entry_points={ diff --git a/template/filters.py b/template/filters.py index d060d2c..133ecbb 100644 --- a/template/filters.py +++ b/template/filters.py @@ -1,6 +1,7 @@ #!/usr/bin/env python from __future__ import (absolute_import, division, print_function, unicode_literals) +import six # noqa: F401 def to_yaml(value): @@ -46,10 +47,10 @@ def from_json(value): >>> 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('"a"') == six.text_type(u'a') + True + >>> from_json('{"1": {"a": [1, 2, 3]}}') == {u'1': {u'a': [1, 2, 3]}} + True ''' from json import loads return loads(value) @@ -77,8 +78,13 @@ def pprint(value): Examples: >>> pprint(1) '1' - >>> pprint([{'first_name': 'John', 'last_name': 'Doe'}, {'first_name': 'Jane', 'last_name': 'Doe'}]) # noqa: E501 - "[{'first_name': 'John', 'last_name': 'Doe'},\\n {'first_name': 'Jane', 'last_name': 'Doe'}]" + >>> output = pprint([{'first_name': 'John', 'last_name': 'Doe'}, {'first_name': 'Jane', 'last_name': 'Doe'}]) # noqa: E501 + >>> if six.PY3: + ... output == "[{'first_name': 'John', 'last_name': 'Doe'},\\n {'first_name': 'Jane', 'last_name': 'Doe'}]" + ... elif six.PY2: + ... output == "[{u'first_name': u'John', u'last_name': u'Doe'},\\n {u'first_name': u'Jane', u'last_name': u'Doe'}]" + ... + True ''' from pprint import pformat return pformat(value) @@ -89,12 +95,7 @@ def combine(default, override): Returns a combined dictionary of the 2 dictionaries given (with the 2nd overriding the 1st). Examples: - >>> combined = combine({'a': 1, 'b': 2}, {'b': 3, 'c': 4}) - >>> 'a' in combined - True - >>> 'c' in combined - True - >>> combined['b'] == 3 + >>> combine({'a': 1, 'b': 2}, {'b': 3, 'c': 4}) == {'a': 1, 'b': 3, 'c': 4} True ''' combined = default.copy() -- GitLab