jip.utils

JIP utilities and helper functions

jip.utils.flat_list(source)

Make sure source is a list, else wrap it, and return a flat list.

Parameters:source – source list
Returns:flat list
jip.utils.ignored(*args, **kwds)

Ignores given set of exception in a with context. For example:

with ignore(Exception):
    ...

This will ignore all Exceptions raised within the with block.

Parameters:exceptions – list of exception classes that will be ignored
jip.utils.list_dir(base, recursive=True)

Generator function to iterates a directory recursively and yields all files.

Parameters:
  • base – the base directory
  • recursively – if true, only the content of the top level directory will be yield
jip.utils.parse_mem(mem)

Takse a string and parses a memory pattern. The supported suffixes are G M or K both upper and lower case.

Parameters:mem – the memory string
Returns:memory in megabyte
jip.utils.parse_time(time)

Parse time string and returns time in minutes.

The string can be either a number, which is the time in minutes, or of the form:

<int>d<int>h<int>m<int>s

where any part can be left out, but the order matters.

Examples:

30:
returns 30 minutes
1h:
returns 60 minutes
2h30m:
return 150 minutes

In addition, you can use a colon separated format that is either:

HH:MM

or

HH:MM:SS

Parameters:time (string) – time string
Returns:time in minutes
Return type:integer
Raises:ValueError in case the time could not be parsed
jip.utils.rreplace(s, old, new, occurences=-1)

Replace all occurencens of ‘old’ with ‘new’ starting at the right hand side of the string. If occurences is specified, the number of replacements is limited to occurences.

Parameters:
  • s (string) – the input string
  • old (string) – the string that will be replaced
  • new (string) – the replacement string
  • occurences (integer) – the maximal number of replacements
Fork me on GitHub