Simplify, simplify, simplify:
Copydef p1(args):
whatever
def p2(more args):
whatever
myDict = {
"P1": p1,
"P2": p2,
...
"Pn": pn
}
def myMain(name):
myDict[name]()
That's all you need.
You might consider the use of dict.get with a callable default if name refers to an invalid function—
Copydef myMain(name):
myDict.get(name, lambda: 'Invalid')()
(Picked this neat trick up from Martijn Pieters)
Answer from S.Lott on Stack OverflowDict methods
python - Using a dictionary to select function to execute - Stack Overflow
dictionary - Not understanding a trick on .get() method in python - Stack Overflow
Dictionary functions
Videos
Kind of ranting about dictionary methods:
Why does keys() not return a simple list. I nearly broke my head today, because I could not figure out how to iterate through my dict without using "each in dict".
Maybe I'm dumb, but "for each in dict" seems harder to me than simply ranging through my dict. And yeah, like 10h in I find list(dict.keys()) is a viable solution...
Second one: why does nobody mention "dict[x] =+ value" does exist? I had a very hard time finding this. Maybe too basic? I don't know.
Not even mentioning "del", which works totally different than other methods, imho.
Could somebody make me feel less dumb? Are dictionarys the lower lifeform of data in python?
Simplify, simplify, simplify:
Copydef p1(args):
whatever
def p2(more args):
whatever
myDict = {
"P1": p1,
"P2": p2,
...
"Pn": pn
}
def myMain(name):
myDict[name]()
That's all you need.
You might consider the use of dict.get with a callable default if name refers to an invalid function—
Copydef myMain(name):
myDict.get(name, lambda: 'Invalid')()
(Picked this neat trick up from Martijn Pieters)
Simplify, simplify, simplify + DRY:
Copytasks = {}
task = lambda f: tasks.setdefault(f.__name__, f)
@task
def p1():
whatever
@task
def p2():
whatever
def my_main(key):
tasks[key]()
The get method on a dictionary is documented here: https://docs.python.org/3/library/stdtypes.html#dict.get
get(key[, default])
Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.
So this explains the 0 - it's a default value to use when letternum doesn't contain the given letter.
So we have letternum.get(each_letter, 0) - this expression finds the value stored in the letternum dictionary for the currently considered letter. If there is no value stored, it evaluates to 0 instead.
Then we add one to this number: letternum.get(each_letter, 0) + 1
Finally we stored it back into the letternum dictionary, although this time converting the letter to lowercase: letternum[each_letter.lower()] = letternum.get(each_letter, 0) + 1 It seems this might be a mistake. We probably want to update the same item we just looked up, but if each_letter is upper-case that's not true.
letternum is a dict (a dictionary). It has a method called get which returns the value associated with a given key. If the key is absent from the dictionary, it returns a default value, which is None unless an optional second argument is present, in which case that argument value is returned for missing elements.
In this case, letternum.get(each_letter,0) returns letternum[each_letter] if each_letter is in the dictionary. Otherwise it returns 0. Then the code adds 1 to this value and stores the result in letternum[each_letter.lower()].
This creates a count of the number of occurrences of each letter, except that it inconsistently converts the letter to lowercase when updating, but not when retrieving existing values, so it won't work properly for uppercase letters.