milestonexprotectrestpython.xprutils

Module: xprutils.py

Revision History

Date Version Description
2023/07/07 1.0.0.0 Initial Version.

def static_init(cls):

Define the decorator used to call an initializer for a class with all static methods. This allows static variables to be initialized one time for the class.

def export(fn):

Define the decorator used to modify a module's "__all__" variable. This avoids us having to manually modify a module's "__all__" variable when adding new classes.

class Event:

C# like event processing in Python3.

View Sample Code

# Define the class that will be raising events:
class MyFileWatcher:
    def __init__(self):
        self.fileChanged = Event()      # define event

    def watchFiles(self):
        source_path = "foo"
        self.fileChanged(source_path)   # fire event

def log_file_change(source_path):       # event handler 1
    print "%r changed." % (source_path,)

def log_file_change2(source_path):      # event handler 2
    print "%r changed!" % (source_path,)

# Define the code that will be handling raised events.
watcher              = MyFileWatcher()
watcher.fileChanged += log_file_change2
watcher.fileChanged += log_file_change
watcher.fileChanged -= log_file_change2
watcher.watchFiles()

Event(*args)

Initializes a new instance of the class.

handlers
def fire(self, *args, **kargs):

Calls (i.e. "fires") all method handlers defined for this event.

def getHandlerCount(self):

Returns the number of method handlers defined for this event.

def handle(self, handler):

Adds a method handler for this event.

def unhandle(self, handler):

Removes the specified method handler for this event.

Arguments:
  • handler (object): The method handler to remove.

This method will not throw an exception.

def unhandle_all(self):

Removes all method handlers (if any) for this event.

This method will not throw an exception.

class DataTypeHelper:

Helper class used for processing different types of data.

@staticmethod
def BoolToStringYesNo(value: bool) -> str:

Converts a boolean value to a "Yes" (true) or "No" (false) string.

Arguments:
  • value (bool): Boolean value to convert.
Returns:

A "Yes" or "No" string value.

@staticmethod
def BoolFromString(value: str) -> bool:

Converts a string to a boolean value.

Arguments:
  • value (str): String value to convert.
Returns:

A bool value.

True is returned if the value argument contains "yes", "true", "t", or "1";
otherwise, False is returned.

@staticmethod
def DateTimeFromString( value: str, argumentName: str, raiseExceptionIfNull: bool = True) -> datetime.datetime:

Converts a string to a datetime value.

Arguments:
  • value (str): String value to convert.
  • argumentName (str): Argument Name the datetime string was loaded from - used in exception details if the string could not be converted successfully.
  • raiseExceptionIfNull (bool): If True, and Exception will be raised if the string could not be converted to a date or if the value is null; otherwise, False to not raise an exception.
Returns:

A datetime value.

True is returned if the value argument contains "yes", "true", "t", or "1";
otherwise, False is returned.

Supported examples for datetime string are:

  • "0001-01-01T00:00:00.0000000"
  • "2023-07-24T17:12:31.0210000Z"
  • "2023-07-24T17:12:31.0210Z"
  • "2023-07-24T17:12:31Z"