Monday, June 1, 2020

ds_utils documentation

Python: module ds_utils
 
 
ds_utils
index
/home/david/Dropbox/PythonCode/ds_utils.py

ds_utils.py
 
A library of miscellaneous utils that I find I use often. 
 
Version 1.0
 
dsinfield6@gmail.com/ Telegram or signal:omotforest 
 
Released under GPL
 
Use at your own risk. Subject to update or change at any time.

 
Modules
       
getpass
os
platform

 
Classes
       
ANSI
Searches
config

 
class ANSI
    GetANSI(text,foreback,colour,style)
text - text to print
forecolour - [black|red|green|yellow|blue|magenta|cyan|white]
backcolour - [black|red|green|yellow|blue|magenta|cyan|white]
style - [normal|bold|italic|underline|inverse|strikethough]
 
Returns a string with ansi precursors to set foreground and background
colours and style. Set to normal after printing.
 
Most terminals support 8 colours. Some will not understand bold.
 
Out of range parameters throw a dictionary error
 
Colours supported:
    black
    red
    green
    yellow
    blue
    magenta
    cyan
    white
Styles supported:
    normal
    bold
    italic
    underline
    inverse
    strikethrough
 
Example: set foreground to red
    print GetANSI('this is red text','red','none','none')
Example: set foreground bold red, background cyan
    print GetANSI('set properties','red','cyan','bold')
 
  Methods defined here:
GetANSI(self, text, forecolour='', backcolour='', style='')
Constructs an ansi string to create multicoloured terminal display
Parameters
    text - the string to be output
    forecolour - the colour to set the foreground. Optional. Default=normal
    backcolour - the colour to set the background. Optional. Default=normal
    style - one of the styles in dictStyle
Returns 
    A string decorated with ansi codes
__init__(self)

Data and other attributes defined here:
dictColour = {'black': 0, 'blue': 4, 'cyan': 6, 'green': 2, 'magenta': 5, 'red': 1, 'white': 7, 'yellow': 3}
dictForeBack = {'back': 4, 'fore': 3}
dictStyle = {'bold': 1, 'inverse': 7, 'italic': 3, 'normal': 0, 'strikethrough': 9, 'underline': 4}
post = 'm'
pre = '\x1b['
tonormal = '\x1b[0m'

 
class Searches
    A class to contain search functions.
 
Initialisation
__init__(head, tail,source,start=0,ignorcase=false,inclusivefind=false)
    head - string to search from
    tail - string to search to
    start - integer position to start search. Optional. Default=0
    ignorecase - set to true to make case insensitive. Optional. Default=false
    inclusivefind - return the string from the start of head the end of tail. Optional.
    overlapsearch - if True sets newstart to start+1 
        default is to return the string from the end of head to the beginning of tail
 NB init calls get head to tail once so results of the first search are available after initialisation
Reading results
    newstart - set to the end of the head
    result - set tp the string found or "" empty if not found
    resultstatus - text description of result of search
    resultsuccess - set to True if head and tail were found.
    EOF - set to true if the search extends past end of file
    listAll[] - set only if getAll has been called
 
  Methods defined here:
__init__(self, head, tail, source, start=0, ignorecase=False, inclusivefind=False, overlapsearch=False)
getAll(self, head, tail)
getHeadtoTail(self, head, tail, start=False)

Data and other attributes defined here:
EOF = False
head = ''
ignorecase = False
inclusivefind = False
listAll = []
newstart = 0
overlapsearch = False
result = ''
resultstatus = ''
resultsuccess = False
source = ''
start = 0
tail = ''

 
class config
    Config File Helper Class
 
Config files are in the format:
    #start comments
    ParameterName1 value1
    ParameterName2 value2
    .
    .
    .
    #endcomments
Assumes values are one word when reading. Anything after a second space is ignored.
    
Constructor takes 1 arguments
    filename - the name of the file only the path will be derived depejding on the operating system
                 Linux - will be a hidden file the user's home directory
                 Windows will be a file in the same directory as the application
                 Other will cause an exception
 
  Methods defined here:
__init__(self, filename)
getConfigPath(self)
Accessor function for file path property
Call with object reference as parameter
readParameter(self, parametername)
Read the parameter from the file
    Returns False if not found
writeFile(self, dictParams, comments='', endcomments='')
dictParams - a dictionary of parameters e.g {name1 : value1, name2 : value2}
comments - a file header. Optional, default is empty string
endcomments - a footnote. Optional, default is an empty string.
           For neatness when doing a cat /n will be appended

Data and other attributes defined here:
config_file_name = ''
config_file_path = ''
platform_supported = False

 
Functions
       
InputUntil(prompt, acceptable, case_sensitive=True, tries=0)
Usage InputUntil(prompt,acceptable_input,[case sensitive=True],[tries=0])
Parameters:
    prompt - Prompt to be displayed (the acceptable terms will be listed)
    acceptable_input - String containing acceptable responses
    [case_sensitive] if false ignores case.; Default is True
    [tries] - Attempts before raisin an error.
Keeps gettin input until one of the terms in acceptable is found. Works best for single characters
See InputUntilList(prompt, acceptable, tries) for a word version
 
You should still check the returned input because in YNC, YN will match.
InputUntilList(prompt, acceptable, case_sensitive=True, tries=0)
Input until one of the items on the list is typed or the number of tries is reached.
Returns the actual answer typed (irrespective of the case sensitive flag.
Raises an error if tries exceeded catch it in your code to handle it.
 
For single letter (eg YN), the InputUntil in this module might be what you are looking for
 
Usage InputUntil(prompt,acceptable_input, [case_sensitive=True], [tries=0])
Parameters:
    prompt - Prompt to be displayed (the acceptable terms will be listed)
    acceptable_input - list containing acceptable responses
 
    case_sensitve - David DAVID, daVid all match david if this is false
    tries - Number of attempts until an error is raised. Set to <1 for infnite
getParameterValue(parametername, args, unitary=False)
Gets the command line parameter wih the name in parametername.
Assumes the parameters are in -name value pairs.
Longname parameters in the form --long_name value are supported but you must supply the --
Parameters with no value are not supported. (Coming soon: Optional argument to specify standalone which returns True/False)
Usage: getParameterValue(parametername,args)
Parameters:
    parametername - the name of the parameter if no - then one is prepended
    args - usually sys.argv. If you want to use another string you need a placeholder where the program name would be
           e.g ["params",  "-p", "param1","-p2"] param2 works as expected
    unitary - will not look for, a value just the presence or absence of a parameter
makeDirectory(dirPath)
Usage makeDirectory(directory_path)
 
Makes the directory in dirPath if it does not exist
 
Throws exception if it fails.

 
Data
        ARG_NOT_FOUND = False
version = '1.0'