Example #1
0
def test_dictionaries_are_equal():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['a'] = 1
    d2['b'] = 2

    assert d1 == d2
Example #2
0
def test_dictionaries_are_not_equal_order():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['b'] = 2
    d2['a'] = 1

    assert d1 != d2
Example #3
0
def test_dictionaries_are_not_equal_order():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['b'] = 2
    d2['a'] = 1

    assert d1 != d2  # this is using the __str__ method.. They are diff order
Example #4
0
def test_dictionaries_are_not_equal_elements():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['c'] = 1
    d2['d'] = 2

    assert d1 != d2
Example #5
0
def test_add_two_dicts_with_unique_keys():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['c'] = 3
    d2['d'] = 4

    d3 = d1 + d2

    assert d3 == {'a': 1, 'b': 2, 'c': 3, 'd': 4}
def ordered_unbunchify(x):
    """ Recursively converts a OrderedBunch into a dictionary.
        
        >>> b = OrderedBunch(foo=OrderedBunch(lol=True), hello=42, ponies='are pretty!')
        >>> ordered_unbunchify(b)
        {'ponies': 'are pretty!', 'foo': {'lol': True}, 'hello': 42}
        
        ordered_unbunchify will handle intermediary dicts, lists and tuples (as well as
        their subclasses), but ymmv on custom datatypes.
        
        >>> b = OrderedBunch(foo=['bar', OrderedBunch(lol=True)], hello=42, 
        ...         ponies=('are pretty!', OrderedBunch(lies='are trouble!')))
        >>> ordered_unbunchify(b) #doctest: +NORMALIZE_WHITESPACE
        {'ponies': ('are pretty!', {'lies': 'are trouble!'}), 
         'foo': ['bar', {'lol': True}], 'hello': 42}
        
        nb. As dicts are not hashable, they cannot be nested in sets/frozensets.
    """
    if isinstance(x, OrderedDict):
        return OrderedDict(
            (k, ordered_unbunchify(v)) for k, v in x.iteritems())
    elif isinstance(x, dict):
        return dict((k, ordered_unbunchify(v)) for k, v in x.iteritems())
    elif isinstance(x, (list, tuple)):
        return type(x)(ordered_unbunchify(v) for v in x)
    else:
        return x
Example #7
0
def test_add_two_dicts_with_repeated_keys():
    d1 = OrderedDict()
    d1['a'] = 1
    d1['b'] = 2

    d2 = OrderedDict()
    d2['c'] = 3
    d2['a'] = 4  # repeated

    d3 = d1 + d2

    assert d3 == {
        'a': 4,  # updated
        'b': 2,
        'c': 3
    }
Example #8
0
def test_get_items_with_existent_items():
    d = OrderedDict()

    d['language'] = 'Python'
    d['other'] = ('a', 'b', 'c')
    assert d['language'] == 'Python'
    assert d['other'] == ('a', 'b', 'c')
Example #9
0
    def visit(self, ast_node, compiled_children_by_node, ancestors=None):
        if ancestors is None:
            ancestors = []

        childs_ancestors = [ast_node] + ancestors
        print(dump(ast_node, include_attributes=True))
        compiled_children = OrderedDict()
        for field, value in ast.iter_fields(ast_node):
            # print("iterating:", get_name(ast_node), field)
            if isinstance(value, list):
                compiled_children[field] = [
                    self.visit(item, compiled_children_by_node,
                               childs_ancestors) for item in value
                    if isinstance(item, ast.AST)
                ]
            elif isinstance(value, ast.AST):
                compiled_children[field] = self.visit(
                    value, compiled_children_by_node, childs_ancestors)
        compile_func = getattr(compilers, "compile_" + get_name(ast_node))
        compiled_children_by_node[ast_node] = compiled_children.copy()

        # `ancestors` argument is optional
        try:
            return compile_func(ast_node, compiled_children, ancestors)
        except TypeError as e:
            if str(e).startswith("compile_") and str(e).endswith(
                    "takes 2 positional arguments but 3 were given"):
                return compile_func(ast_node, compiled_children)
            else:
                print("cannot handle error", str(e))
                raise e
        except Exception as e:
            raise e
 def __recursiveUpdate(left, right):
     for key, value in right.iteritems():
         if isinstance(value, dict):
             tmp = __recursiveUpdate(left.get(key, OrderedDict()), value)
             left[key] = tmp
         else:
             left[key] = right[key]
     return left
