/
WRC_2021.py
1423 lines (1098 loc) · 46.2 KB
/
WRC_2021.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.6.0
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# # WRC Scraper 2021
#
# The `WRC2021` package provides a means of scraping timing data from the WRC website and working with it in-memory, or storing it in a database.
#
# *TO DO: Add in `asyncio` scheduler elements to call WRC API regularly. Avoid race conditions by scheduling items together in the same scheduled event if they compete for the same write resource.*
# + tags=["active-ipynb"]
# %load_ext autoreload
# %autoreload 2
#
# #%load_ext pycodestyle_magic
# #%flake8_on --ignore D100
# -
from WRC_2021_scraper import *
import pandas as pd
from sqlite_utils import Database
import uuid
import pickle
# + tags=["active-ipynb"]
# TESTDB = 'testdbfy2021a.db'
# !rm $TESTDB
# -
from WRCUtils2021 import _isnull, _notnull, _checkattr, _jsInt, listify
# +
# TO DO - define a class for each table
import warnings
#https://stackoverflow.com/questions/28208949/log-stack-trace-for-python-warning/29567225
import traceback
_formatwarning = warnings.formatwarning
# -
# The following generates stacktrace warnings useful when debugging in IPython notebook kernel environment.
# +
def formatwarning_tb(*args, **kwargs):
"""Format warning message with added stack trace."""
s = _formatwarning(*args, **kwargs)
_tb = traceback.format_stack()
tb = []
for i in _tb:
if 'ipython-input' in i:
tb.append(i)
s += ''.join(tb[:-1])
return s
warnings.formatwarning = formatwarning_tb
# logging.captureWarnings(True)
class IPythonWarner:
"""Tools for reporting warnings."""
def __init__(self, nowarn=False):
"""Initialise class with nowarn attribute."""
self.nowarn = nowarn or None
def warner(self, cond, msg, nowarn=None):
"""Test a condition and if True display a warning message."""
if nowarn is None:
nowarn = self.nowarn
if not nowarn and cond:
warnings.warn(msg)
# -
class Pickler:
"""Pickle support for class."""
def __init__(self, nowarn=False):
"""Initialise pickler."""
pass
def pickle(self, fn=None):
"""Pickle self."""
fn = fn or f'{uuid.uuid4().hex}.pickle'
tmp = self.db
self.db = None
with open(fn, 'wb') as f:
pickle.dump(self, f)
print(f'Pickled to {fn}')
self.db = tmp
# +
# TO DO
# how about a SqliteDB class that can provde db methods. Use sqlite_utils.
# upserter is most important, so can save each table: upsert(table, df, key)
# upserter may require col renaming or additional cols to df before upsert
# -
class SQLiteDB:
"""Simple sqlite utils."""
def __init__(self, dbname=None, nowarn=True):
"""Create db connection if dbname provided."""
if dbname:
self.db_connect(dbname)
def db_connect(self, dbname=None):
"""
Connect to a db.
Give the file a randomly generated name if one not provided.
"""
dbname = dbname or f'{uuid.uuid4().hex}.db'
if not _checkattr(self, dbname) or (dbname and self.dbname != dbname):
self.dbname = dbname
else:
self.dbname = dbname
self.db = Database(self.dbname)
def upsert(self, table, data=None, pk=None):
"""
Upsert data to database.
If we forget to create a database, this will create one.
"""
#print(f'upserting into {table}')
_upserts = []
if isinstance(table, str):
if _notnull(data) and pk is not None:
# One table
_upserts.append((table, data, pk))
# Check data is None. It may be {}, which would be data...
elif hasattr(self, table) and data is None and pk is not None:
# One table, data from self attribute
data = getattr(self, table)
_upserts.append((table, data, pk))
elif isinstance(table, tuple) and len(table) == 2 and _isnull(data):
(_table, _pk) = table
if isinstance(_table, str) and hasattr(self, _table) and _pk is not None:
_data = getattr(self, _table)
_upserts.append((_table, _data, _pk))
elif isinstance(table, list) and _isnull(data):
# Several tables from self attributes: [(table, pk), ..]
for _t in table:
if isinstance(_t, tuple) and len(_t) == 2:
(_table, _pk) = _t
if isinstance(_table, str) and hasattr(self, _table) and _pk is not None:
_data = getattr(self, _table)
_upserts.append((_table, _data, _pk))
# TO DO - if the pk is none, and data is a df, use the df index?
if not _checkattr(self, 'dbname') and not _checkattr(self, 'db'):
self.db_connect()
# The alter=True allows us the table to be modified if we have extra columns
for (_table, _data, _pk) in _upserts:
# TO DO - the try is to cope with this issue:
# https://github.com/simonw/sqlite-utils/issues/73#issuecomment-571138093
# The ignore does not result in a dupe: the original row is left in place
if isinstance(_data, pd.DataFrame) and _notnull(_data) and _pk is not None:
try:
self.db[_table].upsert_all(_data.to_dict(orient='records'),
pk=_pk, alter=True)
except:
try:
self.db[_table].insert_all(_data.to_dict(orient='records'),
pk=_pk, alter=True, replace=True)
except:
warning(f"Couldn't add data to {_table} with PK {_pk}")
elif isinstance(_data, dict) and _data and _pk is not None:
try:
self.db[_table].upsert_all(_data, pk=_pk, alter=True)
except:
try:
self.db[_table].insert_all(_data, pk=_pk,
alter=True, replace=True)
except:
warning(f"Couldn't add data to {_table} with PK {_pk}")
# TO DO - what if we have a dict of dicts like for the stagesplits etc?
def dbfy(self, table, data=None, pk=None):
"""If we have a database, use it."""
if hasattr(self, 'db'):
self.upsert(table, data, pk)
def q(self, query):
"""Run SQL query on db, returning a dataframe."""
if hasattr(self, 'db'):
return pd.read_sql(query, self.db.conn)
def dbtables(self):
"""Show tables in db."""
query = "SELECT * FROM sqlite_master where type='table';"
return self.q(query)
def exportschema(self, fn='WRC2020db_setup.sql'):
"""Export database schema."""
with open(fn, 'w') as f:
for table in self.dbtables()['sql']:
f.write(f'{table}\n\n')
def dump(self, fn='WRC2020db.sql'):
"""Export database contents to text file."""
with open(fn, 'w') as f:
for line in self.db.conn.iterdump():
f.write(f'{line}\n')
class WRCBase(IPythonWarner, SQLiteDB, Pickler):
"""Base class for all WRC stuff."""
def __init__(self, nowarn=True, dbname=None):
"""Add universal classes to WRCBase class."""
IPythonWarner.__init__(self, nowarn=nowarn)
SQLiteDB.__init__(self, dbname=dbname)
def _null():
pass
class WRCLive(WRCBase):
"""Base class for live rallies."""
def __init__(self, live=False, dbname=None):
"""Initialise class for live datasets."""
WRCBase.__init__(self, dbname=dbname)
self.live = live
def stageStatus(self, stageId):
"""Get status of a stage."""
if hasattr(self, 'stages'):
_stage_status = self.stages.loc[self.stages['stageId'] == stageId]
if not _stage_status.empty:
return _stage_status['status'].iloc[0]
return None
# + run_control={"marked": false}
class WRCSeasonBase(WRCBase):
"""Base class for things related to with seasons."""
def __init__(self, season_external_id=None, autoseed=False,
nowarn=True, dbname=None):
"""Add WRCBase class. Autoseed with season details if required."""
WRCBase.__init__(self, nowarn=nowarn, dbname=dbname)
self.season_external_id = _jsInt(season_external_id)
if not self.season_external_id and autoseed:
self._check_season_external_id()
def _check_season_external_id(self, season_external_id=None):
"""Check that season_external_id exists and if not, get one."""
self.season_external_id = _jsInt(season_external_id) or self.season_external_id
if not _checkattr(self, 'season_external_id'):
self.fetchSeasonExternalId(season_external_id)
def fetchSeasonExternalId(self, season_external_id=None):
"""Fetch a season external ID howsoever we can."""
# Get current one from active rally
# It's also available from current_season_events
event, days, channels = getActiveRally()
self.event, self.days, self.channels = event, days, channels
# The returned np.int64 is not JSON serialisable
self.season_external_id = _jsInt(event.loc[0, 'season.externalId'])
# Upsert the data if there's a db connection
self.dbfy([('event', 'id'),
('days', 'id'),
('channels', 'id')])
# + tags=["active-ipynb"]
# zz = WRCSeasonBase(autoseed=True) # .season_external_id
# zz.db_connect()
# + tags=["active-ipynb"]
# zz.pickle()
# !ls *.pickle
# -
class WRCSeasonCategories(WRCSeasonBase):
"""Class describing season categories."""
def __init__(self, autoseed=False, nowarn=True, dbname=None):
"""
Add season categories to base class.
Autoseed if required. Update db if available.
"""
WRCSeasonBase.__init__(self, nowarn=nowarn, dbname=dbname)
self.season_categories = None
self.championship_codes = None
if autoseed:
self.fetchSeasonCategories()
def fetchSeasonCategories(self):
"""Fetch season categories from WRC API. Add to db if required."""
self.season_categories = getSeasonCategories()
self.championship_codes = getSeasonCategories()
self.dbfy([('season_categories', 'id'),
('championship_codes', 'id')])
# + tags=["active-ipynb"]
# zz = WRCSeasonCategories(dbname=TESTDB)
# zz.fetchSeasonCategories()
# zz.championship_codes
# zz.dbtables()
# -
class WRCChampionship(WRCSeasonCategories):
"""Class for championship."""
def __init__(self, category='WRC', typ='drivers',
season_external_id=None,
autoseed=False, nowarn=True, dbname=None):
"""Build on season categories with championship details."""
WRCSeasonCategories.__init__(self, autoseed=False,
nowarn=True, dbname=dbname)
self.championships = {}
if autoseed:
fetchChampionship(self, category=category, typ=typ,
season_external_id=season_external_id)
def fetchChampionship(self, category='WRC',
typ='drivers', season_external_id=None):
"""Get championship data from WRC API and update db if available."""
self._check_season_external_id(season_external_id)
_c = self.championships
if category not in _c:
_c[category] = {}
_cc = _c[category]
if typ not in _cc:
_cc[typ] = {}
_cct = _cc[typ]
(_cct['championship'], _cct['championshipRounds'],
_cct['championshipEntries']) = getChampionship(category=category,
typ=typ, season_external_id=season_external_id)
self.dbfy('championship', _cct['championship'], 'championshipId')
self.dbfy('championshipRounds', _cct['championshipRounds'],
['eventId', 'championshipId'])
self.dbfy('championshipEntries', _cct['championshipEntries'],
['championshipEntryId', 'championshipId', 'personId'])
# + tags=["active-ipynb"]
# zz = WRCChampionship(dbname=TESTDB)
# zz.fetchChampionship()
# zz.dbtables()
# -
class WRCChampionshipStandings(WRCChampionship):
"""Standings for a championship."""
def __init__(self, category='WRC', typ='drivers',
season_external_id=None, autoseed=False,
nowarn=True, dbname=None):
"""Build on championship with championship standings."""
WRCChampionship.__init__(self, autoseed=False,
season_external_id=season_external_id,
nowarn=True, dbname=dbname)
self.championship_standings = {}
if autoseed:
fetchChampionshipStandings(self, category=category, typ=typ,
season_external_id=season_external_id)
def fetchChampionshipStandings(self, category='WRC',
typ='drivers', season_external_id=None):
"""Get championship standings from API and update db if available."""
self._check_season_external_id(season_external_id)
_c = self.championship_standings
if category not in _c:
_c[category] = {}
_cc = _c[category]
if typ not in _cc:
_cc[typ] = {}
_cct = _cc[typ]
(_cct['championship_standings'],
_cct['round_results']) = getChampionshipStandings(category=category,
typ=typ,
season_external_id=season_external_id)
self.dbfy('championship_standings', _cct['championship_standings'],
['championshipId', 'championshipEntryId'])
self.dbfy('round_results', _cct['round_results'],
['championshipEntryId', 'championshipId', 'eventId'])
# + tags=["active-ipynb"]
# zz = WRCChampionshipStandings(dbname=TESTDB)
# zz.fetchChampionshipStandings('JWRC')
# zz.fetchChampionshipStandings('WRC')
# zz.championship_standings
# zz.dbtables()
# -
# TO DO - in a clean environment, the following fails on first run. But if we run it again, it works.
# +
# TO DO - need a more general season events class?
# If, that is, we can we look up arbitrary season events...
class WRCActiveSeasonEvents(SQLiteDB):
"""Class for Season events."""
def __init__(self, autoseed=False, dbname=None):
"""Create active season event class with db support."""
SQLiteDB.__init__(self, dbname=dbname)
if autoseed:
self.fetchActiveSeasonEvents()
def fetchActiveSeasonEvents(self):
"""Fetch active season data from WRC API."""
(self.current_season_events, self.eventdays,
self.eventchannel) = getActiveSeasonEvents()
self.dbfy([('current_season_events', 'id'),
('eventdays', 'id'),
('eventchannel', 'id')])
# + tags=["active-ipynb"]
# zz = WRCActiveSeasonEvents(autoseed=True, dbname=TESTDB)
# zz.current_season_events.head()
# zz.fetchActiveSeasonEvents()
# zz.dbtables()
# + tags=["active-ipynb"]
# zz.dbfy([('current_season_events', 'id')])
# zz.dbtables()
# -
class WRCRally_sdb(WRCBase):
"""
Base class for things with an sdbRallyId.
Can also help find an active sdbRallyId.
"""
def __init__(self, sdbRallyId=None,
autoseed=False, nowarn=True, dbname=None):
"""Initialise things that have a rallyId."""
WRCBase.__init__(self, nowarn=nowarn, dbname=dbname)
self.warner(not sdbRallyId, "sdbRallyId should really be set...")
self.sdbRallyId = _jsInt(sdbRallyId)
if autoseed:
self._checkRallyId(sdbRallyId)
def _checkRallyId(self, sdbRallyId=None):
"""Return a rally ID or lookup active one."""
sdbRallyId = _jsInt(sdbRallyId) or self.sdbRallyId
if not _checkattr(self, 'sdbRallyId'):
self.activerally = WRCActiveRally()
self.sdbRallyId = self.activerally.sdbRallyId
self.name = self.activerally.name
return self.sdbRallyId
# + tags=["active-ipynb"]
# zz = WRCRally_sdb(nowarn=False, dbname=TESTDB)#.nowarn
# zz.dbtables()
# -
class WRCActiveRally(WRCRally_sdb, WRCLive):
"""Create class for the active rally."""
def __init__(self, live=False, dbname=False):
"""Active rally class builds on Rallyid and live classes."""
WRCRally_sdb.__init__(self, nowarn=True, dbname=dbname)
WRCLive.__init__(self, live=live)
self.fetchData()
def fetchData(self):
"""Get data for active rally."""
event, days, channels = getActiveRally()
self.event, self.days, self.channels = event, days, channels
self.dbfy([('event', 'id'),
('days', 'id'),
('channels', 'id')])
# np.int64 is not JSON serialisable
self.sdbRallyId = int(event.loc[0, 'id'])
self.name = event.loc[0, 'name']
# + tags=["active-ipynb"]
# # zz = WRCActiveRally(dbname=TESTDB)#.sdbRallyId
# # zz.dbtables()
# zz = WRCActiveRally()
# + tags=["active-ipynb"]
# zz.event
# + tags=["active-ipynb"]
# zz = WRCRally_sdb(autoseed=True)
# print(zz.sdbRallyId)
# -
# We use the `.fetchData()` method so as to try not to be greedy. This way, we can define a class and start to work towards only grabbling the data if we need it.
class WRCRetirements(WRCRally_sdb):
"""Callable class for retirements."""
def __init__(self, sdbRallyId=None, live=False,
autoseed=False, dbname=None):
"""Initialise retirements class."""
WRCRally_sdb.__init__(self, sdbRallyId=sdbRallyId,
autoseed=autoseed, dbname=dbname)
self.retirements = None
if self.sdbRallyId:
self.fetchRetirements(self.sdbRallyId)
def fetchRetirements(self, sdbRallyId=None):
"""Fetch retirements data from WRC API. Add to db if available."""
self._checkRallyId(sdbRallyId)
self.retirements = getRetirements(self.sdbRallyId)
# Upsert the data if there's a db connection
self.dbfy([('retirements', 'retirementId')])
def __call__(self):
"""Make retirements class callable. Returns dataframe."""
return self.retirements
# + tags=["active-ipynb"]
# zz = WRCRetirements(autoseed=True, dbname=TESTDB)
# display(zz.retirements.head(3))
# zz.dbtables()
# -
class WRCPenalties(WRCRally_sdb):
"""Callable class for penalties."""
def __init__(self, sdbRallyId=None, live=False,
autoseed=False, dbname=None):
"""Initialise penalties class."""
WRCRally_sdb.__init__(self, sdbRallyId=sdbRallyId,
autoseed=autoseed, dbname=dbname)
self.penalties = None
if self.sdbRallyId:
self.fetchPenalties(self.sdbRallyId)
def fetchPenalties(self, sdbRallyId=None):
"""Fetch penalties data from WRC API. Add to db if available."""
self._checkRallyId(sdbRallyId)
self.penalties = getPenalties(self.sdbRallyId)
# Upsert the data if there's a db connection
self.dbfy([('penalties', 'penaltyId')])
def __call__(self):
"""Make penalties class callable. Returns dataframe."""
return self.penalties
# + tags=["active-ipynb"]
# zz = WRCPenalties(autoseed=True, dbname=TESTDB)
# display(zz.penalties.head(3))
# zz.dbtables()
# -
class WRCStagewinners(WRCRally_sdb):
"""Callable class for penalties."""
def __init__(self, sdbRallyId=None, live=False, autoseed=False, dbname=None):
"""Initialise penalties class."""
WRCRally_sdb.__init__(self, sdbRallyId=sdbRallyId,
autoseed=autoseed, dbname=dbname)
self.stagewinners = None
if self.sdbRallyId:
self.fetchStagewinners(self.sdbRallyId)
def fetchStagewinners(self, sdbRallyId=None):
"""Fetch the data from WRC API."""
self._checkRallyId(sdbRallyId)
self.stagewinners = getStagewinners(self.sdbRallyId)
self.dbfy(('stagewinners', ['stageId', 'entryId']))
def __call__(self):
"""Make stagewinners class callable. Returns dataframe."""
return self.stagewinners
# + tags=["active-ipynb"]
# zz = WRCStagewinners(dbname=TESTDB)
# zz.fetchStagewinners()
# display(zz.stagewinners.head())
# zz.dbtables()
# -
class WRCItinerary(WRCRally_sdb, WRCLive):
"""Class for WRC2020 Itinerary."""
def __init__(self, sdbRallyId=None, live=False, autoseed=False, dbname=None):
"""Initialise itinerary class."""
WRCRally_sdb.__init__(self, sdbRallyId=sdbRallyId, autoseed=autoseed, dbname=dbname)
WRCLive.__init__(self, live=live)
self.itinerary = None
self.legs = None
self.sections = None
self.controls = None
self.stages = None
if self.sdbRallyId:
self.fetchItinerary(sdbRallyId)
def _checkItinerary(self):
"""
Check itinerary.
If rally not known, use active rally.
Also set a default startListId.
"""
_itinerary_items = ['itinerary', 'legs', 'sections', 'controls', 'stages']
if not all([_checkattr(self, i) for i in _itinerary_items]):
self.fetchItinerary()
def fetchItinerary(self, sdbRallyId=None):
"""Fetch the data from WRC API."""
self._checkRallyId(sdbRallyId)
(self.itinerary, self.legs, self.sections,
self.controls, self.stages) = getItinerary(self.sdbRallyId)
_ccols = ['code'] + (list(set(self.controls.columns) - set(self.stages.columns)))
self.richstages = self.stages.merge(self.controls[_ccols], on='code')
self.dbfy([('itinerary', ['itineraryId']),
('legs', ['itineraryLegId']),
('sections', ['itinerarySectionId']),
('controls', ['controlId']),
('stages', ['stageId']),
('richstages', ['stageId'])])
def getCurrentLeg(self):
"""Get current leg details."""
if self.legs is None:
self.fetchItinerary()
return getCurrentLeg(legs=self.legs)
def getStageIdFromCode(self, code=None):
"""Return a stageID from a single codes."""
# Accept: 'SS1', '1', 1
if _jsInt(code):
code = f'SS{code}'
if code and isinstance(code, str) and _checkattr(self, 'stages'):
_df = self.stages[self.stages['code'] == code][['rallyid', 'stageId']]
return tuple(_df.iloc[0])
elif code and isinstance(code, list):
# TO DO - this might be dangerous if we are expecting a single tuple response
return getStageIdsFromCode(code, response='tuples')
def getStageIdsFromCode(self, codes=None, response='dict'):
"""Return a stageID from one or more codes."""
# TO DO: this should really run through getStageIdFromCode()
codes = [f'SS{_jsInt(c)}' if _jsInt(c) else c for c in listify(codes)]
if codes and _checkattr(self, 'stages'):
_df = self.stages[self.stages['code'].isin(codes)][['code', 'rallyid', 'stageId']]
if response == 'df':
return _df
elif response == 'tuples':
return list(_df.itertuples(index=False, name=None))
return _df.set_index('code').to_dict(orient='index')
def getStageIds(self, typ='all', response='dict'):
"""Return stageIDs by stage status."""
if _checkattr(self, 'stages'):
if typ == 'all':
_df = self.stages[['code', 'rallyid', 'stageId']]
else:
if typ == 'completed':
_statuses = ['Completed', 'Interrupted']
elif typ == 'onlycompleted':
_statuses = ['Completed']
elif typ == 'interrupted':
_statuses = ['Interrupted']
_df = self.stages[self.stages['status'].isin(_statuses)][['code', 'rallyid', 'stageId']]
if response == 'df':
return _df
elif response == 'tuples':
return list(_df.itertuples(index=False, name=None))
return _df.set_index('code').to_dict(orient='index')
# + tags=["active-ipynb"]
# sdbRallyId = 100
# zz = WRCItinerary(sdbRallyId, autoseed=True, dbname=TESTDB)
# zz.dbtables()
# + tags=["active-ipynb"]
# zz.getStageIds('interrupted')
# zz.getStageIds()
# + tags=["active-ipynb"]
# WRCItinerary(sdbRallyId=100).legs
# -
# Each leg has a startlist.
class WRCStartlist(WRCItinerary):
"""Class for WRC2020 Startlist table."""
def __init__(self, startListId=None, live=False, autoseed=True,
nowarn=False, dbname=None):
"""Startlist class, builds on itinerary class."""
WRCItinerary.__init__(self, live=live, autoseed=autoseed,
dbname=dbname)
self.startListId = _jsInt(startListId)
self.startList = None
self.startListItems = None
self.startLists = {}
if not nowarn and not self.startListId:
warnings.warn("startListId should really be set..")
if self.startListId or autoseed:
self.fetchStartList(self.startListId)
def _checkStartListId(self, startListId=None):
"""Return a startlistId or look one up."""
self._checkItinerary()
if not startListId or not _jsInt(startListId):
# TO DO - Should we do this?
#self.startListId = int(self.legs.loc[0, 'startListId'])
self.startListId = getStartlistId(startListId=startListId,
legs=self.legs, stages=self.stages)
else:
self.startListId = _jsInt(startListId or self.startListId)
def fetchStartList(self, startListId=None):
"""Get startlist data from API. Add to db if available."""
self._checkStartListId(startListId)
(startList, startListItems) = getStartlist(startListId=startListId)
if _notnull(startList) and _notnull(startListItems):
self.startList, self.startListItems = startList, startListItems
self.dbfy([('startList', ['startListId']),
('startListItems', ['startListItemId'])])
if not startListId:
startListId = startList['startListId'].iloc[0]
if startListId not in self.startLists:
self.startLists[startListId] = startListItems
def fetchStartLists(self):
"""Get all startlists data from API. Add to db if available."""
self._checkItinerary()
self.legs.apply(lambda row: self.fetchStartList(row['startListId']), axis=1)
# + tags=["active-ipynb"]
# zz = WRCStartlist() # (autoseed=True, dbname=TESTDB)
# # zz.fetchStartList()
# # What startlist should we return by default?
# zz.startListId#, zz.startListItems
# + tags=["active-ipynb"]
# #zz.fetchStartLists()
# #zz.startLists
# zz.fetchStartList('SS2')
# zz.startList
# -
class WRCCars(WRCRally_sdb):
"""Class for WRC2020 Cars table."""
def __init__(self, sdbRallyId=None, live=False,
autoseed=False, dbname=None):
"""Cars class, building on rallyid class."""
WRCRally_sdb.__init__(self, sdbRallyId=sdbRallyId,
autoseed=autoseed, dbname=dbname)
self.cars = None
self.classes = None
if self.sdbRallyId:
self.fetchCars(sdbRallyId)
def fetchCars(self, sdbRallyId=None):
"""Fetch cars data from WRC API. Add to db if available."""
self._checkRallyId(sdbRallyId)
cars, classes = getCars(self.sdbRallyId)
self.cars, self.classes = cars, classes
self.dbfy([('cars', ['entryId', 'eventId']),
('classes', ['entryId'])])
# + tags=["active-ipynb"]
# zz = WRCCars(autoseed=True, dbname=TESTDB)
# zz.dbtables()
# -
class WRCRally(WRCRally_sdb):
"""Class for WRC2020 Rally table. This gives external ids."""
def __init__(self, sdbRallyId=None, autoseed=False, dbname=False):
"""Rally class, builds on rallid class."""
WRCRally_sdb.__init__(self, sdbRallyId=sdbRallyId,
autoseed=autoseed, dbname=dbname)
self.rally = None
self.eligibilities = None
self.groups = None
if self.sdbRallyId:
self.fetchRally(sdbRallyId)
def fetchRally(self, sdbRallyId=None):
"""Fetch rally details from WRC API. Add to db if avaailable."""
self._checkRallyId(sdbRallyId)
(self.rally, self.eligibilities, self.groups) = getRally(self.sdbRallyId)
self.dbfy([('rally', ['sdbRallyId']),
('eligibilities', ['sdbRallyId', 'category']),
('groups', ['sdbRallyId', 'groupId'])])
# TO DO - define iterators?
# + tags=["active-ipynb"]
# zz = WRCRally(autoseed=True, dbname=TESTDB)
# display(zz.rally)
# # zz.dbtables()
# +
# TO DO - have a check stages function to get some data...
class WRCRallyStages(WRCItinerary):
"""Class referring to all rally stages."""
def __init__(self, sdbRallyId=None, live=False,
autoseed=False, nowarn=True, dbname=None):
"""Rally stages class;. Builds on itinerary."""
WRCItinerary.__init__(self, sdbRallyId=None, live=False,
autoseed=autoseed, dbname=dbname)
self.sdbRallyId = _jsInt(sdbRallyId)
if autoseed:
self._checkStages(self.sdbRallyId)
def _checkStages(self, sdbRallyId=None):
"""Return a stages list or lookup list for active rally."""
# Have we got an sdbRallyId?
if not _checkattr(self, 'stages'):
self.fetchStages(sdbRallyId=None)
def fetchStages(self, sdbRallyId=None):
"""Fetch stages for a specified rally."""
self._checkItinerary()
self.activerally = WRCActiveRally()
self.sdbRallyId = self.activerally.sdbRallyId
self.name = self.activerally.name
def lastCompletedStage(self):
"""Return last completed stage ID."""
return self.stages[self.stages['status'] == 'Completed'].iloc[-1]['stageId']
# + tags=["active-ipynb"]
# zz = WRCRallyStages(autoseed=True, dbname=TESTDB)
# zz.richstages.head() # stages / controls
# # zz._checkStages()[1].head()
# # zz.lastCompletedStage()
# zz.dbtables()
# + tags=["active-ipynb"]
# zz.lastCompletedStage()
# +
# Does this actually do anything other than possible checks?
class WRCRallyStage(WRCRallyStages):
"""
Base class for things with a stageId.
Can also help find a stageId list for a given rally.
"""
def __init__(self, sdbRallyId=None, stageId=None, live=False,
autoseed=False, nowarn=True, dbname=None):
"""Rallystage class. Builds on rally stages."""
WRCRallyStages.__init__(self, sdbRallyId=sdbRallyId,
live=live, autoseed=autoseed,
nowarn=nowarn, dbname=dbname)
if not nowarn:
if not sdbRallyId:
warnings.warn("sdbRallyId should really be set...")
if not stageId:
warnings.warn("stageId should really be set...")
self.stageId = _jsInt(stageId)
if autoseed:
pass
#self.fetchData(self.sdbRallyId, stageId)
def _checkStageId(self, sdbRallyId=None,
stageId=None, fallback='lastCompleted'):
"""Return a stage ID or lookup a current one."""
self._checkRallyId(sdbRallyId)
stageId = _jsInt(stageId)
# One method for finding a stage ID - last completed
if not stageId:
if 'lastCompleted':
stageId = _jsInt(self.lastCompletedStage())
# What else? Most recent still running, else lastCompleted?
return stageId
# sdbRallyId = sdbRallyId or self.sdbRallyId
# if not hasattr(self, 'sdbRallyId') or not self.sdbRallyId:
# self.activerally = WRCActiveRally()
# self.sdbRallyId = self.activerally.sdbRallyId
# self.name = self.activerally.name
# return self.sdbRallyId
# + tags=["active-ipynb"]
# WRCRallyStage(autoseed=False)
# -
# # TO DO
#
# Build in iterator to grab all stage data. overall, stagetimes and splittimes build on stage class.
class WRCOverall(WRCRallyStage):
"""Class for overall stage table."""
def __init__(self, sdbRallyId=None, stageId=None, live=False,
autoseed=False, nowarn=True, dbname=None):
"""Overall class. Builds on Rallystage class."""
WRCRallyStage.__init__(self, sdbRallyId=sdbRallyId, stageId=stageId,
live=live, autoseed=autoseed,
nowarn=nowarn, dbname=dbname)
self.overall = {}