dj_bitfields package

Submodules

dj_bitfields.bitstringfield module

class dj_bitfields.bitstringfield.Bits(auto=None, length=None, offset=None, **kwargs)[source]

Bases: object

A container holding an immutable sequence of bits.

For a mutable container use the BitArray class instead.

Methods:

all() – Check if all specified bits are set to 1 or 0. any() – Check if any of specified bits are set to 1 or 0. count() – Count the number of bits set to 1 or 0. cut() – Create generator of constant sized chunks. endswith() – Return whether the bitstring ends with a sub-string. find() – Find a sub-bitstring in the current bitstring. findall() – Find all occurrences of a sub-bitstring in the current bitstring. join() – Join bitstrings together using current bitstring. rfind() – Seek backwards to find a sub-bitstring. split() – Create generator of chunks split by a delimiter. startswith() – Return whether the bitstring starts with a sub-bitstring. tobytes() – Return bitstring as bytes, padding if needed. tofile() – Write bitstring to file, padding if needed. unpack() – Interpret bits using format string.

Special methods:

Also available are the operators [], ==, !=, +, *, ~, <<, >>, &, |, ^.

Properties:

bin – The bitstring as a binary string. bool – For single bit bitstrings, interpret as True or False. bytes – The bitstring as a bytes object. float – Interpret as a floating point number. floatbe – Interpret as a big-endian floating point number. floatle – Interpret as a little-endian floating point number. floatne – Interpret as a native-endian floating point number. hex – The bitstring as a hexadecimal string. int – Interpret as a two’s complement signed integer. intbe – Interpret as a big-endian signed integer. intle – Interpret as a little-endian signed integer. intne – Interpret as a native-endian signed integer. len – Length of the bitstring in bits. oct – The bitstring as an octal string. se – Interpret as a signed exponential-Golomb code. ue – Interpret as an unsigned exponential-Golomb code. sie – Interpret as a signed interleaved exponential-Golomb code. uie – Interpret as an unsigned interleaved exponential-Golomb code. uint – Interpret as a two’s complement unsigned integer. uintbe – Interpret as a big-endian unsigned integer. uintle – Interpret as a little-endian unsigned integer. uintne – Interpret as a native-endian unsigned integer.

all(value, pos=None)[source]

Return True if one or many bits are all set to value.

value – If value is True then checks for bits set to 1, otherwise
checks for bits set to 0.
pos – An iterable of bit positions. Negative numbers are treated in
the same way as slice indices. Defaults to the whole bitstring.
any(value, pos=None)[source]

Return True if any of one or many bits are set to value.

value – If value is True then checks for bits set to 1, otherwise
checks for bits set to 0.
pos – An iterable of bit positions. Negative numbers are treated in
the same way as slice indices. Defaults to the whole bitstring.
bin

The bitstring as a binary string. Read only.

bool

The bitstring as a bool (True or False). Read only.

bytes

The bitstring as a bytes object. Read only.

count(value)[source]

Return count of total number of either zero or one bits.

value – If True then bits set to 1 are counted, otherwise bits set
to 0 are counted.
>>> Bits('0xef').count(1)
7
cut(bits, start=None, end=None, count=None)[source]

Return bitstring generator by cutting into bits sized chunks.

bits – The size in bits of the bitstring chunks to generate. start – The bit position to start the first cut. Defaults to 0. end – The bit position one past the last bit to use in the cut.

Defaults to self.len.
count – If specified then at most count items are generated.
Default is to cut as many times as possible.
endswith(suffix, start=None, end=None)[source]

Return whether the current bitstring ends with suffix.

suffix – The bitstring to search for. start – The bit position to start from. Defaults to 0. end – The bit position to end at. Defaults to self.len.

find(bs, start=None, end=None, bytealigned=None)[source]

Find first occurrence of substring bs.

Returns a single item tuple with the bit position if found, or an empty tuple if not found. The bit position (pos property) will also be set to the start of the substring if it is found.

bs – The bitstring to find. start – The bit position to start the search. Defaults to 0. end – The bit position one past the last bit to search.

Defaults to self.len.
bytealigned – If True the bitstring will only be
found on byte boundaries.

Raises ValueError if bs is empty, if start < 0, if end > self.len or if end < start.

>>> BitArray('0xc3e').find('0b1111')
(6,)
findall(bs, start=None, end=None, count=None, bytealigned=None)[source]

Find all occurrences of bs. Return generator of bit positions.

bs – The bitstring to find. start – The bit position to start the search. Defaults to 0. end – The bit position one past the last bit to search.

Defaults to self.len.

count – The maximum number of occurrences to find. bytealigned – If True the bitstring will only be found on

byte boundaries.

Raises ValueError if bs is empty, if start < 0, if end > self.len or if end < start.

