@@ -1073,7 +1073,7 @@ def hrandfield(self, key, count=None, withvalues=False):
1073
1073
return self .execute_command ("HRANDFIELD" , key , * params )
1074
1074
1075
1075
def randomkey (self ):
1076
- "Returns the name of a random key"
1076
+ """ Returns the name of a random key"" "
1077
1077
return self .execute_command ('RANDOMKEY' )
1078
1078
1079
1079
def rename (self , src , dst ):
@@ -1083,7 +1083,7 @@ def rename(self, src, dst):
1083
1083
return self .execute_command ('RENAME' , src , dst )
1084
1084
1085
1085
def renamenx (self , src , dst ):
1086
- "Rename key ``src`` to ``dst`` if ``dst`` doesn't already exist"
1086
+ """ Rename key ``src`` to ``dst`` if ``dst`` doesn't already exist"" "
1087
1087
return self .execute_command ('RENAMENX' , src , dst )
1088
1088
1089
1089
def restore (self , name , ttl , value , replace = False , absttl = False ,
@@ -1545,32 +1545,25 @@ def sort(self, name, start=None, num=None, by=None, get=None,
1545
1545
1546
1546
pieces = [name ]
1547
1547
if by is not None :
1548
- pieces .append (b'BY' )
1549
- pieces .append (by )
1548
+ pieces .extend ([b'BY' , by ])
1550
1549
if start is not None and num is not None :
1551
- pieces .append (b'LIMIT' )
1552
- pieces .append (start )
1553
- pieces .append (num )
1550
+ pieces .extend ([b'LIMIT' , start , num ])
1554
1551
if get is not None :
1555
1552
# If get is a string assume we want to get a single value.
1556
1553
# Otherwise assume it's an interable and we want to get multiple
1557
1554
# values. We can't just iterate blindly because strings are
1558
1555
# iterable.
1559
1556
if isinstance (get , (bytes , str )):
1560
- pieces .append (b'GET' )
1561
- pieces .append (get )
1557
+ pieces .extend ([b'GET' , get ])
1562
1558
else :
1563
1559
for g in get :
1564
- pieces .append (b'GET' )
1565
- pieces .append (g )
1560
+ pieces .extend ([b'GET' , g ])
1566
1561
if desc :
1567
1562
pieces .append (b'DESC' )
1568
1563
if alpha :
1569
1564
pieces .append (b'ALPHA' )
1570
1565
if store is not None :
1571
- pieces .append (b'STORE' )
1572
- pieces .append (store )
1573
-
1566
+ pieces .extend ([b'STORE' , store ])
1574
1567
if groups :
1575
1568
if not get or isinstance (get , (bytes , str )) or len (get ) < 2 :
1576
1569
raise DataError ('when using "groups" the "get" argument '
@@ -1729,15 +1722,15 @@ def zscan_iter(self, name, match=None, count=None,
1729
1722
1730
1723
# SET COMMANDS
1731
1724
def sadd (self , name , * values ):
1732
- "Add ``value(s)`` to set ``name``"
1725
+ """ Add ``value(s)`` to set ``name``"" "
1733
1726
return self .execute_command ('SADD' , name , * values )
1734
1727
1735
1728
def scard (self , name ):
1736
- "Return the number of elements in set ``name``"
1729
+ """ Return the number of elements in set ``name``"" "
1737
1730
return self .execute_command ('SCARD' , name )
1738
1731
1739
1732
def sdiff (self , keys , * args ):
1740
- "Return the difference of sets specified by ``keys``"
1733
+ """ Return the difference of sets specified by ``keys``"" "
1741
1734
args = list_or_args (keys , args )
1742
1735
return self .execute_command ('SDIFF' , * args )
1743
1736
@@ -1750,7 +1743,7 @@ def sdiffstore(self, dest, keys, *args):
1750
1743
return self .execute_command ('SDIFFSTORE' , dest , * args )
1751
1744
1752
1745
def sinter (self , keys , * args ):
1753
- "Return the intersection of sets specified by ``keys``"
1746
+ """ Return the intersection of sets specified by ``keys``"" "
1754
1747
args = list_or_args (keys , args )
1755
1748
return self .execute_command ('SINTER' , * args )
1756
1749
@@ -1763,15 +1756,17 @@ def sinterstore(self, dest, keys, *args):
1763
1756
return self .execute_command ('SINTERSTORE' , dest , * args )
1764
1757
1765
1758
def sismember (self , name , value ):
1766
- "Return a boolean indicating if ``value`` is a member of set ``name``"
1759
+ """
1760
+ Return a boolean indicating if ``value`` is a member of set ``name``
1761
+ """
1767
1762
return self .execute_command ('SISMEMBER' , name , value )
1768
1763
1769
1764
def smembers (self , name ):
1770
- "Return all members of the set ``name``"
1765
+ """ Return all members of the set ``name``"" "
1771
1766
return self .execute_command ('SMEMBERS' , name )
1772
1767
1773
1768
def smove (self , src , dst , value ):
1774
- "Move ``value`` from set ``src`` to set ``dst`` atomically"
1769
+ """ Move ``value`` from set ``src`` to set ``dst`` atomically"" "
1775
1770
return self .execute_command ('SMOVE' , src , dst , value )
1776
1771
1777
1772
def spop (self , name , count = None ):
@@ -1850,8 +1845,7 @@ def xadd(self, name, fields, id='*', maxlen=None, approximate=True,
1850
1845
pieces .append (b'~' )
1851
1846
pieces .append (minid )
1852
1847
if limit is not None :
1853
- pieces .append (b"LIMIT" )
1854
- pieces .append (limit )
1848
+ pieces .extend ([b'LIMIT' , limit ])
1855
1849
if nomkstream :
1856
1850
pieces .append (b'NOMKSTREAM' )
1857
1851
pieces .append (id )
@@ -2440,41 +2434,113 @@ def bzpopmin(self, keys, timeout=0):
2440
2434
keys .append (timeout )
2441
2435
return self .execute_command ('BZPOPMIN' , * keys )
2442
2436
2437
+ def _zrange (self , command , dest , name , start , end , desc = False ,
2438
+ byscore = False , bylex = False , withscores = False ,
2439
+ score_cast_func = float , offset = None , num = None ):
2440
+ if byscore and bylex :
2441
+ raise DataError ("``byscore`` and ``bylex`` can not be "
2442
+ "specified together." )
2443
+ if (offset is not None and num is None ) or \
2444
+ (num is not None and offset is None ):
2445
+ raise DataError ("``offset`` and ``num`` must both be specified." )
2446
+ if bylex and withscores :
2447
+ raise DataError ("``withscores`` not supported in combination "
2448
+ "with ``bylex``." )
2449
+ pieces = [command ]
2450
+ if dest :
2451
+ pieces .append (dest )
2452
+ pieces .extend ([name , start , end ])
2453
+ if byscore :
2454
+ pieces .append ('BYSCORE' )
2455
+ if bylex :
2456
+ pieces .append ('BYLEX' )
2457
+ if desc :
2458
+ pieces .append ('REV' )
2459
+ if offset is not None and num is not None :
2460
+ pieces .extend (['LIMIT' , offset , num ])
2461
+ if withscores :
2462
+ pieces .append ('WITHSCORES' )
2463
+ options = {
2464
+ 'withscores' : withscores ,
2465
+ 'score_cast_func' : score_cast_func
2466
+ }
2467
+ return self .execute_command (* pieces , ** options )
2468
+
2443
2469
def zrange (self , name , start , end , desc = False , withscores = False ,
2444
- score_cast_func = float ):
2470
+ score_cast_func = float , byscore = False , bylex = False ,
2471
+ offset = None , num = None ):
2445
2472
"""
2446
2473
Return a range of values from sorted set ``name`` between
2447
2474
``start`` and ``end`` sorted in ascending order.
2448
2475
2449
2476
``start`` and ``end`` can be negative, indicating the end of the range.
2450
2477
2451
- ``desc`` a boolean indicating whether to sort the results descendingly
2478
+ ``desc`` a boolean indicating whether to sort the results in reversed
2479
+ order.
2452
2480
2453
2481
``withscores`` indicates to return the scores along with the values.
2482
+ The return type is a list of (value, score) pairs.
2483
+
2484
+ ``score_cast_func`` a callable used to cast the score return value.
2485
+
2486
+ ``byscore`` when set to True, returns the range of elements from the
2487
+ sorted set having scores equal or between ``start`` and ``end``.
2488
+
2489
+ ``bylex`` when set to True, returns the range of elements from the
2490
+ sorted set between the ``start`` and ``end`` lexicographical closed
2491
+ range intervals.
2492
+ Valid ``start`` and ``end`` must start with ( or [, in order to specify
2493
+ whether the range interval is exclusive or inclusive, respectively.
2494
+
2495
+ ``offset`` and ``num`` are specified, then return a slice of the range.
2496
+ Can't be provided when using ``bylex``.
2497
+ """
2498
+ return self ._zrange ('ZRANGE' , None , name , start , end , desc , byscore ,
2499
+ bylex , withscores , score_cast_func , offset , num )
2500
+
2501
+ def zrevrange (self , name , start , end , withscores = False ,
2502
+ score_cast_func = float ):
2503
+ """
2504
+ Return a range of values from sorted set ``name`` between
2505
+ ``start`` and ``end`` sorted in descending order.
2506
+
2507
+ ``start`` and ``end`` can be negative, indicating the end of the range.
2508
+
2509
+ ``withscores`` indicates to return the scores along with the values
2454
2510
The return type is a list of (value, score) pairs
2455
2511
2456
2512
``score_cast_func`` a callable used to cast the score return value
2457
2513
"""
2458
- if desc :
2459
- return self .zrevrange (name , start , end , withscores ,
2460
- score_cast_func )
2461
- pieces = ['ZRANGE' , name , start , end ]
2462
- if withscores :
2463
- pieces .append (b'WITHSCORES' )
2464
- options = {
2465
- 'withscores' : withscores ,
2466
- 'score_cast_func' : score_cast_func
2467
- }
2468
- return self .execute_command (* pieces , ** options )
2514
+ return self .zrange (name , start , end , desc = True ,
2515
+ withscores = withscores ,
2516
+ score_cast_func = score_cast_func )
2469
2517
2470
- def zrangestore (self , dest , name , start , end ):
2518
+ def zrangestore (self , dest , name , start , end ,
2519
+ byscore = False , bylex = False , desc = False ,
2520
+ offset = None , num = None ):
2471
2521
"""
2472
2522
Stores in ``dest`` the result of a range of values from sorted set
2473
2523
``name`` between ``start`` and ``end`` sorted in ascending order.
2474
2524
2475
2525
``start`` and ``end`` can be negative, indicating the end of the range.
2526
+
2527
+ ``byscore`` when set to True, returns the range of elements from the
2528
+ sorted set having scores equal or between ``start`` and ``end``.
2529
+
2530
+ ``bylex`` when set to True, returns the range of elements from the
2531
+ sorted set between the ``start`` and ``end`` lexicographical closed
2532
+ range intervals.
2533
+ Valid ``start`` and ``end`` must start with ( or [, in order to specify
2534
+ whether the range interval is exclusive or inclusive, respectively.
2535
+
2536
+ ``desc`` a boolean indicating whether to sort the results in reversed
2537
+ order.
2538
+
2539
+ ``offset`` and ``num`` are specified, then return a slice of the range.
2540
+ Can't be provided when using ``bylex``.
2476
2541
"""
2477
- return self .execute_command ('ZRANGESTORE' , dest , name , start , end )
2542
+ return self ._zrange ('ZRANGESTORE' , dest , name , start , end , desc ,
2543
+ byscore , bylex , False , None , offset , num )
2478
2544
2479
2545
def zrangebylex (self , name , min , max , start = None , num = None ):
2480
2546
"""
@@ -2484,13 +2550,7 @@ def zrangebylex(self, name, min, max, start=None, num=None):
2484
2550
If ``start`` and ``num`` are specified, then return a slice of the
2485
2551
range.
2486
2552
"""
2487
- if (start is not None and num is None ) or \
2488
- (num is not None and start is None ):
2489
- raise DataError ("``start`` and ``num`` must both be specified" )
2490
- pieces = ['ZRANGEBYLEX' , name , min , max ]
2491
- if start is not None and num is not None :
2492
- pieces .extend ([b'LIMIT' , start , num ])
2493
- return self .execute_command (* pieces )
2553
+ return self .zrange (name , min , max , bylex = True , offset = start , num = num )
2494
2554
2495
2555
def zrevrangebylex (self , name , max , min , start = None , num = None ):
2496
2556
"""
@@ -2500,13 +2560,8 @@ def zrevrangebylex(self, name, max, min, start=None, num=None):
2500
2560
If ``start`` and ``num`` are specified, then return a slice of the
2501
2561
range.
2502
2562
"""
2503
- if (start is not None and num is None ) or \
2504
- (num is not None and start is None ):
2505
- raise DataError ("``start`` and ``num`` must both be specified" )
2506
- pieces = ['ZREVRANGEBYLEX' , name , max , min ]
2507
- if start is not None and num is not None :
2508
- pieces .extend ([b'LIMIT' , start , num ])
2509
- return self .execute_command (* pieces )
2563
+ return self .zrange (name , max , min , desc = True ,
2564
+ bylex = True , offset = start , num = num )
2510
2565
2511
2566
def zrangebyscore (self , name , min , max , start = None , num = None ,
2512
2567
withscores = False , score_cast_func = float ):
@@ -2522,19 +2577,29 @@ def zrangebyscore(self, name, min, max, start=None, num=None,
2522
2577
2523
2578
`score_cast_func`` a callable used to cast the score return value
2524
2579
"""
2525
- if (start is not None and num is None ) or \
2526
- (num is not None and start is None ):
2527
- raise DataError ("``start`` and ``num`` must both be specified" )
2528
- pieces = ['ZRANGEBYSCORE' , name , min , max ]
2529
- if start is not None and num is not None :
2530
- pieces .extend ([b'LIMIT' , start , num ])
2531
- if withscores :
2532
- pieces .append (b'WITHSCORES' )
2533
- options = {
2534
- 'withscores' : withscores ,
2535
- 'score_cast_func' : score_cast_func
2536
- }
2537
- return self .execute_command (* pieces , ** options )
2580
+ return self .zrange (name , min , max , byscore = True ,
2581
+ offset = start , num = num ,
2582
+ withscores = withscores ,
2583
+ score_cast_func = score_cast_func )
2584
+
2585
+ def zrevrangebyscore (self , name , max , min , start = None , num = None ,
2586
+ withscores = False , score_cast_func = float ):
2587
+ """
2588
+ Return a range of values from the sorted set ``name`` with scores
2589
+ between ``min`` and ``max`` in descending order.
2590
+
2591
+ If ``start`` and ``num`` are specified, then return a slice
2592
+ of the range.
2593
+
2594
+ ``withscores`` indicates to return the scores along with the values.
2595
+ The return type is a list of (value, score) pairs
2596
+
2597
+ ``score_cast_func`` a callable used to cast the score return value
2598
+ """
2599
+ return self .zrange (name , max , min , desc = True ,
2600
+ byscore = True , offset = start ,
2601
+ num = num , withscores = withscores ,
2602
+ score_cast_func = score_cast_func )
2538
2603
2539
2604
def zrank (self , name , value ):
2540
2605
"""
@@ -2572,56 +2637,6 @@ def zremrangebyscore(self, name, min, max):
2572
2637
"""
2573
2638
return self .execute_command ('ZREMRANGEBYSCORE' , name , min , max )
2574
2639
2575
- def zrevrange (self , name , start , end , withscores = False ,
2576
- score_cast_func = float ):
2577
- """
2578
- Return a range of values from sorted set ``name`` between
2579
- ``start`` and ``end`` sorted in descending order.
2580
-
2581
- ``start`` and ``end`` can be negative, indicating the end of the range.
2582
-
2583
- ``withscores`` indicates to return the scores along with the values
2584
- The return type is a list of (value, score) pairs
2585
-
2586
- ``score_cast_func`` a callable used to cast the score return value
2587
- """
2588
- pieces = ['ZREVRANGE' , name , start , end ]
2589
- if withscores :
2590
- pieces .append (b'WITHSCORES' )
2591
- options = {
2592
- 'withscores' : withscores ,
2593
- 'score_cast_func' : score_cast_func
2594
- }
2595
- return self .execute_command (* pieces , ** options )
2596
-
2597
- def zrevrangebyscore (self , name , max , min , start = None , num = None ,
2598
- withscores = False , score_cast_func = float ):
2599
- """
2600
- Return a range of values from the sorted set ``name`` with scores
2601
- between ``min`` and ``max`` in descending order.
2602
-
2603
- If ``start`` and ``num`` are specified, then return a slice
2604
- of the range.
2605
-
2606
- ``withscores`` indicates to return the scores along with the values.
2607
- The return type is a list of (value, score) pairs
2608
-
2609
- ``score_cast_func`` a callable used to cast the score return value
2610
- """
2611
- if (start is not None and num is None ) or \
2612
- (num is not None and start is None ):
2613
- raise DataError ("``start`` and ``num`` must both be specified" )
2614
- pieces = ['ZREVRANGEBYSCORE' , name , max , min ]
2615
- if start is not None and num is not None :
2616
- pieces .extend ([b'LIMIT' , start , num ])
2617
- if withscores :
2618
- pieces .append (b'WITHSCORES' )
2619
- options = {
2620
- 'withscores' : withscores ,
2621
- 'score_cast_func' : score_cast_func
2622
- }
2623
- return self .execute_command (* pieces , ** options )
2624
-
2625
2640
def zrevrank (self , name , value ):
2626
2641
"""
2627
2642
Returns a 0-based value indicating the descending rank of
0 commit comments