pyworkflow.utils.utils module

class pyworkflow.utils.utils.Environ[source]

Bases: dict

Some utilities to handle environment settings.

BEGIN = 1
END = 2
REPLACE = 0
addLibrary(libraryPath, position=1)[source]

Adds a path to LD_LIBRARY_PATH at the requested position if the provided paths exist.

getFirst(keys, mandatory=False)[source]

Return the value of the first key present in the environment. If none is found, returns the ‘defaultValue’ parameter.

getPrepend()[source]

Return if there is any prepend value. See setPrepend function.

set(varName, varValue, position=0)[source]

Modify the value for some variable.

Parameters
  • varName – for example LD_LIBRARY_PATH

  • varValue – the value to set, prefix or suffix.

  • position – controls how the value will be changed. If REPLACE, it will overwrite the value of the var. BEGIN or END will preserve the current value and will add, at the beginning or end, the new value.

setPrepend(prepend)[source]

Use this method to set a prepend string that will be added at the beginning of any command that will be run in this environment. This can be useful for example when ‘modules’ need to be loaded and a simple environment variables setup is not enough.

update(valuesDict, position=0)[source]

Use set for each key, value pair in valuesDict.

class pyworkflow.utils.utils.LazyDict(callback=<class 'dict'>)[source]

Bases: object

Dictionary to be initialized at the moment it is accessed for the first time. Initialization is done by a callback passed at instantiation

Parameters

callback – method to initialize the dictionary. Should return a dictionary

evaluate_callback()[source]
class pyworkflow.utils.utils.OS[source]

Bases: object

classmethod WLSfile2Windows(file)[source]
classmethod getDistro()[source]
static getPlatform()[source]
classmethod getWLSNAME()[source]
classmethod isCentos()[source]
classmethod isUbuntu()[source]
classmethod isWSL()[source]
class pyworkflow.utils.utils.Timer(message='')[source]

Bases: object

Simple Timer base in datetime.now and timedelta.

getElapsedTime()[source]
getToc()[source]
tic()[source]
toc(message='Elapsed:')[source]
class pyworkflow.utils.utils.UtcConverter[source]

Bases: object

Class to make date conversions to utc

utc_delta = datetime.timedelta(days=-1, seconds=79199, microseconds=999995)
pyworkflow.utils.utils.ansi(n, bold=False)[source]

Return function that escapes text with ANSI color n.

pyworkflow.utils.utils.black(txt)
pyworkflow.utils.utils.blackB(txt)
pyworkflow.utils.utils.blue(txt)
pyworkflow.utils.utils.blueB(txt)
pyworkflow.utils.utils.blueStr(text)[source]
pyworkflow.utils.utils.cyan(txt)
pyworkflow.utils.utils.cyanB(txt)
pyworkflow.utils.utils.cyanStr(text)[source]
pyworkflow.utils.utils.dateStr(dt=None, time=True, secs=False, dateFormat=None)[source]

Get a normal string representation of datetime. If dt is None, use NOW.

pyworkflow.utils.utils.envVarOn(varName, env=None)[source]

Is variable set to True in the environment?

pyworkflow.utils.utils.environAdd(varName, newValue, valueFirst=False)[source]

Add a new value to some environ variable. If valueFirst is true, the new value will be at the beginning.

pyworkflow.utils.utils.existsVariablePaths(variableValue)[source]

Check if the path (or paths) in variableValue exists. Multiple paths are allowed if separated by os.

pyworkflow.utils.utils.formatExceptionInfo(level=6)[source]
pyworkflow.utils.utils.getBoolListFromValues(valuesStr, length=None)[source]

Convert a string to a list of booleans

pyworkflow.utils.utils.getColorStr(text, color, bold=False)[source]

Add ANSI color codes to the string if there is a terminal sys.stdout.

Parameters
  • text – text to be colored

  • color – red or green

  • bold – bold the text

pyworkflow.utils.utils.getEnvVariable(variableName, default=None, exceptionMsg=None)[source]

Returns the value of an environment variable or raise an exception message. Useful when adding variable to the config file and report accurate messages

pyworkflow.utils.utils.getFloatListFromValues(valuesStr, length=None)[source]

Convert a string to a list of floats

pyworkflow.utils.utils.getFreePort(basePort=0, host='')[source]
pyworkflow.utils.utils.getHostFullName()[source]

Return the fully-qualified name of the local machine.

pyworkflow.utils.utils.getHostName()[source]

Return the name of the local machine.

pyworkflow.utils.utils.getLineInFile(text, fileName)[source]

Find the line where the given text is located in the given file.

Parameters
  • text – Text to check.

  • filePath – File path to check.

:return line number where the text was located.

pyworkflow.utils.utils.getListFromRangeString(rangeStr)[source]

Create a list of integers from a string with range definitions. Examples: “1,5-8,10” -> [1,5,6,7,8,10] “2,6,9-11” -> [2,6,9,10,11] “2 5, 6-8” -> [2,5,6,7,8]

pyworkflow.utils.utils.getListFromValues(valuesStr, length=None, caster=<class 'str'>)[source]

Convert a string representing list items into a list. The items should be separated by spaces or commas and a multiplier ‘x’ can be used. If length is not None, then the last element will be repeated until the desired length is reached.

