Comments (3)
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.
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.
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:
Lines 1183 to 1187 in 64d4614
This mirrors the behavior of Lodash's mapKeys
: https://lodash.com/docs/4.17.15#mapKeys
from pydash.
Related Issues (20)
- pytest_mypy_testing tests are very slow HOT 2
- slugify behavior with single quotes HOT 1
- No module named 'typing_extensions' in pydash 7.0.0 HOT 1
- Missing dependency "typing_extensions" HOT 1
- Incompatible with typing-extensions 4.6 HOT 3
- how to use capture group in reg_exp_js_replace? HOT 1
- Typing: find_index only accepts Callable or None HOT 3
- Zip type hinting and starmap HOT 3
- Take while does not support generators
- Support for multithreading. HOT 1
- Typing call-overload error HOT 5
- max_by should return max value, not object HOT 3
- Wrong assumption of function argcount when using a negated lambda assigned as a class attribute
- Documentation: pydash-cheatsheet
- Type overloading for get
- Unexpected difference in path behavior for set_() when accessing attribute of object
- multiply does not math
- Use ruff HOT 1
- Embrace the tuple HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pydash.