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

  • SEARCH
  • Home
  • 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 780707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:07:05+00:00 2026-05-14T20:07:05+00:00

i have functions that look like this that is littered through out the code

  • 0

i have functions that look like this that is littered through out the code

def get_M_status(S): 
    M_id = marital.select(marital.c.marital_status_description == S).execute().fetchone()
    if M_id == None:
        print "Warning: No Marital id found for %s Marital status to Single" % S
        M_id = marital.select(marital.c.marital_status_description == "Single").execute().fetchone()       
    return M_id[0]

i was wondering if their is a way to write a generic function where i can pass the relevant values ie: table name primary key column filter column and filter value

cheers

  • 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-14T20:07:06+00:00Added an answer on May 14, 2026 at 8:07 pm

    If the primary key is only one column, you can do something like:

    getattr(table.c, pkey_col_name) == S
    

    as the ‘generic’ version of marital.c.marital_status_description == S.

    So, something like (please note: this is untested):

    def get_row(table, col_name, val, default=None):
        col = getattr(table.c, col_name)
        row = table.select(col == S).execute().fetchone()
        if row == None:
            print "Warning: No row found for %s in %s; using %s" % (val, table, default)
            row = table.select(col == default).execute().fetchone()       
        return row[0]
    

    If you have mapped classes, this is even easier; you can do things like:

    record = session.query(Marital).get(key)
    

    where Marital is the mapped class for the table marital, session is a sql alchemy session, key is a tuple of key columns (in order). If the key exists in the table, record will be the row found; otherwise it will be None.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alot of my controllers have functions that look like this. What is the best
I have a bunch of functions that look like this 'tea' : function (caller)
Basically I have a few functions that look like this: class MyClass { void
I have an if statement that needs to look like this: UPDATE $(input#textbox).keypress(function(e){ key==e.which;
Ruby noob here! I have an array of structs that look like this Token
I have a bunch of Javascript functions that look like the following: function generateBusinessImage
We have some code that looks like this: class Serializer { public: template<class Type>
I have two plots that I make and update in real-time that look like
I have a string that looks like this: str = 751CEC132EB348978013CE7AF8375137 It is 32
I have a large series of functions that all look very similar: they take

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.