Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Tesi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Marco Aceti
Tesi
Commits
5f41ded9
Verified
Commit
5f41ded9
authored
Mar 13, 2023
by
Marco Aceti
Browse files
Options
Downloads
Patches
Plain Diff
Add Train.date() method
parent
e9e3b85f
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#2631
passed
Mar 13, 2023
Stage: test
Stage: lint
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/scraper/tests/test_train.py
+4
-2
4 additions, 2 deletions
src/scraper/tests/test_train.py
src/scraper/train.py
+42
-3
42 additions, 3 deletions
src/scraper/train.py
with
46 additions
and
5 deletions
src/scraper/tests/test_train.py
+
4
−
2
View file @
5f41ded9
...
...
@@ -49,5 +49,7 @@ def test_unfetched_repr_2():
def
test_hash
():
milan
:
Station
=
Station
.
by_code
(
"
S01700
"
)
train
:
Train
=
Train
(
10911
,
milan
)
assert
hash
(
train
)
is
not
None
trains
:
list
[
Train
]
=
milan
.
departures
()
if
not
trains
:
return
assert
hash
(
trains
[
0
])
is
not
None
This diff is collapsed.
Click to expand it.
src/scraper/train.py
+
42
−
3
View file @
5f41ded9
import
typing
as
t
from
datetime
import
datetime
from
datetime
import
date
,
datetime
import
src.scraper.api
as
api
import
src.scraper.station
as
st
import
src.scraper.train_stop
as
tr_st
from
src
import
types
from
src.const
import
TIMEZONE
class
Train
:
...
...
@@ -131,7 +132,7 @@ class Train:
stop
:
tr_st
.
TrainStop
=
tr_st
.
TrainStop
.
_from_raw_data
(
raw_stop
)
self
.
stops
.
append
(
stop
)
#
T
here should always be at least two stops
:
the first and the last.
#
Assertion: t
here should always be at least two stops
-
the first and the last.
assert
len
(
self
.
stops
)
>=
2
self
.
_fetched
=
datetime
.
now
()
...
...
@@ -154,4 +155,42 @@ class Train:
)
def
__hash__
(
self
)
->
int
:
return
hash
(
self
.
number
)
+
hash
(
self
.
origin
.
code
)
"""
Return the hash code.
Notes:
Trains with the same number and origin but departing in different days
will have a different hash code.
"""
return
hash
(
self
.
number
)
+
hash
(
self
.
origin
.
code
)
+
hash
(
self
.
date
())
def
date
(
self
)
->
date
:
"""
Return the departing date of the train.
Used to distinguish trains of different days.
Returns:
date (datetime.date): the departing date of the train
Side effects:
If the train is not yet _fetched, this method calls fetch().
If the train is _phantom, the today
'
s date is returned.
Notes:
To better handle trains having different departing and arriving days
(like
'
Intercity Notte
'
), the departing date is the only one considered.
"""
if
not
self
.
_fetched
:
self
.
fetch
()
if
self
.
_phantom
:
return
datetime
.
now
(
tz
=
TIMEZONE
).
date
()
# Assertion: if the train is fetched, then it should have stops
assert
isinstance
(
self
.
stops
,
list
)
departing_stop
:
tr_st
.
TrainStop
=
next
(
filter
(
lambda
s
:
s
.
stop_type
==
tr_st
.
TrainStopType
.
FIRST
,
self
.
stops
)
)
# Assertion: if the selected stop is the first, it should have an expected departing time
assert
isinstance
(
departing_stop
.
departure
,
tr_st
.
TrainStopTime
)
return
departing_stop
.
departure
.
expected
.
date
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment