473,289 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

"filtered view" upon lists?

Hi there :)

I don't know how else to call what I'm currently implementing: An object
that behaves like a list but doesn't store it's own items but rather
pulls them from a larger list (if they match a certain criterion).
Changes to the filter are instantly reflected in the underlying list.
Clear enough?

Ok, so I figured that this is generic enough to be found in some
standard module already (I've had this often enough: Painfully
implementing s/th and then finding it in the libs some weeks later.).

Any pointers?

c.u.
wildemar
Sep 12 '06 #1
10 1731
Wildemar Wildenburger <wi******@freakmail.dewrites:
I don't know how else to call what I'm currently implementing: An object that
behaves like a list but doesn't store it's own items but rather pulls them
from a larger list (if they match a certain criterion).
Changes to the filter are instantly reflected in the underlying list.
Clear enough?
It looks like you're implementing a callable to me. This is a method that
returns results based on some input -- here your original list and filter.

Then you'll use this method wherever you need that filtered list.
Ok, so I figured that this is generic enough to be found in some standard
module already (I've had this often enough: Painfully implementing s/th and
then finding it in the libs some weeks later.).
I don't believe it is generic. Nobody knows your data specs or filtering
needs.
Any pointers?
Use of list comprehension might make it easier to code this:
def myCallable(my_list, filter):
filtered_list = [(item) for item in my_list if filter(item)]
return filtered_list
Example of full code:
>>test_list = range(10)
filter = lambda x: not x%2
def myCallable(list, filter):
.... filtered_list = [(item) for item in list if filter(item)]
.... return filtered_list
....
>>myCallable(test_list, filter)
[0, 2, 4, 6, 8]
>>for item in myCallable(test_list, filter):
.... print "See? I'm", item
....
See? I'm 0
See? I'm 2
See? I'm 4
See? I'm 6
See? I'm 8
>>>

--
Jorge Godoy <jg****@gmail.com>
Sep 12 '06 #2
"Jorge Godoy" <jg****@gmail.comwrote in message
news:87************@gmail.com...
Wildemar Wildenburger <wi******@freakmail.dewrites:
>I don't know how else to call what I'm currently implementing: An object
that
behaves like a list but doesn't store it's own items but rather pulls
them
from a larger list (if they match a certain criterion).
Changes to the filter are instantly reflected in the underlying list.
Clear enough?

It looks like you're implementing a callable to me. This is a method that
returns results based on some input -- here your original list and filter.

Then you'll use this method wherever you need that filtered list.
>Ok, so I figured that this is generic enough to be found in some standard
module already (I've had this often enough: Painfully implementing s/th
and
then finding it in the libs some weeks later.).

I don't believe it is generic. Nobody knows your data specs or filtering
needs.
>Any pointers?

Use of list comprehension might make it easier to code this:
def myCallable(my_list, filter):
filtered_list = [(item) for item in my_list if filter(item)]
return filtered_list
Example of full code:
>>>test_list = range(10)
filter = lambda x: not x%2
def myCallable(list, filter):
... filtered_list = [(item) for item in list if filter(item)]
... return filtered_list
...
>>>myCallable(test_list, filter)
[0, 2, 4, 6, 8]
>>>for item in myCallable(test_list, filter):
... print "See? I'm", item
...
See? I'm 0
See? I'm 2
See? I'm 4
See? I'm 6
See? I'm 8
>>>>


--
Jorge Godoy <jg****@gmail.com>
Functionally-speaking, there is the filter built-in that does this same
thing:
>>data = range(10)
odds = filter(lambda x : x % 2, data)
evens = filter(lambda x : x % 2 != 1, data)
odds
[1, 3, 5, 7, 9]
>>evens
[0, 2, 4, 6, 8]
-- Paul
Sep 12 '06 #3
Jorge Godoy wrote:
Wildemar Wildenburger <wi******@freakmail.dewrites:
>I don't know how else to call what I'm currently implementing: An
object that
>behaves like a list but doesn't store it's own items but rather
pulls them
>from a larger list (if they match a certain criterion).
Changes to the filter are instantly reflected in the underlying list.
Clear enough?

