MCPcopy
hub / github.com/redis/redis-py / encode

Method encode

redis/_parsers/encoders.py:14–35  ·  view source on GitHub ↗

Return a bytestring or bytes-like representation of the value

(self, value)

Source from the content-addressed store, hash-verified

12 self.decode_responses = decode_responses
13
14 def encode(self, value):
15 "Return a bytestring or bytes-like representation of the value"
16 if isinstance(value, (bytes, bytearray, memoryview)):
17 return value
18 elif isinstance(value, bool):
19 # special case bool since it is a subclass of int
20 raise DataError(
21 "Invalid input of type: 'bool'. Convert to a "
22 "bytes, string, int or float first."
23 )
24 elif isinstance(value, (int, float)):
25 value = repr(value).encode()
26 elif not isinstance(value, str):
27 # a value we don't know how to deal with. throw an error
28 typename = type(value).__name__
29 raise DataError(
30 f"Invalid input of type: '{typename}'. "
31 f"Convert to a bytes, string, int or float first."
32 )
33 if isinstance(value, str):
34 value = value.encode(self.encoding, self.encoding_errors)
35 return value
36
37 def decode(self, value, force=False):
38 "Return a unicode string from the bytes-like representation"

Calls 1

DataErrorClass · 0.85