Skip to main content

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']

Metadata

Release files for pinform 0.9.1

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

Source distribution (sdist)

Source distribution for pinform 0.9.1
File Size Uploaded Provenance
pinform-0.9.1.tar.gz 11.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pinform 0.9.1
File Interpreter ABI Platform
pinform-0.9.1-py3-none-any.whl Python 3 none any Details

Total release size: 24.1 kB

Release files / pinform-0.9.1.tar.gz

Download URL pinform-0.9.1.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
de97b5c584f85284fc0cad6443d4e036012b2f990a8fd72ac52b71f1ff69de2b
BLAKE2b-256 checksum
How to use checksums
f43514017d2c8a1e996d96a64612f41d7ec060e6001478725adfb4f78cecf2f0
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / pinform-0.9.1-py3-none-any.whl

Download URL pinform-0.9.1-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
df2cc918fbdd9c747c9ce5dbbb8f8f6ee12bc7fd53ae60307ec4b716eb0053fc
BLAKE2b-256 checksum
How to use checksums
da26f80cd06dc8eaddefe0c0795d24151434a68b2d1d5254fd0ba88f61ef4925
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for pinform 0.9.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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