Utils¶
- class chepy.modules.utils.Utils(*data)¶
-
- count_occurances(regex: str, case_sensitive: bool = False) UtilsT ¶
Counts occurrences of the regex.
Counts the number of times the provided string occurs.
- Parameters
regex (str) – Required. Regex string to search for
case_sensitive (bool, optional) – If search should be case insensitive, by default False
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy("AABCDADJAKDJHKSDAJSDdaskjdhaskdjhasdkja").count_occurances("ja").out 2
- diff(state: int = None, buffer: int = None, colors: bool = False, swap: bool = False, only_changes: bool = False)¶
Diff state with another state or buffer
- Parameters
state (int, optional) – Index of state to compare against. Defaults to None.
buffer (int, optional) – Index of buffer to compare against. Defaults to None.
colors (bool, optional) – Show colored diff. Defaults to False.
swap (bool, optional) – Swap the diff order. Defaults to False.
only_changes (bool, optional) – Return only changes. Defaults to False.
- Raises
TypeError – If both state and buffer is set to True.
- Returns
The Chepy object.
- Return type
Examples
>>> c = Chepy("first string", "First $trin") >>> # there are two states >>> c.diff(state=1) # this will diff state 0 with state 1 {F->f}irst {-$}strin{+g} >>> # optionally set colors=True in the diff method to see colored output
- drop_bytes(start: int, length: int) UtilsT ¶
Drop bytes from starting index up to length
- Parameters
start (int) – Starting index
length (int) – Number of bytes to drop
- Raises
ValueError – If start or length < -1
- Returns
The Chepy object
- Return type
- escape_string() UtilsT ¶
Escape all special characters in a string
- Returns
The Chepy object.
- Return type
- expand_alpha_range(join_by: Optional[str] = None)¶
Get all alphanumberic or hex chars for the specified range
- Parameters
join_by (str, optional) – Join by. Defaults to Union[str, None].
- Returns
The Chepy object.
- Return type
- filter_dict_key(by: str) UtilsT ¶
Filter dictionary by key
- Parameters
by (str) – Required. Key to filter by.
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy({'some': 'dict', 'another': 'val'}).filter_dict_key('ano') {'another': 'val'}
- filter_dict_value(by: str) UtilsT ¶
Filter dictionary by value.
This method does descend into nested dictionary values.
- Parameters
by (str) – Required. Value to filter by.
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy({'some': 'dict', 'another': 'val'}).filter_dict_value('val') {'another': 'val'}
- filter_list(by: Union[str, dict], regex: bool = True) UtilsT ¶
Filter a list by a string regex or dict key
- Parameters
by (Union[str, dict]) – If string, supports regex. Or dictionary
regex (bool, optional) – If pattern is a regex. Defaults to True
- Raises
StateNotList – If state is not a list
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy('[{"a": 1}, {"b": 2}, {"a": 1, "b": 3}]').str_list_to_list().filter_list("b").o [{"b": 2}, {"a": 1, "b": 3}]
- filter_list_by_length(length: int, exact: bool = False) UtilsT ¶
Filter a list by length by specifying minimum length.
It will also return items that exceed the specified length.
- Parameters
length (int) – Minimum length to match
exact (bool) – Match exact length
- Returns
The Chepy object.
- Return type
- find_replace(pattern: str, repl: str, ignore_case=True) UtilsT ¶
Replace matched pattern with repln
- Parameters
pattern (str) – Required. Pattern to search
repl (str) – Required. Pattern to match
ignore_case (bool, optional) – Case insensitive. Defaults to True.
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy("some some data").find_replace("some\s", "data").o "datadatadata"
- pad(width: int, direction: str = 'left', char: str = ' ') UtilsT ¶
Pad string with a character
- Parameters
width (int) – Required. Total length of string. The padding is calculated from the length of state minus width.
direction (str, optional) – Padding direction. left or right. Defaults to ‘left’.
char (str, optional) – Char to fill with. Defaults to ‘ ‘.
- Returns
The Chepy object.
- Return type
Examples
>>> c = Chepy("lol").pad(5, char="a") lol # this is because "lol" is len 3, and padding is 5 - 2, which is 2, so no padding is added >>> c = Chepy("lol").pad(8, char="a") lolaa # 8 - 3 so the output is padded for 5 chars >>> c = Chepy("lol").pad(8, direction="right", char="a") aalol
- pick(*values: Any)¶
Only pick specified values from the state. Works on strings, bytes, lists and dicts
- Raises
TypeError – If state does not contain valid data
- Returns
The chepy object.
- Return type
- regex_search(pattern: str, is_bytes: bool = False, ignore_case: bool = False, multiline: bool = False, dotall: bool = False, unicode: bool = False, extended: bool = False) UtilsT ¶
Regex search on current data. State will be an array of matches.
- Parameters
pattern (str) – Required. The regex pattern to search by
is_bytes (bool, optional) – Treat the pattern and state as bytes. Defaults to False.
ignore_case (bool, optional) – Set case insensitive flag. Defaults to False.
multiline (bool, optional) – ^/$ match start/end. Defaults to False.
dotall (bool, optional) – . matches newline. Defaults to False.
unicode (bool, optional) – Match unicode characters. Defaults to False.
extended (bool, optional) – Ignore whitespace. Defaults to False.
- Returns
The Chepy object.
- Return type
Examples
>>> c = Chepy("loLolololoL") >>> c.regex_search("ol", ignore_case=True)
- regex_to_str(all_combo: bool = False) UtilsT ¶
Convert a regex to a matching string
- Parameters
all_combo (bool, optional) – Generate all combos that match regex. Defaults to False.
- Returns
The Chepy object.
- Return type
- remove_nullbytes() UtilsT ¶
Remove null x00 byes from binary data
- Returns
The Chepy object.
- Return type
- remove_whitespace(spaces: bool = True, carriage_return: bool = True, line_feeds: bool = True, tabs: bool = True, form_feeds: bool = True)¶
Remove whitespace from a string
- Parameters
spaces (bool, optional) – Remove spaces. Defaults to True.
carriage_return (bool, optional) – Remove carriage return r. Defaults to True.
line_feeds (bool, optional) – Remove line feeds n. Defaults to True.
tabs (bool, optional) – Temove tabs t. Defaults to True.
form_feeds (bool, optional) – Remove form feeds f. Defaults to True.
- Returns
The Chepy object.
- Return type
- reverse(count: int = 1) UtilsT ¶
Reverses a string
- Parameters
count (int, optional) – Reverse by the number of characters indicated in count. Defaults to 1.
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy("abcdefg").reverse().out "gfedcba"
- select_every_n(n: int, start: int = 0) UtilsT ¶
Select every nth item from a list or string.
Index starts at 0
- Parameters
n (int) – n from 0
start (int) – starting position. Defaults to 0.
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy(["a", 1, "lol", "b", True]).select_every_n(3) ["a", "b"]
- shuffle() UtilsT ¶
Shuffle the state if it is a list, string or bytes. The state is unchanged if any other types.
- Returns
The Chepy object
- Return type
- slice(start: int = 0, end: int = None) UtilsT ¶
Returns the specified slice
- Parameters
start (int, optional) – Start position. Defaults to 0.
end (int, optional) – End position. Defaults to None.
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy("some data").slice(3, 6).o "e d"
- sort_dict_key(reverse: bool = False) UtilsT ¶
Sort a dictionary by key
- Parameters
reverse (bool, optional) – Reverse sort order. Defaults to False.
- Returns
The Chepy object.
- Return type
Examples
>>> c = Chepy({'z': 'string', 'a': True, 'zz': 1, 'aaa': {'bb': 'data'}, 'ccc': [1,'a']}) >>> c.sort_dict_key(reverse=True) {'zz': 1, 'z': 'string', 'ccc': [1, 'a'], 'aaa': {'bb': 'data'}, 'a': True}
- sort_dict_value(reverse=False) UtilsT ¶
Sort dictionary by value
- Parameters
reverse (bool, optional) – Reverse sort order. Defaults to False.
- Returns
The Chepy object.
- Return type
Examples
>>> c = Chepy({'z': 'string', 'a': 'True', 'zz': '1', 'aaa': {'bb': 'data'}, 'ccc': [1,'a']}) >>> c.sort_dict_value() {'zz': '1', 'a': 'True', 'ccc': [1, 'a'], 'z': 'string', 'aaa': {'bb': 'data'}}
- sort_list(reverse: bool = False) UtilsT ¶
Sort a list
- Parameters
reverse (bool, optional) – In reverse order. Defaults to False.
- Raises
StateNotList – If state is not list
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy(["a", "b", "1", "2"]).sort_list().o ["1", "2", "a", "b"]
- split_by_char(delimiter: str = ' ') UtilsT ¶
Split a string by a delimiter
- Parameters
delimiter (str, optional) – Delimiter to split by. Defaults to ” “.
- Returns
The Chepy object.
- Return type
UtilsT
- split_by_n(n: int) UtilsT ¶
Split a string by n characters.
- Parameters
n (int) – n from 0
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy("some string").split_by_n(2).o[2] " s"
- split_by_regex(pattern: str = '\n', trim=True) UtilsT ¶
Split a string by the given regex pattern
- Parameters
pattern (str, optional) – Pattern to split by. Defaults to ‘n’.
time (bool, optional) – Trim whitespace after split. Defaults to True
- Returns
The Chepy object.
- Return type
- split_chunks(chunk_size) UtilsT ¶
Split data in chunks
- Parameters
chunk_size (int) – Chunk size
- Returns
The Chepy object.
- Return type
- strip(pattern: str, ignore_case=True) UtilsT ¶
Strip matched pattern
- Parameters
pattern (str) – Required. Pattern to search
ignore_case (bool, optional) – Case insensitive. Defaults to True.
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy("some some data").strip("some\s").o "data"
- strip_ansi() UtilsT ¶
Strip ANSI escape codes from string
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy("[31mThis is a string[0m").strip_ansi().o "This is a string"
- unescape_string() UtilsT ¶
Unescape from a string
- Returns
The Chepy object.
- Return type
Examples
>>> Chepy("\$ome' d@ta").unescape_string().o "$ome' d@ta"
- unique() UtilsT ¶
Get an array of unique list items
- Raises
StateNotList – If state is not a list
- Returns
The Chepy object.
- Return type