slicing return iter?
hello everyone, I'm new here :-) sometimes I want to iterate a part of a sequence. but don't want to copy it. i.e. a = list(...) # now a is a list, and a[:] is another list, and so a[m:n] # now we do something with the 0~len(a)-3 elements of a for val in a[:-2]: #do something but, this will cause a copy on a, has some convenience way to get a iter to iterate on a part of list? i made this: class iterslice: def __init__(self, list): self.list = list def __len__(self): return len(self.list) def __getitem__(self, slice): import itertools listlen = len(self.list) range = (((slice.start + listlen) % listlen) if slice.start else 0, ((slice.stop + listlen) % listlen) if slice.stop else listlen, slice.step) return itertools.islice(self.list, *range) a = [1,2,3,4] for i in iterslice(a)[:-1:2]: print i my question is: - are there any *Standard* way to do this? (a buit-in function? a module?) - are there any better implements? thanks for attention :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: umlauts
On 10月17日, 下午9时54分, Arian Kuschki
wrote:
> Hi all
>
> this has been bugging me for a long time and I do not seem to be able to
> understand what to do. I always have problems when dealing input text that
> contains umlauts. Consider the following:
>
> In [1]: import urllib
>
> In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen";)
>
> In [3]: xml = f.read()
>
> In [4]: f.close()
>
> In [5]: print xml
> --> print(xml)
> tab_id="0" mobile_row="0" mobile_zipped="1" row="0"
> section="0">
> y data="Munich, BY"/> data=""/> data="2009-10-17"/> data="SI"/> data="Meistens
> bew kt"/> umidity data="Feuchtigkeit: 87 %"/> data="/ig/images/weather/mostly_cloudy.gif"/> ent_conditions> data="1"/> data="/ig/images/weather/chance_of_rain.gif"/> data="So."/> data="/ig/images/weather/chance_of_sno
> w.gif"/> data="Mo."/> data="Di."/> /> data="Klar"/>
>
> As you can see the umlauts in the XML are not displayed properly. When I want
> to process this text (for example with xml.sax), I get error messages because
> the parses can't read this.
>
> I've tried to read up on this and there is a lot of information on the web,
> but
> nothing seems to work for me. For example setting the coding to UTF like this:
> # -*- coding: utf-8 -*- or using the decode() string method.
>
> I always have this kind of problem when input contains umlauts, not just in
> this case. My locale (on Ubuntu) is en_GB.UTF-8.
>
> Cheers
> Arian
try this?
# vim: set fencoding=utf-8:
import urllib
import xml.sax as sax, xml.sax.handler as handler
f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen";)
xml = f.read()
xml = xml.decode("cp1252")
f.close()
class my_handler(handler.ContentHandler):
def startElement(self, name, attrs):
print "begin:", name, attrs
def endElement(self, name):
print "end:", name
sax.parseString(xml, my_handler())
--
http://mail.python.org/mailman/listinfo/python-list
Re: umlauts
On 10月18日, 上午12时14分, MRAB wrote:
> Arian Kuschki wrote:
> > Hi all
>
> > this has been bugging me for a long time and I do not seem to be able to
> > understand what to do. I always have problems when dealing input text that
> > contains umlauts. Consider the following:
>
> > In [1]: import urllib
>
> > In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen";)
>
> > In [3]: xml = f.read()
>
> > In [4]: f.close()
>
> > In [5]: print xml
> > --> print(xml)
> > > tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0"
> >> > y data="Munich, BY"/> > data=""/> > data="2009-10-17"/> > data="SI"/> > data="Meistens
> > bew kt"/> > umidity data="Feuchtigkeit: 87 %"/> > data="/ig/images/weather/mostly_cloudy.gif"/> > ent_conditions> > data="1"/> > data="/ig/images/weather/chance_of_rain.gif"/> > data="So."/> > data="/ig/images/weather/chance_of_sno
> > w.gif"/> > data="Mo."/> > data="Di."/> > /> > data="Klar"/>
>
> > As you can see the umlauts in the XML are not displayed properly. When I
> > want
> > to process this text (for example with xml.sax), I get error messages
> > because
> > the parses can't read this.
>
> > I've tried to read up on this and there is a lot of information on the web,
> > but
> > nothing seems to work for me. For example setting the coding to UTF like
> > this:
> > # -*- coding: utf-8 -*- or using the decode() string method.
>
> > I always have this kind of problem when input contains umlauts, not just in
> > this case. My locale (on Ubuntu) is en_GB.UTF-8.
>
> The string you received from the website is a bytestring and you're just
> printing it to your console, which is configured for UTF-8. However, the
> bytestring isn't valid UTF-8, so the console is replacing the invalid
> parts with the funny characters.
>
> You should decode the bytestring to Unicode and then re-encode it to
> UTF-8. I don't know what encoding the website is actually using; here
> I'm assuming ISO-8859-1:
>
> print xml.decode("iso-8859-1").encode("utf-8")
in 2.6, str.decode return unicode, so you can directly print it.
in 3.1, str.encode return bytes, so you can also directly print it.
so, just decode("cp1252"), it's enough.
--
http://mail.python.org/mailman/listinfo/python-list
Re: umlauts
On 10月18日, 上午12时50分, "Diez B. Roggisch" wrote:
> StarWing schrieb:
>
>
>
> > On 10月17日, 下午9时54分, Arian Kuschki
> > wrote:
> >> Hi all
>
> >> this has been bugging me for a long time and I do not seem to be able to
> >> understand what to do. I always have problems when dealing input text that
> >> contains umlauts. Consider the following:
>
> >> In [1]: import urllib
>
> >> In [2]: f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen";)
>
> >> In [3]: xml = f.read()
>
> >> In [4]: f.close()
>
> >> In [5]: print xml
> >> --> print(xml)
> >> >> tab_id="0" mobile_row="0" mobile_zipped="1" row="0"
> >> section="0">
> >> y data="Munich, BY"/> >> data=""/> >> data="2009-10-17"/> >> data="SI"/> >> data="Meistens
> >> bew kt"/> >> umidity data="Feuchtigkeit: 87 %"/> >> data="/ig/images/weather/mostly_cloudy.gif"/> >> ent_conditions> >> data="1"/> >> data="/ig/images/weather/chance_of_rain.gif"/> >> data="So."/> >> data="/ig/images/weather/chance_of_sno
> >> w.gif"/> >> data="Mo."/> >> data="Di."/> >> /> >> data="Klar"/>
>
> >> As you can see the umlauts in the XML are not displayed properly. When I
> >> want
> >> to process this text (for example with xml.sax), I get error messages
> >> because
> >> the parses can't read this.
>
> >> I've tried to read up on this and there is a lot of information on the
> >> web, but
> >> nothing seems to work for me. For example setting the coding to UTF like
> >> this:
> >> # -*- coding: utf-8 -*- or using the decode() string method.
>
> >> I always have this kind of problem when input contains umlauts, not just in
> >> this case. My locale (on Ubuntu) is en_GB.UTF-8.
>
> >> Cheers
> >> Arian
>
> > try this?
>
> > # vim: set fencoding=utf-8:
> > import urllib
> > import xml.sax as sax, xml.sax.handler as handler
>
> > f = urllib.urlopen("http://www.google.de/ig/api?weather=Muenchen";)
> > xml = f.read()
> > xml = xml.decode("cp1252")
> > f.close()
>
> > class my_handler(handler.ContentHandler):
> > def startElement(self, name, attrs):
> > print "begin:", name, attrs
>
> > def endElement(self, name):
> > print "end:", name
>
> > sax.parseString(xml, my_handler())
>
> This is wrong. XML is a *byte*-based format, which explicitly states
> encodings. So decoding a byte-string to a unicode-object and then
> passing it to a parser is not working in the very moment you have data that
>
> - is outside your default-system-encoding (ususally ascii)
> - the system-encoding and the declared decoding differ
>
> Besides, I don't see where the whole SAX-stuff is supposed to do
> anything the direct print and the decode() don't do - smells like
> cargo-cult to me.
>
> Diez
yes, XML is a *byte*-based format, and so as utf-8 and code-page
(cp936, cp1252, etc.). so usually XML will sign its coding at head.
but this didn't work now.
in Python2.6, sys.getdefaultcoding() return 'ascii', and I can't use
sys.setdefaultcoding(), and f.read() return a str. so it must be a
undecoded, byte-base format (i.e. raw XML data). so use the right code-
page to decode it is safe.(notice the webpage is google.de).
in Python3.1, read() returns a bytes object. so we *must* decode it,
nor we can't pass it into a parser.
--
http://mail.python.org/mailman/listinfo/python-list
Re: slicing return iter?
On 10月17日, 下午11时56分, Arnaud Delobelle wrote: > On Oct 17, 3:40 pm, StarWing wrote: > > > > > hello everyone, I'm new here :-) > > > sometimes I want to iterate a part of a sequence. but don't want to > > copy it. i.e. > > > a = list(...) > > # now a is a list, and a[:] is another list, and so a[m:n] > > # now we do something with the 0~len(a)-3 elements of a > > for val in a[:-2]: > > #do something > > > but, this will cause a copy on a, has some convenience way to get a > > iter to iterate on a part of list? > > > i made this: > > class iterslice: > > def __init__(self, list): > > self.list = list > > > def __len__(self): > > return len(self.list) > > > def __getitem__(self, slice): > > import itertools > > listlen = len(self.list) > > range = (((slice.start + listlen) % listlen) if slice.start > > else 0, > > ((slice.stop + listlen) % listlen) if slice.stop else > > listlen, > > slice.step) > > return itertools.islice(self.list, *range) > > > a = [1,2,3,4] > > for i in iterslice(a)[:-1:2]: > > print i > > > my question is: > > - are there any *Standard* way to do this? (a buit-in function? a > > module?) > > - are there any better implements? > > > thanks for attention :-) > > Check the itertools module. > > HTH > > -- > Arnaud I had checked it for serval times. maybe it's my inattention :-(. but what i could find the nearest thing is itertools.islice. but it can't process negative index -- that's supported by slice. so I need something, bind object with slice, and return a iter. I can find anything like it...:-( -- http://mail.python.org/mailman/listinfo/python-list
Re: slicing return iter?
On 10月18日, 上午9时09分, Raymond Hettinger wrote: > [StarWing] > > > > > sometimes I want to iterate a part of a sequence. but don't want to > > > > copy it. i.e. > . . . > > I had checked it for serval times. maybe it's my inattention :-(. but > > what i could find the nearest thing is itertools.islice. but it can't > > process negative index -- that's supported by slice. so I need > > something, bind object with slice, and return a iter. I can find > > anything like it...:-( > > If it really is a sequence (with len and getitem), you can write your > own indexing iterator: > > def myslice(seq, start, stop, step): >'Allow forward or backwards iteration over a subslice' >for i in range(start, stop, step): >yield seq[i] > > Raymond Thank you. but it can't support negative index :-( Terry Reedy is right. since a range (or xrange or slice etc.) don't have a length, so it can't support a negative index. so the best way to do it is that binding a range with a object. and return a iter. I think, why standard library didn't have anything like that, that will be very useful. maybe we should have a builtin functoin itertools.bslice (stands for bind slice)... -- http://mail.python.org/mailman/listinfo/python-list
Re: list to tuple and vice versa
On 10月18日, 下午12时19分, Ben Finney wrote: > Jabba Laci writes: > > Right, it was my bad. After removal the tuple() function works > > perfectly. > > Note that, though it is callable, ‘tuple’ is not a function but a type: > > >>> tuple > > >>> len > > > You can use the built-in ‘type’ type to get the type of any object: > > >>> foo = 12 > >>> type(foo) > > >>> bar = 1, 2, 3 > >>> type(bar) > > >>> type(tuple) > > >>> type(len) > > >>> type(type) > > > -- > \ “Pinky, are you pondering what I'm pondering?” “Uh, I think so | > `\ Brain, but this time, you wear the tutu.” —_Pinky and The Brain_ | > _o__) | > Ben Finney A type is always callable. call a type will call its __init__ special method (or and __new__ special method together). -- http://mail.python.org/mailman/listinfo/python-list
a gap of do....while?
okay, I think somethings dowhile is useful, but why python didn't have it? in lisp, we can (while (progn )) and in all other language we have do...while. but in python, we only can: cond = 1 while cond: cond = 0 . if : cond = 1 has any polite way to handle this? -- http://mail.python.org/mailman/listinfo/python-list
Re: a gap of do....while?
On 10月18日, 下午1时36分, Chris Rebert wrote: > On Sat, Oct 17, 2009 at 10:34 PM, Chris Rebert wrote: > > On Sat, Oct 17, 2009 at 10:22 PM, StarWing wrote: > > >> but in python, we only can: > >> cond = 1 > >> while cond: > >> cond = 0 > >> . > >> if : cond = 1 > > >> has any polite way to handle this? > > > It's essentially the same: > > > while True: > > ... > > if not cond: break > > Substituting in the appropriate conditional expression for cond of > course (your use of cond for the flag caught me off guard). > > Cheers, > Chris Thank you, this is nice than mine :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: list to tuple and vice versa
On 10月18日, 下午1时32分, Ben Finney wrote: > StarWing writes: > > On 10月18日, 下午12时19分, Ben Finney wrote: > > > Jabba Laci writes: > > > > Right, it was my bad. After removal the tuple() function works > > > > perfectly. > > > > Note that, though it is callable, ‘tuple’ is not a function but a > > > type: > > > A type is always callable. > > Yes (modulo perversions like explicitly making a non-callable type). > This matches what I said above. > > > call a type will call its __init__ special method (or and __new__ > > special method together). > > No http://docs.python.org/reference/datamodel.html#object.__new__> > http://docs.python.org/reference/datamodel.html#object.__init__>. > > -- > \ “Anyone who believes exponential growth can go on forever in a | > `\ finite world is either a madman or an economist.” —Kenneth | > _o__) Boulding | > Ben Finney Sorry for my ambiguity. I exactly mean that, but thanks for the docs :-) -- http://mail.python.org/mailman/listinfo/python-list
How to check the exists of a name?
Sometimes I want to make a simple flags. and i need to check there is a name in current scope or not (that is, we can visit this name, no matter where is it). and how to do that in python? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check the exists of a name?
On 10月18日, 下午2时37分, Chris Rebert wrote: > On Sat, Oct 17, 2009 at 11:30 PM, StarWing wrote: > > Sometimes I want to make a simple flags. and i need to check there is > > a name in current scope or not (that is, we can visit this name, no > > matter where is it). and how to do that in python? > > You should avoid needing to do that in the first place. Common > alternatives include using a dict instead of variables, depending on > your circumstances. > > Cheers, > Chris > --http://blog.rebertia.com Okay... Thank you... But if I want to do that, what shall I do? I got a idea, use a try...except statement. there are another way to do it ? (I just curious now, because I solve my problem in another way :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: strange behaviour of colon within string
On Oct 19, 9:15 pm, khany wrote:
> On 19 Oct, 13:44, khany wrote:
>
> > hello all,
>
> > i am relatively new to python and i am trying to convert a php app i
> > have over to it using googleapps.
>
> > anyway here is the problem. i poll ebay API which has in its XML " > xml version="1.0" encoding="utf-8"?> > xmlns="http://www.ebay.com/marketplace/search/v1/services";> "
>
> > however it fails to create the string UNLESS i remove the colon (:) in
> > the http section. i tried to substitute it with chr(58) but it errors
> > the same without showing why. does anyone know what i am doing wrong?
> > is this peculiar to googleapps?
>
> > thanks
>
> OK
>
> first thanks for replying so soon (most other groups out there take
> days)
>
> here is the code
>
>
> xml = ' \
> http://www.ebay.com/marketplace/
> search/v1/services"> \
> %(category)i \
> %(searchtext)s \
> \
> %(page)i \
> 10 \
> \
> ' % \
> {'category':9834, 'searchtext':"bmw", 'page':1}
> print xml
>
>
> if i run this, the page is blank (even the source) with no errors or
> anything. if i remove the colon from the string (after the http) it
> prints the string on the browser fine.
>
> HTH
you can use a tri-quote string or make sure all lines end with \:
xml1 = """
http://www.ebay.com/marketplace/\
search/v1/services">
%(category)i
%(searchtext)s
%(page)i
10
""" % \
{'category':9834, 'searchtext':"bmw", 'page':1}
xml2 = '\n\
http://www.ebay.com/marketplace/\
search/v1/services">\n\
%(category)i\n\
%(searchtext)s\n\
\n\
%(page)i\n\
10\n\
\n\
' % \
{'category':9834, 'searchtext':"bmw", 'page':1}
print xml1
print xml2
--
http://mail.python.org/mailman/listinfo/python-list