Example #11
0
def test_get_items_with_missing_items():
    d = OrderedDict()

    d['language'] = 'Python'

    assert d['language'] == 'Python'
    with pytest.raises(KeyError):
        d['other-key']
Example #12
0
def test_membership():
    d = OrderedDict()

    d['language'] = 'Python'
    d['other'] = ('a', 'b', 'c')
    assert 'language' in d
    assert 'other' in d

    assert 'not-found' not in d
Example #13
0
        def get_dict(source, offset, size, original):
            #assert is_md5sum(source)
            assert offset >= 0
            assert size >= 0
            assert type(original) == bool

            return OrderedDict([("source", source), ("offset", offset),
                                ("size", size), ("original", original),
                                ("repeat", 1)])
def readDictionary(file):
    """ read csv file and parse it to dictionary """

    # define local function to parse last token in proper Python types
    def __parseToken(token):

        return {
            's': lambda x: x,
            'i': lambda x: int(x),
            'd': lambda x: float(x),
            'f': lambda x: __findFunction(x),
            't': lambda x: tuple(map(__parseToken, x.split("|"))),
            'b': lambda x: bool(x),
            'n': lambda x: None
        }[token[0]](token[1:])

    def __findFunction(string):
        # does not allow for nested functions right now
        # split name into parts
        string = string.split(".")
        # import module
        module = __import__(string[0], globals(), locals(), [], -1)
        # return function object
        return module.__dict__[string[1]]

    # recursively merge dictionary right into dictionary left
    def __recursiveUpdate(left, right):
        for key, value in right.iteritems():
            if isinstance(value, dict):
                tmp = __recursiveUpdate(left.get(key, OrderedDict()), value)
                left[key] = tmp
            else:
                left[key] = right[key]
        return left

    # create dictionary for output
    out_dict = OrderedDict()

    # open file and iterate over all lines expects empty ones
    f = open(file, 'r')
    for line in f:
        if (line != "\n") and (not line.startswith("#")):

            # split line into segments
            items = line.strip().split("||")

            # parse last item
            tmp_dict = __parseToken(items[-1])

            # create hierachy
            for item in items[:-1:][::-1]:
                tmp_dict = {item: tmp_dict}

            # merge temporaly built dictionary into output dictionary
            __recursiveUpdate(out_dict, tmp_dict)
    return out_dict
