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
91598f40
Verified
Commit
91598f40
authored
Mar 15, 2023
by
Marco Aceti
Browse files
Options
Downloads
Patches
Plain Diff
Use pickle instead of jsonpickle
parent
e223f22e
No related branches found
No related tags found
No related merge requests found
Pipeline
#2664
passed
Mar 15, 2023
Stage: test
Stage: lint
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
requirements.txt
+0
-1
0 additions, 1 deletion
requirements.txt
src/scraper/main.py
+14
-20
14 additions, 20 deletions
src/scraper/main.py
with
14 additions
and
21 deletions
requirements.txt
+
0
−
1
View file @
91598f40
requests
==2.28.2
python-dateutil
==2.8.2
tqdm
==4.65.0
jsonpickle
==3.0.1
This diff is collapsed.
Click to expand it.
src/scraper/main.py
+
14
−
20
View file @
91598f40
...
...
@@ -2,10 +2,10 @@ import itertools
import
logging
import
os
import
pathlib
import
pickle
import
typing
as
t
from
datetime
import
date
,
datetime
,
timedelta
import
jsonpickle
from
tqdm
import
tqdm
from
src.const
import
TIMEZONE
...
...
@@ -16,22 +16,16 @@ DATA_DIR = pathlib.Path("data/")
def
load_dataset
(
file_path
:
pathlib
.
Path
)
->
dict
[
t
.
Any
,
t
.
Any
]:
os
.
system
(
f
"
touch
{
file_path
.
absolute
()
}
"
)
with
open
(
file_path
,
"
r
"
)
as
f
:
contents
:
str
=
"
\n
"
.
join
(
f
.
readlines
())
dataset
:
dict
[
t
.
Any
,
t
.
Any
]
if
len
(
contents
)
>
0
:
dataset
=
jsonpickle
.
loads
(
contents
)
# type: ignore
else
:
dataset
=
dict
()
return
dataset
try
:
with
open
(
file_path
,
"
rb
"
)
as
f
:
return
pickle
.
load
(
f
)
except
FileNotFoundError
:
return
dict
()
def
save_dataset
(
file_path
:
pathlib
.
Path
,
dataset
:
dict
[
t
.
Any
,
t
.
Any
])
->
None
:
os
.
system
(
f
"
touch
{
file_path
.
absolute
()
}
"
)
frozen
=
jsonpickle
.
encode
(
dataset
)
with
open
(
file_path
,
"
w
"
)
as
f
:
f
.
write
(
str
(
frozen
))
with
open
(
file_path
,
"
wb
"
)
as
f
:
pickle
.
dump
(
dataset
,
f
)
def
main
()
->
None
:
...
...
@@ -43,9 +37,9 @@ def main() -> None:
except
FileExistsError
:
pass
station_cache
:
dict
[
str
,
Station
]
=
load_dataset
(
DATA_DIR
/
"
stations.
json
"
)
fetched_trains
:
dict
[
int
,
Train
]
=
load_dataset
(
today_path
/
"
trains.
json
"
)
unfetched_trains
:
dict
[
int
,
Train
]
=
load_dataset
(
today_path
/
"
unfetched.
json
"
)
station_cache
:
dict
[
str
,
Station
]
=
load_dataset
(
DATA_DIR
/
"
stations.
pickle
"
)
fetched_trains
:
dict
[
int
,
Train
]
=
load_dataset
(
today_path
/
"
trains.
pickle
"
)
unfetched_trains
:
dict
[
int
,
Train
]
=
load_dataset
(
today_path
/
"
unfetched.
pickle
"
)
fetched_old_n
=
len
(
fetched_trains
)
unfetched_old_n
=
len
(
unfetched_trains
)
...
...
@@ -114,9 +108,9 @@ def main() -> None:
f
"
(
{
(
len
(
unfetched_trains
)
-
unfetched_old_n
)
:
+
d
}
)
"
)
save_dataset
(
DATA_DIR
/
"
stations.
json
"
,
Station
.
_cache
)
save_dataset
(
today_path
/
"
trains.
json
"
,
fetched_trains
)
save_dataset
(
today_path
/
"
unfetched.
json
"
,
unfetched_trains
)
save_dataset
(
DATA_DIR
/
"
stations.
pickle
"
,
Station
.
_cache
)
save_dataset
(
today_path
/
"
trains.
pickle
"
,
fetched_trains
)
save_dataset
(
today_path
/
"
unfetched.
pickle
"
,
unfetched_trains
)
logging
.
info
(
f
"
Trains saved today:
{
len
(
fetched_trains
)
}
"
)
logging
.
info
(
f
"
Station cache size:
{
len
(
Station
.
_cache
)
}
"
)
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