Welcome to Simpler’s documentation!
simpler package
Submodules
simpler.algorithms module
- class simpler.algorithms.DynamicProgramming(initial_state)[source]
Bases:
object
Abstract class to solve problems using dynamic programming. To use it, make a child class implementing alternatives, is_final and penalty. Then, make one of such objects providing the initial state in the constructor, and call to solve(), providing one search type.
- ALL_OPTIMAL_SOLUTIONS = 3
- ALL_SOLUTIONS = 2
- ONE_OPTIMAL_SOLUTION = 1
- ONE_SOLUTION = 0
- alternatives(state)[source]
This should return all the alternatives for a given state, without modifying the given state.
- Return type:
list
- is_final(state)[source]
This should return whether a state is a final state or not.
- Return type:
bool
simpler.bioinformatics module
- simpler.bioinformatics.dna_to_rna(dna)[source]
Transforms a DNA string into RNA by replacing Ts with Us.
- Return type:
str
- simpler.bioinformatics.parse_fasta(data_string, first=False)[source]
Given a string in FASTA format, returns the DNA strings on it.
- simpler.bioinformatics.reverse_complement(seq, is_rna=True)[source]
Obtains the reverse complement of a DNA or RNA string.
- Return type:
str
simpler.connectors module
- class simpler.connectors.Excel(path)[source]
Bases:
object
Pandas Excel backend.
- block_from_code(code)[source]
Transforms an Excel code like A4:B5 to block delimiters like (3, 0, 5, 2).
- Return type:
tuple
- class simpler.connectors.SQL(host='localhost', user=None, password=None, db=None, charset='utf8mb4', collation='utf8mb4_general_ci', use_unicode=True, max_insertions=None, print_queries=False, native_types=True, engine='mysql', force_init=False)[source]
Bases:
object
Connector for SQL databases with a handful of helpers.
- ENGINES = ('mysql', 'mariadb', 'mssql', 'postgre')
- apply(query, *params)[source]
Applies a modification (update or delete) and returns the number of affected rows.
- Return type:
int
- delete(table, filters=<function SQL.<lambda>>)[source]
Executes a delete operation and returns the number of affected rows, specifying a filters list, i.e. {‘a’: 4, ‘b’: None} will be translated into WHERE A = 4 AND B = NULL.
- Return type:
int
- escape(value, is_literal=True)[source]
Escapes the given value for its injection into the SQL query. By default, the data is_literal=True, which will wrap strings with quotes for its insertion.
- Return type:
str
- execute(query, params=None, multi=False, commit=False)[source]
Wrapper for the database connector execute method that won’t send the params argument if the params are empty, thus avoiding the need to replace % with %%.
- exists(table, column, value)[source]
Returns True if the value exists in the specified column of the specified table.
- Return type:
bool
- find(query, *params)[source]
Returns a {column: value} dict of the first selected row.
- Return type:
dict
- find_all(query, *params)[source]
Returns a list of {column: value} dicts of the selected rows.
- Return type:
List
[dict
]
- find_all_tuples(query, *params)[source]
Returns a list of tuples of the selected rows.
- Return type:
List
[tuple
]
- find_column(query, *params)[source]
Returns the value of the first column of every selected row.
- Return type:
list
- find_tuple(query, *params)[source]
Returns a tuple of the values of the first selected row.
- Return type:
tuple
- find_value(query, *params)[source]
Returns the value of the first column of the first selected row.
- Return type:
Any
- insert(query, *params)[source]
Inserts a row and returns its id (if engine=”postgre”, you’ll have to use the RETURNING keyword).
- Return type:
int
- insert_all(table, rows, tuple_rows=False, commit=True)[source]
Insert a list of rows and returns the id of the last one. By default, these rows are a list of {column: value} dicts, but they can be inserted from tuples of values setting tuple_rows to True.
- Return type:
Optional
[int
]
- iter_all(query, *params)[source]
Returns a generator of {column: value} dicts of the selected rows.
- Return type:
Generator
[dict
,None
,None
]
- iter_all_tuples(query, *params)[source]
Returns a generator of tuples of the selected rows.
- Return type:
Generator
[tuple
,None
,None
]
- iter_column(query, *params)[source]
Returns a generator of the first column of every selected row.
- Return type:
Generator
[list
,None
,None
]
- print_query(query, params=None, color='yellow', max_size=1000)[source]
Shows a query attempting to inject the parameters, for debugging purposes.
- select(table, filters=<function SQL.<lambda>>, first_row=False, first_column=False, tuple_rows=True, or_filters=False)[source]
Executes an select operation and returns the resulting rows, specifying a filters list, i.e. {‘a’: 4, ‘b’: None} will be translated into WHERE A = 4 and B = NULL.
- Return type:
int
- update(table, updates=<function SQL.<lambda>>, filters=<function SQL.<lambda>>)[source]
Executes an update operation and returns the number of affected rows, specifying a {column: value} list of updates and a filters list, i.e. {‘a’: 4, ‘b’: None} will be translated into WHERE A = 4 and B = NULL.
- Return type:
int
simpler.files module
- simpler.files.already_running(path_pidfile='pid.txt')[source]
Uses a PID file to check if an instance of this script is already running. If it’s not running, it will create a PID file (a file with the PID of the current process).
- Return type:
bool
- simpler.files.decompress(input_file, output_dir=None, format='auto')[source]
Decompress the given file to the output directory regardless of its format.
- Return type:
None
- simpler.files.detect_format(path, format, accept=None, default=None)[source]
Detects the format of a file from its path.
- Return type:
Optional
[str
]
- simpler.files.directory_compare(old, new, kind='dir', ignored=('.class', '.metadata', '.recommenders', '.pyc', '.git', '.svn', '.cached', '__pycache__'))[source]
Compares the files in two directories (old and new) to detect files that have been created, deleted, changed or updated, ignoring the specified files.
- Return type:
None
- simpler.files.disk_cache(method=None, *, seconds=None, directory='.cached/', identifier=None)[source]
The first time the decorated method is called, its result is stored as a pickle file, the next call loads the cached result from the disk. The cached files are used indefinitely unless the seconds lifespan is defined. The cached files are stored at .cached unless otherwise specificed with the directory argument. The cache file identifier is generated from the method name plus its arguments, unless otherwise specified with the identifier argument.
Recursively looks for compressed file signatures in a file.
- Return type:
Set
[str
]
- simpler.files.import_from_path(path, name, module_name='.')[source]
Loads the script at the specified path and returns an object given its name.
- Return type:
Any
- simpler.files.load(path, format='auto', encoding='utf-8', inner_args=None, inner_kwargs=None)[source]
Load a file in a given format.
- Return type:
object
- simpler.files.mem_cache(method=None, *, key=None, maxsize=None, is_global=False, global_name=None)[source]
Decorator to cache the output of a method. It is indexed by its arguments unless the key argument is specified, in which case key(*args, **kwargs) will be called to get the indexing key. If maxsize is defined, it is bounded as an LRU cache with maxsize elements at most. If is_global is defined, the cache will be stored globally, so that it can be shared accross multiple methods of multiple instances of a class. A global_name can be defined to identify the method; otherwise, the method name will be used.
- simpler.files.register_protocol_handler(protocol, path, link_name=None, content_type=None)[source]
Register a protocol handler in Windows, so that <protocol>:<address> links call command <path> <address>. I.e, register_protocol_handler(‘magnet’, ‘C:/Program Files/qBittorrent/qbittorrent.exe’, link_name=’URL:Magnet link’, content_type=’application/x-magnet’).
- Return type:
None
- simpler.files.save(path, content, format='auto', encoding='utf-8', append=False, inner_args=None, inner_kwargs=None)[source]
Saves a file to the given format.
- Return type:
None
simpler.format module
- simpler.format.human_bytes(size, decimal_places=2)[source]
Returns a human readable file size from a number of bytes.
- Return type:
str
- simpler.format.human_date(date)[source]
Return a date the a human-friendly format “1 month ago”.
- Return type:
str
- simpler.format.human_seconds(seconds)[source]
Returns a human readable string from a number of seconds.
- Return type:
str
simpler.mail module
simpler.math module
- simpler.math.all_equal(seq)[source]
Returns true if every element in the sequence has the same value.
- Return type:
list
- simpler.math.base_change(n, base_from, base_to)[source]
Changes the base of n represented as a list of integers. Example: base_change([1, 1, 0, 1], 2, 10) = [1, 3]
- Return type:
list
- simpler.math.clamp(value, smallest=0, largest=1)[source]
Returns the value clamped between smallest and largest. I.e.: clamp(10, 2, 8) would return 8.
- Return type:
float
- simpler.math.jaccard(seq1, seq2)[source]
Returns the Jaccard index of two sequences.
- Return type:
list
- simpler.math.levenshtein(seq1, seq2)[source]
Returns the Levenshtein distance of two sequences.
- Return type:
list
- simpler.math.palindrome_list(k)[source]
Returns a list of every palindromic number with k digits.
- Return type:
list
- simpler.math.prime_list(n)[source]
Returns the list of prime numbers from 2 to n.
- Return type:
list
simpler.profiling module
simpler.sparql module
simpler.terminal module
- simpler.terminal.cprint(*args, fg='default', bg='default', **kwargs)[source]
Same syntax as print, with two optional parameters fg and bg to change the print color. Available colors are: default, black, red, green, yellow, blue, magenta, cyan, light_gray, dark_gray, light_red, light_green, light_yellow, light_blue, light_magenta, light_cyan, and white.
simpler.tests module
- class simpler.tests.Suite(*tests)[source]
Bases:
object
Class for running a test suite. Is built as Suite(FirstTest, SecondTest…) where the arguments are an enumeration of subclasses of simpler.Test classes that will be run when using this class run method. There are a few run_<format> methods to get a formatted output.
- run()[source]
Runs the tests and returns a dictionary of tests, where each test is a dictionary of case: error pairs.
- Return type:
Tuple
[Dict
[str
,Tuple
[Dict
[str
,Tuple
[Optional
[str
],float
]],float
,int
,int
]],float
,int
,int
]
simpler.validation module
- simpler.validation.assert_exists(path)[source]
Asserts that the given path exists within PATH_STATIC.
- Return type:
None
- simpler.validation.assert_id(data, name, optional=False, allow_zero=False, default=None)[source]
Asserts that data[name] is a valid database id and returns it.
- Return type:
int
- simpler.validation.assert_mail(data, name, optional=False, default=None)[source]
Asserts that data[name] is a valid mail string and returns it.
- Return type:
str
- simpler.validation.assert_number(data, name, optional=False, min_val=None, max_val=None, is_integer=None, default=None)[source]
Asserts all the requested numeric checks to data[name] and returns it.
- Return type:
int
simpler.web module
- class simpler.web.Driver(timeout=3, keystroke_delay=0.005, headless=True, disable_flash=True, disable_images=True, language='en-US, en', options=None)[source]
Bases:
object
- all_cookies(clear=True, path=None, domain=None, http_only=None, secure=None)[source]
- Return type:
dict
- cookie(name, value=None, expiry=None, delete=False, path=None, domain=None, http_only=None, secure=None)[source]
- Return type:
Optional
[str
]