Skip to content

Commit 149f56a

Browse files
Merge pull request #1831 from redis/DOC-5225-testable-python-prob-examples
DOC-5225 enabled testable Python prob examples
2 parents f43d872 + 80bd6c5 commit 149f56a

File tree

1 file changed

+12
-179
lines changed
  • content/develop/clients/redis-py

1 file changed

+12
-179
lines changed

content/develop/clients/redis-py/prob.md

Lines changed: 12 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -99,47 +99,16 @@ add. The following example adds some names to a Bloom filter representing
9999
a list of users and checks for the presence or absence of users in the list.
100100
Note that you must use the `bf()` method to access the Bloom filter commands.
101101

102-
```py
103-
res1 = r.bf().madd("recorded_users", "andy", "cameron", "david", "michelle")
104-
print(res1) # >>> [1, 1, 1, 1]
105-
106-
res2 = r.bf().exists("recorded_users", "cameron")
107-
print(res2) # >>> 1
108-
109-
res3 = r.bf().exists("recorded_users", "kaitlyn")
110-
print(res3) # >>> 0
111-
```
112-
113-
<!-- < clients-example home_prob_dts bloom Python >}}
114-
< /clients-example >}} -->
102+
{{< clients-example home_prob_dts bloom Python >}}
103+
{{< /clients-example >}}
115104

116105
A Cuckoo filter has similar features to a Bloom filter, but also supports
117106
a deletion operation to remove hashes from a set, as shown in the example
118107
below. Note that you must use the `cf()` method to access the Cuckoo filter
119108
commands.
120109

121-
```py
122-
res4 = r.cf().add("other_users", "paolo")
123-
print(res4) # >>> 1
124-
125-
res5 = r.cf().add("other_users", "kaitlyn")
126-
print(res5) # >>> 1
127-
128-
res6 = r.cf().add("other_users", "rachel")
129-
print(res6) # >>> 1
130-
131-
res7 = r.cf().mexists("other_users", "paolo", "rachel", "andy")
132-
print(res7) # >>> [1, 1, 0]
133-
134-
res8 = r.cf().delete("other_users", "paolo")
135-
print(res8) # >>> 1
136-
137-
res9 = r.cf().exists("other_users", "paolo")
138-
print(res9) # >>> 0
139-
```
140-
141-
<!-- < clients-example home_prob_dts cuckoo Python >}}
142-
< /clients-example >}} -->
110+
{{< clients-example home_prob_dts cuckoo Python >}}
111+
{{< /clients-example >}}
143112

144113
Which of these two data types you choose depends on your use case.
145114
Bloom filters are generally faster than Cuckoo filters when adding new items,
@@ -159,28 +128,8 @@ You can also merge two or more HyperLogLogs to find the cardinality of the
159128
[union](https://en.wikipedia.org/wiki/Union_(set_theory)) of the sets they
160129
represent.
161130

162-
```py
163-
res10 = r.pfadd("group:1", "andy", "cameron", "david")
164-
print(res10) # >>> 1
165-
166-
res11 = r.pfcount("group:1")
167-
print(res11) # >>> 3
168-
169-
res12 = r.pfadd("group:2", "kaitlyn", "michelle", "paolo", "rachel")
170-
print(res12) # >>> 1
171-
172-
res13 = r.pfcount("group:2")
173-
print(res13) # >>> 4
174-
175-
res14 = r.pfmerge("both_groups", "group:1", "group:2")
176-
print(res14) # >>> True
177-
178-
res15 = r.pfcount("both_groups")
179-
print(res15) # >>> 7
180-
```
181-
182-
<!-- < clients-example home_prob_dts hyperloglog Python >}}
183-
< /clients-example >}} -->
131+
{{< clients-example home_prob_dts hyperloglog Python >}}
132+
{{< /clients-example >}}
184133

185134
The main benefit that HyperLogLogs offer is their very low
186135
memory usage. They can count up to 2^64 items with less than
@@ -220,36 +169,8 @@ a Count-min sketch object, add data to it, and then query it.
220169
Note that you must use the `cms()` method to access the Count-min
221170
sketch commands.
222171

