Helpers
File with helper function implementations
json_save(path, obj)
Save supported files to json format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
path to save object |
required |
obj: Dict object
Source code in app/utils.py
json_load(path, encoding='utf-8')
Load supported json format files
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str
|
path of the json file |
required |
encoding |
str
|
encoding to use |
'utf-8'
|
Returns:
| Name | Type | Description |
|---|---|---|
obj |
Object
|
any object with any json supported format |
Source code in app/utils.py
preprocess_remark(remark)
Replace html tags and pre-war
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remark |
str
|
home remark |
required |
Returns:
| Name | Type | Description |
|---|---|---|
remark |
str
|
remark without html tags and prewar instead of pre-war |
Source code in app/utils.py
replace_none(patterns, home_synonyms)
Replace none in patters with home synonyms
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns |
list
|
list of patterns |
required |
home_synonyms |
list
|
home synonyms |
required |
Returns:
| Name | Type | Description |
|---|---|---|
patterns |
list
|
list of patterns with home synonyms |
Source code in app/utils.py
generate_guid(k=6)
Generate random id
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k |
length of guid |
6
|
Returns:
| Name | Type | Description |
|---|---|---|
guid |
string
|
random id in string format |
Source code in app/utils.py
filter_and_sort_list(to_process, by)
Filter and reorder a list based on given ranking
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_process |
list
|
elements to filter and sort |
required |
by |
list
|
determines which elements should stay from the original list and in what order |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
list
|
filtered and sorted list |
inputs = ["A", "B", "C", "D", "E", "F", "G"] vowels = ["E", "O", "A", "U", "I"] filter_and_sort_list(to_process=inputs, by=vowels) ["E", "A"]
Source code in app/utils.py
generate_item_order(num1, num2)
Generate a 2d ordering of indices where - diagonal elems are most preferred ones (from top to bottom) - then come the combination with unused indices (if one of the list is longer than other) - then all the unused items from left to right from top to bottom
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num1 |
int
|
length of ordering (rows and cols) |
required |
num2 |
int
|
length of ordering (rows and cols) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
list of tuple of int
|
ordered indices |
Source code in app/utils.py
get_different_elems(long, short)
Create a list of pairs from given lists when diagonal elems are first, then if one list is than another the range is "shifted" and new diagonal is taken etc.. Ex.
get_different_elems(range(6), range(4)) [(0, 0), (1, 1), (2, 2), (3, 3), (4, 0), (5, 1)]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
long |
List of int
|
items to create new pairs from. For best results put longer one first |
required |
short |
List of int
|
items to create new pairs from. For best results put longer one first |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
List of Tuple
|
indices of items |
Source code in app/utils.py
connect_lists_by_order(arr1, arr2)
Create list of pairs from 2 lists, where the diagonal elems are first, then come unused
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arr1 |
list
|
arrays to connect |
required |
arr2 |
list
|
arrays to connect |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
List[Tuple]
|
connected lists |
Source code in app/utils.py
remove_used_strings(to_filter, by)
Remove all elements from one list that are substrings of any element of the second list
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
to_filter |
list of str
|
strings to filter |
required |
by |
list of str
|
strings to search for substrings in |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
List[str]
|
filtered strings |
Source code in app/utils.py
lowercase_phrase(phrase, exceptions)
Lowercase the phrase except for the words in exceptions list
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phrase |
str
|
string to lowercase possibly consisting of several words |
required |
exceptions |
List[str]
|
list of words that shouldn't be lowercased |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
str
|
processed string |
Source code in app/utils.py
case_insensitive_intersection(list1, list2)
Return intersection of two lists of string preserving the original order and case from the first list. The intersection is case insensitive
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list1 |
List of str
|
lists to find the intersection of |
required |
list2 |
List of str
|
lists to find the intersection of |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
List of str
|
elements that are present in both lists in the order they appear in list1 |
Source code in app/utils.py
del_duplicates_ordered(seq, ignore_case=False)
Remove duplicate elements from the sequence preserving their initial order
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq |
List
|
elements to filter |
required |
ignore_case |
bool
|
valid only for list of strings. Shows whether to treat uppercase/lowercase versions of the same string as duplicates |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
out |
List
|
deduplicated list |
Source code in app/utils.py
generate_openapi_json(openapi_version='3.1.0')
Generate and save openapi specification of the application
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
openapi_version |
str
|
openapi version specifier |
'3.1.0'
|
Returns:
| Type | Description |
|---|---|
None
|
|
Source code in app/utils.py
make_prompt(system_message, prompt)
Make prompt from two separate parts
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_message |
List[Dict]
|
The message to help generate text |
required |
prompt |
str
|
Prompt with which to generate |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
List[Dict]
|
|
Source code in app/utils.py
replace_pattern(match)
Replace patterns in a matched pattern with commas and spaces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
match |
Match
|
A regular expression match object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
out |
str
|
|