Skip to main content

A database adapter to access MobilityDB from Python

Project description

python-mobilitydb

python-mobilitydb is a database adapter to access MobilityDB from Python. It supports both the psycopg2 and the asyncpg adapters for PostgreSQL and uses the postgis adapter for PostGIS.

Install

pip install python-mobilitydb

Requirements

  • Python >= 3.0
  • MobilityDB

Basic Usage

Using the psycopg2 adapter for PostgreSQL

import psycopg2
from postgis.psycopg import register
from mobilitydb.psycopg import register

connectionObject = None

try:
    # Set the connection parameters to PostgreSQL
    connection = psycopg2.connect(host='localhost', database='test', user='user', password='pw')
    connection.autocommit = True

    # Register MobilityDB data types
    register(connection)

    # Open a cursor to perform database operations
    cursor = connection.cursor()

    # Query the database and obtain data as Python objects
    select_query = "SELECT * FROM tbl_tfloatseq ORDER BY k LIMIT 10"
    cursor.execute(select_query)
    rows = cursor.fetchall()

    # Print the obtained rows and call a method on the instances
    for row in rows:
        print("key =", row[0])
        print("tfloatseq =", row[1])
        if not row[1]:
            print("")
        else:
            print("startTimestamp =", row[1].startTimestamp(), "\n")

except (Exception, psycopg2.Error) as error:
    print("Error while connecting to PostgreSQL", error)

finally:
    # Close the connection
    if connectionObject:
        connectionObject.close()

Using the asyncg adapter for PostgreSQL

import asyncio
import asyncpg
from mobilitydb.asyncpg import register


async def run():
    # Connect to an existing database
    connection = await asyncpg.connect(host='localhost', database='test', user='user', password='pw')

    try:
        # Register MobilityDB data types
        await register(connection)

        # Query the database and obtain data as Python objects
        select_query = "SELECT * FROM tbl_tgeompointseq ORDER BY k LIMIT 10"
        rows = await connection.fetch(select_query)

        # Print the obtained rows and call a method on the instances
        for row in rows:
            print("key =", row[0])
            print("tgeompointseq =", row[1])
            if not row[1]:
                print("")
            else:
                print("startTimestamp =", row[1].startTimestamp(), "\n")
    finally:
        # Close the connection
        await connection.close()

# Launch the process
loop = asyncio.get_event_loop()
loop.run_until_complete(run())

Contributing

Issues and Pull Requests are welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-mobilitydb-0.1.0.tar.gz (39.3 kB view details)

Uploaded Source

File details

Details for the file python-mobilitydb-0.1.0.tar.gz.

File metadata

  • Download URL: python-mobilitydb-0.1.0.tar.gz
  • Upload date:
  • Size: 39.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.9

File hashes

Hashes for python-mobilitydb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 33ca5ca1afc59be2b87cbc9548b4675b3bc0e30f9dcd7fd977c5019f1b91ceb8
MD5 de2f771675ed40aaf289aeb2a227ac09
BLAKE2b-256 019ea2846fe064dc2f7f0a753d0c14562a5dc114d604c1670f226f1a12b048b1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page