223-
```py
224-
# Specify that you want to keep the counts within 0.01
225-
# (1%) of the true value with a 0.005 (0.5%) chance
226-
# of going outside this limit.
227-
res16 = r.cms().initbyprob("items_sold", 0.01, 0.005)
228-
print(res16) # >>> True
229-
230-
# The parameters for `incrby()` are two lists. The count
231-
# for each item in the first list is incremented by the
232-
# value at the same index in the second list.
233-
res17 = r.cms().incrby(
234-
"items_sold",
235-
["bread", "tea", "coffee", "beer"], # Items sold
236-
[300, 200, 200, 100]
237-
)
238-
print(res17) # >>> [300, 200, 200, 100]
239-
240-
res18 = r.cms().incrby(
241-
"items_sold",
242-
["bread", "coffee"],
243-
[100, 150]
244-
)
245-
print(res18) # >>> [400, 350]
246-
247-
res19 = r.cms().query("items_sold", "bread", "tea", "coffee", "beer")
248-
print(res19) # >>> [400, 200, 350, 100]
249-
```
250-
251-
<!-- < clients-example home_prob_dts cms Python >}}
252-
< /clients-example >}} -->
172+
{{< clients-example home_prob_dts cms Python >}}
173+
{{< /clients-example >}}
253174

254175
The advantage of using a CMS over keeping an exact count with a
255176
[sorted set]({{< relref "/develop/data-types/sorted-sets" >}})
@@ -281,53 +202,8 @@ shows how to merge two or more t-digest objects to query the combined
281202
data set. Note that you must use the `tdigest()` method to access the
282203
t-digest commands.
283204

284-
```py
285-
res20 = r.tdigest().create("male_heights")
286-
print(res20) # >>> True
287-
288-
res21 = r.tdigest().add(
289-
"male_heights",
290-
[175.5, 181, 160.8, 152, 177, 196, 164]
291-
)
292-
print(res21) # >>> OK
293-
294-
res22 = r.tdigest().min("male_heights")
295-
print(res22) # >>> 152.0
296-
297-
res23 = r.tdigest().max("male_heights")
298-
print(res23) # >>> 196.0
299-
300-
res24 = r.tdigest().quantile("male_heights", 0.75)
301-
print(res24) # >>> 181
302-
303-
# Note that the CDF value for 181 is not exactly
304-
# 0.75. Both values are estimates.
305-
res25 = r.tdigest().cdf("male_heights", 181)
306-
print(res25) # >>> [0.7857142857142857]
307-
308-
res26 = r.tdigest().create("female_heights")
309-
print(res26) # >>> True
310-
311-
res27 = r.tdigest().add(
312-
"female_heights",
313-
[155.5, 161, 168.5, 170, 157.5, 163, 171]
314-
)
315-
print(res27) # >>> OK
316-
317-
res28 = r.tdigest().quantile("female_heights", 0.75)
318-
print(res28) # >>> [170]
319-
320-
res29 = r.tdigest().merge(
321-
"all_heights", 2, "male_heights", "female_heights"
322-
)
323-
print(res29) # >>> OK
324-
325-
res30 = r.tdigest().quantile("all_heights", 0.75)
326-
print(res30) # >>> [175.5]
327-
```
328-
329-
<!-- < clients-example home_prob_dts tdigest Python >}}
330-
< /clients-example >}} -->
205+
{{< clients-example home_prob_dts tdigest Python >}}
206+
{{< /clients-example >}}
331207

332208
A t-digest object also supports several other related commands, such
333209
as querying by rank. See the
@@ -349,48 +225,5 @@ top *k* items and query whether or not a given item is in the
349225
list. Note that you must use the `topk()` method to access the
350226
Top-K commands.
351227

352-
```py
353-
# The `reserve()` method creates the Top-K object with
354-
# the given key. The parameters are the number of items
355-
# in the ranking and values for `width`, `depth`, and
356-
# `decay`, described in the Top-K reference page.
357-
res31 = r.topk().reserve("top_3_songs", 3, 7, 8, 0.9)
358-
print(res31) # >>> True
359-
360-
# The parameters for `incrby()` are two lists. The count
361-
# for each item in the first list is incremented by the
362-
# value at the same index in the second list.
363-
res32 = r.topk().incrby(
364-
"top_3_songs",
365-
[
366-
"Starfish Trooper",
367-
"Only one more time",
368-
"Rock me, Handel",
369-
"How will anyone know?",
370-
"Average lover",
371-
"Road to everywhere"
372-
],
373-
[
374-
3000,
375-
1850,
376-
1325,
377-
3890,
378-
4098,
379-
770
380-
]
381-
)
382-
print(res32)
383-
# >>> [None, None, None, 'Rock me, Handel', 'Only one more time', None]
384-
385-
res33 = r.topk().list("top_3_songs")
386-
print(res33)
387-
# >>> ['Average lover', 'How will anyone know?', 'Starfish Trooper']
388-
389-
res34 = r.topk().query(
390-
"top_3_songs", "Starfish Trooper", "Road to everywhere"
391-
)
392-
print(res34) # >>> [1, 0]
393-
```
394-
395-
<!-- < clients-example home_prob_dts topk Python >}}
396-
< /clients-example >}} -->
228+
{{< clients-example home_prob_dts topk Python >}}
229+
{{< /clients-example >}}

0 commit comments

Comments
 (0)