Skip to content

Commit

Permalink
Merge pull request #31 from ambitioninc/develop
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
somewes committed Apr 6, 2018
2 parents 6f5b0ec + fc5ed22 commit 4824dfd
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ docs/_build/

# IPython Notebook
.ipynb_checkpoints/

.eggs
4 changes: 4 additions & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Release Notes
=============

v0.2.0
------
* Handle data type mismatch comparisons and return False

v0.1.7
------
* Python 3.6 support
Expand Down
12 changes: 10 additions & 2 deletions kmatch/kmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@ class K(object):
'!?': lambda key, value: key not in value,
}

def __init__(self, p, suppress_key_errors=False):
def __init__(self, p, suppress_key_errors=False, suppress_exceptions=False):
"""
Sets the pattern, performs validation on the pattern, and compiles its regexs if it has any.
:param p: The kmatch pattern
:type p: list
:param suppress_key_errors: Suppress KeyError exceptions on filters and return False instead
:type suppress_key_errors: bool
:param suppress_exceptions: Suppress all exceptions on filters and return False instead
:type suppress_exceptions: bool
:raises: :class:`ValueError <exceptions.ValueError>` on an invalid pattern or regex
"""
self._raw_pattern = deepcopy(p)
self._compiled_pattern = deepcopy(p)
self._suppress_key_errors = suppress_key_errors
self._suppress_exceptions = suppress_exceptions

# Validate the pattern is in the appropriate format
self._validate(self._compiled_pattern)
Expand Down Expand Up @@ -112,7 +115,12 @@ def _match(self, p, value):
else:
return self._match_key_filter(p, value)
except KeyError:
if self._suppress_key_errors:
if self._suppress_key_errors or self._suppress_exceptions:
return False
else:
raise
except TypeError:
if self._suppress_exceptions:
return False
else:
raise
Expand Down
35 changes: 35 additions & 0 deletions kmatch/tests/kmatch_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from sys import version
from unittest import TestCase
from mock import patch

Expand Down Expand Up @@ -91,6 +92,9 @@ def test_basic_nonexistence_false(self):
def test_basic_suppress_key_errors(self):
self.assertFalse(K(['==', 'k', 3], suppress_key_errors=True).match({}))

def test_basic_suppress_exceptions(self):
self.assertFalse(K(['==', 'k', 3], suppress_exceptions=True).match({}))

def test_not_field_true(self):
self.assertTrue(K([
'!', ['>=', 'f', 3],
Expand All @@ -104,6 +108,37 @@ def test_compound_suppress_key_errors_gte_true(self):
]
], suppress_key_errors=True).match({'f': 6}))

def test_compound_suppress_exceptions_gte_true(self):
self.assertTrue(K([
'|', [
['==', 'f1', 5],
['>', 'f', 5],
]
], suppress_exceptions=True).match({'f': 6}))

def test_type_exception(self):
"""
Handles different data type comparisons in py3
"""
if version[0] == '2': # pragma: no cover
with patch('kmatch.K._match_value_filter') as mock_match_value_filter:
mock_match_value_filter.side_effect = TypeError

with self.assertRaises(TypeError):
K(['>=', 'k', 3]).match({'k': None})
with self.assertRaises(TypeError):
K(['>=', 'k', 3]).match({'k': ''})
self.assertFalse(K(['>=', 'k', 3], suppress_exceptions=True).match({'k': None}))
self.assertFalse(K(['>=', 'k', 3], suppress_exceptions=True).match({'k': ''}))

if version[0] == '3': # pragma: no cover
with self.assertRaises(TypeError):
K(['>=', 'k', 3]).match({'k': None})
with self.assertRaises(TypeError):
K(['>=', 'k', 3]).match({'k': ''})
self.assertFalse(K(['>=', 'k', 3], suppress_exceptions=True).match({'k': None}))
self.assertFalse(K(['>=', 'k', 3], suppress_exceptions=True).match({'k': ''}))

def test_compound_existence_gte_true(self):
self.assertTrue(K([
'&', [
Expand Down
2 changes: 1 addition & 1 deletion kmatch/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.7'
__version__ = '0.2.0'
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ cover-package=kmatch
max-line-length = 120
exclude = docs,env,*.egg
max-complexity = 10
ignore = E402
ignore = E402,E722
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def get_version():
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
Expand Down

0 comments on commit 4824dfd

Please sign in to comment.