FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-fire/fire/interact.py at master · google/python-fire · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
google
/
python-fire
Public
Notifications
You must be signed in to change notification settings
Fork
1.5k
Star
28.2k
Code
Issues
133
Pull requests
61
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-fire
/
fire
/
interact.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
96 lines (78 loc) · 2.96 KB
Breadcrumbs
python-fire
/
fire
/
interact.py
Copy path
File metadata and controls
96 lines (78 loc) · 2.96 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright (C) 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This module enables interactive mode in Python Fire.
It uses IPython as an optional dependency. When IPython is installed, the
interactive flag will use IPython's REPL. When IPython is not installed, the
interactive flag will start a Python REPL with the builtin `code` module's
InteractiveConsole class.
"""
import
inspect
def
Embed
(
variables
,
verbose
=
False
):
"""Drops into a Python REPL with variables available as local variables.
Args:
variables: A dict of variables to make available. Keys are variable names.
Values are variable values.
verbose: Whether to include 'hidden' members, those keys starting with _.
"""
print
(
_AvailableString
(
variables
,
verbose
))
try
:
_EmbedIPython
(
variables
)
except
ImportError
:
_EmbedCode
(
variables
)
def
_AvailableString
(
variables
,
verbose
=
False
):
"""Returns a string describing what objects are available in the Python REPL.
Args:
variables: A dict of the object to be available in the REPL.
verbose: Whether to include 'hidden' members, those keys starting with _.
Returns:
A string fit for printing at the start of the REPL, indicating what objects
are available for the user to use.
"""
modules
=
[]
other
=
[]
for
name
,
value
in
variables
.
items
():
if
not
verbose
and
name
.
startswith
(
'_'
):
continue
if
'-'
in
name
or
'/'
in
name
:
continue
if
inspect
.
ismodule
(
value
):
modules
.
append
(
name
)
else
:
other
.
append
(
name
)
lists
=
[
(
'Modules'
,
modules
),
(
'Objects'
,
other
)]
list_strs
=
[]
for
name
,
varlist
in
lists
:
if
varlist
:
items_str
=
', '
.
join
(
sorted
(
varlist
))
list_strs
.
append
(
f'
{
name
}
:
{
items_str
}
'
)
lists_str
=
'
\n
'
.
join
(
list_strs
)
return
(
'Fire is starting a Python REPL with the following objects:
\n
'
f'
{
lists_str
}
\n
'
)
def
_EmbedIPython
(
variables
,
argv
=
None
):
"""Drops into an IPython REPL with variables available for use.
Args:
variables: A dict of variables to make available. Keys are variable names.
Values are variable values.
argv: The argv to use for starting ipython. Defaults to an empty list.
"""
import
IPython
# pylint: disable=import-outside-toplevel,g-import-not-at-top
argv
=
argv
or
[]
IPython
.
start_ipython
(
argv
=
argv
,
user_ns
=
variables
)
def
_EmbedCode
(
variables
):
import
code
# pylint: disable=import-outside-toplevel,g-import-not-at-top
code
.
InteractiveConsole
(
variables
).
interact
()
Back
|
FazBrowse Home
|
New Git URL