Zut

Reusable Python utilities.

Installation

Zut package is published on PyPI. Install with default, minimal dependencies:

pip install zut[default]

Other possible dependency specifications:

  • pip install zut : no dependency installed

  • pip install zut[excel] : dependencies to manage Excel files (including openpyxl)

  • pip install zut[smb] : dependencies to access files on Samba shares from Linux, or on Windows with non-standard security context (including smbprotocol) - Not required to access Samba shares from Windows using default credentials

  • pip install zut[sqlite] : dependencies to connect with a SQLite database (sqlparse)

  • pip install zut[postgresql] (or [pg]) : dependencies to connect with a PostgreSQL database (psycopg and sqlparse)

  • pip install zut[mariadb] (or [mysql]) : dependencies to connect with a MariaDB or MySQL database (mysqlclient and sqlparse)

  • pip install zut[sqlserver] : dependencies to connect with a Microsoft SQL Server database (including pyodbc and sqlparse)

Usage examples

See also full documentation (including API reference).

Configure logging

from zut import configure_logging
configure_logging()

Flexible output

Write tabular data to a flexible, easily configurable output: CSV or Excel file, or tabulated stdout/stderr.

The output file may be on the local file system or on a Windows/Samba share (including when the library is used on Linux).

Export tabular data to stdout or to a file:

import sys
from zut import tabular_dumper

with tabular_dumper(filename or sys.stdout, headers=["Id", "Word"]) as t:
    t.append([1, "Hello"])
    t.append([2, "World"])

Tabular data can also be exported using dictionnaries (in this case, headers will be detected automatically by the library):

import sys
from zut import tabular_dumper

with tabular_dumper(filename or sys.stdout) as t:
    t.append({'id': 1, 'name': "Hello"})
    t.append({'id': 2, 'col3': True})

If filename has extension with .xlsx, output will be in Excel 2010 format. Otherwise it will be in CSV format.

If filename starts with \\, output will be done on the corresponding Windows/Samba share. To indicate Samba credentials, call configure_smb_credentials before using function tabular_dumper. Example:

from zut import tabular_dumper, configure_smb_credentials

configure_smb_credentials(user=..., password=...)

with tabular_dumper(r"\\server\share\path\to\file") as o:
    ...