The main package and a background support of project PyCAMIA.
Project description
pycamia
Introduction
pycamia
is the base package affiliated to project PyCAMIA
. It is a collection of different useful tools necessary in python programming. pycamia
was designed for python v3.6+
. It is consist of the following sub-packages.
- environment is a package containing functions to inspect the context. e.g. get the local variables in the context that calls a function.
- strop is a collection of advanced functions for strings. e.g. tokenize a string by spliting outside brackets OR find all indices for matched sub-strings.
- listop is a collection of advanced functions for lists. e.g. flatten a nested list.
- manager is a package to manage file and package infos. e.g. easily check the dependencies OR easy update the version.
- functions is a package of special (and commonly trivial) functions. e.g. empty functions.
- exceptions is a package to handle exceptions. e.g. touch a function and suppress the error OR assert with comment OR quickly create an Error.
- inout is a package to extend the input/output. e.g. printing to a string OR suppressing the console output.
- timing is a package to time the executions. e.g. use
with
structure to record time spent for a set of commands. - more is a collection of uncategorized functions, one need to import them from
pycamia.more
.
Installation
This package can be installed by pip install pycamia
or moving the source code to the directory of python libraries (the source code can be downloaded on github or PyPI).
pip install pycamia
Package environment
This package fetches the surrounding environment of the call. It is likely that no more functions would be added to it. If there's any suggestion, please contact the developer.
- Use
v = get_environ_locals()
orv = get_environ_globals()
to get the dictionary of local or global variables in the parent environment. If the result is out of expectations, please contact the developer. - Use
v['name']
to read variablename
andv['name'] = value
to add variable to the environment.
Package strop
This package cope with str objects.
- Use
str_len
to find the ASCII length for a string, with a length2
for wide characters. - Use
str_slice
to slice a string by given indices. - Use
find_all
to obtain all the indices of a given string.str_slice(s, find_all(s, k))
is equivalent tos.split(k)
. - Use
sorted_dict_repr
to create a repr string for a dictionary with ordered key. - Use
enclosed_object
to find the first object enclosed by brackets. - Use
tokenize
to split a string without breaking enclosed objects. This is useful in breaking text of dictionary structures or arguments. e.g. one can usetokenize(args, sep=[',', '='])[::2]
to find the argument names ifargs
is a string in the formatkey1=value1, key2 = value2, ...
.
Package listop
This package cope with list objects. More useful functions will be added to it in the future.
- Use
argmin(list, domain)
to find the indices for the minimal value in list. The function only search in the indicesdomain
. A list is output as there may be multiple entries. - Use
argmax
to find the indices for the maximal value, similar toargmin
. - Use
flat_list
to unwrap the list elements to create a list with no element in typelist
. - Use
prod
to obtain the product of all numbers in the list. - Use
item
to fetch the only element in the list. An error will be raised if there isn't any or are more than 1 elements.
Package manager
This package manages the info of packages and files. One can use it to organize the project.
- Use
__info__ = info_manager(project="PyCAMIA", ...)
to list the properties at the front of files. This serve as a brief introduction to readers. - Use
info_manager
at the beginning of__init__.py
,pack.py
uses it to create the portrait of a package. - Use
__info__.check_requires()
to automatically check if the dependencies in attributerequires
exist or not. This is commonly used in__init__.py
. One can use__info__ = info_manager(...).check()
to perform an in-place check. - Use
with __info__:
before importing required dependencies as well to perform a double check.
Package functions
This package contains simple functions. It is the simplest package in the project so far.
- Use
empty_function
for a function that does nothing. One can put any argument to the function but nothing would happen. - Use
const_function(a)
for a function that accepts any argument but does nothing and always returna
.
Package exceptions
This package handles exceptions.
- Use
touch(function)
to try a function and suppress the error in the mean time. e.g.touch(lambda: 1/a)
returnsNone
to tell you that an exception occurs whena=0
, but returns1
whena=1
. - Use
crashed(function)
to check whether a function fails or not. - Use
avouch(bool_to_be_tested, assertion_text)
to avouch that the given expression is true and output your designedassertion_text
when the test fails. - Use
Error("name")
to creat a new error type. It is the same as creating an Error tag byclass nameError(Exception): pass
.
Package inout
This package manipulates the input/output. Currently, it only deal with print. Shell handler or other inout functions will be added here in the future.
- Use
with no_print:
to suppress the console output. Although not recommended, one can usewith no_print as out_stream:
andoutput = str(out_stream)
inside thewith
structure to fetch the output. - Use
sprint = SPrint()
to create a functionsprint
that collects the printed text. Useout = sprint()
orsprint.text
to get the results. - Use
str_print.clear()
to erase the text insidestr_print
andstr_print(output)
to print to this string. It is an instance alias ofSprint
.
Package timing
This package use the time spent of commands to perform useful inspection or organization of the codes.
- Use
@time_this
to time a function. - Use
with Timer("name"):
to time a series of commands. - Use
with scope("name"):
to nest a series of commands. It is an alias ofTimer
. - Use
with scope("name"), jump:
to jump a series of commands. - Use
with scope("name"), Jump(False):
to disable the jump. - Use
wf = Workflow("step1", "step2")
andwith wf("step1(2)"), wf.jump:
before commands of "step1(2)" to create a workflow. One can change the arguments in the init function to decide which steps to run. - Use
@periodic(seconds, max_repeat)
to run a function repeatedly.
Package more
Currently, only once
is contained in the more
package.
Adding it in a function to check if the function is run once or not.
Waiting to Be Imroved
- More functions will be added in the future, including path handler, tools for shell and so on.
- Contact us to make suggestions.
Acknowledgment
@Yuncheng Zhou: Developer
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.