pyreports modules

io

The io module contains all the classes and functions needed to interface with inputs and outputs.

Contains all input management.

class pyreports.io.Connection(*args, **kwargs)

Bases: abc.ABC

Connection base class

__init__(*args, **kwargs)

Connection base object.

__weakref__

list of weak references to the object (if defined)

class pyreports.io.CsvFile(filename)

Bases: pyreports.io.File

CSV file class

read(**kwargs)

Read csv format

Returns

Dataset object

write(data)

Write data on csv file

Parameters

data – data to write on csv file

Returns

None

class pyreports.io.DatabaseManager(connection: pyreports.io.Connection)

Bases: object

Database manager class for SQL connection

__init__(connection: pyreports.io.Connection)

Database manager object for SQL connection

Parameters

connection – Connection based object

__repr__()

Representation of DatabaseManager object

Returns

string

__weakref__

list of weak references to the object (if defined)

callproc(proc_name, params=None)

Calls the stored procedure named

Parameters
  • proc_name – name of store procedure

  • params – sequence of parameters must contain one entry for each argument that the procedure expects

Returns

Dataset object

commit()

This method sends a COMMIT statement to the server

Returns

None

execute(query, params=None)

Execute query on database cursor

Parameters
  • query – SQL query language

  • params – parameters of the query

Returns

None

executemany(query, params)

Execute query on database cursor with many parameters

Parameters
  • query – SQL query language

  • params – list of parameters of the query

Returns

None

fetchall()

Fetches all (or all remaining) rows of a query result set

Returns

Dataset object

fetchmany(size=1)

Fetches the next set of rows of a query result

Parameters

size – the number of rows returned

Returns

Dataset object

fetchone()

Retrieves the next row of a query result set

Returns

Dataset object

reconnect()

Close and start connection

Returns

None

class pyreports.io.ExcelFile(filename)

Bases: pyreports.io.File

Excel file class

read(**kwargs)

Read xlsx format

Returns

Dataset object

write(data)

Write data on xlsx file

Parameters

data – data to write on yaml file

Returns

None

class pyreports.io.File(filename)

Bases: abc.ABC

File base class

__init__(filename)

File base object

Parameters

filename – file path

__weakref__

list of weak references to the object (if defined)

abstract read(**kwargs)

Read with format

Returns

Dataset object

abstract write(data)

Write data on file

Parameters

data – data to write on file

Returns

None

class pyreports.io.FileManager(file: pyreports.io.File)

Bases: object

File manager class for various readable file format

__init__(file: pyreports.io.File)

File manager object for various readable file format

Parameters

file – file object

__repr__()

Representation of FileManager object

Returns

string

__weakref__

list of weak references to the object (if defined)

read(**kwargs)

Read file

Returns

Dataset object

write(data)

Write data on file

Parameters

data – data to write on file

Returns

None

class pyreports.io.JsonFile(filename)

Bases: pyreports.io.File

JSON file class

read(**kwargs)

Read json format

Returns

Dataset object

write(data)

Write data on json file

Parameters

data – data to write on json file

Returns

None

class pyreports.io.LdapManager(server, username, password, ssl=False, tls=True)

Bases: object

LDAP manager class

__init__(server, username, password, ssl=False, tls=True)

LDAP manager object

Parameters
  • server – fqdn server name or ip address

  • username – username for bind operation

  • password – password of the username used for bind operation

  • ssl – disable or enable SSL. Default is False.

  • tls – disable or enable TLS. Default is True.

__repr__()

Representation of LdapManager object

Returns

string

__weakref__

list of weak references to the object (if defined)

query(base_search, search_filter, attributes)

Search LDAP element on subtree base search directory

Parameters
  • base_search – distinguishedName of LDAP base search

  • search_filter – LDAP query language

  • attributes – list of returning LDAP attributes

Returns

Dataset object

rebind(username, password)

Re-bind with specified username and password

Parameters
  • username – username for bind operation

  • password – password of the username used for bind operation

Returns

None

unbind()

