Last active
February 4, 2022 14:40
-
-
Save mindflayer/afb3cd0d385d63bb32557141962079f8 to your computer and use it in GitHub Desktop.
Make development great again!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
import requests | |
import redis | |
from mocket import Mocket | |
Mocket.enable('example', '.') | |
# get all users | |
users = requests.get( | |
'http://jsonplaceholder.typicode.com/users' | |
).json() | |
# users will be retrieved by userId | |
users_by_id = {u['id']: u for u in users} | |
todos = requests.get( | |
'http://jsonplaceholder.typicode.com/todos' | |
).json() | |
# let's put some salt and pepper, skipping some todos | |
filtered_todos = [ | |
t for t in todos if 'et' in t['title'] | |
] | |
# use a counter for user todos | |
user_todos = Counter() | |
for t in filtered_todos: | |
user_todos[t['userId']] += 1 | |
# it's going to connect to localhost on port 6379 | |
r = redis.StrictRedis()# be careful when you flush your DB | |
# in case you need it, it's as simple as | |
# r.flushdb() | |
for k, v in user_todos.items(): | |
r.zadd("todos", {users_by_id[k]["username"]: v}) | |
print(r.zrange('todos', 0, len(todos), desc=True, withscores=True)) |
After the first execution, there won't be any more accesses to the network (neither Redis nor HTTPS).
Mocket will be using the example.json
file produced by the first run.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On a Debian/Ubuntu, you'll need to execute to create a
virtualenv
, then: