Skip to content

Instantly share code, notes, and snippets.

@mindflayer
Last active February 4, 2022 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindflayer/afb3cd0d385d63bb32557141962079f8 to your computer and use it in GitHub Desktop.
Save mindflayer/afb3cd0d385d63bb32557141962079f8 to your computer and use it in GitHub Desktop.
Make development great again!
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))
@mindflayer
Copy link
Author

mindflayer commented Feb 4, 2022

On a Debian/Ubuntu, you'll need to execute to create a virtualenv, then:

pip install requests redis mocket ipython
sudo apt install redis-server

@mindflayer
Copy link
Author

mindflayer commented Feb 4, 2022

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