It looks like you're implementing a callable to me. This is a method
that
returns results based on some input -- here your original list and
filter.
Then you'll use this method wherever you need that filtered list.
Ok, so I'm not clear enough ;) .
I don't just want to extract certain elements from a list, I want an
object that looks like a list, however all changes made to that object
are automagically reflected in the original list. (I guess that is one
of those 'if it's hard to explain, ...' cases.)

I should have included an example right away ... here goes:

# I have a list
l = [1, 2, 3, 4, 5, 6, 7]

# I then want to create a Filter instance
# (Filter beeing a *class* implemented by me)
# where isEven() returns True whenever an item of l
# should be included in f (in this case, even numbers).
# (I'm asking if something like this exists in the libs
# or elsewhere)
f = Filter(l, isEven)

# The desired behavior now goes something like this:
f
>>[2, 4, 6]
del f[1]
f
>>[2, 6]
l
>>[1, 2, 3, 5, 6, 7]
f.append(77)
f
>>[2, 6, 77]
# 77 being intentionally uneven
l
>>[1, 2, 3, 5, 6, 7, 77]
# could be [1, 2, 3, 5, 6, 77, 7] as well
# I don't care here

# and so forth ...

I think SQL views are the direct analog.
I don't believe it is generic. Nobody knows your data specs or filtering
needs.
Hence the additional function in the Filter constructor ;) . You suggest
the same thing below, so that is obviously no problem.
Use of list comprehension might make it easier to code this:

<snip elaborate example>
I sort of wanted to avoid these. Though my lists shouldn't terribly
long, so performance is not an issue so much. I simply want to avoid
having two datasets that I have to sync. Major pain, I believe.
Coding all that really is quite straight forward, but it is a lot of
mule-work. I hoped (no, I still hope) that there might be somethin like
that around already.

A bit clearer now?

bye
wildemar
Sep 12 '06 #4
Wildemar Wildenburger wrote:
Jorge Godoy wrote:
Wildemar Wildenburger <wi******@freakmail.dewrites:
>
>I don't know how else to call what I'm currently implementing: An
object that
>behaves like a list but doesn't store it's own items but rather
pulls them
>from a larger list (if they match a certain criterion).
>Changes to the filter are instantly reflected in the underlying list.
>Clear enough?
>
It looks like you're implementing a callable to me. This is a method
that
returns results based on some input -- here your original list and
filter.
Then you'll use this method wherever you need that filtered list.
>
Ok, so I'm not clear enough ;) .
I don't just want to extract certain elements from a list, I want an
object that looks like a list, however all changes made to that object
are automagically reflected in the original list. (I guess that is one
of those 'if it's hard to explain, ...' cases.)

I should have included an example right away ... here goes:

# I have a list
l = [1, 2, 3, 4, 5, 6, 7]

# I then want to create a Filter instance
# (Filter beeing a *class* implemented by me)
# where isEven() returns True whenever an item of l
# should be included in f (in this case, even numbers).
# (I'm asking if something like this exists in the libs
# or elsewhere)
f = Filter(l, isEven)

# The desired behavior now goes something like this:
f
>>[2, 4, 6]
del f[1]
f
>>[2, 6]
l
>>[1, 2, 3, 5, 6, 7]
f.append(77)
f
>>[2, 6, 77]
# 77 being intentionally uneven
l
>>[1, 2, 3, 5, 6, 7, 77]
# could be [1, 2, 3, 5, 6, 77, 7] as well
# I don't care here

# and so forth ...

I think SQL views are the direct analog.
I don't think they are. If you add a non-conforming row to a SQL view it
doesn't appear int he view. If you modify a row in an updateable view so
it no longer confirms with the view's conditions it disappears from the
view.

This is a classic cause of user perplexity - they update a record
without being aware that they are using a view and suddenly it "disappears".

Also you don't say whether changes to l are supposed to be reflected in
f in the same way that changes to f are reflected in l.

It might be better if you were to explain your use case: what is this
beast supposed to be for?
I don't believe it is generic. Nobody knows your data specs or filtering
needs.
Hence the additional function in the Filter constructor ;) . You suggest
the same thing below, so that is obviously no problem.
Use of list comprehension might make it easier to code this:
>
<snip elaborate example>
I sort of wanted to avoid these. Though my lists shouldn't terribly
long, so performance is not an issue so much. I simply want to avoid
having two datasets that I have to sync. Major pain, I believe.
Coding all that really is quite straight forward, but it is a lot of
mule-work. I hoped (no, I still hope) that there might be somethin like
that around already.

