mli.lib package¶
Submodules¶
mli.lib.config module¶
The module provides an interface for reading and editing the configuration file.
- Classes:
ConfigProgram(sFilePath=’config.ini’)
Using:
There are two ways to use the class.
Foo = ConfigProgram()or
Foo = ConfigProgram('my_config.ini')Now you can read parameters from configfile and save to it.
Parameter = Foo.get_config_value(Section, Option)
- class mli.lib.config.ConfigProgram(sPathApp, sFilePath='config.ini')[source]¶
Bases:
ConfigParser
A class for working with a configuration file. Allows you to read from the configuration file and write there.
- Using:
Foo = ConfigProgram() # For reading. Section = 'bar' Options = 'baz' Value = Foo.get_config_value(Section, Option) # For writing. Value = 'some_string' Foo.set_config_value (Section, Option, Value)
If you only need to create a Section and an Option, you omit the Value.
- get_config_value(sSection, sOption)[source]¶
The method allows reading from a configuration file.
- Parameters
sSection (str) – The section in the configuration file to read from.
sOption (str) – The option in the configuration file to need reading.
- Returns
The value of the specified parameter in the section.
- Return type
str
mli.lib.gbif_parser module¶
The module provides a means to get information from a taxon, as specified in gbif .
- function:
gbif_get_children(sGBIF_id) gbif_get_id_from_gbif(sName, sLevel=’species’) gbif_get_id(oConnector, sName, sLevelEn) gbif_get_many(sURL, sGBIF_id) gbif_get_status_id(oConnector, sStatus) gbif_get_synonyms(sGBIF_id) gbif_get_taxon_info(sGBIF_id, sLevel=’species’) gbif_get_update(oConnector, iLevel) gbif_is_lichen(dTaxon) gbif_parser_name(sString) gbif_parser_taxon(dData) gbif_parsing_answer(oConnector, lAnswer, sType) gbif_parsing_species(oConnector, dAnswer) gbif_save_species(oConnector, dAnswer, iLevel) gbif_update(oConnector, dAnswer, iTaxonID)
- mli.lib.gbif_parser.gbif_get_children(sGBIF_id)[source]¶
- Generates an api link for obtaining children from the server and
returns a normalized response.
- Parameters
sGBIF_id – A key ID of taxon in gbif.
- Type
str
- Returns
A normalized response.
- Return type
list[dict[str, bool, str, str, str, str, str, int]|None]
- mli.lib.gbif_parser.gbif_get_id(oConnector, sName, sLevelEn)[source]¶
Checks if the taxon’s id exists in the database, and if it doesn’t, it gets it from the site.
- Parameters
oConnector (SQL) – An instance of the sqlite database api class.
sName (str) – A name of the taxon.
sLevelEn (str) – A name of the taxon rank in english language.
- Returns
ID taxon in gbif.
- Return type
int
- mli.lib.gbif_parser.gbif_get_id_from_gbif(sName, sLevel='species')[source]¶
Looks up a taxon name in the gbif database.
- Parameters
sName (str) – The name of a taxon.
sLevel (str) – The rank of a taxon.
- Returns
Key ID of the taxon in gbif.
- Return type
str
- mli.lib.gbif_parser.gbif_get_many(sURL, sGBIF_id)[source]¶
- Generates an api link for obtaining children from the server and
returns a normalized response.
- Parameters
sURL (str) – An URL for sending to gbif server.
sGBIF_id – A key ID of taxon in gbif.
- Type
str
- Returns
A normalized response.
- Return type
list[dict[str, bool, str, str, str, str, str, int]|None]
- mli.lib.gbif_parser.gbif_get_status_id(oConnector, sStatus)[source]¶
Returns the status id from the database.
- Parameters
oConnector (SQL) – An instance of the sqlite database api class.
sStatus (str) – The name of the status, as is customary in gbif.
- Returns
the status ID.
- Return type
int or bool
- mli.lib.gbif_parser.gbif_get_synonyms(sGBIF_id)[source]¶
- Generates an api link for obtaining synonyms from the server and
returns a normalized response.
- Parameters
sGBIF_id – A key ID of taxon in gbif.
- Type
str
- Returns
A normalized response.
- Return type
list[dict[str, bool, str, str, str, str, str, int]|None]
- mli.lib.gbif_parser.gbif_get_taxon_info(sGBIF_id, sLevel='species')[source]¶
General information about the taxon by its ID.
- Parameters
sGBIF_id – A key ID of taxon in gbif.
sLevel (str) – A level of the taxon.
- Type
str
- Returns
A normalized dictionary of taxon information.
- Return type
dict[str, bool, str, str, str, str, str, int]|None
- mli.lib.gbif_parser.gbif_get_update(oConnector, iLevel)[source]¶
Allows you to select all names from the database by level, start getting data from gbif and enter information into the database.
- Parameters
oConnector (SQL) – An instance of the sqlite database api class.
iLevel (int) – ID of a rank in database.
- Returns
None
- mli.lib.gbif_parser.gbif_is_lichen(dTaxon)[source]¶
Checks if the taxon is a lichen.
- Parameters
dTaxon (dict) – Filed ‘result’ of answer from gbif.
- Returns
True if taxon is lichen, and False if opposite.
- Return type
bool
- mli.lib.gbif_parser.gbif_parser_name(sString)[source]¶
Parsing a name of the taxon separating the canonical name from the name of the author and year, if possible.
- Parameters
sString (str) – A string with a name of the taxon.
- Returns
A canonical name, an author name and a naming year of the taxon.
- Return type
list[str, str, int]
- mli.lib.gbif_parser.gbif_parser_taxon(dData)[source]¶
- Selects from the server response the information necessary for further
processing.
- Parameters
dData (dict) – An answer of server.
- Returns
Selection from the server response with the necessary information.
- Return type
dict[str, bool, str, str, str, str, str, int]|None
- mli.lib.gbif_parser.gbif_parsing_answer(oConnector, lAnswer, sType)[source]¶
Parses the response a list of dictionaries with taxon information.
- Parameters
oConnector (SQL) – An instance of the sqlite database api class.
lAnswer (list[dict[str, bool, str, str, str, str, str, int]|None]) – A list of dictionaries with taxon information.
sType (str) – The first word to output to the string. It makes sense to indicate either ‘Synonym’ or ‘Children’.
- Returns
None
- mli.lib.gbif_parser.gbif_parsing_species(oConnector, dAnswer)[source]¶
Specifies whether to make changes to the database.
- Parameters
oConnector (SQL) – An instance of the sqlite database api class.
dAnswer (dict[str, bool, str, str, str, str, str, int]) – A dictionary with information about the taxon.
- Returns
None
- mli.lib.gbif_parser.gbif_save_species(oConnector, dAnswer, iLevel)[source]¶
Saving information about the taxon in database.
- Parameters
oConnector (SQL) – An instance of the sqlite database api class.
dAnswer (dict[str, bool, str, str, str, str, str, int]) – A dictionary with information about the taxon.
iLevel (int) – The level’s ID in database.
- Returns
The taxon’s ID in database.
- Return type
int or bool
- mli.lib.gbif_parser.gbif_update(oConnector, dAnswer, iTaxonID)[source]¶
Updates information in the database about the author and naming year.
- Parameters
oConnector (SQL) – An instance of the sqlite database api class.
dAnswer (dict[str, bool, str, str, str, str, str, int]) – A dictionary with information about the taxon.
iTaxonID (int) – The taxon’s ID in database.
- Returns
None
mli.lib.log module¶
mli.lib.sql module¶
The module provides an API for working with the database. It creates a multi-level API that can be used in other modules to create requests using a minimum of transmitted data.
- Function:
get_columns(sColumns, sConj=’AND’)
- Class:
SQL
- Using:
Foo = SQL(_DataBaseFile_)
- class mli.lib.sql.SQL(sFileDB)[source]¶
Bases:
object
Provides interface for working with database from others scripts.
- Methods
- # Standard methods.
__init__ – Method initializes a cursor of sqlite database.
__del__ – Method closes the cursor of sqlite database.
- # Low level methods.
export_db – Method exports from db to sql script.
execute_script – Method imports from slq script to db.
execute_query – Method execute sql_search query.
insert_row – Method inserts a record in the database table.
delete_row – Method deletes a row from the table.
update – Method updates value(s) in record of the database table.
select – Method does selection from the table.
- # Average level API.
sql_get_id: Finds id of the row by value(s) of table column(s).
sql_get_all: Method gets all records in database table.
sql_count: Method counts number of records in database table.
sql_table_clean: Method cleans up the table.
- delete_row(sTable, sColumns=None, tValues=None)[source]¶
Deletes row in the database table by value(s).
- Parameters
sTable (str or None) – A table as string in where need to delete row.
sColumns (str or None) – Column(s) where the value(s) will be found. (by default, None).
tValues (tuple or list) – value(s) as tuple for search of rows. (by default, None).
- Returns
True if the deletion is successful, otherwise False.
- Return type
bool
- execute_query(sSQL, tValues=None)[source]¶
Method executes sql script.
- Parameters
sSQL (str) – SQL query.
tValues (tuple or list or None) – value(s) that need to safe inserting into query (by default, None).
- Returns
Cursor or bool – True if script execution is successful, otherwise False.
- execute_script(sSQL)[source]¶
Method executes sql script.
The main difference from the method is the ability to execute several commands at the same time. For example, using this method, you can restore the database from sql dump.
- Parameters
sSQL (str) – SQL Script as string.
- Returns
True if script execution is successful, otherwise False.
- Return type
bool
- insert_row(sTable, sColumns, tValues)[source]¶
Inserts a record in the database table.
- Parameters
sTable (str) – Table name as string.
sColumns (str) – Columns names of the table by where needs inserting.
tValues (tuple or list) – Value(s) as tuple for inserting.
- Returns
ID of an inserted row if the insert was successful. Otherwise, False.
- Return type
str or bool
- select(sTable, sGet, sWhere='', tValues='', sConj='', sFunc='')[source]¶
Looks for row by value(s) in table column(s).
- Parameters
sTable (str) – Table name as string.
sGet (str) – Name of the column of the table, which will be returned.
sWhere (str or None) – Names of columns of the table, by which to search (by default, empty).
sConj (str or None) – The one from ‘AND’ or ‘OR’ operator condition. By default, is used ‘AND’.
tValues (tuple or list or None) – Value(s) as tuple for search (by default, empty).
sFunc (str or None) – Function name of sqlite, which need to apply (by default, empty). Note: Now, you can use only two sqlite functions: Count and DISTINCT.
- Returns
Cursor or bool – Cursor object within rows that was found, or False, if the row not found.
- sql_count(sTable)[source]¶
Counts number of records in database table.
- Parameters
sTable (str) – Table name as string where records should be count.
- Returns
Number of found records.
- Return type
int or bool
- sql_get_all(sTable)[source]¶
Gets all records in database table.
- Parameters
sTable (str) – Table name as string where records should be received.
- Returns
Tuple of all rows of table.
- Return type
tuple or bool
- sql_get_values(sTable, sID, sWhere, tValues, sConj='')[source]¶
Looks for ID of the row by value(s) of table column(s).
- Parameters
sTable (str) – Table name as string.
sID (str) – Name of the column of the table by which to search.
sWhere (str) – Names of columns of the table by which to search.
tValues (tuple or list) – Value(s) as tuple for search.
sConj (str or None) – The one from ‘AND’ or ‘OR’ operator condition. By default, is used ‘AND’.
- Returns
ID as Number in the row cell, or 0, if the row not found.
- Return type
list or bool
- sql_table_clean(lTable)[source]¶
Cleans up the table.
- Parameters
lTable (tuple or list) – Table names as list or tuple of string, or table name as string where cleaning is need to do.
- Returns
True, if execution is successful. Otherwise, False. Note: False is returned even if cleaning the last table in the tuple was not successful.
- Return type
bool
- update(sTable, sSetUpdate, sWhereUpdate, tValues)[source]¶
Updates value(s) in the record of the database table.
- Parameters
sTable (str) – A Table as string where update is need to do.
sSetUpdate (str) – Column(s) where the value are writen.
sWhereUpdate (str) – A column where values correspond to the required.
tValues (tuple or list) – value(s) as tuple for search corresponding rows.
- Returns
True if the insert was successful, otherwise False.
- Return type
bool
- mli.lib.sql.check_connect_db(oConnector, sBasePath, sDBDir)[source]¶
- Checks for the existence of a database and if it does not find it, then
creates it with default values.
- Parameters
oConnector (SQL) – Instance attribute of SQL.
sBasePath (str) – A path of the executed script.
sDBDir (str) – A dir when database is by default.
- Returns
None
- mli.lib.sql.get_columns(sColumns, sConj='AND')[source]¶
The function of parsing a string, accepts a list of table columns separated by commas and returns this list with ‘=? AND’ or ‘=? OR’ as separator.
- Parameters
sColumns (str) – A string with a list of table columns separated by commas.
sConj (str or None) – The one from ‘AND’ or ‘OR’ operator condition. By default, is used ‘AND’.
- Returns
The string with a list of table columns separated by ‘=? AND’ or ‘=? OR’.
- Return type
str
- mli.lib.sql.get_increase_value(sColumns, tValues)[source]¶
- Checks counting elements of values, and if them fewer,
then makes them equal.
- Note:
In the rison that tuple can’t be multiplied on flot, the process of increasing the tuple becomes somewhat resource-intensive. So, tValues should be consisting of one element.
- Parameters
sColumns (str) – Colum(s) in query.
tValues (tuple or list) – Values should be specified in the request.
- Returns
A tuple with values, which equal to sColumns.
- Return type
list
mli.lib.str module¶
The module contains a collection of functions for solving routine tasks with strings.
- mli.lib.str.str_get_file_patch(sDir, sFile)[source]¶
Concatenates file path and file name based on OS rules.
- Parameters
sDir – String with a patch to a file.
sFile – String with a filename.
- Returns
Patch to file based on OS rules.
- mli.lib.str.str_get_html_name(sCanonicalName, sAuthor='')[source]¶
- Inserts html tags into the taxon scientific name so that the canonical
name is written in italics.
- Parameters
sCanonicalName (str) – A canonical name of the taxon.
sAuthor (str) – Author(s) of the taxon.
- Returns
Formatted string.
- Return type
str
- mli.lib.str.str_get_path(sFullFile)[source]¶
Splits a path to path and file name.
- Parameters
sFullFile (str) – Path with filename.
- Returns
The path to file.
- Return type
str
- mli.lib.str.str_sep_comma(sString)[source]¶
Separates a string by comma to list.
- Parameters
sString (str) – A string that needs to separate.
- Returns
A separated string by comma.
- Return type
list or None
- mli.lib.str.str_sep_dot(sString)[source]¶
Separates a string by dot to list.
- Parameters
sString (str) – A string that needs to separate.
- Returns
A separated string by dot.
- Return type
list or None
- mli.lib.str.str_sep_name_taxon(sString)[source]¶
- Splits the string into taxon name and author, taking a string of the
form ‘(rank) Taxon_name, authors’. It is permissible to indicate authors separated by commas, in brackets, using the ‘&’ symbol.
- Parameters
sString (str) – A string that needed to separate.
- Returns
A canonical form of taxon name and a string with the authors. Returns empty instead of author if no author was specified in the string.
- Return type
list[str, str|None]