Odd behavior of function return - python

I've spent many hours searching through all the "my function returns none" and "nested dict search" messages and none apply specifically nor do any resolve my issue.
I made a function to search a nested dictionary and return the path. This works great! I can print the results from within the function but the return immediately below the print returns None. Maybe I've been looking at it too long and it probably is right in front of my face but I'm just not seeing what's wrong here. Here's my complete code:
def search(v, searchterm, vid, path=(),):
if isinstance(v, dict):
for k, v2 in v.items():
p2 = path + ('{}'.format(k),)
search(v2, searchterm, vid, p2)
else:
if searchterm in v:
a = {}
a[0] = path
a[1] = v[vid]
print(a)
return(a)
def main():
mydata = {}
mydata[1] = {}
mydata[1][1] = 'data-1-1','reason-1-1','notes-1-1'
mydata[1][2] = 'data-1-2','reason-1-2','notes-1-2'
mydata[1][3] = 'data-1-3','reason-1-3','notes-1-3'
mydata[1][4] = 'data-1-4','reason-1-4','notes-1-4'
mydata[1][5] = 'data-1-5','reason-1-5','notes-1-5'
mydata[1][6] = 'data-1-6','reason-1-6','notes-1-6'
mydata[1][7] = 'data-1-7','reason-1-7','notes-1-7'
mydata[1][8] = 'data-1-8','reason-1-8','notes-1-8'
mydata[1][9] = 'data-1-9','reason-1-9','notes-1-9'
mydata[1][10] = 'data-1-10','reason-1-10','notes-1-10'
mydata[2] = {}
mydata[2][1] = 'data-2-1','reason-2-1','notes-2-1'
mydata[2][2] = 'data-2-2','reason-2-2','notes-2-2'
mydata[2][3] = 'data-2-3','reason-2-3','notes-2-3'
mydata[2][4] = 'data-2-4','reason-2-4','notes-2-4'
mydata[2][5] = 'data-2-5','reason-2-5','notes-2-5'
mydata[2][6] = 'data-2-6','reason-2-6','notes-2-6'
mydata[2][7] = 'data-2-7','reason-2-7','notes-2-7'
mydata[2][8] = 'data-2-8','reason-2-8','notes-2-8'
mydata[2][9] = 'data-2-9','reason-2-9','notes-2-9'
mydata[2][10] = 'data-2-10','reason-2-10','notes-2-10'
b = search(mydata,'reason-2-4', 2)
print(b)
if __name__ == '__main__':
main()
The results:
{0: ('2', '4'), 1: 'notes-2-4'}
None
You can see the print works great from within the function but the return and print from main returns None. I've been programming in Python for a few years now with many functions, classes and methods with returns written but this one has me stuck.

