Package: pyobjectdict
PyObjectDictPython dictionaries for more convenient useOverviewThe problemWhen it comes to the Pythondictsyntax, what makes it clanky and annoying is the need to always pass keys as strings and use square brackets when accessing adict's value:x = {'a': 1, 'b': 2}
value = x['b'] # we have to write square brackets and use strings inside themIt is made quite concise in JavaScript language where all you need is access your value through the dot syntax like the following:x = {'a': 1, 'b': 2}
value = x.b // no need to use square brackets or strings!The solutionThis simple package introduces a new Pythondictobject which has some similarities to the JavaScript object in terms of dot syntax.
The object is calledPyObjectDict.How to useimport the package and try to initialize your PyObjectDict in a preferred way:from object_dict import PyObjectDict

a = PyObjectDict({'a': 2, 'b': 3, 'c': 10, 'd': 8})
b = PyObjectDict.form(**{'a': 2, 'b': 3, 'c': 4, 'g': 8})Note:PyObjectDictconstructor works with types:dict,OrderedDict, andPyObjectDict(to create a copy)you may add new attributes to the object by simply using the dot syntax:a.e = 1
a.f = 1
a.g = 1
a.h = 1
a.i = 1you may then easily access any attribute by using:print(a.a) # will print 2
print(a.e) # will print 1ThePyObjectDictalso exposes a lot of additional convenience methods as well as standarddictmethods.
They are listed in the table above and an example of their usage can be seen below it.Methods of PyObjectDictNameParametersReturn valueDescriptionlength-intreturns the lenght of the dictionarykeys-listreturns the list of keys of the dictionaryitems-listoftuplesreturns the list of tuples containing key, value pairs of each item in the dicrionaryiteritems-iteratoriterator foritemsmethodvalues-listreturns the list of values of the dictionaryupdateother(PyObjectDict)Noneupdates onePyObjectDictwith the other (merges them) in place. Returns nothinggetkey(str),default(None)type ofkeyor type ofdefaultparameterreturns value of thekeyif key exists ordefaultparameter (which isNoneby default)addother(PyObjectDict)PyObjectDictmerges twoPyObjectDicts and returns a merged copyadd_each_valueother(PyObjectDict)PyObjectDictmerges the keys of twoPyObjectDicts and adds the corresponding key values together. returns a merged copysubother(PyObjectDict)PyObjectDictsubtracts onePyObjectDictfrom another and returns a copy containing not intersecting keys of the firstPyObjectDictwith their valuessub_each_valueother(PyObjectDict)PyObjectDictsubtracts the values of the corresponding keys of the firstPyObjectDictfrom the second one. Returns a copysub_by_valueother(PyObjectDict)PyObjectDictthe same assubbut subtraction happens by the uniquekey: valuepairs not just the unique keysOperators of PyObjectDictNameParametersReturn valueDescription+other(PyObjectDict)PyObjectDictthe same asaddmethod+=other(PyObjectDict)PyObjectDictthe same asaddmethod but assigns the result to thePyObjectDictto the left side of the operator|other(PyObjectDict)PyObjectDictthe same asaddmethod|=other(PyObjectDict)PyObjectDictthe same as+=operator-other(PyObjectDict)PyObjectDictthe same assubmethod-=other(PyObjectDict)PyObjectDictthe same assubmethod but assigns the result to thePyObjectDictto the left side of the operatorDunder methods of PyObjectDictNameParametersReturn valueDescriptionlen-intreturns the lenght of the dictionarystr-strreturns the string representation of the dicrionaryConstructors of PyObjectDictNameParametersReturn valueDescriptionPyObjectDict-PyObjectDictcreates anPyObjectDictfromdict,OrderedDict, orPyObjectDict(copy)PyObjectDict.from-PyObjectDictcreates anPyObjectDictfrom the key value pairs that are successively passed to the constructorAn Example of Usagefrom pyobjectdict import PyObjectDict

a = PyObjectDict({'a': 2, 'b': 3, 'c': 10, 'd': 8})
b = PyObjectDict.form(**{'a': 2, 'b': 3, 'c': 4, 'g': 8})

