utils package

Submodules

utils.compare module

filename

sppas.src.utils.compare.py

author

Brigitte Bigi

contact

develop@sppas.org

summary

Utilities to compare data.

class utils.compare.sppasCompare(verbose=False, case_sensitive=False)[source]

Bases: object

Utility class to compare data.

>>> sc = sppasCompare()
>>> d1 = {1:"one", 2:"two"}
>>> d2 = {2:"TWO", 1:"ONE"}
>>> sc.equals(d1, d2)
>>> True
>>> sc.equals_lists(d1.keys(), d2.keys())
>>> False
>>> sc.set_case_sensitive(True)
>>> sc.equals(d1, d2)
>>> False
__init__(verbose=False, case_sensitive=False)[source]

Create a sppasCompare instance and set options.

Parameters
  • verbose – (bool) Print comparison results on stdout

  • case_sensitive – (bool) Only to compare strings

static contains(list1, list2)[source]

Check if a list is contained in another one.

Parameters
  • list1 – (list)

  • list2 – (list)

Returns

(bool)

equals(data1, data2)[source]

Compare two data sets of any type.

:param data1 (any) The data to compare. :param data2 (any) The data to be compared with. :returns: (bool) whether the 2 data sets are equals or not

equals_dictionaries(dict1, dict2)[source]

Compare two dictionaries.

Parameters
  • dict1 – (dict or collection) The dict to compare.

  • dict2 – (dict or collection) The dict to be compared with.

Returns

(bool) whether the 2 dictionaries are equals or not

equals_items(item1, item2)[source]

Compare 2 items of type string or numeric.

Parameters
  • item1 – The string or numeric to compare

  • item2 – The string or numeric to be compared with

Returns

(bool) whether the 2 items are equals or not

equals_lists(list1, list2)[source]

Compare two lists.

:param list1 (list) The list to compare. :param list2 (list) The list to be compared with. :returns: (bool) whether the 2 lists are equals or not

equals_strings(item1, item2)[source]

Compare 2 data of type string or unicode.

Parameters
  • item1 – The string to compare

  • item2 – The string to be compared with

Returns

(bool) whether the 2 items are equals or not

set_case_sensitive(v)[source]

Compare strings with lower/upper case.

Parameters

v – (bool) Enable or not the case sensitive comparison of strings

set_verbose(v)[source]

Print comparison results on stdout or not.

Parameters

v – (bool) Enable or disable verbosity

utils.datatype module

filename

sppas.src.utils.datatype.py

author

Brigitte Bigi

contact

develop@sppas.org

summary

Data types for SPPAS.

class utils.datatype.bidict(*args, **kwargs)[source]

Bases: dict

A simple bidirectional dictionary.

Implements a 1:1 dictionary. It implies that two keys can’t have the same value.

>>> relation = bidict()
>>> relation['Alice'] = 'Bob'
>>> print(relation['Bob'])
>>> 'Alice'
>>> print(relation['Alice'])
>>> 'Bob'
__init__(*args, **kwargs)[source]
class utils.datatype.sppasTime(now=None)[source]

Bases: object

Utility class to represent date time with a string.

How SPPAS works with the date…

__init__(now=None)[source]

Create a sppasTime() instance.

Given time must be formatted exactly like: ‘%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}’

Parameters

now – (str) String representing the current time

Example

>>> p = sppasTime('2018-04-09T15:00:37+02:00')
>>> p.now
>>> '2018-04-09T15:00:37+02:00'
>>> p.gmt
>>> '+02:00'
class utils.datatype.sppasType[source]

Bases: object

Utility class to check data type.

static is_bool(entry)[source]

Check if the entry is boolean.

Parameters

entry – (any type)

Returns

(bool)

static is_dict(entry)[source]

Check if the entry is of any type of dictionary.

Parameters

entry – (any type)

Returns

(bool)

static is_number(entry)[source]

Check if the entry is numeric.

Parameters

entry – (any type)

Returns

(bool)

utils.fileutils module

filename

sppas.src.wkps.fileutils.py

author

Brigitte Bigi

contact

develop@sppas.org

summary

Utility classes to manage file names.

class utils.fileutils.sppasDirUtils(dirname)[source]

Bases: object

Utility directory manager for SPPAS.

>>> sd = sppasDirUtils("my-path")
>>> print(sd.get_files())
__init__(dirname)[source]

