Module hosh.misc.helper

Just shortcuts

Expand source code
#  Copyright (c) 2021. Davi Pereira dos Santos
#  This file is part of the hosh project.
#  Please respect the license - more about this in the section (*) below.
#
#  hosh 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.
#
#  hosh 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 hosh.  If not, see <http://www.gnu.org/licenses/>.
#
#  (*) Removing authorship by any means, e.g. by distribution of derived
#  works or verbatim, obfuscated, compiled or rewritten versions of any
#  part of this work is illegal and is unethical regarding the effort and
#  time spent here.
"""Just shortcuts"""
from dataclasses import dataclass

from hosh.hosh_ import Hosh


@dataclass
class Helper:
    """Internal use only.

    Not to be directly instantiated."""

    version: str

    def __call__(self, blob, etype="ordered"):
        return Hosh(blob, etype=etype, version=self.version)

    def u(self, blob):
        return Hosh(blob, etype="unordered", version=self.version)

    def h(self, blob):
        return Hosh(blob, etype="hybrid", version=self.version)

    fromid = Hosh.fromid
    fromn = Hosh.fromn

Classes

class Helper (version: str)

Internal use only.

Not to be directly instantiated.

Expand source code
@dataclass
class Helper:
    """Internal use only.

    Not to be directly instantiated."""

    version: str

    def __call__(self, blob, etype="ordered"):
        return Hosh(blob, etype=etype, version=self.version)

    def u(self, blob):
        return Hosh(blob, etype="unordered", version=self.version)

    def h(self, blob):
        return Hosh(blob, etype="hybrid", version=self.version)

    fromid = Hosh.fromid
    fromn = Hosh.fromn

Class variables

var version : str

Static methods

def fromid(id)

Create an element from a textual id.

Usage:

>>> a = Hosh.fromid("abcdefabcdefabcdefabcdefabcdefab")
>>> a.n
1094566309952642687224764830259410933250743749332933330234
>>> a.cells
[748932665, 516513868, 468764361, 3316970622, 2727293743, 316029245]
>>> a.etype
'ordered'
>>> bid = a.id[:2] + "_" + a.id[3:]
>>> bid
'ab_defabcdefabcdefabcdefabcdefab'
>>> b = Hosh.fromid(bid)
>>> b.id
'ab_defabcdefabcdefabcdefabcdefab'
>>> b.n
59377482839139050825606534576063885287
>>> b.cells
[0, 0, 749449200, 1774140626, 3139018916, 292801225]
>>> b.etype
'hybrid'
>>> Hosh.fromid("0000000000000000000000000000000000000000000000000000000000000000") == 0
True

Parameters

id
 

Parameters

id
 

Returns

A new Hosh object
 
Expand source code
@classmethod
def fromid(cls, id):
    """
    Create an element from a textual id.

    Usage:

    >>> a = Hosh.fromid("abcdefabcdefabcdefabcdefabcdefab")
    >>> a.n
    1094566309952642687224764830259410933250743749332933330234
    >>> a.cells
    [748932665, 516513868, 468764361, 3316970622, 2727293743, 316029245]
    >>> a.etype
    'ordered'
    >>> bid = a.id[:2] + "_" + a.id[3:]
    >>> bid
    'ab_defabcdefabcdefabcdefabcdefab'
    >>> b = Hosh.fromid(bid)
    >>> b.id
    'ab_defabcdefabcdefabcdefabcdefab'
    >>> b.n
    59377482839139050825606534576063885287
    >>> b.cells
    [0, 0, 749449200, 1774140626, 3139018916, 292801225]
    >>> b.etype
    'hybrid'
    >>> Hosh.fromid("0000000000000000000000000000000000000000000000000000000000000000") == 0
    True

    Parameters
    ----------
    id

    Parameters
    ----------
    id

    Returns
    -------
    A new Hosh object
    """

    if len(id) not in groups:
        raise WrongIdentifier(f"Wrong identifier length: {len(id)}   id:[{id}]")
    return Hosh(cells_fromid(id, p=groups[len(id)].p), version=groups[len(id)])
def fromn(n: int, version=Group(p=1099511627689, p4=1461501636868331575725436266114840805196834679841, p6=1766847063939562670646036165286872353986524172769430561878277294118845361, digits=40, bytes=30, firstp='0_100000000_____________________________', lastp='f_8afffffff_____________________________', firstp4='00_1000000000000000000000000000000000000', lastp4='.._87c2a630003eec7dffff561b0000004aeffff', firstp6='1000000000000000000000000000000000000000', lastp6='g-8KOjCQREq2Vz8VTc30gLMd..vvX6000ov.....'))

Create a Hosh object representing the given int.

Default 'p' is according to version UT64.4.

Usage:

>>> h = Hosh.fromn(7647544756746324134134)
>>> h.id
'00_e49c1c505dcd0039e91000000000000000000'

Parameters

n
 
version
 

Returns

A new Hosh object
 
Expand source code
@classmethod
def fromn(cls, n: int, version=UT40_4):
    """
    Create a Hosh object representing the given int.

    Default 'p' is according to version UT64.4.

    Usage:

    >>> h = Hosh.fromn(7647544756746324134134)
    >>> h.id
    '00_e49c1c505dcd0039e91000000000000000000'

    Parameters
    ----------
    n
    version

    Returns
    -------
    A new Hosh object
    """
    p, order = version.p, version.p6
    if n > order:
        raise ElementTooHigh(f"Element outside allowed range: {n} >= {order}")
    return Hosh(int2cells(n, p), version=version)

Methods

def h(self, blob)
Expand source code
def h(self, blob):
    return Hosh(blob, etype="hybrid", version=self.version)
def u(self, blob)
Expand source code
def u(self, blob):
    return Hosh(blob, etype="unordered", version=self.version)