FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
replicate-python/replicate/json.py at main · hatgit/replicate-python · GitHub
hatgit
/
replicate-python
Public
forked from
replicate/replicate-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
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
40 lines (35 loc) · 1.19 KB
Breadcrumbs
replicate-python
/
replicate
/
json.py
Copy path
File metadata and controls
40 lines (35 loc) · 1.19 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
36
37
38
39
40
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
# pylint: disable=too-many-return-statements
def
encode_json
(
obj
:
Any
,
# noqa: ANN401
upload_file
:
Callable
[[
io
.
IOBase
],
str
],
)
->
Any
:
# noqa: ANN401
"""
Return 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
file
:
return
upload_file
(
file
)
if
isinstance
(
obj
,
io
.
IOBase
):
return
upload_file
(
obj
)
if
HAS_NUMPY
:
if
isinstance
(
obj
,
np
.
integer
):
# type: ignore
return
int
(
obj
)
if
isinstance
(
obj
,
np
.
floating
):
# type: ignore
return
float
(
obj
)
if
isinstance
(
obj
,
np
.
ndarray
):
# type: ignore
return
obj
.
tolist
()
return
obj
Back
|
FazBrowse Home
|
New Git URL