print('a:', a)
print('b:', b)

c = a.sub_by_value(b)
print('c:', c)

print('c.length():', c.length())
print('c.keys():', c.keys())
print('c.items():', c.items())
print('c.iteritems():', list(c.iteritems()))
print('c.values():', c.values())
print('c.update(PyObjectDict({"a": 1, "b": 2, "c": 3})):', c.update(PyObjectDict({'a': 1, 'b': 2, 'c': 3})))
print('c.update(PyObjectDict({"a": 1, "b": 2, "c": 3})):', c.update(PyObjectDict({'a': 1, 'b': 2, 'c': 3})))

print('iteratibng over c:')
for k in c:
    print('key: ', k)


print('a + b:', a + b)
print('a | b:', a | b)
print('a.add(b):', a.add(b))
print('a.add_each_value(b):', a.add_each_value(b))
a.update(b)
print('a after a.update(b):', a)

a.cc = 12
b.cc = 12
print('a after adding "cc": 12:', a)
print('b after adding "cc": 12:', b)

print('a - b:', a - b)
print('a.sub(b):', a.sub(b))
print('a.sub_by_value(b):', a.sub_by_value(b))
print('a.sub_each_value(b):', a.sub_each_value(b))

a += b
print('a after a += b:', a)
a -= b
print('a after a -= b:', a)
a |= b
print('a after a |= b', a)The result of the code above will be:a: PyObjectDict({'a': 2, 'b': 3, 'c': 10, 'd': 8})
b: PyObjectDict({'a': 2, 'b': 3, 'c': 4, 'g': 8})
c: PyObjectDict({'c': 10, 'd': 8})
c.length(): 2
c.keys(): ['c', 'd']
c.items(): [('c', 10), ('d', 8)]
list(c.iteritems()): [('c', 10), ('d', 8)]
c.values(): [10, 8]
c.update(PyObjectDict({"a": 1, "b": 2, "c": 3})): None
c.update(PyObjectDict({"a": 1, "b": 2, "c": 3})): None
iteratibng over c:
key:  c
key:  d
key:  a
key:  b
a + b: PyObjectDict({'a': 2, 'b': 3, 'c': 4, 'd': 8, 'g': 8})
a | b: PyObjectDict({'a': 2, 'b': 3, 'c': 4, 'd': 8, 'g': 8})
a.add(b): PyObjectDict({'a': 2, 'b': 3, 'c': 4, 'd': 8, 'g': 8})
a.add_each_value(b): PyObjectDict({'a': 4, 'b': 6, 'c': 14, 'd': 8})
a after a.update(b): PyObjectDict({'a': 2, 'b': 3, 'c': 4, 'd': 8, 'g': 8})
a after adding "cc": 12: PyObjectDict({'a': 2, 'b': 3, 'c': 4, 'd': 8, 'g': 8, 'cc': 12})
b after adding "cc": 12: PyObjectDict({'a': 2, 'b': 3, 'c': 4, 'g': 8, 'cc': 12})
a - b: PyObjectDict({'d': 8})
a.sub(b): PyObjectDict({'d': 8})
a.sub_by_value(b): PyObjectDict({'d': 8})
a.sub_each_value(b): PyObjectDict({'a': 0, 'b': 0, 'c': 0, 'd': 8, 'g': 0, 'cc': 0})
a after a += b: PyObjectDict({'a': 2, 'b': 3, 'c': 4, 'd': 8, 'g': 8, 'cc': 12})
a after a -= b: PyObjectDict({'d': 8})
a after a |= b PyObjectDict({'d': 8, 'a': 2, 'b': 3, 'c': 4, 'g': 8, 'cc': 12})NotesRemember that in JavaScripts objects it is possible to access a non-existing attribute and receve anundefinedvalue.
However, since Python has no concept ofundefinedand checking forNonecould be missleading (+ the limitations of Python objects), you cannot access an unexisting attribute ofPyObjectDictwithout triggering anAttributeErrorexception. Alternatively you can usegetmethod as in standard Python dicts to check if the attribute exists before accessing it.