You're making a recursive call and the print statement is issued in the nested call. However, the return value of search is not used, that's why it never reaches the main function.
Below, I have added a nested variable that is checked, and if anything was found that is actually returned.
def search(v, searchterm, vid, path=(),):
if isinstance(v, dict):
for k, v2 in v.items():
p2 = path + ('{}'.format(k),)
nested = search(v2, searchterm, vid, p2)
if nested:
# before, nothing was ever returned here
return nested
else:
if searchterm in v:
a = {}
a[0] = path
a[1] = v[vid]
print(a)
return(a)
Not related, but in here you could make great use of the powerful dict literals of python
mydata = {
1: {
1: ('data-1-1', 'reason-1-1', 'notes-1-1'),
2: ('data-1-2', 'reason-1-2', 'notes-1-2')
2: {
1: ('data-2-1', 'reason-2-1', 'notes-2-1'),
2: ('data-2-2', 'reason-2-2', 'notes-2-2')
}
Also, if all your dictionary keys are int, you might as well use a list.

Out of the 3 paths in search(), only 1 returns something:
def search(v, searchterm, vid, path=(),):
if isinstance(v, dict):
# Path 1
for k, v2 in v.items():
p2 = path + ('{}'.format(k),)
search(v2, searchterm, vid, p2)
# Nothing returned here
else:
if searchterm in v:
# Path 2
a = {}
a[0] = path
a[1] = v[vid]
print(a)
return(a)
else:
# Path 3
# Nothing returned here
Path 1 calls Path 2 which explains why there is something printed but not returned to main().

Related

For loop is stopping

I am trying to create a function that returns the similarity of words, but the loop stops after processing only the first argument! For example, if I execute example.py hello there the program returns this:
hello is close to:
held, heel, helpt, hele, Hallo, het, helaas, half, helden, heb, veel, Meld, zelf, heeft, beeld, alle, wel, Rel, Geld, cel, geld, Alle, hoezo,
there is close to:
Here is my code:
def create_data():
data =defaultdict(int)
value = 0
for line in sys.stdin:
[ident, user, text, terms] = line.rstrip().split('\t')
for word in terms.split():
data[word] = value
return data
def find_closest(word):
data = create_data()
data_with_distance= defaultdict(int)
for key in data:
distance = lev_dist(word, key)
data_with_distance[key] = distance
return {k: v for k, v in sorted(data_with_distance.items(), key=lambda item: item[1])}
def main():
if len(sys.argv) > 1:
for w in sys.argv[1:]:
print("\n",w, "is close to:\n")
closest = find_closest(w)
closest_words = [k for k, v in closest.items() if v < 4]
#minimal_distance = list(closest.values())[0]
for close in closest_words:
print(close, end=", ")
else:
sys.stderr.write("no argument\n")
if __name__ == '__main__':
main()
You need to cache the results of create_data if you want to reuse it:
def find_closest(word, data): # take data as param here
data_with_distance= defaultdict(int)
for key in data:
distance = lev_dist(word, key)
data_with_distance[key] = distance
return {k: v for k, v in sorted(data_with_distance.items(), key=lambda item: item[1])}
def main():
data = create_data() # load data from stdin ONCE
if len(sys.argv) > 1:
for w in sys.argv[1:]:
print("\n",w, "is close to:\n")
closest = find_closest(w, data) # pass data as param here
closest_words = [k for k, v in closest.items() if v < 4]
#minimal_distance = list(closest.values())[0]
for close in closest_words:
print(close, end=", ")
Another option would be to stick a caching decorator on create_data:
from functools import cache
#cache
def create_data():
data = defaultdict(int)
value = 0
for line in sys.stdin:
[ident, user, text, terms] = line.rstrip().split('\t')
for word in terms.split():
data[word] = value
return data
This "fixes" the function by making it cache the result from the first time you run it, and return the same result on subsequent calls instead of actually executing the function.
In a function that takes parameters, the caching would happen based on the parameters; since this function takes no parameters, it'll just cache a single return value. If the function had a desirable side effect you would not want to cache it like this, but in this case the side effect is undesirable so sticking a #cache on it is a very easy solution.

xmltodict returns string instead of OrderedDict [duplicate]

The following Code produces an error, if there is only one "car" in "garage":
import xmltodict
mydict = xmltodict.parse(xmlstringResults)
for carsInGarage in mydict['garage']['car']:
# do something...
The Reason is that mydict['garage']['car'] is only a list if there is more than one element of "car". So I did something like this:
import xmltodict
mydict = xmltodict.parse(xmlstringResults)
if isinstance(mydict['garage']['car'], list):
for carsInGarage in mydict['garage']['car']:
# do something for each car...
else:
# do something for the car
to get the code to run. But for more advanced operations this is no solution.
Does someone know some kind of function to use, even if there is only one element?
This problem is discussed in this issue on Github. The xmltodict package now supports
d = xmltodict.parse(s, force_list={'car'})
Although this still doesn't create an empty list if the field is absent.
This is of course not an elegant way, but this is what i have done to get the code run (if someone hase the same probleme an found this via google):
import xmltodict
def guaranteed_list(x):
if not x:
return []
elif isinstance(x, list):
return x
else:
return [x]
mydict = xmltodict.parse(xmlstringResults)
for carsInGarage in guaranteed_list(mydict['garage']['car']):
# do something...
but i thing i will write my code again and "use XML directly" as one of the comments said.
I am using the combination of
1)
json_dict = xmltodict.parse(s, force_list={'item'})
And
2)
# Removes a level in python dict if it has only one specific key
#
# Examples:
# recursive_skip_dict_key_level({"c": {"a": "b"}}, "c") # -> {"a", "b"}
# recursive_skip_dict_key_level({"c": ["a", "b"]}, "c") # -> ["a", "b"]
#
def recursive_skip_dict_key_level(d, skipped_key):
if issubclass(type(d), dict):
if list(d.keys()) == [skipped_key]:
return recursive_skip_dict_key_level(d[skipped_key], skipped_key)
else:
for key in d.keys():
d[key] = recursive_skip_dict_key_level(d[key], skipped_key)
return d
elif issubclass(type(d), list):
new_list = []
for e in d:
new_list.append(recursive_skip_dict_key_level(e, skipped_key))
return new_list
else:
return d
# Removes None values from a dict
#
# Examples:
# recursive_remove_none({"a": None}) # -> {}
# recursive_remove_none([None]) # -> []
#
def recursive_remove_none(d):
if issubclass(type(d), dict):
new_dict = {}
for key in d.keys():
if not (d[key] is None):
new_dict[key] = recursive_remove_none(d[key])
return new_dict
elif issubclass(type(d), list):
new_list = []
for e in d:
if not (e is None):
new_list.append(recursive_remove_none(e))
return new_list
else:
return d
json_dict = recursive_skip_dict_key_level(json_dict, "item")
json_dict = recursive_remove_none(json_dict)
to interpret any "item" XML-elements as lists.
In addition to the existing answers, xmltodict now also supports the following to force everything to be a list:
xml = xmltodict.parse(s, force_list=True)

