Reputation: 2343
I have the following setting:
import sys
from flask import Flask
from flask.ext import restful
from model import Model
try:
gModel = Model(int(sys.argv[1]))
except IndexError, pExc:
gModel = Model(100)
def main():
lApp = Flask(__name__)
lApi = restful.Api(lApp)
lApi.add_resource(FetchJob, '/')
lApp.run(debug=True)
class FetchJob(restful.Resource):
def get(self):
lRange = gModel.getRange()
return lRange
if __name__ == '__main__':
main()
Is there a way to instantiate the Model-class inside the main()-function? Here, the Flask framework instantiates the FetchJob-class, so that I cannot provide it the parameters it forwards during the instantiation process.
I don't like to have global variables as this messes up the whole design ...
Upvotes: 0
Views: 684
Reputation: 12150
I think this should work, although I'm not familiar with Flask:
import functools
def main():
try:
gModel = Model(int(sys.argv[1]))
except IndexError as pExc:
gModel = Model(100)
lApp = Flask(__name__)
lApi = restful.Api(lApp)
lApi.add_resource(functools.partial(FetchJob, gModel), '/')
lApp.run(debug=True)
class FetchJob(restful.Resource):
def __init__(self, obj, *args, **kwargs):
restfult.Resource.__init__(self, *args, **kwargs)
self.obj = obj
def get(self):
lRange = self.obj.getRange()
return lRange
Upvotes: 2