Initializing a dictionary of dictionaries for integer values with defaultdict [duplicate] - python

The error comes from publishDB = defaultdict(defaultdict({})) I want to make a database like {subject1:{student_id:{assignemt1:marks, assignment2:marks,finals:marks}} , {student_id:{assignemt1:marks, assignment2:marks,finals:marks}}, subject2:{student_id:{assignemt1:marks, assignment2:marks,finals:marks}} , {student_id:{assignemt1:marks, assignment2:marks,finals:marks}}}. I was trying to populate it as DB[math][10001] = a dict and later read out as d = DB[math][10001]. Since, I am on my office computer I can not try different module.
Am I on right track to do so?

Such a nested dict structure can be achieved using a recursive defaultdict "tree":
def tree():
return defaultdict(tree)
publishDB = tree()
At each level, the defaultdicts are instantiated with tree which is a zero-argument callable, as required.
Then you can simply assign marks:
publishDB[subject][student][assignment] = mark

defaultdict() requires that its first argument be callable: it must be a class that you want an instance of, or a function that returns an instance.
defaultdict({}) has an empty dictionary, which is not callable.
You likely want defaultdict(dict), as dict is a class that returns a dictionary when instantiated (called).
But that still doesn't solve the problem... just moves it to a different level. The outer defaultdict(...) in defaultdict(defaultdict(dict)) has the exact same issue because defaultdict(dict) isn't callable.
You can use a lambda expression to solve this, creating a one-line function that, when called, creates a defaultdict(dict):
defaultdict(lambda: defaultdict(dict))
You could also use the lambda at the lower level if you wanted:
defaultdict(lambda: defaultdict(lambda: {}))

Related

How to set an argument as a dictionary or variable in python

I am trying to make a function in python that creates dictionaries with custom names. The code I am using so far looks like this:
def PCreate(P):
P = {}
print('Blank Party Created')
The problem that I am having is that whenever I use the function, no matter what I put down for P, for example:
PCreate('Party1')
It creates a blank dictionary with the name 'P'. is there a way to make it create a dictionary with the name Party1?
It looks like you're confused with how variable names, and strings, and objects interact withing Python. When you have the function PCreate(P) you are saying that when the function is called, it will take on parameter, and within the function that parameter will be called P. This means that if you have the function,
def func(P):
print(P)
and call it three times,
func('two words')
func(4)
func([3, 'word'])
you will get the output:
two words
4
[3, 'word']
This is because the parameter P has no explicit type in Python. So, when you called your function with the argument 'Party1' the values looked like this
def PCreate(P):
# P is currently 'Party1'
P = {}
# P no longer is Party1, and now references {}
...
So you didn't assign {} to the variable with the name Party1, you overwrote the local variable P with a new empty dict.
I think you probably do not want to be doing what you're doing, but see this answer for more information on setting a variable using a string variable as its name.
What I recommend you do is create a function that returns your custom dictionaries, and assign the returned value to your custom name.
def new_custom_dict():
my_dict = {} # Pretend this is somehow custom
return my_dict
Party1 = my_custom_dict()
If you need the reference key to your new dictionary to be stored in a string, then you're in luck because that's what dictionaries are for!
You can first create a dictionary that will be used to store your custom named dictionaries:
dictionaries = {}
and when you want to add a new dictionary with a custom name, call this function
def insert_new_dictionary(dictionaries, dictionary_name):
dictionaries[dictionary_name] = {}
e.g.
insert_new_dictionary(dictionaries, 'Party1')
insert_new_dictionary(dictionaries, 'Party2')
would leave you with two dictionaries accessible by dictionaries['Party1'] and dictionaries['Party2']

Static methods for recursive functions within a class?