Create a sppasDirUtils instance.

Parameters

dirname – (str) Name of the current directory

static dir_entries(dir_name, extension=None, subdir=True)[source]

Return a list of file names found in directory ‘dir_name’.

If ‘subdir’ is True, recursively access subdirectories under ‘dir_name’. Additional argument, if any, is file extension to match filenames.

get_files(extension, recurs=True)[source]

Return the list of files of the directory.

Parameters
  • extension – (str) extension of files to filter the dir content

  • recurs – (bool) Find files recursively

Returns

a list of files

Raises

IOError

class utils.fileutils.sppasFileUtils(filename=None)[source]

Bases: object

Utility file manager for SPPAS.

>>> sf = sppasFileUtils("path/myfile.txt")
>>> print(sf.exists())
>>> sf = sppasFileUtils()
>>> sf.set_random()
>>> fn = sf.get_filename() + ".txt"
__init__(filename=None)[source]

Create a sppasFileUtils instance.

Parameters

filename – (str) Name of the current file

clear_whitespace()[source]

Set filename without whitespace.

Returns

new filename with spaces replaced by underscores.

exists(directory=None)[source]

Check if the file exists, or exists in a given directory.

Case-insensitive test on all platforms.

Parameters

directory – (str) Optional directory to test if a file exists.

Returns

the filename (including directory) or None

format()[source]

Set filename without whitespace and with only US-ASCII characters.

Returns

new filename with non-ASCII characters and spaces replaced by underscores.

get_filename()[source]

Return the current filename.

set_random(root='sppas_tmp', add_today=True, add_pid=True)[source]

Set randomly a basename, i.e. a filename without extension.

Parameters
  • root – (str) String to start the filename

  • add_today – (bool) Add today’s information to the filename

  • add_pid – (bool) Add the process PID to the filename

Returns

a random name of a non-existing file or directory

to_ascii()[source]

Set filename with only US-ASCII characters.

Returns

new filename with non-ASCII chars replaced by underscores.

utils.utilsexc module

filename

sppas.src.utils.utilsexc.py

author

Brigitte Bigi

contact

develop@sppas.org

summary

Exceptions for utils package.

exception utils.utilsexc.UtilsDataTypeError(data_name, expected_type, data_type)[source]

Bases: TypeError

:ERROR 7010:.

Expected a {data_name} of type {expected_type}. Got {data_type} instead.

__init__(data_name, expected_type, data_type)[source]

Module contents

filename

sppas.src.utils.__init__.py

author

Brigitte Bigi

contact

develop@sppas.org

summary

Utilities for SPPAS.

utils: utility classes.

This package includes any utility class to extend python features. Currently, it implements a class to manage identically unicode data with all versions of python. It also includes a comparator of data which is very powerful for lists and dictionaries, a bidirectional dictionary, a representation of time, etc.

Requires the following other packages:

  • config

class utils.bidict(*args, **kwargs)[source]

Bases: dict

A simple bidirectional dictionary.

Implements a 1:1 dictionary. It implies that two keys can’t have the same value.

>>> relation = bidict()
>>> relation['Alice'] = 'Bob'
>>> print(relation['Bob'])
>>> 'Alice'
>>> print(relation['Alice'])
>>> 'Bob'
__init__(*args, **kwargs)[source]
class utils.sppasCompare(verbose=False, case_sensitive=False)[source]

Bases: object

Utility class to compare data.

>>> sc = sppasCompare()
>>> d1 = {1:"one", 2:"two"}
>>> d2 = {2:"TWO", 1:"ONE"}
>>> sc.equals(d1, d2)
>>> True
>>> sc.equals_lists(d1.keys(), d2.keys())
>>> False
>>> sc.set_case_sensitive(True)
>>> sc.equals(d1, d2)
>>> False
__init__(verbose=False, case_sensitive=False)[source]

Create a sppasCompare instance and set options.

Parameters
  • verbose – (bool) Print comparison results on stdout

  • case_sensitive – (bool) Only to compare strings

static contains(list1, list2)[source]

Check if a list is contained in another one.

Parameters
  • list1 – (list)

  • list2 – (list)

Returns

(bool)

equals(data1, data2)[source]

Compare two data sets of any type.

:param data1 (any) The data to compare. :param data2 (any) The data to be compared with. :returns: (bool) whether the 2 data sets are equals or not

