Source code for nosqlapi.common.odm
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# vim: se ts=4 et syn=python:
# created by: matteo.guadrini
# odm -- nosqlapi
#
# Copyright (C) 2022 Matteo Guadrini <matteo.guadrini@hotmail.it>
#
# This program 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Module that contains some ODM common objects."""
# region Imports
import string
from datetime import date
from datetime import datetime
from datetime import time
from datetime import timedelta
from decimal import Decimal as Dc
from uuid import uuid1
# endregion
# region global variable
__all__ = ['Null', 'List', 'Map', 'Int', 'Inet', 'Ascii', 'Time', 'SmallInt', 'Decimal', 'Timestamp', 'Counter',
'Date', 'Text', 'Blob', 'Boolean', 'Double', 'Uuid', 'Duration', 'Float', 'Varint', 'Varchar', 'Array']
# endregion
# region Classes
[docs]class Null:
"""Represents None"""
[docs] def __repr__(self):
return 'null'
[docs]class List(list):
"""Represents list of objects"""
pass
[docs]class Map(dict):
"""Represents dict of objects"""
pass
[docs]class Ascii(str):
"""Represents ASCII string"""
[docs] def __init__(self, value=''):
"""ASCII string
:param value: String printable characters
"""
for char in value:
if char not in string.printable:
raise ValueError(f'The string "{value}" contains non-ASCII characters: {char}')
[docs]class Blob(bytes):
"""Represents bytes"""
pass
[docs]class Boolean:
"""Represents bool"""
[docs] def __init__(self, value):
"""Boolean object
:param value: True of False
"""
self.value = bool(value)
[docs] def __repr__(self):
return self.value.__repr__()
def __bool__(self):
return self.value
[docs]class Counter:
"""Represents integer counter"""
[docs] def __init__(self, value=0):
"""Counter object
:param value: Integer (default 0)
"""
self.value = int(value)
[docs] def increment(self, value=1):
"""Increment number
:param value: Number (default 1)
:return: None
"""
self.value += value
[docs] def decrement(self, value=1):
"""Decrement number
:param value: Number (default 1)
:return: None
"""
self.value -= value
def __add__(self, other):
self.increment(other)
def __sub__(self, other):
self.decrement(other)
[docs] def __repr__(self):
return self.value.__repr__()
[docs]class Date(date):
"""Represents date in format %Y-%m-%d"""
[docs] def __repr__(self):
return self.strftime('%Y-%m-%d')
[docs]class Decimal(Dc):
"""Represents decimal number"""
pass
[docs]class Double(float):
"""Represents float"""
pass
[docs]class Duration(timedelta):
"""Represents duration ISO 8601 format: P[n]Y[n]M[n]DT[n]H[n]M[n]S"""
[docs] def __repr__(self):
return self.string_format()
[docs]class Float(float):
"""Represents float"""
pass
[docs]class Inet:
"""Represents ip address version 4 or 6 like string"""
[docs] def __init__(self, ip):
"""Network ip address object
:param ip: String ip value
"""
self.ip = ip
[docs] def __repr__(self):
return self.ip
[docs]class Int(int):
"""Represents integer"""
[docs] def __init__(self, number):
"""Integer object
:param number: Integer
"""
self.number = number
[docs] def __repr__(self):
return str(self.number)
[docs]class SmallInt(Int):
"""Represents small integer: -32767 to 32767"""
[docs] def __init__(self, number):
"""Integer number from -32767 to 32767
:param number: Integer
"""
if number > 32767 or number < -32767:
raise ValueError('the number must be between 32767 and -32767')
super().__init__(number)
[docs]class Text(str):
"""Represents str"""
pass
[docs]class Time(time):
"""Represents time"""
[docs] def __repr__(self):
return self.strftime('%H:%M:%S')
[docs]class Timestamp(datetime):
"""Represents datetime timestamp"""
[docs] def __repr__(self):
return self.timestamp().__repr__()
[docs]class Uuid:
"""Represents uuid version 1"""
[docs] def __init__(self):
"""Uuid1 object"""
self.uuid = uuid1()
[docs] def __repr__(self):
return self.uuid.__str__()
Varchar = Text
Varint = Int
Array = List
# endregion