Note that all occurrences of bs are found, even if they overlap.

float

The bitstring as a floating point number. Read only.

floatbe

The bitstring as a big-endian floating point number. Read only.

floatle

The bitstring as a little-endian floating point number. Read only.

floatne

The bitstring as a native-endian floating point number. Read only.

hex

The bitstring as a hexadecimal string. Read only.

int

The bitstring as a two’s complement signed int. Read only.

intbe

The bitstring as a two’s complement big-endian signed int. Read only.

intle

The bitstring as a two’s complement little-endian signed int. Read only.

intne

The bitstring as a two’s complement native-endian signed int. Read only.

join(sequence)[source]

Return concatenation of bitstrings joined by self.

sequence – A sequence of bitstrings.

len

The length of the bitstring in bits. Read only.

length

The length of the bitstring in bits. Read only.

oct

The bitstring as an octal string. Read only.

rfind(bs, start=None, end=None, bytealigned=None)[source]

Find final occurrence of substring bs.

Returns a single item tuple with the bit position if found, or an empty tuple if not found. The bit position (pos property) will also be set to the start of the substring if it is found.

bs – The bitstring to find. start – The bit position to end the reverse search. Defaults to 0. end – The bit position one past the first bit to reverse search.

Defaults to self.len.
bytealigned – If True the bitstring will only be found on byte
boundaries.

Raises ValueError if bs is empty, if start < 0, if end > self.len or if end < start.

se

The bitstring as a signed exponential-Golomb code. Read only.

sie

The bitstring as a signed interleaved exponential-Golomb code. Read only.

split(delimiter, start=None, end=None, count=None, bytealigned=None)[source]

Return bitstring generator by splittling using a delimiter.

The first item returned is the initial bitstring before the delimiter, which may be an empty bitstring.

delimiter – The bitstring used as the divider. start – The bit position to start the split. Defaults to 0. end – The bit position one past the last bit to use in the split.

Defaults to self.len.
count – If specified then at most count items are generated.
Default is to split as many times as possible.

bytealigned – If True splits will only occur on byte boundaries.

Raises ValueError if the delimiter is empty.

startswith(prefix, start=None, end=None)[source]

Return whether the current bitstring starts with prefix.

prefix – The bitstring to search for. start – The bit position to start from. Defaults to 0. end – The bit position to end at. Defaults to self.len.

tobytes()[source]

Return the bitstring as bytes, padding with zero bits if needed.

Up to seven zero bits will be added at the end to byte align.

tofile(f)[source]

Write the bitstring to a file object, padding with zero bits if needed.

Up to seven zero bits will be added at the end to byte align.

ue

The bitstring as an unsigned exponential-Golomb code. Read only.

uie

The bitstring as an unsigned interleaved exponential-Golomb code. Read only.

uint

The bitstring as a two’s complement unsigned int. Read only.

uintbe

The bitstring as a two’s complement big-endian unsigned int. Read only.

uintle

The bitstring as a two’s complement little-endian unsigned int. Read only.

uintne

The bitstring as a two’s complement native-endian unsigned int. Read only.

unpack(fmt, **kwargs)[source]

Interpret the whole bitstring using fmt and return list.

fmt – A single string or a list of strings with comma separated tokens
describing how to interpret the bits in the bitstring. Items can also be integers, for reading new bitstring of the given length.
kwargs – A dictionary or keyword-value pairs - the keywords used in the
format string will be replaced with their given value.

Raises ValueError if the format is not understood. If not enough bits are available then all bits to the end of the bitstring will be used.

See the docstring for ‘read’ for token examples.

class dj_bitfields.bitstringfield.BitStringField(*args, **kwargs)[source]

Bases: django.db.models.fields.Field

A Postgres bit string.

class_lookups = {'and': <class 'dj_bitfields.bitstringfield.BitstringAND'>, 'disjoint': <class 'dj_bitfields.bitstringfield.BitstringContains'>, 'intersects': <class 'dj_bitfields.bitstringfield.BitstringContains'>, 'or': <class 'dj_bitfields.bitstringfield.BitstringOR'>, 'subset': <class 'dj_bitfields.bitstringfield.BitstringContains'>, 'superset': <class 'dj_bitfields.bitstringfield.BitstringContains'>, 'xor': <class 'dj_bitfields.bitstringfield.BitstringXOR'>}
db_type(connection)[source]

Return the database column data type for this field, for the provided connection.

get_default()[source]

Return the default value for this field.

get_prep_lookup(lookup_type, value)[source]
get_prep_value(value)[source]

Perform preliminary non-db specific value checks and conversions.

to_python(value)[source]

Convert the input value into the expected Python data type, raising django.core.exceptions.ValidationError if the data can’t be converted. Return the converted value. Subclasses should override this.

Module contents