I/O

pyqvd.io

This module contains the classes and functions for reading and writing QVD files. QVD files can be read and written to/from any arbitrary stream-like target using the pyqvd.io.QvdFileReader and pyqvd.io.QvdFileWriter classes respectively.

QvdFileReader

class pyqvd.io.QvdFileReader(source: str | Path | BinaryIO, chunk_size: int = None)

Class for reading QVD files into memory. Parses the binary data of a QVD file and converts it into a QvdTable object.

Parameters:
  • source – The source to the QVD file. Either a file path (str or Path) or a BinaryIO object.

  • chunk_size – Optional chunk size if the data should be read in chunks. The chunk size is given as number of records.

__init__(source: str | Path | BinaryIO, chunk_size: int = None)

Constructs a new QVD file parser. The source can be either a file path (str or Path) or a BinaryIO object.

Parameters:

source – The source to the QVD file.

read() QvdTable | Iterator[QvdTable]

Reads the QVD file and returns its data as a QvdTable object.

Returns:

The data table of the QVD file or an iterator for reading the data in chunks.

QvdFileWriter

class pyqvd.io.QvdFileWriter(target: str | ~pathlib.Path | ~typing.BinaryIO, table: ~pyqvd.qvd.QvdTable, options: ~pyqvd.io.writer.QvdFileWriterOptions = QvdFileWriterOptions(table_name='UNKNOWN', time_formatter=<pyqvd.io.format.TimeValueFormatter object>, date_formatter=<pyqvd.io.format.DateValueFormatter object>, timestamp_formatter=<pyqvd.io.format.TimestampValueFormatter object>, interval_formatter=<pyqvd.io.format.IntervalValueFormatter object>, money_formatter=<pyqvd.io.format.MoneyValueFormatter object>))

Class allows to write a QvdTable as a QVD file to disk.

__init__(target: str | ~pathlib.Path | ~typing.BinaryIO, table: ~pyqvd.qvd.QvdTable, options: ~pyqvd.io.writer.QvdFileWriterOptions = QvdFileWriterOptions(table_name='UNKNOWN', time_formatter=<pyqvd.io.format.TimeValueFormatter object>, date_formatter=<pyqvd.io.format.DateValueFormatter object>, timestamp_formatter=<pyqvd.io.format.TimestampValueFormatter object>, interval_formatter=<pyqvd.io.format.IntervalValueFormatter object>, money_formatter=<pyqvd.io.format.MoneyValueFormatter object>))

Constructs a new QVD file writer. The target can be either a file path (str or Path) or a BinaryIO object.

Parameters:
  • target – The destination to which the Qvd file should be written.

  • table – The data to persist.

  • options – The options for the QVD file writer.

write()

Writes the QvdTable object as a QVD file to the target destination.

QvdFileWriterOptions

class pyqvd.io.QvdFileWriterOptions(table_name: str = 'UNKNOWN', time_formatter: ~pyqvd.io.format.TimeValueFormatter = <pyqvd.io.format.TimeValueFormatter object>, date_formatter: ~pyqvd.io.format.DateValueFormatter = <pyqvd.io.format.DateValueFormatter object>, timestamp_formatter: ~pyqvd.io.format.TimestampValueFormatter = <pyqvd.io.format.TimestampValueFormatter object>, interval_formatter: ~pyqvd.io.format.IntervalValueFormatter = <pyqvd.io.format.IntervalValueFormatter object>, money_formatter: ~pyqvd.io.format.MoneyValueFormatter = <pyqvd.io.format.MoneyValueFormatter object>)

Class for storing options for the QVD file writer.

__init__(table_name: str = 'UNKNOWN', time_formatter: ~pyqvd.io.format.TimeValueFormatter = <pyqvd.io.format.TimeValueFormatter object>, date_formatter: ~pyqvd.io.format.DateValueFormatter = <pyqvd.io.format.DateValueFormatter object>, timestamp_formatter: ~pyqvd.io.format.TimestampValueFormatter = <pyqvd.io.format.TimestampValueFormatter object>, interval_formatter: ~pyqvd.io.format.IntervalValueFormatter = <pyqvd.io.format.IntervalValueFormatter object>, money_formatter: ~pyqvd.io.format.MoneyValueFormatter = <pyqvd.io.format.MoneyValueFormatter object>) None

DateValueFormatter

class pyqvd.io.DateValueFormatter(qvd_format: str)

Class for formatting date values.

__init__(qvd_format: str)

Initialize the date value formatter.

Parameters:

qvd_format – The QVD format string.

Supported Tokens

Token

Description

YYYY

Year with century as a decimal number.

YY

Year without century as a decimal number.

MMMM

Month as a full name.

MMM

Month as an abbreviation.

MM

Month as a zero-padded decimal number.

DD

Day of the month as a zero-padded decimal number.

Examples

>>> formatter = DateValueFormatter("YYYY-MM-DD")
>>> formatter.format(DateValue.from_date(datetime(2021, 1, 1).date()))
"2021-01-01"
>>> formatter = DateValueFormatter("DD/MM/YYYY")
>>> formatter.format(DateValue.from_date(datetime(2021, 1, 1).date()))
"01/01/2021"
format(value: DateValue) str

Format the date value.

get_qvd_format_string() str

Get the QVD format string.

TimeValueFormatter

class pyqvd.io.TimeValueFormatter(qvd_format: str)

