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:
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:
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 toio.StringIO.Streams need to be re-readable in cslug. An
io.StringIOis viafile.getvalue()- everything else generally isn't. The goal of usingiostreams 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
CCenvironment variable to!blockwhich 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
listto append to. An empty list is created if one is not supplied.
- Returns:
The flattened output.
- Return type:
>>> 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
PATHfromos.environso that name can't be found.
- cslug.misc.read(path, mode='r')[source]
Read a path or a stream.
Line endings are normalised to Unix
'\n'ifmode == 'r'so as to be consistent withopen.