Re:Shell Script Best Practices, from a decade of scripting t...
by kotlin2 on Thursday October 27, 2022 @10:13AM
(#33357710)
c = defaultdict(lambda: 42)
b = defaultdict(lambda: c)
a = defaultdict(lambda: b)
a["a"]["b"]["c"] # --> 42
Re:Shell Script Best Practices, from a decade of scripting t...
by Spivak on Thursday October 27, 2022 @10:14AM
(#33357731)
Okay fair, I deserve that. I assumed it was obvious I meant arbitrary depth.
Also d["a"] and d["a"]["b"] aren't 42.
Re:Shell Script Best Practices, from a decade of scripting t...
by kotlin2 on Thursday October 27, 2022 @10:19AM
(#33357795)
If d["a"]["b"] is 42, then how could d["a"]["b"]["c"] also be 42? What you want doesn't make sense semantically. Normally, we'd expect these two statements to be equivalent
d["a"]["b"]["c"] == (d["a"]["b"])["c"]
Re:Shell Script Best Practices, from a decade of scripting t...
by Spivak on Thursday October 27, 2022 @11:00AM
(#33358556)
I mean you got it but it's something a lot of people want. The semantic reason for it is so you can look up an arbitrary path on a dict and if it's not present get a default, usually None. It can be done by catching KeyError but it has to happen on the caller side which is annoying. I can't make a real nested mapping that returns none if the keys aren't there.
d = magicdict()
is42 = d["foo"]["bar"]["baz"]
# -> You can read any path and get a default if it doesn't exist.
d["hello"]["world"] = 420
# -> You can set any path and d will then contain { "hello": { "world": 420 }
People use things like jmespath to do this but the fundamental issue is that __getitem__ isn't None safe when you want nested dicts. It's a godsend when dealing with JSON.I feel like we're maybe too in the weeds, I should have just said "now have two expressions in your lambda."
Re:Shell Script Best Practices, from a decade of scripting t...
by kotlin2 on Thursday October 27, 2022 @11:24AM
(#33358974)
What languages allow such a construct? It seems like it would be super confusing if these two code samples produced different values:
# One
a = d["a"]["b"]["c"]
# Two
a = d["a"]["b"]
b = a["c"]
Re:Shell Script Best Practices, from a decade of scripting t...
by Too on Thursday October 27, 2022 @11:58AM
(#33359538)
The MagicMock class from unittest package does what you want.
I have a hard time understanding any use case outside of such mocking.
Re:Shell Script Best Practices, from a decade of scripting t...
by Macha on Thursday October 27, 2022 @12:45PM
(#33360303)
In this case you're chaining discreet lookup operations where it sounds like you really want a composite key. You could easily implement this if you accepted the syntax of it as d["a.b.c"] or d["a", "b", "c"] or d.query("a", "b", "c")
Otherwise I'm not sure of a mainstream language that would let you do a.get(x).get(y) == 42 but a.get(x).get(y).get(z) == 42, unless you resorted to monkey patching the number type, as it implies 42.get(z) == 42, which seems.. silly
Try in Python to make a nested defaultdict you can access like the following.
Can't be done because it's impossible for user code to detect what the last __getitem__ call is and return the default.Edit: Dang it, I mean arbitrary depth.