contaix.util¶
General utilities for contaix
This module provides core utility functions used throughout the contaix package, including:
File path handling (fullpath)
URL detection and content retrieval (is_url, url_to_contents)
File saving utilities (save_to_file_and_return_file)
Basic helper functions (identity)
Functions
|
Returns the full path of the given path. |
|
Returns the input unchanged. |
|
Check if the given path is a URL. |
|
Remove improperly double newlines from a string. |
|
Save |
Decorator that sources a |
|
|
Fetch the content from a URL. |
- contaix.util.fullpath(path)[source]¶
Returns the full path of the given path.
- Parameters:
path (
str) – The path to convert to a full path.- Returns:
The full path.
- Return type:
Example
>>> fullpath('~/Downloads') '/home/user/Downloads'
>>> fullpath('.') '/home/user/python_projects/aix/aix'
- contaix.util.identity(x)[source]¶
Returns the input unchanged.
- Parameters:
x – Any input
- Returns:
The input unchanged
- contaix.util.remove_improperly_double_newlines(string, *, copy_to_clipboard=True)[source]¶
Remove improperly double newlines from a string.
Delegates to
dn.repair.remove_improperly_double_newlinesfor the pure transform, adding clipboard integration via the decorator.- Parameters:
- Returns:
The string with improperly double newlines removed.
- Return type:
Example
>>> text = "This is a test.\n\nThis should be one newline.\n \nThis too." >>> result = remove_improperly_double_newlines(text, copy_to_clipboard=False) >>> assert result == 'This is a test.\n\nThis should be one newline.\nThis too.'
- contaix.util.save_to_file_and_return_file(obj=None, *, encoder=<function identity>, key=None)[source]¶
Save
encoder(obj)to a file using a random name inrootdir(or a temp directory if not provided). Returns the full path to the saved file. Ifobjis None, returns a partial function with preconfiguredencoderandrootdir.- Parameters:
obj – The object to save. If None, return a partial function.
encoder – A function to encode the object into text or bytes. Defaults to identity.
key (
str|Callable) – The key (by default, filepath) to write to. If None, a temporary file is created. If a string starting with ‘*’, the ‘*’ is replaced with a unique temporary filename. If a string that has a ‘*’ somewhere in the middle, what’s on the left of if is used as a directory and the ‘*’ is replaced with a unique temporary filename. For example ‘/tmp/*_file.ext’ would be replaced with ‘/tmp/oiu8fj9873_file.ext’. If a callable, it will be called with obj as input to get the key. One use case is to use a function that generates a key based on the object.
- Returns:
Full path to the saved file, or a partial function if
objis None.- Return type:
Examples
>>> from pathlib import Path >>> filepath = save_to_file_and_return_file("hello world") >>> import os >>> Path(filepath).read_text() 'hello world'
The default encoder is identity, so you can save binary data as well:
>>> filepath = save_to_file_and_return_file(b"binary data", encoder=lambda x: x) >>> Path(filepath).read_bytes() b'binary data'
- contaix.util.source_first_arg_from_clipboard_if_none(func)[source]¶
Decorator that sources a
stringargument from the clipboard when it’s None and copies the function’s return value back to the clipboard when the keyword argumentcopy_to_clipboardis truthy.The wrapped function must accept a parameter named
string. It may also accept a keyword-only parameter namedcopy_to_clipboard(default True).