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
9c82264e
Verified
Commit
9c82264e
authored
Mar 12, 2023
by
Marco Aceti
Browse files
Options
Downloads
Patches
Plain Diff
Add _fetched attribute to Train and repr() support
parent
99675a3a
No related branches found
No related tags found
No related merge requests found
Pipeline
#2625
failed
Mar 12, 2023
Stage: test
Stage: lint
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/scraper/__init__.py
+0
-3
0 additions, 3 deletions
src/scraper/__init__.py
src/scraper/tests/test_train.py
+25
-0
25 additions, 0 deletions
src/scraper/tests/test_train.py
src/scraper/train.py
+12
-1
12 additions, 1 deletion
src/scraper/train.py
with
37 additions
and
4 deletions
src/scraper/__init__.py
+
0
−
3
View file @
9c82264e
from
src.scraper.api
import
BadRequestException
,
ViaggiaTrenoAPI
from
src.scraper.api
import
BadRequestException
,
ViaggiaTrenoAPI
# from src.scraper.station import Station
# from src.scraper.train import Train
This diff is collapsed.
Click to expand it.
src/scraper/tests/test_train.py
+
25
−
0
View file @
9c82264e
...
@@ -26,3 +26,28 @@ def test_fetch(kind, station_code):
...
@@ -26,3 +26,28 @@ def test_fetch(kind, station_code):
)
)
for
train
in
trains
:
for
train
in
trains
:
train
.
fetch
()
train
.
fetch
()
def
test_unfetched_repr_1
():
milan
:
Station
=
Station
.
by_code
(
"
S01700
"
)
train
:
Train
=
Train
(
10911
,
milan
)
assert
(
repr
(
train
)
==
"
Treno [?] ??? 10911 : Stazione di Milano Centrale [S01700@1] -> ???
"
)
def
test_unfetched_repr_2
():
train
:
Train
=
Train
.
_from_station_departures_arrivals
(
{
"
numeroTreno
"
:
10911
,
"
codOrigine
"
:
"
S01700
"
,
"
categoriaDescrizione
"
:
"
REG
"
,
"
nonPartito
"
:
False
,
"
provvedimento
"
:
0
,
}
)
assert
(
repr
(
train
)
==
"
Treno [D] REG 10911 : Stazione di Milano Centrale [S01700@1] -> ???
"
)
This diff is collapsed.
Click to expand it.
src/scraper/train.py
+
12
−
1
View file @
9c82264e
...
@@ -23,7 +23,10 @@ class Train:
...
@@ -23,7 +23,10 @@ class Train:
delay (int | None): instantaneous delay of the train, based on last detection
delay (int | None): instantaneous delay of the train, based on last detection
last_detection_place (str | None): place of last detection, it can be a station (or a stop)
last_detection_place (str | None): place of last detection, it can be a station (or a stop)
last_detection_time (datetime | None): time of last detection
last_detection_time (datetime | None): time of last detection
Meta attributes:
_phantom (bool): true if no more data can be fetched (e.g. train is cancelled)
_phantom (bool): true if no more data can be fetched (e.g. train is cancelled)
_fetched (datetime | None): the last time the data has been fetched successfully
"""
"""
def
__init__
(
self
,
number
:
int
,
origin
:
st
.
Station
)
->
None
:
def
__init__
(
self
,
number
:
int
,
origin
:
st
.
Station
)
->
None
:
...
@@ -50,6 +53,7 @@ class Train:
...
@@ -50,6 +53,7 @@ class Train:
self
.
last_detection_time
:
datetime
|
None
=
None
self
.
last_detection_time
:
datetime
|
None
=
None
self
.
_phantom
:
bool
=
False
self
.
_phantom
:
bool
=
False
self
.
_fetched
:
datetime
|
None
=
None
@classmethod
@classmethod
def
_from_station_departures_arrivals
(
cls
,
train_data
:
dict
)
->
"
Train
"
:
def
_from_station_departures_arrivals
(
cls
,
train_data
:
dict
)
->
"
Train
"
:
...
@@ -130,12 +134,19 @@ class Train:
...
@@ -130,12 +134,19 @@ class Train:
# There should always be at least two stops: the first and the last.
# There should always be at least two stops: the first and the last.
assert
len
(
self
.
stops
)
>=
2
assert
len
(
self
.
stops
)
>=
2
self
.
_fetched
=
datetime
.
now
()
def
__repr__
(
self
)
->
str
:
def
__repr__
(
self
)
->
str
:
if
not
self
.
_fetched
:
if
self
.
departed
and
self
.
category
:
return
f
"
Treno [
{
'
D
'
if
self
.
departed
else
'
S
'
}
]
{
self
.
category
}
{
self
.
number
}
:
{
self
.
origin
}
-> ???
"
else
:
return
f
"
Treno [?] ???
{
self
.
number
}
:
{
self
.
origin
}
-> ???
"
if
self
.
_phantom
:
if
self
.
_phantom
:
return
f
"
Treno [?]
{
self
.
category
}
{
self
.
number
}
:
{
self
.
origin
}
-> ?
"
return
f
"
Treno [?]
{
self
.
category
}
{
self
.
number
}
:
{
self
.
origin
}
-> ?
"
assert
isinstance
(
self
.
stops
,
list
)
assert
isinstance
(
self
.
stops
,
list
)
return
(
return
(
f
"
Treno [
{
'
D
'
if
self
.
departed
else
'
S
'
}{
'
X
'
if
self
.
cancelled
else
''
}
]
"
f
"
Treno [
{
'
D
'
if
self
.
departed
else
'
S
'
}{
'
X
'
if
self
.
cancelled
else
''
}
]
"
f
"
{
self
.
category
}
{
self
.
number
}
:
{
self
.
origin
}
->
{
self
.
destination
}
"
f
"
{
self
.
category
}
{
self
.
number
}
:
{
self
.
origin
}
->
{
self
.
destination
}
"
...
...
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