Conversation
Hi, this is good feature but I dont like your implementation -- it is too complex and has bugs. yield from redis.hmset('key', {'a': 1}, {'b': 2}, 'c', 3, d=4, a=5)
res = yield from redis.hgetall('key')
assert res == {b'a': b'5',
b'b': b'2',
b'c': b'3',
b'd': b'4'} Second, implementation, the public interface must be as follows:
|
Hi @popravich. My implementation dicts_or_pairs not supported mixed types. This test will fail in both cases in one and the same place.
A similar bug in hset: |
This is intended behaviour
If so it should raise exception when mixing arg types |
More successful implementation |
if not args: | ||
pairs = functools.reduce(operator.add, kwargs.items()) | ||
|
||
elif all(is_dict): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you need all args
to be dicts?
what is the reason for calling hmset like this:
redis.hmset(key, {'a': 1}, {'b': 2})
and not simply with one dict?
An example of the need OrderedDict
As the regular dict:
Another crazy example:
|
OMG this is totaly crazy) redis.hmset(key, {b'a': 1, 'a': 2}, {'a': 3, b'a': 4}) |
I agree with @popravich: no need to be too smart. If conflicts happens, better to cry loudly instead of picking decision that user may not be aware/expected about. Only user may solve them right. |
I like it as simple as possible -- so it 100% clear how it works and what to expect. |
Convinced. Therefore, I propose a simple implementation. |
pairs = args | ||
|
||
if kwargs: | ||
pairs = make_flat(kwargs.items(), pairs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's a point of generating tuples for every iteration? Sounds like you need in:
pairs = list(pairs)
pairs.extend(chain.from_iterable(kwargs.items())
It's all said:
In [6]: %timeit make_flat(kwargs.items(), tuple())
100 loops, best of 3: 2.45 ms per loop
In [8]: %timeit list(chain.from_iterable(kwargs.items()))
10000 loops, best of 3: 78.2 µs per loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, but what is your kwargs?
In [46]: kwargs = dict(enumerate(string.ascii_lowercase))
In [47]: %timeit make_flat(kwargs.items(), tuple())
100000 loops, best of 3: 11.1 µs per loop
In [48]: %timeit make_flat(kwargs.items())
100000 loops, best of 3: 10.3 µs per loop
In [49]: %timeit list(chain.from_iterable(kwargs.items()))
The slowest run took 5.11 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 6.89 µs per loop
In [50]: %timeit list(chain.from_iterable(kwargs.items()))
The slowest run took 5.22 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 6.93 µs per loop
In [51]: %timeit tuple(chain.from_iterable(kwargs.items()))
100000 loops, best of 3: 6.04 µs per loop```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kwargs = {i:i for i in range(1000)}
1 similar comment
thanks |
PR is squashed and merged. |
hmset_dict not the correct name, as well as the method of supporting the pair. Separate methods is that if someone thought to call the old hmset:
But I do not think that someone so does |
make_pair can also be used in mset |
Sorry, of course kwargs at the end
crazy? |
This does not work -- multiple values for positional args |
I try to challenge the motivation of separation methods. |
Ok, I want to keep original hopefully_its_pairs = get_something()
yield from redis.hmset(key, *hopefully_its_pairs)
# and assume `get_something` is a function with lots of if/else:
def get_something():
if case_a:
return 'field', 'value'
elif case_b:
return 'field2', 'value2'
elif ...
elif ...
elif case_N:
return {'field': "value"} Such cases may not be covered with tests and/or may appear only in production environment. |
Reasonable.
explicit arguments and simplified checks |
this may be conflicting with user's fields: |
but current implementation already contains conflict
|
Implementation