Had a use for reduce recently at work. My tester wanted to combine some nested dictionaries, so I wrote a function for him to do it, which took two dictionaries and returned a new dictionary that was the combination of the two. Two days later he said "can we do this with a list of dictionaries" and reduce was the perfect way to do that. I probably wouldn't use this for production code, but for test suite code it ended up being a great solution
@JakeCallahan Apologies I've only just seen this. I did another one recently # iterate over a list of directories and read in a parquet file to a spark dataframe # add a filename column to each dataframe duplicates = ( reduce(Dataframe.union, list_of_dataframes).groupby("uniqueid").agg(sf.collect_list("filename").alias("filenames").filter(sf.size(sf.col("filenames")) >1).collect() ) Might need some formatting on that one. Typing on a phone. I'll find the recursive dictionary merge later and post that too
The thing to remember, is that when you call a method from an instance, self is the instance. When you call the same method from a class, you can pass an instance of the class in as self. For example ", ".join(list_of_strings) Or str.join(", ", list_of_strings)
I have very little exposure to Django, so would not be a good teacher for a course on that. In the past I've tended to favor Flask, but today would also be inclined to try out FastAPI. Django might be something I revisit in the future, but that won't be anytime soon unfortunately.
Since I haven't personally gone through any available Django courses, I wouldn't want to mislead you with a poor recommendation. I'd hate to waste your time. However, if you do find a Django course you like, please link it here so others can follow your lead!
@@JakeCallahan I think this channel is good if you want to learn about the "theory" of Django, but i really wanted another resource to learn from. Very Academy:youtube.com/@veryacademy?si=7mF2rSqa3QVkivGK
What do you use most from functools? For me, it has to be caching, followed by partials.
This channel is an absolute gem
Professional quality tuition and production - as ever!
Had a use for reduce recently at work. My tester wanted to combine some nested dictionaries, so I wrote a function for him to do it, which took two dictionaries and returned a new dictionary that was the combination of the two.
Two days later he said "can we do this with a list of dictionaries" and reduce was the perfect way to do that.
I probably wouldn't use this for production code, but for test suite code it ended up being a great solution
Oh nice! If you don't mind sharing, will you put an example in a GitHub gist and link it here?
@JakeCallahan
Apologies I've only just seen this. I did another one recently
# iterate over a list of directories and read in a parquet file to a spark dataframe
# add a filename column to each dataframe
duplicates = (
reduce(Dataframe.union, list_of_dataframes).groupby("uniqueid").agg(sf.collect_list("filename").alias("filenames").filter(sf.size(sf.col("filenames")) >1).collect()
)
Might need some formatting on that one. Typing on a phone.
I'll find the recursive dictionary merge later and post that too
The thing to remember, is that when you call a method from an instance, self is the instance. When you call the same method from a class, you can pass an instance of the class in as self.
For example
", ".join(list_of_strings)
Or
str.join(", ", list_of_strings)
@JakeCallahan
def merge_dictionaries(dict1, dict2):
all_keys = set(dict1.keys()).union(dict2.keys())
new_dict ={}
for key in all_keys():
value = dict1.get(key, dict2.get(key))
If isinstance(value, dict):
new_dict[key] = merge_dictionaries(dict1.get(key, {}), dict2.get(key, {}))
elif isinstance(value, list):
new_dict[key] = [*dict1.get(key, []), *dict2.get(key, [])]
return new_dict
reduce(merge_dictionaries, list_of_dicts)
It relies on the dictionaries having the same datatype for the same key, and isn't exhaustive. But works for my testers usecase
20:00 💡 moment you have created factorial using partial reduce ..that's interesting .
Nice, thanks!
woooooooooooooo
can you do django plz
I have very little exposure to Django, so would not be a good teacher for a course on that. In the past I've tended to favor Flask, but today would also be inclined to try out FastAPI.
Django might be something I revisit in the future, but that won't be anytime soon unfortunately.
@@JakeCallahan ah🥲, it's ok.
can you give me a good resource to learn django , like the theory of it plz
Since I haven't personally gone through any available Django courses, I wouldn't want to mislead you with a poor recommendation. I'd hate to waste your time.
However, if you do find a Django course you like, please link it here so others can follow your lead!
@@JakeCallahan I think this channel is good if you want to learn about the "theory" of Django, but i really wanted another resource to learn from.
Very Academy:youtube.com/@veryacademy?si=7mF2rSqa3QVkivGK
Man i discovered your channel a week back and i am so glad... Great job with these videos🫡