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 6854701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:40:00+00:00 2026-05-27T01:40:00+00:00

I’m trying to write a Database Abstraction Layer in Python which lets you construct

  • 0

I’m trying to write a Database Abstraction Layer in Python which lets you construct SQL statments using chained function calls such as:

results = db.search("book")
          .author("J. K. Rowling")
          .price("<40.00")
          .title("Harry")
          .execute()

but I am running into problems when I try to dynamically add the required methods to the db class.

Here is the important parts of my code:

import inspect

def myName():
    return inspect.stack()[1][3]

class Search():

    def __init__(self, family):
        self.family = family
        self.options = ['price', 'name', 'author', 'genre']
        #self.options is generated based on family, but this is an example
        for opt in self.options:
            self.__dict__[opt] = self.__Set__
        self.conditions = {}

    def __Set__(self, value):
        self.conditions[myName()] = value
        return self

    def execute(self):
        return self.conditions

However, when I run the example such as:

print(db.search("book").price(">4.00").execute())

outputs:

{'__Set__': 'harry'}

Am I going about this the wrong way? Is there a better way to get the name of the function being called or to somehow make a ‘hard copy’ of the function?

  • 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-27T01:40:00+00:00Added an answer on May 27, 2026 at 1:40 am

    You can simply add the search functions (methods) after the class is created:

    class Search:  # The class does not include the search methods, at first
        def __init__(self):
            self.conditions = {}
    
    def make_set_condition(option):  # Factory function that generates a "condition setter" for "option"
        def set_cond(self, value):
            self.conditions[option] = value
            return self
        return set_cond
    
    for option in ('price', 'name'):  # The class is extended with additional condition setters
        setattr(Search, option, make_set_condition(option))
    
    Search().name("Nice name").price('$3').conditions  # Example
    {'price': '$3', 'name': 'Nice name'}
    

    PS: This class has an __init__() method that does not have the family parameter (the condition setters are dynamically added at runtime, but are added to the class, not to each instance separately). If Search objects with different condition setters need to be created, then the following variation on the above method works (the __init__() method has a family parameter):

    import types
    
    class Search:  # The class does not include the search methods, at first
    
        def __init__(self, family):
            self.conditions = {}
            for option in family:  # The class is extended with additional condition setters
                # The new 'option' attributes must be methods, not regular functions:
                setattr(self, option, types.MethodType(make_set_condition(option), self))
    
    def make_set_condition(option):  # Factory function that generates a "condition setter" for "option"
        def set_cond(self, value):
            self.conditions[option] = value
            return self
        return set_cond
    
    >>> o0 = Search(('price', 'name'))  # Example
    >>> o0.name("Nice name").price('$3').conditions
    {'price': '$3', 'name': 'Nice name'}
    >>> dir(o0)  # Each Search object has its own condition setters (here: name and price)
    ['__doc__', '__init__', '__module__', 'conditions', 'name', 'price']
    
    >>> o1 = Search(('director', 'style'))
    >>> o1.director("Louis L").conditions  # New method name
    {'director': 'Louis L'}
    >>> dir(o1)  # Each Search object has its own condition setters (here: director and style)
    ['__doc__', '__init__', '__module__', 'conditions', 'director', 'style']
    

    Reference: http://docs.python.org/howto/descriptor.html#functions-and-methods


    If you really need search methods that know about the name of the attribute they are stored in, you can simply set it in make_set_condition() with

           set_cond.__name__ = option  # Sets the function name
    

    (just before the return set_cond). Before doing this, method Search.name has the following name:

    >>> Search.price
    <function set_cond at 0x107f832f8>
    

    after setting its __name__ attribute, you get a different name:

    >>> Search.price
    <function price at 0x107f83490>
    

    Setting the method name this way makes possible error messages involving the method easier to understand.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.