• Frits (unregistered)

    Frist

  • M (unregistered)

    I have seen this in Magento (php webshop) as well. Tough i think that was bigger. It used the amount of function parameters.

  • Krzysztof Słychań (google)

    Nice! Will make a good IOPyCC entry, that's for sure.

  • Brian Boorman (google)

    Given the example structure,

    "api", "defaultHeaaders", "X-Api-Key"

    should return null.

  • Adam (unregistered)

    Oh sweet, 8 == "any arbitrary depth." That will make my next project a lot easier to handle.

  • Chronomium (unregistered)

    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.

  • (nodebb)

    Also note the horrible misnaming: config_data_list is a dictionary, not a list, and getObjectValue works on a dictionary, not an object. For reference, here's how the function should have looked:

    def resolve_config(config, keys):
        return reduce(lambda cur, k: cur[k], keys, config)
    
  • Colin (unregistered) in reply to phihag

    I kind of feel that if your Python solution involves both reduce and lambda, there's probably a better solution.

  • Uhm (unregistered)

    Like what? using haskell directly?

  • Karl Bielefeldt (github) in reply to Colin

    How about: return reduce(dict.get, keys, config)

  • Daniel (unregistered)

    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, ...}

  • Philipp Hagemeister (google) in reply to Karl Bielefeldt

    reduce(dict.get, keys, config) works, as long as all config keys are dicts. It fails if any are lists, tuples, or dict-like.

  • isthisunique (unregistered)

    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.

  • Norman Rasmussen (google)

    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)

  • bytepusher (unregistered) in reply to phihag

    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".

  • MineRobber9000 (unregistered)

    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.

Leave a comment on “The Key to Lookups”

Log In or post as a guest

Replying to comment #:

« Return to Article