python - 启动一个包含本地对象的 python 解释器

标签 python python-3.x

我想在执行一些操作后启动一个 python 解释器,以方便用户使用。到目前为止,我已经有了我想要的骨架。

class ApiManager(object):
  def __init__(self, username, password, version='v1'):
    self.base_url = 'http://localhost:3000/api/' + version + '/{endpoint}'

    login = requests.post(self.base_url.format(endpoint='login'), {
      'user': username, 'password': password
    }).json()['data']

    self.headers = {
      'X-Auth-Token': login['authToken'],
      'X-User-Id': login['userId']
    }

  def get(self, endpoint):
    " example method "
    return requests.get(self.base_url.format(endpoint))

if __name__ == '__main__':
  Api = ApiManager('user@user.org', 'password')
  code.interact(banner="use Api.<Crud Command> to do a method")

但是,我想知道的是,如何将这个本地文件导入我的 code.interact

我试过 local=Api 但它不起作用,说 TypeError: exec() arg 2 must be a dict, not ApiInterpreter。我试过使用 Api.__dict__() 但这也不起作用。我还认为可以将 code.InteractiveConsole

子类化

最佳答案

尝试:

code.interact(banner="use Api.<Crud Command> to do a method", local={"Api": Api})

关于python - 启动一个包含本地对象的 python 解释器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31326709/

相关文章:

python - IndexError : list index out of range. 谁能帮我解决这个Python代码吗?包括 numpy 和 pandas 的概念

python - 从 URL 中查找字符串中的所有匹配词

python-3.x - Python 3.6 - 属性错误 : module 'tkinter' has no attribute 'filedialog'

python - 你如何组合 2 个相似的 if 语句来缩短你的 python 编程代码?

python - 使用 suds SOAP 库进行 HTTP 身份验证的奇怪行为

python - 仅当位置可被给定值整除时,才使用 matplotlib.ticker.Locator 放置刻度

python - python 中是否有等效的 drand48() 或它的包装器?

javascript - 是否可以用漂亮的汤从动态图中提取数据?

python - 如何确定 Networkx 图表中所选节点的特定颜色和大小

python - 用于浮点转换的自定义语言环境配置