simplebench.utils packageπ
Utility functions for simplebench.
- class simplebench.utils.MachineInfo[source]π
Bases:
TypedDictTypedDict for machine info returned by
get_machine_info().- Variables:
processor (str) β Processor name.
machine (str) β Machine type.
python_compiler (str) β Python compiler used.
python_implementation (str) β Python implementation name.
python_implementation_version (str) β Python implementation version.
python_version (str) β Python version.
python_build (tuple[str, str]) β Python build information.
release (str) β Operating system release.
system (str) β Operating system name.
- simplebench.utils.arg_to_flag(arg: str) str[source]π
Convert a Python argument name to a command-line flag.
This function takes a Python argument name (e.g., βmy_flagβ) and converts it to a command-line flag (e.g., ββmy-flagβ) by adding leading dashes and replacing underscores with hyphens.
- Parameters:
arg (str) β The Python argument name to convert.
- Raises:
SimpleBenchTypeError β If the
argargument is not a str.SimpleBenchValueError β If the
argargument is an empty string.
- Returns:
The corresponding command-line flag.
- Return type:
- simplebench.utils.collect_arg_list( ) list[str][source]π
Collects a list of argument values from a Namespace for a given flag.
This function retrieves the values associated with the specified flag from the provided
Namespaceobject. If the value is a sequence (excluding str and bytes), it returns the values as a list. If the value is a single item, it returns a list containing that single item. If the flag does not exist in the Namespace, it returns an empty list.argparse lists consist of lists of lists of strings and so they have to be flattened to be processed.
- Parameters:
- Raises:
SimpleBenchTypeError β If the
argsargument is not aNamespaceor if the retrieved argument value is of an unexpected type.- Returns:
A list of unique argument values associated with the specified flag.
- Return type:
- simplebench.utils.flag_to_arg(flag: str) str[source]π
Convert a command-line flag to a valid Python argument name.
This function takes a command-line flag (e.g., ββmy-flagβ) and converts it to a valid Python argument name (e.g., βmy_flagβ) as expected in a
Namespaceresult by removing leading dashes and replacing hyphens with underscores.- Parameters:
flag (str) β The command-line flag to convert.
- Raises:
SimpleBenchTypeError β If the
flagarg is not a str.SimpleBenchValueError β If the
flagarg is an empty string or does not start with βββ.
- Returns:
The corresponding Python argument name.
- Return type:
- simplebench.utils.get_machine_info() MachineInfo[source]π
Return a dictionary of information about the current machine and Python version.
- simplebench.utils.kwargs_variations( ) list[dict[str, Any]][source]π
Variations of keyword arguments for the benchmark.
This function takes a dictionary where each key is a keyword argument name and the value is a Sequence of possible values for that argument. It returns a list of dictionaries, each dictionary representing a unique combination of keyword arguments and their values.
Example:
kwargs_variations({ 'arg1': [1, 2], 'arg2': ['a', 'b'] }) # output: # [ # {'arg1': 1, 'arg2': 'a'}, # {'arg1': 1, 'arg2': 'b'}, # {'arg1': 2, 'arg2': 'a'}, # {'arg1': 2, 'arg2': 'b'} # ]
- Parameters:
kwargs (dict[str, Sequence[Any]]) β A dictionary of keyword arguments and their possible values. The value must be a Sequence (e.g., list, tuple, set), but not a str or bytes instance.
- Returns:
A list of dictionaries, each representing a unique combination of keyword arguments and values.
- Return type:
- simplebench.utils.platform_architecture() str[source]π
Return the current architecture.
- Returns:
The architecture.
- Return type:
- simplebench.utils.platform_id() str[source]π
Return a string that uniquely identifies the current machine and Python version.
The platform ID is a lowercase string that combines the operating system, Python implementation, Python version, and architecture.
- Returns:
The platform ID.
- Return type:
- simplebench.utils.platform_implementation() str[source]π
Return the current Python implementation name.
See
platform.python_implementation()for details.- Returns:
The Python implementation name.
- Return type:
- simplebench.utils.platform_machine() str[source]π
Return the current machine type.
- Returns:
The machine type.
- Return type:
- simplebench.utils.platform_processor() str[source]π
Return the current processor name.
- Returns:
The processor name.
- Return type:
- simplebench.utils.platform_system() str[source]π
Return the current operating system name.
- Returns:
The operating system name.
- Return type:
- simplebench.utils.platform_version() str[source]π
Return the current Python version.
- Returns:
The Python version.
- Return type:
- simplebench.utils.python_implementation_version() str[source]π
Return the Python implementation version.
For CPython, this is the same as
platform.python_version(). For PyPy, this is the PyPy version (e.g., β7.3.5β).- Returns:
The Python implementation version.
- Return type:
- simplebench.utils.sanitize_filename(name: str) str[source]π
Sanitizes a filename by replacing invalid characters with _ (underline).
Only a-z, A-Z, 0-9, _ (underline), and - (dash) characters are allowed. All other characters are replaced with _ and multiple sequential _ characters are then collapsed to single _ characters. Leading and trailing _ and - characters are removed.
Examples:
sanitize_filename("My File-Name.txt") # returns "My_File-Name_txt" sanitize_filename("Invalid/Chars\In:Name*?") # returns "Invalid_Chars_In_Name" sanitize_filename(" Leading and Trailing ") # returns "Leading_and_Trailing" sanitize_filename("!!!") # returns "_"
Note
This function does not check for reserved filenames on any operating system.
It is the callerβs responsibility to ensure the sanitized filename is valid for the target filesystem.
If a filename becomes completely empty after sanitization, the function will return a single underscore (β_β) character. This is the one exception to the rule that leading and trailing _ and - characters are removed.
- Parameters:
name (str) β The filename to sanitize.
- Returns:
The sanitized filename.
- Return type:
- Raises:
SimpleBenchTypeError β If the
namearg is not a str.SimpleBenchValueError β If the
namearg is an empty string.
- simplebench.utils.sigfigs(number: float, figures: int = 3) float[source]π
Rounds a floating point number to the specified number of significant figures.
If the number of significant figures is not specified, it defaults to
DEFAULT_SIGNIFICANT_FIGURES.14.2 to 2 digits of significant figures becomes 14
0.234 to 2 digits of significant figures becomes 0.23
0.0234 to 2 digits of significant figures becomes 0.023
14.5 to 2 digits of significant figures becomes 15
0.235 to 2 digits of significant figures becomes 0.24
- Parameters:
- Raises:
TypeError β If the
numberarg is not a float or thefiguresarg is not an int.ValueError β If the
figuresarg is not at least 1.
- Returns:
The rounded number as a float.
- Return type:
Submodulesπ
- simplebench.utils.exceptions module
- simplebench.utils.filenames module
- simplebench.utils.flags_and_args module
- simplebench.utils.kwargs_variations module
- simplebench.utils.machine_info module
- simplebench.utils.significant_figures module