Examples: ‘1 1 2x2 4 4’ -> [‘1’, ‘1’, ‘2’, ‘2’, ‘4’, ‘4’] ‘2x3, 3x4, 1’ -> [‘3’, ‘3’, ‘4’, ‘4’, ‘4’, ‘1’]

pyworkflow.utils.utils.getLocalHostName()[source]
pyworkflow.utils.utils.getLocalUserName()[source]

Recover local machine user name. returns: Local machine user name.

pyworkflow.utils.utils.getMemoryAvailable()[source]

Return the total memory of the system in MB

pyworkflow.utils.utils.getPython()[source]
pyworkflow.utils.utils.getPythonPackagesFolder()[source]
pyworkflow.utils.utils.getRangeStringFromList(list)[source]
pyworkflow.utils.utils.getStringListFromValues(valuesStr, length=None)[source]

Convert a string to a list of booleans

pyworkflow.utils.utils.getUniqueItems(originalList)[source]

Method to remove repeated items from one list originalList – Original list with repeated items, or not. returns – New list with the content of original list without repeated items

pyworkflow.utils.utils.grayStr(text)[source]
pyworkflow.utils.utils.green(txt)
pyworkflow.utils.utils.greenB(txt)
pyworkflow.utils.utils.greenStr(text)[source]
pyworkflow.utils.utils.hasAnyFileChanged(files, time)[source]

Returns true if any of the files in files list has been changed after ‘time’

pyworkflow.utils.utils.hasFileChangedSince(file, time)[source]

Returns true if the file has changed after ‘time’

pyworkflow.utils.utils.hex_to_rgb(value)[source]
pyworkflow.utils.utils.isInFile(text, filePath)[source]

Checks if given text is in the given file.

Parameters
  • text – Text to check.

  • filePath – File path to check.

:returns True if the given text is in the given file,

False if it is not in the file.

pyworkflow.utils.utils.isPower2(num)[source]

Return True if ‘num’ is a power of 2.

pyworkflow.utils.utils.lighter(color, percent)[source]

assumes color is rgb between (0, 0, 0) and (255, 255, 255)

pyworkflow.utils.utils.magenta(txt)
pyworkflow.utils.utils.magentaB(txt)
pyworkflow.utils.utils.magentaStr(text)[source]
pyworkflow.utils.utils.parseBibTex(bibtexStr)[source]

Parse a bibtex file and return a dictionary.

pyworkflow.utils.utils.parseHyperText(text, matchCallback)[source]

Parse the text recognizing Hyper definitions below.

Parameters

matchCallback – a callback function to processing each matching, it should accept the type of match (HYPER_BOLD, ITALIC or LINK)

:return The input text with the replacements made by matchCallback

pyworkflow.utils.utils.prettyDate(time=False)[source]

Get a datetime object or a int() Epoch timestamp and return a pretty string like ‘an hour ago’, ‘Yesterday’, ‘3 months ago’, ‘just now’, etc

pyworkflow.utils.utils.prettyDelta(timedelta)[source]

Remove the milliseconds of the timedelta.

pyworkflow.utils.utils.prettyDict(d)[source]
pyworkflow.utils.utils.prettyLog(msg)[source]
pyworkflow.utils.utils.prettySize(size)[source]

Human friendly file size.

pyworkflow.utils.utils.prettyTime(dt=None, time=True, secs=False, dateFormat=None)

Get a normal string representation of datetime. If dt is None, use NOW.

pyworkflow.utils.utils.prettyTimestamp(dt=None, format='%Y-%m-%d_%H%M%S')[source]
pyworkflow.utils.utils.prettyXml(elem, level=0)[source]

Add indentation for XML elements for more human readable text.

pyworkflow.utils.utils.printTraceBack()[source]
pyworkflow.utils.utils.readProperties(propsFile)[source]
pyworkflow.utils.utils.red(txt)
pyworkflow.utils.utils.redB(txt)
pyworkflow.utils.utils.redStr(text)[source]
pyworkflow.utils.utils.rgb_to_hex(rgb)[source]
pyworkflow.utils.utils.sortListByList(inList, priorityList)[source]

Returns a list sorted by some elements in a second priorityList

pyworkflow.utils.utils.strToBoolean(string)[source]

Converts a string into a Boolean if the string is on of true, yes, on, 1. Case insensitive.

pyworkflow.utils.utils.strToDuration(durationStr)[source]

Converts a string representing an elapsed time to seconds E.g.: for “1m 10s” it’ll return 70

pyworkflow.utils.utils.timeit(func)[source]

Decorator function to have a simple measurement of the execution time of a given function. To use it

@timeit
def func(...)
    ...
pyworkflow.utils.utils.trace(nlevels, separator=' --> ', stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]
pyworkflow.utils.utils.weakImport(package, msg=None)[source]

This method can be used to tolerate imports that may fail.

e.g:

from .protocol_ctffind import CistemProtCTFFind
with weakImport('tomo'):
    from .protocol_ts_ctffind import CistemProtTsCtffind

In this case CistemProtTsCtffind should fail if tomo package is missing, but exception is captured and all the imports above should be available

Parameters

package – name of the package that is expected to fail

pyworkflow.utils.utils.white(txt)
pyworkflow.utils.utils.whiteB(txt)
pyworkflow.utils.utils.yellow(txt)
pyworkflow.utils.utils.yellowB(txt)
pyworkflow.utils.utils.yellowStr(text)[source]