Module huracan.utils

Huracan utilities

Expand source code
# SPDX-FileCopyrightText: © 2021 Antonio López Rivera <antonlopezr99@gmail.com>
# SPDX-License-Identifier: GPL-3.0-only

"""
Huracan utilities
-----------------
"""

import re
import sys
import inspect


def setattr_namespace(o, namespace):
    """
    Set all variables declared in a namespace as as attributes
    of a class instance.
    ---

    1. Obtain list of module names
    2. Get namespace variables
       - Discard all variables with names starting and ending with '_'
    3. Create a dictionary of namespace variable names and values
    4. Set namespace variables as attributes of the input object
       - Given class instance _o_ will not set as attribute of itself
       - The parent class of _o_ will not be set as an attribute of _o_
         if present in the provided namespace.

    :param o: Instance of any given class.
    :param namespace: A given namespace.
                      - locals()
                      - globals()

    :type o: object
    :type namespace: dict
    """
    # List of module names
    _mods_ = list(set(sys.modules) & set(namespace))
    # List of function arguments
    _args_ = setattr_namespace.__code__.co_varnames
    # Get namespace variables:
    #    List of local variables which are not special variables nor module names
    keys = [key for key in namespace.keys() if (key[0] != '_' and key[-1] != '_') and key not in _mods_]
    # Dictionary of namespace variable names and values
    vars = {key: namespace[key] for key in keys}
    for key, value in vars.items():
        if not type(value) == type(o)\
                and not isinstance(o, value if inspect.isclass(value) else type(value)):  # Avoid _o_, parent of _o_
            # Set namespace variables as attributes of the input object
            setattr(o, key, value)


class markers:
    """
    Markers class
    -------------
    """
    circle          = "o"
    x               = "x"
    thin_diamond    = "d"
    triangle_down   = "v"
    pentagon        = "p"
    vline           = "|"
    hline           = "_"
    # Decent
    point           = "."
    square          = "s"
    plus            = "+"
    triangle_up     = "^"
    triangle_left   = "<"
    triangle_right  = ">"
    tri_down        = "1"
    tri_up          = "2"
    tri_left        = "3"
    tri_right       = "4"
    octagon         = "8"
    hexagon1        = "h"
    hexagon2        = "H"
    diamond         = "D"
    tickleft        = 0
    tickright       = 1
    tickup          = 2
    tickdown        = 3
    caretleft       = 4
    caretright      = 5
    caretup         = 6
    caretdown       = 7
    caretleft_base  = 8
    caretright_base = 9
    caretup_base    = 10
    caretdown_base  = 11
    # Wonky tier
    plus_filled     = "P"
    star            = "*"
    # Garbage tier
    pixel           = ","

    # Icompatible
    incompatible    = [x]

    def __init__(self, hollow=False, plotter='plot'):
        self.hollow  = hollow
        self.plotter = plotter

        assert self.plotter in ['plot', 'scatter'], \
            "Plotter must be either 'plot' (for plt.plot, ax.scatter) or 'scatter' (plt.scatter)"

    def __getitem__(self, item):

        special = r'^__(.*?)\__$'

        m       = {k: markers.__dict__[k] for k in markers.__dict__.keys() if (self.hollow and k not in self.incompatible) or not self.hollow}
        keys    = [k for k in m.keys() if not re.match(special, k)]
        marker  = {'marker': m[keys[item]]}

        if self.hollow:
            if self.plotter == 'scatter':
                marker['mfc']        = 'none'
            else:
                marker['facecolors'] = 'none'

        return marker

Functions

def setattr_namespace(o, namespace)

Set all variables declared in a namespace as as attributes of a class instance.


  1. Obtain list of module names
  2. Get namespace variables
  3. Discard all variables with names starting and ending with '_'
  4. Create a dictionary of namespace variable names and values
  5. Set namespace variables as attributes of the input object
  6. Given class instance o will not set as attribute of itself
  7. The parent class of o will not be set as an attribute of o if present in the provided namespace.

