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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:47:27+00:00 2026-06-18T01:47:27+00:00

I’m trying to abstract my sqlalchemy query call, the complete unmodified call looks like

  • 0

I’m trying to abstract my sqlalchemy query call, the complete unmodified call looks like this:

Session.query(User).options(FromCache('redis1')).filter(User.id == user_id).all()

The dynamic parts I need control over is User, redis1 and the last call all()
I want to call something like above line with the possibility to change the things in bold above in the arguments.

My first try turned out like this:

# Abstracts the final chained method
def q(obj, action):
    return getattr(obj, action)()

# Removes a lot of boiler plate code
# but doesn't give me control over the cache argument
# which I need in the same method as the action above
def query(model):
    return Session.query(model).options(FromCache('redis1'))

# Called like this
result = q(query(User).filter(User.id == user_id), 'all')

But I’d obviously only want one function instead of two so I can control both the action and the model.
The reason for this is that I’d like to wrap the .one() or .all() call in a try-except block that will go through different cache servers in the FromCache option in case the query fails. But doing this every time I write a query would be annoying.

I’m looking for something like this (code doesn’t work, obviously):

def q(model, filters, action):
    data = None
    servers = ['redis1', 'redis2']
    for server in servers:
        try:
            call = Session.query(model).options(FromCache(redis)).filters_here_somehow()
            data = getattr(call, obj)() # Final call from action added
        except:
            continue
        break;
    return data

# and called something like this.. (or some other way)
result = q(User, the filter/between/limit/whatnot here, 'all')

Any ideas or am I totally way off base? Or is there a smarter way to do this?

  • 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-06-18T01:47:29+00:00Added an answer on June 18, 2026 at 1:47 am

    the only part of this where something interesting has to happen is the “all()”. The rest of it seems like you’d stick with a straight Query.

    So here’s one way pattern:

    q = query(User).filter(User.id = user_id).limit(whatever)
    results = from_redis(q, Query.all)
    

    and here’s a way we can do that:

    def from_redis(query, meth):
        for server in ['r1', 'r2']:
            try:
                return meth(q.options(FromCache(server)))
            except Exception, e:
                continue
        else:
            raise e
    

    but that might seem a little lame. Since you’re using the caching recipe and subclassing Query, you could add your redis feature to it as well, sort of like:

    class RedisQuery(CachingQuery):
        redis_servers = False
    
        @_generative(self):
        def using_redis(self):
            self.redis_servers = True
    
        def __iter__(self):
            if self.redis_servers:
                for server in ['r1', 'r2']:
                    try:
                        return super(RedisQuery, self).options(FromCache(server)).__iter__()
                    except Exception, e:
                        continue
                else:
                    raise e
            else:
                return super(RedisQuery, self).__iter__()
    

    that one you call like this:

    q = query(User).filter(User.id = user_id).limit(whatever)
    results = q.using_redis().all()
    

    just some ideas.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,

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.