Reputation: 163
I am modifying some Python code. In this code there is a class called Proxy which has a function "run" that is executed.
import flask
import gevent
class: Proxy
def __init(self):
'''
*stuff*
'''
flask_app = Flask(__name__)
self.run = lambda: (
self.logger.critical('LISTENING %s%s' % (host, port)) or
flask_app.run(host=host, port=port)
)
proxy = Proxy()
proxy.run()
I need to change the self.run = lambda: ('...')
so that proxy.run()
executes the following code:
http = WSGIServer(('', 8000), flask_app)
http.serve_forever()
How do I do this? I don't think I need a lambda expression and I should be using something else. How do I make proxy.run()
execute the 2 lines of code from above? Assume I have all the proper dependencies imported and that the Proxy class functions as intended. Thanks in advance for your help.
I tried the following:
def run():
http = WSGIServer(('', 8000), flask_app)
http.serve_forever()
Then I tried proxy.run()
and I get the error:
AttributeError: 'Proxy" object has no attribute 'run'
Upvotes: 0
Views: 424
Reputation: 71522
Usually the way you define a function that's an attribute of an object is to make it a method:
class Proxy:
def __init__(self, app: Flask):
self._app = app
def run(self) -> None:
WSGIServer(('', 8000), self._app).serve_forever()
app = Flask(__name__)
proxy = Proxy(app) # this calls the __init__ method defined above
proxy.run() # and this calls the run method with "self" = "proxy"
Upvotes: 2
Reputation: 163
import flask
import gevent
class: Proxy
def __init(self):
'''
*stuff*
'''
flask_app = Flask(__name__)
http = WSGIServer(('', 8000), flask_app)
self.run = lambda: (
self.logger.critical('LISTENING %s%s' % (host, port)) or
#flask_app.run(host=host, port=port)
http.serve_forever()
)
proxy = Proxy()
proxy.run()
Thanks everyone for your contributions, they helped me figure it out.
Upvotes: 0