A list of path to a dictionnary

I am trying to populate a python dict from a list of paths (the purpose is to create a ttk.treview) :
paths = ["\\hihi", "\\hihi\\hoho\\huhu", "\\hihi\\hoho\\haha", "\\haha", "\\huhu"]
and I want to create this dictionnary (json serialized here) :
{
"haha": {},
"hihi": {
"hoho": {
"huhu": 0
},
"huhu": {
"haha": 0
}
},
"huhu": {}
}
What is the best way to do this? I tried with a for loop (recursive loop?), with a dict comprehension and with dpath but I have no valid result.
The biginning of my code :
split = paths.split("\\")
del split[0]
dict = {}
?
Thank you very much in advance
You could use defaultdict for this:
def make_empty_default_dict():
return defaultdict(make_empty_default_dict)
Define how you add a path:
def add_path(pth, dct):
if pth:
subdict = dct[pth[0]]
return add_path(pth[1:], subdict)
else:
return dct
Then populate your default dict with keys:
d = make_empty_default_dict()
for path in paths:
d = add_path(path.split("\\"), d)
I have an alternative to the recursive solution. For each path:
put a cursor at the root of the target dict
search sequence: move the cursor forth until you find a 0 or a missing part of the path
build sequence: add an empty dict and move the cursor on that dict until you hit the last part.
the last part needs a special handling for the 0.
Here's the code:
def build_paths(paths, d={}):
for path in paths:
parts = path.split("\\")[1:] # remove the part before \
cursor = d
search = True
for part in parts[:-1]:
if search:
if part not in cursor or not cursor[part]: # not found or 0
cursor[part] = {} # set a new dict
search = False
else:
cursor[part] = {}
cursor = cursor[part] # advance one level deeper in the dict
cursor[parts[-1]] = 0 # close with a 0
return d
It's faster than the recursive vesion of #xtofl, but not that fast. With timeit:
iterative: 6.169872568580601
recursive: 17.209112331781498
You can use recursion with itertools.groupby:
import itertools
paths = ["\\hihi", "\\hihi\\hoho\\huhu", "\\hihi\\hoho\\haha", "\\haha", "\\huhu"]
new_paths = [list(filter(None, i.split('\\'))) for i in paths]
def update_output(f):
def wrapper(_d):
result = f(_d)
final = lambda x, level = 0:{a:{} if not level and not b else b if not b else final(b, level+1) for a, b in x.items()}
return final(result)
return wrapper
#update_output
def full_directory(data):
def files(d):
return {a:(lambda x:0 if len(x) == 1 else files([i[1:] for i in filter(lambda y:len(y) != 1 or y[0] != a, x)]))(list(b)) for a, b in itertools.groupby(sorted(d, key=lambda x:x[0]), key=lambda x:x[0])}
return files(data)
print(full_directory(new_paths))
Output:
{'haha': {}, 'hihi': {'hoho': {'haha': 0, 'huhu': 0}}, 'huhu': {}}
I found this : http://connor-johnson.com/2015/02/28/generate-a-tree-structure-in-python/
It works very well! So the code :
def add(t, path):
for node in path:
t = t[node]
Tree = lambda: defaultdict(Tree)
t = Tree()
paths = ["\\hihi", "\\hihi\\hoho\\huhu", "\\hihi\\hoho\\haha", "\\haha", "\\huhu"]
for path in paths:
split = path.split("\\")
del split[0]
for elt in split:
add(t, split)
dicts = lambda t: { k:dicts(t[k]) for k in t }
print(json.dumps(dicts(t), indent=4))

Nested insertion/creation of dictionary