Example #15
0
    def analyzeDataSet(self, state, data):
        timestamp = data[state['mainid']][state['subid']]["timestamp"]

        try:
            value = data[state['mainid']][state['subid']][self.field]

            if self.differential_mode:
                if state['last_value'] is None:
                    state['last_value'] = data[state['mainid']][
                        state['subid']][self.field]
                    return
                t = value - state['last_value']
            else:
                t = value

        except KeyError:
            return (self.name, state['mainid'], state['subid'], "KeyError",
                    timestamp, timestamp, "%s not in data" % self.field,
                    str(sys.exc_info()))

        except TypeError:
            return (self.name, state['mainid'], state['subid'], "TypeError",
                    timestamp, timestamp, "%s not in data" % self.field,
                    str(sys.exc_info()))

        state['st'] = self.p_st * t + (1 - self.p_st) * state['st']
        state['ewmv'] = self.p_ewmv * (t - state['st'])**2 + (
            1 - self.p_ewmv) * state['ewmv']

        lower_bound = state['st'] - self.L * math.sqrt(
            state['ewmv'] * self.p_st / (2 - self.p_st))
        upper_bound = state['st'] + self.L * math.sqrt(
            state['ewmv'] * self.p_st / (2 - self.p_st))

        parameterdump = OrderedDict([("mainid", state['mainid']),
                                     ("subid", state['subid']),
                                     ("last_value", state['last_value']),
                                     ("L", self.L), ("st", state['st']),
                                     ("p_st", self.p_st),
                                     ("ewmv", state['ewmv']),
                                     ("p_ewmv", self.p_ewmv), ("value", value),
                                     ("t", t), ("lower_bound", lower_bound),
                                     ("upper_bound", upper_bound)])

        state['last_value'] = data[state['mainid']][state['subid']][self.field]

        # print >> sys.stderr, parameterdump

        if lower_bound - t > 6e-14 and lower_bound - t > self.tolerance:
            return (self.name, state['mainid'], state['subid'], "LowValue",
                    timestamp, timestamp, "%s < %s" % (t, lower_bound),
                    str(parameterdump))
        if t - upper_bound > 6e-14 and t - upper_bound > self.tolerance:
            return (self.name, state['mainid'], state['subid'], "HighValue",
                    timestamp, timestamp, "%s > %s" % (t, upper_bound),
                    str(parameterdump))
Example #16
0
def test_ordered_dict_get_item():
    o = OrderedDict()
    o['a'] = 1
    o['b'] = 'asdf'

    assert_eq(1, o['a'])
    assert_eq(1, o.get('a'))
    assert_eq('asdf', o['b'])
    assert_eq('asdf', o.get('b'))
    assert_eq(None, o.get('c'))
Example #17
0
def test_count_items():
    d = OrderedDict()

    assert len(d) == 0

    d['language'] = 'Python'
    assert len(d) == 1

    d['other'] = ('a', 'b', 'c')
    assert len(d) == 2
Example #18
0
 def get_recipe(self):
     assert self.closed
     if self.recipe == None:
         self.recipe = OrderedDict([("md5sum", self.md5summer.hexdigest()),
                                    ("size", len(self.tail_buffer)),
                                    ("method", "concat"),
                                    ("pieces", list(self.__seq2rec()))])
         # We now have a complete and useful recipe. But can it be improved?
         self.__polish_recipe_tail()
         self.__polish_recipe_repeats()
     return self.recipe
Example #19
0
def test_ordered_dict_popitem():
    o = OrderedDict()
    o['a'] = 1
    o['b'] = 'asdf'

    assert_eq(1, o.popitem(True))
    assert_eq(1, len(o))

    o['a'] = 1
    assert_eq(1, o.popitem())
    assert_eq(1, len(o))
Example #20
0
def merge_plain_and_compiled_fields(node, compiled_children):
    result = OrderedDict()
    # print("in merge_plain_and_compiled_fields......")
    # print(compiled_children)
    for name, field in ast.iter_fields(node):
        # print(name)
        if name in compiled_children:
            result[name] = compiled_children[name]
        else:
            result[name] = field
    return result
Example #21
0
def test_set_new_items():
    d = OrderedDict()
    assert d.keys() == []
    assert d.values() == []

    d['language'] = 'Python'
    assert d.keys() == ['language']
    assert d.values() == ['Python']

    d['other'] = ('a', 'b', 'c')
    assert d.keys() == ['language', 'other']
    assert d.values() == ['Python', ('a', 'b', 'c')]
def create_fieldDict(backend, convert_generic, convert_snmp, csv_file):
    field_map = readDictionary(
        os.path.join(os.path.dirname(__file__), '..', 'config', csv_file))
    fieldDict = OrderedDict()

    for key, dictionary in field_map.iteritems():
        name = dictionary['name']
        table = dictionary['table']
        if (not 'only' in dictionary) or (dictionary['only'] == backend):
            if not table in fieldDict:
                fieldDict[table] = OrderedDict()

            if 'use_type' in dictionary:
                if dictionary['use_type'] == 'predef_value':
                    fieldDict[table][name] = dictionary['value']
                else:
                    fieldDict[table][name] = convert_generic(
                        dictionary['use_type'])
            else:
                fieldDict[table][name] = convert_snmp(dictionary['snmp_type'])
    return fieldDict
