Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 352
corvid Avatar asked Apr 18 '26 19:04

corvid


1 Answers

Try:

code.interact(banner="use Api.<Crud Command> to do a method", local={"Api": Api})
like image 198
CristiFati Avatar answered Apr 21 '26 09:04

CristiFati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!