Internal Auxiliary Functions¶
Auxiliary functions.
-
zlogging._aux.
readline
(file, separator=b'\t', maxsplit=-1, decode=False)¶ Wrapper for
file.readline()
function.- Parameters
file (_io.BufferedReader) – Log file object opened in binary mode.
separator (bytes) – Data separator.
maxsplit (int) – Maximum number of splits to do; see
bytes.split()
andstr.split()
for more information.decode (bool) – If decide the buffered string with
ascii
encoding.
- Returns
The splitted line as a
list
ofbytes
, or asstr
ifdecode
if set toTrue
.- Return type
List[AnyStr]
-
zlogging._aux.
decimal_toascii
(data, infinite=None)¶ Convert
decimal.Decimal
to ASCII.- Parameters
data (decimal.Decimal) – A
decimal.Decimal
object.infinite (str) – The ASCII representation of infinite numbers (
NaN
and infinity).
- Returns
The converted ASCII string.
- Return type
str
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)¶ Convert
float
to ASCII.- Parameters
data (float) – A
float
number.infinite (str) – The ASCII representation of infinite numbers (
NaN
and infinity).
- Returns
The converted ASCII string.
- Return type
str
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)¶ Conterprocess of
bytes.decode('unicode_escape')()
.- Parameters
string (bytes) – The bytestring to be escaped.
- Returns
The escaped bytestring as an encoded string
- Return type
str
Example
>>> b'\x09'.decode('unicode_escape') '\t' >>> unicode_escape(b'\t') '\x09'
-
zlogging._aux.
expand_typing
(cls, exc=None)¶ Expand typing annotations.
- Parameters
cls (
Model
orRecordType
object) – a variadic class which supports PEP 484 style attribute typing annotationsexc (Optional[ValueError]) – (
ValueError
, optional): exception to be used in case of inconsistent values forunset_field
,empty_field
andset_separator
- Returns
- The returned dictionary contains the
following directives:
record_fields
(OrderedDict
mappingstr
andRecordType
):a mapping proxy for fields of
record
data type, i.e. an instance ofRecordType
unset_fields
(bytes
): placeholder for unset fieldempty_fields
(bytes
): placeholder for empty fieldset_separator
(bytes
): separator forset
/vector
fields
- Return type
Dict[str, Any]
- 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