Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F102747246
utils.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Feb 23, 18:45
Size
972 B
Mime Type
text/x-python
Expires
Tue, Feb 25, 18:45 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
24412771
Attached To
R6602 InternetAnalytics
utils.py
View Options
# ######################
# Some useful utilities.
# ######################
import
json
,
os
,
pickle
def
save_json
(
objects
,
path
):
"""
Save a list of objects as JSON (.txt).
"""
# Remove the file if it exists
if
os
.
path
.
exists
(
path
):
os
.
remove
(
path
)
for
obj
in
objects
:
# 'a' stands for 'append' to the end of the file
# '+' to create the file if it doesn't exist
with
open
(
path
,
'a+'
)
as
f
:
f
.
write
(
json
.
dumps
(
obj
))
f
.
write
(
'
\n
'
)
def
load_json
(
path
):
"""
Read a JSON from a text file. Expect a list of objects.
"""
with
open
(
path
)
as
f
:
lines
=
f
.
readlines
()
return
[
json
.
loads
(
s
)
for
s
in
lines
]
def
save_pkl
(
obj
,
path
):
"""
Save an object to path.
"""
with
open
(
path
,
'wb'
)
as
f
:
pickle
.
dump
(
obj
,
f
)
def
load_pkl
(
path
):
"""
Load a pickle from path.
"""
with
open
(
path
,
'rb'
)
as
f
:
return
pickle
.
load
(
f
)
Event Timeline
Log In to Comment