Unbind LDAP connection

Returns

None

class pyreports.io.MSSQLConnection(*args, **kwargs)

Bases: pyreports.io.Connection

Connection microsoft sql class

class pyreports.io.MySQLConnection(*args, **kwargs)

Bases: pyreports.io.Connection

Connection mysql class

class pyreports.io.PostgreSQLConnection(*args, **kwargs)

Bases: pyreports.io.Connection

Connection postgresql class

class pyreports.io.SQLliteConnection(*args, **kwargs)

Bases: pyreports.io.Connection

Connection sqlite class

class pyreports.io.TextFile(filename)

Bases: pyreports.io.File

Text file class

read(**kwargs)

Read with format

Returns

Dataset object

write(data)

Write data on file

Parameters

data – data to write on file

Returns

None

class pyreports.io.YamlFile(filename)

Bases: pyreports.io.File

YAML file class

read(**kwargs)

Read yaml format

Returns

Dataset object

write(data)

Write data on yaml file

Parameters

data – data to write on yaml file

Returns

None

pyreports.io.create_database_manager(dbtype, *args, **kwargs)

Creates a DatabaseManager object

Parameters

dbtype – type of database connection

Returns

DatabaseManager

pyreports.io.create_file_manager(filetype, filename)

Creates a FileManager object

Parameters
  • filetype – type of file

  • filename – path of file

Returns

FileManager

pyreports.io.create_ldap_manager(server, username, password, ssl=False, tls=True)

Creates a LdapManager object

Parameters
  • server – fqdn server name or ip address

  • username – username for bind operation

  • password – password of the username used for bind operation

  • ssl – disable or enable SSL. Default is False.

  • tls – disable or enable TLS. Default is True.

pyreports.io.manager(datatype, *args, **kwargs)

Creates manager object based on datatype

Parameters
  • datatype – type of manager

  • args – various positional arguments

  • kwargs – various keyword arguments

Returns

Manager object

core

The core module contains all the classes that refer to the creation and manipulation of data.

Contains all business logic and data processing.

class pyreports.core.Executor(data, header=None)

Bases: object

Executor receives, processes, transforms and writes data

__init__(data, header=None)

Create Executor object

Parameters
  • data – everything type of data

  • header – list header of data

__iter__()

Iterate over dataset

Returns

next value

__len__()

Count data

Returns

integer

__str__()

Pretty representation of Executor object

Returns

string

__weakref__

list of weak references to the object (if defined)

add_column(column, value)

Add column to data

Parameters
  • column – column name

  • value – list value for column, or function with no arguments that returns a value

Returns

None

clone()

Clone Executor object

Returns

executor

count_columns()

Count all column

Returns

integer

count_rows()

Count all rows

Returns

integer

del_column(column)

Delete column

Parameters

column – column name

Returns

None

filter(flist=None, key=None, column=None)

Filter data through a list of strings (equal operator) and/or function key

Parameters
  • flist – list of strings

  • key – function that takes a single argument and returns data

  • column – select column name or index number

Returns

None

get_data()

Get dataset

Returns

dataset

headers(header)

Set header

Parameters

header – header of data

Returns

None

map(key, column=None)

Apply function to data

Parameters
  • key – function that takes a single argument

  • column – select column name or index number

Returns

None

reset()

Reset data to original data

Returns

None

select_column(column)

Filter dataset by column

Parameters

column – name or index of column

Returns

Dataset object

class pyreports.core.Report(input_data: tablib.core.Dataset, title=None, filters=None, map_func=None, column=None, count=False, output: Optional[pyreports.io.FileManager] = None)

Bases: object

Report represents the workflow for generating a report

__init__(input_data: tablib.core.Dataset, title=None, filters=None, map_func=None, column=None, count=False, output: Optional[pyreports.io.FileManager] = None)

Create Report object

Parameters
  • input_data – Dataset object

  • title – title of Report object

  • filters – list or function for filter data

  • map_func – function for modifying data

  • column – select column name or index

  • count – count rows

  • output – FileManager object

