-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from redis_dict import RedisDict | ||
### Insertion Order | ||
from redis_dict import PythonRedisDict | ||
|
||
dic = PythonRedisDict() | ||
dic["1"] = "one" | ||
dic["2"] = "two" | ||
dic["3"] = "three" | ||
|
||
assert list(dic.keys()) == ["1", "2", "3"] | ||
|
||
### Extending RedisDict with Custom Types | ||
import json | ||
|
||
class Person: | ||
def __init__(self, name, age): | ||
self.name = name | ||
self.age = age | ||
|
||
def encode(self) -> str: | ||
return json.dumps(self.__dict__) | ||
|
||
@classmethod | ||
def decode(cls, encoded_str: str) -> 'Person': | ||
return cls(**json.loads(encoded_str)) | ||
|
||
redis_dict = RedisDict() | ||
|
||
# Extend redis dict with the new type | ||
redis_dict.extends_type(Person) | ||
|
||
# RedisDict can now seamlessly handle Person instances. | ||
person = Person(name="John", age=32) | ||
redis_dict["person1"] = person | ||
|
||
result = redis_dict["person1"] | ||
|
||
assert result.name == person.name | ||
assert result.age == person.age |