corvid
corvid

Reputation: 11187

Start a python interpreter with local object included

I want to start a python interpreter after performing a few operations to make it more easy for the user. So far, I have a skeleton of what I want.

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('[email protected]', 'password')
  code.interact(banner="use Api.<Crud Command> to do a method")

However, what I am wondering is, how can I import this local into my code.interact?

I've tried local=Api but that does not work, saying TypeError: exec() arg 2 must be a dict, not ApiInterpreter. I've tried using Api.__dict__() but that does not work either. I was also thinking it might be possible to subclass code.InteractiveConsole

Upvotes: 3

Views: 49

Answers (1)

CristiFati
CristiFati

Reputation: 41116

Try:

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

Upvotes: 1

Related Questions