Yes, the parent is the value. But with the code if not any(heir1 == parent for heir1 in heir_to_parent) you are cycling through all keys of heir_to_parent and checking whether one of them is equal to the variable parent. So basically you are just checking whether parent is a key of heir_to_parent, which can also be done by if parent in heir_to_parent.
def fill_inherited_features(features_by_function):
# e.g. hort gets its InThePast value from imp,
# and imp gets its InThePast value from fut,
# so they should both ultimately be filled in based on fut
heir_to_parent = {}
for function, features_this_function in features_by_function.items():
for feature, val in features_this_function.items():
is_inherited = val.startswith("{") and val.endswith("}")
if is_inherited:
parent_function = val[1:-1]
parent_tup = (parent_function, feature)
heir_tup = (function, feature)
assert heir_tup not in heir_to_parent
heir_to_parent[heir_tup] = parent_tup
[Highlighted line:]
# "don't optimize prematurely" - Donald Knuth, probably
[End highlighted line.]
while True:
# get the parents that are not also heirs
ancestors = [parent for heir, parent in heir_to_parent.items()
if not any(heir1 == parent for heir1 in heir_to_parent)]
if len(ancestors) == 0:
break
for func, feat in ancestors:
val = features_by_function[func][feat]
heirs = [heir for heir, parent in heir_to_parent.items() if parent == (func, feat)]
for heir_func, heir_feat in heirs:
assert heir_feat == feat, "can't inherit value from the wrong feature"
features_by_function[heir_func][heir_feat] = val
del heir_to_parent[(heir_func, heir_feat)]
return features_by_function
Ultra late to the party, but now I'm seriously considering leaving out of context quotes in the comments of anything I code. Just in case I decide to publish it one day.
"I also like to ride Segways" - Steve Wozniak
"I am not now, nor have I ever been, a member of the demigodic party." - Dennis Ritchie
"When a poor man eats a chicken, one of them is sick." - Yiddish Proverb
8
u/Mrfazzles Aug 11 '22
This is beautifully bad. I also like how the style of variable names changes in the 2nd half.
function is now func, feature is now feat, instead of something like 'ancestor_tup' we have val.
OP, do you have some example input data? I'd love to play around with it.
6
u/Tc14Hd Aug 11 '22
Couldn't you just write
if parent in heir_to_parentinstead ofif not any(heir1 == parent for heir1 in heir_to_parent)? That's way easier.92
u/Spootba Aug 11 '22
This is fucking unintelligible
40
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 11 '22
Image Transcription: Code
[Highlighted line:]
[End highlighted line.]
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
29
u/PaulieGlot Aug 11 '22
"Who are you? How did you get in my house?"
21
u/Knuffya Aug 11 '22
I managed to pick up a piece of food using three sticks converging on it from three different directions, and put it in my mouth. // Richard Stallman16
u/cheezballs Aug 11 '22
Jesus fuck is that python?? Good God I can't tell what the fuck is going on.
11
u/smol_pp8269 Aug 11 '22
well lets just say its a "little" unclean
9
u/sorte_kjele Aug 11 '22
I see the CrusaderKings codebase has leaked.
29
u/shizzy0 Aug 11 '22
Let’s be real. He chose Python; optimization was never an option.
1
u/Icy_Mastodon_4911 Nov 20 '22
speaking the language of gods