Given that you have an empty dictionary
data = {}
I have a path and a value
path = "root.sub.item"
value = 12
How could I recursively add objects that do not exist?
def add_value(path, value):
for part in path.split('.'):
if not part in data:
data[part] = {}
The expected output for this would be:
data = {
'root':{
'sub':{
'item': 12
}
}
}
Could somebody help out with this or point me in the right direction?
I'm using Python 3.6.
You can use some another kind of solution like recursive defaultdict, as in this answer.
A quick and stupid example about how it can used:
from collections import defaultdict
def func(rdict, path, value):
items = path.split('.')
d = rdict[items[0]]
for item in items[1:-1]:
d = d[item]
d[items[-1]] = value
nested_dict = lambda: defaultdict(nested_dict)
result = nested_dict()
func(result, 'root.sub.item', 12)
func(result, 'root.moon.value', 1)
assert result['root']['sub']['item'] == 12
assert result['root']['moon']['value'] == 1
assert result['root']['moon']['noop'] != 0
You're almost there, you just need to keep track of how far you are into the tree structure, and a way to know when you're on the last element of the path:
def add_value(path, value):
tmp = data
parts = list(path.split('.'))
for i in range(len(parts) - 1):
part = parts[i]
if not part in tmp:
tmp[part] = {}
tmp = tmp[part]
tmp[parts[-1]] = value
you can try Raymond Hettinger recipe :
source: https://twitter.com/raymondh/status/343823801278140417
from collections import defaultdict
infinity_dict=lambda:defaultdict(infinity_dict)
d=infinity_dict()
d['root']['sub']['item'] = 12

Dynamic dictionary recursion in Python3