Class for formatting time values.

__init__(qvd_format: str)

Initialize the time value formatter.

Parameters:

qvd_format – The QVD format string.

Supported Tokens

Token

Description

hh

Hour (00-23) as a zero-padded decimal number.

HH

Hour (01-12) as a zero-padded decimal number.

mm

Minute as a zero-padded decimal number.

ss

Second as a zero-padded decimal number.

f - fffffff

Fractional seconds with up to six digits.

tt

AM/PM indicator.

Examples

>>> formatter = TimeValueFormatter("HH:mm:ss tt")
>>> formatter.format(TimeValue.from_time(datetime(2021, 1, 1, 14, 30, 0).time()))
"02:30:00 PM"
>>> formatter = TimeValueFormatter("hh:mm:ss")
>>> formatter.format(TimeValue.from_time(datetime(2021, 1, 1, 14, 30, 0).time()))
"14:30:00"
format(value: TimeValue) str

Format the time value.

get_qvd_format_string() str

Get the QVD format string.

TimestampValueFormatter

class pyqvd.io.TimestampValueFormatter(qvd_format: str)

Class for formatting timestamp values.

__init__(qvd_format: str)

Initialize the timestamp value formatter.

Parameters:

qvd_format – The QVD format string.

Supported Tokens

Token

Description

YYYY

Year with century as a decimal number.

YY

Year without century as a decimal number.

MMMM

Month as a full name.

MMM

Month as an abbreviation.

MM

Month as a zero-padded decimal number.

DD

Day of the month as a zero-padded decimal number.

hh

Hour (00-23) as a zero-padded decimal number.

HH

Hour (01-12) as a zero-padded decimal number.

mm

Minute as a zero-padded decimal number.

ss

Second as a zero-padded decimal number.

f - fffffff

Fractional seconds with up to six digits.

tt

AM/PM indicator.

Examples

>>> formatter = TimestampValueFormatter("YYYY-MM-DD hh:mm:ss tt")
>>> formatter.format(TimestampValue.from_timestamp(datetime(2021, 1, 1, 14, 30, 0)))
"2021-01-01 02:30:00 PM"
>>> formatter = TimestampValueFormatter("DD/MM/YYYY hh:mm:ss")
>>> formatter.format(TimestampValue.from_timestamp(datetime(2021, 1, 1, 14, 30, 0)))
"01/01/2021 14:30:00"
format(value: TimestampValue) str

Format the timestamp value.

get_qvd_format_string() str

Get the QVD format string.

IntervalValueFormatter

class pyqvd.io.IntervalValueFormatter(qvd_format: str)

Class for formatting interval values.

__init__(qvd_format: str)

Initialize the interval value formatter.

Parameters:

qvd_format – The QVD format string.

Supported Tokens

Token

Description

D

Number of days.

hh

Number of hours.

mm

Number of minutes.

ss

Number of seconds.

f - fffffff

Fractional seconds with up to six digits.

Examples

>>> formatter = IntervalValueFormatter("D hh:mm:ss")
>>> formatter.format(IntervalValue.from_interval(timedelta(days=1, hours=14, minutes=30, seconds=0)))
"1 14:30:00"
>>> formatter = IntervalValueFormatter("hh:mm:ss")
>>> formatter.format(IntervalValue.from_interval(timedelta(days=1, hours=14, minutes=30, seconds=0)))
"38:30:00"
format(value: IntervalValue) str

Format the interval value.

get_qvd_format_string() str

Get the QVD format string.

MoneyValueFormatter

class pyqvd.io.MoneyValueFormatter(thousand_separator: str = None, decimal_separator: str = '.', currency_symbol: str = None, currency_symbol_position: Literal['precede', 'follow'] = 'precede', currency_symbol_space_separated: bool = False, decimal_precision: int = 2)

Class for formatting money values.

__init__(thousand_separator: str = None, decimal_separator: str = '.', currency_symbol: str = None, currency_symbol_position: Literal['precede', 'follow'] = 'precede', currency_symbol_space_separated: bool = False, decimal_precision: int = 2)

Initialize the money value formatter.

Parameters:
  • thousand_separator – The thousand separator.

  • decimal_separator – The decimal separator.

  • currency_symbol – The currency symbol.

  • currency_symbol_position – The currency symbol position.

  • currency_symbol_space_separated – Whether the currency symbol is space separated.

  • decimal_precision – The decimal precision.

Examples

>>> formatter = MoneyValueFormatter()
>>> formatter.format(MoneyValue.from_serial_number(1000))
"1000.00"
>>> formatter = MoneyValueFormatter(thousand_separator=",", decimal_separator=",")
>>> formatter.format(MoneyValue.from_serial_number(1000))
"1,000,00"
>>> formatter = MoneyValueFormatter(currency_symbol="$", currency_symbol_position="follow")
>>> formatter.format(MoneyValue.from_serial_number(1000))
"1000.00$"
property currency_symbol: str

Get the currency symbol.

property currency_symbol_position: Literal['precede', 'follow']

Get the currency symbol position.

property currency_symbol_space_separated: bool

Get whether the currency symbol is space separated.

property decimal_precision: int

Get the decimal precision.

property decimal_separator: str

Get the decimal separator.

format(value: MoneyValue) str

Format the money value.

get_qvd_format_string() str

Get the QVD format string.

property thousand_separator: str

Get the thousand separator.