__repr__()

Representation of Report object

Returns

string

__str__()

Pretty representation of Report object

Returns

string

__weakref__

list of weak references to the object (if defined)

exec()

Create Executor object to apply filters and map function to input data

Returns

None

export()

Process and save data on output

Returns

if count is True, return row count

send(server, _from, to, cc=None, bcc=None, subject=None, body='', auth=None, _ssl=True, headers=None)

Send saved report to email

Parameters
  • server – server SMTP

  • _from – email address ‘from:’

  • to – email address ‘to:’

  • cc – email address ‘cc:’

  • bcc – email address ‘bcc:’

  • subject – email subject. Default is report title

  • body – email body

  • auth – authorization tuple “(user, password)”

  • _ssl – boolean, if True port is 465 else 25

  • headers – more header value “(header_name, key, value)”

Returns

None

class pyreports.core.ReportBook(reports=None, title=None)

Bases: object

ReportBook represent a collection of Report’s object

__add__(other)

Add report object

Parameters

other – Report object

Returns

ReportBook

__init__(reports=None, title=None)

Create a ReportBook object

Parameters
  • reports – Report’s object list

  • title – title of report book

__iter__()

Return report iterator

Returns

iterable object

__len__()

Number of Report objects

Returns

int

__repr__()

Representation of ReportBook object

Returns

string

__str__()

Pretty representation of ReportBook object

Returns

string

__weakref__

list of weak references to the object (if defined)

add(report: pyreports.core.Report)

Add report object

Parameters

report – Report object

Returns

None

export(output=None)

Save data on report output or an Excel workbook

Parameters

output – output path for report export

Returns

None

remove(index: Optional[int] = None)

Remove Report object, last added or index specified

Parameters

index – report number to remove

Returns

None

send(server, _from, to, cc=None, bcc=None, subject=None, body='', auth=None, _ssl=True, headers=None)

Send saved report to email

Parameters
  • server – server SMTP

  • _from – email address ‘from:’

  • to – email address ‘to:’

  • cc – email address ‘cc:’

  • bcc – email address ‘bcc:’

  • subject – email subject. Default is report title

  • body – email body

  • auth – authorization tuple “(user, password)”

  • _ssl – boolean, if True port is 465 else 25

  • headers – more header value “(header_name, key, value)”

Returns

None

datatools

The datatools module contains all utility functions for data processing.

Contains all functions for data processing.

pyreports.datatools.aggregate(*columns, fill_empty: bool = False, fill_value=None)

Aggregate in a new Dataset the columns

Parameters
  • columns – columns added

  • fill_empty – fill the empty field of data with “fill_value” argument

  • fill_value – fill value for empty field if “fill_empty” argument is specified

Returns

Dataset

pyreports.datatools.average(data, column)

Average of list of integers or floats

Parameters
  • data – Dataset object

  • column – column name or index

Returns

float

pyreports.datatools.chunks(data, length)

Yield successive n-sized chunks from data

Parameters
  • data – Dataset object

  • length – n-sized chunks

Returns

generator

pyreports.datatools.counter(data, column)

Count all row value

Parameters
  • data – Dataset object

  • column – column name or index

Returns

Counter

pyreports.datatools.merge(*datasets)

Merge two or more dataset in only one

Parameters

datasets – Dataset object collection

Returns

Dataset

pyreports.datatools.most_common(data, column)

The most common element in a column

Parameters
  • data – Dataset object

  • column – column name or index

Returns

Any

pyreports.datatools.percentage(data, filter_)

Calculating the percentage according to filter

Parameters
  • data – Dataset object

  • filter – filter

Returns

float

exception

The exception module contains all the classes that represent explicit package exceptions.

Contains all custom exception.

exception pyreports.exception.ReportDataError

Bases: pyreports.exception.ReportException

exception pyreports.exception.ReportException

Bases: Exception

__weakref__

list of weak references to the object (if defined)

exception pyreports.exception.ReportManagerError

Bases: pyreports.exception.ReportException