I've been working on this for too long and need some help.
I'm trying to create a dictionary using faker. If it were only that simple.
Initially the dictionary is flat. A key and item. If the first letter of the key is 'B' or 'M' it will then turn that string, into a dictionary with 5 keys and keep doing that until it finds none starting with either of those two letters. I know, there's no recursion happening now. That's why I need help. I'm trying to figure out how to properly recurse rather than hard code the depth.
Starting Dictionary:
{
"Marcia": "https://www.skinner.biz/categories/tags/terms.htm",
"Nicholas": "https://scott-tran.com/",
"Christopher": "https://www.ellis.com/",
"Paul": "https://lopez.com/index/",
"Jennifer": "https://www.sosa.com/wp-content/main/login.php"
}
Marcia should expand to this...
Example:
"Marcia": {
"Alexander": "http://hicks.net/home.html",
"Barry": {
"Jared": "https://www.parker-robinson.com/faq.html",
"Eddie": "https://www.smith-thomas.com/",
"Ryan": "https://www.phillips.org/homepage/",
"Mary": {
"Alex": "http://www.perry.com/tags/explore/post.htm",
"Joseph": "https://www.hansen.com/main/list/list/index/",
"Alicia": "https://www.tran.biz/wp-content/explore/posts/",
"Anna": "http://lee-mclaughlin.biz/search/login/",
"Kevin": "https://blake.net/main/index/"
}
"Evan": "http://carroll.com/homepage.html"
}
"Sharon": "https://www.watson.org/categories/app/login/",
"Hayley": "https://www.parks.com/",
"William": "https://www.wyatt-ware.com/"
}
My code is more manual than dynamic in that I must explicitly know now many levels deep the dictionary goes rather than dynamically figuring it out.
Here's what I have that works to the depth of 2 levels but I want to to find any key starting with 'B' or 'M' and acting on it.
import json
from build_a_dictionary import add_dic
from faker import Faker
dic = add_dic(10)
dic1 = {}
dic2 = {}
def build_dic(dic_len):
dic1 = {}
fake = Faker()
if len(dic1) == 0:
dic1 = add_dic(dic_len)
print(json.dumps(dic1, indent=4))
for k, v in dic1.items():
dic2[k] = add_dic(dic_len)
for key in dic2[k].keys():
for f in key:
if f == 'B' or f == 'M':
dic2[k][key] = add_dic(dic_len)
return dic2
Here is the code from add_dic() I wrote:
import string, time
from faker import Faker #had to install with pip
fake = Faker()
dic = {}
dics = {}
key = ""
def add_dic(x):
dic={}
start = time.time()
if x > 690:
print("Please select a value under 690")
sys.exit()
for n in range(x):
while len(dic) < x:
key = fake.first_name()
if key in dic.keys():
break
val = fake.uri()
dic[key] = val
end = time.time()
runtime = end - start
return dic
You're just doing it wrong, if you want it to be recursive, write the function as a recursive function. It's essentially a custom (recursive) map function for a dictionary. As for your expected dictionary, I'm not sure how you'd ever get Faker to deterministically give you that same output every time. It's random...
Note: There is nothing "dynamic" about this, it's just a recursive map function.
from faker import Faker
import pprint
pp = pprint.PrettyPrinter(indent=4)
fake = Faker()
def map_val(key, val):
if key[0] == 'M' or key[0] == 'B':
names = [(fake.first_name(), fake.uri()) for i in range(5)]
return {k : map_val(k, v) for k,v in names}
else:
return val
#uncomment below to generate 5 initial names
#names = [(fake.first_name(), fake.uri()) for i in range(5)]
#initial_dict = {k : v for k,v in names}
initial_dict = {
"Marcia": "https://www.skinner.biz/categories/tags/terms.htm",
"Nicholas": "https://scott-tran.com/",
"Christopher": "https://www.ellis.com/",
"Paul": "https://lopez.com/index/",
"Jennifer": "https://www.sosa.com/wp-content/main/login.php"
}
dict_2 = {k : map_val(k,v) for k,v in initial_dict.items()}
pp.pprint(dict_2)
Output:
rpg711$ python nested_dicts.py
{ 'Christopher': 'https://www.ellis.com/',
'Jennifer': 'https://www.sosa.com/wp-content/main/login.php',
'Marcia': { 'Chelsea': 'http://francis.org/category.jsp',
'Heather': 'http://www.rodgers.com/privacy.jsp',
'Jaime': 'https://bates-molina.com/register/',
'John': 'http://www.doyle.com/author.htm',
'Kimberly': 'https://www.harris.org/homepage/'},
'Nicholas': 'https://scott-tran.com/',
'Paul': 'https://lopez.com/index/'
}
Thank you all for your help. I've managed to figure it out.
It now builds a dynamic dictionary or dynamic json for whatever need.
import sys, json
from faker import Faker
fake = Faker()
def build_dic(dic_len, dic):
if isinstance(dic, (list, tuple)):
dic = dict(dic)
if isinstance(dic, dict):
for counter in range(len(dic)):
for k,v in dic.items():
if k[0] == 'B' or k[0] == "M":
update = [(fake.first_name(), fake.uri()) for i in range(5)]
update = dict(update)
dic.update({k: update})
return dic
def walk(dic):
for key, item in dic.items():
#print(type(item))
if isinstance(item, dict):
build_dic(5, item)
walk(item)
return dic
a = build_dic(10, ([(fake.first_name(), fake.uri()) for i in range(10)]))
walk(a)
print(json.dumps(a, indent=4))
Recursion is when a function calls itself; when designing a recursive function, it's important to have an exit condition in mind (i.e. when will the recursion stop).
Let's consider a contrived example to increment a number until it reaches a certain value:
def increment_until_equal_to_or_greater_than_value(item, target):
print 'item is', item,
if item < target:
print 'incrementing'
item += 1
increment_until_equal_to_or_greater_than_value(item, target)
else:
print 'returning'
return item
increment_until_equal_to_or_greater_than_value(1, 10)
And the output
item is 1 incrementing
item is 2 incrementing
item is 3 incrementing
item is 4 incrementing
item is 5 incrementing
item is 6 incrementing
item is 7 incrementing
item is 8 incrementing
item is 9 incrementing
item is 10 returning
You can see we've defined our recursive part in the if statement and the exit condition in the else.
I've put together a snippet that shows a recursive function on a nested data structure.
It doesn't solve exactly your issue, this way you can learn by dissecting it and making it fit for your use case.
# our recursive method
def deep_do_something_if_string(source, something):
# if source is a dict, iterate through it's values
if isinstance(source, dict):
for v in source.itervalues():
# call this method on the value
deep_do_something_if_string(v, something)
# if source is a list, tuple or set, iterate through it's items
elif isinstance(source, (list, tuple, set)):
for v in source:
deep_do_something_if_string(v, something)
# otherwise do something with the value
else:
return something(source)
# a test something to do with the value
def print_it_out(value):
print value
# an example data structure
some_dict = {
'a': 'value a',
'b': [
{
'c': 'value c',
'd': 'value d',
},
],
'e': {
'f': 'value f',
'g': {
'h': {
'i': {
'j': 'value j'
}
}
}
}
}
deep_do_something_if_string(some_dict, print_it_out)
And the output
value a
value c
value d
value j
value f

Categories