- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Frist
Admin
I have seen this in Magento (php webshop) as well. Tough i think that was bigger. It used the amount of function parameters.
Admin
Nice! Will make a good IOPyCC entry, that's for sure.
Admin
Given the example structure,
should return null.
Admin
Oh sweet, 8 == "any arbitrary depth." That will make my next project a lot easier to handle.
Admin
I had to re-read the requirements five-ish times to figure out how the code was at all related.
Python is not for writing code that looks like a snake.
Admin
Also note the horrible misnaming:
config_data_list
is a dictionary, not a list, andgetObjectValue
works on a dictionary, not an object. For reference, here's how the function should have looked:Admin
I kind of feel that if your Python solution involves both reduce and lambda, there's probably a better solution.
Admin
Like what? using haskell directly?
Admin
How about: return reduce(dict.get, keys, config)
Admin
I once had to review Python code from a third-party developer who would use nested Lists/Tuples to store key-value pairs, eg. [('key1', value1), ('key2', value2), ...]
So I taught them to use Dictionaries for key-value pairs. What I didn't expect was that they also replaced all instances where Lists/Tuples should be used with Dictionaries instead, eg. {'1': value1, '2': value2, ...}
Admin
reduce(dict.get, keys, config) works, as long as all config keys are dicts. It fails if any are lists, tuples, or dict-like.
Admin
I am a bit surprised python doesn't support multiple level array access syntactically. I would be even further surprised if it doesn't provide functions to do this.
Admin
The real wtf is the keys should have been key = "api.defaultHeaders.X-Api-Key" (and split on "." in resolve_config) so the callers don't have to mess around with passing a list of strings. (and no - "." is not valid in a key, deal with it)
Admin
It's a list. You access the values not via numbers but via keys. Still a list. Not an array, but a "dictionary", also known as a "hash" or an "associative array".
Admin
This reminds me of Pokemon Crystal, where the function for getting what frame of animation a tree in the forest should use uses compare instructions with jumps to labels for either returning 0 (even) or returning 2 and setting the carry flag. (odd) ("CP $01; jr z,.odd;CP $02;jr z,.even" etc.) See the code at https://github.com/pret/pokecrystal/blob/master/tilesets/animations.asm#L644.