Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pairs_to_dict forces float cast for values #18

Closed
SJD opened this issue Mar 16, 2010 · 2 comments
Closed

pairs_to_dict forces float cast for values #18

SJD opened this issue Mar 16, 2010 · 2 comments

Comments

@SJD
Copy link

SJD commented Mar 16, 2010

Heya Andy, thanks for the speedy turnaround on the new hash functions. I notice however that the pairs_to_dict function is forcing values to be cast as float via a map call. Is this intended? It means I can't put string values into a hash.

r.hset('foo', 'bar', 'zoo')
1
r.hgetall('foo')
Traceback (most recent call last):
File "", line 1, in
File "redis/client.py", line 902, in hgetall
File "redis/client.py", line 325, in format_inline
File "redis/client.py", line 247, in execute_command
File "redis/client.py", line 242, in _execute_command
File "redis/client.py", line 310, in parse_response
File "redis/client.py", line 207, in
File "redis/client.py", line 158, in pairs_to_dict
ValueError: invalid literal for float(): zoo

Not sure what side effect there would be from removing the float call - other than everything coming back as a string. Maybe instead of mapping to float, it could use a function like this?

def map_type(value):
... try:
... return '.' in value and float(value) or int(value)
... except ValueError:
... return value

Anyway, thought I would let you know =]

Cheers
Sam

@SJD
Copy link
Author

SJD commented Mar 16, 2010

Hrm, I've monkey patched my local version and it seems to work ok so far ...

def map_value_type(value):
try:
return '.' in value and float(value) or int(value)
except ValueError:
return value

def pairs_to_dict(response):
"Create a dict given a list of key/value pairs"

return dict(zip(response[::2], map(map_value_type, response[1::2])))

r.hgetall('foo')
{'bar': 'zoo'}
r.hset('foo', 'heartbeat', 30)
1
r.hset('foo', 'pi', 3.1415927)
1
r.hgetall('foo')
{'heartbeat': 30, 'pi': 3.1415926999999999, 'bar': 'zoo'}

@andymccurdy
Copy link
Contributor

Gah, I copy/pasted and forgot to actually remove the map() call. The values should come back as strings, just how redis.get() would return. Fix pushed in 5723a54.

dbravender pushed a commit to dbravender/redis-py that referenced this issue May 6, 2014
… and forgot to actually change it! thanks to Sam (SJD) for the bug report
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants