Skip to content

Commit

Permalink
address flake8 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mustaballer committed Jul 25, 2023
1 parent 2f66b8a commit f29f199
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
7 changes: 6 additions & 1 deletion openadapt/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ def get_latest_recording() -> Recording:
return db.query(Recording).order_by(sa.desc(Recording.timestamp)).limit(1).first()


def get_recording_by_id(recording_id):
def get_recording_by_id(recording_id: int) -> Recording:
"""Get the recording by an id.
Returns:
Recording: The latest recording object.
"""
return db.query(Recording).filter_by(id=recording_id).first()


Expand Down
10 changes: 8 additions & 2 deletions openadapt/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
Module: db.py
"""

from typing import Any
import os

from dictalchemy import DictableModel
from loguru import logger
from sqlalchemy import create_engine, event
from sqlalchemy.engine import reflection
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.schema import MetaData
Expand Down Expand Up @@ -97,7 +99,11 @@ def copy_recording_data(
tgt_metadata = MetaData()

@event.listens_for(src_metadata, "column_reflect")
def genericize_datatypes(inspector, tablename, column_dict):
def genericize_datatypes(
inspector: reflection.Inspector,
tablename: str,
column_dict: dict[str, Any],
) -> None:
column_dict["type"] = column_dict["type"].as_generic(
allow_nulltype=True
)
Expand Down Expand Up @@ -186,7 +192,7 @@ def genericize_datatypes(inspector, tablename, column_dict):


def export_recording(recording_id: int) -> str:
"""Export a recording by creating a new database, importing the recording, and then restoring the previous state.
"""Export a recording by its ID to a new SQLite database.
Args:
recording_id (int): The ID of the recording to export.
Expand Down
3 changes: 1 addition & 2 deletions openadapt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
This module provides various utility functions used throughout OpenAdapt.
"""

from collections import Counter, defaultdict
from datetime import datetime, timedelta
from collections import defaultdict
from io import BytesIO
from logging import StreamHandler
from typing import Union
Expand Down
25 changes: 7 additions & 18 deletions openadapt/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
MAX_TABLE_STR_LEN = 1024
PROCESS_EVENTS = True
IMG_WIDTH_PCT = 60
CSS = string.Template(
"""
CSS = string.Template("""
table {
outline: 1px solid black;
}
Expand Down Expand Up @@ -70,8 +69,7 @@
.screenshot:active img:nth-child(3) {
display: block;
}
"""
).substitute(
""").substitute(
IMG_WIDTH_PCT=IMG_WIDTH_PCT,
)

Expand Down Expand Up @@ -164,18 +162,12 @@ def dict2html(
children = indicate_missing(children, all_children, "...")
html_str = "\n".join(children)
elif isinstance(obj, dict):
rows_html = "\n".join(
[
f"""
rows_html = "\n".join([f"""
<tr>
<th>{format_key(key, value)}</th>
<td>{dict2html(value, max_children)}</td>
</tr>
"""
for key, value in obj.items()
if value not in EMPTY
]
)
""" for key, value in obj.items() if value not in EMPTY])
html_str = f"<table>{rows_html}</table>"
else:
html_str = html.escape(str(obj))
Expand All @@ -190,9 +182,8 @@ def dict2html(


@logger.catch
def main(recording: models.Recording = None):
def main(recording: models.Recording = None) -> None:
"""Main function to generate an HTML report for a recording."""

configure_logging(logger, LOG_LEVEL)

if recording is None:
Expand Down Expand Up @@ -299,13 +290,11 @@ def main(recording: models.Recording = None):
</table>
""",
),
Div(
text=f"""
Div(text=f"""
<table>
{dict2html(action_event_dict)}
</table>
"""
),
"""),
),
]
)
Expand Down
19 changes: 11 additions & 8 deletions tests/openadapt/test_share.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Module to test scrub.py"""
"""Module to test share.py."""

from unittest.mock import patch
from zipfile import ZIP_DEFLATED, ZipFile
Expand All @@ -14,8 +14,9 @@
def test_export_recording_to_folder() -> None:
"""Tests the export_recording_to_folder function.
This test creates a temporary recording database file, mocks the crud.export_recording()
function to return the temporary file path, and then asserts that the file is removed after calling
This test creates a temporary recording database file, mocks the
crud.export_recording() function to return the temporary file path,
and then asserts that the file is removed after calling
export_recording_to_folder.
Returns:
Expand All @@ -41,8 +42,9 @@ def test_export_recording_to_folder() -> None:
def test_send_file() -> None:
"""Tests the send_file function.
This test creates a temporary file, mocks the subprocess.run() function to avoid executing the command,
and then verifies that the command is called with the correct arguments.
This test creates a temporary file, mocks the subprocess.run() function
to avoid executing the command, and then verifies that the command is called
with the correct arguments.
Returns:
None
Expand Down Expand Up @@ -94,8 +96,9 @@ def test_send_recording() -> None:
def test_receive_recording() -> None:
"""Tests the receive_recording function.
This test function creates a temporary zip file, mocks the subprocess.run() function
to avoid executing the command, and simulates receiving a recording with a wormhole code.
This test function creates a temporary zip file, mocks the subprocess.run()
function to avoid executing the command, and simulates receiving a recording
with a wormhole code.
Returns:
None
Expand All @@ -121,7 +124,7 @@ def test_receive_recording() -> None:


# Test visualize_recording function
def test_visualize_recording(setup_database: engine):
def test_visualize_recording(setup_database: engine) -> None:
"""Tests the visualize_recording function.
This test calls the function being tested with the "recording.db" created from
Expand Down

0 comments on commit f29f199

Please sign in to comment.