Package: cerberedis
CerbeRedisDe/serialiseCerberusdata to aRedisdatabase.Installation$pipinstallcerberedisExampleA basic example:fromcerberusimportValidator,TypeDefinitionfromredisimportRedisfromcerberedisimportCerbeRedisschema={'name':{'type':'string','required':True},'email':{'type':'string','required':True},}data={'name':'Bob','email':'bob@example.com'}# connect to our redis serverr=Redis()db=CerbeRedis(r)# save the schema under the type name 'User'# with a document id of 1model_name,id='User',1db.save(model_name,schema,id,document)# print the key the model is stored underprint(db.key(model_name,id))# reload the documentloaded_document=db.load(model_name,schema,id)A full featured example, including custom types:fromipaddressimportip_address,IPv4Address,IPv6Addressfromdatetimeimportdate,datetimefromcerberusimportValidator,TypeDefinitionfromredisimportRedis# alternatively use redis_mock (https://github.com/adamlwgriffiths/redis-mock)# from redis_mock import RedisfromcerberedisimportCerbeRedis# Add custom types to both Cerberus# https://docs.python-cerberus.org/en/stable/customize.htmlValidator.types_mapping.update({'ipaddress':TypeDefinition('ipaddress',(IPv4Address,IPv6Address),()),'ipv4address':TypeDefinition('ipv4address',(IPv4Address,),()),'ipv6address':TypeDefinition('ipv6address',(IPv6Address,),()),})# Provide CerbeRedis with information on how to handle these new Cerberus typesCerbeRedis.rules.update({# dictionary of: <cerberus type name>: [to redis function, from redis function]'ipaddress':[lambdax:str(x),lambdax:ip_address(x.decode('utf-8'))],'ipv4address':[lambdax:str(x),lambdax:IPv4Address(x.decode('utf-8'))],'ipv6address':[lambdax:str(x),lambdax:IPv6Address(x.decode('utf-8'))],})# An example Cerberus schema that uses all the built-in Cerberus types# and the custom ones we defined aboveschema={'boolean':{'type':'boolean','required':True},'binary':{'type':'binary','required':True},'date':{'type':'date','required':True},'datetime':{'type':'datetime','required':True},'float':{'type':'float','required':True},'integer':{'type':'integer','required':True},'number':{'type':'number','required':True},'string':{'type':'string','required':True},'dict':{'type':'dict','schema':{'dict_a':{'type':'string','required':True},'dict_b':{'type':'integer','required':True},}},'list':{'type':'list','schema':{'type':'integer'}},'set':{'type':'set','schema':{'type':'string'}},'ipv4address':{'type':'ipv4address','required':True},}data={'boolean':True,'binary':b'123','date':date.today(),'datetime':datetime.now(),'float':1.23,'integer':456,'number':789.0,'string':'abcdefg','dict':{'dict_a':'dict_a_value','dict_b':9999,},'list':[1,2,3,4,5],'set':{'a','b','c'},'ipv4address':IPv4Address('127.0.0.1'),}# Use cerberus to validate and normalise the datavalidator=Validator(schema)document=validator.normalized(data)ifnotdocument:raiseValueError(str(validator.errors))# connect to our redis serverr=Redis()db=CerbeRedis(r)model_name,id='TestModel',1db.save(model_name,schema,id,document)# reload the documentloaded_document=db.load(model_name,schema,id)# verify the data is laid out how we expectassertdocument==loaded_documentSupported TypesAll existingCerberus Types:booleanbinarydatedatetimefloatintegernumberstringdictlistsetLimitationsContainers cannot be nested. Ie. lists and sets cannot contain lists, sets, or dicts.Future WorkUse a Redis Pipeline for the load functionChangelog1.0.5Return None rather than empty containers. Permits us to know if the value actually exists.1.0.4Fix image link1.0.3Fix handling of None values for non-required fields1.0.2Expose key function1.0.1Version bump due to PyPi version pedantry1.0.0Initial releaseBSD 2-Clause LicenseCopyright (c) 2021, Adam Griffiths
All rights reserved.Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.