equals_dictionaries(dict1, dict2)[source]

Compare two dictionaries.

Parameters
  • dict1 – (dict or collection) The dict to compare.

  • dict2 – (dict or collection) The dict to be compared with.

Returns

(bool) whether the 2 dictionaries are equals or not

equals_items(item1, item2)[source]

Compare 2 items of type string or numeric.

Parameters
  • item1 – The string or numeric to compare

  • item2 – The string or numeric to be compared with

Returns

(bool) whether the 2 items are equals or not

equals_lists(list1, list2)[source]

Compare two lists.

:param list1 (list) The list to compare. :param list2 (list) The list to be compared with. :returns: (bool) whether the 2 lists are equals or not

equals_strings(item1, item2)[source]

Compare 2 data of type string or unicode.

Parameters
  • item1 – The string to compare

  • item2 – The string to be compared with

Returns

(bool) whether the 2 items are equals or not

set_case_sensitive(v)[source]

Compare strings with lower/upper case.

Parameters

v – (bool) Enable or not the case sensitive comparison of strings

set_verbose(v)[source]

Print comparison results on stdout or not.

Parameters

v – (bool) Enable or disable verbosity

class utils.sppasDirUtils(dirname)[source]

Bases: object

Utility directory manager for SPPAS.

>>> sd = sppasDirUtils("my-path")
>>> print(sd.get_files())
__init__(dirname)[source]

Create a sppasDirUtils instance.

Parameters

dirname – (str) Name of the current directory

static dir_entries(dir_name, extension=None, subdir=True)[source]

Return a list of file names found in directory ‘dir_name’.

If ‘subdir’ is True, recursively access subdirectories under ‘dir_name’. Additional argument, if any, is file extension to match filenames.

get_files(extension, recurs=True)[source]

Return the list of files of the directory.

Parameters
  • extension – (str) extension of files to filter the dir content

  • recurs – (bool) Find files recursively

Returns

a list of files

Raises

IOError

class utils.sppasFileUtils(filename=None)[source]

Bases: object

Utility file manager for SPPAS.

>>> sf = sppasFileUtils("path/myfile.txt")
>>> print(sf.exists())
>>> sf = sppasFileUtils()
>>> sf.set_random()
>>> fn = sf.get_filename() + ".txt"
__init__(filename=None)[source]

Create a sppasFileUtils instance.

Parameters

filename – (str) Name of the current file

clear_whitespace()[source]

Set filename without whitespace.

Returns

new filename with spaces replaced by underscores.

exists(directory=None)[source]

Check if the file exists, or exists in a given directory.

Case-insensitive test on all platforms.

Parameters

directory – (str) Optional directory to test if a file exists.

Returns

the filename (including directory) or None

format()[source]

Set filename without whitespace and with only US-ASCII characters.

Returns

new filename with non-ASCII characters and spaces replaced by underscores.

get_filename()[source]

Return the current filename.

set_random(root='sppas_tmp', add_today=True, add_pid=True)[source]

Set randomly a basename, i.e. a filename without extension.

Parameters
  • root – (str) String to start the filename

  • add_today – (bool) Add today’s information to the filename

  • add_pid – (bool) Add the process PID to the filename

Returns

a random name of a non-existing file or directory

to_ascii()[source]

Set filename with only US-ASCII characters.

Returns

new filename with non-ASCII chars replaced by underscores.

class utils.sppasTime(now=None)[source]

Bases: object

Utility class to represent date time with a string.

How SPPAS works with the date…

__init__(now=None)[source]

Create a sppasTime() instance.

Given time must be formatted exactly like: ‘%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}’

Parameters

now – (str) String representing the current time

Example

>>> p = sppasTime('2018-04-09T15:00:37+02:00')
>>> p.now
>>> '2018-04-09T15:00:37+02:00'
>>> p.gmt
>>> '+02:00'
class utils.sppasType[source]

Bases: object

Utility class to check data type.

static is_bool(entry)[source]

Check if the entry is boolean.

Parameters

entry – (any type)

Returns

(bool)

static is_dict(entry)[source]

Check if the entry is of any type of dictionary.

Parameters

entry – (any type)

Returns

(bool)

static is_number(entry)[source]

Check if the entry is numeric.

Parameters

entry – (any type)

Returns

(bool)