Skip to content

Commit

Permalink
Add pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreenbury committed Oct 15, 2023
1 parent 613dbbf commit 7b7e300
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 21 deletions.
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
files: ^python/
exclude: synthpop_pb2.py$
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.9.1
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.292
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
2 changes: 1 addition & 1 deletion python/draw_venues.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# You may need to 'pip install' some extra dependencies, or run in a
# conda/poetry environment.

import click
import pandas as pd
import plotly.express as px
import synthpop_pb2
import click


@click.command()
Expand Down
2 changes: 1 addition & 1 deletion python/protobuf_to_csv.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# You may need to 'pip install' some extra dependencies, or run in a
# conda/poetry environment.

import click
import pandas as pd
import synthpop_pb2
import click


@click.command()
Expand Down
9 changes: 5 additions & 4 deletions python/protobuf_to_json.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from google.protobuf.json_format import MessageToJson
import synthpop_pb2
import sys

import synthpop_pb2
from google.protobuf.json_format import MessageToJson

if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} synthpop_protobuf_file")
sys.exit(-1)
print(f"Usage: {sys.argv[0]} synthpop_protobuf_file")
sys.exit(-1)

pop = synthpop_pb2.Population()
f = open(sys.argv[1], "rb")
Expand Down
2 changes: 1 addition & 1 deletion python/tests/data/test_region_info_per_msoa.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion python/tests/data/test_region_venues_per_activity.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions python/tests/test_reader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from uatk_spc.reader import SPCReaderParquet as SPCReader
import pathlib
import os
import pathlib

from uatk_spc.reader import SPCReaderParquet as SPCReader


def get_path():
Expand All @@ -24,6 +25,7 @@ def test_merge_people_and_time_use_diaries():
)
assert merged.shape == (197_397, 30)


def test_merge_people_and_households():
spc = SPCReader(TEST_PATH, TEST_REGION)
merged = spc.merge_people_and_households()
Expand Down
14 changes: 8 additions & 6 deletions python/uatk_spc/reader.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import json
import os
from typing import Any, Dict, List
from google.protobuf.json_format import MessageToDict
import polars as pl

import pandas as pd
import polars as pl
import uatk_spc.synthpop_pb2 as synthpop_pb2
import json
import pprint
from google.protobuf.json_format import MessageToDict

# TODO:
# - Add graph data structure reading for flows (e.g. into networkx)
Expand Down Expand Up @@ -49,7 +49,8 @@ def __init__(self, path: str, region: str, backend="polars"):
)
else:
raise ValueError(
f"Backend: {backend} is not implemented. Use 'polars' or 'pandas' instead."
f"Backend: {backend} is not implemented. Use 'polars' or 'pandas' "
f"instead."
)
self.venues_per_activity = pop_as_dict["venuesPerActivity"]
self.info_per_msoa = pop_as_dict["infoPerMsoa"]
Expand Down Expand Up @@ -97,7 +98,8 @@ def __init__(self, path: str, region: str, backend="polars"):
self.venues_per_activity = pd.read_parquet(path_ + "_venues.pq")
else:
raise ValueError(
f"Backend: {backend} is not implemented. Use 'polars' or 'pandas' instead."
f"Backend: {backend} is not implemented. Use 'polars' or 'pandas' "
f"instead."
)
with open(path_ + "_info_per_msoa.json", "rb") as f:
self.info_per_msoa = json.loads(f.read())
Expand Down
10 changes: 5 additions & 5 deletions python/uatk_spc/scripts.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import pandas as pd
from google.protobuf.json_format import MessageToJson
import uatk_spc.synthpop_pb2 as synthpop_pb2
import click
import uatk_spc.synthpop_pb2 as synthpop_pb2
from google.protobuf.json_format import MessageToJson


@click.command()
@click.option("--input_path", required=True, help="path to an SPC .pb file")
def convert_to_json(input_path: str) -> None:
"""Converts a protobuf population to JSON.
Args:
input_path (str): Input path to a SPC protobuf file.
"""
pop = synthpop_pb2.Population()
with open(input_path, "rb") as f:
Expand Down

0 comments on commit 7b7e300

Please sign in to comment.