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.

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

Modify the value for some variable. Params:

varName: for example LD_LIBRARY_PATH varValue: the value to add or replace. 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 add (at begin or end) the new value.
update(valuesDict, position=0)[source]

Use set for each key, value pair in valuesDict.

class pyworkflow.utils.utils.StrColors[source]

Bases: enum.Enum

An enumeration.

blue = 34
cyan = 36
gray = 30
green = 32
magenta = 35
red = 31
yellow = 33
class pyworkflow.utils.utils.Timer[source]

Bases: object

Simple Timer base in datetime.now and timedelta.

getToc()[source]
tic()[source]
toc(message='Elapsed:')[source]
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)

We use a subset of TWiki hyper text conventions. In particular:

some_text will display some_text in bold _some_text_ will display some_text in italic Links:

http://www.link-page.com -> hyperlink using the url as label [[http://www.link-page.com][Link page]] -> hyperlink using “Link page” as label
pyworkflow.utils.utils.blue(txt)
pyworkflow.utils.utils.blueB(txt)

We use a subset of TWiki hyper text conventions. In particular:

some_text will display some_text in bold _some_text_ will display some_text in italic Links:

http://www.link-page.com -> hyperlink using the url as label [[http://www.link-page.com][Link page]] -> hyperlink using “Link page” as label
pyworkflow.utils.utils.blueStr(text)[source]
pyworkflow.utils.utils.cyan(txt)
pyworkflow.utils.utils.cyanB(txt)

We use a subset of TWiki hyper text conventions. In particular:

some_text will display some_text in bold _some_text_ will display some_text in italic Links:

http://www.link-page.com -> hyperlink using the url as label [[http://www.link-page.com][Link page]] -> hyperlink using “Link page” as label
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.executeLongRemote(command, hostName, userName, password)[source]

Execute a remote command. Params:

command: Command to execute. hostName: Remote host name. userName: User name. password: Password.
Returns:
Tuple with standard input, standard output and error output.
pyworkflow.utils.utils.executeRemote(command, hostName, userName, password)[source]

Execute a remote command. Params:

command: Command to execute. hostName: Remote host name. userName: User name. password: Password.
Returns:
Tuple with standard input, standard output and error output.
pyworkflow.utils.utils.executeRemoteX(command, hostName, userName, password)[source]

Execute a remote command with X11 forwarding. Params:

command: Command to execute. hostName: Remote host name. userName: User name. password: Password.
Returns:
Tuple with standard output and error output.
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. Params:

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. params:

text: Text to check. filePath : File path to check.

returns: File 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)[source]

Convert a string representing list items into a list. The items should be separated by spaces 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.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)

We use a subset of TWiki hyper text conventions. In particular:

some_text will display some_text in bold _some_text_ will display some_text in italic Links:

http://www.link-page.com -> hyperlink using the url as label [[http://www.link-page.com][Link page]] -> hyperlink using “Link page” as label
pyworkflow.utils.utils.greenStr(text)[source]
pyworkflow.utils.utils.hex_to_rgb(value)[source]
pyworkflow.utils.utils.isInFile(text, filePath)[source]

Checks if given text is in the given file. params:

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)

We use a subset of TWiki hyper text conventions. In particular:

some_text will display some_text in bold _some_text_ will display some_text in italic Links:

http://www.link-page.com -> hyperlink using the url as label [[http://www.link-page.com][Link page]] -> hyperlink using “Link page” as label
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. Params:

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)

We use a subset of TWiki hyper text conventions. In particular:

some_text will display some_text in bold _some_text_ will display some_text in italic Links:

http://www.link-page.com -> hyperlink using the url as label [[http://www.link-page.com][Link page]] -> hyperlink using “Link page” as label
pyworkflow.utils.utils.redStr(text)[source]
pyworkflow.utils.utils.rgb_to_hex(rgb)[source]
pyworkflow.utils.utils.startDebugger(password='a')[source]
pyworkflow.utils.utils.timeit(func)[source]

Decorator function to have a simple measurement of the execution time of a given function. Just use: @timeit def func(…)

to use it.

pyworkflow.utils.utils.trace(nlevels, separator=' --> ', stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]
pyworkflow.utils.utils.white(txt)
pyworkflow.utils.utils.whiteB(txt)

We use a subset of TWiki hyper text conventions. In particular:

some_text will display some_text in bold _some_text_ will display some_text in italic Links:

http://www.link-page.com -> hyperlink using the url as label [[http://www.link-page.com][Link page]] -> hyperlink using “Link page” as label
pyworkflow.utils.utils.yellow(txt)
pyworkflow.utils.utils.yellowB(txt)

We use a subset of TWiki hyper text conventions. In particular:

some_text will display some_text in bold _some_text_ will display some_text in italic Links:

http://www.link-page.com -> hyperlink using the url as label [[http://www.link-page.com][Link page]] -> hyperlink using “Link page” as label
pyworkflow.utils.utils.yellowStr(text)[source]