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 withascii
encoding.
- Return type:
- Returns:
The splitted line as a
list
ofbytes
, or asstr
ifdecode
if set toTrue
.
Value Conversion¶
- zlogging._aux.decimal_toascii(data, infinite=None)[source]¶
Convert
decimal.Decimal
to ASCII.- Parameters:
data (
Decimal
) – Adecimal.Decimal
object.infinite (
Optional
[str
]) – The ASCII representation of infinite numbers (NaN
and infinity).
- Return type:
- Returns:
The converted ASCII string.
Example
When converting a
decimal.Decimal
object, 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.
NaN
and 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
float
to ASCII.- Parameters:
- Return type:
- Returns:
The converted ASCII string.
Example
When converting a
float
number, 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.
NaN
and 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_field
andset_separator
- Returns:
fields
: a mapping proxy of field names and their corresponding data types, i.e. an instance of aBaseType
subclassrecord_fields
: a mapping proxy for fields ofrecord
data type, i.e. an instance ofRecordType
unset_fields
: placeholder for unset fieldempty_fields
: placeholder for empty fieldset_separator
: separator forset
/vector
fields
- 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_field
andset_separator
.
Example
Define a custom log data model from
Model
using 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
cls
attribute assignments. Should there be any conflicts, theexc
will be raised.Note
Fields of
zlogging.types.RecordType
type 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
->string
data typerecord.two
->vector[count]
data type