Please see the following code:
--------------------------------
class rev_wrap(object):
def __init__(self,l):
self.l=l
def __getitem__(self,i):
return self.l[-i-1]
class rev_subclass(list):
def __getitem__(self,i):
return list.__getitem__(self,-i-1)
if __name__=='__main__':
l=rev_wrap([1,2,3])
assert l[0]==3
assert list(l)==[3,2,1]
l=rev_subclass([1,2,3])
assert l[0]==3
assert list(l)==[3,2,1]
I know there is "reversed" and "reverse" but that's not the point. The
code fails at the last line.
Now that UserList is deprecated(not recommended) I suppose subclassing
built-in types are preferable than wrapping them. How do I properly
change the behaviours of built-in types? I think I have to override
__iter__ and next to pass the last line. Is it a good style? If so,
what is the most recommended way of implementing them?
Thank you in advance.
Jane 12 1327
Jane Austine wrote: Please see the following code: -------------------------------- class rev_wrap(object): def __init__(self,l): self.l=l def __getitem__(self,i): return self.l[-i-1]
class rev_subclass(list): def __getitem__(self,i): return list.__getitem__(self,-i-1)
if __name__=='__main__': l=rev_wrap([1,2,3]) assert l[0]==3 assert list(l)==[3,2,1]
l=rev_subclass([1,2,3]) assert l[0]==3 assert list(l)==[3,2,1]
Oh... I forgot one. assert l==[3,2,1] at this point doesn't pass
either. "print l" outputs the wrong one([1,2,3]) as well.
Fuzzyman wrote: I guess print is using the __repr__ (or __str__ ?) methods of lsit - which you will need to override as well.
Regards,
Fuzzy http://www.voidspace.org.uk/python/index.shtml
Thank you but the problem is that I have to express my intention in
duplicate places -- __iter__(along with "next"), __str__, __eq__ and so
on.
p.s. the reason I'm not sticking to reversed or even reverse : suppose
the size of the list is huge. ja***********@hotmail.com wrote: p.s. the reason I'm not sticking to reversed or even reverse : suppose the size of the list is huge.
Reversed is an iterator - it does NOT copy the list. In other words, reversed
already does pretty much what you want.
Cheers,
Nick.
--
Nick Coghlan | nc******@email.com | Brisbane, Australia
--------------------------------------------------------------- http://boredomandlaziness.skystorm.net ja***********@hotmail.com wrote: Fuzzyman wrote: I guess print is using the __repr__ (or __str__ ?) methods of lsit
- which you will need to override as well.
Regards,
Fuzzy http://www.voidspace.org.uk/python/index.shtml Thank you but the problem is that I have to express my intention in duplicate places -- __iter__(along with "next"), __str__, __eq__ and
so on.
If you are sublassing the built in types I guess you will have to
change all methods that have changed..
an alternative wuld be to not subclass object and override __getattr__
:
class rev_wrap:
def __init__(self,l):
self.l=l
def __getitem__(self,i):
return self.l[-i-1]
def __getattr__(self, attr):
return getattr(self.l, attr)
Regards,
Fuzzy http://www.voidspace.org.uk/python/index.shtml
p.s. the reason I'm not sticking to reversed or even reverse :
suppose the size of the list is huge. ja***********@hotmail.com wrote: p.s. the reason I'm not sticking to reversed or even reverse : suppose the size of the list is huge.
reversed() returns an iterator so list size shouldn't be an issue.
What problem are you actually trying to solve?
Kent
Kent Johnson wrote: ja***********@hotmail.com wrote: p.s. the reason I'm not sticking to reversed or even reverse :
suppose the size of the list is huge.
reversed() returns an iterator so list size shouldn't be an issue.
What problem are you actually trying to solve?
Kent
Oh, you are right.
Actually, it's more complicated than simple reversion. The list order
should be somewhat "twisted" and the list is big.
For example,
[1,2,3,4,5,6,7,8,9,10]
--> [10,9,8,7,6,1,2,3,4,5]
so __getitem__(self,i) => __getitem__(self,-i-1) if i<len(size)/2,
otherwise __getitem__(self,i-len(size)/2)
I'd like to have TwistedList class that takes in an original list and
pretends as if it is twisted actually. However, I have to have
duplicate codes here and there to make it act like a "list", say assert
twisted_list == [10,9,...] and for each in twisted_list and etc. ja***********@hotmail.com wrote: Kent Johnson wrote:
ja***********@hotmail.com wrote:
p.s. the reason I'm not sticking to reversed or even reverse : suppose the size of the list is huge.
reversed() returns an iterator so list size shouldn't be an issue.
What problem are you actually trying to solve?
Kent
Oh, you are right.
Actually, it's more complicated than simple reversion. The list order should be somewhat "twisted" and the list is big.
For example,
[1,2,3,4,5,6,7,8,9,10]
--> [10,9,8,7,6,1,2,3,4,5]
so __getitem__(self,i) => __getitem__(self,-i-1) if i<len(size)/2, otherwise __getitem__(self,i-len(size)/2)
I'd like to have TwistedList class that takes in an original list and pretends as if it is twisted actually. However, I have to have duplicate codes here and there to make it act like a "list", say assert twisted_list == [10,9,...] and for each in twisted_list and etc.
If you want a twisted 'view' of an existing list, then a wrapper makes most sense.
If, however, you only need the twisted version, why not simply override
list.__init__ (and extend, append etc... as required): class rev_list(list):
... def __init__(self, iterable):
... list.__init__(self, iterable[::-1])
... l = rev_list([1,2,3]) l
[3, 2, 1]
Michael
Michael Spencer wrote: ja***********@hotmail.com wrote: Kent Johnson wrote:
ja***********@hotmail.com wrote:
p.s. the reason I'm not sticking to reversed or even reverse : suppose
the size of the list is huge.
reversed() returns an iterator so list size shouldn't be an issue.
What problem are you actually trying to solve?
Kent
Oh, you are right.
Actually, it's more complicated than simple reversion. The list
order should be somewhat "twisted" and the list is big.
For example,
[1,2,3,4,5,6,7,8,9,10]
--> [10,9,8,7,6,1,2,3,4,5]
so __getitem__(self,i) => __getitem__(self,-i-1) if i<len(size)/2, otherwise __getitem__(self,i-len(size)/2)
I'd like to have TwistedList class that takes in an original list
and pretends as if it is twisted actually. However, I have to have duplicate codes here and there to make it act like a "list", say
assert twisted_list == [10,9,...] and for each in twisted_list and etc. If you want a twisted 'view' of an existing list, then a wrapper
makes most sense. If, however, you only need the twisted version, why not simply
override list.__init__ (and extend, append etc... as required): >>> class rev_list(list): ... def __init__(self, iterable): ... list.__init__(self, iterable[::-1]) ... >>> l = rev_list([1,2,3]) >>> l [3, 2, 1]
Michael
Thank you but your advice doesn't fit in my case since I want to keep
the memory usage and the initial time minimum. iterable[::-1] would
build another list and it would take big memory and time during
reversing if iterable were huge. (and the "iterable" wouldn't be
garbage-collected because I want to keep a reference to it)
In article <11**********************@f14g2000cwb.googlegroups .com>, ja***********@hotmail.com wrote: Thank you but your advice doesn't fit in my case since I want to keep the memory usage and the initial time minimum. iterable[::-1] would build another list and it would take big memory and time during reversing if iterable were huge. (and the "iterable" wouldn't be garbage-collected because I want to keep a reference to it)
If your list contains numbers (or lists of numbers), consider using
NumPy (Numeric) or Numarray, in which seq[::-1] will actually return a
"view", and not a copy.
Just ja***********@hotmail.com wrote: Thank you but your advice doesn't fit in my case since I want to keep the memory usage and the initial time minimum. iterable[::-1] would build another list and it would take big memory and time during reversing if iterable were huge. (and the "iterable" wouldn't be garbage-collected because I want to keep a reference to it)
You need to implement __iter__ method to pass your assert statement:
def __iter__(self):
return reversed(self)
With regards to style guide:
1. write empty subclass
class rev_subclass(list):
pass
2. print dir(rev_subclass) and write unit tests for every method. There
are 41 of them :) There is also __doc__ attribute you need to override.
3. Implement all the methods of rev_subclass to pass the tests.
Serge. ja***********@hotmail.com wrote: Thank you but your advice doesn't fit in my case since I want to keep the memory usage and the initial time minimum. iterable[::-1] would build another list and it would take big memory and time during reversing if iterable were huge. (and the "iterable" wouldn't be garbage-collected because I want to keep a reference to it)
1. Do you have benchmark results or a mathematical analysis to show that
duplicating the list uses too much memory, or is too slow to startup?
2. Do you have quantitative definitions for "too much" and "too slow", and
rationale to back up those numbers?
3. Do you have a benchmark to determine if attempting to reduce memory
consumption and start-up time has a detrimental effect on run-time performance?
If the answer to any of the above questions is 'no', then just do something like:
from itertools import islice, chain
def twisted_iter(orig):
halfway, skip_middle = divmod(len(orig), 2)
fwditr = islice(iter(orig), halfway + skip_middle, None)
revitr = islice(reversed(orig), halfway, None)
return chain(revitr, fwditr)
Py> from itertools import islice, chain
Py> def twisted_iter(orig):
.... halfway, skip_middle = divmod(len(orig), 2)
.... fwditr = islice(iter(orig), halfway + skip_middle, None)
.... revitr = islice(reversed(orig), halfway, None)
.... return chain(revitr, fwditr)
....
Py> list(twisted_iter(range(10)))
[4, 3, 2, 1, 0, 5, 6, 7, 8, 9]
Py> list(twisted_iter(range(11)))
[5, 4, 3, 2, 1, 0, 6, 7, 8, 9, 10]
Since twisted_iter is actually a highly-optimised twisted view of the original
list, you may want to just leave the original list alone, and create
twisted_iter's when you want them.
However, if you mainly use the twisted view, and only occasionally want the
original view, then you can twist it once, store the result, discard the
original, and twist it again to get the original back:
Py> list(twistediter(list(twistediter(range(10)))))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Py> list(twistediter(list(twistediter(range(11)))))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Those benchmarks I mentioned earlier will let you know which approach is best.
No-optimisations-without-measurements-'ly,
Nick.
--
Nick Coghlan | nc******@email.com | Brisbane, Australia
--------------------------------------------------------------- http://boredomandlaziness.skystorm.net This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Christian Seberino |
last post by:
Linux kernel style guide, Guido's C style guide and (I believe) old
K&R style recommends 8 SPACES for indent.
I finally got convinced of wisdom of 8 space indentation.
Guido also likes 8 space...
|
by: David Vaughan |
last post by:
I'm using v2.3, and trying to write to text files, but with a maximum
line length. So, if a line is getting too long, a suitable ' '
character is replaced by a new line. I'm subclassing the file...
|
by: RayS |
last post by:
I've begun a Python module to provide a complete interface to the
Meade LX200 command set, and have searched for a style/development
guide for Python Lib/site-packages type modules, but only saw...
|
by: Fredrik Lundh |
last post by:
(reposted from doc-sig, which seems to be mostly dead
these days).
over at the pytut wiki, "carndt" asked:
Are there any guidelines about conventions concerning
punctuation, text styles and...
|
by: paul.hester |
last post by:
Hi all,
I'm planning to migrate a website from ASP to ASP .NET 2.0. The old
page has a common include file that performs a series of tasks when
every page loads. I'm new to ASP .NET 2.0 and am...
|
by: hrobinson4 |
last post by:
Is there a quick and easy way to obtain the style string that the
TableItemStyle properties builds without subclassing TableItemStyle and
adding a new method that examins all the parent class's...
|
by: Darren Dale |
last post by:
I was just searching for some guidance on how to name packages and modules,
and discovered some inconsistencies on the www.python.org.
http://www.python.org/doc/essays/styleguide.html says "Module...
|
by: SpaceMarine |
last post by:
hello,
i thought i remember seeing at one point a "recommended" style guide
written for C#. this .NET/csharp guide was written by microsoft and
covered things like case, formatting, naming...
|
by: Christian Heimes |
last post by:
Rick King schrieb:
datetime.date is a C extension class. Subclassing of extension classes
may not always work as you'd expect it.
Christian
|
by: Ray |
last post by:
Hi all,
I am thinking of subclassing the standard string class so I can do
something like:
mystring str;
....
str.toLower ();
A quick search on this newsgroup has found messages by others
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |