Skip to content

Commit af78574

Browse files
authored
Switch from print to logging (#46)
* refactor: Update linter settings to no longer allow printouts, all output should be controlled through the module-level loggers that will be added * docs: Update link to badge * refactor: Added type hinting to all methods/functions * refactor: Updated more code to pass stricter linting * refactor: Enable pydocstyle linting via ruff * refactor: Turn on a few more linting checks * chore: Remove a pre-commit hook that isn't needed * feat: Switched to using logging instead of prints * refactor: Update code to use imports that work properly in Python 3.8 * docs: Update example docstring
1 parent a0694c8 commit af78574

25 files changed

+743
-433
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
__pycache__
22
*.py[cod]
33
*$py.class
4+
**/logs/**
45

56
# ignore .pyi files
67
*.pyi
@@ -54,6 +55,7 @@ coverage.xml
5455
*.py,cover
5556
.results*/
5657
.hypothesis/
58+
tests/generated_**/*
5759

5860
# Translations
5961
*.mo

.pre-commit-config.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ repos:
6060
rev: 8072181c0f2eab9f2dd8db2eb3b9556d7cd0bd74 # frozen: 1.17.0
6161
hooks:
6262
- id: yamlfix
63-
# TODO: get this working
64-
# - repo: https://github.com/motet-a/jinjalint
65-
# rev: "0.5"
66-
# hooks:
67-
# - id: jinjalint
6863
- repo: https://github.com/thibaudcolas/curlylint
6964
rev: 71adf4d34c290684fd9f94a4d21ac55bcfe640f0 # frozen: v0.13.1
7065
hooks:

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ Things to be included in the next release go here.
2525
### Changed
2626

2727
- Updated the documentation by moving portions from the Basic Usage page to the API docs.
28+
- Switched from using standard `print()` calls to using the `logging` module for all logging in the `tekhsi` package.
29+
- A configuration function provides the ability to set different logging levels for stdout and file logging.
30+
- By default, a log file is created with every debug message logged to it.
31+
32+
### Removed
33+
34+
- _**<span style="color:orange">minor breaking change</span>**_: Removed the `print_with_timestamp()` function since this functionality is now handled by the `logging` module.
35+
- _**<span style="color:orange">minor breaking change</span>**_: Removed the `get_timestamp_string()` function since this functionality is now handled by the `logging` module.
2836

2937
---
3038

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<div markdown="1" class="custom-badge-table">
22

3-
| | |
4-
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
5-
| **Testing** | [![Code testing status](https://github.com/tektronix/TekHSI/actions/workflows/test-code.yml/badge.svg?branch=main)](https://github.com/tektronix/TekHSI/actions/workflows/test-code.yml) [![Docs testing status](https://github.com/tektronix/TekHSI/actions/workflows/test-docs.yml/badge.svg?branch=main)](https://github.com/tektronix/TekHSI/actions/workflows/test-docs.yml) [![Coverage status](https://codecov.io/gh/tektronix/TekHSI/branch/main/graph/badge.svg)](https://codecov.io/gh/tektronix/TekHSI) |
6-
| **Code Quality** | [![CodeQL status](https://github.com/tektronix/TekHSI/actions/workflows/codeql-analysis.yml/badge.svg?branch=main)](https://github.com/tektronix/TekHSI/actions/workflows/codeql-analysis.yml) [![CodeFactor grade](https://www.codefactor.io/repository/github/tektronix/TekHSI/badge)](https://www.codefactor.io/repository/github/tektronix/TekHSI) [![pre-commit status](https://results.pre-commit.ci/badge/github/tektronix/TekHSI/main.svg)](https://results.pre-commit.ci/latest/github/tektronix/TekHSI/main) |
7-
| **Package** | [![PyPI: Package status](https://img.shields.io/pypi/status/TekHSI?logo=pypi)](https://pypi.org/project/TekHSI/) [![PyPI: Latest release version](https://img.shields.io/pypi/v/TekHSI?logo=pypi)](https://pypi.org/project/TekHSI/) [![PyPI: Supported Python versions](https://img.shields.io/pypi/pyversions/TekHSI?logo=python)](https://pypi.org/project/TekHSI/) [![PyPI: Downloads](https://pepy.tech/badge/TekHSI)](https://pepy.tech/project/TekHSI) [![License: Apache 2.0](https://img.shields.io/pypi/l/tekhsi)](https://github.com/tektronix/TekHSI/blob/main/LICENSE.md) [![Package build status](https://github.com/tektronix/TekHSI/actions/workflows/package-build.yml/badge.svg?branch=main)](https://github.com/tektronix/TekHSI/actions/workflows/package-build.yml) [![PyPI upload status](https://github.com/tektronix/TekHSI/actions/workflows/package-release.yml/badge.svg?branch=main)](https://github.com/tektronix/TekHSI/actions/workflows/package-release.yml) |
8-
| **Documentation** | [![ReadtheDocs Status](https://img.shields.io/readthedocs/tekhsi/stable?logo=readthedocs)](https://tekhsi.readthedocs.io) |
9-
| **Code Style** | [![Test style: pytest](https://img.shields.io/badge/test%20style-pytest-blue)](https://github.com/pytest-dev/pytest) [![Code style: ruff](https://img.shields.io/badge/code%20style-ruff-black)](https://docs.astral.sh/ruff/formatter/) [![Docstring style: google](https://img.shields.io/badge/docstring%20style-google-tan)](https://google.github.io/styleguide/pyguide.html) |
10-
| **Linting** | [![pre-commit enabled](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit) [![Docstring formatter: docformatter](https://img.shields.io/badge/docstring%20formatter-docformatter-tan)](https://github.com/PyCQA/docformatter)[![Linter: pylint](https://img.shields.io/badge/linter-pylint-purple)](https://github.com/pylint-dev/pylint) |
3+
| | |
4+
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
5+
| **Testing** | [![Code testing status](https://github.com/tektronix/TekHSI/actions/workflows/test-code.yml/badge.svg?branch=main)](https://github.com/tektronix/TekHSI/actions/workflows/test-code.yml) [![Docs testing status](https://github.com/tektronix/TekHSI/actions/workflows/test-docs.yml/badge.svg?branch=main)](https://github.com/tektronix/TekHSI/actions/workflows/test-docs.yml) [![Coverage status](https://codecov.io/gh/tektronix/TekHSI/branch/main/graph/badge.svg)](https://codecov.io/gh/tektronix/TekHSI) |
6+
| **Code Quality** | [![CodeQL status](https://github.com/tektronix/TekHSI/actions/workflows/codeql-analysis.yml/badge.svg?branch=main)](https://github.com/tektronix/TekHSI/actions/workflows/codeql-analysis.yml) [![CodeFactor grade](https://www.codefactor.io/repository/github/tektronix/TekHSI/badge)](https://www.codefactor.io/repository/github/tektronix/TekHSI) [![pre-commit status](https://results.pre-commit.ci/badge/github/tektronix/TekHSI/main.svg)](https://results.pre-commit.ci/latest/github/tektronix/TekHSI/main) |
7+
| **Package** | [![PyPI: Package status](https://img.shields.io/pypi/status/TekHSI?logo=pypi)](https://pypi.org/project/TekHSI/) [![PyPI: Latest release version](https://img.shields.io/pypi/v/TekHSI?logo=pypi)](https://pypi.org/project/TekHSI/) [![PyPI: Supported Python versions](https://img.shields.io/pypi/pyversions/TekHSI?logo=python)](https://pypi.org/project/TekHSI/) [![PyPI: Downloads](https://static.pepy.tech/badge/TekHSI)](https://pepy.tech/project/TekHSI) [![License: Apache 2.0](https://img.shields.io/pypi/l/tekhsi)](https://github.com/tektronix/TekHSI/blob/main/LICENSE.md) [![Package build status](https://github.com/tektronix/TekHSI/actions/workflows/package-build.yml/badge.svg?branch=main)](https://github.com/tektronix/TekHSI/actions/workflows/package-build.yml) [![PyPI upload status](https://github.com/tektronix/TekHSI/actions/workflows/package-release.yml/badge.svg?branch=main)](https://github.com/tektronix/TekHSI/actions/workflows/package-release.yml) |
8+
| **Documentation** | [![ReadtheDocs Status](https://img.shields.io/readthedocs/tekhsi/stable?logo=readthedocs)](https://tekhsi.readthedocs.io) |
9+
| **Code Style** | [![Test style: pytest](https://img.shields.io/badge/test%20style-pytest-blue)](https://github.com/pytest-dev/pytest) [![Code style: ruff](https://img.shields.io/badge/code%20style-ruff-black)](https://docs.astral.sh/ruff/formatter/) [![Docstring style: google](https://img.shields.io/badge/docstring%20style-google-tan)](https://google.github.io/styleguide/pyguide.html) |
10+
| **Linting** | [![pre-commit enabled](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit) [![Docstring formatter: docformatter](https://img.shields.io/badge/docstring%20formatter-docformatter-tan)](https://github.com/PyCQA/docformatter)[![Linter: pylint](https://img.shields.io/badge/linter-pylint-purple)](https://github.com/pylint-dev/pylint) |
1111

1212
</div>
1313

docs/basic_usage.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,15 @@ several benefits over the traditional data retrieval methods.
134134
# fmt: off
135135
--8<-- "examples/tm_devices_usage.py"
136136
```
137+
138+
## Customize logging and console output
139+
140+
The amount of console output and logging saved to the log file can be customized as needed. This
141+
configuration can be done in the Python code itself as demonstrated here. If no logging is
142+
explicitly configured, the default logging settings will be used (as defined by the
143+
[`configure_logging()`][tekhsi.helpers.logging.configure_logging] function).
144+
145+
```python
146+
# fmt: off
147+
--8<-- "examples/customize_logging.py"
148+
```

docs/macros.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ def define_env(env: MacrosPlugin) -> None:
235235
used to perform a transformation
236236
"""
237237
# Read in the current package version number to use in templates and files
238-
with open(
239-
pathlib.Path(f"{pathlib.Path(__file__).parents[1]}") / "pyproject.toml", "rb"
238+
with (pathlib.Path(f"{pathlib.Path(__file__).parents[1]}") / "pyproject.toml").open(
239+
"rb"
240240
) as file_handle:
241241
pyproject_data = tomli.load(file_handle)
242242
package_version = "v" + pyproject_data["tool"]["poetry"]["version"]

examples/analog_waveform_usage.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Use TekHSI to plot an analog waveform."""
2+
13
import matplotlib.pyplot as plt
24

35
from tm_data_types import AnalogWaveform

examples/custom_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
def custom_filter(
1313
previous_header: Dict[str, WaveformHeader], current_header: Dict[str, WaveformHeader]
14-
):
14+
) -> bool:
1515
"""A custom criterion for deciding when to consider an acquisition for acceptance."""
1616
for key, cur in current_header.items():
1717
if key not in previous_header:

examples/customize_logging.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""A script demonstrating how to customize the logging that happens during runtime."""
2+
3+
from tm_data_types import AnalogWaveform, write_file
4+
5+
from tekhsi import configure_logging, LoggingLevels, TekHSIConnect
6+
7+
addr = "192.168.0.1" # Replace with the IP address of your instrument
8+
9+
configure_logging(
10+
log_console_level=LoggingLevels.NONE, # completely disable console logging
11+
log_file_level=LoggingLevels.DEBUG, # log everything to the file
12+
log_file_directory="./log_files", # save the log file in the "./log_files" directory
13+
log_file_name="custom_log_filename.log", # customize the filename
14+
)
15+
16+
# Connect to instrument, select channel 1
17+
with TekHSIConnect(f"{addr}:5000", ["ch1"]) as connect:
18+
# Save data from 10 acquisitions as a set of CSV files
19+
for i in range(10):
20+
with connect.access_data():
21+
wfm: AnalogWaveform = connect.get_data("ch1")
22+
23+
# Save the waveform to a file
24+
write_file(f"{wfm.source_name}_{i}.csv", wfm)

examples/digital_waveform_usage.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Use TekHSI to plot a digital waveform."""
2+
13
import matplotlib.pyplot as plt
24
import numpy as np
35

0 commit comments

Comments
 (0)