Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7019429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:10:55+00:00 2026-05-27T23:10:55+00:00

I have a webform-based macroeconomics model here that takes parameters and inputs and solves

  • 0

I have a webform-based macroeconomics model here that takes parameters and inputs and solves a system of linear equations.

webapp2 handler:

class Islm(webapp2.RequestHandler):
    def get(self):
        a = self.request.get('a')
        b = self.request.get('b')
        c = self.request.get('c')
        d = self.request.get('d')
        A = self.request.get('A')
        G = self.request.get('G')
        T = self.request.get('T')
        M = self.request.get('M')
        P = self.request.get('P')

        template_values = {
            'url': url,
            'url_linktext': url_linktext,
            'a': a,
            'b': b,
            'c': c,
            'd': d,
            'A': A,
            'G': G,
            'T': T,
            'M': M,
            'P': P
            }

        if a: # need to change to have a better validation test.
            kwargs = {'a': float(a),
                      'b': float(b),
                      'c': float(c),
                      'd': float(d),
                      'A': float(A),
                      'G': float(G),
                      'T': float(T),
                      'M': float(M),
                      'P': float(P)
                      }
            Y, E, r = islm(**kwargs)
            template_values['Y'] = float(Y)
            template_values['E'] = float(E)
            template_values['r'] = float(r)
        else:
            pre_solve = 'ENTER VALUES'
            template_values['Y'] = pre_solve
            template_values['E'] = pre_solve
            template_values['r'] = pre_solve

        template = jinja_environment.get_template('islm.html')
        self.response.out.write(template.render(template_values))

solver:

import numpy as np


def islm(**kwargs):

    a = kwargs['a']
    b = kwargs['b']
    c = kwargs['c']
    d = kwargs['d']
    A = kwargs['A']
    G = kwargs['G']
    T = kwargs['T']
    M = kwargs['M']
    P = kwargs['P']

    matrix_a = np.matrix([[1,-1,0],
                          [-1*a,1,b],
                          [c,0,-1*d]],
                         dtype=float)

    matrix_b = np.matrix([[0, 1, 0, 0],
                          [1, 0, -1*a, 0],
                          [0, 0, 0, 1/P]],
                         dtype=float)

    matrix_c = np.matrix([[A],
                          [G],
                          [T],
                          [M]],
                         dtype=float)

    return np.dot(np.linalg.inv(matrix_a), np.dot(matrix_b, matrix_c))

I know this code is disgusting, I just have no idea how to eliminate the DRY required to do the following for each parameter:

  1. Get the parameter value from the http GET submission.
  2. Send it to the template values dictionary.
  3. If it has a value, convert it to float so numpy knows what to do with it.
  4. Get the parameter in scope for the solver function.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:10:56+00:00Added an answer on May 27, 2026 at 11:10 pm

    You can start by making tuples of all of your inputs and outputs:

    input_vars = ('a', 'b', 'c', 'd', 'A', 'G', 'T', 'M', 'P')
    output_vars = ('Y', 'E', 'r')
    

    You can also just leave template_values with a few values in it and populate the rest dynamically:

    template_values = {
        'url': url,
        'url_linktext': url_linktext,
    }
    

    Now you can dynamically create kwargs and fill up template_values:

    kwargs = {}
    complete = True
    for var in input_vars:
        template_values[var] = self.request.get(var)
        if var in self.request:
            try:
                kwargs[var] = float(self.request[var])
            except ValueError:
                # not a number
                complete = False
        else:
            # not provided
            complete = False
    

    Here, we also have a variable complete; if everything was provided and was successfully parsed as a float, complete will be True. If something wasn’t provided or failed to parse as a float, complete will be False.

    Now we can run the solver:

    if complete:
        output = islm(**kwargs)
        for var, value in zip(output_vars, output):
            template_values[var] = value
    else:
        for var in output_vars:
            template_values[var] = 'ENTER VALUES'
    

    Here, if it’s complete, we can run the solver and then put the output values into the template_values. Otherwise we just set each template variable to 'ENTER VALUES'.

    As for your solver, there’s no need to take a **kwargs argument. Just do this:

    def islm(a, b, c, d, A, G, T, M, P):
        matrix_a = # ...
        # ...
        return np.dot( # ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webform with quite a few fields (between 15 to 40, based
I have a WebForm that contains the following definition for the FCKeditor: <FCKeditorV2:FCKeditor ID=txtBody
I have a simple webform that will allow unauthenticated users to input their information,
I have a .NET webform that has a file upload control that is tied
Does outputcaching VaryByParams in webforms understand route parameters? Such that if I have a
within an asp.net webform project I have a session variable that I am populating
I have a simple javascript (JQuery based) method that does some css magic to
I have custom membership and role provider (C#) in my .net 4 (webform based)
I'm using ASP.NET and I have a save button on webform. When that save
I've got an old WebForms-based ASP.NET app that I have upgraded to ASP.NET 4.0

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.