I'm working with nested dictionaries on Python (2.7) obtained from YAML objects and I have a couple of questions that I've been trying to get an answer to by reading, but have not been successful. I'm somewhat new to Python.
One of the simplest functions is one that reads the whole dictionary and outputs a list of all the keys that exist in it. I use an underscore at the beginning since this function is later used by others within a class.
class Myclass(object):
#staticmethod
def _get_key_list(d,keylist):
for key,value in d.iteritems():
keylist.append(key)
if isinstance(value,dict):
Myclass._get_key_list(d.get(key),keylist)
return list(set(keylist))
def diff(self,dict2):
keylist = []
all_keys1 = self._get_key_list(self.d,keylist)
all_keys2 = self._get_key_list(dict2,keylist)
... # More code
Question 1: Is this a correct way to do this? I am not sure whether it's good practice to use a static method for this reason. Since self._get_key_list(d,keylist) is recursive, I dont want "self" to be the first argument once the function is recursively called, which is what would happen for a regular instance method.
I have a bunch of static methods that I'm using, but I've read in a lot of places thay they could perhaps not be good practice when used a lot. I also thought I could make them module functions, but I wanted them to be tied to the class.
Question 2: Instead of passing the argument keylist to self._get_key_list(d,keylist), how can I initialize an empty list inside the recursive function and update it? Initializing it inside would reset it to [] every time.
I would eliminate keylist as an explicit argument:
def _get_keys(d):
keyset = set()
for key, value in d.iteritems():
keylist.add(key)
if isinstance(value, dict):
keylist.update(_get_key_list(value))
return keyset
Let the caller convert the set to a list if they really need a list, rather than an iterable.
Often, there is little reason to declare something as a static method rather than a function outside the class.
If you are concerned about efficiency (e.g., getting lots of repeat keys from a dict), you can go back to threading a single set/list through the calls as an explicit argument, but don't make it optional; just require that the initial caller supply the set/list to update. To emphasize that the second argument will be mutated, just return None when the function returns.
def _get_keys(d, result):
for key, value in d.iteritems():
result.add(key)
if isinstance(value, dict):
_get_keys(value, result)
result = set()
_get_keys(d1, result)
_get_keys(d2, result)
# etc
There's no good reason to make a recursive function in a class a static method unless it is meant to be invoked outside the context of an instance.
To initialize a parameter, we usually assign to it a default value in the parameter list, but in case it needs to be a mutable object such as an empty list in this case, you need to default it to None and the initialize it inside the function, so that the list reference won't get reused in the next call:
class Myclass(object):
def _get_key_list(self, d, keylist=None):
if keylist is None:
keylist = []
for key, value in d.iteritems():
keylist.append(key)
if isinstance(value, dict):
self._get_key_list(d.get(key), keylist)
return list(set(keylist))
def diff(self, dict2):
all_keys1 = self._get_key_list(self.d)
all_keys2 = self._get_key_list(dict2)
... # More code

sending one object as an argument for another when using dictionaries keys as objects

This is actually related to a previous question that I have asked about here:create multiple objects of a class with different arguments
I want to know if there is a way to send one of the objects as an argument for another when using dictionaries keys as objects.. e.g.:
objects={'obj1':['object1','Tom',10],'obj2':['object2','John',13]}
dic={name: MyClass(*args) for name, args in objects.items()}
in the normal coding I would write ....
obj1 = MyClass('object1','Tom',10)
obj2 = MyClass('object2','John',obj1)
but with the following structure, it doesn't accept passing the object obj1:
objects={'obj1':['object1','Tom',10],'obj2':['object2','John',obj1]}
dic={name: MyClass(*args) for name, args in objects.items()}
where it gives (NameError: global name 'obj1' is not defined):
so how can i do this when using dictionary keys as an objects in a right way?
updated.. error message has been added.
objects={'obj1':['object1','Tom',10],'obj2':['object2','John',obj1]} this wont work ever
however you could do something like
objects={'obj1':['object1','Tom',10],'obj2':['object2','John','obj1']}
fetch_ob = lambda x:objects.get(x,x)
dic = {name:MyClass(*list(map(fetch_ob,args))) for name,args in objects.items()}
but its kind of gross

what's the right way to put *arg in a tuple that can be sorted?

