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

SQLite

Wrapper for connecting to and querying SQLite databases

Functions

ispc(→ bool)

Check if the current platform is Windows

isnonemptydir(→ bool)

Check if a directory is non-empty

isnonemptyfile(→ bool)

Check if a file is non-empty

slugify(→ str)

Convert a string to a slug

downloadurl(, overwrite, progress)

Download and save file from a given URL

iohead(→ None)

Print the first n rows of a text file

system(→ str)

Run a system command; allows you to redirect stdout and stderr.

infoml.utils.ispc() bool[source]

Check if the current platform is Windows

infoml.utils.isnonemptydir(path: Path | str) bool[source]

Check if a directory is non-empty

infoml.utils.isnonemptyfile(path: Path | str) bool[source]

Check if a file is non-empty

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

execute(query: str, \*args, \*\*kwargs) sqlite3.Cursor[source]

Execute a query on the database

select(query: str, \*args, \*\*kwargs) list[sqlite3.Row][source]

Execute a query on the database and return the results as a DataFrame

insert(table: str, data: dict, \*\*kwargs) None[source]

Insert data into a table

is_table(table: str) bool[source]

Check if a table exists in the database

drop(table: str) None[source]

Drop a table from the database

tables() dict[source]

Return a list of tables in the database and their schema

close() None[source]

Close the connection to the database

__enter__()[source]

Enter context manager

__exit__(exc_type, exc_value, traceback)[source]

Exit context manager

__repr__() str[source]

Return string representation of SQLite class

__str__() str[source]

Return string representation of SQLite class

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")
tables() dict[source]

List all tables in the database and their schema

Returns

Dictionary of table names and their schema

Return type

dict

Examples

>>> db = SQLite("test.db")
>>> db.tables()
{'test': 'CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)'}
close() None[source]

Close the database connection

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