cslug.misc

The miff-muffet-moof module.

cslug.misc.anchor(*paths)[source]

Replace relative paths with frozen paths relative to __file__'s parent.

Parameters:

paths (str or os.PathLike or io.IOBase) – Path(s) to freeze or pseudo files.

Returns:

List of modified paths.

Return type:

list

Pseudo files (io.IOBase) and absolute paths are left unchanged. Use this function to make your code working-dir independent.

cslug.misc.array_typecode(c_name)[source]

Choose a type code for array.array.

Parameters:

c_name (str) – The name you would use in C to define the type.

Returns:

Any of array.typecodes.

Return type:

str

Use this function to normalise aliases and platform specific exact types.

Examples

>>> array_typecode("long")
'l'
>>> array_typecode("double")
'd'
>>> array_typecode("uint64_t")
'Q'
>>> array_typecode("size_t")
'Q'
cslug.misc.as_path_or_buffer(file)[source]

Normalise filenames to pathlib.Path, leaving streams untouched.

cslug.misc.as_path_or_readable_buffer(file)[source]

Normalise filenames to pathlib.Path, and streams to io.StringIO.

Streams need to be re-readable in cslug. An io.StringIO is via file.getvalue() - everything else generally isn't. The goal of using io streams is only to prevent strings of source code from being confused for string filenames - not, as is the more normal usage, to avoid holding large files in memory.

cslug.misc.block_compile()[source]

Temporarily block cslug compilation.

A context manager to temporarily set the CC environment variable to !block which is a signal to cslug that it is not allowed to use any C compiler.

>>> from cslug import cc, misc
>>> cc()
'c:\MinGW\bin\gcc.EXE'
>>> with misc.block_compile():
...     cc()
cslug.exceptions.BuildBlockedError: The build was blocked by the
environment variable `CC=block`.

This is only meant for testing.

cslug.misc.flatten(iterable, types=(<class 'tuple'>, <class 'list'>), initial=None)[source]

Collapse nested iterables into one flat list.

Parameters:
  • iterable – Nested container.

  • types – Type(s) to be collapsed. This argument is passed directly to isinstance.

  • initial – A pre-existing list to append to. An empty list is created if one is not supplied.

Returns:

The flattened output.

Return type:

list

>>> flatten([[1, 2], 3, [4, [5]]])
[1, 2, 3, 4, 5]
>>> flatten(1.0)
[1.0]
>>> flatten([(1, 2), 3, (4, 5)])
[1, 2, 3, 4, 5]
>>> flatten([(1, 2), 3, (4, 5)], types=list)
[(1, 2), 3, (4, 5)]
>>> flatten([(1, 2), 3, (4, 5)], initial=[6, 7])
[6, 7, 1, 2, 3, 4, 5]
cslug.misc.hide_from_PATH(name)[source]

Modify PATH from os.environ so that name can't be found.

Parameters:

name (str) – The executable name to hide.

Returns:

The original value of os.environ['PATH'].

Return type:

str

cslug.misc.read(path, mode='r')[source]

Read a path or a stream.

Line endings are normalised to Unix '\n' if mode == 'r' so as to be consistent with open.

cslug.misc.write(path, *data, mode='w')[source]

Write to a path or a stream.

Parameters:
Returns:

The number of characters written.

Return type:

int

If path is a stream it is simply written to without closing it afterwards. If path is a filename then it is opened, written to, then closed.