Skip to content

Commit

Permalink
composer: app: handle edge-cases
Browse files Browse the repository at this point in the history
Force file sync writing and handle 0%
  • Loading branch information
DylanVanAssche committed Oct 24, 2023
1 parent 214d219 commit 8ca54aa
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions composer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,7 @@ def apply_updates(size, seed, additions, modifications, deletions):
if modifications > 0:
with open('CALENDAR.csv', 'r') as services_fd:
reader = csv.reader(services_fd)
row = next(reader) # header
service_writer.writerow(row)
next(reader) # skip header

# Pick random service IDs
rows = []
Expand All @@ -663,30 +662,32 @@ def apply_updates(size, seed, additions, modifications, deletions):
service_ids.append(picked)
break

# Modify each picked service by generating random values for days and dates
# Services which are not picked are left unmodified
with open('CALENDAR.csv', 'r') as services_fd:
reader = csv.reader(services_fd)
next(reader) # header
for row in reader:
if row[0] in service_ids:
monday = random.randint(0, 1)
tuesday = random.randint(0, 1)
wednesday = random.randint(0, 1)
thursday = random.randint(0, 1)
friday = random.randint(0, 1)
saturday = random.randint(0, 1)
sunday = random.randint(0, 1)
year = random.randint(2017, 2023)
start_date = f'{year}-{random.randint(1, 12):02d}-' + \
f'{random.randint(1, 31):02d}'
end_date = f'{year + 1}-{random.randint(1, 12):02d}-' + \
f'{random.randint(1, 31):02d}'
service_writer.writerow([row[0], monday, tuesday, wednesday,
thursday, friday, saturday, sunday,
start_date, end_date])
else:
service_writer.writerow(row)
# Modify each picked service by generating random values for days and dates
# Services which are not picked are left unmodified
with open('CALENDAR.csv', 'r') as services_fd:
reader = csv.reader(services_fd)
row = next(reader) # write header
service_writer.writerow(row)

for row in reader:
if row[0] in service_ids:
monday = random.randint(0, 1)
tuesday = random.randint(0, 1)
wednesday = random.randint(0, 1)
thursday = random.randint(0, 1)
friday = random.randint(0, 1)
saturday = random.randint(0, 1)
sunday = random.randint(0, 1)
year = random.randint(2017, 2023)
start_date = f'{year}-{random.randint(1, 12):02d}-' + \
f'{random.randint(1, 31):02d}'
end_date = f'{year + 1}-{random.randint(1, 12):02d}-' + \
f'{random.randint(1, 31):02d}'
service_writer.writerow([row[0], monday, tuesday, wednesday,
thursday, friday, saturday, sunday,
start_date, end_date])
else:
service_writer.writerow(row)

# Additions: add route and associated data with it.
print(f'\tAdditions: {additions}%')
Expand Down Expand Up @@ -777,20 +778,21 @@ def apply_updates(size, seed, additions, modifications, deletions):
pickup_type, drop_off_type,
shape_dist_traveled])

os.sync()
routes2_fd.close()
trips2_fd.close()
shapes2_fd.close()
service2_fd.close()
stoptimes2_fd.close()
stops2_fd.close()

# Replace original files with updated files
# Replace original files with updated files if they are updated
shutil.move('ROUTES-CHANGE.csv', 'ROUTES.csv')
shutil.move('TRIPS-CHANGE.csv', 'TRIPS.csv')
shutil.move('SHAPES-CHANGE.csv', 'SHAPES.csv')
shutil.move('CALENDAR-CHANGE.csv', 'CALENDAR.csv')
shutil.move('STOP_TIMES-CHANGE.csv', 'STOP_TIMES.csv')
shutil.move('STOPS-CHANGE.csv', 'STOPS.csv')
shutil.move('CALENDAR-CHANGE.csv', 'CALENDAR.csv')


signal.signal(signal.SIGINT, signal_handler)
Expand Down

0 comments on commit 8ca54aa

Please sign in to comment.