Skip to main content

Python InfluxDB ORM (OSTM)

Project description

alt text alt text

Pinform: An InfluxDB ORM (OTSM) for Python

PInfORM (Python InfluxDB ORM) is an Object/TimeSeries Mapping layer for connecting to InfluxDB in python.

Tested with:

  • Python 3.6+
  • InfluxDB 1.7.3 - 1.7.9

Use the following command to install using pip:

pip install pinform

Usage example

Create Measurement Models

First, create your measurement model in

from pinform import Measurement
from pinform.fields import FloatField
from pinform.tags import Tag

class OHLC(Measurement):
  class Meta:
    measurement_name = 'ohlc'

  symbol = Tag(null=False)
  open = FloatField(null=False)
  high = FloatField(null=False)
  low = FloatField(null=False)
  close = FloatField(null=False)

Create Measurement Instance

First, create your measurement model in

today_ohlc = OHLC(time_point=datetime.datetime.now(), symbol='AAPL', open=80.2, high=86.0, low=78.9, close=81.25)

Create InfluxClient

Then you must create an instance of InfluxClient to connect to database:

from pinform.client import InfluxClient

cli = InfluxClient(host="localhost", port=8086, database_name="defaultdb")

If the database needs authentication, use:

cli = InfluxClient(host="localhost", port=8086, database_name="defaultdb", username='your db username', password='your db password')

Save and Retrieve Points

To save data in database, use save_points or save_dataframe functions of InfluxClient:

ohlc = OHLC(time_point=datetime.datetime.now(), symbol='AAPL', open=100.6, high=102.5, low=90.4, close=94.2)
cli.save_points([ohlc])

To retrieve data from database, use load_points or load_points_as_dataframe functions of InfluxClient:

ohlc_points = cli.load_points(OHLC, {'symbol':'AAPL'})

Get Distinct Tag Values

To get distinct tag values from all measurements, use get_distinct_existing_tag_values function from InfluxClient:

tag_values = cli.get_distinct_existing_tag_values('symbol')

To get distinct tag values from an specific measurements,pass measurement to the previous function:

tag_values = cli.get_distinct_existing_tag_values('symbol', measurement=OHLC)

Fields

It's possible to use IntegerField, FloatField, BooleanField and StringField to save field values in InfluxDB. There are four other types of fields which help with storing fields with specific integer or string values. To create a field with multiple choice integer values, use MultipleChoiceIntegerField or EnumIntegerField classes. To create a field with multiple choice string values, use MultipleChoiceStringField or EnumStringField classes.

Example for MultipleChoiceStringField:

from pinform.fields import MultipleChoiceStringField

class WeatherInfo(Measurement):
  class Meta:
    measurement_name = 'weather_info'

  condition = MultipleChoiceStringField(options=['sunny','cloudy','rainy'], null=False)

Example for EnumStringField:

from enum import Enum
from pinform.fields import EnumStringField

class WeatherCondition(Enum):
  SUNNY = 'sunny'
  CLOUDY = 'cloudy'
  RAINY = 'rainy'


class WeatherInfo(Measurement):
  class Meta:
    measurement_name = 'weather_info'

  condition = EnumStringField(enum=WeatherCondition, null=False)

Advanced usage

Dynamic measurement names

It is possible to use 'MeasurementNameComponent's in measurement name wrapped in parenthesis, their value is replaced in the measurement name at runtime.

from pinform import MeasurementNameComponent

class OHLC(Measurement):
  class Meta:
    measurement_name = 'ohlc_(symbol)'

  symbol = MeasurementNameComponent(name='symbol')
  ...

Query Field and Pandas Series

Use get_fields_as_series function from InfluxClient to get fields of specific measurement class as Pandas Series. It's also possible to aggregate data and group by time. This function returnes a dict with aggregated field names as keys and pandas series as values.

from pinform.client import AggregationMode

series_dict = cli.get_fields_as_series(OHLC, 
                field_aggregations={'close': [AggregationMode.MEAN, AggregationMode.STDDEV]},
                tags={'symbol': 'AAPL'},
                time_range=(start_datetime, end_datetime),
                group_by_time_interval='10d')
mean_close_series = series_dict['mean_close']
stddev_close_series = series_dict['stddev_close']

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

pinform-0.9.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pinform-0.9.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file pinform-0.9.1.tar.gz.

File metadata

  • Download URL: pinform-0.9.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for pinform-0.9.1.tar.gz
Algorithm Hash digest
SHA256 de97b5c584f85284fc0cad6443d4e036012b2f990a8fd72ac52b71f1ff69de2b
MD5 3be277f1992034e63e9f9e45da562bcb
BLAKE2b-256 f43514017d2c8a1e996d96a64612f41d7ec060e6001478725adfb4f78cecf2f0

See more details on using hashes here.

File details

Details for the file pinform-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: pinform-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for pinform-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df2cc918fbdd9c747c9ce5dbbb8f8f6ee12bc7fd53ae60307ec4b716eb0053fc
MD5 ce6526e0c70f6b1fb213ed27f7bd0865
BLAKE2b-256 da26f80cd06dc8eaddefe0c0795d24151434a68b2d1d5254fd0ba88f61ef4925

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