Skip to content

Commit

Permalink
core: fulfill requirements for PR
Browse files Browse the repository at this point in the history
  • Loading branch information
strom-und-spiele committed Feb 25, 2022
1 parent 95f6ba0 commit a5b388a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [import_data_set_sync_batching.py](import_data_set_sync_batching.py) - How to use [RxPY](https://rxpy.readthedocs.io/en/latest/) to prepare batches for synchronous write into InfluxDB
- [write_api_callbacks.py](write_api_callbacks.py) - How to handle batch events
- [write_structured_data.py](write_structured_data.py) - How to write structured data - [NamedTuple](https://docs.python.org/3/library/collections.html#collections.namedtuple), [Data Classes](https://docs.python.org/3/library/dataclasses.html) - (_requires Python v3.8+_)
- [logging_handler.py](logging_handler.py) - How to set up a python native logging handler that writes to influx.

## Queries
- [query.py](query.py) - How to query data into `FluxTable`s, `Stream` and `CSV`
Expand Down
13 changes: 10 additions & 3 deletions examples/logging_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import time

from influxdb_client import InfluxLoggingHandler, WritePrecision, Point
from influxdb_client.client.write_api import SYNCHRONOUS

DATA_LOGGER_NAME = '…'

Expand All @@ -20,9 +21,10 @@ def setup_logger():
This can happen in your core module.
"""
influx_logging_handler = InfluxLoggingHandler(url="…", token="…", org="…", bucket="…",
client_args={'arg1': '…'},
write_api_args={'arg': '…'})
influx_logging_handler = InfluxLoggingHandler(
url="http://localhost:8086", token="my-token", org="my-org", bucket="my-bucket",
client_args={'timeout': 30_000}, # optional configuration of the client
write_api_args={'write_options': SYNCHRONOUS}) # optional configuration of the write api
influx_logging_handler.setLevel(logging.DEBUG)

data_logger = logging.getLogger(DATA_LOGGER_NAME)
Expand All @@ -45,3 +47,8 @@ def use_logger():
.field('temperature', 25.3)
.time(datetime.datetime.utcnow(), WritePrecision.MS)
)


if __name__ == "__main__":
setup_logger()
use_logger()
15 changes: 2 additions & 13 deletions influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class Point(object):
Ref: http://bit.ly/influxdata-point
"""

__str___rep = None

@staticmethod
def measurement(measurement):
"""Create a new Point with specified measurement name."""
Expand Down Expand Up @@ -201,17 +199,8 @@ def set_str_rep(cls, rep_function):
cls.__str___rep = rep_function

def __str__(self):
"""
Create string representation of this Point.
Can be set via `Point.set_str_rep`. Defaults to `to_line_protocol`
Example:
.. code-block:: python
Point.set_str_rep(lambda p: f'{p._name} - {p._tags} - {p._fields} - {p._time}')
"""
if self.__str___rep is None:
return self.to_line_protocol()
return self.__str___rep()
"""Create string representation of this Point."""
return self.to_line_protocol()


def _append_tags(tags):
Expand Down
9 changes: 1 addition & 8 deletions tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ class PointTest(unittest.TestCase):

def test_ToStr(self):
point = Point.measurement("h2o").tag("location", "europe").field("level", 2.2)
expected_str = "h2o,location=europe level=2.2"
expected_str = point.to_line_protocol()
self.assertEqual(expected_str, str(point))

def my_str_rep(p: Point) -> str:
return f'{p._name} - {p._tags} - {p._fields} - {p._time}'

Point.set_str_rep(my_str_rep)

self.assertEqual(my_str_rep(point), str(point))

def test_MeasurementEscape(self):
point = Point.measurement("h2 o").tag("location", "europe").tag("", "warn").field("level", 2)
self.assertEqual(point.to_line_protocol(), "h2\\ o,location=europe level=2i")
Expand Down

0 comments on commit a5b388a

Please sign in to comment.