eta.util.general
Generic utility functions for various basic data types.
Functions
Append each sublist within lst together, creating a single combined list. |
|
Select the n top values in a given list, according to a list of scores for each list element. |
|
Check whether an input is an atom (either empty list or a non-list). |
|
Map a certainty in [0,1] to a period (in seconds) that an expected event should occur within. |
|
Clear the symbol table used for creating new symbols. |
|
Insert a value to the front of a list or set. |
|
Add v to the list at key k in dct, creating a new list if none exists. |
|
Safe version of dict accessor that returns an empty list if key is not found. |
|
Safe version of dict pop that removes a key from the dict. |
|
Safe version of dict remove that removes val from the list stored at key (or pops the key if it stores an atom). |
|
Given a set of replacements, make a substitution to the keys in a dict for each replacement. |
|
Given an episode variable like ?e1, return the non-fluent dual of that variable, e.g., !e1 (and vice-versa). |
|
Duplicate a variable by generating a new variable symbol with the initial variable (excluding trailing numbers) as a prefix. |
|
Generate a new episode name. |
|
Generate a new episode variable. |
|
Check if a given symbol is an "escaped" symbol, equivalent to vertical bars in LISP. |
|
Recurse through a (possibly nested) list and extract categories that satisfy a given function. |
|
Recursively flatten a list, creating a single list with no sub-lists. |
|
Generate a unique symbol that hasn't been used before by appending an integer suffix i and then incrementing i. |
|
Get the contents immediately following each keyword in keys from a list. |
|
Check whether a given time delta has elapsed the period corresponding to a given certainty. |
|
Indent a string some number of levels. |
|
Check if a given input is a quoted expression. |
|
Compute a linear sum of a list of vectors, scaling each vector by the corresponding coefficient. |
|
Check whether an input is a list (including the empty list). |
|
Normalize a vector to sum to 1. |
|
Insert a value to the end of a list or set. |
|
Select a random element from a list. |
|
Return subexpressions in a tree that are the same as the given symbol. |
|
Return subexpressions in a tree that satisfy cndfn. |
|
Recursively remove a given target from a list. |
|
Recursively replace some old value with a new value throughout a list. |
|
Remove duplicate items in a list, preserving the initial order of order is given as True. |
|
Remove any null values from a list. |
|
Make a list of replacements to a given string in sequence. |
|
Split a list by a given condition function. |
|
Squash each number within a vector to be within the given range. |
|
Standardize a string by applying a series of transformations. |
|
Recursively substitute b for a throughout a list. |
|
Given a set of replacements, make a substitution in a list for each replacement. |
|
Check if a given variable is a "symbol" (i.e., a string). |
|
Convert a list to a valid dict key consisting of only tuples and strings. |
|
Check if a given input is a variable symbol, i.e., starts with '?' or '!'. |
- gentemp(str)[source]
Generate a unique symbol that hasn’t been used before by appending an integer suffix i and then incrementing i.
- Parameters:
str (str) – String to use for symbol generation.
- Returns:
A “symbol” with a unique integer suffix attached to the given string.
- Return type:
Notes
This currently relies on an external symbol table stored in a file, which exploits the fact that race conditions with a shared file cannot occur with Python multiprocess (see: https://superfastpython.com/multiprocessing-race-condition-python/#Race_Condition_With_Shared_Data-2) However, this is somewhat clumsy/inefficient and should eventually be replaced by a proper solution.
- escaped_symbol_p(s)[source]
Check if a given symbol is an “escaped” symbol, equivalent to vertical bars in LISP.
- variablep(s)[source]
Check if a given input is a variable symbol, i.e., starts with ‘?’ or ‘!’.
Notes
For now, this excludes indexical variables, such as ‘^you’.
- dual_var(ep_var)[source]
Given an episode variable like ?e1, return the non-fluent dual of that variable, e.g., !e1 (and vice-versa).
- duplicate_var(var)[source]
Duplicate a variable by generating a new variable symbol with the initial variable (excluding trailing numbers) as a prefix.
- replaceall(str, replist)[source]
Make a list of replacements to a given string in sequence.
- Parameters:
- Return type:
- standardize(str, remove_parentheticals=False)[source]
Standardize a string by applying a series of transformations.
Specifically:
Replace – with -, and _ with whitespace.
Remove parenthetical content (i.e., […] or …), if remove_parentheticals is True.
Add whitespace around all punctuation.
Collapse all whitespace to a single space.
Convert to lowercase.
- rec_replace(old, new, lst)[source]
Recursively replace some old value with a new value throughout a list.
- remove_duplicates(lst, order=False)[source]
Remove duplicate items in a list, preserving the initial order of order is given as True.
- substall(lst, replist)[source]
Given a set of replacements, make a substitution in a list for each replacement.
- get_keyword_contents(lst, keys)[source]
Get the contents immediately following each keyword in keys from a list.
- split_by_cond(lst, cndfn)[source]
Split a list by a given condition function.
- Parameters:
lst (list) –
cndfn (function) –
- Returns:
filtered (list) – The input list with elements matching cndfn filtered out.
matching (list) – A list of elements from the input list matching cndfn.
- extract_category(lst, catfn, ignfn=None)[source]
Recurse through a (possibly nested) list and extract categories that satisfy a given function.
- Parameters:
lst (s-expr) –
catfn (function) – A function used to match categories to be extracted.
ignfn (function, optional) – If given, a function used to ignore matching subexpressions (i.e., avoid recursing within them).
- Returns:
lst_new (s-expr) – The input list with matching subexpressions removed.
categories (list[s-expr]) – A list of extracted matching subexpressions.
- rec_find(lst, x, test=<function <lambda>>)[source]
Return subexpressions in a tree that are the same as the given symbol.
A different binary function can be provided using the test argument.
- dict_substall_keys(dct, replist)[source]
Given a set of replacements, make a substitution to the keys in a dict for each replacement.
- cons_dict(dct, k, v)[source]
Add v to the list at key k in dct, creating a new list if none exists.
- dict_get(dct, k)[source]
Safe version of dict accessor that returns an empty list if key is not found.
- dict_rem_val(dct, k, val)[source]
Safe version of dict remove that removes val from the list stored at key (or pops the key if it stores an atom).
- squash(vector, range=(0, 1))[source]
Squash each number within a vector to be within the given range.
- linsum(vectors, coeffs)[source]
Compute a linear sum of a list of vectors, scaling each vector by the corresponding coefficient.
- argmax(lst, scores, n)[source]
Select the n top values in a given list, according to a list of scores for each list element.
- certainty_to_period(certainty)[source]
Map a certainty in [0,1] to a period (in seconds) that an expected event should occur within.
The proportion between the period and the quantity
-log(1 - certainty)
is determined by the global constant EXPECTED_STEP_FAILURE_PERIOD_COEFFICIENT, defined ineta.constants
.