infoml.utils
This module contains utility functions for the infoml package. Some of these functions are modified from the bmes package by Dr. Ahmet Sacan.
Module Contents
Classes
Wrapper for connecting to and querying SQLite databases |
Functions
|
Check if the current platform is Windows |
|
Check if a directory is non-empty |
|
Check if a file is non-empty |
|
Convert a string to a slug |
|
Download and save file from a given URL |
|
Print the first n rows of a text file |
|
Run a system command; allows you to redirect stdout and stderr. |
- infoml.utils.slugify(text: str, allow_unicode: bool = False) str[source]
Convert a string to a slug
Based on Django’s slugify function 1. This function converts a string to a slug. A slug is a string that contains only letters, numbers, underscores or hyphens. It is typically used to generate URL-friendly strings.
- Parameters
text (str) – The string to be sluggified
allow_unicode (bool, optional) – Should unicode characters be permitted, by default False
- Returns
The sluggified string
- Return type
str
- Raises
AttributeError – The input must be a string
Examples
>>> slugify("Jack & Jill like numbers 1,2,3 and 4 and silly characters ?%.$!/") 'jack-jill-like-numbers-123-and-4-and-silly-characters'
References
- 1
Django. (n.d.). Django/text.py at main · Django/Django. GitHub. Retrieved January 2, 2023, from https://github.com/django/django/blob/main/django/utils/text.py
- infoml.utils.downloadurl(url: str, file: str | Path = CONFIG.tempdir(), overwrite: bool = False, progress: bool = True) pathlib.Path[source]
Download and save file from a given URL
- Parameters
url (str) – The URL to download the file from
file (str, optional) – Path to file (or directory) where downloaded file will be stored, by default the file will be saved to a temporary directory
overwrite (bool, optional) – Should existing files be overwritten, by default False
progress (bool, optional) – Should a progress bar be displayed, by default True
- Returns
Path to downloaded file
- Return type
Path
- Raises
FileNotFoundError – If the file does not exist
FileExistsError – If the file already exists and overwrite is False
ConnectionError – If the URL is not valid
Examples
>>> downloadurl("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png") PosixPath('/tmp/googlelogo_color_272x92dp.png')
- class infoml.utils.SQLite(file: str | Path, quiet: bool = False, **kwargs)[source]
Wrapper for connecting to and querying SQLite databases
- file
Path to SQLite database file
- Type
Path
- conn
Connection to SQLite database
- Type
sqlite3.Connection
- select(query: str, \*args, \*\*kwargs) list[sqlite3.Row][source]
Execute a query on the database and return the results as a DataFrame
- execute(query: str, *args, **kwargs) sqlite3.Cursor | DataFrame | None[source]
Execute a query
- Parameters
query (str) – Query to execute
- Returns
Cursor object
- Return type
sqlite3.Cursor
- Raises
AttributeError – If the input is not a string
Examples
>>> db = SQLite("test.db") >>> db.execute("CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, name TEXT)") <sqlite3.Cursor object at 0x7f8b8c0b0e00> >>> db.execute("INSERT INTO test (name) VALUES ('John')") <sqlite3.Cursor object at 0x7f8b8c0b0e00> >>> db.execute("INSERT INTO test (name) VALUES ('Jane')") <sqlite3.Cursor object at 0x7f8b8c0b0e00> >>> db.close()
- select(query: str, *args, **kwargs) pandas.DataFrame[source]
Execute a SELECT query and return the results as a DataFrame
- Parameters
query (str) – Query to execute
- Returns
Results of query
- Return type
DataFrame
- Raises
AttributeError – If the input is not a string
Examples
>>> db = SQLite("test.db") >>> db.select("SELECT * FROM test") id name age 0 1 John 30 1 2 Jane 25
- insert(table: str, data: dict, **kwargs) None[source]
Insert data into a table
- Parameters
table (str) – Name of table
data (dict) – Data to insert
- Raises
AttributeError – If the table does not exist If the dictionary values are not lists of the same length If a database column is missing from the dictionary
Examples
>>> db = SQLite("test.db") >>> data = {'name': ['John', 'James', 'Rose', 'Jane'], 'age': [30, 25, 60, 45]} >>> db.insert("test", data) >>> db.close()
- is_table(table: str) bool[source]
Check if a table exists in the database
- Parameters
table (str) – Name of table
- Returns
True if table exists, False otherwise
- Return type
bool
Examples
>>> db = SQLite("test.db") >>> db.is_table("test") True >>> db.is_table("foo") False
- drop(table: str) None[source]
Drop a table from the database
- Parameters
table (str) – Name of table
Examples
>>> db = SQLite("test.db") >>> db.drop("test")
- infoml.utils.iohead(file: str, n: int = 5) None[source]
Print the first n rows of a text file
- Parameters
file (str) – Name of file
n (int, optional) – Number of rows to print
- infoml.utils.system(command: str, stdout: str | None = None, stderr: str | None = None, quiet: bool = False, *args, **kwargs) str[source]
Run a system command; allows you to redirect stdout and stderr.
- Parameters
command (str) – Command to run
stdout (str, optional) – File to redirect stdout to, by default ‘’
stderr (str, optional) – File to redirect stderr to, by default ‘’
quiet (bool, optional) – Suppress output, by default False
- Returns
Output from the command
- Return type
str