Tools

Few helpers, and they are free :)

Constants

thermalprinter.tools.ls(*constants: Any) None

Print constants values.

Parameters

*constants (list) – constant(s) to print.

Examples.

Print all constants:

>>> ls()

You can print only constants you want too:

>>> from thermalprinter.constants import Chinese, CodePage
>>> # Print Chinese values
>>> ls(Chinese)
>>> # Print Chinese and CodePage values
>>> ls(Chinese, CharSet)

Tests

thermalprinter.tools.printer_tests(printer: Optional[ThermalPrinter] = None, raise_on_error: bool = True) None

Send to the printer several insctructions to test every printing functions.

Parameters
  • printer (ThermalPrinter) – optional printer to use for printer_tests.

  • raise_on_error (bool) – raise on error.

Changed in version 0.2.0: Removed port and heat_time arguments. Added printer and raise_on_error arguments.

Example:

>>> from thermalprinter.tools import printer_tests
>>> printer_tests()
>>> printer = ThermalPrinter(port='/dev/ttyS0', heat_time=120)
>>> printer_tests(printer=printer)

Code pages

thermalprinter.tools.print_char(char: str, printer: Optional[ThermalPrinter] = None) None

Test one character with all possible code page.

Parameters
  • char (str) – character to print.

  • printer (ThermalPrinter) – optional printer to use for printer_tests.

Say you are looking for the good code page to print a sequence, you can print it using every code pages:

>>> print_char("现")

This function is as simple as:

>>> for codepage in list(CodePage):
...    printer.out(printer.out(f"{codepage.name}: {char}"))

Changed in version 0.2.0: Added printer argument.

Data validation

These are special functions to handle data validation. As it could take a lot of lines and logic, we prefered create them outsite the class to keep a light code for every ThermalPrinter’s methods.

thermalprinter.validate.validate_barcode(data: str, barcode_type: BarCode) None

Validate data against the bar code type.

Parameters
  • data (str) – data to print.

  • barecode_type (BarCode) – bar code type to use.

Raises
thermalprinter.validate.validate_barcode_position(position: BarCodePosition) None

Validate a bar code position.

Parameters

position (BarCodePosition) – the position to use.

Raises

ThermalPrinterConstantError – On bad position’s type.

thermalprinter.validate.validate_charset(charset: CharSet) None

Validate a charset.

Parameters

charset (CharSet) – new charset to use.

Raises

ThermalPrinterConstantError – On bad charset’s type.

thermalprinter.validate.validate_chinese_format(fmt: Chinese) None

Validate a Chinese format.

Parameters

fmt (Chinese) – new format to use.

Raises

ThermalPrinterConstantError – On bad fmt’s type.

thermalprinter.validate.validate_codepage(codepage: CodePage) None

Validate a code page.

Parameters

codepage (CodePage) – new code page to use.

Raises

ThermalPrinterConstantError – On bad codepage’s type.