Source code for utils.datatype

# -*- coding: UTF-8 -*-
:author:   Brigitte Bigi
:summary:  Data types for SPPAS.

.. _This file is part of SPPAS:

     ___   __    __    __    ___
    /     |  \  |  \  |  \  /              the automatic
    \__   |__/  |__/  |___| \__             annotation and
       \  |     |     |   |    \             analysis
    ___/  |     |     |   | ___/              of speech

    Copyright (C) 2011-2021  Brigitte Bigi
    Laboratoire Parole et Langage, Aix-en-Provence, France

    Use of this software is governed by the GNU Public License, version 3.

    SPPAS is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SPPAS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SPPAS. If not, see <>.

    This banner notice must not be removed.



import time

from .utilsexc import UtilsDataTypeError

# ---------------------------------------------------------------------------

[docs]class bidict(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' """
[docs] def __init__(self, *args, **kwargs): super(bidict, self).__init__(*args, **kwargs) # add all value=key in the dictionary inverse = dict() for key in self: inverse[self[key]] = key self.update(inverse)
def __setitem__(self, key, value): if key in self: del self[key] super(bidict, self).__setitem__(key, value) super(bidict, self).__setitem__(value, key) def __delitem__(self, key): value = self[key] super(bidict, self).__delitem__(key) super(bidict, self).__delitem__(value)
# ---------------------------------------------------------------------------
[docs]class sppasTime(object): """Utility class to represent date time with a string. How SPPAS works with the date... """
[docs] def __init__(self, now=None): """Create a sppasTime() instance. Given time must be formatted exactly like: '%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}' :param now: (str) String representing the current time :Example: >>> p = sppasTime('2018-04-09T15:00:37+02:00') >>> >>> '2018-04-09T15:00:37+02:00' >>> p.gmt >>> '+02:00' """ # Fix now if now is None: ctz = -time.altzone if time.localtime(time.time()).tm_isdst and \ time.daylight else -time.timezone = time.strftime('%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}' '').format(ctz // 3600, ctz % 3600) else: = now # Fix other members from now if 'T' not in or \ '-' not in or \ ":" not in raise UtilsDataTypeError("sppasTime(now)", "%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}", now) try: self.year ='T')[0].split('-')[0] self.month ='T')[0].split('-')[1] ='T')[0].split('-')[2] self.hours ='T')[1].split(':')[0] self.min ='T')[1].split(':')[1] self.sec =[-8:-6] self.gmt =[-6:] except IndexError: raise UtilsDataTypeError("sppasTime(now)", "%Y-%m-%dT%H:%M:%S{:0=+3d}:{:0=2d}", now)
# ---------------------------------------------------------------------------
[docs]class sppasType(object): """Utility class to check data type. """
[docs] @staticmethod def is_bool(entry): """Check if the entry is boolean. :param entry: (any type) :returns: (bool) """ return str(entry) in ['True', 'False']
# -----------------------------------------------------------------------
[docs] @staticmethod def is_number(entry): """Check if the entry is numeric. :param entry: (any type) :returns: (bool) """ try: float(entry) return True except (TypeError, ValueError): pass try: import unicodedata unicodedata.numeric(entry) return True except (TypeError, ValueError): pass return False
# -----------------------------------------------------------------------
[docs] @staticmethod def is_dict(entry): """Check if the entry is of any type of dictionary. :param entry: (any type) :returns: (bool) """ if type(entry) is dict: return True if "collections." in str(type(entry)): return True return False