A bit clearer now?
Hardly at all. Frankly it doesn't seem as though you really know what
the specifications are yourself, so you can hardly expect us to divine
them by use of our psychic powers. Besides which, psychic powers cost
extra ;-)

So give us that use case so we get a bit more insight into why you even
see the need for such a thing and how you want it ti behave. Or is all
this just theoretical?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 12 '06 #5
Wildemar Wildenburger:
I sort of wanted to avoid these. Though my lists shouldn't terribly
long, so performance is not an issue so much. I simply want to avoid
having two datasets that I have to sync. Major pain, I believe.
Note that if you just have to scan the "view list", then you can use
the lazy:

itertool.ifilter(predicate, iterable)

Bye,
bearophile

Sep 12 '06 #6
Steve Holden wrote:
>I think SQL views are the direct analog.
I don't think they are. If you add a non-conforming row to a SQL view
it doesn't appear int he view. If you modify a row in an updateable view
so it no longer confirms with the view's conditions it disappears from
the view.

Me and my wording again: I typed 'direct analog' but meant 'somewhat
analog'.
Also you don't say whether changes to l are supposed to be reflected
in f in the same way that changes to f are reflected in l.

Right. Yes, the synchronicity (a word?) is meant to be in both directions.
It might be better if you were to explain your use case: what is this
beast supposed to be for?

I have a database of interconnected objects. Each object has a list
tuples; these tuples hold references to other objects and details about
the type of connection.
The 'views' I am imagining are sub-lists of this one list, based on the
connection types. Order is important here, that's why I'm not using sets.
BTW: I have gone the way of nested lists before and found it too rigid
and complicated to use.

>A bit clearer now?
Hardly at all. Frankly it doesn't seem as though you really know what
the specifications are yourself, so you can hardly expect us to
divine them by use of our psychic powers. Besides which, psychic
powers cost extra ;-)

Right again. I'm not sure what I want yet as I'm still exploring ideas.
If something /like/ the above had existed (which by now I'm gathering is
not the case) I'd just adapted my ideas to the specs given.

So give us that use case so we get a bit more insight into why you
even see the need for such a thing and how you want it ti behave. Or is
all this just theoretical?

No no, its very much real. Just not as fleshed out as it might be ---
I'm sort of an explorative coder; I usually don't know how something is
going to work until it works.

Thx for the effort :)
wildemar

Sep 12 '06 #7
be************@lycos.com wrote:
Note that if you just have to scan the "view list", then you can use
the lazy:

itertool.ifilter(predicate, iterable)
Yeah, had found that already; thanks anyway.
This function is actually the very reason I'm even asking this: 'If the
std libs solve this problem for, maybe they solve the next one as well'
I thought.
Well, can't always win, can you?

;)
wildemar
Sep 12 '06 #8
Wildemar Wildenburger wrote:
Steve Holden wrote:
>>I think SQL views are the direct analog.
I don't think they are. If you add a non-conforming row to a SQL view
it doesn't appear int he view. If you modify a row in an updateable
view so it no longer confirms with the view's conditions it disappears
from the view.

Me and my wording again: I typed 'direct analog' but meant 'somewhat
analog'.
>Also you don't say whether changes to l are supposed to be reflected
in f in the same way that changes to f are reflected in l.

Right. Yes, the synchronicity (a word?) is meant to be in both directions.
>It might be better if you were to explain your use case: what is this
beast supposed to be for?

I have a database of interconnected objects. Each object has a list
tuples; these tuples hold references to other objects and details about
the type of connection.
The 'views' I am imagining are sub-lists of this one list, based on the
connection types. Order is important here, that's why I'm not using sets.
BTW: I have gone the way of nested lists before and found it too rigid
and complicated to use.

>>A bit clearer now?
Hardly at all. Frankly it doesn't seem as though you really know what
the specifications are yourself, so you can hardly expect us to divine
them by use of our psychic powers. Besides which, psychic powers cost
extra ;-)

