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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:56:23+00:00 2026-06-05T23:56:23+00:00

I am creating a query builder class that will help in constructing a query

  • 0

I am creating a query builder class that will help in constructing a query for mongodb from URL params. I have never done much object oriented programming, or designed classes for consumption by people other than myself, besides using basic language constructs and using django’s built in Models.

So I have this QueryBuilder class

class QueryHelper():
    """
    Help abstract out the problem of querying over vastly
    different dataschemas.
    """

    def __init__(self, collection_name, field_name, params_dict):
        self.query_dict = {}
        self.params_dict = params_dict
        db = connection.get_db()
        self.collection = db[collection_name]

    def _build_query(self):
        # check params dict and build a mongo query
        pass

Now in _build_query I will be checking the params_dict and populating query_dict so as to pass it to mongo’s find() function.
In doing this I was just wondering if there was an absolute correct approach to as whether _build_query should return a dictionary or whether it should just modify self.query_dict. Since it is an internal method I would assume it is OK to just modify self.query_dict. Is there a right way (pythonic) way of approaching this? Is this just silly and not an important design decision? Any help is appreciated.

  • 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-05T23:56:28+00:00Added an answer on June 5, 2026 at 11:56 pm

    Returning a value is preferable as it allows you to keep all the attribute modifying in one place (__init__). Also, this makes it easier to extend the code later; suppose you want to override _build_query in a subclass, then the overriding method can just return a value, without needing to know which attribute to set. Here’s an example:

    class QueryHelper(object):
        def __init__(self, param, text):
            self._param = param
            self._query = self._build_query(text)
    
        def _build_query(self, text):
            return text + " and ham!"
    
    class RefinedQueryHelper(QueryHelper):
        def _build_query(self, text):
            # no need to know how the query object is going to be used
            q = super(RefinedQueryHelper, self)._build_query()
            return q.replace("ham", "spam")
    

    vs. the “setter version”:

    class QueryHelper(object):
        def __init__(self, param, text):
            self._param = param
            self._build_query(text)
    
        def _build_query(self, text):
            self._query = text + " and ham!"
    
    class RefinedQueryHelper(QueryHelper):
        def _build_query(self, text):
            # what if we want to store the query in __query instead?
            # then we need to modify two classes...
            super(RefinedQueryHelper, self)._build_query()
            self._query = self._query.replace("ham", "spam")
    

    If you do choose to set an attribute, you might want to call the method _set_query for clarity.

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

Sidebar

Related Questions

I'm working on creating a SQL query that will pull records from a table
I am working on creating a web app that will query event logs on
Background: I'm creating a dashboard as a project and I have a query that
I'm creating a query that will display information for a record which is derived
I have problems creating a custom query. This is what entities: @Entity public class
I'm creating a query that will be run twice a month: On the 5th
I am creating a query, which will have various results depending on the parameters
Hey there. I would really appreciate your help in creating a query that would
I need some help creating a query for my mySQL database. I have recently
I need help in creating a query for my database. I have 2 tables,

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.