Skip to main content

banner

A lightweight async Python library for MySQL queries and modeling.

Installation

The latest version of Targa can be downloaded and installed using pip.

pip install targa

Any requirements (including aiomysql) should be automatically installed for you.

Usage

Connecting to a database

The targa.Database.connect method may be used to connect to an existing MySQL database. This method returns a targa.Database instance that can be used to issue queries.

import asyncio
import targa

async def main():
    database = targa.Database.connect(
        host          = '', # hostname or IP of the database server
        username      = '', # username to connect with
        password      = '', # password to connect with
        database_name = ''  # name of the database instance to connect to
    )

if __name__ == '__main__':
    asyncio.run(main())

Issuing a query

Once a targa.Database is initialized, the query method may be used to execute SQL queries. For example, consider a scenario where the rows in the following persons table need to be read:

id first_name occupation
1 Will Developer
2 John Accountant
3 Sue Engineer

Assuming a database connection has already been established, these rows could be accessed as follows:

import asyncio
import targa

async def main():
    # ... database connection already established

    persons = await database.query('SELECT * FROM persons')

    for person_dict in persons:
        print(person_dict)

if __name__ == '__main__':
    asyncio.run(main())

The query method returns each result row as a dict mapping the column names as keys to the row values. As a result, this program would output:

{'id': 1, 'first_name': 'Will', 'occupation': 'Developer'}
{'id': 2, 'first_name': 'John', 'occupation': 'Accountant'}
{'id': 3, 'first_name': 'Sue', 'occupation': 'Engineer'}

Defining models

Object models in Targa are represented as annotated Python classes that inherit the targa.Model base class. For example, a Person model for the table previously discussed would look like this:

import targa

class Person(targa.Model):
    id: int
    first_name: str
    occupation: str

Once this model is defined, an individual dict returned from querying the persons table could be wrapped as follows:

import asyncio
import targa

async def main():
    # ... database connection already established

    persons = await database.query('SELECT * FROM persons')

    for person_dict in persons:
        print(Person(**person_dict))

if __name__ == '__main__':
    asyncio.run(main())

This program would output the following:

Person(id=1, name='Will', occupation='Developer')
Person(id=2, name='John', occupation='Accountant')
Person(id=3, name='Sue', occupation='Engineer')

The fields of each Targa model are type checked as they are instantiated and may be accessed just like the fields of any other Python class.

Release files for targa 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for targa 1.0.5
File Size Uploaded
targa-1.0.5.tar.gz 9.2 kB Details

Release files / targa-1.0.5.tar.gz

Download URL targa-1.0.5.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
76695a0114f71f37ef10f0b851e3b79439cf210b7bf8d7c6a90fea0027a5d232
BLAKE2b-256 checksum
How to use checksums
665e8d8b66c898948759658398b684f22dac783f04fd79ca580f7294bc1e9fa3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.4

Release history Release notifications | RSS feed

This release

1.0.5 This release

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page