This page contains the Library Package documentation.
Bases: unittest.TestCase
This class tests the graph algorithms implemented in the class graph
Bases: exceptions.Exception
This exceptipon is called if the hash function requires data, which was not set (for example if PHF is generated without setting set of keys
This is a base class for implementation of the hashing function. It defines the basic interface that every hash function in the pattern_match module has to implement
Bases: unittest.TestCase
Test basic functions of the hash function implemented in netbench library
Prints out a table, padded to make it pretty.
call pprint_table with an output (e.g. sys.stdout, cStringIO, file) and table as a list of lists. Make sure table is “rectangular” – each row has the same number of columns.
MIT License
Format a number according to given places. Adds commas, etc.
Will truncate floats into ints!
Prints out a table of data, padded for alignment
@param out: Output stream (“file-like object”) @param table: The table to print. A list of lists. Each row must have the same number of columns.
Bases: netbench.pattern_match.bin.library.b_hash.b_hash
Class for perfect hash function generated by the BDZ algorithm. This algorithms uses uniform random hypergraph.
Bases: netbench.pattern_match.bin.library.b_hash.b_hash
This class computes jenkins hash function. The key of the function has to be implemented as a string
Bases: netbench.pattern_match.bin.library.jenkins.jenkins_wrapper
Return the size of output hash value.
Returns: | The size in bits of the hash value. |
---|---|
Return type: | int |
Compute the hash value of input data and truncate the result to the size specified by output_size.
Parameter: | key (tuple(int, int)) – input data to be hashed divided into two parts. |
---|---|
Returns: | Hash value. |
Return type: | int |
Set the size of output hash value.
Parameter: | output_size (int) – The size in bits of the hash value. |
---|
Bases: netbench.pattern_match.bin.library.jenkins.jenkins
This class defines faster variant of the jenkins hash function. The idea is that tthe key is divided into two parts by the user of the function. First part is hashed by classical jenkins hash function while the other part is just xored to the output of the jenkins hash.
Key should be two tuple, where the first item is the key for jenkins hash while the second part is only xored to the first part.
Parameter: | key (tuple(int, int)) – input data to be hashed divided into two parts. |
---|---|
Returns: | Hash value. |
Return type: | int |
Bases: netbench.pattern_match.bin.library.jenkins.jenkins
This class enables usage of the base jenkins class with input key divided into two parts like in class jenkins_fast.
Concatenate the two parts of input key and return the result of jenkins hash function. Warning: the concatenation is per byte, the results is different then concatenation of BitArrays.
Parameter: | key (tuple(int, int)) – input data to be hashed divided into two parts. |
---|---|
Returns: | Hash value. |
Return type: | int |
Bases: exceptions.Exception
This exception is generated if the order of edge does is not the same as an order of the graph.
Bases: exceptions.Exception
This exception indicates that the edge is trying to access nonexistent vertex
This class represents the edge of the graph.
This is support class for the PHF functions and others algorithms working on graphs and hypergraphs. It contains only the graph algorithms used in the netbench library. It should behave as a common interface for connecting the netbench pattern matching module to any graph library simply by rewriting these methods.
This class is used to represent vertices of the graph
Bases: netbench.pattern_match.bin.library.b_hash.b_hash
Fast hash in hardware. This hash uses only XOR and AND operation
This package defines classes that simplify bit-wise creation, manipulation and interpretation of data.
Classes:
Bits – An immutable container for binary data. BitArray – A mutable container for binary data. ConstBitStream – An immutable container with streaming methods. BitStream – A mutable container with streaming methods.
Bits (base class)/ + mutating methods / + streaming methods
/ BitArray ConstBitStream /
- /
- /
BitStream
Functions:
pack – Create a BitStream from a format string.
Exceptions:
Error – Module exception base class. CreationError – Error during creation. InterpretError – Inappropriate interpretation of binary data. ByteAlignError – Whole byte position or length needed. ReadError – Reading or peeking past the end of a bitstring.
http://python-bitstring.googlecode.com
Bases: netbench.pattern_match.bin.library.bitstring.Bits
A container or stream holding an immutable sequence of bits.
For a mutable container use the BitStream class instead.
Methods inherited from Bits:
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.
Other methods:
bytealign() – Align to next byte boundary. peek() – Peek at and interpret next bits as a single item. peeklist() – Peek at and interpret next bits as a list of items. read() – Read and interpret next bits as a single item. readlist() – Read and interpret next bits as a list of items.
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. bytepos – The current byte position in the bitstring. 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. pos – The current bit position in the bitstring. 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.
Align to next byte and return number of skipped bits.
Raises ValueError if the end of the bitstring is reached before aligning to the next byte.
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.
Raises ValueError if bs is empty, if start < 0, if end > self.len or if end < start.
>>> BitArray('0xc3e').find('0b1111')
(6,)
Interpret next bits according to format string and return result.
fmt – Token string describing how to interpret the next bits.
The position in the bitstring is not changed. If not enough bits are available then all bits to the end of the bitstring will be used.
Raises ReadError if not enough bits are available. Raises ValueError if the format is not understood.
See the docstring for ‘read’ for token examples.
Interpret next bits according to format string(s) and return list.
The position in the bitstring is not changed. If not enough bits are available then all bits to the end of the bitstring will be used.
Raises ReadError if not enough bits are available. Raises ValueError if the format is not understood.
See the docstring for ‘read’ for token examples.
Interpret next bits according to the format string and return result.
fmt – Token string describing how to interpret the next bits.
fmt may also be an integer, which will be treated like the ‘bits’ token.
The position in the bitstring is advanced to after the read items.
Raises ReadError if not enough bits are available. Raises ValueError if the format is not understood.
Interpret next bits according to format string(s) and return list.
The position in the bitstring is advanced to after the read items.
Raises ReadError is not enough bits are available. Raises ValueError if the format is not understood.
See the docstring for ‘read’ for token examples.
>>> h, b1, b2 = s.readlist('hex:20, bin:5, bin:3')
>>> i, bs1, bs2 = s.readlist(['uint:12', 10, 10])
Read up to and including next occurrence of bs and return result.
bs – The bitstring to find. An integer is not permitted. bytealigned – If True the bitstring will only be
found on byte boundaries.
Raises ValueError if bs is empty. Raises ReadError if bs is not found.
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.
Raises ValueError if bs is empty, if start < 0, if end > self.len or if end < start.
Bases: netbench.pattern_match.bin.library.bitstring.ConstBitStream, netbench.pattern_match.bin.library.bitstring.BitArray
A container or stream holding a mutable sequence of bits
Subclass of the ConstBitStream and BitArray classes. Inherits all of their methods.
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. append() – Append a bitstring. bytealign() – Align to next byte boundary. byteswap() – Change byte endianness in-place. 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. insert() – Insert a bitstring. invert() – Flip bit(s) between one and zero. join() – Join bitstrings together using current bitstring. overwrite() – Overwrite a section with a new bitstring. peek() – Peek at and interpret next bits as a single item. peeklist() – Peek at and interpret next bits as a list of items. prepend() – Prepend a bitstring. read() – Read and interpret next bits as a single item. readlist() – Read and interpret next bits as a list of items. replace() – Replace occurrences of one bitstring with another. reverse() – Reverse bits in-place. rfind() – Seek backwards to find a sub-bitstring. rol() – Rotate bits to the left. ror() – Rotate bits to the right. set() – Set bit(s) to 1 or 0. 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:
Mutating operators are available: [], <<=, >>=, +=, *=, &=, |= and ^= in addition to [], ==, !=, +, *, ~, <<, >>, &, | and ^.
Properties:
bin – The bitstring as a binary string. bool – For single bit bitstrings, interpret as True or False. bytepos – The current byte position in the bitstring. 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. pos – The current bit position in the bitstring. 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.
Prepend a bitstring to the current bitstring.
bs – The bitstring to prepend.
Bases: netbench.pattern_match.bin.library.bitstring.Bits
A container holding a mutable sequence of bits.
Subclass of the immutable Bits class. Inherits all of its methods (except __hash__) and adds mutating methods.
Mutating methods:
append() – Append a bitstring. byteswap() – Change byte endianness in-place. insert() – Insert a bitstring. invert() – Flip bit(s) between one and zero. overwrite() – Overwrite a section with a new bitstring. prepend() – Prepend a bitstring. replace() – Replace occurrences of one bitstring with another. reverse() – Reverse bits in-place. rol() – Rotate bits to the left. ror() – Rotate bits to the right. set() – Set bit(s) to 1 or 0.
Methods inherited from Bits:
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:
Mutating operators are available: [], <<=, >>=, +=, *=, &=, |= and ^= in addition to the inherited [], ==, !=, +, *, ~, <<, >>, &, | and ^.
Properties:
bin – The bitstring as a binary string. bool – For single bit bitstrings, interpret as True or False. bytepos – The current byte position in the bitstring. 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. pos – The current bit position in the bitstring. 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.
Append a bitstring to the current bitstring.
bs – The bitstring to append.
Change the endianness in-place. Return number of repeats of fmt done.
start – Start bit position, defaults to 0. end – End bit position, defaults to self.len. repeat – If True (the default) the byte swapping pattern is repeated
as much as possible.
Insert bs at bit position pos.
bs – The bitstring to insert. pos – The bit position to insert at.
Raises ValueError if pos < 0 or pos > self.len.
Invert one or many bits from 0 to 1 or vice versa.
Raises IndexError if pos < -self.len or pos >= self.len.
Overwrite with bs at bit position pos.
bs – The bitstring to overwrite with. pos – The bit position to begin overwriting from.
Raises ValueError if pos < 0 or pos + bs.len > self.len
Prepend a bitstring to the current bitstring.
bs – The bitstring to prepend.
Replace all occurrences of old with new in place.
Returns number of replacements made.
old – The bitstring to replace. new – The replacement bitstring. start – Any occurrences that start before this will not be replaced.
Defaults to 0.
Raises ValueError if old is empty or if start or end are out of range.
Reverse bits in-place.
start – Position of first bit to reverse. Defaults to 0. end – One past the position of the last bit to reverse.
Defaults to self.len.
Using on an empty bitstring will have no effect.
Raises ValueError if start < 0, end > self.len or end < start.
Rotate bits to the left in-place.
bits – The number of bits to rotate by. start – Start of slice to rotate. Defaults to 0. end – End of slice to rotate. Defaults to self.len.
Raises ValueError if bits < 0.
Rotate bits to the right in-place.
bits – The number of bits to rotate by. start – Start of slice to rotate. Defaults to 0. end – End of slice to rotate. Defaults to self.len.
Raises ValueError if bits < 0.
Set one or many bits to 1 or 0.
value – If True bits are set to 1, otherwise they are set to 0. pos – Either a single bit position or an iterable of bit positions.
Negative numbers are treated in the same way as slice indices. Defaults to the entire bitstring.
Raises IndexError if pos < -self.len or pos >= self.len.
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.
Return True if one or many bits are all set to value.
Return True if any of one or many bits are set to value.
Return count of total number of either zero or one bits.
>>> Bits('0xef').count(1)
7
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.
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 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.
Raises ValueError if bs is empty, if start < 0, if end > self.len or if end < start.
>>> BitArray('0xc3e').find('0b1111')
(6,)
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.
Return concatenation of bitstrings joined by self.
sequence – A sequence of bitstrings.
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.
Raises ValueError if bs is empty, if start < 0, if end > self.len or if end < start.
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.
bytealigned – If True splits will only occur on byte boundaries.
Raises ValueError if the delimiter is empty.
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.
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.
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.
Interpret the whole bitstring using fmt and return list.
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.
Pack the values according to the format string and return a new BitStream.
values – Zero or more values to pack according to the format. kwargs – A dictionary or keyword-value pairs - the keywords used in the
format string will be replaced with their given value.
>>> s = pack('uint:12, bits', 100, '0xffe')
>>> t = pack(['bits', 'bin:3'], s, '111')
>>> u = pack('uint:8=a, uint:8=b, uint:55=a', a=6, b=44)
Bases: exceptions.Exception
Base class for errors in the bitstring module.
Bases: netbench.pattern_match.bin.library.bitstring.Error, exceptions.IndexError
Reading or peeking past the end of a bitstring.
Bases: netbench.pattern_match.bin.library.bitstring.Error, exceptions.ValueError
Inappropriate interpretation of binary data.
Bases: netbench.pattern_match.bin.library.bitstring.Error
Whole-byte position or length needed.
Bases: netbench.pattern_match.bin.library.bitstring.Error, exceptions.ValueError
Inappropriate argument during bitstring creation.