utils module

utils module.

General-purpose utilities.

Author - Christopher Teh Boon Sung


AFGen class

Table lookup class.

Linear inter- or extrapolation for a given set of tabulated (x,y) data. Stores (x,y) values and uses linear inter- or extrapolation, if needed, to return y based on a given x.

Sorts the data based on x values in an ascending order.

METHODS

  • val: Given x, return its corresponding y for a list of (x,y) pairs

Constructor __init__

AFGen(self, xydict)

Create and initialize the AFGen object.

Arguments

  • xydict (dict): dictionary holding the (x,y) pairs of values

val

AFGen.val(self, x)

Given x, return y, using linear extra- or interpolation if needed.

Arguments

  • x (int/float): the x in (x,y) pair of values

Returns

int/float: y value

Float class

Proxy class for floats (can also be used for integers).

Wraps a float number, so that whenever its value is used, a function is called first to returned a desired value This wrapper class allows some pre-computations (to be done by the stored function) to be carried out first before returning the desired value.

ATTRIBUTES

  • fn: Function to be called when the value of the float is used
  • args: Variable length arguments to be passed into fn, if any
  • kwargs: Dictionary to be passed into fn, if any

METHODS

  • real: Calls the stored function
  • __float__: Float conversion will call the stored functon

EXAMPLE

def power(base, exponent):
    return base ** exponent

def foo(val):
    print(val.real)     # prints whatever value that has been passed into foo

foo(8)                  # prints an integer: 8
foo(8.0)                # prints a float: 8.0
f = Float(power, 2, 3)  # will call power(2,3) when f is used
foo(f)                  # prints an integer: 8
foo(float(f))           # prints a float: 8.0
f.args = [3,2]          # change the base to 3 and exponent to 2
foo(f)                  # prints an integer: 9

Constructor __init__

Float(self, fn, *args, **kwargs)

Create and initialize the Float object.

Setup the function to call and its required arguments, if any.

Arguments

  • fn: function to call, with its paramaters, if any
  • args: positional parameters of fn function
  • kwargs: dictionary parameters of fn function

Note

fn function must return the intended object/value

__float__

Float.__float__(self)

Override __float__ conversion method.

The saved function fn() is called when float() conversion is applied to this class.

Returns

float: return value of fn()

real

Override real property.

The saved function fn() is called when the value of the float is used.

Returns

float: return value of fn()

set_common_path function

set_common_path(path='')

Return a wrapper function that prefix the same, common path to all file names.

A shortcut to avoid typing or specifying the same path to all files. This method will check if specified path exists.

Arguments

  • path (str): the common path to all files

Raises

  • IOError: if invalid or non-existant file path or file name (see Example)

Example

addpath = set_common_path('C:\\Users\\adminuser')
print(addpath('ini.txt')        # this will print: C:\Users\adminuser\ini.txt
print(addpath('ini.txt', True)  # set the second argument to True (default is False)
                                #   to check if file 'ini.txt' exist, else raises
                                #   IOError exception. If the second argument is set
                                #   to False, no check for the file name is done.
                                #   Set the second argument to False (or leave it
                                #   unspecified) if the file is an output file, but
                                #   for an input or initialization file that will
                                #   read, this file must already exist, so set the
                                #   second argument to True for all input files.

Whether the second argument is True or False, this function will check if the specified file path (excluding the file name) exist. If not, the exception IOError is raised.

This function will append the folder slash symbol if the path has no such slash as the end of the path, e.g., 'C:\\Users\\adminuser'