Re: Flexible string representation, unicode, typography, ...
Le samedi 25 août 2012 02:24:35 UTC+2, Antoine Pitrou a écrit : > Ramchandra Apte gmail.com> writes: > > > > > > The zen of python is simply a guideline > > > > What's more, the Zen guides the language's design, not its implementation. > > People who think CPython is a complicated implementation can take a look at > PyPy > > :-) Unicode design: a flat table of code points, where all code points are "equals". As soon as one attempts to escape from this rule, one has to "pay" for it. The creator of this machinery (flexible string representation) can not even benefit from it in his native language (I think I'm correctly informed). Hint: Google -> "Das grosse Eszett" jmf -- http://mail.python.org/mailman/listinfo/python-list
Re: Flexible string representation, unicode, typography, ...
wxjmfa...@gmail.com writes: > Unicode design: a flat table of code points, where all code > points are "equals". Yes, Unicode's design entails a flat table of hundreds of thousands of code points, expansible in future. This is in direct conflict with the design of all significant computers we need to write software for: data stored and transported as 8-bit bytes, which can only ever hold 256 different values, no expansion. > As soon as one attempts to escape from this rule, one has to > "pay" for it. Yes, in either direction; the conflict means that trade-offs need to be made. See this presentation by Ned Batchelder, “Pragmatic Unicode” http://nedbatchelder.com/text/unipain.html>, which lays out the fundamental conflict of representing human text in computer data; and several practical approaches to deal with it. -- \ “I busted a mirror and got seven years bad luck, but my lawyer | `\thinks he can get me five.” —Steven Wright | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Publish unittest results from test discovery
On 25/08/2012 02:03, Steven D'Aprano wrote: On Fri, 24 Aug 2012 17:25:05 -0700, Lucretiel wrote: [...] Is there a way to get unittest disover to work with xmlrunner Steady on there! It's only been about an hour and a half since you last asked this exact same question, almost word-for-word identical. The more specialised the question, the longer it may take for somebody who knows the answer to reply. For something like this, I would wait at least a couple of days before replying to your original post. (Don't just re-post the question in a new thread, keep the response in a single thread.) I have no idea about xmlrunner and unittest discovery, sorry. I entirely agree with Steven's comments. I'd put a question like this on a specialised list. How about gmane.comp.python.testing.general ? Sorry I've no idea what it's called elsewhere. -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Objects in Python
On 25/08/2012 07:34, Chris Angelico wrote: On Sat, Aug 25, 2012 at 1:04 PM, Steven D'Aprano I'm just wondering out aloud if the number of times this type of thread has been debated here will fit into a Python long or float? -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Flexible string representation, unicode, typography, ...
On 25/08/2012 08:27, wxjmfa...@gmail.com wrote: Le samedi 25 août 2012 02:24:35 UTC+2, Antoine Pitrou a écrit : Ramchandra Apte gmail.com> writes: The zen of python is simply a guideline What's more, the Zen guides the language's design, not its implementation. People who think CPython is a complicated implementation can take a look at PyPy :-) Unicode design: a flat table of code points, where all code points are "equals". As soon as one attempts to escape from this rule, one has to "pay" for it. The creator of this machinery (flexible string representation) can not even benefit from it in his native language (I think I'm correctly informed). Hint: Google -> "Das grosse Eszett" jmf It's Saturday morning, I'm stone cold sober, had a good sleep and I'm still baffled as to the point if any. Could someone please enlightem me? -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Segfault when setting an instance property on 2.7.3
Hi. (please keep me in CC for replies, I'm not subscribed) I wrote a ctypes-(wait, read on)-based binding[1] for libusb1, in which I'm triggering a segfault from an application[2] I wrote. I've been through several segfault caused by ctypes mis-usage, this one seems different enough. I think there is something else (maybe ultimately caused by some ctypes effect, but I don't see the relation yet). The Python line causing the segfault: https://github.com/vpelletier/python-libusb1/blob/master/usb1.py#L192 C stack at segfault (with -dbg package installed): http://pastebin.com/rVUPsSrU #0 (gdb) print *op $1 = {ob_refcnt = -4247522206314328575, ob_type = 0xcf0dc50ec50dc50e} (gdb) up #1 (gdb) print *obj $2 = {ob_refcnt = 6, ob_type = 0x9c5f70} (gdb) print obj $3 = The program using python-libusb1 which triggers the segfault: https://github.com/vpelletier/ITI1480A-linux/blob/master/iti1480a/capture.py The event loop is at the bottom: allocate USB transfers, submit them, loop on libusb1 event handling until there is no more submitted transfer, libusb uses callback which resubmits transfer, ... ctypes possible segfault causes checklist: - callback is cast into a ctype CFUNCTYPE type instance See: https://github.com/vpelletier/python-libusb1/blob/master/libusb1.py#L587 https://github.com/vpelletier/python-libusb1/blob/master/usb1.py#L133 - a strong ref to it is kept on USBTransfer instance so it is not GC'ed See: https://github.com/vpelletier/python-libusb1/blob/master/usb1.py#L808 - application is single-threaded (libusb1 doesn't create any C thread either) so even if there were missing GIL acquisitions, it shouldn't be a problem Also, a strong ref to USBTransfer is kept on USBDeviceHandle instance. When an USBDeviceHandle is GC'ed, it cancels any pending transfer, waits for completion (=libusb1 callback is executed) and then allow them to be GC'ed. - we are not accessing unallocated memory in this traceback (although it could be that memory got overwritten somehow) I couldn't trigger the bug while under valgrind (which reported some "Conditional jump or move depends on uninitialized value(s)" & "Use of uninitialized value of size 8" in PyObject_Free, but reading the code I guess they are harmless and unrelated). Any idea of ways to debug this problem further ? Regards, -- Vincent Pelletier -- http://mail.python.org/mailman/listinfo/python-list
Re: Flexible string representation, unicode, typography, ...
On 25/08/2012 10:58, Mark Lawrence wrote: On 25/08/2012 08:27, wxjmfa...@gmail.com wrote: Unicode design: a flat table of code points, where all code points are "equals". As soon as one attempts to escape from this rule, one has to "pay" for it. The creator of this machinery (flexible string representation) can not even benefit from it in his native language (I think I'm correctly informed). Hint: Google -> "Das grosse Eszett" jmf It's Saturday morning, I'm stone cold sober, had a good sleep and I'm still baffled as to the point if any. Could someone please enlightem me? Here's what I think he is saying. I am posting this to test the water. I am also confused, and if I have got it wrong hopefully someone will correct me. In python 3.3, unicode strings are now stored as follows - if all characters can be represented by 1 byte, the entire string is composed of 1-byte characters else if all characters can be represented by 1 or 2 bytea, the entire string is composed of 2-byte characters else the entire string is composed of 4-byte characters There is an overhead in making this choice, to detect the lowest number of bytes required. jmfauth believes that this only benefits 'english-speaking' users, as the rest of the world will tend to have strings where at least one character requires 2 or 4 bytes. So they incur the overhead, without getting any benefit. Therefore, I think he is saying that he would have preferred that python standardise on 4-byte characters, on the grounds that the saving in memory does not justify the performance overhead. Frank Millman -- http://mail.python.org/mailman/listinfo/python-list
Re: python 6 compilation failure on RHEL
Cameron Simpson writes: > My personal habit to to build with (adjust to match): > > --prefix=/usr/local/python-2.6.4 > > and put some symlinks in /usr/local/bin afterwards (python2.6, etc). There's actually a program for that, it's called stow. -- http://mail.python.org/mailman/listinfo/python-list
Re: Objects in Python
On Sat, Aug 25, 2012 at 6:55 PM, Mark Lawrence wrote: > I'm just wondering out aloud if the number of times this type of thread has > been debated here will fit into a Python long or float? Well, when I have to store currency information, I like to store it as an integer, using the native currency's "small unit" (eg the cent in dollar+cent currencies). In this instance, instead of trying to count the threads (which would be fractional), just count the number of posts. It then is an integer, and I've yet to find any integer that can't be represented as a Python long (or, in 3.x, int). ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Objects in Python
On 25/08/2012 11:23, Chris Angelico wrote: On Sat, Aug 25, 2012 at 6:55 PM, Mark Lawrence wrote: I'm just wondering out aloud if the number of times this type of thread has been debated here will fit into a Python long or float? Well, when I have to store currency information, I like to store it as an integer, using the native currency's "small unit" (eg the cent in dollar+cent currencies). In this instance, instead of trying to count the threads (which would be fractional), just count the number of posts. It then is an integer, and I've yet to find any integer that can't be represented as a Python long (or, in 3.x, int). ChrisA That could have been fun in the good old days of pounds, shillings and pence. Why they had to complicate things by going decimal I shall never know. Bring back simplistic imperial measures for everything, that's what I say. Using long just shows I've still got a Python 2 hat on. Still when those fine people who develop Matplotlib deliver 1.2 with its Py3k compliance, aided or hindered by me testing on Windows, Python 3.3 here I come. I suppose an alternative to long (or int) or float would have been the Decimal class from the decimal module? Opinions on this anybody? -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Flexible string representation, unicode, typography, ...
On Sat, Aug 25, 2012 at 7:46 PM, Frank Millman wrote: > Therefore, I think he is saying that he would have preferred that python > standardise on 4-byte characters, on the grounds that the saving in memory > does not justify the performance overhead. If that's indeed the argument, then at least it's something to argue. What gets difficult is when people complain about the expansion from a 2-byte narrow build to the current 1/2/4-byte representation, which will indeed use more memory if there are a small number of >0x codepoints. But there's a correctness difference there. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Flexible string representation, unicode, typography, ...
On 25/08/2012 10:46, Frank Millman wrote: On 25/08/2012 10:58, Mark Lawrence wrote: On 25/08/2012 08:27, wxjmfa...@gmail.com wrote: Unicode design: a flat table of code points, where all code points are "equals". As soon as one attempts to escape from this rule, one has to "pay" for it. The creator of this machinery (flexible string representation) can not even benefit from it in his native language (I think I'm correctly informed). Hint: Google -> "Das grosse Eszett" jmf It's Saturday morning, I'm stone cold sober, had a good sleep and I'm still baffled as to the point if any. Could someone please enlightem me? Here's what I think he is saying. I am posting this to test the water. I am also confused, and if I have got it wrong hopefully someone will correct me. In python 3.3, unicode strings are now stored as follows - if all characters can be represented by 1 byte, the entire string is composed of 1-byte characters else if all characters can be represented by 1 or 2 bytea, the entire string is composed of 2-byte characters else the entire string is composed of 4-byte characters There is an overhead in making this choice, to detect the lowest number of bytes required. jmfauth believes that this only benefits 'english-speaking' users, as the rest of the world will tend to have strings where at least one character requires 2 or 4 bytes. So they incur the overhead, without getting any benefit. Therefore, I think he is saying that he would have preferred that python standardise on 4-byte characters, on the grounds that the saving in memory does not justify the performance overhead. Frank Millman I thought Terry Reedy had shot down any claims about performance overhead, and that the memory savings in many cases must be substantial and therefore worthwhile. Or have I misread something? Or what? -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Flexible string representation, unicode, typography, ...
On Sat, Aug 25, 2012 at 9:05 PM, Mark Lawrence wrote: > I thought Terry Reedy had shot down any claims about performance overhead, > and that the memory savings in many cases must be substantial and therefore > worthwhile. Or have I misread something? Or what? My reading of the thread(s) is/are that there are two reasons for the debate to continue to rage: 1) Comparisons with a "narrow build" in which most characters take two bytes but there are one or two characters that get encoded with surrogates. The new system will allocate four bytes per character for the whole string. 2) Arguments on the basis of huge strings that represent _all the data_ that your program's working with, forgetting that there are numerous strings all through everything that are ASCII-only. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Flexible string representation, unicode, typography, ...
On 8/25/2012 7:05 AM, Mark Lawrence wrote: I thought Terry Reedy had shot down any claims about performance overhead, and that the memory savings in many cases must be substantial and therefore worthwhile. Or have I misread something? No, you have correctly read what I and others have said. Jim appears to not be interested in dialog. Lets leave it at that. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to configure IDLE to use spaces instead of tabs for indenting?
On 8/24/2012 6:33 PM, Alex wrote: I'm new to Python and have been using IDLE 3.2.3 to experiment with code as I learn. Despite being configured to use a 4 space indentation That applies to the editor and works in the editor for me and others. A tab becomes 4 space characters, and a backspace in the appropriate place deletes 4 space characters. width, sometimes IDLE's "smart" indentation insists upon using width-8 tabs. Only for the simulated interpreter. There is a tracker issue about changing that but no consensus. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Installation of yappi (timing module)
On 24-Aug-2012 12:28, Virgil Stokes wrote: I have been doing some experiments with different modules for the timing of functions and code segments. One module I would like to test is yappi (thread aware timer) which is listed at PyPI. However, I have been unable to install it on Windows Vista and Windows 7 (Python 2.7 on both). I have tried both easy_install and pip (as suggested at http://code.google.com/p/yappi/). Here is what happens with easy_install C:\Users\Virgil>easy_install yappi Searching for yappi Reading http://pypi.python.org/simple/yappi/ Reading http://yappi.googlecode.com/ Best match: yappi 0.62 Downloading http://yappi.googlecode.com//files/yappi-0.62.tar.gz Processing yappi-0.62.tar.gz Writing c:\users\virgil\appdata\local\temp\easy_install-tzt5gl\yappi-0.62\setup.cfg Running yappi-0.62\setup.py -q bdist_egg --dist-dir c:\users\virgil\appdata\local\temp\easy_install-tzt5gl\yappi-0.62\egg-dist-tmp-t3qodo In file included from D:\python27\include\Python.h:8, from config.h:4, from _yappi.c:10: D:\python27\include\pyconfig.h:68: io.h: No such file or directory D:\python27\include\pyconfig.h:296: stdio.h: No such file or directory In file included from config.h:4, from _yappi.c:10: D:\python27\include\Python.h:19: limits.h: No such file or directory D:\python27\include\Python.h:22: #error "Something's broken. UCHAR_MAX should be defined in limits.h." D:\python27\include\Python.h:26: #error "Python's source code assumes C's unsigned char is an 8-bit type." D:\python27\include\Python.h:33: stdio.h: No such file or directory D:\python27\include\Python.h:35: #error "Python.h requires that stdio.h define NULL." D:\python27\include\Python.h:38: string.h: No such file or directory D:\python27\include\Python.h:40: errno.h: No such file or directory D:\python27\include\Python.h:42: stdlib.h: No such file or directory D:\python27\include\Python.h:49: stddef.h: No such file or directory D:\python27\include\Python.h:56: assert.h: No such file or directory In file included from D:\python27\include\Python.h:58, from config.h:4, from _yappi.c:10: D:\python27\include\pyport.h:306: stdlib.h: No such file or directory D:\python27\include\pyport.h:312: math.h: No such file or directory D:\python27\include\pyport.h:325: time.h: No such file or directory D:\python27\include\pyport.h:377: sys\stat.h: No such file or directory In file included from D:\python27\include\Python.h:85, from config.h:4, from _yappi.c:10: D:\python27\include\unicodeobject.h:4: stdarg.h: No such file or directory D:\python27\include\unicodeobject.h:57: ctype.h: No such file or directory D:\python27\include\unicodeobject.h:120: wchar.h: No such file or directory In file included from D:\python27\include\Python.h:94, from config.h:4, from _yappi.c:10: D:\python27\include\stringobject.h:10: stdarg.h: No such file or directory In file included from D:\python27\include\Python.h:98, from config.h:4, from _yappi.c:10: D:\python27\include\bytearrayobject.h:9: stdarg.h: No such file or directory In file included from D:\python27\include\Python.h:121, from config.h:4, from _yappi.c:10: D:\python27\include\pyerrors.h:319: stdarg.h: No such file or directory In file included from D:\python27\include\Python.h:126, from config.h:4, from _yappi.c:10: D:\python27\include\modsupport.h:10: stdarg.h: No such file or directory In file included from _yappi.c:10: config.h:15: stdint.h: No such file or directory In file included from _yappi.c:23: timing.h:8: windows.h: No such file or directory error: Setup script exited with error: command 'gcc' failed with exit status 1 And pip fails with similar problems (same pyconfig errors where C++ header files are not found). In both cases yappi-0.62.tar.gz was downloaded.Note: 1) I also tried to install from the source which also failed with similar problems, 2) I have both cygwin and MinGW gcc compilers on my systems and they do contain in their include folder these "missing" header files. Any suggestions on how yappi can be installed would be appreciated. --V :-) Problem solved! The ordering of the gcc compilers in my PATH statement caused this failure. I have ordered these compilers such that the first one contains the required header files and the installation of yappi is now successful. --V -- http://mail.python.org/mailman/listinfo/python-list
Re: lpod-python
Le dimanche 12 août 2012 21:45:49 UTC+2, Agon Hajdari a écrit : > On Fri, 10 Aug 2012 19:37:16 +0200, Francesco wrote: > > > > > I'm trying to use the lpod-python module to programmatically read data > > > from Open Document files. My problem is: i can't download the module > > > from its authors' site, > > > http://download.lpod-project.org/lpod-python/lpod-python-0.9.3.tar.gz. > > > It seems the download site is unavailable, while the main site is > > > working. > > > I also tried to install the module with pip (I read on the site that > > > it's now available), but again, no luck. > > > Do somebody know what's happening to download.lpod-project.org ? It > > > doesn't even ping... > > > > > > Please let me know, thank you very much. > > > Francesco > > > > It seems that they are hosting their project at gitorious.org via git. > > Try this: > > git clone git://gitorious.org/lpod-project/lpod-python.git (u need the > > git client of course) > > > > Agon Hi, The lpod-python code is now at https://github.com/lpod/lpod-python git clone git://github.com/lpod/lpod-python.git We are currently improving source code. There are also new development recipes to help (see the readme on git hub). regards, jd -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to configure IDLE to use spaces instead of tabs for indenting?
Terry Reedy wrote: > On 8/24/2012 6:33 PM, Alex wrote: > > Despite being configured to use a 4 space > > indentation ... > > sometimes IDLE's "smart" indentation insists upon using > > width-8 tabs. > > [The 4-space indentation setting] applies to the editor and works in > the editor for me and others. > > [The width-8 tabs are inserted] Only for the simulated interpreter. > There is a tracker issue about changing that but no consensus. Yes, it works in the editor. I was referring to the simulated interpreter. I guess I didn't make that clear. In my search for a solution, I did see some of the traffic regarding the tracker issue, but the posts were all several years old and I was hoping maybe there was a fix by now. I guess not. Maybe in Python 4, eh? Thanks. Alex -- http://mail.python.org/mailman/listinfo/python-list
Re: Filter versus comprehension (was Re: something about split()???)
On Fri, Aug 24, 2012 at 3:03 PM, Walter Hurry wrote: > On Fri, 24 Aug 2012 14:29:00 -0400, Dennis Lee Bieber wrote: > >> It appears to be a change Google made in the last month or two... My >> hypothesis is that they are replacing hard EOL found in inbound NNTP >> with an HTML , and then on outgoing replacing the with a pair of >> NNTP line endings. In contrast, text composed on Google is coming in as >> long single lines (since quoting said text in a response produces on a >> ">" at the start of the paragraph. > > Google Groups sucks. These are computer literate people here. Why don't > they just use a proper newsreader? I haven't used a newsreader in over a decade. I'm quite happy with a mailing list. Am I missing something? -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to configure IDLE to use spaces instead of tabs for indenting?
On 25/08/2012 13:50, Alex wrote: Terry Reedy wrote: On 8/24/2012 6:33 PM, Alex wrote: Despite being configured to use a 4 space indentation ... sometimes IDLE's "smart" indentation insists upon using width-8 tabs. [The 4-space indentation setting] applies to the editor and works in the editor for me and others. [The width-8 tabs are inserted] Only for the simulated interpreter. There is a tracker issue about changing that but no consensus. Yes, it works in the editor. I was referring to the simulated interpreter. I guess I didn't make that clear. In my search for a solution, I did see some of the traffic regarding the tracker issue, but the posts were all several years old and I was hoping maybe there was a fix by now. I guess not. Maybe in Python 4, eh? Thanks. Alex For the record issue 7676, yes? -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to configure IDLE to use spaces instead of tabs for indenting?
Mark Lawrence wrote: > On 25/08/2012 13:50, Alex wrote: > > Terry Reedy wrote: > > > > > On 8/24/2012 6:33 PM, Alex wrote: > > > > Despite being configured to use a 4 space > > > > indentation > > ... > > > > sometimes IDLE's "smart" indentation insists upon using > > > > width-8 tabs. > > > > > > [The 4-space indentation setting] applies to the editor and works > > > in the editor for me and others. > > > > > > [The width-8 tabs are inserted] Only for the simulated > > > interpreter. There is a tracker issue about changing that but no > > > consensus. > > > > Yes, it works in the editor. I was referring to the simulated > > interpreter. I guess I didn't make that clear. > > > > In my search for a solution, I did see some of the traffic regarding > > the tracker issue, but the posts were all several years old and I > > was hoping maybe there was a fix by now. I guess not. Maybe in > > Python 4, eh? > > > > Thanks. > > > > Alex > > > > For the record issue 7676, yes? Yes, that appears to be the issue I was talking about and is, in fact, one of the threads I had looked at before posting here. Of course, I didn't pay enough attention to the dates. I see the most recent posting on the issue appears to have been made in January of this year, so I should have realized it's an ongoing issue. -- http://mail.python.org/mailman/listinfo/python-list
Re: Filter versus comprehension (was Re: something about split()???)
On 25/08/2012 13:57, David Robinow wrote: On Fri, Aug 24, 2012 at 3:03 PM, Walter Hurry wrote: On Fri, 24 Aug 2012 14:29:00 -0400, Dennis Lee Bieber wrote: It appears to be a change Google made in the last month or two... My hypothesis is that they are replacing hard EOL found in inbound NNTP with an HTML , and then on outgoing replacing the with a pair of NNTP line endings. In contrast, text composed on Google is coming in as long single lines (since quoting said text in a response produces on a ">" at the start of the paragraph. Google Groups sucks. These are computer literate people here. Why don't they just use a proper newsreader? I haven't used a newsreader in over a decade. I'm quite happy with a mailing list. Am I missing something? Not really. I'm the same; it just means you can skip over the occasional ggroups-newsreader discussion threads which pop up about 3 times a year on average. :) TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Flexible string representation, unicode, typography, ...
Le samedi 25 août 2012 11:46:34 UTC+2, Frank Millman a écrit : > On 25/08/2012 10:58, Mark Lawrence wrote: > > > On 25/08/2012 08:27, wxjmfa...@gmail.com wrote: > > >> > > >> Unicode design: a flat table of code points, where all code > > >> points are "equals". > > >> As soon as one attempts to escape from this rule, one has to > > >> "pay" for it. > > >> The creator of this machinery (flexible string representation) > > >> can not even benefit from it in his native language (I think > > >> I'm correctly informed). > > >> > > >> Hint: Google -> "Das grosse Eszett" > > >> > > >> jmf > > >> > > > > > > It's Saturday morning, I'm stone cold sober, had a good sleep and I'm > > > still baffled as to the point if any. Could someone please enlightem me? > > > > > > > Here's what I think he is saying. I am posting this to test the water. I > > am also confused, and if I have got it wrong hopefully someone will > > correct me. > > > > In python 3.3, unicode strings are now stored as follows - > >if all characters can be represented by 1 byte, the entire string is > > composed of 1-byte characters > >else if all characters can be represented by 1 or 2 bytea, the entire > > string is composed of 2-byte characters > >else the entire string is composed of 4-byte characters > > > > There is an overhead in making this choice, to detect the lowest number > > of bytes required. > > > > jmfauth believes that this only benefits 'english-speaking' users, as > > the rest of the world will tend to have strings where at least one > > character requires 2 or 4 bytes. So they incur the overhead, without > > getting any benefit. > > > > Therefore, I think he is saying that he would have preferred that python > > standardise on 4-byte characters, on the grounds that the saving in > > memory does not justify the performance overhead. > > > > Frank Millman Very well explained. Thanks. More precisely, affected are not only the 'english-speaking' users, but all the users who are using not latin-1 characters. (See the title of this topic, ... typography). Being at the same time, latin-1 and unicode compliant is a plain absurdity in the mathematical sense. --- For those you do not know, the go language has introduced the rune type. As far as I know, nobody is complaining, I have not even seen a discussion related to this subject. 100% Unicode compliant from the day 0. Congratulations. jmf -- http://mail.python.org/mailman/listinfo/python-list
Re: Publish unittest results from test discovery
Lucretiel wrote: > So I've started using unittest, and I love it. I use testdiscovery (python > -m unittest discover) so that I can distribute my tests and don't have to > manage them all manually. I wanted to start publishing my test results to > xml, though. I found xmlrunner by googling around, but it requires me to > add an if __name__ == '__main__' block to my code, which isn't executed by > unittest discover. Is there a way to get unittest disover to work with > xmlrunner, or to some other way to solve this without restructuring all my > test code? I don't see where you could specify a test runner on the commandline, but you can reuse the discovery code in your own scripts. For the following example I basically copied unittest.__main__.py: $ cat discover.py #!/usr/bin/env python import xmlrunner __unittest = True from unittest.main import main, TestProgram, USAGE_AS_MAIN TestProgram.USAGE = USAGE_AS_MAIN main(module=None, testRunner=xmlrunner.XMLTestRunner(output='test-reports')) $ cat test_alpha.py import unittest class T(unittest.TestCase): def test_alpha(self): pass def test_beta(self): self.assertEquals(["a", "b", "c"], ["a", "B", "c"]) $ ./discover.py discover Running tests... -- .F == FAIL [0.001s]: test_beta (test_alpha.T) -- Traceback (most recent call last): File "/somewhere/over/the/rainbow/discover/test_alpha.py", line 7, in test_beta self.assertEquals(["a", "b", "c"], ["a", "B", "c"]) AssertionError: Lists differ: ['a', 'b', 'c'] != ['a', 'B', 'c'] First differing element 1: b B - ['a', 'b', 'c'] ?^ + ['a', 'B', 'c'] ?^ -- Ran 2 tests in 0.002s FAILED (failures=1) Generating XML reports... $ ls discover.py test_alpha.py test_alpha.pyc test-reports $ ls test-reports/ TEST-test_alpha.T.xml $ -- http://mail.python.org/mailman/listinfo/python-list
Probability Algorithm
Hi, All, I have a problem of probability algorithm The goal is obtain a list which contains three items. as the *FinalList* There has Four source lists. * ALIST, BLIST, CLIST, DLIST There are all Unknown length. They contains unique elements* ( In fact, there are all empty at the program beginning, when running, there growing ) Choose items form this source lists. pick up random items to generate the FinalList Ensure The Following Requirements In the FinalList, probability of ALIST's item appeared is 43% probability of BLIST's item appeared is 37% probability of CLIST's item appeared is 19% probability of DLIST's item appeared is 1% I have written some code, but this just for the four lists are have a lots of elements. from random import choice final_list = [] slot = [] a_picked_times = 0 while a_picked_times < 43: item = choice(ALIST) ALIST.remove(item) if item in already_picked_list: continue slot.append(item) a_picked_times += 1 b_picked_times = 0 while_b_picked_times < 37: ... SOME CODE SIMILAR # now slot is a list which contains 100 elements, # in slot, there are 43 elements of ALIST'items, 37 of B, 19 of C, 1 of D for i in range(3): final_list.append( choice(slot) ) So, this can ensure the probability requirements. *BUT only under the condition: this Four lists have a lots of elements. * list.remove( item ) that will not remove all elements in list, so we will correct pick up items with the needs times. But, when A, B, C, D empty OR not enough elements, How could ensure the probability requirements? A, B, C, D list are all get from redis sorted list. Or some solution with redis ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Probability Algorithm
On 08/25/2012 12:03 PM, 月忧茗 wrote: > Hi, All, > > I have a problem of probability algorithm > > > The goal is obtain a list which contains three items. as the *FinalList* > > There has Four source lists. * > ALIST, BLIST, CLIST, DLIST > > There are all Unknown length. They contains unique elements* > ( In fact, there are all empty at the program beginning, when running, > there growing ) > > Choose items form this source lists. pick up random items to generate the > FinalList > Ensure The Following Requirements > > In the FinalList, > probability of ALIST's item appeared is 43% > probability of BLIST's item appeared is 37% > probability of CLIST's item appeared is 19% > probability of DLIST's item appeared is 1% > > Would you like to tell us the actual assignment? This looks like it's paraphrased. if you have 3 items, each coming from one of four lists, the four probabilities have to add up to much more than 100%. Perhaps what you meant was that each of the three items had those probabilities of coming from the respective lists. Then it'd add up to 100%. Your code is far more complex than needed, and as you observed, doesn't work if each list doesn't have sufficient members. I'd simply pick a random number from 0 to 99, see if it's less than 43 and if so, use ALIST. Else if it's less than 80, use BLIST. else if it's less than 99, use CLIST. Else DLIST. Then do that 2 more times and you're done. Don't forget to factor the problem into functions, so you can easily repeat similar code. If a list is picked, and it's empty, throw an exception. Or wait till the missing item arrives. And you have to decide whether to remove the selected items from the respective lists. That wasn't specified in the problem statement. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to configure IDLE to use spaces instead of tabs for indenting?
On 8/25/2012 10:17 AM, Alex wrote: Yes, that appears to be the issue I was talking about and is, in fact, one of the threads I had looked at before posting here. Of course, I didn't pay enough attention to the dates. I see the most recent posting on the issue appears to have been made in January of this year, so I should have realized it's an ongoing issue. There have also been a few posts this year on the idle-sig mail list. There are only a few people working on IDLE and we have concentrated this calendar year on fixing crashers, not semi-aesthetic issues. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
issue with struct.unpack
I am trying to unpack values from sensor data I am retrieving through a serial cable, but I get errors while using struct.unpack, how can I use struct.unpack to unload the data in a readable format? I checked the python documentation for struct and I can seen to find any argument for this. I have data = struct.unpack('char',data) but I still get errors -- http://mail.python.org/mailman/listinfo/python-list
Re: issue with struct.unpack
On 25/08/2012 19:34, 9bizy wrote: I am trying to unpack values from sensor data I am retrieving through a serial cable, but I get errors while using struct.unpack, how can I use struct.unpack to unload the data in a readable format? I checked the python documentation for struct and I can seen to find any argument for this. I have data = struct.unpack('char',data) but I still get errors We have two options here. Either a) People reading your original request go on a mind reading course or similar in an attempt to find out what the errors are, though I'm confused as to how you get errors from one line of code. Or b) Provide the smallest sample of code that allows the problem to be reproduced together with the complete traceback so we can see exactly what happened. -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: issue with struct.unpack
On 25/08/2012 19:34, 9bizy wrote: I am trying to unpack values from sensor data I am retrieving through a serial cable, but I get errors while using struct.unpack, how can I use struct.unpack to unload the data in a readable format? I checked the python documentation for struct and I can seen to find any argument for this. I have data = struct.unpack('char',data) but I still get errors The format strings are described here for Python 3: http://docs.python.org/3.2/library/struct.html and here for Python 2: http://docs.python.org/2.7/library/struct.html -- http://mail.python.org/mailman/listinfo/python-list
[RELEASED] Python 3.3.0 release candidate 1
On behalf of the Python development team, I'm delighted to announce the first release candidate of Python 3.3.0. This is a preview release, and its use is not recommended in production settings. Python 3.3 includes a range of improvements of the 3.x series, as well as easier porting between 2.x and 3.x. Major new features and changes in the 3.3 release series are: * PEP 380, syntax for delegating to a subgenerator ("yield from") * PEP 393, flexible string representation (doing away with the distinction between "wide" and "narrow" Unicode builds) * A C implementation of the "decimal" module, with up to 80x speedup for decimal-heavy applications * The import system (__import__) now based on importlib by default * The new "lzma" module with LZMA/XZ support * PEP 397, a Python launcher for Windows * PEP 405, virtual environment support in core * PEP 420, namespace package support * PEP 3151, reworking the OS and IO exception hierarchy * PEP 3155, qualified name for classes and functions * PEP 409, suppressing exception context * PEP 414, explicit Unicode literals to help with porting * PEP 418, extended platform-independent clocks in the "time" module * PEP 412, a new key-sharing dictionary implementation that significantly saves memory for object-oriented code * PEP 362, the function-signature object * The new "faulthandler" module that helps diagnosing crashes * The new "unittest.mock" module * The new "ipaddress" module * The "sys.implementation" attribute * A policy framework for the email package, with a provisional (see PEP 411) policy that adds much improved unicode support for email header parsing * A "collections.ChainMap" class for linking mappings to a single unit * Wrappers for many more POSIX functions in the "os" and "signal" modules, as well as other useful functions such as "sendfile()" * Hash randomization, introduced in earlier bugfix releases, is now switched on by default In total, almost 500 API items are new or improved in Python 3.3. For a more extensive list of changes in 3.3.0, see http://docs.python.org/3.3/whatsnew/3.3.html To download Python 3.3.0 visit: http://www.python.org/download/releases/3.3.0/ Please consider trying Python 3.3.0 with your code and reporting any bugs you may notice to: http://bugs.python.org/ Enjoy! -- Georg Brandl, Release Manager georg at python.org (on behalf of the entire python-dev team and 3.3's contributors) -- http://mail.python.org/mailman/listinfo/python-list
modeling complex data with sqlalchemy
Hello all: I had a quick question. In my game, I have an is-a setup, where all objects contain data like an id for sqlalchemy, a name, a description and a list of contents. In order to add functionality to an object, you add components. So for example, a player would have the Player and Living component associated with the basic object. It's sort of a way to give myself a way to add functionality without having a lot of multiple inheritance. This creates a problem for me though, since it looks like each component would have it's own table. How would you go about modeling the 1:n relationship between entity and each component? Also, I'm going to have a location property on the object, which is basically it's location (a reference to another Entity), or None if it has no parent. How would you set that up in SA so that location gets translated to an ID and then translated back to the required object? Might there be another easier way to model all this data? It looks like this database could get rather large, extremely quickly. -- Take care, Ty http://tds-solutions.net The aspen project: a barebones light-weight mud engine: http://code.google.com/p/aspenmud He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave. -- http://mail.python.org/mailman/listinfo/python-list
Re: Flexible string representation, unicode, typography, ...
On Sat, Aug 25, 2012 at 9:47 AM, wrote: > For those you do not know, the go language has introduced > the rune type. As far as I know, nobody is complaining, I > have not even seen a discussion related to this subject. Python has that also. We call it "int". More seriously, strings in Go are not sequences of runes. They're actually arrays of UTF-8 bytes. That means that they're quite efficient for ASCII strings, at the expense of other characters, like Chinese (wait, this sounds familiar for some reason). It also means that you have to bend over backwards if you want to work with actual runes instead of bytes. Want to know how many characters are in your string? Don't call len() on it -- that will only tell you how many bytes are in it. Don't try to index or slice it either -- that will (accidentally) work for ASCII strings, but for other strings your indexes will be wrong. If you're unlucky you might even split up the string in the middle of a character, and now your string has invalid characters in it. The right way to do it looks something like this: len([]rune("白鵬翔")) // get the length of the string in characters string([]rune("白鵬翔")[0:2]) // get the substring containing the first two characters It reminds me of working in Python 2.X, except that instead of an actual unicode type you just have arrays of ints. -- http://mail.python.org/mailman/listinfo/python-list
Re: Objects in Python
On Sun, Aug 26, 2012 at 5:56 AM, Dennis Lee Bieber wrote: > On Sat, 25 Aug 2012 09:55:27 +0100, Mark Lawrence > declaimed the following in > gmane.comp.python.general: > >> >> I'm just wondering out aloud if the number of times this type of thread >> has been debated here will fit into a Python long or float? > > Well, since I don't think one can have a fractional debate (maybe if > someone starts a thread and NOBODY ever follows up on it), then float's > don't gain us anything there. > > Presuming a double-precision float, we would have 14-15 significant > digits for the mantissa -- so anything greater than > (9)99,999,999,999,999 will have lost accuracy. In contrast Python longs > have effectively unlimited significant digits. I wonder if some people are applying an alternative form of duck typing - if it quacks like a "should Python have variables" debate, it gets silenced with that universal grey tape... ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: ONLINE SERVER TO STORE AND RUN PYTHON SCRIPTS
On Friday, 17 August 2012 18:16:08 UTC+5:30, coldfire wrote: > I would like to know that where can a python script be stored on-line from > were it keep running and can be called any time when required using internet. > > I have used mechanize module which creates a webbroswer instance to open a > website and extract data and email me. > > I have tried Python anywhere but they dont support opening of anonymous > websites. > > What s the current what to DO this? > > Can someone point me in the write direction. > > My script have no interaction with User It just Got on-line searches for > something and emails me. > > > > Thanks I got most of it. I will really appreciate is someone out the address of any of the following for use with python 1>Webhost 2>Shell Account 3>VPS I am really new to all this Got web server and shell account but unable to figure out how to use it or deploy the Code, My problem is that I m using lot of third party Library which are mostly not supported or I don't know How to make it RUN over Internet Plz Help -- http://mail.python.org/mailman/listinfo/python-list
Computing win/loss records in Python
Greetings, I have code that I run via Django that grabs the results from various sports from formatted text files. The script iterates over every line in the formatted text files, finds the team in the Postgres database updates their w/l record depending on the outcome on that line, saves the team's row in the db, and then moves on to the next line in the file. I'm trying to get away from Django for this project, I want to run the files, get the W/L results and output a formatted text file with the teams and their W/L records. What's confusing me I guess how to store the data/results as the wins and losses tally up. We're talking hundreds of teams, thousands of games, but a quick example would be: Marshall Ohio State Kentucky Indiana Marshall,24,Ohio State,48, Kentucky,14,Indiana,10, Marshall,10,Indiana,7, Ohio State,28,Kentucky,10 That's just a quick example, I can handle seperating the data in the lines, figuring it all out, I just am unsure of how to keep a running total of a team's record. I would do "for line in file:" then on the first line I see that Marshall lost so they would have 1, Ohio State won so they'd have 1 win. It'd go to the next line Kentucky 1 win, Indiana 1 loss, then on the 3rd line, Marshall got a win so they'd have 1 win, but it would have to remember that loss from line 1... Does this make sense? Thanks, -- http://mail.python.org/mailman/listinfo/python-list
Re: Computing win/loss records in Python
On Aug 25, 2012, at 10:22 PM, Christopher McComas wrote: > Greetings, > > I have code that I run via Django that grabs the results from various sports > from formatted text files. The script iterates over every line in the > formatted text files, finds the team in the Postgres database updates their > w/l record depending on the outcome on that line, saves the team's row in the > db, and then moves on to the next line in the file. > > I'm trying to get away from Django for this project, I want to run the files, > get the W/L results and output a formatted text file with the teams and their > W/L records. What's confusing me I guess how to store the data/results as the > wins and losses tally up. We're talking hundreds of teams, thousands of > games, but a quick example would be: > > Marshall > Ohio State > Kentucky > Indiana > > Marshall,24,Ohio State,48, > Kentucky,14,Indiana,10, > Marshall,10,Indiana,7, > Ohio State,28,Kentucky,10 > > That's just a quick example, I can handle seperating the data in the lines, > figuring it all out, I just am unsure of how to keep a running total of a > team's record. I would do "for line in file:" then on the first line I see > that Marshall lost so they would have 1, Ohio State won so they'd have 1 win. > It'd go to the next line Kentucky 1 win, Indiana 1 loss, then on the 3rd > line, Marshall got a win so they'd have 1 win, but it would have to remember > that loss from line 1... > > Does this make sense? Yes, use a RDBMS, SQLite may be the best fit for your use case its quick and has low overhead. > > Thanks, > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Computing win/loss records in Python
On 8/25/2012 10:20 PM, Christopher McComas wrote: Greetings, I have code that I run via Django that grabs the results from various sports from formatted text files. The script iterates over every line in the formatted text files, finds the team in the Postgres database updates their w/l record depending on the outcome on that line, saves the team's row in the db, and then moves on to the next line in the file. I'm trying to get away from Django for this project, I want to run the files, get the W/L results and output a formatted text file with the teams and their W/L records. What's confusing me I guess how to store the data/results as the wins and losses tally up. We're talking hundreds of teams, thousands of games, but a quick example would be: Marshall Ohio State Kentucky Indiana Marshall,24,Ohio State,48, Kentucky,14,Indiana,10, Marshall,10,Indiana,7, Ohio State,28,Kentucky,10 That's just a quick example, I can handle seperating the data in the lines, figuring it all out, I just am unsure of how to keep a running total of a team's record. I would do "for line in file:" then on the first line I see that Marshall lost so they would have 1, Ohio State won so they'd have 1 win. It'd go to the next line Kentucky 1 win, Indiana 1 loss, then on the 3rd line, Marshall got a win so they'd have 1 win, but it would have to remember that loss from line 1... Does this make sense? Thanks, win_count = defaultdict(int) loss_count = defaultdict(int) items = line.split(',') if items[1] > items[3]: windex = 0 lossdex = 2 else: windex = 2 lossdex = 0 win_count[windex] += 1 loss_count[lossdex] += 1 Zat help? -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Computing win/loss records in Python
On Sat, 25 Aug 2012 22:42:59 -0400, Steven W. Orr wrote: > win_count = defaultdict(int) > loss_count = defaultdict(int) When I try that, I get "NameError: name 'defaultdict' is not defined." I think it is rather unfair on beginners to show them code that almost, but not quite, works, and expect them to somehow work out what this mysterious "defaultdict" is. The answer is probably to do this first: from collections import defaultdict > items = line.split(',') > if items[1] > items[3]: > windex = 0 > lossdex = 2 That's not going to work, because you are doing string comparisons instead of numeric comparisons. Consider: Kentucky,6,Indiana,59 '6' > '59' and you will wrongly count that as a win to Kentucky. > else: > windex = 2 > lossdex = 0 > win_count[windex] += 1 > loss_count[lossdex] += 1 And that certainly won't work, because all you are doing is counting how many times the first team beats the second, instead of counting how many times each team wins. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Computing win/loss records in Python
On Sat, 25 Aug 2012 22:20:05 -0400, Christopher McComas wrote: > Marshall,24,Ohio State,48, > Kentucky,14,Indiana,10, > Marshall,10,Indiana,7, > Ohio State,28,Kentucky,10 > > That's just a quick example, I can handle seperating the data in the > lines, figuring it all out, I just am unsure of how to keep a running > total of a team's record. I would do "for line in file:" then on the > first line I see that Marshall lost so they would have 1, Ohio State won > so they'd have 1 win. It'd go to the next line Kentucky 1 win, Indiana 1 > loss, then on the 3rd line, Marshall got a win so they'd have 1 win, but > it would have to remember that loss from line 1... There are many ways to do this. Here's one: we keep three sets of data, wins, losses and ties. wins = {} losses = {} ties = {} for line in open("datafile.txt"): line = line.strip() # get rid of leading and trailing whitespace line = line.rstrip(',') # and any trailing comma teamA, scoreA, teamB, scoreB = line.split(',') # split on commas teamA = teamA.strip().title() # normalise the case teamB = teamB.strip().title() scoreA = int(scoreA) scoreB = int(scoreB) if scoreA == scoreB: # Handle a draw. ties[teamA] = ties.get(teamA, 0) + 1 ties[teamB] = ties.get(teamB, 0) + 1 else: if scoreA > scoreB: winner = teamA loser = teamB else: winner = teamB loser = teamA wins[winner] = wins.get(winner, 0) + 1 losses[loser] = losses.get(loser, 0) + 1 Once you've done that, you can check the win/loss score of any team: name = 'Marshall' w = wins.get(name, 0) l = losses.get(name, 0) d = ties.get(name, 0) total = w+l+d print( "Team %s played %d games, won %d, lost %d and tied %d." % (name, total, w, l, d) ) If you want to store these results permanently, you need to write them out to file. You can roll your own, but a simpler way might be to use one of the pickle, json, csv or plistlib modules to do it. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Probability Algorithm
Sorry, missing some conditions *already_picked_list* is get from db. > Why keep a counter? Rather than an iterated loop so , if use a iterated loop: for i in range(43): item = choice( ALIST ) ALIST.remove( item ) if item in already_picked_list: continue slot.append( item ) For example, if we picked item from ALIST 43 times, but all picked items are in already_picked_list What's the slot? It's a empty list, not contains item from ALIST. *This is wrong* > Do you really want to remove an item from the source list? *Yes*, i*tems in slot must be unique* ( This is also a condition, which I have missing... ) > and if you don't want duplicates from within a source list, you should remove them when building the source list * As I mentioned , The source list are all unique elements* Your last code. if B ,C ,D are all empty, result's elements are all from A, A's probability is 100% ? I Know , this problem should treated as two situation: If the four list has not enough elements, There are no way to ensure the probability requirements. If they has enough elements, Your solution is a good way. -- http://mail.python.org/mailman/listinfo/python-list
Re: Probability Algorithm
On 08/25/2012 12:03 PM, 月忧茗 wrote: > In the FinalList, > probability of ALIST's item appeared is 43% probability of BLIST's > item appeared is 37% probability of CLIST's item appeared is 19% > probability of DLIST's item appeared is 1% First, select one of the four lists with those appropriate probabilities. Then once you selected a list, select one of its items randomly. import random def select_list(): x = random.randint(1, 100) if x <= 43: return ALIST elif x <= 80: # 43 + 37 return BLIST elif x <= 99: # + 19 return CLIST else: return DLIST the_list = select_list() the_item = random.choice(the_list) -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Computing win/loss records in Python
Christopher McComas writes: > I have code that I run via Django that grabs the results from various > sports from formatted text files. The script iterates over every line > in the formatted text files, finds the team in the Postgres database > updates their w/l record depending on the outcome on that line, saves > the team's row in the db, and then moves on to the next line in the > file. It seems that you already have a PostgreSQL database storing this data. > I'm trying to get away from Django for this project That existing database can be accessed without Django. You could talk directly using the ‘psycopg2’ library, but you don't have to go that far. I would recommend you use SQLAlchemy as a good and flexible way to access existing databases (or make new ones) in a Pythonic manner http://www.sqlalchemy.org/>. If you are using a free-software operating system, you will likely already have packages available to install SQLAlchemy from your operating system's package repositories. -- \ “True greatness is measured by how much freedom you give to | `\ others, not by how much you can coerce others to do what you | _o__) want.” —Larry Wall | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Probability Algorithm
Thanks for helps This code almost meets my needs . But not accurate probability when not enough source elements. So I give up the not enough elements situation. For source list is growing fast, the bast situation just appear in the program starting 2012/8/26 Steven D'Aprano > On 08/25/2012 12:03 PM, 月忧茗 wrote: > > > In the FinalList, > > probability of ALIST's item appeared is 43% probability of BLIST's > > item appeared is 37% probability of CLIST's item appeared is 19% > > probability of DLIST's item appeared is 1% > > First, select one of the four lists with those appropriate probabilities. > Then once you selected a list, select one of its items randomly. > > import random > > def select_list(): > x = random.randint(1, 100) > if x <= 43: > return ALIST > elif x <= 80: # 43 + 37 > return BLIST > elif x <= 99: # + 19 > return CLIST > else: > return DLIST > > the_list = select_list() > the_item = random.choice(the_list) > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Objects in Python
On 08/24/2012 10:04 PM, Steven D'Aprano wrote: The fact that the end result is the same is hardly surprising -- Python's VM is built on top of C pointer indirection, so of course you can start with pointers and end up with Python semantics. But the practice of coding are very different: * in C, I care about identifiers ("names") in order to explicitly manage addresses and pointers as a means to reach the data I actually care about; * in Python, I care about identifiers in order to reach the data I actually care about. So I find this comment very interesting. It makes me wonder if the root cause of our (pretty minor) disagreement is in some sense related to our mental models of *C* variables. I'm actually not much of a C programmer specifically, but I do a lot of C++ stuff. Of those two descriptions, I'd actually say that the Python description sounds more like how I think about variables in C++ most of the time. Obviously there are differences between value and reference semantics between the two languages, but thinking about some variable being located at some address in memory is something that I actually do pretty rarely; I basically think of variables as naming data, and addresses mostly come into play when thinking about points-to and aliasing information at a more abstract level, much the same as I do in Python. Evan -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Objects in Python
On 08/24/2012 05:00 AM, Steven D'Aprano wrote: No. The compiler remembers the address of 'a' by keeping notes about it somewhere in memory during the compilation process. When you run the compiled program, there is no longer any reference to the name 'a'. ... The mapping of name:address is part of the *compilation* process -- the compiler knows that variable 'x' corresponds to location 12345678, but the compiled code has no concept of anything called 'x'. It only knows about locations. The source code 'x = 42' is compiled into something like 'store 42 into location 12345678'. (Locations may be absolute or relative.) In languages with name bindings, the compiler doesn't need to track name:address pairs. The compiled application knows about names, but not about addresses. The source code 'x = 42' is compiled into something like 'store 42 into the namespace using key "x"'. What you describe is sorta correct, but it's also not... you're describing implementations rather than the language. And while the language semantics certainly impose restrictions on the implementation, I think in this case the situation is closer than you acknowledge: From the Python side, I suspect that for most functions, you'd be able to create a Python implementation that behaves more like C, and allocates locals in a more traditional fashion. I don't know much about it, but I'd guess that PyPy already does something along this line; someone also mentioned that Cython (admittedly not a full-blown Python implementation, but close for the purpose of this question) tries to do the same thing. On the C side, imagine a function with locals x, y, and z which never takes the address of any of them. (You said later that "Just because the public interface of the language doesn't give you any way to view the fixed locations of variables, doesn't mean that variables cease to have fixed locations.") First, C variables may not even have a memory address. They can disappear completely during compilation, or live in a register for their entire life. Second, it's possible that those variables *don't* occupy a fixed location. If you never explicitly take an address of a variable (&x), then I can't think of any way that the address can be observed without invoking undefined behavior -- and this means the C compiler is free to transform it to anything that is equivalent under the C semantics. In particular, it can split uses of a variable into multiple ones if there are disjoint live ranges. For instance, in: x = 5 print x x = 10 print x there are two live ranges of x, one consisting of lines 1 and 2, and one consisting of lines 3 and 4. These live ranges could have been different variables; I could just of easily have written x = 5 print x y = 10 print y and these pieces of code are observationally equivalent, so the compiler is allowed to generate the same code for both. In particular, it could either compile the second example to share the same memory address for x and y (meaning that a memory address isn't uniquely named by a single variable) or it could compile the first to put the two live ranges of x into different memory addresses (meaning that a variable doesn't uniquely name a memory address). In fact, I'd *expect* an optimizing compiler to share memory for x and y, and I'd also expect to be able to concoct an example where different live ranges of one variable wind up at different addresses. (The latter I'm less sure of though, and I also expect it'd be a little hard, as you'd have to come up with an example where even at the high optimization levels you'd need to see that, both live ranges would wind up in memory.) Third, and more wackily, you could technically create a C implementation that works like Python, where it stores variables (whose addresses aren't taken) in a dict keyed by name, and generates code that on a variable access looks up the value by accessing that dict using the name of the variable. Evan -- http://mail.python.org/mailman/listinfo/python-list
Re: Objects in Python
Jan Kuiken於 2012年8月24日星期五UTC+8上午2時02分00秒寫道: > On 8/23/12 06:11 , Steven D'Aprano wrote: > > > > >> 2) Related to the above, you can infinitely nest scopes. There's nothing > > >> wrong with having six variables called 'q'; you always use the innermost > > >> one. Yes, this can hurt readability > > > > > > Well, there you go. There *is* something wrong with having six variables > > > called 'q'. > > > > Sometimes you don't want only six variables called 'q' but a hundred > > of them :-) > > > >def fac(q): > >if q < 1 : > >return 1 > >else: > >return q * fac(q-1) > > > >print(fac(100)) > > > > > > Jan Kuiken The long integer arithmetic operations are built in. This makes mathematicians and designers focused on the theory side. -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Objects in Python
On Sun, Aug 26, 2012 at 3:45 PM, Evan Driscoll wrote: > Third, and more wackily, you could technically create a C implementation > that works like Python, where it stores variables (whose addresses aren't > taken) in a dict keyed by name, and generates code that on a variable access > looks up the value by accessing that dict using the name of the variable. That would be a reasonable way to build a C interactive interpreter. ChrisA -- http://mail.python.org/mailman/listinfo/python-list