def compute(form, request): """ Generic function for compute_function with arguments taken from a form object (django.forms.ModelForm subclass). Return the output from the compute_function. """ # Extract arguments to the compute function import inspect arg_names = inspect.getargspec(compute_function).args form_data = [] for name in arg_names: if name == "filename": if request.user.is_authenticated(): filename = getattr(form, name).name form_data.append(filename or None) else: form_data.append(request.session.get("filename")) else: if hasattr(form, name): form_data.append(getattr(form, name)) defaults = inspect.getargspec(compute_function).defaults # Make defaults as long as arg_names so we can traverse both with zip if defaults: defaults = ["none"]*(len(arg_names)-len(defaults)) + list(defaults) else: defaults = ["none"]*len(arg_names) # Convert form data to the right type: import numpy for i in range(len(form_data)): if defaults[i] != "none": #if isinstance(defaults[i], (str,bool,int,float)): # bool not ready if isinstance(defaults[i], (str,int,float)): pass # special widgets for these types do the conversion elif isinstance(defaults[i], numpy.ndarray): form_data[i] = numpy.array(eval(form_data[i])) elif defaults[i] is None: if form_data[i] == 'None': form_data[i] = None else: try: # Try eval if it succeeds... form_data[i] = eval(form_data[i]) except: pass # Just keep the text else: # Use eval to convert to right type (hopefully) try: form_data[i] = eval(form_data[i]) except: print 'Could not convert text %s to %s for argument %s' % (form_data[i], type(defaults[i]), arg_names[i]) print 'when calling the compute function...' # Run computations result = compute_function(*form_data) return result
def index(): result = None user = current_user form = ComputeForm(request.form) if request.method == "POST": if form.validate(): result = compute_function(form.A.data, form.b.data, form.w.data, form.T.data) if user.is_authenticated(): object = Compute() form.populate_obj(object) object.result = result object.user = user db.session.add(object) db.session.commit() # Send email notification if user.notify and user.email: send_email(user) else: if user.is_authenticated(): if user.Compute.count() > 0: instance = user.Compute.order_by('-id').first() result = instance.result form = populate_form_from_instance(instance) return render_template("view.html", form=form, result=result, user=user)
def compute(form): """ Generic function for compute_function with arguments taken from a form object (wtforms.Form subclass). Return the output from the compute_function. """ # Extract arguments to the compute function import inspect arg_names = inspect.getargspec(compute_function).args # Extract values from form form_values = [getattr(form, name) for name in arg_names if hasattr(form, name)] form_data = [value.data for value in form_values] # Convert data to right types (if necessary) # for i in range(len(form_data)): # name = arg_names[i] # if name == '...': # form_data[i] = int(form_data[i]) # elif name == '...': # Run computations result = compute_function(*form_data) return result
def compute(form): """ Generic function for compute_function with arguments taken from a form object (wtforms.Form subclass). Return the output from the compute_function. """ # Extract arguments to the compute function import inspect arg_names = inspect.getargspec(compute_function).args # Extract values from form form_values = [ getattr(form, name) for name in arg_names if hasattr(form, name) ] import wtforms form_data = [] for value in form_values: if not isinstance(value, wtforms.fields.simple.FileField): form_data.append(value.data) else: form_data.append(session["filename"]) defaults = inspect.getargspec(compute_function).defaults # Make defaults as long as arg_names so we can traverse both with zip if defaults: defaults = ["none"] * (len(arg_names) - len(defaults)) + list(defaults) else: defaults = ["none"] * len(arg_names) # Convert form data to the right type: import numpy for i in range(len(form_data)): if defaults[i] != "none": if isinstance(defaults[i], (str, bool, int, float)): pass # special widgets for these types do the conversion elif isinstance(defaults[i], numpy.ndarray): form_data[i] = numpy.array(eval(form_data[i])) elif defaults[i] is None: if form_data[i] == 'None': form_data[i] = None else: try: # Try eval if it succeeds... form_data[i] = eval(form_data[i]) except: pass # Just keep the text else: # Use eval to convert to right type (hopefully) try: form_data[i] = eval(form_data[i]) except: print 'Could not convert text %s to %s for argument %s' % ( form_data[i], type(defaults[i]), arg_names[i]) print 'when calling the compute function...' # Run computations result = compute_function(*form_data) return result
def compute(form): """ Generic function for compute_function with arguments taken from a form object (wtforms.Form subclass). Return the output from the compute_function. """ # Extract arguments to the compute function import inspect arg_names = inspect.getargspec(compute_function).args # Extract values from form form_values = [getattr(form, name) for name in arg_names if hasattr(form, name)] form_data = [value.data for value in form_values] defaults = inspect.getargspec(compute_function).defaults # Make defaults as long as arg_names so we can traverse both with zip if defaults: defaults = ["none"]*(len(arg_names)-len(defaults)) + list(defaults) else: defaults = ["none"]*len(arg_names) # Convert form data to the right type: import numpy for i in range(len(form_data)): if defaults[i] != "none": if isinstance(defaults[i], (str,bool,int,float)): pass # special widgets for these types do the conversion elif isinstance(defaults[i], numpy.ndarray): form_data[i] = numpy.array(eval(form_data[i])) elif defaults[i] is None: if form_data[i] == 'None': form_data[i] = None else: try: # Try eval if it succeeds... form_data[i] = eval(form_data[i]) except: pass # Just keep the text else: # Use eval to convert to right type (hopefully) try: form_data[i] = eval(form_data[i]) except: print 'Could not convert text %s to %s for argument %s' % (form_data[i], type(defaults[i]), arg_names[i]) print 'when calling the compute function...' # Run computations result = compute_function(*form_data) return result
def compute(pool): """ Generic function for calling compute_function with values taken from the pool object. Return the output from the compute_function. """ # compute_function must have only one positional argument # named pool import inspect arg_names = inspect.getargspec(compute_function).args if len(arg_names) == 1 and arg_names[0] == "pool": result = compute_function(pool) else: raise TypeError('%s(%s) can only have one argument named "pool"' % (compute_function.__name__, ', '.join(arg_names))) return result
def index(): form = Average(request.form) filename = None # default if request.method == 'POST': # Save uploaded file on server if it exists and is valid if request.files: file = request.files[form.filename.name] if file and allowed_file(file.filename): # Make a valid version of filename for any file ystem filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) result = compute_function(filename) else: result = None return render_template("view.html", form=form, result=result)
def index(): form = Average(request.form) filename = None # default if request.method == 'POST': # Save uploaded file on server if it exists and is valid if request.files: file = request.files[form.filename.name] if file and allowed_file(file.filename): # Make a valid version of filename for any file ystem filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename)) result = compute_function(filename) else: result = None return render_template("view.html", form=form, result=result)