micropython

internal_tests/objectset.py

476:a6ddb1e042ea
2011-10-31 Paul Boddie Switched the attribute usage empty set representation from a new ObjectSet instance to None, introducing logic to handle the addition of attribute usage observations to such empty sets and to detect empty sets when type deductions are being made. This reduces the amount of allocated instances enormously. Changed the combination function for mapping dictionaries (mapping from names to object sets) to not propagate observations for names to locations where such names are not defined.
     1 #!/usr/bin/env python     2      3 from micropython.common import ObjectSet, combine_mapping_dicts     4      5 o1 = ObjectSet()     6 o1.add("a")     7 o1.add("b")     8 o1.add("c")     9 print "o1 =", o1    10     11 o2 = ObjectSet()    12 o2.add("b")    13 o2["c"] = ["x"]    14 o2.add("d")    15 o2["d"] += ["p", "q"]    16 print "o2 =", o2    17     18 assert o1 != o2    19 print "o1 == o2", o1 == o2    20 print "o1 != o2", o1 != o2    21     22 o12 = o1.merge(o2)    23 print "o1 =", o1    24 print "o2 =", o2    25 print "o12 =", o12    26     27 o12_keys = o12.keys()    28 o12_keys.sort()    29 assert o12_keys == ["a", "b", "c", "d"]    30 print "o12_keys =", o12_keys    31     32 o1_copy = ObjectSet(o1)    33 assert o1 == o1_copy    34 print "o1 =", o1    35 print "o1_copy =", o1_copy    36 print "o1 == o1_copy", o1 == o1_copy    37     38 o1_copy.update(o2)    39 assert o1 != o1_copy    40 assert o12 == o1_copy    41 print "o1_copy =", o1_copy    42 print "o1 != o1_copy", o1 != o1_copy    43 print "o12 == o1_copy", o12 == o1_copy    44     45 o3 = ObjectSet()    46 o3.add("c")    47 o3["c"].append("y")    48 print "o3 =", o3    49     50 d1 = {'a' : [ObjectSet(['f', 'g']), ObjectSet(['f', 'h'])]}    51 d2 = {'a' : [ObjectSet(['f']), ObjectSet(['e', 'f', 'g'])]}    52 d3 = combine_mapping_dicts(d1, d2)    53 print "d1 =", d1    54 print "d2 =", d2    55 print "d3 =", d3    56     57 d4 = {'a' : None}    58 d5 = combine_mapping_dicts(d1, d4)    59 print "d4 =", d4    60 print "d5 =", d5    61     62 # vim: tabstop=4 expandtab shiftwidth=4