Commit 4756f5c3cffaad3610f1b190b2e2fe05bd7dea1f

Authored by kvik
1 parent 8c4c6efc

Add functions to work with dictionaries

... ... @@ -4,7 +4,7 @@ from .local_time import get_now, TZ
4 4 from copy import deepcopy
5 5 from datetime import timedelta
6 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 8 from haversine import haversine
9 9 from json import load, loads
10 10 from jsonpath_rw import parse as parse_path
... ... @@ -63,7 +63,9 @@ from restkit import request
63 63 import os
64 64 import re
65 65
  66 +
66 67 NUM_TYPES = (int, long, float)
  68 +STR_TYPES = (str, unicode)
67 69
68 70
69 71 def get_current_tzdate():
... ... @@ -587,3 +589,17 @@ def convert_amount_string_to_float(amount_string):
587 589 def compare_rationale_types(type1, type2):
588 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