Skip to content

Commit

Permalink
Add pre-commit hooks and run black formatter (#43)
Browse files Browse the repository at this point in the history
* Add pre-commit hooks for black/flake8

* Format code base with black/flake8
  • Loading branch information
manishsaha authored Sep 22, 2019
1 parent 4a51ef3 commit d969313
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .flake8.ini → .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[flake8]
ignore = E111,E121,E114,E302,E305,E722,W503,W504
ignore = E121,E114,E203,E302,E305,E722,W503,W504
exclude = .git,__pycache__,venv
max-line-length = 120
2 changes: 1 addition & 1 deletion .hound.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
flake8:
enabled: true
config_file: .flake8.ini
config_file: .flake8
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
language_version: python3.7
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.2.3
hooks:
- id: flake8
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tool.black]
line-length = 120
target_version = ['py37']
exclude = '''
/(
\.git
| venv
| __pycache__
)/
'''
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Jinja2==2.10.1
lxml==4.2.5
MarkupSafe==1.1.0
oauthlib==3.1.0
pre-commit==1.18.3
promise==2.2.1
PySocks==1.7.0
python-dateutil==2.7.5
Expand Down
9 changes: 2 additions & 7 deletions src/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ def is_access_token_expired():

def fetch_access_token():
basic_token = os.environ["TOKEN"]
headers = {
"Cache-Control": "no-cache",
"Authorization": "Basic {}".format(basic_token),
}
headers = {"Cache-Control": "no-cache", "Authorization": "Basic {}".format(basic_token)}
global access_token, expiration_date
try:
rq = requests.post(
TOKEN_URL, headers=headers, data={"grant_type": "client_credentials"}
)
rq = requests.post(TOKEN_URL, headers=headers, data={"grant_type": "client_credentials"})
res = rq.json()
access_token = res.get("access_token")
expires_in = res.get("expires_in")
Expand Down
9 changes: 5 additions & 4 deletions src/gtfs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import csv
from subprocess import PIPE, Popen, STDOUT
import datetime
import threading
import zipfile

# from subprocess import PIPE, Popen, STDOUT
# import datetime
# import threading
# import zipfile

TCAT_NY_US = "tcat-ny-us"
TEN_SECONDS = 10
Expand Down
26 changes: 5 additions & 21 deletions src/stops.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
}
MIN_DIST_BETWEEN_STOPS = 160.0 # Measured in meters
ONE_HOUR_IN_SEC = 60 * 60
STOPS_URL = (
"https://gateway.api.cloud.wso2.com:443/t/mystop/tcat/v1/rest/Stops/GetAllStops"
)
STOPS_URL = "https://gateway.api.cloud.wso2.com:443/t/mystop/tcat/v1/rest/Stops/GetAllStops"

stops_data = None

Expand Down Expand Up @@ -52,14 +50,8 @@ def filter_stops(stops):
stop_names_to_info[stop["name"]].append(stop)

# Get all stops with and without duplicate names
non_duplicate_stops = [
stops_info[0]
for stops_info in stop_names_to_info.values()
if len(stops_info) == 1
]
duplicate_stops = [
stops_info for stops_info in stop_names_to_info.values() if len(stops_info) > 1
]
non_duplicate_stops = [stops_info[0] for stops_info in stop_names_to_info.values() if len(stops_info) == 1]
duplicate_stops = [stops_info for stops_info in stop_names_to_info.values() if len(stops_info) > 1]

# Go through the stops with duplicate names
for bus_stops in duplicate_stops:
Expand All @@ -72,12 +64,7 @@ def filter_stops(stops):
# If stops are too close to each other, combine into a new stop with averaged location
if distance < MIN_DIST_BETWEEN_STOPS:
middle_lat, middle_long = get_middle_coordinate(first_coords, last_coords)
middle_stop = {
"name": first_stop["name"],
"lat": middle_lat,
"long": middle_long,
"type": BUS_STOP,
}
middle_stop = {"name": first_stop["name"], "lat": middle_lat, "long": middle_long, "type": BUS_STOP}
non_duplicate_stops.append(middle_stop)
else: # Otherwise, add directly to list of stops
non_duplicate_stops.append(first_stop)
Expand All @@ -98,10 +85,7 @@ def get_middle_coordinate(coord_a, coord_b):
x = math.cos(b_lat) * math.cos(long_diff)
y = math.cos(b_lat) * math.sin(long_diff)

middle_lat = math.atan2(
math.sin(a_lat) + math.sin(b_lat),
math.sqrt((math.cos(a_lat) + x) ** 2 + y ** 2),
)
middle_lat = math.atan2(math.sin(a_lat) + math.sin(b_lat), math.sqrt((math.cos(a_lat) + x) ** 2 + y ** 2))
middle_long = a_long + math.atan2(y, math.cos(a_lat) + x)

return math.degrees(middle_lat), math.degrees(middle_long)
Expand Down
12 changes: 3 additions & 9 deletions src/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ class TwitterAPI:
TWITTER_SCREEN_NAME = "IthacaTransit"

def __init__(self):
auth = tweepy.OAuthHandler(
os.environ["TWITTER_KEY"], os.environ["TWITTER_KEY_SECRET"]
)
auth.set_access_token(
os.environ["TWITTER_TOKEN"], os.environ["TWITTER_TOKEN_SECRET"]
)
auth = tweepy.OAuthHandler(os.environ["TWITTER_KEY"], os.environ["TWITTER_KEY_SECRET"])
auth.set_access_token(os.environ["TWITTER_TOKEN"], os.environ["TWITTER_TOKEN_SECRET"])
self.api = tweepy.API(auth)

def convert_str_to_date(self, date_str):
Expand All @@ -42,9 +38,7 @@ def get_statuses(self, count):
status_objects = self.api.user_timeline(
screen_name=self.TWITTER_SCREEN_NAME, count=count, tweet_mode="extended"
)
return [
html.unescape(s.full_text.split("\nEffective: ")[0]) for s in status_objects
]
return [html.unescape(s.full_text.split("\nEffective: ")[0]) for s in status_objects]

def get_new_alerts(self, alerts):
"""Filter the array of alerts by removing already tweeted alert messages"""
Expand Down

0 comments on commit d969313

Please sign in to comment.