Example #23
0
 def compile(self):
     print("...in compile() of ", self.name, self.field_name)
     if self.name is not None:
         method = getattr(compilers, "compile_" + self.name)
         if self.is_leaf():
             print("compiling", self.name)
             return method(node=self, compiled_children=OrderedDict())
         return str(method(
             node=self,
             compiled_children=OrderedDict(
                 (child.name, child.compile())
                 for child in self.children()
             )
         ))
     # else:
     # This instance is a pseudo node to increase hierarchy
     # e.g. function_def -> body is a list of nodes
     # so these nodes cannot be direct children of function_def.
     # body itself is no ast node.
     # In this case we return a list instead of a string.
     return compilers.compile_list(self.children())
Example #24
0
def test_ordered_dict_set_item():
    o = OrderedDict()
    o['a'] = 1
    o['b'] = 'asdf'

    assert_eq(1, o['a'])
    assert_eq('asdf', o['b'])

    try:
        o['c']
        assert False, 'Should throw key error'
    except KeyError:
        pass
Example #25
0
def test_ordered_dict_contains():
    o = OrderedDict()
    o['a'] = 1
    o['b'] = 'asdf'

    assert 'a' in o
    assert 'b' in o
    assert 'c' not in o

    del o['a']
    assert 'a' not in o

    o['c'] = 2
    assert 'c' in o
Example #26
0
def test_items_method():
    d1 = OrderedDict()

    d1['a'] = 1
    d1['b'] = 2
    d1['c'] = 3
    d1['d'] = 4

    assert d1.items() == [
        ('a', 1),
        ('b', 2),
        ('c', 3),
        ('d', 4),
    ]
Example #27
0
def test_ordered_dict_del_item():
    o = OrderedDict()
    o['a'] = 1
    o['b'] = 'asdf'

    assert_eq(1, o['a'])
    assert_eq('asdf', o['b'])

    del o['a']
    assert_eq(None, o.get('a'))
    try:
        o['a']
        assert False, 'Should throw key error'
    except KeyError:
        pass
Example #28
0
def test_set_repeated_items():
    d = OrderedDict()
    assert d.keys() == []
    assert d.values() == []

    d['language'] = 'Python'
    assert d.keys() == ['language']
    assert d.values() == ['Python']

    d['other'] = ('a', 'b', 'c')
    assert d.keys() == ['language', 'other']
    assert d.values() == ['Python', ('a', 'b', 'c')]

    # Same key again:
    d['language'] = 'Ruby'
    assert d.keys() == ['language', 'other']
    assert d.values() == ['Ruby', ('a', 'b', 'c')]
Example #29
0
def test_str_and_repr():
    d = OrderedDict()
    assert str(d) == "{}"
    assert repr(d) == str(d)

    d['a'] = 1
    assert str(d) == "{'a': 1}"
    assert repr(d) == str(d)

    d['language'] = 'Python'

    assert str(d) == "{'a': 1, 'language': 'Python'}"
    assert repr(d) == str(d)

    d['points'] = (13, 4)
    assert str(d) == "{'a': 1, 'language': 'Python', 'points': (13, 4)}"
    assert repr(d) == str(d)
Example #30
0
def test_ordered_dict_len():
    o = OrderedDict()
    o['a'] = 1
    o['b'] = 'asdf'

    assert_eq(1, o['a'])
    assert_eq('asdf', o['b'])
    assert_eq(2, len(o))

    del o['a']
    assert_eq(1, len(o))

    o['c'] = 'qwer'
    assert_eq(2, len(o))

    del o['c']
    del o['b']
    assert_eq(0, len(o))