File: dt_bitmap.py

package info (click to toggle)
python-redis 6.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,432 kB
  • sloc: python: 60,318; sh: 179; makefile: 128
file content (40 lines) | stat: -rw-r--r-- 768 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# EXAMPLE: bitmap_tutorial
# HIDE_START
"""
Code samples for Bitmap doc pages:
    https://redis.io/docs/latest/develop/data-types/bitmaps/
"""
import redis

r = redis.Redis(decode_responses=True)
# HIDE_END

# REMOVE_START
r.delete("pings:2024-01-01-00:00")
# REMOVE_END

# STEP_START ping
res1 = r.setbit("pings:2024-01-01-00:00", 123, 1)
print(res1)  # >>> 0

res2 = r.getbit("pings:2024-01-01-00:00", 123)
print(res2)  # >>> 1

res3 = r.getbit("pings:2024-01-01-00:00", 456)
print(res3)  # >>> 0
# STEP_END

# REMOVE_START
assert res1 == 0
# REMOVE_END

# STEP_START bitcount
# HIDE_START
r.setbit("pings:2024-01-01-00:00", 123, 1)
# HIDE_END
res4 = r.bitcount("pings:2024-01-01-00:00")
print(res4)  # >>> 1
# STEP_END
# REMOVE_START
assert res4 == 1
# REMOVE_END