Skip to main content

Data writers with optional timestamps for logging of events

Project description

event_data_logging

Save events to json or csv files. This package comes in 4 flavors:

  • CSVWriter
  • StampedCSVWriter
  • JSONWriter
  • StampedJSONWriter

All of these will check if the given filename already exists and add a numerical suffix if it does to give a unused new filename. Additionally, if a filename that includes directories is used, the directories will be created if they don't exist, if possible.

The StampedWriters will add a leading entry with the current epoch timestamp to each event or line saved. The default format is in seconds with fraction of a second as decimal numbers. Alternatively you can save the timestamps as nanoseconds instead.

Readme content:

Install

Install from PYPI

pip install event_data_logging

Alternatively, install from github

pip install git+https://git@github.com/maimonlab/event_data_logging.git

Usage

JSON example without timestamps

from event_data_logging.json_writer import JSONWriter
test_events = [
    {"bar_color": [1, 2, 3]},
    {"bar_width_degrees": 10},
    {"example_float": 0.1},
]
filename = "data/json_data.json"
writer = JSONWriter(filename)
for event in test_events:
    writer.save_event(event)

This would create the file data/json_data.json with content:

[
{"bar_color": [1, 2, 3]},
{"bar_width_degrees": 10},
{"example_float": 0.1}
]

JSON example with timestamp

from event_data_logging.json_writer import StampedJSONWriter
test_events = [
    {"bar_color": [1, 2, 3]},
    {"bar_width_degrees": 10},
    {"example_float": 0.1},
]
filename = "data/stamped_json_data.json"
writer = StampedJSONWriter(filename)
for event in test_events:
    writer.save_event(event)

This would create the file data/stamped_json_data.json with content:

[
{"timestamp": 1661201947.0682852, "bar_color": [1, 2, 3]},
{"timestamp": 1661201947.0683577, "bar_width_degrees": 10},
{"timestamp": 1661201947.0684075, "example_float": 0.1}
]

CSV example with nanosecond timestamp

from event_data_logging.csv_writer import StampedCSVWriter, TimestampModes
filename = "data/csv_data.csv"
xyz_header = ["x", "y", "z"]
csv_writer = StampedCSVWriter(
    filename, header=xyz_header, timestamp_mode=TimestampModes.NANOSECONDS
)
for i in range(3):
    line = [
        str(10 * i + 1),
        str(10 * i + 2),
        str(10 * i + 3),
    ]
    csv_writer.save_line(line)

This will give the file data/csv_data.csv with the following content:

timestamp,x,y,z
1661110000123456789,1,2,3
1661110001123456789,11,12,13
1661110002123456789,21,22,23

Developing

To install the testing dependencies, install with

pip install -e .[test]

You can run the tests with pytest, and check the coverage. To do so, use the following commands:

coverage run -m pytest

The coverage report prints to the terminal with:

coverage report

This report shows how much of all the code is actually run during the test.

Uploading to pypi

Build the distribution

python3 -m build

Upload the distribution to pypi

python3 -m twine upload --repository pypi dist/* --verbose

ros2_message_handling using outside dependencies

In our lab, this package is mainly used to save ros2 data, and thus turning ros2 messages to dictionaries is very common. The ros2_message_handling module therefore requires some ros2 packages to be installed, and is outside of the scope of most uses.

The test_ros2_message_handling.py will thus fail in environments without ROS2. _We will likely remove this module before the stable release. the ros2_message handling should be integrated in it's own ros packages

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

event_data_logging-0.1.4.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

event_data_logging-0.1.4-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file event_data_logging-0.1.4.tar.gz.

File metadata

  • Download URL: event_data_logging-0.1.4.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for event_data_logging-0.1.4.tar.gz
Algorithm Hash digest
SHA256 083a316b3eb4b1bde8083dfde03da7098ab5b53f8e7bf94bad68273ffd76782f
MD5 4ccf9d3ea56017cd88b6c1dd6e1f78d9
BLAKE2b-256 a31aee97ba0e7f6df590c4b665ac47abce269b4de221d7b16eda21e5014e3f92

See more details on using hashes here.

File details

Details for the file event_data_logging-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for event_data_logging-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1a54b3274b31f4cbd97897ec0ef89f052286ee208544da37bdb9c8d0831f6b5d
MD5 71fa7c160998212730497698f959062d
BLAKE2b-256 d85cf1a4b454859f85a6a856fbf2ca5a8a3ad53e3e84db0cb1f8a62d1b241aef

See more details on using hashes here.

Supported by

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