GithubHelp home page GithubHelp logo

Map keys is broken about pydash HOT 3 CLOSED

Drvanon avatar Drvanon commented on June 5, 2024
Map keys is broken

from pydash.

Comments (3)

zbynekcz12 avatar zbynekcz12 commented on June 5, 2024

Yes, your analysis is correct. The map_keys() function in Pydash iterates over the object's keys and values in the order (value, key), which is the opposite of the order that most iterators use. This is because the map_keys() function needs to know the key in order to generate the new key.

However, this order can cause problems if the iterator function expects the values to be strings. For example, the str.upper() function expects a string argument, but it will raise a TypeError if it is passed an integer.

To fix this, Pydash could change the order of the arguments in the iteriteratee() function to (key, value). This would make the map_keys() function more consistent with other iterators and would also prevent the error that you are seeing.

In the meantime, there are a few workarounds that you can use. One option is to explicitly pass the key argument to the iterator function. For example:

from pydash import py_

def upper_key(key):
    return key.upper()

assert py_(dict(a=1, b=2, c=3)).map_keys(upper_key).value() == dict(A=1, B=2, C=3)

Another option is to use a lambda function that takes the key and value arguments in the desired order. For example:

from pydash import py_

assert py_(dict(a=1, b=2, c=3)).map_keys(lambda key, value: key.upper()).value() == dict(A=1, B=2, C=3)

Finally, you could also use a list comprehension to iterate over the object's keys and values and generate the new keys. For example:

from pydash import py_

assert py_({key.upper(): value for key, value in dict(a=1, b=2, c=3).items()}).value() == dict(A=1, B=2, C=3)

I hope this helps!

from pydash.

Drvanon avatar Drvanon commented on June 5, 2024

Thank you for your quick response! I would like to point out that both your snippets fail:

assert py_(dict(a=1, b=2, c=3)).map_keys(lambda key: key.upper()).value() == dict(A=1, B=2, C=3)

fails because the key argument is an integer. Your second snippet also does not work, since key and value need to be swapped:

from pydash import py_

assert py_(dict(a=1, b=2, c=3)).map_keys(lambda _value, key: key.upper()).value() == dict(A=1, B=2, C=3)

I agree that your third option does indeed work, however I really enjoy the way that I can use postfix notation on chains. It, so to say, breaks the chain.

from pydash.

dgilland avatar dgilland commented on June 5, 2024

iteriteratee calls like most iteratees with the values first and the keys second, but that is undesirable behavior for this function.

The behavior of map_keys has always been to pass (value, key, object) to the callback or the first N arguments if the callback accepts fewer than 3 arguments:

pydash/src/pydash/objects.py

Lines 1183 to 1187 in 64d4614

def map_keys(obj, iteratee=None):
"""
The opposite of :func:`map_values`, this method creates an object with the same values as object
and keys generated by running each own enumerable string keyed property of object through
iteratee. The iteratee is invoked with three arguments: ``(value, key, object)``.

This mirrors the behavior of Lodash's mapKeys: https://lodash.com/docs/4.17.15#mapKeys

from pydash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.