Helpers

This part of the documentation covers all the helpers of GMUtils.

Argument

class gmutils.helpers.helpme_argument.Arguments(**options)
__export__()

Export functionality for Config().export()

Returns:dict – dict to export
__init__(**options)

Arguments allows you to create a more powerful experience with Argument parsing and loading from configuration files.

Parameters:{dict} -- argument options (**kargvs) –
add_argument(dest, ..., name=value, ...)

add_argument(option_string, option_string, …, name=value, …)

defaults()

Default arguments that get added to Arguments()

error(message: string)

Prints a usage message incorporating the message to stderr and exits.

If you override this in a subclass, it should not return – it should either exit or raise an exception.

get(argument)

Gets the argument by name

Parameters:{str} -- argument name (argument) –
Raises:GMException – if the argument does not exist
Returns:object – arguments
load(**options)

The load function allows for

Parameters:{dict} -- the different options + arguments (**options) –
#!/usr/bin/env python3
from os.path import join
from time import sleep

from gmutils import Config
from gmutils.helpers.helpme_argument import Arguments

# Init project variables
__version__ = '0.1.0'
__name__ = 'MyExampleProject'

__description__ = 'My Example CLI'

__banner__ = """\
 _____                          _       ______
|  ___|                        | |      | ___ \\
| |____  ____ _ _ __ ___  _ __ | | ___  | |_/ / __ _ _ __  _ __   ___ _ __
|  __\ \/ / _` | '_ ` _ \| '_ \| |/ _ \ | ___ \/ _` | '_ \| '_ \ / _ \ '__|
| |___>  < (_| | | | | | | |_) | |  __/ | |_/ / (_| | | | | | | |  __/ |
\____/_/\_\__,_|_| |_| |_| .__/|_|\___| \____/ \__,_|_| |_|_| |_|\___|_|
                         | |
                         |_|\
"""

# Run the GMUtils simple init cli function
Config.initCLI()

# Many standard arguments will be added by the gmutils module such as
# `-c` or `--config` to set the locaitons of multiple configuration files
# `-vvv` or `--verbose` to set the application verbosity level
# `-v` or `--version` to display the softwares version and exit the program

# Adding arguments is the same as using the standard `argparse` module
Config.arguments.add_argument(
    '-m', '-my-argument', action='store_true'
)

Printing

class gmutils.helpers.helpme_printing.Printing
static banner()

Printing banner

static blank()

Enters a blank line in the std out

static colour(colour, text, out=0)

Printing in colour the the text provided

Parameters:
  • {str} -- colour by name (colour) –
  • {str} -- text (text) –
Keyword Arguments:
 

{int} -- std out/err (default (out) – {0})

static data(title, text)

Printing of data

Parameters:
  • {str} -- title of the data (title) –
  • {str} -- data to be printed (text) –
static error(code, text, exp=None)

Printing Errors

Parameters:
  • {str} -- error code message (code) –
  • {str} -- error message (text) –
Keyword Arguments:
 
  • {Excecption} -- exception thrown by an applciation (exp) –
  • (default – {None})
static format(type, code, text)

The formatting

Parameters:
  • {str} -- info/debug/error codes (type) –
  • {str} -- simple message/code (code) –
  • {str} -- text for formatting (text) –
static info(code, text)

Printing infomation

Parameters:
  • {str} -- simple message (code) –
  • {str} -- text (text) –

Thread

gmutils.helpers.helpme_thread.createThreads(name, thread_count=1, waitfor=0)

This is a wrapping / annotation to allow applications to wrapper functions that they want to thread quickly. This allows you to quickly spwan threads by wrapping functions

Parameters:

{str} -- name of the groups of threads (name) –

Keyword Arguments:
 
  • {int} -- the number of threads spawned (default (thread_count) – {1})
  • {int} -- default wait time between function executions (waitfor) –
  • (default – {0})
gmutils.helpers.helpme_thread.main()

The main function allows an applications main thread to do nothing except halt all threads on KeyboardInterrupt.

#!/usr/bin/env python

from time import sleep

from gmutils import Config
from gmutils.helpers.helpme_thread import createThreads

# Define a function that you want to thread and wrap the function with
# `createThreads()` and setting a name you want to give the function.

@createThreads('my-little-function')
def simple_function():
    sleep(.5)

# You can also create multiple threads in the same way as before
# This will spawn 3 threads at the same time 

@createThreads('my-many-functions', thread_count=3)
def simple_func2():
    sleep(.5)

# You can also add pauses between each thread function call

@createThreads('my-slow-function', waitfor=5)
def simple_func3():
    sleep(.5)

# You can also get access to the Thread objects that have been created
for thread in Config.THREADS:
    print(thread.name)

# Kill the function and threads using:
Config.haltThreads()

# Note: This will kill all the threads you have running