I want a dict or tuple I can sort based on attributes of the objects I'm using as arguments for *arg. The way I've been trying to do it just gives me AttributeErrors, which leads me to believe I'm doing it weird.
def function(*arg):
items = {}
for thing in arg:
items.update({thing.name:thing})
while True:
for thing in items:
## lots of other code here, basically just a game loop.
## Problem is that the 'turn order' is based on whatever
## Python decides the order of arguments is inside "items".
## I'd like to be able to sort the dict based on each object's
## attributes (ie, highest 'thing.speed' goes first in the while loop)
The problem is when I try to sort "items" based on an attribute of the objects I put into function(), it gives me "AttributeError: 'str' object has no attribute 'attribute'". Which leads me to believe I'm either unpacking *arg in a lousy way, or I'm trying to do something the wrong way.
while True:
for thing in sorted(items, key=attrgetter('attribute')):
...doesn't work either, keeps telling me I'm trying to manipulate a 'str' object. What am I not doing here?
arg already is a tuple you can sort by an attribute of each item:
def function(*args):
for thing in sorted(args, key=attrgetter('attribute')):
When you iterate over a dict, as sorted is doing, you just get the keys, not the values. So, if you want to use a dict, you need to do:
def function(*args):
# or use a dict comprehension on 2.7+
items = dict((thing.name, thing) for thing in args)
# or just items.values on 3+
for thing in sorted(items.itervalues(), key=attrgetter('attribute')):
to actually sort the args by an attribute. If you want the keys of the dict available as well (not necessary here because the key is also an attribute of the item), use something like:
for name, thing in sorted(items.iteritems(), key=lambda item: item[1].attribute):
Your items is a dict, you can't properly sort a dict. When you try to use it as an iterable, it silently returns its keys list, which is a list of strings. And you don't use your arg after creating a dict.
If you don't need dict lookup, as you just iterate through it, you can replace dict with list of 2-tuples (thing.name, thing), sort it by any attribute and iterate through it. You can also use collections.OrderedDict from Python 2.7 (it exists as a separate ordereddict package for earlier versions) if you really want both dict lookup and ordering.
{edit} Thanks to agf, I understood the problem. So, what I wrote below is a good answer in itself, but not when related to the question above... I let it here for the trace.
Looking to the answers, I may have not understood the question. But here's my understanding: as args is a tuple of arguments you give to your function, it's likely that none of these arguments is an object with a name attribute. But, looking to the errors you report, you're giving string arguments.
Maybe some illustration will help my description:
>>> # defining a function using name attribute
>>> def f(*args):
... for arg in args:
... print arg.name
>>> # defining an object with a name attribute
>>> class o(object):
... def __init__(self, name):
... self.name = name
>>> # now applying the function on the previous object, and on a string
>>> f( o('arg 1'), 'arg 2' )
arg 1
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
f(o('arg 1'), 'ets')
File "<pyshell#3>", line 3, in f
print arg.name
AttributeError: 'str' object has no attribute 'name'
This is failing as strings have no such attribute.
For me, in your code, there is a mistake: you're trying to use attribute name on your inputs, without ever verifying that they have such an attribute. Maybe you should test with hasattr first:
>>> if hasattr(arg, 'name'):
... print arg.name
... else:
... print arg
or with some inspection on the input, to verify if it's an instance of a given class, known to have the requested attribute.

Python getattr equivalent for dictionaries?

What's the most succinct way of saying, in Python, "Give me dict['foo'] if it exists, and if not, give me this other value bar"? If I were using an object rather than a dictionary, I'd use getattr:
getattr(obj, 'foo', bar)
but this raises a key error if I try using a dictionary instead (a distinction I find unfortunate coming from JavaScript/CoffeeScript). Likewise, in JavaScript/CoffeeScript I'd just write
dict['foo'] || bar
but, again, this yields a KeyError. What to do? Something succinct, please!
dict.get(key, default) returns dict[key] if key in dict, else returns default.
Note that the default for default is None so if you say dict.get(key) and key is not in dict then this will just return None rather than raising a KeyError as happens when you use the [] key access notation.
Also take a look at collections module's defaultdict class. It's a dict for which you can specify what it must return when the key is not found. With it you can do things like:
class MyDefaultObj:
def __init__(self):
self.a = 1
from collections import defaultdict
d = defaultdict(MyDefaultObj)
i = d['NonExistentKey']
type(i)
<instance of class MyDefalutObj>
which allows you to use the familiar d[i] convention.
However, as mikej said, .get() also works, but here is the form closer to your JavaScript example:
d = {}
i = d.get('NonExistentKey') or MyDefaultObj()
# the reason this is slightly better than d.get('NonExistent', MyDefaultObj())
# is that instantiation of default value happens only when 'NonExistent' does not exist.
# With d.get('NonExistent', MyDefaultObj()) you spin up a default every time you .get()
type(i)
<instance of class MyDefalutObj>

Categories

Resources