Skip to content

Instantly share code, notes, and snippets.

@LazukinPavel
Forked from Th1sUs3rH4sN0N4m3/code.py
Created February 23, 2021 07:53
Show Gist options
  • Save LazukinPavel/e7f8e493a7cb9f557e3e66d27cd1788b to your computer and use it in GitHub Desktop.
Save LazukinPavel/e7f8e493a7cb9f557e3e66d27cd1788b to your computer and use it in GitHub Desktop.
import re
import datetime
from collections import OrderedDict
def create_racer_abbreviations_dict(file_name):
"""Retrieves {'abbreviation': (name, team)}" format dict from abbreviations.txt"""
abbreviations_dict = {}
with open(file_name, 'r') as fn:
for line in fn:
matchObj = re.match(r'^(\w+)_([a-zA-Z\s]+)_([a-zA-Z\s]+)$', line)
# group(1) is abbreviation, i.e 'SVM'
abbreviations_dict[matchObj.group(1)] = (
matchObj.group(2), # name of a pilot
matchObj.group(3).rstrip(), # team
)
return abbreviations_dict
# {'abbreviation of pilot': ('name of pilot, 'team')}
abbr_dict = create_racer_abbreviations_dict(
'abbreviations.txt')
# returns timing log from start.log or end.log in {'abbreviation': time} format
def retrieve_timings_from_log(file_name):
timing_log = {}
with open(file_name, 'r') as fn:
for line in fn:
# matches 2 groups: abbreviation of a racer and time
matchObj = re.match(r'^([A-Z]+).*(\d{2}:\d+:\d+\.\d+)$', line)
# converts time from a string to datetime object
lap_time = datetime.datetime.strptime(
matchObj.group(2).rstrip(), '%H:%M:%S.%f')
# adds key, value to a timing_log
timing_log[matchObj.group(1)] = lap_time
return timing_log
start_timings = retrieve_timings_from_log('start.log')
end_timings = retrieve_timings_from_log('end.log')
def sorted_individual_results(start_timings_, end_timings_, abbr_dict_, reverse_order=False):
"""
Receives start and end timings and returns an OrderedDict with
{abbreviations:timedeltas}
"""
# creating dict with best lap results
lap_results = {key: end_timings_[key] - start_timings_.get(key, 0)
for key in start_timings_.keys()}
sorted_results = dict(
sorted(lap_results.items(), key=lambda x: x[1], reverse=reverse_order))
return sorted_results
sorted_lap_results = sorted_individual_results(
start_timings, end_timings, abbr_dict)
# prints result board to a console
def print_result_board(sorted_lap_results_):
counter = 1
for key, value in sorted_lap_results_.items():
racer_name = abbr_dict[key][0]
team_name = abbr_dict[key][1]
best_time = str(value)[2:-3]
print(("{: <3} {: <18} | {: <30} | {}".format(
str(counter)+'.', racer_name, team_name, best_time)))
if counter == 15:
print(
'----------------------------------------------------------------------')
counter += 1
print_result_board(sorted_lap_results)
@LazukinPavel
Copy link
Author

Notes and suggestions:

  1. L1-2 - Sort import alphabetically
  2. L3 - Unused import
  3. L11, L31 - Variables should be named in lowercase, e.g. user_id
  4. L6, L26 - Consider specify function output/input data type with type hints instead of specifying in names and comments
  5. L12-15 - Consider use variables instead of comments
  6. L14-15, L20 - DRY
  7. L20-22, L40-41, L57-58, L75 - Consider to create function that will perform all data preparations and get back final result
  8. L26 - Use meaningful naming and type hints instead of comments
  9. L33 - Importing whole module instead of needed parts of it considered as a bad practice
  10. L44 - Use verbs in function naming; abbr_dict_ is not used in function, but it should actually
  11. L53 - This line could be done simpler
  12. L63 - Consider use enumerate instead of counter
  13. L70-71 - Use multiplication
  14. Add usage instructions and files samples
  15. Try to avoid obvious or/and redundant comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment