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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:16:11+00:00 2026-05-30T15:16:11+00:00

When you give a developer a robs_numbers object, the developer can call the various

  • 0

When you give a developer a “robs_numbers” object, the developer can call the various filters in any order they want e.g. obj.filter1().filter2().filtern() ..

However generally the more numbers I filter out early, the faster the filtering is.

I want the developer to be able to call the filter() methods in any order they want.
The developer then gets back a robs_numbers object (or perhaps an iterable..).
However I want my methods (filters) to actually run in the order given by priority().

technically priority() should take the pair filter: [args], so that I can use any filter. priority() will then return the priority value

Here is some sample code, though you may change the object completely if it’s more pythonic:

from random import randint
data = [randint(0,100) for i in range(100)]

class robs_numbers():
    def __init__(self,numbers,_min,_max):
        self.numbers = iter(numbers)
        self._min = _min #approx
        self._max = _max #approx

    def bigger(self,x):
        print "priority = ", self.priority(x)
        return robs_numbers([i for i in self.numbers if i>x],x,self._max)

    def smaller(self,x):
        print "priority = ", self.priority(x)
        return robs_numbers([i for i in self.numbers if i<x],self._min,x)

    def order(self,backwards=False):
        print "priority = ", self.priority()
        self.numbers = iter(sorted(self.numbers,reverse=backwards))
        return self

    # ...
    # More filter methods
    # ...

    def priority(self,x=None):
        #works out the order methods should be called to increase efficiency
        if x is None:
            return 99999
        else:
            return abs(x-(self._max-self._min)/2)

and example of two valid uses, however I want them both to be fast, right now only one is efficient:

def get():
    return robs_numbers(data,0,100)

rf = get().bigger(80).smaller(90).order() #faster in this order
print list(rf.numbers), rf._min, rf._max

#priority =  30
#priority =  80
#priority =  99999
#[81, 81, 82, 83, 85, 86, 87, 87, 89] 80 90


rf = get().order().smaller(90).bigger(80) #slower in this order
print list(rf.numbers), rf._min, rf._max
#priority =  99999
#priority =  40
#priority =  35
#[81, 81, 82, 83, 85, 86, 87, 87, 89] 80 90
  • 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-30T15:16:12+00:00Added an answer on May 30, 2026 at 3:16 pm

    You’ll have to create some kind of lazily evaluated query set, and only execute all the filters whenever the developer calls list() or something else that requires you to export the data to another object. Django has a good example of this in their query sets, which are lazy.

    So, you could write something to the effect of

    class RobsNumbers(object):
        def __init__(self, numbers, min, max):
            self._numbers = numbers
            self._min = min
            self._max = max
            self._filters = []
    
        def bigger(self, x):
            new_obj = RobsNumbers(self._numbers, x, self._max)
            new_obj._filters = self._filters + [GreaterThanFilter(x)]
            return new_obj
    
        # Other filter methods simply add a filter to the _filters list of the object
    
        def __iter__(self):
            # Here is the complicated optimization logic to reorder and execute the filters
            pass
    

    Note that this code does zero filtering until the developer asks for the data contained inside a RobsNumbers object, so it is quite lazy.

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

Sidebar

Related Questions

I want to give Windows Server 2008 a try as a .NET developer and
I'm a .NET developer who knows very little about Python, but want to give
I am a developer in DotNet. I want to give skype a possibility to
During hiring a .NET web developer I give the candidate a coding test. I
What would a developer used to working in Visual Studio have to give up
Give a base class Base , I want to write a method Test, like
Give an Image component, how can I tell the size [width and height] of
Will using global variables give a speed up? In Intel's Architecture Software Developer Manual
I'm working on a game and I'd like to give a non-developer friend a
I am new Rails developer. Right now, I can let user register in my

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.