:param o: Instance of any given class. :param namespace: A given namespace. - locals() - globals()

:type o: object :type namespace: dict

Expand source code
def setattr_namespace(o, namespace):
    """
    Set all variables declared in a namespace as as attributes
    of a class instance.
    ---

    1. Obtain list of module names
    2. Get namespace variables
       - Discard all variables with names starting and ending with '_'
    3. Create a dictionary of namespace variable names and values
    4. Set namespace variables as attributes of the input object
       - Given class instance _o_ will not set as attribute of itself
       - The parent class of _o_ will not be set as an attribute of _o_
         if present in the provided namespace.

    :param o: Instance of any given class.
    :param namespace: A given namespace.
                      - locals()
                      - globals()

    :type o: object
    :type namespace: dict
    """
    # List of module names
    _mods_ = list(set(sys.modules) & set(namespace))
    # List of function arguments
    _args_ = setattr_namespace.__code__.co_varnames
    # Get namespace variables:
    #    List of local variables which are not special variables nor module names
    keys = [key for key in namespace.keys() if (key[0] != '_' and key[-1] != '_') and key not in _mods_]
    # Dictionary of namespace variable names and values
    vars = {key: namespace[key] for key in keys}
    for key, value in vars.items():
        if not type(value) == type(o)\
                and not isinstance(o, value if inspect.isclass(value) else type(value)):  # Avoid _o_, parent of _o_
            # Set namespace variables as attributes of the input object
            setattr(o, key, value)

Classes

class markers (hollow=False, plotter='plot')

Markers class

Expand source code
class markers:
    """
    Markers class
    -------------
    """
    circle          = "o"
    x               = "x"
    thin_diamond    = "d"
    triangle_down   = "v"
    pentagon        = "p"
    vline           = "|"
    hline           = "_"
    # Decent
    point           = "."
    square          = "s"
    plus            = "+"
    triangle_up     = "^"
    triangle_left   = "<"
    triangle_right  = ">"
    tri_down        = "1"
    tri_up          = "2"
    tri_left        = "3"
    tri_right       = "4"
    octagon         = "8"
    hexagon1        = "h"
    hexagon2        = "H"
    diamond         = "D"
    tickleft        = 0
    tickright       = 1
    tickup          = 2
    tickdown        = 3
    caretleft       = 4
    caretright      = 5
    caretup         = 6
    caretdown       = 7
    caretleft_base  = 8
    caretright_base = 9
    caretup_base    = 10
    caretdown_base  = 11
    # Wonky tier
    plus_filled     = "P"
    star            = "*"
    # Garbage tier
    pixel           = ","

    # Icompatible
    incompatible    = [x]

    def __init__(self, hollow=False, plotter='plot'):
        self.hollow  = hollow
        self.plotter = plotter

        assert self.plotter in ['plot', 'scatter'], \
            "Plotter must be either 'plot' (for plt.plot, ax.scatter) or 'scatter' (plt.scatter)"

    def __getitem__(self, item):

        special = r'^__(.*?)\__$'

        m       = {k: markers.__dict__[k] for k in markers.__dict__.keys() if (self.hollow and k not in self.incompatible) or not self.hollow}
        keys    = [k for k in m.keys() if not re.match(special, k)]
        marker  = {'marker': m[keys[item]]}

        if self.hollow:
            if self.plotter == 'scatter':
                marker['mfc']        = 'none'
            else:
                marker['facecolors'] = 'none'

        return marker

Class variables

var caretdown
var caretdown_base
var caretleft
var caretleft_base
var caretright
var caretright_base
var caretup
var caretup_base
var circle
var diamond
var hexagon1
var hexagon2
var hline
var incompatible
var octagon
var pentagon
var pixel
var plus
var plus_filled
var point
var square
var star
var thin_diamond
var tickdown
var tickleft
var tickright
var tickup
var tri_down
var tri_left
var tri_right
var tri_up
var triangle_down
var triangle_left
var triangle_right
var triangle_up
var vline
var x