Skip to content

Commit b6bee63

Browse files
committed
single values and hmset
1 parent ba4c225 commit b6bee63

File tree

5 files changed

+107
-14
lines changed

5 files changed

+107
-14
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Redis Python Tutorial
2+
3+
![Python](https://img.shields.io/badge/Python-v3.7-blue.svg?logo=python&longCache=true&logoColor=white&colorB=5e81ac&style=flat-square&colorA=4c566a)
4+
![Redis](https://img.shields.io/badge/Redis-v3.2.1-red.svg?longCache=true&style=flat-square&logo=redis&logoColor=white&colorA=4c566a&colorB=bf616a)
5+
![Loguru](https://img.shields.io/badge/Loguru-v0.4.1-blue.svg?longCache=true&logo=python&style=flat-square&logoColor=white&colorB=5e81ac&colorA=4c566a)
6+
![GitHub Last Commit](https://img.shields.io/github/last-commit/google/skia.svg?style=flat-square&colorA=4c566a&colorB=a3be8c&logo=GitHub)
7+
[![GitHub Issues](https://img.shields.io/github/issues/hackersandslackers/redis-python-tutorial.svg?style=flat-square&colorA=4c566a&logo=GitHub&colorB=ebcb8b)](https://github.com/hackersandslackers/redis-python-tutorial/issues)
8+
[![GitHub Stars](https://img.shields.io/github/stars/hackersandslackers/redis-python-tutorial.svg?style=flat-square8&colorA=4c566a&logo=GitHub&colorB=ebcb8b)](https://github.com/hackersandslackers/redis-python-tutorial/stargazers)
9+
[![GitHub Forks](https://img.shields.io/github/forks/hackersandslackers/redis-python-tutorial.svg?style=flat-square&colorA=4c566a&logo=GitHub&colorB=ebcb8b)](https://github.com/hackersandslackers/redis-python-tutorial/network)

config.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
redis_host = environ.get('REDIS_HOST')
55
redis_password = environ.get('REDIS_PASSWORD')
66
redis_port = environ.get('REDIS_PORT')
7-
redis_uri = f'redis://:{redis_host}@{redis_password}:{redis_port}'
7+
redis_db = environ.get('REDIS_DATABASE')
8+
redis_uri = f'redis://:{redis_host}@{redis_password}:{redis_port}/{redis_db}'
89

910
redis_expiration = 604800

poetry.lock

+38-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ authors = ["Todd Birchard <toddbirchard@gmail.com>"]
66
maintainers = ["Todd Birchard <toddbirchard@gmail.com>"]
77
license = "MIT"
88
readme = "README.md"
9-
homepage = ""
9+
homepage = "https://hackersandslackers.com/redis-python/"
1010
repository = "https://github.com/hackersandslackers/redis-python-tutorial/"
1111
documentation = "https://hackersandslackers.com/using-redis-python-applications/"
1212
keywords = ["Redis",
@@ -18,6 +18,8 @@ keywords = ["Redis",
1818
[tool.poetry.dependencies]
1919
python = "^3.7"
2020
redis = "*"
21+
loguru = "*"
22+
python-dotenv = "*"
2123

2224
[tool.poetry.dev-dependencies]
2325
pytest = "^4.6"
@@ -30,4 +32,4 @@ build-backend = "poetry.masonry.api"
3032
init = "wsgi:main"
3133

3234
[tool.poetry.urls]
33-
issues = "https://github.com/hackersandslackers/python-poetry-tutorial/issues"
35+
issues = "https://github.com/hackersandslackers/redis-python-tutorial/issues"

redis_python_tutorial/__init__.py

+54-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,63 @@
11
"""Application entry point."""
2+
import sys
23
import redis
3-
from config import redis_uri, redis_expiration
4+
import json
5+
from loguru import logger
6+
import time
7+
from config import (redis_host,
8+
redis_password,
9+
redis_port,
10+
redis_db,
11+
redis_expiration)
412

5-
r = redis.StrictRedis(url=redis_uri,
13+
r = redis.StrictRedis(host=redis_host,
14+
password=redis_password,
15+
port=redis_port,
16+
db=redis_db,
617
charset="utf-8",
718
decode_responses=True)
819

920

21+
logger.add(sys.stderr,
22+
format="{time} {level} {message}",
23+
filter="redis_python_tutorial",
24+
level="INFO")
25+
26+
1027
def main():
11-
r.set('name', 'todd')
28+
"""Create records in Redis."""
29+
set_single_values()
30+
set_nested_values()
31+
32+
33+
def set_single_values():
34+
"""Set single key/value pairs."""
1235
r.set('ip_address', '0.0.0.0.')
13-
r.set('entry_page', 'dashboard')
14-
r.set('current_page', 'account')
15-
r.set('name', 'todd', redis_expiration)
16-
r.set('name', 'todd', redis_expiration)
17-
r.set('name', 'todd', redis_expiration)
36+
r.set('timestamp', int(time.time()))
37+
r.set('user_agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3)')
38+
r.set('current_page', 'account', redis_expiration)
39+
logger.info(r.keys())
40+
41+
42+
def set_nested_values():
43+
"""Set a nested dictionary."""
44+
record = {
45+
"name": "Hackers and Slackers",
46+
"type": "Mediocre tutorials",
47+
"address": {
48+
"street": "42 E 69th Street",
49+
"city": "New York",
50+
"state": "NY",
51+
"zip": 10009,
52+
},
53+
"profiles": {
54+
"website": "https://hackersandslackers.com/",
55+
"twitter": "https://twitter.com/hackersslackers",
56+
"facebook": "https://www.facebook.com/groups/hackersnslackers"
57+
},
58+
"owner": {
59+
"name": "Todd Birchard",
60+
"email": "todd@example.com"
61+
}
62+
}
63+
r.hmset('business', json.dumps(record))

0 commit comments

Comments
 (0)