Internal Auxiliary Functions¶
I/O Utilities¶
- zlogging._aux.readline(file, separator=b'\\t', maxsplit=-1, decode=False)[source]¶
Wrapper for
file.readline()function.- Parameters:
file (
BufferedReader) – Log file object opened in binary mode.separator (
bytes) – Data separator.maxsplit (
int) – Maximum number of splits to do; seebytes.split()andstr.split()for more information.decode (
bool) – If decide the buffered string withasciiencoding.
- Return type:
- Returns:
The splitted line as a
listofbytes, or asstrifdecodeif set toTrue.
Value Conversion¶
- zlogging._aux.decimal_toascii(data, infinite=None)[source]¶
Convert
decimal.Decimalto ASCII.- Parameters:
data (
Decimal) – Adecimal.Decimalobject.infinite (
Optional[str]) – The ASCII representation of infinite numbers (NaNand infinity).
- Return type:
- Returns:
The converted ASCII string.
Example
When converting a
decimal.Decimalobject, for example:>>> d = decimal.Decimal('-123.123456789')
the function will preserve only 6 digits of its fractional part, i.e.:
>>> decimal_toascii(d) '-123.123456'
Note
Infinite numbers, i.e.
NaNand infinity (inf), will be converted as the value specified ininfinite, in default the string representation of the number itself, i.e.:NaN->'NaN'Infinity ->
'Infinity'
- zlogging._aux.float_toascii(data, infinite=None)[source]¶
Convert
floatto ASCII.- Parameters:
- Return type:
- Returns:
The converted ASCII string.
Example
When converting a
floatnumber, for example:>>> f = -123.123456789
the function will preserve only 6 digits of its fractional part, i.e.:
>>> float_toascii(f) '-123.123456'
Note
Infinite numbers, i.e.
NaNand infinity (inf), will be converted as the value specified ininfinite, in default the string representation of the number itself, i.e.:NaN->'nan'Infinity ->
'inf'
- zlogging._aux.unicode_escape(string)[source]¶
Conterprocess of
bytes.decode('unicode_escape').- Parameters:
string (
bytes) – The bytestring to be escaped.- Return type:
- Returns:
The escaped bytestring as an encoded string
Example
>>> b'\x09'.decode('unicode_escape') '\\t' >>> unicode_escape(b'\t') '\\x09'
Typing Inspection¶
- zlogging._aux.expand_typing(cls, exc=None)[source]¶
Expand typing annotations.
- Parameters:
cls (
Union[Model,Type[Model],_VariadicType,Type[_VariadicType]]) – a variadic class which supports PEP 484 style attribute typing annotationsexc (
Optional[Type[ValueError]]) – exception to be used in case of inconsistent values forunset_field,empty_fieldandset_separator
- Returns:
fields: a mapping proxy of field names and their corresponding data types, i.e. an instance of aBaseTypesubclassrecord_fields: a mapping proxy for fields ofrecorddata type, i.e. an instance ofRecordTypeunset_fields: placeholder for unset fieldempty_fields: placeholder for empty fieldset_separator: separator forset/vectorfields
- Return type:
The returned dictionary contains the following directives
- Warns:
BroDeprecationWarning – Use of
bro_*prefixed typing annotations.- Raises:
ValueError – In case of inconsistent values for
unset_field,empty_fieldandset_separator.
Example
Define a custom log data model from
Modelusing the prefines Bro/Zeek data types, or subclasses ofBaseType:class MyLog(Model): field_one = StringType() field_two = SetType(element_type=PortType)
Or you may use type annotations as PEP 484 introduced when declaring data models. All available type hints can be found in
zlogging.typing:class MyLog(Model): field_one: zeek_string field_two: zeek_set[zeek_port]
However, when mixing annotations and direct assignments, annotations will take proceedings, i.e. the function shall process first typing annotations then
clsattribute assignments. Should there be any conflicts, theexcwill be raised.Note
Fields of
zlogging.types.RecordTypetype will be expanded as plain fields of thecls, i.e. for the variadic class as below:class MyLog(Model): record = RecrodType(one=StringType(), two=VectorType(element_type=CountType()))
will have the following fields:
record.one->stringdata typerecord.two->vector[count]data type