Commit 4756f5c3cffaad3610f1b190b2e2fe05bd7dea1f
1 parent
8c4c6efc
Add functions to work with dictionaries
Showing
1 changed file
with
17 additions
and
1 deletions
| @@ -4,7 +4,7 @@ from .local_time import get_now, TZ | @@ -4,7 +4,7 @@ from .local_time import get_now, TZ | ||
| 4 | from copy import deepcopy | 4 | from copy import deepcopy |
| 5 | from datetime import timedelta | 5 | from datetime import timedelta |
| 6 | from dateutil.parser import parse | 6 | from dateutil.parser import parse |
| 7 | -from dpath.util import new as xpathnew | 7 | +from dpath.util import delete as xpathdelete, get as xpathget, new as xpathnew |
| 8 | from haversine import haversine | 8 | from haversine import haversine |
| 9 | from json import load, loads | 9 | from json import load, loads |
| 10 | from jsonpath_rw import parse as parse_path | 10 | from jsonpath_rw import parse as parse_path |
| @@ -63,7 +63,9 @@ from restkit import request | @@ -63,7 +63,9 @@ from restkit import request | ||
| 63 | import os | 63 | import os |
| 64 | import re | 64 | import re |
| 65 | 65 | ||
| 66 | + | ||
| 66 | NUM_TYPES = (int, long, float) | 67 | NUM_TYPES = (int, long, float) |
| 68 | +STR_TYPES = (str, unicode) | ||
| 67 | 69 | ||
| 68 | 70 | ||
| 69 | def get_current_tzdate(): | 71 | def get_current_tzdate(): |
| @@ -587,3 +589,17 @@ def convert_amount_string_to_float(amount_string): | @@ -587,3 +589,17 @@ def convert_amount_string_to_float(amount_string): | ||
| 587 | def compare_rationale_types(type1, type2): | 589 | def compare_rationale_types(type1, type2): |
| 588 | return set(type1) == set(type2) | 590 | return set(type1) == set(type2) |
| 589 | 591 | ||
| 592 | + | ||
| 593 | +def delete_from_dictionary(variable, path): | ||
| 594 | + if not type(path) in STR_TYPES: | ||
| 595 | + raise TypeError('path must be one of: ' + | ||
| 596 | + str([x.__name__ for x in STR_TYPES])) | ||
| 597 | + return xpathdelete(variable, path, separator='.') | ||
| 598 | + | ||
| 599 | + | ||
| 600 | +def dictionary_should_not_contain_path(dictionary, path): | ||
| 601 | + try: | ||
| 602 | + xpathget(dictionary, path, separator='.') | ||
| 603 | + except KeyError: | ||
| 604 | + return | ||
| 605 | + raise RuntimeError("Dictionary contains path '%s'." % path) |
Please
register
or
login
to post a comment