From 919d99a89314e67f3145854998f03248d97b5be9 Mon Sep 17 00:00:00 2001 From: minh5 Date: Tue, 3 Mar 2020 17:18:40 -0500 Subject: [PATCH 1/8] added datetime to acceptable leaf --- dpath/segments.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dpath/segments.py b/dpath/segments.py index 65f8920..f7b6020 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -1,4 +1,5 @@ from copy import deepcopy +from datetime import datetime from dpath.exceptions import InvalidGlob, InvalidKeyName, PathNotFound from dpath import options from fnmatch import fnmatchcase @@ -22,7 +23,7 @@ def leaf(thing): leaf(thing) -> bool ''' - leaves = (bytes, str, int, float, bool, type(None)) + leaves = (bytes, str, int, float, bool, type(None), datetime) return isinstance(thing, leaves) From e06d5d93aec62fc53e80377b30f2fc20e2c33856 Mon Sep 17 00:00:00 2001 From: minh5 Date: Wed, 4 Mar 2020 16:36:47 -0500 Subject: [PATCH 2/8] adjust leaf logic --- dpath/segments.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dpath/segments.py b/dpath/segments.py index f7b6020..5c59ef6 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -23,9 +23,9 @@ def leaf(thing): leaf(thing) -> bool ''' - leaves = (bytes, str, int, float, bool, type(None), datetime) - - return isinstance(thing, leaves) + return not isinstance(thing, collections.abc.Container) or isinstance( + thing, six.string_types + ) def leafy(thing): From 01933d1976cd6ae58f24330ed90f3c028228aa91 Mon Sep 17 00:00:00 2001 From: minh5 Date: Wed, 4 Mar 2020 16:36:52 -0500 Subject: [PATCH 3/8] added tests --- tests/test_segments.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_segments.py b/tests/test_segments.py index af9df85..a3780e1 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -1,3 +1,5 @@ +import datetime +import decimal from dpath import options from hypothesis import given, assume, settings, HealthCheck import dpath.segments as api @@ -339,3 +341,20 @@ def test_view(walkable): view = api.view(node, segments) assert api.get(view, segments) == api.get(node, segments) + + +def test_leaves_passed(): + ''' + Test if a value is correctly classified as a leaf + ''' + for thing in [ + 'a', 1, True, None, datetime.datetime(2020, 1, 1), decimal.Decimal(1.99)]: + assert api.leaf(thing) == True + + +def test_leaves_failed(): + ''' + Test if a value is correctly classified as a leaf + ''' + for thing in ['a', 1, True, None, datetime.datetime(2020, 1, 1)]: + assert api.leaf(thing) == True From bbae56308a268bcd0ef00c48c862ef506221e992 Mon Sep 17 00:00:00 2001 From: minh5 Date: Wed, 4 Mar 2020 16:38:29 -0500 Subject: [PATCH 4/8] lint --- tests/test_segments.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/test_segments.py b/tests/test_segments.py index a3780e1..6d0a52d 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -1,10 +1,13 @@ import datetime import decimal -from dpath import options -from hypothesis import given, assume, settings, HealthCheck +import os + import dpath.segments as api +from dpath import options + import hypothesis.strategies as st -import os +from hypothesis import HealthCheck, assume, given, settings + settings.register_profile("default", suppress_health_check=(HealthCheck.too_slow,)) settings.load_profile(os.getenv(u'HYPOTHESIS_PROFILE', 'default')) @@ -348,7 +351,13 @@ def test_leaves_passed(): Test if a value is correctly classified as a leaf ''' for thing in [ - 'a', 1, True, None, datetime.datetime(2020, 1, 1), decimal.Decimal(1.99)]: + 'a', + 1, + True, + None, + datetime.datetime(2020, 1, 1), + decimal.Decimal(1.99) + ]: assert api.leaf(thing) == True @@ -356,5 +365,5 @@ def test_leaves_failed(): ''' Test if a value is correctly classified as a leaf ''' - for thing in ['a', 1, True, None, datetime.datetime(2020, 1, 1)]: - assert api.leaf(thing) == True + for thing in [set(), [], {'cat': 'dog'}]: + assert api.leaf(thing) == False From 348cc3b51e8a8dd3a321f3b626874694a40a9c29 Mon Sep 17 00:00:00 2001 From: minh5 Date: Wed, 4 Mar 2020 17:00:49 -0500 Subject: [PATCH 5/8] forgot import --- dpath/segments.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dpath/segments.py b/dpath/segments.py index 5c59ef6..1df26df 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -1,3 +1,4 @@ +import collections from copy import deepcopy from datetime import datetime from dpath.exceptions import InvalidGlob, InvalidKeyName, PathNotFound From 3026964ee47aa1f0e7d5dc08ebccf819b812037d Mon Sep 17 00:00:00 2001 From: minh5 Date: Wed, 4 Mar 2020 17:08:43 -0500 Subject: [PATCH 6/8] forgot another import --- dpath/segments.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dpath/segments.py b/dpath/segments.py index 1df26df..4a39e62 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -4,6 +4,7 @@ from dpath.exceptions import InvalidGlob, InvalidKeyName, PathNotFound from dpath import options from fnmatch import fnmatchcase +import six def kvs(node): From fa59b539f2efd26cd72d005f41dffaa91c010100 Mon Sep 17 00:00:00 2001 From: minh5 Date: Wed, 4 Mar 2020 17:14:31 -0500 Subject: [PATCH 7/8] added six package to tox --- tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tox.ini b/tox.ini index 8969270..a89e37a 100644 --- a/tox.ini +++ b/tox.ini @@ -14,6 +14,7 @@ deps = hypothesis mock nose + six commands = nosetests {posargs} [testenv:flake8] From 61bfc0f087e7c79ff4fb2884b1e58c9907cd2c1b Mon Sep 17 00:00:00 2001 From: minh5 Date: Wed, 4 Mar 2020 17:30:52 -0500 Subject: [PATCH 8/8] added logic for binary --- dpath/segments.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dpath/segments.py b/dpath/segments.py index 4a39e62..b7d9ed5 100644 --- a/dpath/segments.py +++ b/dpath/segments.py @@ -25,8 +25,10 @@ def leaf(thing): leaf(thing) -> bool ''' - return not isinstance(thing, collections.abc.Container) or isinstance( - thing, six.string_types + return ( + not isinstance(thing, collections.abc.Container) + or isinstance(thing, six.string_types) + or isinstance(thing, six.binary_type) )