FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
replicate-python/replicate/json.py at main · windweb/replicate-python · GitHub
windweb
/
replicate-python
Public
forked from
replicate/replicate-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
replicate-python
/
replicate
/
json.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (30 loc) · 1.04 KB
Breadcrumbs
replicate-python
/
replicate
/
json.py
Copy path
File metadata and controls
35 lines (30 loc) · 1.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
io
from
pathlib
import
Path
from
types
import
GeneratorType
from
typing
import
Any
,
Callable
try
:
import
numpy
as
np
# type: ignore
has_numpy
=
True
except
ImportError
:
has_numpy
=
False
def
encode_json
(
obj
:
Any
,
upload_file
:
Callable
[[
io
.
IOBase
],
str
])
->
Any
:
"""
Returns a JSON-compatible version of the object. Effectively the same thing as cog.json.encode_json.
"""
if
isinstance
(
obj
,
dict
):
return
{
key
:
encode_json
(
value
,
upload_file
)
for
key
,
value
in
obj
.
items
()}
if
isinstance
(
obj
, (
list
,
set
,
frozenset
,
GeneratorType
,
tuple
)):
return
[
encode_json
(
value
,
upload_file
)
for
value
in
obj
]
if
isinstance
(
obj
,
Path
):
with
obj
.
open
(
"rb"
)
as
f
:
return
upload_file
(
f
)
if
isinstance
(
obj
,
io
.
IOBase
):
return
upload_file
(
obj
)
if
has_numpy
:
if
isinstance
(
obj
,
np
.
integer
):
return
int
(
obj
)
if
isinstance
(
obj
,
np
.
floating
):
return
float
(
obj
)
if
isinstance
(
obj
,
np
.
ndarray
):
return
obj
.
tolist
()
return
obj
Back
|
FazBrowse Home
|
New Git URL