MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / from_bytes

Method from_bytes

lib/sqlalchemy/dialects/postgresql/bitstring.py:68–90  ·  view source on GitHub ↗

Returns a ``BitString`` consisting of the bits in the given ``value`` bytes. If ``length`` is provided, then the length of the provided string will be exactly ``length``, with ``'0'`` bits inserted at the left of the string in order to produce a value of the required

(cls, value: bytes, length: int = -1)

Source from the content-addressed store, hash-verified

66
67 @classmethod
68 def from_bytes(cls, value: bytes, length: int = -1) -> BitString:
69 """Returns a ``BitString`` consisting of the bits in the given
70 ``value`` bytes.
71
72 If ``length`` is provided, then the length of the provided string
73 will be exactly ``length``, with ``'0'`` bits inserted at the left of
74 the string in order to produce a value of the required length.
75 If the bits obtained by omitting the leading ``'0'`` bits of ``value``
76 cannot be represented in a string of this length a ``ValueError``
77 will be raised.
78 """
79 str_v: str = "".join(f"{int(c):08b}" for c in value)
80 if length >= 0:
81 str_v = str_v.lstrip("0")
82
83 if len(str_v) > length:
84 raise ValueError(
85 f"Cannot encode {value!r} as a BitString of "
86 f"length {length}"
87 )
88 str_v = str_v.zfill(length)
89
90 return cls(str_v)
91
92 def get_bit(self, index: int) -> Literal["0", "1"]:
93 """Returns the value of the flag at the given

Callers 1

test_bytes_conversionMethod · 0.80

Calls 3

lstripMethod · 0.80
zfillMethod · 0.80
joinMethod · 0.45

Tested by 1

test_bytes_conversionMethod · 0.64