Re: YouTube showing repr() of a tuple
Paul Boddie wrote: On 2 Apr, 16:19, Steve Holden [EMAIL PROTECTED] wrote: Both fixed by the time I managed to follow the links. There wasn't much to see, and putting apostrophes into the input didn't seem to cause proper repr() behaviour. So I suspect that the Python resemblance was coincidental. The apostrophes were being HTML-escaped (into #39;) before repr() was called. The second example I found used a Unicode character, and caused YouTube to show the repr() of a Unicode string, escape codes and all. -- http://mail.python.org/mailman/listinfo/python-list
YouTube showing repr() of a tuple
Thought this might amuse some of you: http://youtube.com/results?search_query=korect+my+speling I'd heard that YouTube uses Python, but it's fun to see proof of that, even if it comes in the form of a minor bug. -- http://mail.python.org/mailman/listinfo/python-list
Re: YouTube showing repr() of a tuple
Leif K-Brooks wrote: Thought this might amuse some of you: http://youtube.com/results?search_query=korect+my+speling Better example: http://youtube.com/results?search_query=korect+my+speling%C2%A1 -- http://mail.python.org/mailman/listinfo/python-list
Re: No module named ...
gtb wrote: I was having trouble with the 'no module named' error message when trying to import and noticed that other successful imports were pulling from .py files that had the dot replaced with $ and .class appended to the name. Actually in one case it picked up a .pyc file then created a .class file. How do I create a .class file from my .py file so that functions can be imported. Sounds like a Jython implementation detail, like CPython's .pyc files. Those files should presumably be created automatically the first time your module is imported. The problem importing your module is probably caused by something else, like an incorrect sys.path. -- http://mail.python.org/mailman/listinfo/python-list
Re: dict.items() vs dict.iteritems and similar questions
Laurent Pointal wrote: Both work, you may prefer xrange/iteritems for iteration on large collections, you may prefer range/items when processing of the result value explicitly need a list (ex. calculate its length) or when you are going to manipulate the original container in the loop. xrange actually supports len(): len(xrange(10)) 10 -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting a list to a dictionary
Samuel wrote: This does not work: res_dict = dict([r.get_id(), r for r in res_list]) This does: res_dict = dict([(r.get_id(), r) for r in res_list]) -- http://mail.python.org/mailman/listinfo/python-list
Re: UTF-8
Laurent Pointal wrote: You should prefer to put # -*- coding: utf-8 -*- at the begining of your sources files. With that you are ok with all Python installations, whatever be the defautl encoding. Hope this will become mandatory in a future Python version. The default encoding specifies how Unicode strings are implicitly converted into byte strings. The code you gave specifies how Unicode string literals in a file are encoded, which is something completely different. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to Read Bytes from a file
Alex Martelli wrote: You should probaby prepare before the loop a mapping from char to number of 1 bits in that char: m = {} for c in range(256): m[c] = countones(c) Wouldn't a list be more efficient? m = [countones(c) for c in xrange(256)] -- http://mail.python.org/mailman/listinfo/python-list
Re: Lists: Converting Double to Single
[EMAIL PROTECTED] wrote: So I have lists that look like this: [1, 2, 3, 4, 5]. When I concatenate lists, I end up with a list of lists that looks like this: [[1, 2, 3. 4, 5]. [6, 7. 8, 9. 10]]. Really? [1, 2, 3, 4, 5] + [6, 7, 8, 9, 10] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Then, I average the column values so I end up with a single list, but with two brackets on each end, for example, [[3.5, 4.5, 5.5, 6.5, 7.5]]. Unfortunately, when I try to use that last list in a NumPy function, I'm told that it cannot be broadcast to the correct shape. So, what I want to do is strip the extra brackes from each end to leave just [3.5, 4.5, 5.5, 6.5, 7.5]. l = l[0] -- http://mail.python.org/mailman/listinfo/python-list
Re: About getattr()
Jm lists wrote: Since I can write the statement like: print os.path.isdir.__doc__ Test whether a path is a directory Why do I still need the getattr() func as below? print getattr(os.path,isdir).__doc__ Test whether a path is a directory You don't. getattr() is only useful when the attribute name is determined at runtime. -- http://mail.python.org/mailman/listinfo/python-list
Re: begin to parse a web page not entirely downloaded
k0mp wrote: Is there a way to retrieve a web page and before it is entirely downloaded, begin to test if a specific string is present and if yes stop the download ? I believe that urllib.openurl(url) will retrieve the whole page before the program goes to the next statement. Use urllib.urlopen(), but call .read() with a smallish argument, e.g.: foo = urllib.urlopen('http://google.com') foo.read(512) 'htmlhead ... foo.read(512) will return as soon as 512 bytes have been received. You can keep caling it until it returns an empty string, indicating that there's no more data to be read. -- http://mail.python.org/mailman/listinfo/python-list
Re: begin to parse a web page not entirely downloaded
k0mp wrote: It seems to take more time when I use read(size) than just read. I think in both case urllib.openurl retrieve the whole page. Google's home page is very small, so it's not really a great test of that. Here's a test downloading the first 512 bytes of an Ubuntu ISO (beware of wrap): $ python -m timeit -n1 -r1 import urllib urllib.urlopen('http://ubuntu.cs.utah.edu/releases/6.06/ubuntu-6.06.1-desktop-i386.iso').read(512) 1 loops, best of 1: 596 msec per loop -- http://mail.python.org/mailman/listinfo/python-list
Re: default mutable arguments
Gigs_ wrote: I read that this is not the same: def functionF(argString=abc, argList = None): if argList is None: argList = [] # this ... def functionF(argString=abc, argList=None): argList = argList or [] # and this ... Why? If argList is a false value besides None (, [], {}, False, etc.), the second example will replace it with an empty list. -- http://mail.python.org/mailman/listinfo/python-list
Re: def obj()
Gert Cuykens wrote: def obj(): return {'data':'hello', 'add':add(v)} def add(v): data=data+v if __name__ == '__main__': test=obj() test.add('world') print test.data I don't know why but i have one of does none class c programing style moods again. I was wondering if the following was possible without using a class ? def obj(): result = {'data': 'hello'} result['add'] = adder(result) return result def adder(obj): def add(value): obj['data'] += value return add if __name__ == '__main__': test = obj() test['add']('world') print test['data'] -- http://mail.python.org/mailman/listinfo/python-list
Re: 'IF' Syntax For Alternative Conditions
[EMAIL PROTECTED] wrote: However, I cannot find, nor create by trial-and-error, the syntax for alternative conditions that are ORed; e.g., if cond1 OR if cond2: do_something. if cond1 or cond2: do_something() -- http://mail.python.org/mailman/listinfo/python-list
Re: HTMLParser's start_tag method never called ?
ychaouche wrote: class ParseurHTML(HTMLParser): def __init__(self): HTMLParser.__init__(self) def start_body(self,attrs): print this is my body def start_tag(self, name, attrs): if name == 'body': print this is my body -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert from unicode chars to HTML entities
Steven D'Aprano wrote: I have a string containing Latin-1 characters: s = u© and many more... I want to convert it to HTML entities: result = copy; and many more... Decimal/hex escapes would be acceptable: #169; and many more... #xA9; and many more... s = u© and many more... s.encode('ascii', 'xmlcharrefreplace') '#169; and many more...' -- http://mail.python.org/mailman/listinfo/python-list
Re: Random passwords generation (Python vs Perl) =)
NoName wrote: from random import choice import string print ''.join([choice(string.letters+string.digits) for i in range(1,8)]) !!generate password once :( So add a while true: line. who can write this smaller or without 'import'? Why are those your goals? -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert from unicode chars to HTML entities
Steven D'Aprano wrote: A few issues: (1) It doesn't seem to be reversible: '#169; and many more...'.decode('latin-1') u'#169; and many more...' What should I do instead? Unfortunately, there's nothing in the standard library that can do that, as far as I know. You'll have to write your own function. Here's one I've used before (partially stolen from code in Python patch #912410 which was written by Aaron Swartz): from htmlentitydefs import name2codepoint import re def _replace_entity(m): s = m.group(1) if s[0] == u'#': s = s[1:] try: if s[0] in u'xX': c = int(s[1:], 16) else: c = int(s) return unichr(c) except ValueError: return m.group(0) else: try: return unichr(name2codepoint[s]) except (ValueError, KeyError): return m.group(0) _entity_re = re.compile(r(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));) def unescape(s): return _entity_re.sub(_replace_entity, s) (2) Are XML entities guaranteed to be the same as HTML entities? XML defines one entity which doesn't exist in HTML: apos;. But xmlcharrefreplace only generates numeric character references, and those should be the same between XML and HTML. (3) Is there a way to find out at runtime what encoders/decoders/error handlers are available, and what they do? From what I remember, that's not possible because the codec system is designed so that functions taking names are registered instead of the names themselves. But all of the standard codecs are documented at http://python.org/doc/current/lib/standard-encodings.html, and all of the standard error handlers are documented at http://python.org/doc/current/lib/codec-base-classes.html. -- http://mail.python.org/mailman/listinfo/python-list
Re: __getattr__ equivalent for a module
Maksim Kasimov wrote: so my question is: how to tune up a module get default attribute if we try to get access to not actually exists attribute of a module? You could wrap it in an object, but that's a bit of a hack. import sys class Foo(object): def __init__(self, wrapped): self.wrapped = wrapped def __getattr__(self, name): try: return getattr(self.wrapped, name) except AttributeError: return 'default' sys.modules[__name__] = Foo(sys.modules[__name__]) -- http://mail.python.org/mailman/listinfo/python-list
Legally correct way of copying stdlib module?
I'm writing a package where I need to use the uuid module. Unfortunately, that module is only available in Python 2.5, and my package needs to be compatible with 2.4. I'm planning to copy it from Python 2.5's stdlib into my package, and import it like this: try: import uuid except ImportError: from mypackage import _uuid as uuid However, I'm concerned about how to do this in a legal way. uuid.py is presumably released under the Python license, but the file itself includes no mention of this. Should I copy the Python license as a comment into my _uuid.py? Do I need a copyright statement too (and if so, how do I know who owns the copyright to that particular module)? -- http://mail.python.org/mailman/listinfo/python-list
Re: Colons, indentation and reformatting.
Paddy wrote: Thinking about it a little, it seems that a colon followed by non-indented code that has just been pasted in could also be used by a Python-aware editor as a flag to re-indent the pasted code. How would it reindent this code? if foo: print Foo! if bar: print Bar! Like this? if foo: print Foo! if bar: print Bar! Or like this? if foo: print Foo! if bar: print Bar! -- http://mail.python.org/mailman/listinfo/python-list
Re: How to test if two strings point to the same file or directory?
Tim Chase wrote: Comparing file system paths as strings is very brittle. Why do you say that? Are you thinking of something like this? /home//user/somedirectory/../file /home/user/file Or even ~/file ~ is interpreted as my home directory by the shell, but when it's used in a path, it has no special meaning. open('~/foo.txt') tries to open a file called foo.txt in a subdirectory of the current directory called '~'. -- http://mail.python.org/mailman/listinfo/python-list
Re: YouTube written in Python
Terry Reedy wrote: In a thread on the PyDev list, Guido van Rossum today wrote: And I just found out (after everyone else probably :-) that YouTube is almost entirely written in Python. (And now I can rub shoulders with the developers since they're all Googlers now... :-) Interesting. I wonder what they're using for a Web framework? Of course, sites that size generally use lots of custom stuff, but it would presumably be based on something. -- http://mail.python.org/mailman/listinfo/python-list
Re: Question about unreasonable slowness
[EMAIL PROTECTED] wrote: i = 0 while (i 20): i = i + 1 for i in xrange(20): (shellIn, shellOut) = os.popen4(/bin/sh -c ':') # for testing, the spawned shell does nothing print 'next' # for line in shellOut: # print line On my system (AIX 5.1 if it matters, with Python 2.4.3), this simple loop spawning 20 subshells takes .75 sec. Ok, that's reasonable. Now, if I uncomment the two commented lines, which loop over the empty shellOut array, the progam now takes 11 secs. That slowdown seems very hard to believe. Why should it slow down so much? The key fact here is that shellOut isn't an array; it's a living, breathing file object. If you don't iterate over it, you can run all 20 shell processes in parallel if necessary; but if you do iterate over it, you're waiting for sh's stdout pipe to reach EOF, which effectively means you can only run one process at a time. On my system (OS X 10.4 with Python 2.5 installed), your code runs in .187 secs with the loop commented out, and in .268 secs otherwise. But I guess AIX's sh is slower than OS X's. -- http://mail.python.org/mailman/listinfo/python-list
Re: Random image text generation?
Steven D'Aprano wrote: For a text only solution, consider putting up a natural language question such as: What is the third letter of 'national'? What is four plus two? How many eggs in a dozen? Fill in the blank: Mary had a little its fleece was white as snow. Cat, Dog, Apple, Bird. One of those words is a fruit. Which one? That wouldn't work as a true CAPTCHA (Completely Automated *Public* Turing test to tell Computers and Humans Apart), since making the list of questions and answers public would defeat its purpose. -- http://mail.python.org/mailman/listinfo/python-list
Re: Random image text generation?
Ben Finney wrote: Leif K-Brooks [EMAIL PROTECTED] writes: Steven D'Aprano wrote: For a text only solution, consider putting up a natural language question That wouldn't work as a true CAPTCHA (Completely Automated *Public* Turing test to tell Computers and Humans Apart), since making the list of questions and answers public would defeat its purpose. The Public part of a CAPTCHA is the algorithm. The data consumed and produced by the algorithm don't need to be publicly correlated -- and indeed shouldn't be, for exactly the reason you state. When the CAPTCHA is based entirely on a fixed list of questions and answers, I think it's reasonable to treat that list as part of the algorithm, since the CAPTCHA couldn't function without it. Similarly, I think most people would consider an image-based CAPTCHA for which the algorithm but not the fonts were available to be non-public -- http://mail.python.org/mailman/listinfo/python-list
Re: what is @param in docstrings?
[EMAIL PROTECTED] wrote: What does the @param mean? It looks like something meant to be machine readable. Alas, googling on @param doesn't work... It looks at first like a decorator, but that doesn't make much sense. It's Epydoc's Epytext markup: http://epydoc.sourceforge.net/. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sending Dictionary via Network
Frithiof Andreas Jensen wrote: mumebuhi [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] The simplejson module is really cool and simple to use. This is great! JUST what I need for some configuration files!! Thanks for the link (die, configparse, dieee). I would personally use YAML for configuration files instead of JSON, because it's more human-readable. But it's a matter of personal preference. -- http://mail.python.org/mailman/listinfo/python-list
Re: The format of filename
Neil Cerutti wrote: Where can I find documentation of what Python accepts as the filename argument to the builtin function file? Python will accept whatever the OS accepts. As an example, I'm aware (through osmosis?) that I can use '/' as a directory separator in filenames on both Unix and Dos. But where is this documented? It's documented in the OS's documentation. It can be queried with os.sep and os.altsep. -- http://mail.python.org/mailman/listinfo/python-list
Re: The format of filename
Neil Cerutti wrote: Is translation of '/' to '\\' a feature of Windows or Python? It's a feature of Windows, but it isn't a translation. Both slashes are valid path separators on Windows; backslashes are just the canonical form. -- http://mail.python.org/mailman/listinfo/python-list
Re: pretty basic instantiation question
[EMAIL PROTECTED] wrote: let's say i have a class, and i need to create a different number of instances (changes every time - and i can't know the number in advance) in a loop. a function receives the number of instances that are needed, and creates them like, a=Myclass() b=Myclass() def create_instances(n): return [Myclass() for i in xrange(n)] -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get each pixel value from a picture file!
Lucas wrote: 1)I just copy the tutorial to run print pix[44,55]. I really dont know why they wrote that?! What tutorial? Where does it say that? -- http://mail.python.org/mailman/listinfo/python-list
Re: operator overloading + - / * = etc...
Paul Rubin wrote: The symbols on the left side of = signs are called variables even in Haskell, where they don't vary (you can't change the value of a variable once you have set it). FWIW, that's the original, mathematical meaning of the word 'variable'. They _do_ vary, but only when you call the function with different arguments (which happens frequently in Haskell, which uses recursion in place of loops). -- http://mail.python.org/mailman/listinfo/python-list
Re: A Universe Set
Jorgen Grahn wrote: - infinite xrange()s itertools.count()? -- http://mail.python.org/mailman/listinfo/python-list
Re: Looping over a list question
Tim Williams wrote: Maybe def myfunc(txt): ... print txt ... datafiles = ['1.txt','2.txt','3.txt','4.tst'] null = [myfunc(i) for i in datafiles if '.txt' in i] 1.txt 2.txt 3.txt Ew. List comprehensions with side effects are very icky. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can't get around IndexError: list index out of range
[EMAIL PROTECTED] wrote: I'm trying to get this bit of code to work without triggering the IndexError. import shutil, os, sys if sys.argv[1] != None: ver = sys.argv[1] else: ver = '2.14' Catch it: try: ver = sys.argv[1] except IndexError: ver = '2.14' -- http://mail.python.org/mailman/listinfo/python-list
Re: How to find number of characters in a unicode string?
Lawrence D'Oliveiro wrote: Hmmm, for some reason len(uC\u0327) returns 2. Is len(unicodedata.normalize('NFC', uC\u0327)) what you want? -- http://mail.python.org/mailman/listinfo/python-list
Re: Auto color selection PIL
Gabriel Genellina wrote: Try this. It first chooses 0, 1/2, then 1/4, 3/4, then */8... It's the best I could make if you don't know the number of colors beforehand. If you *do* know how many colors, your previous response is OK. Um, that's the same thing my second suggestion does: h = hues() h.next() 0.0 h.next() 0.5 h.next() 0.25 h.next() 0.75 h.next() 0.125 Your implementation is less terse than mine, though. And I suspect it runs faster, though I haven't checked that. -- http://mail.python.org/mailman/listinfo/python-list
Re: Auto color selection PIL
Xiaolei wrote: I'm trying to plot some points in an image. Each point has an associating type and I'd like to have different colors (preferrably of high contrast) for different types. The number of types in the data file is unknown a priori. Is there a way to do this at runtime? How about: from colorsys import hsv_to_rgb def colors(n): incr = 1.0 / n hue = 0.0 for i in xrange(n): r, g, b = hsv_to_rgb(hue, 1.0, 1.0) yield int(r*255), int(g*255), int(b*255) hue += incr -- http://mail.python.org/mailman/listinfo/python-list
Re: Auto color selection PIL
Xiaolei wrote: I'm trying to plot some points in an image. Each point has an associating type and I'd like to have different colors (preferrably of high contrast) for different types. The number of types in the data file is unknown a priori. Is there a way to do this at runtime? If you don't know how many colors are needed even at run time, this code might be helpful. But it generates colors that look similar pretty quickly, so I wouldn't use it unless you have to. (Anyone know of a better algorithm for this?) from itertools import count from colorsys import hsv_to_rgb def hues(): yield 0.0 for i in count(): for j in xrange(2**i): yield (1.0 / 2**(i+1)) + ((1.0 / 2**i) * j) def colors(): for hue in hues(): r, g, b = hsv_to_rgb(hue, 1.0, 1.0) yield int(r*255), int(g*255), int(b*255) -- http://mail.python.org/mailman/listinfo/python-list
Re: unicode, bytes redux
Paul Rubin wrote: Duncan Booth explains why that doesn't work. But I don't see any big problem with a byte count function that lets you specify an encoding: u = buf.decode('UTF-8') # ... later ... u.bytes('UTF-8') - 3 u.bytes('UCS-4') - 4 That avoids creating a new encoded string in memory, and for some encodings, avoids having to scan the unicode string to add up the lengths. It requires a fairly large change to code and API for a relatively uncommon problem. How often do you need to know how many bytes an encoded Unicode string takes up without needing the encoded string itself? -- http://mail.python.org/mailman/listinfo/python-list
Re: Reverse a String?
Gregory Piñero wrote: Is my mind playing tricks on me? I really remember being able to reverse a string as in: text='greg' print text.reverse() 'gerg' That method has never existed AFAIK. Maybe you're thinking of the reverse() method on lists? In any case, the you can reverse strings in a couple of ways: ''.join(reversed('foo')) 'oof' 'foo'[::-1] 'oof' -- http://mail.python.org/mailman/listinfo/python-list
Re: Python programs always open source?
Ben Finney wrote: So long as you're not distributing some or all of Python itself, or a derivative work, the license for Python has no legal effect on what license you choose for your own work. How many Python programs use nothing from the standard library? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python programs always open source?
Steve Holden wrote: Leif K-Brooks wrote: Ben Finney wrote: So long as you're not distributing some or all of Python itself, or a derivative work, the license for Python has no legal effect on what license you choose for your own work. How many Python programs use nothing from the standard library? That doesn't matter either. The required copyright statements are included in the Python interpreter. There is no reason to fear distributing Python as a part of either open source or proprietary works, and Guido framed the original license to allow precisely such uses. Later versions have retained the same spirit. Yes, I know that. I was replying to Ben Finney's claim that in a hypothetical world where Python was licensed under the GPL, there would still be no restriction on distributing Python programs under a closed-source license. -- http://mail.python.org/mailman/listinfo/python-list
Re: Pythondocs.info : collaborative Python documentation project
[EMAIL PROTECTED] wrote: I am a bit disapointed with the current Python online documentation. I have read many messages of people complaining about the documentation, it's lack of examples and the use of complicated sentences that you need to read 10 times before understanding what it means. That's why I have started a collaborative project to make a user contributed Python documentation. The wiki is online here: http://www.pythondocs.info I agree that Python's docs could use improvement, and I love the idea of using a Wiki for the purpose. But maybe MediaWiki would be a better choice of software? Dokuwiki's syntax looks foreign and a little bit intimidating to me, but Wikipedia has made pretty much everyone familiar with MediaWiki's syntax. Less syntax to learn lowers the cost of entry, which should lead to more contributors. -- http://mail.python.org/mailman/listinfo/python-list
Re: function v. method
danielx wrote: This is still a little bit of magic, which gets me thinking again about the stuff I self-censored. Since the dot syntax does something special and unexpected in my case, why not use some more dot-magic to implement privates? Privates don't have to be entirely absent from Klass.__dict__ (which would make Python not introspective); they can just be invisible when using the dot-syntax. You can do this now, kind of: class Foo(object): ... x = property() ... def doStuffWithX(self): ... self.__dict__['x'] = 123 ... print self.__dict__['x'] ... bar = Foo() bar.doStuffWithX() 123 bar.x Traceback (most recent call last): File stdin, line 1, in ? AttributeError: unreadable attribute If you're proposing that self.x and bar.x should give different results, then that's quite a bit more magic than property() and methods use. They both use the descriptor API; for more information on that, read http://python.org/download/releases/2.2.3/descrintro/. -- http://mail.python.org/mailman/listinfo/python-list
Re: range() is not the best way to check range?
Grant Edwards wrote: Using xrange as somebody else suggested is also insane. Sorry about that, I somehow got the misguided notion that xrange defines its own __contains__, so that it would be about the same speed as using comparison operators directly. I figured the OP might have a better reason for wanting to use range() than his post mentioned -- perhaps the range to check was being passed from a function, and it would be easier to pass an object than a tuple of lower and upper bound -- but since xrange does looping for a membership test, my suggestion was indeed insane. -- http://mail.python.org/mailman/listinfo/python-list
Re: range() is not the best way to check range?
[EMAIL PROTECTED] wrote: or is there an alternative use of range() or something similar that can be as fast? You could use xrange: [EMAIL PROTECTED]:~$ python -m timeit -n1 1 in range(1) 1 loops, best of 3: 260 usec per loop [EMAIL PROTECTED]:~$ python -m timeit -n1 1 in xrange(1) 1 loops, best of 3: 0.664 usec per loop -- http://mail.python.org/mailman/listinfo/python-list
Re: TSV to HTML
Brian wrote: I was wondering if anyone here on the group could point me in a direction that would expllaing how to use python to convert a tsv file to html. I have been searching for a resource but have only seen information on dealing with converting csv to tsv. Specifically I want to take the values and insert them into an html table. import csv from xml.sax.saxutils import escape def tsv_to_html(input_file, output_file): output_file.write('tabletbody\n') for row in csv.reader(input_file, 'excel-tab'): output_file.write('tr') for col in row: output_file.write('td%s/td' % escape(col)) output_file.write('/tr\n') output_file.write('/tbody/table') Usage example: from cStringIO import StringIO input_file = StringIO('foo\tbar\tbaz\n' ... 'qux\tquux\tquux\n') output_file = StringIO() tsv_to_html(input_file, output_file) print output_file.getvalue() tabletbody trtdfoo/tdtdbar/tdtdbaz/td/tr trtdqux/tdtdquux/tdtdquux/td/tr /tbody/table -- http://mail.python.org/mailman/listinfo/python-list
Re: enumerate() question
Gregory Petrosyan wrote: Hello! I have a question for the developer[s] of enumerate(). Consider the following code: for x,y in coords(dots): print x, y When I want to iterate over enumerated sequence I expect this to work: for i,x,y in enumerate(coords(dots)): print i, x, y Unfortunately, it doesn't =( and I should use (IMHO) ugly for i,pair in enumerate(coords(dots)): print i, pair[0], pair[1] Use: for i, (x, y) in enumerate(coords(dots)): print i, x, y So, why enumerate() works this way and is there any chance of changing the behaviour? Because enumerate has no way to distinguish between iterables you do and don't want unpacked. So, for example, this wouldn't work under your proposal: for index, string in [foo, bar, baz]: print String number %s is %s. % (index, string) But this would: for index, x, y, z in [foo, bar, baz]: print First character of string number %s is %s. % (index, x) -- http://mail.python.org/mailman/listinfo/python-list
Re: which is better, string concatentation or substitution?
fuzzylollipop wrote: niether .join() is the fastest Please quote what you're replying to. No, it's the slowest: [EMAIL PROTECTED]:~$ python -m timeit 'p%s/p\n\n' % 'foobar' 100 loops, best of 3: 0.607 usec per loop [EMAIL PROTECTED]:~$ python -m timeit 'p' + 'foobar' + '/p\n\n' 100 loops, best of 3: 0.38 usec per loop [EMAIL PROTECTED]:~$ python -m timeit ''.join(['p', 'foobar', '/p\n\n']) 100 loops, best of 3: 0.817 usec per loop -- http://mail.python.org/mailman/listinfo/python-list
Re: Multi-line lambda proposal.
Kaz Kylheku wrote: But suppose that the expression and the multi-line lambda body are reordered? That is to say, the expression is written normally, and the mlambda expressions in it serve as /markers/ indicating that body material follows. This results in the most Python-like solution. I think your proposal is good, but it might be more easily understood if we gave names to the lambdas, so that they could be easily picked out in the source code. It could look something like this: func(lambda foo): foo: print Foo! Come to think of it, maybe we could put the lambdas before the statement they're used in instead of in front of it. That could look something like this: lambda foo: print Foo! func(lambda foo) Then again, it would probably be simpler to reuse an existing block-starting keyword for the purpose. Maybe something like this? def foo(): print Foo! func(foo) -- http://mail.python.org/mailman/listinfo/python-list
Re: which is better, string concatentation or substitution?
John Salerno wrote: My initial feeling is that concatenation might take longer than substitution Doesn't look that way: [EMAIL PROTECTED]:~$ python -m timeit 'p%s/p\n\n' % 'foobar' 100 loops, best of 3: 0.6 usec per loop [EMAIL PROTECTED]:~$ python -m timeit 'p' + 'foobar' + '/p\n\n' 100 loops, best of 3: 0.358 usec per loop but that it is also easier to read: I prefer string formatting for readability, but it's a matter of personal preference. -- http://mail.python.org/mailman/listinfo/python-list
Re: scope of variables
Gary Wessle wrote: the example was an in-accuretlly representation of a the problem I am having. my apologies. a = [] def prnt(): print len(a) prnt function prnt at 0xb7dc21b4 I expect to get 0 the length of list a Python requires parenthesis to call a function. a = [] def prnt(): ... print len(a) ... prnt function prnt at 0xb7dcad84 prnt() 0 -- http://mail.python.org/mailman/listinfo/python-list
Re: modifying iterator value.
chun ping wang wrote: i want to modify an iterator value. for x in someList x = 1 for index, value in enumerate(someList): someList[index] = 1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Inexplicable behaviour of type 'function'
Fabiano Sidler wrote: Have a look to the following lines of code: --- snip --- class Foo: pass def bar(): pass Foo.bar = bar --- snap --- Why does 'bar.__get__(Foo) is Foo.bar' evaluate to False here? Did I misunderstand the descriptor protocol? bar.__get__(None, Bar) is what you meant (the first argument is the object, not the type), but even then the result will be False, because the __get__ method on functions returns a different object each time it's called: class Foo(object): ... def bar(self): ... pass ... Foo.bar is Foo.bar False -- http://mail.python.org/mailman/listinfo/python-list
Re: How to determine if a line of python code is a continuation of the line above it
Sandra-24 wrote: I'm not sure how complex this is, I've been brainstorming a little, and I've come up with: from tokenize import generate_tokens, NL, NEWLINE from cStringIO import StringIO def code_lines(source): Takes Python source code (as either a string or file-like object) and yields a tuple of (is_new_logical, code) for each physical line of code. if isinstance(source, basestring): source = StringIO(source) buffer = [] new_logical = True for token_type, source, sloc, eloc, line in \ generate_tokens(source.readline): buffer.append(source) if token_type == NL: yield new_logical, ''.join(buffer) buffer = [] new_logical = False elif token_type == NEWLINE: yield new_logical, ''.join(buffer) buffer = [] new_logical = True if buffer: yield new_logical, ''.join(buffer) -- http://mail.python.org/mailman/listinfo/python-list
Re: how to convert string
[EMAIL PROTECTED] wrote: I want to print number 0 to 9 in one line like this 0 1 2 3 4 5 6 7 8 9 if I do like this, it prints in different lines for i in xrange(10): print i for i in xrange(10): print i, so i tried like this str = for i in xrange(10): str = i + print str but i want to know how convert int i to string. s = # Don't shadow the str builtin. for i in xrange(10): s += str(i) + print s -- http://mail.python.org/mailman/listinfo/python-list
Re: slicing the end of a string in a list
Ben Cartwright wrote: No, since the line variable is unused. This: i = 0 for line in switches: line = switches[i][:-1] i += 1 Would be better written as: for i in range(len(switches)): switches[i] = switches[i][:-1] This is better, IMHO: for i, switch in enumerate(switches): switches[i] = switch[:-1] -- http://mail.python.org/mailman/listinfo/python-list
Re: Proper class initialization
Steven Bethard wrote: class A(object): def _get_sum(): return sum(xrange(10)) sum = _get_sum() What's wrong with sum = sum(xrange(10))? -- http://mail.python.org/mailman/listinfo/python-list
Python shell interpreting delete key as tilde?
I'm running Python 2.3.5 and 2.4.1 under Debian Sarge. Instead of deleting the character after the cursor, pressing my delete key in an interactive Python window causes a system beep and inserts a tilde character. This behavior occurs across all of the terminals I've tried (xterm, Konsole, real Linux terminal), but does not occur when running bash instead of python. Other Readline-provided functionality (moving the cursor, history) seems to work fine. The delete key was working fine in Python up until a few days ago, and I don't remember making any changes that should have affected it. If anyone knows what could be causing this problem, please explain. It's driving me crazy! -- http://mail.python.org/mailman/listinfo/python-list
Re: how to convert string like '\u5927' to unicode string u'\u5927'
Chris Song wrote: Here's my solution _unicodeRe = re.compile((\\\u[\da-f]{4})) def unisub(mo): return unichr(int(mo.group(1)[2:],16)) unicodeStrFromNetwork = '\u5927' unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork) But I think there must be a more straightforward way to do it. '\u5927'.decode('unicode_escape') u'\u5927' -- http://mail.python.org/mailman/listinfo/python-list
Re: OO in Python? ^^
Heiko Wundram wrote: Fredrik Lundh wrote: Matthias Kaeppler wrote: polymorphism seems to be missing in Python QOTW! Let's have some UQOTW: the un-quote of the week! ;-) +1 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to execute an EXE via os.system() with spaces in the directory name?
Leif K-Brooks wrote: It's perfectly reasonable behavior, and it also applies to Linux. The shell uses spaces to separate arguments; how do you expect it to know that you want a space to be part of the program's name unless you escape it? I'm sorry, disregard my message. I failed to read the OP properly. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to execute an EXE via os.system() with spaces in the directory name?
[EMAIL PROTECTED] wrote: This comes up from time to time. The brain damage is all Windows', not Python's. It's perfectly reasonable behavior, and it also applies to Linux. The shell uses spaces to separate arguments; how do you expect it to know that you want a space to be part of the program's name unless you escape it? Here's one thread which seems to suggest a bizarre doubling of the initial quote of the commandline. A better solution would be to use subprocess: http://python.org/doc/current/lib/module-subprocess.html. -- http://mail.python.org/mailman/listinfo/python-list
Re: python speed
Krystian wrote: I would also like to see Half Life 2 in pure Python. or even quake1, do you think it could have any chances to run smoothly? If http://www.abrahamjoffe.com.au/ben/canvascape/ can run at a reasonably speed, yes. -- http://mail.python.org/mailman/listinfo/python-list
Re: Parse file into array
amfr wrote: I was wondering how i could parse the contents of a file into an array. the file would look something like this: gif:image/gif html:text/html jpg:image/jpeg ... As you can see, it contains the mime type and the file extension seperated by commas, 1 per line. I was wondering if it was possible to create and array like this: (Pseudocode) mimetypearray[gif] = image/gif mimetypearray[html] = text/html mimetypearray[jpg] = image/jpeg ... You want a dictionary, not an array. mimetypedict = {} for line in mimetypefile: line = line.rsplit('\r\n') extension, mimetype = line.split(':') mimetypedict[extension] = mimetype Note that there's already a MIME type database in the standard mimtypes module: http://python.org/doc/current/lib/module-mimetypes.html. -- http://mail.python.org/mailman/listinfo/python-list
Re: Parse file into array
Leif K-Brooks wrote: line = line.rsplit('\r\n') Er, that should be line.rstrip, not line.rsplit. -- http://mail.python.org/mailman/listinfo/python-list
Re: list of lambda
jena wrote: hello, when i create list of lambdas: l=[lambda:x.upper() for x in ['a','b','c']] then l[0]() returns 'C', i think, it should be 'A' Fredrik Lundh provided the general solution, but in this specific case, the simplest solution is: l = [x.upper for x in ['a', 'b', 'c']] -- http://mail.python.org/mailman/listinfo/python-list
Re: [ x for x in xrange(10) when p(x) ]
[EMAIL PROTECTED] wrote: George Sakkis wrote: list(takewhile(p, xrange(1000))) [0, 1] thanks. that is what I am doing now, in a more generic form : takewhile(p, (x for x in xrange(1))) How does a useless generator expression make it more generic? -- http://mail.python.org/mailman/listinfo/python-list
Re: [ x for x in xrange(10) when p(x) ]
[EMAIL PROTECTED] wrote: Leif K-Brooks wrote: [EMAIL PROTECTED] wrote: thanks. that is what I am doing now, in a more generic form : takewhile(p, (x for x in xrange(1))) How does a useless generator expression make it more generic? xrange is only picked as an example. I may be newbie on python but not that dumb if all I want is a list of integer(sorted) that meets certain criteria. takewhile(p, (x for x in some_function_that_could_potentially_generate_a_long_list_of_elements_but_first_element_that_meets_the_condition_can_come_fast(*args,**kwargs))) Wrapping a function in a generator expression doesn't magically make it lazily evaluated. The whole list has to be generated and returned; using a generator expression instead of a list comprehension just means that it doesn't need to be copied in memory. -- http://mail.python.org/mailman/listinfo/python-list
Re: Confusion about __call__ and attribute lookup
Kent Johnson wrote: But why doesn't Foo.__call__ shadow type.__call__? Normally an instance attribute takes precedence over a class attribute. Is it something special about how function call syntax is handled internally, or do all special methods work this way, or is there something else going on? New-style classes look up special methods on the class, not on the instance: class Foo(object): ... def __invert__(self): ... return 'foo' ... x = Foo() ~x 'foo' x.__invert__ = 123 x.__invert__() Traceback (most recent call last): File stdin, line 1, in ? TypeError: 'int' object is not callable ~x # equivalent to type(x).__invert__() 'foo' -- http://mail.python.org/mailman/listinfo/python-list
Re: Recompile AST?
[EMAIL PROTECTED] wrote: Is it possible to recompile the AST generated by compiler.parse, back into code or an executable code object? Into a bytecode object: from compiler.pycodegen import ModuleCodeGenerator from compiler.misc import set_filename from compiler import parse tree = parse('foo = 42') set_filename('foo', tree) code = ModuleCodeGenerator(tree).getCode() exec code foo 42 Into Python source code: http://ecritters.biz/asttosource.py. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to stop a loop with ESC key? [newbie]
mo wrote: Can somebody explain how to stop a WHILE loop in running program by pressing ESC key? On Unix-like systems try: import termios, fcntl, sys, os fd = sys.stdin.fileno() oldterm = termios.tcgetattr(fd) newattr = termios.tcgetattr(fd) newattr[3] = newattr[3] ~termios.ICANON ~termios.ECHO termios.tcsetattr(fd, termios.TCSANOW, newattr) oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) i = 0 try: while 1: print i i += 1 try: char = sys.stdin.read(1) if char == '\x1b': print Bye! break except IOError: pass finally: termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) -- http://mail.python.org/mailman/listinfo/python-list
Re: overloading *something
James Stroud wrote: Hello All, How does one make an arbitrary class (e.g. class myclass(object)) behave like a list in method calls with the *something operator? What I mean is: myobj = myclass() doit(*myobj) Make it iterable: class Foo(object): ... def __iter__(self): ... yield 1 ... yield 2 ... yield 3 ... def bar(*args): ... print args ... bar(*Foo()) (1, 2, 3) And, how about the **something operator? Use a dictionary. -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting a List into a String
[EMAIL PROTECTED] wrote: list = ['f', 'e', 'd', 'c', 'b', 'a'] How can i convert it into a string so the output is fedcba print ''.join(list) -- http://mail.python.org/mailman/listinfo/python-list
Re: I Need Motivation Part 2
Sybren Stuvel wrote: There are over 2800 header files on my system in /usr/include. What do you mean a limited number of header files? I assume he's saying that the number is ∞. (Of course, the same is true of Python modules unless you use a special __import__ hook or something...) -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I do this in Python?
Steve Holden wrote: Another alternative might be to serve a script that sent the browser back 2 pages in its history, as long as server state hasn't changed in the meantime. What about users with JavaScript disabled? -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary that have functions with arguments
Alex Martelli wrote: execfunc = { 'key1' : (func1, ()), 'key2' : (func2, args) } now, something like: f, a = execfunc[k] f(**a) will work for either key. Shouldn't func1's args be a dictionary, not a tuple? -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting 2bit hex representation to integer ?
Madhusudan Singh wrote: I am using binascii.b2a_hex to convert some binary data to hex. The result is a two bit hex representation (i. e., without the leading 0x). Surely you mean two-byte? How do I convert the resulting two bit representation into an integer ? int(foo, 16) -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie regex
[EMAIL PROTECTED] wrote: I want to filter some strings,but i don t know how to use compile method. Why not? first character must be [a-zA-z] group and others can only be digits or letters. if re.search('^[a-zA-Z][a-zA-Z0-9]*$', foo): print Valid string. else: print Invalid string. -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie regex
Leif K-Brooks wrote: [EMAIL PROTECTED] wrote: I want to filter some strings,but i don t know how to use compile method. Why not? Sorry: I misread that as not _wanting_ to use the compile method. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing an AST
beza1e1 wrote: Is it possible compiler.parse a statement, then change and then execute/resolve it? This should work: from compiler.pycodegen import ModuleCodeGenerator from compiler.misc import set_filename from compiler import parse tree = parse('foo = 42') set_filename('foo', tree) code = ModuleCodeGenerator(tree).getCode() exec code foo 42 Also, if you need to turn an AST back into Python code, I have some ugly code for doing that: http://ecritters.biz/asttosource.py. -- http://mail.python.org/mailman/listinfo/python-list
Re: os.access with wildcards
mike wrote: i'd like to use os.access(path,mode) where path may contain linux style wildcards. os.access(glob.glob(path), mode) -- http://mail.python.org/mailman/listinfo/python-list
Re: 2 class with same name in different module
Iyer, Prasad C wrote: I have a class in a module which is getting imported in main module. How do you differentiate between the 2 class import foo import bar foo.TheClass().dostuff() bar.TheClass().dostuff() -- http://mail.python.org/mailman/listinfo/python-list
Re: Python eats gator.
Sam wrote: http://www.wnbc.com/family/5060215/detail.html I know there's an on-topic joke in here somewhere, but I'm having some problem finding it, at the moment. You may take a crack at it, if you'd like… Hmm... has anyone ever written a spyware removal tool in Python? -- http://mail.python.org/mailman/listinfo/python-list
Re: Distributing programs
Jeff Schwab wrote: Sorta, but not really. Typically, you might distribute the source (.py) files, but if you don't want to do that, you can distribute the compiled .pyc files instead. Python creates these files automatically when your modules are imported. But remember that Python bytecode can be easily decompiled with a publicly-available program. -- http://mail.python.org/mailman/listinfo/python-list
Re: [Info] PEP 308 accepted - new conditional expressions
Sam wrote: And foo if bar is Perl-ish; yet, even Perl has the ? : operators. What _isn't_ Perl-ish? -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with syntax warnings
Ivan Shevanski wrote: is there a way to turn off syntax warnings or just make them not visible? import warnings warnings.filterwarnings('ignore', category=SyntaxWarning) -- http://mail.python.org/mailman/listinfo/python-list
Re: __call__ in module?
ncf wrote: I have a feeling that this is highly unlikely, but does anyone in here know if it's possible to directly call a module, or will I have to wrap it up in a class? You could use trickery with sys.modules to automatically wrap it in a class: import sys from types import ModuleType class CallableModule(ModuleType): def __call__(self): print You called me! mod = FooModule(__name__, __doc__) mod.__dict__.update(globals()) sys.modules[__name__] = mod -- http://mail.python.org/mailman/listinfo/python-list
Re: replacments for stdio?
[EMAIL PROTECTED] wrote: i was wondering if anyone have written a GUI module that can function as a replacment for stdin/stdout? ie. has a file like interface, by which one could just assaign it to sys.stdout or sys.stdin and have all your prints and raw_inputs and other such things shown in a GUI window? Xterm? -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands and return output
billiejoex wrote: Hi all. I'm searching for a portable (working on *nix and win32) function that executes a system command and encapsulate its output into a string. Searching for the web I found this: os.popen('command').read() It is perfect but when che command return an error the funciotn returns an empy string. Does it is possible to return stdout and stderr too? Use subprocess: from subprocess import Popen, PIPE proc = Popen(['command', 'arg', 'arg'], stdout=PIPE, stderr=PIPE) return_code = proc.wait() if return_code == 0: print Success:\n%s % proc.stdout.read() else: print Failure %s:\n%s % (return_code, proc.stderr.read()) -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands and return output
billiejoex wrote: Thank you for your help but I'm searching a different way. Moreover it doesn't work always (for exaple: try a 'dir' command). Because of I'm implementing a remote shell the [[os.popen('command').read()]] rapresents the best for me because it can also accepts arguments direclty (for example: os.popen('netstat -a -n -o').read() and this is a great advantage. If you really need shell evaluation, try subprocess.Popen('foo', shell=True) instead. -- http://mail.python.org/mailman/listinfo/python-list
Re: using % operator to print possibly unitialized data attributes
Adam Monsen wrote: class J: name = '' value = '' def __str__(self): vals = self.__class__.__dict__ vals.update(self.__dict__) return 'name=%(name)s value=%(value)s' % vals This will update the class's attributes with instance attributes when str() is called, which probably isn't what you want. For instance: foo = J() foo.name = Joe Bloggs print foo name=Joe Bloggs value= bar = J() print bar name=Joe Bloggs value= What's wrong with the obvious version: class J(object): name = '' value = '' def __str__(self): return 'name=%r value=%r' % (self.name, self.value) -- http://mail.python.org/mailman/listinfo/python-list
Re: reading the last line of a file
Xah Lee wrote: i switched to system call with tail because originally i was using a pure Python solution inF = gzip.GzipFile(ff, 'rb'); s=inF.readlines() inF.close() last_line=s[-1] and since the log file is 100 megabytes it takes a long time and hogs massive memory. How about: inF = gzip.GzipFile(ff, 'rb') for line in inF: pass last_line = line It's a bit slower than gzip and tail, but the memory usage is fine. -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands independantly
Mike Tammerman wrote: Hi, I am trying to execute an executable or a pyton script inside my program. I looked at the subprocess and os module. But all the functions in these modules blocks my application. subprocess doesn't block unless you call .wait(): from subprocess import Popen proc = Popen('sleep 2; echo Hello, world!', shell=True) print In two seconds, something will happen. proc.wait() print Did you see that? -- http://mail.python.org/mailman/listinfo/python-list
Re: Replacement for lambda - 'def' as an expression?
Sybren Stuvel wrote: It also allows for dynamic function creation in cases where a name would not be available. What cases are those? -- http://mail.python.org/mailman/listinfo/python-list
Re: Generators and Decorators doing my head in ..
[EMAIL PROTECTED] wrote: Im trying to create a decorator that counts the number of times a function is run. Your code doesn't work because decorators are run at function creation time, not at function run time. Try this instead: from itertools import count def logFunctionCalls(function): times = count() def newfunction(*args, **kwargs): print Entering function:, function.__name__, times.next() return function(*args, **kwargs) newfunction.__doc__ = function.__doc__ newfunction.__name__ = function.__name__ return newfunction @logFunctionCalls def doWork(): print Doing work... -- http://mail.python.org/mailman/listinfo/python-list
Re: Any projects to provide Javascript-style client-side browser access via Python?
Kenneth McDonald wrote: I'm curious about this because, quite aside their function as web browsers, it is now possible to build some very useable interfaces using browsers with HTML, CSS, and JavaScript. (The biggest problem is still the lack of a decent text widget.) However, JavaScript isn't really a good language for building complex applications, and it would be very cool if there were some way to use Python to replace client-side JavaScript, in order to gain access the DOM. You could try Livepage, which is a part of Nevow http://nevow.com/. It doesn't cut JavaScript completely out of the picture, but you could put all of your real code in Python and only use JavaScript as a simple layer to access the DOM. -- http://mail.python.org/mailman/listinfo/python-list