Skip to content

Commit

Permalink
Add black and flake8 (#174)
Browse files Browse the repository at this point in the history
* Update black

Update black from 19.10b0 to 24.4.2. black had been broken due to an
ImportError (see psf/black#2964).

* Add black to pre-commit

* Add black to GitHub Actions

* Add flake8 to pre-commit

* Update requirements.txt using Pipfile

Ran `pipenv requirements --exclude-markers > requirements.txt` to update
requirements.txt with correct dependencies and versions from Pipfile. I
did this primarily to update the black version (because the previous
version was broken with an ImportError), and I might as well update the
rest of the dependencies as well.

* Add flags to black for GitHub Actions

Add the --check and --diff flags to black within GitHub Actions
workflows so that the workflows fail if files need formatting. Without
these flags, black will "format" the files and exit without error, which
doesn't actually write to the files during workflows.

* Add flags to flake8 and black for pre-commit

Add flags to flake8 and black for pre-commit based on existing flags in
GitHub Actions workflows. This includes selecting for specific flake8
errors and adjusting the max line length. These existing configs are
extremely limited, so they should be broadened to more errors and/or a
more widely accepted max line length.

Also ran black on the entire codebase to satisfy the GitHub workflows.

* Fix typo in black option flag

* Fix formatting errors
  • Loading branch information
tianyizheng02 authored Jun 3, 2024
1 parent 38cf761 commit 551d8d9
Show file tree
Hide file tree
Showing 29 changed files with 743 additions and 945 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/autotest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ jobs:
run-test-suite:
runs-on: ubuntu-latest


steps:
- uses: actions/checkout@v3
- name: Set up Python 3.12
Expand All @@ -28,6 +27,8 @@ jobs:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Format with black
run: black --line-length=127 --check --diff .
- name: Run all tests with pytest
run: pytest --cov=pittapi tests/
--ignore=tests/lab_test.py
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests-on-push.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

name: Run Tests On Push

on:
Expand All @@ -12,7 +11,6 @@ permissions:

jobs:
build-and-test:

runs-on: ubuntu-latest

steps:
Expand All @@ -32,6 +30,8 @@ jobs:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Format with black
run: black --line-length=127 --check --diff .
- name: Test with pytest
run: pytest --cov=pittapi tests/
--ignore=tests/lab_test.py
Expand Down
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,23 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-yaml

# https://flake8.pycqa.org/en/latest/user/using-hooks.html
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
# GitHub editor is 127 chars wide, see workflows in .github/
args: [
"--select=E9,F63,F7,F82",
"--max-line-length=127"
]

# https://black.readthedocs.io/en/stable/integrations/source_version_control.html
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.4.2
hooks:
- id: black
language_version: python3.12
# GitHub editor is 127 chars wide, see workflows in .github/
args: ["--line-length=127"]
4 changes: 2 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ nose-cov = "*"
nose-timer = "*"
typing = "*"
virtualenv = "*"
black = "==19.10b0"
black = "*"
sphinx = "*"
requests-html = "*"
parse = "*"
pre-commit = "*"
pytest = "*"
pytest-cov = "==4.1.0"
pytest-xdist = "*"

flake8 = "*"

[dev-packages]

Expand Down
667 changes: 312 additions & 355 deletions Pipfile.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pittapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

Expand Down
41 changes: 11 additions & 30 deletions pittapi/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
TERM_REGEX = "2\d\d[147]"
VALID_TERMS = re.compile(TERM_REGEX)


class Instructor(NamedTuple):
name: str
email: Optional[str] = None
Expand Down Expand Up @@ -91,6 +92,7 @@ class Course(NamedTuple):
course_id: str
course_title: str


class CourseDetails(NamedTuple):
course: Course
course_description: Optional[str] = None
Expand All @@ -100,6 +102,7 @@ class CourseDetails(NamedTuple):
attributes: List[Attribute] = None
sections: Optional[List[Section]] = None


class Subject(NamedTuple):
subject_code: str
courses: Dict[str, Course]
Expand Down Expand Up @@ -128,9 +131,7 @@ def get_subject_courses(subject: str) -> Subject:
return Subject(subject_code=subject, courses=courses)


def get_course_details(
term: Union[str, int], subject: str, course: Union[str, int]
) -> CourseDetails:
def get_course_details(term: Union[str, int], subject: str, course: Union[str, int]) -> CourseDetails:
term = _validate_term(term)
subject = _validate_subject(subject)
course = _validate_course(course)
Expand All @@ -144,11 +145,7 @@ def get_course_details(
credit_range = (json_response["units_minimum"], json_response["units_maximum"])

requisites = None
if (
"offerings" in json_response
and len(json_response["offerings"]) != 0
and "req_group" in json_response["offerings"][0]
):
if "offerings" in json_response and len(json_response["offerings"]) != 0 and "req_group" in json_response["offerings"][0]:
requisites = json_response["offerings"][0]["req_group"]

components = None
Expand Down Expand Up @@ -184,15 +181,10 @@ def get_course_details(
status = section["enrl_stat_descr"]

instructors = None
if (
len(section["instructors"]) != 0
and section["instructors"][0] != "To be Announced"
):
if len(section["instructors"]) != 0 and section["instructors"][0] != "To be Announced":
instructors = []
for instructor in section["instructors"]:
instructors.append(
Instructor(name=instructor["name"], email=instructor["email"])
)
instructors.append(Instructor(name=instructor["name"], email=instructor["email"]))

meetings = None
if len(section["meetings"]) != 0:
Expand Down Expand Up @@ -223,12 +215,7 @@ def get_course_details(
)

return CourseDetails(
course=Course(
subject_code=subject,
course_number=course,
course_id=internal_course_id,
course_title=course_title
),
course=Course(subject_code=subject, course_number=course, course_id=internal_course_id, course_title=course_title),
course_description=course_description,
credit_range=credit_range,
requisites=requisites,
Expand All @@ -238,9 +225,7 @@ def get_course_details(
)


def get_section_details(
term: Union[str, int], class_number: Union[str, int]
) -> Section:
def get_section_details(term: Union[str, int], class_number: Union[str, int]) -> Section:
term = _validate_term(term)

json_response = _get_section_details(term, class_number)
Expand All @@ -265,9 +250,7 @@ def get_section_details(
date_range = meeting["date_range"].split(" - ")

instructors = None
if len(meeting["instructors"]) != 0 and meeting["instructors"][0][
"name"
] not in ["To be Announced", "-"]:
if len(meeting["instructors"]) != 0 and meeting["instructors"][0]["name"] not in ["To be Announced", "-"]:
instructors = []
for instructor in meeting["instructors"]:
name = instructor["name"]
Expand Down Expand Up @@ -328,9 +311,7 @@ def _validate_term(term: Union[str, int]) -> str:
"""Validates that the term entered follows the pattern that Pitt does for term codes."""
if VALID_TERMS.match(str(term)):
return str(term)
raise ValueError(
"Term entered isn't a valid Pitt term, must match regex " + TERM_REGEX
)
raise ValueError("Term entered isn't a valid Pitt term, must match regex " + TERM_REGEX)


def _validate_subject(subject: str) -> str:
Expand Down
10 changes: 2 additions & 8 deletions pittapi/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Lab(NamedTuple):
linux: int
"""


def _fetch_labs():
"""Fetches dictionary of status of all labs."""
labs = {}
Expand Down Expand Up @@ -80,12 +81,5 @@ def get_status():
if key["name"] in labs:
total = key["total"]
in_use = key["active"]
statuses.append(
{
"location": key["name"],
"isOpen": labs[key["name"]],
"total": total,
"in_use": in_use
}
)
statuses.append({"location": key["name"], "isOpen": labs[key["name"]], "total": total, "in_use": in_use})
return statuses
6 changes: 2 additions & 4 deletions pittapi/laundry.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

from bs4 import BeautifulSoup

BASE_URL = (
"https://www.laundryview.com/api/currentRoomData?school_desc_key=197&location={}"
)
BASE_URL = "https://www.laundryview.com/api/currentRoomData?school_desc_key=197&location={}"

LOCATION_LOOKUP = {
"TOWERS": "2430136",
Expand Down Expand Up @@ -63,7 +61,7 @@ def get_status_simple(building_name: str) -> Dict[str, str]:
-> SUTH_WEST
"""
laundry_info = _get_laundry_info(building_name)
freeWashers, freeDryers, totalWashers, totalDryers = 0,0,0,0
freeWashers, freeDryers, totalWashers, totalDryers = 0, 0, 0, 0

for obj in laundry_info["objects"]:
if obj["type"] == "washFL":
Expand Down
8 changes: 2 additions & 6 deletions pittapi/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,11 @@ def _extract_documents(documents: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
return new_docs


def _extract_facets(
facet_fields: List[Dict[str, Any]]
) -> Dict[str, List[Dict[str, Any]]]:
def _extract_facets(facet_fields: List[Dict[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
facets = {} # type: Dict[str,List[Dict[str,Any]]]
for facet in facet_fields:
facets[facet["display_name"]] = []
for count in facet["counts"]:
facets[facet["display_name"]].append(
{"value": count["value"], "count": count["count"]}
)
facets[facet["display_name"]].append({"value": count["value"], "count": count["count"]})

return facets
4 changes: 1 addition & 3 deletions pittapi/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ def _load_n_items(feed: str, max_news_items: int):
request_objs = []
for i in range(int(math.ceil(max_news_items / 10))):
payload["start"] = i * 10
request_objs.append(
grequests.get("https://m.pitt.edu/news/index.json", params=payload)
)
request_objs.append(grequests.get("https://m.pitt.edu/news/index.json", params=payload))

responses = grequests.imap(request_objs)

Expand Down
5 changes: 3 additions & 2 deletions pittapi/people.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

from requests_html import HTMLSession
from typing import List, Dict
from parse import compile
Expand Down Expand Up @@ -66,7 +67,7 @@ def get_person(query: str) -> List[Dict[str, str]]:
session = HTMLSession()
resp = session.post(PEOPLE_SEARCH_URL, data=payload)
if resp.text.__contains__("Too many people matched your criteria."):
return [{"ERROR":"Too many people matched your criteria."}] # Return an error
return [{"ERROR": "Too many people matched your criteria."}] # Return an error
elements = resp.html.xpath("/html/div/section")
result = []
for entry in elements:
Expand All @@ -75,5 +76,5 @@ def get_person(query: str) -> List[Dict[str, str]]:
_parse_segments(person, segments)
result.append(person)
if result == []:
return [{"ERROR":"No one found."}] # Return an error
return [{"ERROR": "No one found."}] # Return an error
return result
8 changes: 2 additions & 6 deletions pittapi/shuttle.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def get_map_vehicle_points(api_key: str = "8882812681") -> Dict[str, Any]:
return response.json()


def get_route_stop_arrivals(
api_key: str = "8882812681", times_per_stop: int = 1
) -> Dict[str, Any]:
def get_route_stop_arrivals(api_key: str = "8882812681", times_per_stop: int = 1) -> Dict[str, Any]:
"""Return stop arrival times for all vehicles."""
payload = {"ApiKey": api_key, "TimesPerStopString": times_per_stop}
response = sess.get(
Expand All @@ -45,9 +43,7 @@ def get_route_stop_arrivals(
return response.json()


def get_vehicle_route_stop_estimates(
vehicle_id: str, quantity: int = 2
) -> Dict[str, Any]:
def get_vehicle_route_stop_estimates(vehicle_id: str, quantity: int = 2) -> Dict[str, Any]:
"""Return {quantity} stop estimates for all active vehicles."""
payload = {"vehicleIdStrings": vehicle_id, "quantity": str(quantity)}
response = sess.get(
Expand Down
Loading

0 comments on commit 551d8d9

Please sign in to comment.