Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python division tweaks #1781

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commonlib
Submodule commonlib updated from 57a3dc to 5bb31d
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
rich-click==1.7.4
pandas==2.2.1
pyarrow==15.0.2
mysqlclient==2.2.4
mysqlclient==2.2.4
PyYAML==6.0.1
-e commonlib
33 changes: 11 additions & 22 deletions scripts/division_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

import re
import sys
from enum import Enum
from pathlib import Path
from typing import cast
Expand All @@ -18,6 +19,11 @@
import rich_click as click
from rich import print
from rich.prompt import Prompt
from pylib.mysociety import config

repository_path = Path(__file__).parent.parent

config.set_file(repository_path / "conf" / "general")

# suppress warnings about using mysqldb in pandas
filterwarnings(
Expand Down Expand Up @@ -48,30 +54,14 @@ def cli():
pass


def fast_config(config_path: Path) -> dict[str, str]:
"""
There's a more comprehensive config parser in commonlib/pylib/mysociety/config.py
But this is all we need for this function.
"""
pattern = r"define\s*\(\s*['\"](.*?)['\"]\s*,\s*['\"]?(.*?)['\"]?\s*\);"

with config_path.open("r") as f:
content = f.read()

return {key: value for key, value in re.findall(pattern, content)}


def get_twfy_db_connection() -> MySQLdb.Connection:
config_path = Path("conf", "general")
config = fast_config(config_path)

db_connection = cast(
MySQLdb.Connection,
MySQLdb.connect(
host=config["OPTION_TWFY_DB_HOST"],
db=config["OPTION_TWFY_DB_NAME"],
user=config["OPTION_TWFY_DB_USER"],
passwd=config["OPTION_TWFY_DB_PASS"],
host=config.get("TWFY_DB_HOST"),
db=config.get("TWFY_DB_NAME"),
user=config.get("TWFY_DB_USER"),
passwd=config.get("TWFY_DB_PASS"),
charset="utf8",
),
)
Expand Down Expand Up @@ -228,8 +218,7 @@ def export_division_data(verbose: bool = False):
"""
Export division data to publically accessible parquet files
"""
config = fast_config(Path("conf", "general"))
raw_data_dir = Path(config["RAWDATA"])
raw_data_dir = Path(config.get("RAWDATA"))
dest_path = raw_data_dir / "votes"
dest_path.mkdir(parents=True, exist_ok=True)

Expand Down
Loading