Right again. I'm not sure what I want yet as I'm still exploring ideas.
If something /like/ the above had existed (which by now I'm gathering is
not the case) I'd just adapted my ideas to the specs given.

>So give us that use case so we get a bit more insight into why you
even see the need for such a thing and how you want it ti behave. Or
is all this just theoretical?

No no, its very much real. Just not as fleshed out as it might be ---
I'm sort of an explorative coder; I usually don't know how something is
going to work until it works.
That's all very well, but you might want to Google for "YAGNI". You
still haven't given us mcuh of a clue about the real need.
Thx for the effort :)Happy to (tray to) help.
regards
Steve

--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
Sep 12 '06 #9
That's all very well, but you might want to Google for "YAGNI".
:)
You still haven't given us mcuh of a clue about the real need.
There is no "real need". I'm trying things, looking what works out
eventually. I don't do that for a living (if I did, I'd be starved by now).
Or do you mean the real use case? Because that I did do. There really
isn't anything more to it.

Ok, maybe there is (though I still think it boils down to what I have
said before):
I have objects, called Ideas. Ideas have IDs, used as primary keys in a
berkeley database. I want to connect them with connections that have a
'type', may or may not have a 'direction' and may or may not have a
'context'. I want to avoid storing the connections as separate database
objects (lets for now just accept that).

So I decided to store connections as lists of tuples inside Ideas:
[(ID, type, direction, context), (ID, type, direction, context), ...]
Those I want to Filter.

I'll gladly take any advice on how to do it better; mind you however
that the only data structure the db is supposed to deal with is the 'Idea'.

Again: I find this hard to explain, so I should probably rethink the
whole thing. ;)

wildemar
Sep 12 '06 #10
[Wildemar Wildenburger]
I don't know how else to call what I'm currently implementing: An object
that behaves like a list but doesn't store it's own items but rather
pulls them from a larger list (if they match a certain criterion).
Changes to the filter are instantly reflected in the underlying list.
Clear enough?
Here's some code to try out:

import UserList

class ListView(UserList.UserList):
@property
def data(self):
return filter(self._filter_function, self._actual_list)
def __init__(self, actual_list, filter_function=None):
self._actual_list = actual_list
self._filter_function = filter_function

### Example calls
a = range(10)
b = ListView(a, lambda x: x%2==0)
print list(b), len(b), b[2], b[:3]
a[:] = range(10,20)
print list(b), len(b), b[2], b[:3]

Raymond Hettinger

Sep 13 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: imani_technology_spam | last post by:
We need to present hierarchical data on a web page, the same way the tree view shows files in Windows Explorer. Here's the catch: that tree view needs to be bound to a SQL Server database. How...
18
by: Lorem Ipsum | last post by:
interesting! I just found a page in which Explorer's View Source does nothing! How did they do that?
1
by: jloxsom | last post by:
Hi all... New user new to VC++. I've used the AppWizard to create an SDI with the CFormclass. The "View" file is a dialog. I want to create a few EditBoxes, associate member variables to...
0
by: COMantilles | last post by:
Hi, Hi created a simple function to open a word doc on server then save it to HTML, ok for the result except that i would like to save it to "filtered" HTML without office's tags (word 2002...
2
by: Vinod I | last post by:
Hi Team, When I tryed following code, I am getting the Runtime Error as "The View State is invalid for this page and might be corrupted." Exception Details: System.Web.HttpException: The View...
2
by: | last post by:
I like the "view in browser" feature of vs.net: right click from an aspx page, and preview a working version of the page. I don't like how this can't be launched from the pages' codebehind. The...
0
by: Amil Hanish | last post by:
Earlier I had inquired as to why a normal <a hreftag didn't work when using the VS hosted site using a random local port. The more I think about it, this seems very wrong. Using the random port...
7
by: alessandro menchini | last post by:
Hello, First of all: i apologize for my english...i'm still learning... If i create a view it seems that DB2 manage this view like an updateable view. But I need to create a view "read only",...
2
by: Phaiz | last post by:
Not sure if you'd need an activex control but I'm looking for a script to change the folder view to "Classic View" within IE. I have an iframe that loads the users My Documents folder and would...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.