Re: When do default parameters get their values set?
Dave Angel dave.angel@1:249/999.remove-cdt-this wrote: On 12/08/2014 05:10 PM, bSneddon wrote: I ran into an issue setting variables from a GUI module that imports a back end module. My approach was wrong obviously but what is the best way to set values in a back end module. To answer the subject line, the default parameter(s) are evaluated when the function is compiled, and then stored with the function. The function is compiled when the module is compiled. At latest that is when the module is imported (though in most cases it was probably compiled the first time you ran the code and isn't recompiled unless the source code changes). The default parameters are actually evaluated when the 'def' statement is executed and the function object is created from the default arguments and the previously compiled code block. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Storage Cost Calculation
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: The Model B supported more graphics modes, had a six-pin DIN connector for a monitor (both the A and B had UHF output for connecting to a television, but only the B supported a dedicated monitor), had support for an optional floppy disk controller and even an optional hard drive controller. It also had RS-232 and Centronics parallel interfaces, a 20-pin user port for I/O, and even support for a second CPU! The Model A didn't support any of those. I won't disagree with most of those, but the graphics modes were simply a function of the available memory as RAM was shared between programs and graphics. The model A couldn't do the higher resolution graphics modes as they took too much out of the main memory (up to 20k which would have been tricky with 16k total RAM). At the time, the BBC Micro memory was (I think) expandable: the Model B could be upgraded to 128K of memory, double what Bill Gates allegedly said was the most anyone would ever need. (He probably didn't say that.) So what we need is to find out what an upgrade would have cost. The memory expansion in the original BBC Micro was mostly ROM. The total addressable space was 64k, but 16k of that was the Acorn operating system and another 16k was paged ROM: by default you got BBC Basic but you could install up to 4 16k ROMs for languages such as BCPL or Logo or to drive external processor cards. That isn't to say of course that you couldn't expand the RAM: a company I worked for in the 80s that wrote the BCPL and Logo ROMs also manufactured a 1MB RAM card with battery backup. Later on the B+ had 64k of RAM and the B+128 had 128k of RAM and in each case the additional RAM was paged in as necessary but I don't think the RAM in the B was ever expandable. -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: newbee
Peter Otten __pete...@web.de wrote: Frank Scafidi wrote: I just acquired a Raspberry Pi and want to program in Python. I was a PL/1 programmer back in the 60's 70's and Python is similar. I am struggling with some very fundamental things that I am not finding in the documentation. Can someone help me with the basics like how do I save a program I've written, reload it in Python, list the program once it's loaded? How do I edit a program? Are these command line functions? You can use any text editor to write a python script. A simple editor which might be present ont the Pi is called nano. It shows the hotkeys to store the text and quit the editor, and thus should be self-explanatory: $ nano helloworld.py Once you have written your simple script you can look at it with the cat command: $ cat helloworld.py #!/usr/bin/env python print Hello world Invoke it with: $ python helloworld.py Hello world You can also make your script executable which means that the first line controls which program is used to run it: $ chmod +x helloworld.py $ ./helloworld.py Hello world $ If the script is in a directory listed in the PATH environment variable you can omit the path (the ./ in the above example): $ mv helloworld.py ~/bin $ helloworld.py Hello world PS: I ran the above demo on a Linux system, but not on the Raspberry Pi, so if something doesn't work as shown above it's probably due to the difference between the two systems. All of the above should work just fine on a Pi. The only thing I thing you could have added is that you also have the option of using Idle to edit and run Python programs. If you are running Raspian on your Pi then you will find an icon to run Idle sitting on the initial desktop. There's an introduction to using Idle on the Raspberry Pi at http://www.raspberrypi.org/documentation/usage/python/ -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: eval [was Re: dict to boolean expression, how to?]
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Consider the namedtuple implementation in the standard library. There's a lot of criticism of it, some of it justified. It uses exec extensively, which means the code is dominated by a giant string template. This defeats your editor's syntax colouring, makes refactoring harder, and makes how the namedtuple works rather less understandable. It seems to me that it's only generating the __new__ method which genuinely needs to use exec, the rest of the namedtuple could and should use just an ordinary class object (although I concede that some of this is just a matter of personal taste). Raymond Hettinger's original, using exec for the entire inner class: http://code.activestate.com/recipes/500261-named-tuples/ My refactoring, with the bare minimum use of exec necessary: https://code.activestate.com/recipes/578918-yet-another-namedtuple/ This may be a silly question, but what would stop you moving the exec inside the class? So: ns = {'_new': tuple.__new__} class Inner(tuple): # Work around for annoyance: type __doc__ is read-only :-( __doc__ = (%(typename)s(%(argtxt)s) % {'typename': typename, 'argtxt': argtxt}) __slots__ = () _fields = field_names exec def __new__(_cls, %(argtxt)s): return _new(_cls, (%(argtxt)s)) % { 'argtxt': argtxt } in ns, locals() ... and so on ... and remove lines from 'ns = ...' to 'Inner.__new__ = ...' The tests at the end of the file still pass so I'm not sure whether there is any situation that wouldn't work. For that matter I don't understand why tuple.__new__ needs to be pre-bound. Just referring to tuple.__new__ directly in the exec simplifies things even more as there is no need to specify any namespaces. exec def __new__(_cls, %(argtxt)s): return tuple.__new__(_cls, (%(argtxt)s)) % { 'argtxt': argtxt } also passes the tests. -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Python and IDEs [was Re: Python 3 is killing Python]
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Unfortunately, software development on Windows is something of a ghetto, compared to the wide range of free tools available for Linux. Outside of a few oases like Microsoft's own commercial development tools, it's hard to do development on Windows. Hard, but not impossible, of course, and there are quite a few resources available for the Windows user willing to download installers from the Internet. For Python users, the IDEs from Wingware and Activestate are notable: https://wingware.com/ http://komodoide.com/ I missed this thread when it started, so please forgive me if this has been covered, but by dismissing Microsoft you look to have skipped over a very powerful Python IDE for Windows, namely PTVS. Microsoft's PTVS is Windows only :-( and completely free (gratuit), partly free (libre): PTVS itself is Apache licensed and the required Visual Studio is of course closed source but PTVS now runs on the latest free versions of Visual Studio Express 2013 for Web or Visual Studio Express 2013 for Desktop (which includes C++). Some of the features: works with CPython (2.x or 3.x) or IronPython. Full support for virtualenv, packages can be installed directly from PTVS individually or from requirements.txt. Intellisense uses a completion database generated in the background from the standard library and all installed libraries. It offers context sensitive completion which does a pretty good job of inferring the type of local variables based on the types of the values used to call the function. Refactoring (Rename, Extract Method, Add Import, Remove unused imports) Interactive windows for all installed Python versions (can use standard shell or IPython) Debugging locally or remotely including Linux and OS X targets (in fact they claim that anything capable of running Python can be debugged). Mixed mode Python and C++ debugging. Profiling (CPython only). Automatic test discovery for tests using unittest. Support for PyLint. Automatic deployment to Windows Azure. Extensive support for Django (including Intellisense and debugging for templates and various Django specific commands such as sync db and admin shell). -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Python and IDEs [was Re: Python 3 is killing Python]
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Duncan Booth wrote: Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Unfortunately, software development on Windows is something of a ghetto, compared to the wide range of free tools available for Linux. I remember writing this. But I don't remember when it was. Presumably some time in the last six months :-) Outside of a few oases like Microsoft's own commercial development tools, it's hard to do development on Windows. Hard, but not impossible, of course, and there are quite a few resources available for the Windows user willing to download installers from the Internet. For Python users, the IDEs from Wingware and Activestate are notable: https://wingware.com/ http://komodoide.com/ I missed this thread when it started, so please forgive me if this has been covered, but by dismissing Microsoft you look to have skipped over a very powerful Python IDE for Windows, namely PTVS. Never heard of it :-) Which is not surprising, since I'm not a Windows developer. [snip feature list] Nice. How does one get it? 1) Get a Windows 8.1 VM, or a real PC if that's more convenient. 2) Download and install either Microsoft Visual Studio Express 2013 with Update 3 for Web or Microsoft Visual Studio Express 2013 with Update 3 for Windows Desktop from http://www.visualstudio.com/downloads/download-visual-studio-vs N.B. If you just download the original versions without update 3 you'll have to apply all updates before proceeding so easier to use the latest versions from the get go. 3) Download and install PTVS 2.1 Beta 2 from https://pytools.codeplex.com/releases Note that you need at least PTVS 2.1 Beta and VS Express 2013 with at least Update 2 to be able to install with just free tools. Earlier versions will refuse to install. There may be more intermediate steps of applying updates, but that's par for the Microsoft course. If you try this out in conjunction with a Microsoft Azure account then be sure to also install the Azure SDK. Documentation is at https://pytools.codeplex.com/documentation There's a Django tutorial at http://pytools.codeplex.com/wikipage? title=Django%20Web%20Site/Cloud%20Service%20Tutorial which gives quite a good walkthrough. If I gave the impression that one cannot do development on Windows, that was not my intent. I tried to indicate that the difference was a matter of degree, not impossibility. One of the reasons why so many of the core developers for Python use Linux is that they got frustrated with the speed humps on Windows, the poor out of the box experience for developers (compare what dev tools you get with Windows by default versus what you get on Linux by default), but that might also be somewhat self-selecting: people who are happy with Windows development tend to stick to VB, Java, C, .Net etc. while those who prefer lighter weight more agile environments migrate to Linux. I don't know. But I do know that the existence of good quality Windows development tools for Python is good news for the community, so thank you for mentioning this. So far they seem to have kept a pretty low profile; I suspect largely because until recently PTVS only worked with the pay versions of Visual Studio. -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Multi-line commands with 'python -c'
Peter Otten __pete...@web.de wrote: Duncan Booth wrote: Chris Angelico ros...@gmail.com wrote: On Sat, May 31, 2014 at 7:42 AM, Devin Jeanpierre jeanpierr...@gmail.com wrote: In unix shells you can literally use a new line. Or is that only bash? You can in bash, I know, but it's fiddly to type it; and more importantly, it's not a good point in the this is cleaner than a series of pipes argument. My primary recommendation, of course, was a three-line script saved as an actual file, but for a more direct parallel to the pipe-it-three-ways model, I wanted to use -c. and you also wrote originally that it's fiddly to edit. I think that Windows Powershell has (at least in the current ISE command line) got the editing a bit better. It's a minor difference though and it has taken Microsoft about 30 years to get to that point. What may be a larger difference, or may just be my lack of Linux-foo, is this: PS C:\python33 $script = @ import os for root, dirs, files in os.walk(.): if len(dirs + files) == 1: print(root) @ PS C:\python33 python -c $script .\Doc .\Lib\concurrent\__pycache__ .\Lib\curses\__pycache__ ... which is a style I've found useful for example when running a group of related timeit.py commands as I can put things like multi-line setup statements in a variable and then have a simpler command to repeat. But bash as far as I can won't let me do that: $ script='import os for root, dirs, files in os.walk(.): if len(dirs + files) == 1: print(root) ' $ python -c $script File string, line 1 import ^ SyntaxError: invalid syntax $ script='import os for root, dirs, files in os.walk(.): if len(dirs + files) == 1: print(root) ' $ python3 -c $script . ./heureka $ python3 -c 'import sys; print(sys.argv)' $script ['-c', 'import', 'os', 'for', 'root,', 'dirs,', 'files', 'in', 'os.walk(.):', 'if', 'len(dirs', '+', 'files)', '==', '1:', 'print(root)'] $ python3 -c 'import sys; print(sys.argv)' $script ['-c', 'import os\nfor root, dirs, files in os.walk(.):\nif len(dirs + files) == 1:\nprint(root)\n'] Thanks, I thought there must be a way to do that (and I should have remembered it). It nicely shows up the difference between the *nix shells that are all about processing the command line as a string and the Powershell way where it is all about objects (so a single value stays as a single argument). -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Multi-line commands with 'python -c'
Chris Angelico ros...@gmail.com wrote: On Sat, May 31, 2014 at 7:42 AM, Devin Jeanpierre jeanpierr...@gmail.com wrote: In unix shells you can literally use a new line. Or is that only bash? You can in bash, I know, but it's fiddly to type it; and more importantly, it's not a good point in the this is cleaner than a series of pipes argument. My primary recommendation, of course, was a three-line script saved as an actual file, but for a more direct parallel to the pipe-it-three-ways model, I wanted to use -c. and you also wrote originally that it's fiddly to edit. I think that Windows Powershell has (at least in the current ISE command line) got the editing a bit better. It's a minor difference though and it has taken Microsoft about 30 years to get to that point. What may be a larger difference, or may just be my lack of Linux-foo, is this: PS C:\python33 $script = @ import os for root, dirs, files in os.walk(.): if len(dirs + files) == 1: print(root) @ PS C:\python33 python -c $script .\Doc .\Lib\concurrent\__pycache__ .\Lib\curses\__pycache__ ... which is a style I've found useful for example when running a group of related timeit.py commands as I can put things like multi-line setup statements in a variable and then have a simpler command to repeat. But bash as far as I can won't let me do that: $ script='import os for root, dirs, files in os.walk(.): if len(dirs + files) == 1: print(root) ' $ python -c $script File string, line 1 import ^ SyntaxError: invalid syntax -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Multi-line commands with 'python -c'
Chris Angelico ros...@gmail.com wrote: Problem: Translate this into a shell one-liner: import os for root, dirs, files in os.walk(.): if len(dirs + files) == 1: print(root) This is one area where Windows seems to do better than Linux shells: PS C:\python33 python -c import os`nfor root, dirs, files in os.walk('.'):`n if len(dirs + files) == 1: print(root)`n .\Doc .\Lib\concurrent\__pycache__ .\Lib\curses\__pycache__ ... The `n shell escaped newline is interpreted well before Python runs. Also the multiline version works and in Powershell ISE up-arrow pulls it back as a single unit for easy editing: PS C:\python33 python -c @ import os for root, dirs, files in os.walk('.'): if len(dirs + files) == 1: print(root) @ .\Doc .\Lib\concurrent\__pycache__ .\Lib\curses\__pycache__ ... and so on ... -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Forking PyPI package
Chris Angelico ros...@gmail.com wrote: If you absolutely can't get in touch with him, the only option is to go back to the original protocol and manually reimplement it, completely ignoring this code. It's sad but true; some code dies because of a trivial thing like Oops, I forgot to actually say that this is MIT-licensed. (At least, I can't see a license anywhere on github there. If you can find another copy of the same code somewhere else, posted by its author, and including a license, then your job is easier.) Deep in the code (pwdhash / pwdhash.py.egg-info / PKG-INFO): Author: Lev Shamardin Author-email: shamar...@gmail.com License: BSD Still better to get in touch with the author, but he has actually stated the license albeit in the most minimal way possible. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: IDE for python
Sameer Rathoud sameer.rath...@gmail.com wrote: On Wednesday, May 28, 2014 5:16:41 PM UTC+5:30, Greg Schroeder wrote: Please suggest, if we have any free ide for python development. Anything that writes text is fine. I recommend the standard text editor for your OS (Notepad if you use Windows, Textedit on Mac, whatever is on your GNU/Linux distro by default) unless you know exactly what you don't like about it. Greg Right now I am looking for ide on windows 7 platform. Actually, I shouldn't say this, But I am bit use to intellisense and on go warnings and error and my text editor (Notepad) doesn't provide me that feature . If you are used to Visual Studio then you could try PTVS. I have no experience of it, but http://pytools.codeplex.com/ PTVS is a free, open source plugin that turns Visual Studio into a Python IDE. PTVS supports CPython, IronPython, editing, browsing, Intellisense, mixed Python/C++ debugging, remote linux/MacOS debugging, profiling, IPython, Django, and cloud computing with client libraries for Windows, Linux and MacOS. Designed, developed, and supported by Microsoft and the community. -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: IDE for python
Duncan Booth duncan.booth@invalid.invalid wrote: Sameer Rathoud sameer.rath...@gmail.com wrote: On Wednesday, May 28, 2014 5:16:41 PM UTC+5:30, Greg Schroeder wrote: Please suggest, if we have any free ide for python development. Anything that writes text is fine. I recommend the standard text editor for your OS (Notepad if you use Windows, Textedit on Mac, whatever is on your GNU/Linux distro by default) unless you know exactly what you don't like about it. Greg Right now I am looking for ide on windows 7 platform. Actually, I shouldn't say this, But I am bit use to intellisense and on go warnings and error and my text editor (Notepad) doesn't provide me that feature . If you are used to Visual Studio then you could try PTVS. I have no experience of it, but http://pytools.codeplex.com/ PTVS is a free, open source plugin that turns Visual Studio into a Python IDE. PTVS supports CPython, IronPython, editing, browsing, Intellisense, mixed Python/C++ debugging, remote linux/MacOS debugging, profiling, IPython, Django, and cloud computing with client libraries for Windows, Linux and MacOS. Designed, developed, and supported by Microsoft and the community. I'm just watching the video from that page. It is impressive just how much of intellisense they have working with PTVS: for example inside a function doing 'find all references' on a local variable that happens to be a method passed in to the function shows you all def lines for methods that are passed as parameters. There's a heck of a lot of type inferencing going on. -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Proper deletion of selected items during map iteration in for loop: Thanks to all
Chris Angelico ros...@gmail.com wrote: # Snapshot of keys: for k in list(d): if f(k): del d[k] No extra loop at the end, no switching out and in of contents, just one little change in the loop header. Obviously you don't want to do this when you're deleting two out of three billion, but for smallish dicts, that won't make a visible change. Even if you have three billion keys, the extra memory needed to create a list that references those keys is going to be a lot less than the memory used by the keys themselves. For example if the keys are 6 character strings then each string needs I think at least 45 bytes (64 bit Python 2.x, up to double that in Python 3.x) but the list only needs one 8 byte pointer per key. I would always choose this simple solution until such time as it is proved to be a problem. -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: object().__dict_
Pavel Volkov sai...@lists.xtsubasa.org wrote: There are some basics about Python objects I don't understand. Consider this snippet: class X: pass ... x = X() dir(x) ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] x.foo = 11 And now I want to make a simple object in a shorter way, without declaring X class: y = object() dir(y) ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] y.foo = 12 Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'object' object has no attribute 'foo' The attribute list is different now and there's no __dict__ and the object does not accept new attributes. Please explain what's going on. Not all classes have a __dict__ attribute. Mostly builtin classes (e.g. tuple, list, int, ...), but also if you have an class using __slots__ which subclasses a class with no __dict__ it won't have a __dict__. Subclasses don't remove attributes, they only add them (although in Python you can bend that rule it still applies here). Therefore for any classes to not have a __dict__ attribute the ultimate base class 'object' has to not have a __dict__. The consequence, as you found out, is that you cannot add attributes to an instance of 'object()', you have to create at least an empty subclass which doesn't include a `__slots__` attribute to get a class that can accept arbitrary attributes. -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Tuples and immutability
Chris Angelico ros...@gmail.com wrote: On Sat, Mar 1, 2014 at 1:41 AM, Joshua Landau jos...@landau.ws wrote: Would it be better to add a check here, such that if this gets raised to the top-level it includes a warning (Addition was inplace; variable probably mutated despite assignment failure)? That'd require figuring out whether or not the variable was actually mutated, and that's pretty hard to work out. So there's a FAQ entry, which Zachary already posted: http://docs.python.org/3/faq/programming.html#why-does-a-tuple-i-item-r aise-an-exception-when-the-addition-works Also, we just answer this question every now and then :) Presumably more often on -tutor than here. ChrisA Another take on this that I haven't seen discussed in this thread: Is there any reason why tuples need to throw an exception on assigning to the element if the old value and new value are the same object? If I say: a = (spam, [10, 30], eggs) then a[0] = a[0] won't actually mutate the object. So tuples could let that silently pass. Then you would be able to safely do: a[1] += [50] but this would still throw an exception: a[0] += x -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: intersection, union, difference, symmetric difference for dictionaries
Tim Chase python.l...@tim.thechases.com wrote: a_dict = dict(...) b_dict = dict(...) a_set = set(a_dict) b_set = set(b_dict) added_keys = b_set - a_set removed_keys = a_set - b_set same_keys = a_set b_set diff_keys = a_set ^ b_set all_keys = a_set | b_set It would save some space if I didn't have to duplicate all the keys into sets (on the order of 10-100k small strings), instead being able to directly perform the set-ops on the dicts. But otherwise, it was pretty readable straight-forward. It doesn't matter whether they were small strings or full-length novels, creating a set from a dict doesn't duplicate any strings. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: [RELEASED] Python 3.4.0 release candidate 1
Terry Reedy tjre...@udel.edu wrote: On 2/11/2014 2:43 AM, Larry Hastings wrote: On behalf of the Python development team, I'm delighted to announce the first release candidate of Python 3.4. To download Python 3.4.0rc1 visit: http://www.python.org/download/releases/3.4.0/ I installed 64 bit 3.3.4 yesterday with no problem. I reran it today in repair mode and again, no problem. With 64 bit 3.4.0, I get There is a problem with this Windows Installer package. A program required for the install to complete could not be run. No, the generic message does not bother to say *which* program :-(. 34 bit 3.4.0 installed fine. I redownloaded 64bit 3.4.0 and install gives the same message. Can someone verify that this is not some bizarre glitch on my machine? I downloaded and installed it without problems on a 64bit Windows 7 system. Maybe it is a bizarre glitch on your system, or perhaps it assumes something is present which is there on my system and missing on yours. I see that part way through the install it downloads setuptools/pip from pypi. Did your system have network access? What happens if you enable the installer log by running: msiexec /i python-3.4.0rc1.amd64.msi /L*v logfile.txt Does it put any useful messages in logfile.txt? -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Google Cloud Platform and GlassSolver Project
eliasbylar...@gmail.com wrote: I am fully ready to invest in the Google Cloud Platform, and bring with me my very own idea: Glass Solver (Sometimes called GlaSolver). One thing you will have to do is find another name for your project. https://developers.google.com/glass/design/branding-guidelines says: Glass is never part of the name of your business, Glassware, other products. Instead, use for Glass. If you use for Glass in conjunction with a logo, for Glass must be a smaller size than the rest of the logo. Correct: Cat Facts for Glass Incorrect: Glass Cat Facts, Glassy Cat Photos -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Vedr: What does means in python?
Gisle Vanem gva...@yahoo.no wrote: Regrading handy uses of ''', you learned me one trick when using Pythonÿ code in a Windows .bat file: rem = ''' @echo off echo This is batch \python32\python %0 echo All done exit /b rem ''' import sys print(This is Python) for i,p in enumerate(sys.path): print('sys.path[%2d]: %s' % (i, p)) print(Python done) You'll have a variable in Python called 'rem' which contains all your batch code :) It exploits the fact that 'rem' makes a one-line comment, but the triple quotes go across multiple lines. A better trick would be to use a Powershell script instead of a batch file: --- filter python() { $_ | c:\Python33\python.exe ($args -replace'(\\*)','$1$1\') } Write-Host This is the powershell script dir cert: | convertto-json | python -c @ import json, sys stores = json.loads(sys.stdin.read()) print(This is Python) for store in stores: print({}: {}.format(store['PSChildName'], ', '.join(store['StoreNames']))) print(Python done) @ Write-Host All done --- C:\scripts . .\Pythoncerts.ps1 This is the powershell script This is Python CurrentUser: Root, UserDS, Disallowed, Trust, My, TrustedPublisher, SmartCardRoot, TrustedPeople, ADDRESSBOOK, AuthRoot, McAfee Trust, CA, REQUEST, ACRS LocalMachine: Disallowed, Trust, CA, TrustedPublisher, SmartCardRoot, My, TrustedPeople, AuthRoot, TrustedDevices, Root Python done All done C:\scripts Notes on the above: Powershell messes up arguments when running legacy programs. The filter ensures that all arguments pass through Windows command line processing unscathed (except they can't contain null characters). You don't actually have to use the filter if you are careful about how you write quotes in the code, but it makes life simpler. Python scripts up to just over 32,000 characters can be written on the command line this way. You can also assign the script to a variable and keep the Python command a bit cleaner: $script = @ print(Python here!) @ python -c $script Or without the filter it is best to avoid the double quotes: $script = @ print('Python here!') @ c:\python33\python.exe -c $script To run from a traditional cmd.exe prompt you have to explicitly use Powershell. The default file associations for .ps1 files will run notepad instead. If your system execution policy is Restricted (the default) use: powershell -executionpolicy RemoteSigned .\Pythoncerts.ps1 Otherwise set the execution policy to something more lenient (at a Powershell prompt running as administrator enter Set-ExecutionPolicy RemoteSigned) and you can just do: powershell .\Pythoncerts.ps1 I also use Powershell interactively so I have the filters defined in my startup ($Home\Documents\WindowsPowerShell\profile.ps1): filter py() { $_ | py.exe ($args -replace'(\\*)','$1$1\') } filter python() { $_ | c:\Python33\python.exe ($args -replace'(\\*)','$1$1\') } -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Ifs and assignments
Chris Angelico ros...@gmail.com wrote: Maybe a for loop isn't the best other example, but I frequently work with places where I want to call some function and keep iterating with the result of that until it returns false: while (var = func()) { } In Python, that gets a lot clunkier. The most popular way is to turn it into an infinite loop: while True: var = func() if not var: break My preferred way would be to write it as a `for` loop: for var in iter(func, False): ... Though you do have to be sure to get the sentinel value correct as it will only break for the expected terminal False, not for 0, , or None. -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie question. Are those different objects ?
Gregory Ewing greg.ew...@canterbury.ac.nz wrote: rusi wrote: Good idea. Only you were beaten to it by about 2 decades. More than 2, I think. Algol: x := y Wher := is pronounced 'becomes'. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: extracting a heapq in a for loop - there must be more elegant solution
Helmut Jarausch jarau...@igpm.rwth-aachen.de wrote: Hi, I'd like to extracted elements from a heapq in a for loop. I feel my solution below is much too complicated. How to do it more elegantly? I know I could use a while loop but I don't like it. Many thanks for some lessons in Python. Here is my clumsy solution from heapq import heappush, heappop # heappop raises IndexError if heap is empty H=[] for N in 'H','C','W','I' : heappush(H,N) # how to avoid / simplify the following function def in_sequence(H) : try : while True : N= heappop(H) yield N except IndexError : raise StopIteration # and here the application: for N in in_sequence(H) : print(N) If all you want to do is pull all of the elements out of the heap in order, you would probably be better off just doing: for N in sorted(H): print(N) Heaps are mostly useful if you want only some of the elements, or if you are continually producing more elements while also processing the smallest ones. However, if you really wnt to do this: for N in iter(lambda: heappop(H) if H else None, None): print(N) will work so long as H cannot contain None. If it can just replace both occurences of None with some other sentinel: sentinel = object() for N in iter(lambda: heappop(H) if H else sentinel, sentinel): print(N) Alternatively your 'in_sequence' function would look better without the exception handling: def in_sequence(H) : while H: yield heappop(H) -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie - Trying to Help a Friend
Denis McMahon denismfmcma...@gmail.com wrote: 1) Find all the numbers less than n that are not divisible by a, b, or c. ask the user for x; assign the value 0 to some other variable i; while i is not greater than than x do the following [ if i is not divisible by a and i is not divisible by b and i is not divisible by c then display i to the user; add 1 to i; ] The question didn't ask to find all the numbers, it asked to count how many there are. Also even if you change this to count instead of print, it could be very inefficient for large values of x. If x is greater than a*b*c, find how many numbers up to a*b*c are not divisible by a, b, or c. (Depending on your interpretation of the English language for 2, 3, 5 this is either 8 or 1, you could check whether the system is set to Australian English to determine the correct action here.) You may then store these numbers in a list for easy reference. Now the answer you want is the length of that list times the integer part of x divided by a*b*c plus the number of values in the list that are less than the remainder you get when dividing x by a*b*c. If x is less than a*b*c then just find how many numbers up to x are not divisible by a, b, or c, which would be a case of re-using some of the above code. For extra credit, calculate and use the least common multiple of a,b and c instead of just using their product. -- Duncan Booth -- https://mail.python.org/mailman/listinfo/python-list
Re: 'isimmutable' and 'ImmutableNester'
=?UTF-8?Q?Frank=2DRene_Sch=C3=A4fer?= fsch...@gmail.com wrote: The ImmutableNester special class type would be a feature to help checks to avoid recursion. Objects of classes derived from ImmutableNester have no mutable access functions and allow insertion of members only at construction time. At construction time it checks whether all entered elements are immutable in the above sense. How does this help anything? If the objects are all immutable the object cannot contain any recursive references. If you cannot see this think about tuples: a tuple containing immutable objects including other tuples can never contain a reference to itself because by definition the tuple did not exist at the point where the elements it contains were constructed. Python already relies on the non-recursive nature of nested tuples when handling exceptions: The expression in the 'except' clause is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception. If you try using something like a list in the exception specification you get a TypeError; only tuples and exception classes (subclasses of BaseException) are permitted. This means the structure can be as deeply nested as you wish, but can never be recursive and no checks against recursion need to be implemented. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Slicing with negative strides
Steven D'Aprano st...@pearwood.info wrote: Does anyone here use slices (or range/xrange) with negative strides other than -1? E.g. sequence[2:15:-3] With any negative stride your example is just the empty sequence. If so, there is a discussion (long, long, looong discussion) on the python-ideas mailing list, debating whether or not to deprecate or change the behaviour of slicing with negative strides. So if you care about the current behaviour, now is the time to stand up and be counted. (Standing up *here* is fine, don't feel that you have to join yet another list.) For those of us that don't really want to join another mailing list, could you summarise what change is being proposed? -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Mysql's mysql module
Skip Montanaro s...@pobox.com wrote: On Mon, Oct 7, 2013 at 1:08 PM, Tobiah t...@tobiah.org wrote: I just noticed this: http://dev.mysql.com/doc/connector-python/en/index.html * Does it adhere to the Python database API? http://www.python.org/dev/peps/pep-0249/ * Is source available? * Does it have a reasonable open source license? These questions come immediately to mind because at work we briefly considered, then rejected, a similar offering from the Sybase folks for connecting to (big surprise) Sybase. We never needed to ask the first and third questions, because the answer to the second was, no, and they only offered a version built against Python 2.6. Since we use Python 2.4 and 2.7, that was an immediate nonstarter. Skip Based on a quick look at the link given, I think the answers to questions 1 and 3 are yes and no respectively. No idea about #2. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Tail recursion to while iteration in 2 easy steps
Neil Cerutti ne...@norwich.edu wrote: On 2013-10-03, Duncan Booth duncan.booth@invalid.invalid wrote: It isn't hard to imagine adding a TAIL_CALL opcode to the interpreter that checks whether the function to be called is the same as the current function and if it is just updates the arguments and jumps to the start of the code block. Tail call optimization doesn't involve verification that the function is calling itself; you just have to verfify that the call is in tail position. You misunderstood me. As usually implemented tail call recursion doesn't require verifying that the function is calling itself, but in Python the function could be rebound so a check would be a necessary protection against this unlikely situation. Consider this Python: @some_decorator def fact(n, acc=1): if n = 1: return acc return fact(n-1, n*acc) Is that tail recursion or not? You cannot tell whether or not that is tail recursion unless you know the definition of 'some_decorator' and even then the answer may vary from run to run. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Tail recursion to while iteration in 2 easy steps
Ian Kelly ian.g.ke...@gmail.com wrote: On Fri, Oct 4, 2013 at 4:41 AM, Ian Kelly ian.g.ke...@gmail.com wrote: There is no doubt that it's a tail call. Whether it is recursion is irrelevant to optimizing it. The reason we talk about tail call recursion specifically is because the recursive case is the one that makes the optimization worthwhile, not because the recursion is necessary to perform the optimization. It is possible that fact is recursive but that some_decorator adds additional stack frames that are not tail calls and not optimizable. If this were the case, then the recursion would still eventually hit the stack limit, but there would still be benefit in optimizing the fact frames, as it would increase the allowable depth before the stack limit is reached. So I see no reason to check the identity of the called function before optimizing the tail call. On the other hand, if you start optimizing every tail call and not just the recursive functions, then I can see where that could start to get problematic for debugging -- as arbitrary functions get removed from the stack traces just because they happened to end in tail calls. Quite so. Losing some stack frames in the traceback because tail recursion was optimised is probably no big deal. Losing arbitrary stack frames because of a more widespread tail call optimisation would not IMHO fit with the spirit of Python except possibly as an optional optimisation that was off by default. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Tail recursion to while iteration in 2 easy steps
Alain Ketterlin al...@dpt-info.u-strasbg.fr wrote: Terry Reedy tjre...@udel.edu writes: Part of the reason that Python does not do tail call optimization is that turning tail recursion into while iteration is almost trivial, once you know the secret of the two easy steps. Here it is. Assume that you have already done the work of turning a body recursive ('not tail recursive') form like def fact(n): return 1 if n = 1 else n * fact(n-1) into a tail recursion like [...] How do know that either = or * didn't rebind the name fact to something else? I think that's the main reason why python cannot apply any procedural optimization (even things like inlining are impossible, or possible only under very conservative assumption, that make it worthless). That isn't actually sufficient reason. It isn't hard to imagine adding a TAIL_CALL opcode to the interpreter that checks whether the function to be called is the same as the current function and if it is just updates the arguments and jumps to the start of the code block. If the function doesn't match it would simply fall through to doing the same as the current CALL_FUNCTION opcode. There is an issue that you would lose stack frames in any traceback. Also it means code for this modified Python wouldn't run on other non-modified interpreters, but it is at least theoretically possible without breaking Python's assumptions. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Why does it have red squiggly lines under it if it works perfectly fine and no errors happen when I run it?
William Bryant gogobe...@gmail.com wrote: Thanks a lot! I have one more question, is there any way I can make my program work on android tablets and ipads? Because I'd like to use it in school because we are learning statistics and we are allowed our devices in school. You can install SL4A on Android and that should let you run your script on an Android tablet. You may want to modify the script to work with the android UI, or it might be enough just to run it in a terminal window. I don't believe there is an ipad equivalent, but so long as you have a network connection, another option to consider (that would work on both android and ipad) would be to run the script on a server somewhere and connect to it through a web browser. Have a look at https://c9.io as that gives you a full development environment that can be accessed through a web browser: I don't know how well it works on a tablet but it would be worth trying. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Python in XKCD today
Roy Smith r...@panix.com wrote: http://xkcd.com/1263/ So now I guess someone has to actually implement the script. At least, that's (sort of) what happened for xkcd 353 so there's a precedent. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: semicolon at end of python's statements
Chris Angelico ros...@gmail.com wrote: On Thu, Aug 29, 2013 at 10:18 AM, Mohsen Pahlevanzadeh moh...@pahlevanzadeh.org wrote: Dear all, I'm C++ programmer and unfortunately put semicolon at end of my statements in python. Quesion: What's really defferences between putting semicolon and don't put? Very little. Putting the semicolon makes you look like a C programmer who's new to Python; omitting it makes you look like you actually understand Python :) As a C and C++ programmer myself, I know where you're coming from, but putting semicolons at the ends of Python statements is as useless as putting lots of (((irritating (((superfluous (((parentheses) in your C++ code. The parser won't mind, but subsequent programmers will wonder what these unnecessary syntactic elements are for. ChrisA Someone I knew actually used these definitions when writing C in a Pascalish, Algol68ish style (if I remembered them correctly): #define IF if((( #define AND ))(( #define OR )||( #define THEN ))){ #define ELSE }else{ #define FI } -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: semicolon at end of python's statements
Chris Angelico ros...@gmail.com wrote: On Fri, Sep 6, 2013 at 12:05 AM, Jeremy Sanders jer...@jeremysanders.net wrote: Chris Angelico wrote: Because s/he thought it made for better code, or as a joke? Usually I see this sort of thing as the latter... It was intended for clearer code, which is true if you don't like curly braces, and who does round here? http://oldhome.schmorp.de/marc/bournegol.html http://utcc.utoronto.ca/~cks/space/blog/programming/BourneGol Yep, that's some impressive code right there! ChrisA That brings back memories all right, but its not as good as the version I remember as it doesn't 'fix' the logical operator priorities. -- Duncan Booth http://kupuguy.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
Joshua Landau jos...@landau.ws wrote: That's true with this example, but is: lines = [ Developments in high-speed rail, and high-speed, transport more generally, have historically been, impeded by the difficulties in managing friction, and air resistance, both of which become, substantial when vehicles approach high speeds., The vactrain concept eliminates these obstacles, by employing magnetically levitating trains in, tubes kept at a complete vacuum, allowing for, heoretical speeds of thousands of miles per, hour. The high cost of constructing such a system,, however, and the difficulty of maintaining a, vacuum over large distances, has prevented this, type of system from ever being built. The, Hyperloop can be viewed as a modified vactrain,, employing more cost-effective solutions to the, same problems the latter was designed to solve. ] really more readable than: lines = \ Developments in high-speed rail, and high-speed transport more generally, have historically been impeded by the difficulties in managing friction and air resistance, both of which become substantial when vehicles approach high speeds. The vactrain concept eliminates these obstacles by employing magnetically levitating trains in tubes kept at a complete vacuum, allowing for heoretical speeds of thousands of miles per hour. The high cost of constructing such a system, however, and the difficulty of maintaining a vacuum over large distances, has prevented this type of system from ever being built. The Hyperloop can be viewed as a modified vactrain, employing more cost-effective solutions to the same problems the latter was designed to solve. [1:-1].split(\n) ? I suppose the question really is whether the author of the second example really meant to start with the word 'evelopments'? If that was a mistake, then the first one is demonstrably less error prone. If it was intentional then the second one is definitely less readable. Either way I think you've proved that the first way of writing it is more readable. -- Duncan Booth -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple Python script as SMTP server for outgoing e-mails?
Chris Angelico ros...@gmail.com wrote: On Tue, Jul 23, 2013 at 12:08 AM, Michael Torrie torr...@gmail.com wrote: On 07/22/2013 06:51 AM, Chris Angelico wrote: Thanks for the tip. I didn't know about SPF http://en.wikipedia.org/wiki/Sender_Policy_Framework It's a great way of detecting legit vs forged mail. If anyone tries to send mail purporting to be from anyth...@kepl.com.au and the receiving mail server is checking SPF records, it'll be rejected after one cheap DNS lookup. It's a simple and cacheable way to ask the owning server, Is this guy allowed to send mail for you?. (The 192.168 block in my SPF record above is permitted to allow some intranet conveniences; omit it unless you need it.) Yes setting SPF records will help your mail be accepted by other servers, but I disagree with your appeal to make mail server SPF handling as strict as your server does. SPF has problems in a number of situations which could cause legitimate mail to be rejected. In my last job I could only use SPF as one spam factor, not as a basis for rejection. If legit mail is rejected for failing an SPF check, it's the sending admin's problem, not yours. You should never have problems with it if it's set up correctly. And since rejected mail gets reported to the transmitting MTA, you don't need to drop it in a spambox or anything. It's not spam, it's simply invalid mail (equivalent to something sent to a dud address). If you want your emails to have the best chance of arriving your SPF should list servers you use but not deny that there might be others. I have a very common situation where an overly strict SPF may cause problems: Like many people I have multiple email addresses which all end up in the same inbox. The one I most commonly give out to businesses bounces the email unchanged to the gmail inbox that I use. That means all emails I receive through that email address appear to Google to have originated from the forwarding servers. An SPF record from the original sender that claims to have a complete list of originating servers will therefore fail validation. It isn't Google's fault: they can't ignore the forwarding step otherwise spammers could bypass SPF simply by claiming to be forwarding the emails. It is simply a limitation of the SPF protocol. Fortunately they only use SPF as one indicator so real messages still get through. -- Duncan Booth -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple Python script as SMTP server for outgoing e-mails?
Chris Angelico ros...@gmail.com wrote: On Tue, Jul 23, 2013 at 6:06 PM, Duncan Booth duncan.booth@invalid.invalid wrote: I have a very common situation where an overly strict SPF may cause problems: Like many people I have multiple email addresses which all end up in the same inbox. The one I most commonly give out to businesses bounces the email unchanged to the gmail inbox that I use. That means all emails I receive through that email address appear to Google to have originated from the forwarding servers. An SPF record from the original sender that claims to have a complete list of originating servers will therefore fail validation. Ah, there's a solution to this one. You simply use your own envelope-from address; SPF shouldn't be being checked for the From: header. Forwarding and using the original sender's address in the SMTP 'MAIL FROM' command is forging mail from them, so it is correct for that to be thrown out. The mail is coming from your own account, so you put your address in it, and you might even be able to put an uber-strict SPF record like v=spf1 ip4:1.2.3.4 -all which is quick to process and guarantees that nobody can pretend to forward mail on your behalf. The checks are for the *current connection*, not anything earlier. sarcasm Excellent idea, I'll tell the email forwarding service to rewrite their system immediately. Or I could just tell Google to rewrite their email system to know about and strip off the forwarding service's headers: that's probably about as easy. Or maybe I could just ask you to add the forwarder's SPF record into your own? /sarcasm I know that I could arrange things so that my emails don't trigger this situation, but that isn't the point. The point is that this situation happens quite commonly, therefore you as the sender of an email with a strict SPF are going to find systems rejecting emails you send that would get through if you have a less strict one. That is of course your choice, but many users of email would prefer to maximise the chance of the email they send arriving rather than reducing slightly the chance of people they may not even know receiving spam. You could also try combining SPF with DKIM although that has its own, different failure scenarios. -- Duncan Booth -- http://mail.python.org/mailman/listinfo/python-list
Re: Callable or not callable, that is the question!
Ulrich Eckhardt ulrich.eckha...@dominolaser.com wrote: Am 11.07.2013 16:11, schrieb Peter Otten: Ulrich Eckhardt wrote: Bug or feature? No bug. Missing feature if you come up with a convincing use-case. class Parser: def _handle_bool(input): # ... pass types = {'bool': _handle_bool, 'boolean': _handle_bool,} def parse(self, line): t,s,v = line.partition(':') handler = types[t] return handler(v) I want a utility function that really behaves just like a function. I'd prefer to nest it inside the class that uses it, to make the code easier to understand. Since I don't want the implicit self-binding either, I would use staticmethod to make this clear, too. But the example you gave works just fine as written! You are only using your utility function as a function so there's no need for it to be a staticmethod or indeed any other sort of method. To be a convincing use-case you would have to show a situation where something had to be both a static method and a utility method rather than just one or the other and also where you couldn't just have both. If you can persuade me that you need _handle_bool as both a static method and a utility function, you probably also need to explain why you can't just use both: class Parser: def _handle_bool(input): ... handle_bool = staticmethod(_handle_bool) -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Why is regex so slow?
Roy Smith r...@panix.com wrote: Except that the complexity in regexes is compiling the pattern down to a FSM. Once you've got the FSM built, the inner loop should be pretty quick. In C, the inner loop for executing a FSM should be something like: for(char* p = input; p; ++p) { next_state = current_state[*p]; if (next_state == MATCH) { break; } } which should compile down to a couple of machine instructions which run entirely in the instruction pipeline cache. But I'm probably simplifying it more than I should :-) I'd just like to point out that your simple loop is looking at every character of the input string. The simple 'ENQ' not in line test can look at the third character of the string and if it's none of 'E', 'N' or 'Q' skip to checking the 6th and then the 9th. It doesn't have to touch the intervening characters at all. Or as the source puts it: it's a mix between Boyer-Moore and Horspool, with a few more bells and whistles on the top. Also the regex library has to do a whole lot more than just figuring out if it got a match, so you have massively over-simplified it. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: I'm looking for some help in finding a term, it's not Python-specific but does apply to some Python code. This is an anti-pattern to avoid. The idea is that creating a resource ought to be the same as turning it on, or enabling it, or similar I've come across this under the name 'two-phase construction', but as a desirable(!?) pattern rathern than as an anti-pattern. In particular Symbian used it throughout as originally their C++ implementation didn't support exceptions. Instead they had a separate cleanup stack and objects that require cleanup were pushed onto that stack after being fully constructed but before calling the initialisation that required cleanup. See http://www.developer.nokia.com/Community/Wiki/Two-phase_construction -- Duncan Booth -- http://mail.python.org/mailman/listinfo/python-list
Re: Running simultaneuos FOR loops
inshu chauhan insidesh...@gmail.com wrote: This statement is giving me the following error Statement: for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(), pixel_count.iterkeys())): Error: Traceback (most recent call last): File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 170, in module access_segments(segimage, data) File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 147, in access_segments for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(), pixel_count.iterkeys())): TypeError: 'dictionary-keyiterator' object is not callable The second argument to `sorted()` is a comparison or key function, if you want to sort all three key lists you need to sort them separately. Try: for p, k, j in zip(sorted(segments), sorted(class_count), sorted(pixel_count)): also you don't need to call the `iterkeys()` method as you need them all to sort and just treating the dict as a sequence will do the right thing. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: howto remove the thousand separator
pyth0n3r pyth0...@gmail.com wrote: I came across a problem that when i deal with int data with ',' as thousand separator, such as 12,916, i can not change it into int() or float(). How can i remove the comma in int data? Any reply will be appreciated!! Parse it using the locale module, just be sure to set the correct locale first: import locale locale.setlocale(locale.LC_ALL, '') 'English_United Kingdom.1252' locale.atoi('1,000') 1000 locale.atof('1,000') 1000.0 locale.setlocale(locale.LC_ALL, 'French_France') 'French_France.1252' locale.atof('1,000') 1.0 -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: What are some other way to rewrite this if block?
Jussi Piitulainen jpiit...@ling.helsinki.fi wrote: Any tips are welcome. A double tip: if (not (0.0 = should_be_on = 24.0) or not (0.0 = came_on = 24.0)): ... Or even: if not (0.0 = should_be_on = 24.0 and 0.0 = came_on = 24.0): ... You might want to raise an exception from the range-check branch instead of returning a value. And raise an exception from the else-branch, because that branch should never be reached. Or even lose the else branch entirely. If the code can never be reached then don't write it. Also you don't need 'elif' when the individual branches all return. Putting that together and allowing some flexibility in the definition of 'on time': EARLY_DELTA = 1.0/60 LATE_DELTA = 5.0/60 def report_status(should_be_on, came_on): if not (0.0 = should_be_on = 24.0 and 0.0 = came_on = 24.0): raise ValueError('time not in range') if should_be_on - EARLY_DELTA = came_on = should_be_on + LATE_DELTA: return 'on time' if came_on should_be_on: return 'delayed' return 'early' Note that none of this will hande situations around midnight such as: should_be_on==23.5, came_on=0.5 -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Unicode
Thomas Heller thel...@ctypes.org wrote: output æm Traceback (most recent call last): File x.py, line 7, in module print(b) File C:\Python33-64\lib\encodings\cp850.py, line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u03bc' in position 0: character maps to undefined /output Using (german) windows, command prompt, codepage 850. The same happens with Python 2.7. What am I doing wrong? They are different characters: repr(a) u'\\xb5m' repr(b) u'\\u03bcm' a contains unicode MICRO SIGN, b contains GREEK SMALL LETTER MU -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: FYI: AI-programmer
Gisle Vanem gva...@broadpark.no wrote: Here is something interesting that you pythonistas might be interested in: http://www.primaryobjects.com/CMS/Article149.aspx This article describes an experiment to produce an AI program, capable of developing its own programs, using a genetic algorithm implementation with self-modifying and self-improving code. The above experimental BrainF** language was written using C#. So who will be the first to make an AI-language in Python that generates it's own program? We already have a Python interpreter written in Python that (in most cases) runs faster than the original, which is pretty much the same thing. All it needs is a bit of AI stuck on to critique the code and complain about the weather and you're done. -- Duncan Booth -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
Rui Maciel rui.mac...@gmail.com wrote: Chris Angelico wrote: On Fri, Feb 22, 2013 at 10:58 PM, Rui Maciel rui.mac...@gmail.com wrote: Mitya Sirenef wrote: Looks very unclear and confusing to me. Whether it's C# or ruby or anything else, most devs don't indent like that; The Go programming language makes that style mandatory. [citation needed] What do you mean by that style? The OP's style with the 'if' indented? I just checked out golang.org for examples, and they're written in OTBS. Read Mitya Sirenef's post, specifically the bit I've replied to. Do you believe that post was talking about the indentation of the keyword 'if' relative to the statements in the same block, or did you think it was talking about the placement of the opening brace on the same line as the `if` statement? I believe that Mitya was talking about the former but that you assumed the latter. -- Duncan Booth -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Newbie
Steve Simmons square.st...@gmail.com wrote: On 22/02/2013 15:26, Duncan Booth wrote: Rui Maciel rui.mac...@gmail.com wrote: Chris Angelico wrote: On Fri, Feb 22, 2013 at 10:58 PM, Rui Maciel rui.mac...@gmail.com wrote: Mitya Sirenef wrote: Looks very unclear and confusing to me. Whether it's C# or ruby or anything else, most devs don't indent like that; The Go programming language makes that style mandatory. [citation needed] What do you mean by that style? The OP's style with the 'if' indented? I just checked out golang.org for examples, and they're written in OTBS. Read Mitya Sirenef's post, specifically the bit I've replied to. Do you believe that post was talking about the indentation of the keyword 'if' relative to the statements in the same block, or did you think it was talking about the placement of the opening brace on the same line as the `if` statement? I believe that Mitya was talking about the former but that you assumed the latter. Oooh, this is making my head spin. Are you saying that the OP's question about proper indentation has resulted in an incorrectly answered post due to poor indentation of a reference to the indentation of another reference? Steve Not at all, I'm saying that the OP's question about proper indentation has resulted in an incorrectly answered post due to poor substitution of ellipses in place of the word `code` compounded by replying to the original post rather than the post containing the OP's example of indentation (containing the aforementiond word `code`) combined with a failure to read and comprehend the entire thread leading to the incorrect answer. I hope that makes it all clear. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Suggested feature: slice syntax within tuples (or even more generally)?
stephenw...@gmail.com wrote: Would it be feasible to modify the Python grammar to allow ':' to generate slice objects everywhere rather than just indexers and top-level tuples of indexers? Would this be a dict with a slice as key or value, or a set with a slice with a step?: {1:2:3} You can't just allow ':' to generate slice objects everwhere without introducing ambiguity, so your proposal would have to be to allow slice objects in wider but still restricted contexts. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: real-time monitoring of propriety system: embedding python in C or embedding C in python?
Bas wegw...@gmail.com wrote: A) Implement the main program in C. In a loop, get a chunk of data using direct call of C functions, convert data to python variables and call an embedded python interpreter that runs one iteration of the user's algorithm. When the script finishes, you read some variables from the interpreter and then call some other C-function to write the results. B) Implement the main loop in python. At the beginning of the loop, you call an embedded C function to get new data (using ctypes?), make the result readable from python (memoryview?), do the user's calculation and finally call another C function to write the result. Are there any advantages for using one method over the other? Note that I have more experience with python than with C. Option B sounds like it makes your life simpler. Just turn the external code into a library, use ctypes to call the library and you're done. That also means reading command line arguments and/or config files can be done in Python and keep the C code simpler. Embedding Python isn't hard but it sounds more complex than needed here. You can of course mix the two. If it's more convenient put the main loop in Python but use callbacks from the library to handle the values as they appear, but again that probably just complicates things. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: confusion with decorators
Dave Angel da...@davea.name wrote: The decorator function will execute while *compiling* the class A, and the one in class B is unreferenced. No, the decorator function is called when *executing* the class body of A. Compilation could have happened weeks earlier. It really does make it a lot easier to understand this sort of issue if you remember that 'class' and 'def' are simply executable statements: the body of 'class' executes as part of the class definition whenever normal execution reaches the 'class' statement, the body of 'def' doesn't execute until the function is called (but default arguments are evaluated when the 'def' is executed). In both cases however the code is fully compiled whenever the module is compiled: that could be when the module is imported or if it is __main__ when the script executes, but after the first run of the program all of the modules (nto the script) will have been compiled once and don't compile again until the source changes. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: mysql solution
Ferrous Cranus nikos.gr...@gmail.com wrote: I can do that but then i have to use that pin column's value in my next statement. cursor.execute( '''UPDATE visitors SET hits = hits + 1, useros = %s, browser = %s, date = %s WHERE pin = %s AND host = %s''', (useros, browser, date, pin, host)) I'm not MySQL expert, but something like this might work: cursor.execute('''UPDATE visitors,counter SET visitors.hits=visitors.hits+1, visitors.useros=%s, visitors.browser =%s, visitors.date=%s WHERE visitors.pin=counter.pin AND counter.page = %s AND visitors.host=%s''', (useros, browser, date, page, host)) -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Else statement executing when it shouldnt
Thomas Boell tboell@domain.invalid wrote: Huh?! I would have expected all your examples to raise a SyntaxError or IndentationError. Why don't they? Is 'else' not required to have a matching 'if'? Matching 'if' or 'for' or 'while'. See http://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Else statement executing when it shouldnt
Duncan Booth duncan.booth@invalid.invalid wrote: Matching 'if' or 'for' or 'while'. or of course 'try'. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: handling return codes from CTYPES
Steve Simmons square.st...@gmail.com wrote: from ctypes import * sLib = cdll.slib lic_key = c_char_p(asdfghjkl.encode(encoding='utf_8', errors='strict')) initResult = sLib.InitScanLib(lic_key.value) print(InitScanLib Result: , initResult) InitScanLib Result: 65535 I've tried declaring initResult as c_short by: inserting... initResult = c_short(0) ... before the call to sLib.InitScanLib but I still get the same response (65535). Tell the function what type to return before you call it: InitScanLib = sLib.InitScanLib InitScanLib.restype = c_short See http://docs.python.org/2/library/ctypes.html#return-types You can also tell it what parameter types to expect which will make calling it simpler. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes
Dave Cinege d...@cinege.com wrote: You will notice that the code is disgusting simple. However I have found that this has completely changed the way I program in python. I've re-written some exiting programs using Thesaurus, and often relized 15-30% code reduction. Additionally I find the new code much easier to read. And here's the same code written without your class but maintaining as far as possible the same structure. I find my version far easier to read then your's with all your spurious 'g.' 'L.' prefixes. - #!python2.7 from textwrap import dedent class Blob(object): pass prog = Blob() prog.VERSION = '1.0'# But isn't this so much cleaner? prog.NAME = 'Thesaurus' class TestClass: no = 'Class' way = 'this' def main (): tc = TestClass() l = ['Some', 'objects'] # Here's how you should create output without a fight. print dedent('''\ When programing python without {prog.NAME}, it is very easy to access your {l[1]}. {l[0]} people might say {prog.NAME} has no {tc.no}.''').format(prog=prog, l=l, tc=tc) if hasattr(prog, 'VERSION'): print 'But I challenge them to write code {tc.way} clean without it!'.format(**locals()) if __name__ == '__main__': main() - -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Over 30 types of variables available in python ?
chaouche yacine yacinechaou...@yahoo.com wrote: booleans ints, floats, longs, complexes strings, unicode strings lists, tuples, dictionaries, dictionary views, sets, frozensets, buffers, bytearrays, slices functions, methods, code objects,modules,classes, instances, types, nulls (there is exactly one object of type Null which is None), tracebacks, frames generators, iterators, xranges, files, memoryviews, context managers, These are all listed in this page http://docs.python.org/2/library/stdtypes.html as built-in types. Am I getting anything wrong here ? I'm a bit confused about it. I have never seen so many types in the few programming languages I saw. Instances aren't types (though types themselves are instances): every object in Python is an instance. If you want a list of types that exist in your particular copy of Python then you can print it out easily enough: def allsubclasses(base): mod = base.__module__ if mod in ('builtins', '__builtin__', 'exceptions'): yield getattr(base, '__qualname__', base.__name__) else: yield {}.{}.format(base.__module__, getattr(base, '__qualname__', base.__name__)) for typ in type.__subclasses__(base): for t in allsubclasses(typ): yield t all_types = sorted(set(allsubclasses(object)), key=str.lower) print(len(all_types)) print(all_types) That won't show any types that haven't been imported, but it gives me 293 types that are all loaded on startup in Python 3.3 and 150 in Python 2.7. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python USB control on Windows 7?
Chris Angelico ros...@gmail.com wrote: On Sun, Dec 23, 2012 at 6:28 PM, Tim Roberts t...@probo.com wrote: Duncan Booth duncan.booth@invalid.invalid wrote: In this year's Christmas Raffle at work I won a 'party-in-a-box' including USB fairy lights. They sit boringly on all the time, so does anyone know if I can toggle the power easily from a script? My work PC is running Win7. Not easily, no. It's not really a USB device -- I'm betting it doesn't even enumerate. It's just sucking power from the USB wires. There's nothing to control. Yes, I understand that, I was wondering whether the power could be toggled. Hmm. Can you control whether a particular port is on or off? (I have no idea what's possible with the underlying API, much less whether it's exposed.) It should in theory be possible - disable the appropriate USB port and the device loses power. So far as I can tell Windows doesn't let you turn the ports on and off. I found some suggestion that by connecting it to a powered hub it may be possible to toggle the hub power on and off but that many hubs don't bother implementing the functionality. Thanks anyway. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Python USB control on Windows 7?
In this year's Christmas Raffle at work I won a 'party-in-a-box' including USB fairy lights. They sit boringly on all the time, so does anyone know if I can toggle the power easily from a script? My work PC is running Win7. -- http://mail.python.org/mailman/listinfo/python-list
Re: Pass and return
Mitya Sirenef msire...@lightbird.net wrote: On 12/21/2012 12:23 AM, iMath wrote: Pass and return Are these two functions the same ? def test(): return def test(): pass From the point of style, of course, the latter is much better because that's the idiomatic way to define a no-op function. With a return, it looks like you might have forgotten to add the value to return or deleted it by mistake. I would say it is *an* idiomatic way to define a no-op function. Another idiomatic way is to use a doc-string as the only body, that way you can also explain why you feel the need for an empty function. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Brython - Python in the browser
Pierre Quentel pierre.quen...@gmail.com wrote: If that's your intention, then instead of coming up with something totally new, unpythonic and ugly, why not take the normal Python route and implement a subset of the ElementTree API? Stefan Because the tree implementation in ElementTree or other tree modules in Python require a lot of typing and parenthesis To produce the HTML code DIVhello Bworld/B/DIV these modules require writing something like div = Tag('DIV') div.appendChild(TextNode('hello ')) b = Tag('B') b.appendChild(TextNode('world')) div.appendChild(b) doc.appendChild(div) Or you can do something like this: from lxml.html.builder import * snippet = DIV(Hello , B(world)) etree.tostring(snippet) 'divHello bworld/b/div' With the tree syntax proposed in Brython it would just be doc = DIV('hello '+B('world')) If pythonic means concise and readable, which one is more pythonic ? The one that doesn't do unexpected things with operators. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with calling function from dll
deep...@poczta.fm wrote: I have problem with using function from dll. import ctypes b = ctypes.windll.LoadLibrary(kernel32) a = b.GetComputerNameA(a,20) But I got exception: Traceback (most recent call last): File pyshell#323, line 1, in module b.GetComputerNameA(a,20) WindowsError: exception: access violation reading 0x0014 Even when I write: a = ctypes.c_char_p('.' * 20) I got the same result. Here I found this function description: http://sd2cx1.webring.org/l/rd?ring=pbring;id=15;url=http%3A%2F% 2Fwww.a stercity.net%2F~azakrze3%2Fhtml%2Fwin32_api_functios.html Please help. You have to allocate a buffer for the result, and the second parameter is a pointer to the length of the buffer on input and contains the length of the result on output. #!python2.7 from ctypes import windll, c_wchar_p, POINTER, c_int, create_unicode_buffer kernel32 = windll.LoadLibrary(kernel32) _GetComputerNameW = kernel32.GetComputerNameW _GetComputerNameW.argtypes = [c_wchar_p, POINTER(c_int)] _GetComputerNameW.restype = c_int def GetComputerName(): buf = create_unicode_buffer(255) len = c_int(255) if not _GetComputerNameW(buf, len): raise RuntimeError(Failed to get computer name) return buf.value[:len.value] print GetComputerName() -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: re.search when used within an if/else fails
Dennis Lee Bieber wlfr...@ix.netcom.com wrote: Unless there has been a major change in the parser... (I still don't have Python 3.x installed) I believe tab is expanded to 8-spaces -- NOT TO NEXT MULTIPLE OF 8... Certainly in Python 2.7 that's not the case: the tab expands to the next multiple of 8 spaces. if 1: ... print yes # space + tab ... print no # eight spaces ... yes no If tab expanded to exactly 8 spaces the leading space would have forced an indentation error, but it didn't. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: how to pass echo t | input to subprocess.check_output() method
dacha...@gmail.com wrote: Hi all, I want to list the repositories in svn using python. For this i have used below command, res = subprocess.check_output([svn.exe, list, Https://127.0.0.1:443/svn/Repos], stderr=subprocess.STDOUT) but it throws an exception, since it requires an user input to validate certificate, (R)eject, accept (t)emporarily or accept (p)ermanently? from Command prompt im able to pass the input while calling the process, and im able to get the output echo t | svn list Https://127.0.0.1:443/svn/Repos But i dont know how to pass the echo t | in subprocess.check_output while calling a process. Is there a way to do this? Please help. Run svn once manually as the same user that is running your script then when you get the prompt verify that it is indeed the certificate and accept it permanently. That will allow your script to work proviuded the certificate doesn't change. Also, change the command you run to include the --non-interactive command line option so that if the certificate ever does change in the future the command will fail rather than prompting. Alternatively use --non-interactive --trust-server-cert to just accept any old server regardless what certificate it uses, but be aware that this impacts security. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: how to pass echo t | input to subprocess.check_output() method
dacha...@gmail.com wrote: Hi Duncan, I tried using --non-interactive --trust-server-cert, but the call fails with error message, svn: E175002: OPTIONS of 'https://127.0.0.1/svn/Repos': Server certificate verification failed: certificate issued for a different hostname, issuer is not trusted (https://127.0.0.1) that's why I want to pass an input to accept the certificate (t)emporarily or (p)ermanently. I think you probably need to configure your web server so the certificate is valid for whichever hostname you use (if the svn server is also used by other machines then connect to it using the external hostname rather than localhost). Or just use http:// configured to allow access through localhost only. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple Question regarding running .py program
Grant Edwards invalid@invalid.invalid wrote: [1] OK, so I'm am annoyed with them after my Google phone updated to Android 4.2 this afternoon and the lock-screen clock is now _physically_painful_ to look at. However, I'm convinced that's not evil -- just a complete and utter lack of visual design ability. You can select any other lock screen widget as the default, so why not download some more widgets from Play and choose something different. e.g. Beautiful Clock Widgets or HD Widgets but there are probably others. To change the default lockscreen widget swipe left from the lockscreen until you get to a page with only a '+', add the new widget there, then long press that widget and drag it to be the rightmost page. Then you should be sorted just so long as you don't have any friends with December birthdays. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: isinstance(.., file) for Python 3
Ulrich Eckhardt ulrich.eckha...@dominolaser.com wrote: If possible, I'm looking for a solution that works for Pythons 2 and 3, since I'm not fully through the conversion yet and have clients that might use the older snake for some time before shedding their skin. Suggestions? Why bother checking types at all? def foo(file_or_string): try: data = file_or_string.read() except AttributeError: data = file_or_string ... use data ... -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Haskell - Python
Ian Kelly ian.g.ke...@gmail.com wrote: On Fri, Nov 2, 2012 at 1:19 PM, foste...@gmail.com wrote: Is there anything anyone could recommend to make it more Pythonic or more functional. It looks clumsy next to the Haskell. def options(heaps): for i, heap in enumerate(heaps): head = heaps[:i] tail = heaps[i+1:] yield from (head + [x] + tail for x in range(heap)) yield from is Python 3.3 syntax. If you're not using Python 3.3, then that line could be replaced by: for x in range(heap): yield head + [x] + tail Cheers, Ian An alternative that is closer to foster63's original but still more Pythonic for some definitions of those words. def options(heaps): if not heaps: return [] head, *tail = heaps for h in range(head): yield [h]+tail for t in options(tail): yield [head]+t For a more 'functional' version there is also the Python 3.3 variant: def options(heaps): if not heaps: return [] head, *tail = heaps yield from ([h]+tail for h in range(head)) yield from ([head]+t for t in options(tail)) -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Float to String %.7e - diff between Python-2.6 and Python-2.7
andrew.macke...@3ds.com wrote: When formatting a float using the exponential format, the rounding is different in Python-2.6 and Python-2.7. See example below. Is this intentional? Is there any way of forcing the Python-2.6 behavior (for compatibility reasons when testing)? It isn't Python 2.6 behaviour, it looks more like a bug in your particular version of 2.6. This one matches what you are seeing on 2.7: [dbooth@localhost ~]$ /opt/local/bin/python2.6 Python 2.6.7 (r267:88850, Jan 5 2012, 16:18:48) [GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2 Type help, copyright, credits or license for more information. x = [2.096732130e+02,2.096732140e+02,2.096732150e+02,2.096732151e+ 02,2.096732160+02] for a in x: ... print ' %.9e%.7e'%(a,a) ... 2.096732130e+022.0967321e+02 2.096732140e+022.0967321e+02 2.096732150e+022.0967321e+02 2.096732151e+022.0967322e+02 4.096732160e+004.0967322e+00 Note that the rounding shown here is correct; the actual value is slightly less than 5 in the last place: [dbooth@localhost ~]$ /opt/local/bin/python2.6 -c print('%.20e'% 2.096732150e+02,'%.7e'%2.096732150e+02) ('2.096732149009e+02', '2.0967321e+02') What do you get printing the value on 2.6 with a '%.20e' format? I seem to remember that 2.7 rewrote float parsing because previously it was buggy. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python interactive help()
Mark Lawrence breamore...@yahoo.co.uk wrote: Good morning/afternoon/evening all, Where is this specific usage documented as my search engine skills have let me down? By this I mean entering help() without parameters to get the following output and then the help prompt. It is documented under 'built-in functions'. http://docs.python.org/library/functions.html#help -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Testing against multiple versions of Python
Michele Simionato michele.simion...@gmail.com wrote: Yesterday I released a new version of the decorator module. It should run under Python 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3. I did not have the will to install on my machine 8 different versions of Python, so I just tested it with Python 2.7 and 3.3. But I do not feel happy with that. Is there any kind of service where a package author can send a pre-release version of her package and have its tests run againsts a set of different Python versions? I seem to remember somebody talking about a service like that years ago but I don't remembers. I do not see anything on PyPI. Any advice is welcome! Not exactly what you asked for, but if you clone https://github.com/collective/buildout.python then a single command will build Python 2.4, 2.5, 2.6, 2.7, 3.2, and 3.3 on your system. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Add if...else... switch to doctest?
David zhushe...@gmail.com wrote: Hello, how to add if...else... switch to doctest? E.g. function outputs different value when global_var change. if (global_var == True): function() [1,2] else: function() [1,2,3] Thank you very much. One other case the other replies don't seem to have covered: If the global variable is determined by the environment, outside your control, and by implication doesn't change while your program is running, then you should use two separate functions: if sys.platform=='win32': def function(): function() [1, 2] return [1, 2] else: def function(): function() [1, 2, 3] return [1, 2, 3] and if it's more than one such function use separate modules. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python interactive help()
Mark Lawrence breamore...@yahoo.co.uk wrote: On 19/10/2012 09:56, Duncan Booth wrote: Mark Lawrence breamore...@yahoo.co.uk wrote: Good morning/afternoon/evening all, Where is this specific usage documented as my search engine skills have let me down? By this I mean entering help() without parameters to get the following output and then the help prompt. It is documented under 'built-in functions'. http://docs.python.org/library/functions.html#help Well Foxtrot Mike :-) Thanks for the fast response. A harder question would have been if you asked where 'exit()', 'quit()' are documented. For some reason they are hidden under Built-in constants (even though the documentation includes the function call syntax) alongside 'license' and 'credits' which are documented without the parentheses but are also callable. All but 'credits' behave similarly having a repr that gives you instructions to call them and doing something different when you do call them. 'credits' just gives you the same text whether or not you call it. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: portable unicode literals
Alex Strickland s...@mweb.co.za wrote: On 2012/10/15 03:05 PM, Ulrich Eckhardt wrote: This actually came as a surprise to me, I assumed that using b'' I could portably create a byte string (which is true) and using u'' I could portably create a unicode string (which is not true). This feature would help porting code between both versions. While this is a state I can live with, I wonder what the rationale for this is. !puzzled thanks u'' is legal in 3.3 again. and if you want it to work in 3.1 and 3.2 there is the uprefix import hook: https://bitbucket.org/vinay.sajip/uprefix -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: + in regular expression
Cameron Simpson c...@zip.com.au wrote: | Because \s{6}+ | has other meanings in different regex syntaxes and the designers didn't | want confusion? I think Python REs are supposed to be Perl compatible; ISTR an opening sentence to that effect... I don't know the full history of how regex engines evolved, but I suspect at least part of the answer is that the decisions the Perl developers made influenced the other implementations. Perl's quantifiers allow both '?' and '+' as modifiers on the standard quantifiers so clearly you cannot stack those particular quantifiers in Perl, therefore quantifiers in general are unstackable. The only grammars I can find online for regular expressions split out the elements and quantifiers the way I did in my previous post. Python's regex parser (and I would guess also most of the others in existence) tend more to the spaghetti code than following a grammar (_parse is a 238 line function). So I think it really is just trying to match existing regular expression parsers and any possible grammar is an excuse for why it should be the way it is rather than an explanation. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Insert item before each element of a list
mooremath...@gmail.com wrote: What's the best way to accomplish this? Am I over-complicating it? My gut feeling is there is a better way than the following: import itertools x = [1, 2, 3] y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in range(len(x y ['insertme', 1, 'insertme', 2, 'insertme', 3] I appreciate any and all feedback. Given the myriad of proposed solutions, I'm surprised nobody has suggested good old list slicing: x = [1,2,3] y = ['insertme']*(2*len(x)) y[1::2] = x y ['insertme', 1, 'insertme', 2, 'insertme', 3] -- Duncan Booth -- http://mail.python.org/mailman/listinfo/python-list
Re: + in regular expression
Cameron Simpson c...@zip.com.au wrote: On 03Oct2012 21:17, Ian Kelly ian.g.ke...@gmail.com wrote: | On Wed, Oct 3, 2012 at 9:01 PM, contro opinion | contropin...@gmail.com wrote: | why the \s{6}+ is not a regular pattern? | | Use a group: (?:\s{6})+ Yeah, it is probably a precedence issue in the grammar. (\s{6})+ is also accepted. It's about syntax, not precedence, but the documentation doesn't really spell it out in full. Like most regex documentation it talks in woolly terms about special characters rather than giving a formal syntax. A regular expression element may be followed by a quantifier. Quantifiers are '*', '+', '?', '{n}', '{n,m}' (and lazy quantifiers '*?', '+?', '{n,m}?'). There's nothing in the regex language which says you can follow an element with two quantifiers. Parentheses (grouping or non-grouping) around a regex turn that regex into a single element which is why you can then use another quantifier. In bnf, I think Python's regexes would be somthing like: re ::= union | simple-re union ::= re | simple-re simple-re ::= concatenation | basic-re concatenation ::= simple-re basic-re basic-re ::= element | element quantifier element ::= group | nc-group | . | ^ | $ | char | charset quantifier = * | + | ? | { NUMBER } | { NUMBER , NUMBER } |*? | +? | { NUMBER , NUMBER }? group ::= ( re ) nc-group ::= (?: re ) char = any non-special character | \ any character ... and so on. I didn't include charsets or all the (?...) extensions or special sequences. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Java singletonMap in Python
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Mon, 24 Sep 2012 00:14:23 +0100, Mark Lawrence wrote: Purely for fun I've been porting some code to Python and came across the singletonMap[1]. I'm aware that there are loads of recipes on the web for both singletons e.g.[2] and immutable dictionaries e.g.[3]. I was wondering how to combine any of the recipes to produce the best implementation, where to me best means cleanest and hence most maintainable. I then managed to muddy the waters for myself by recalling the Alex Martelli Borg pattern[4]. Possibly or even probably the latter is irrelevant, but I'm still curious to know how you'd code this beast. First prize for the best solution is a night out with me, no guesses what the second prize is :) [1]http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ Collections.html Copied from that page: static Map singletonMap(Object key, Object value) Returns an immutable map, mapping only the specified key to the specified value. I don't see the point of this. It takes a single key, with a single value, and is immutable so you can't change it or add new keys. What's the point? Why bother storing the key:value pair in a data structure, then look up the same data structure to get the same value every time? # Pseudo-code d = singletonMap(key, calculate(key)) # later: value = d[key] # there's only one key this could be process(value) Why not just store the value, instead of key, value and mapping? value = calculate(key) # later process(value) Google is your friend. Searching for java singletonMap gives this as the second hit: http://stackoverflow.com/questions/7125536/when-would-i-use-java-collections-singletonmap-method The answers seem to be that it's for all those cases in Java where you have a method that takes a map as an argument and you want to pass in a map with a single kep/value pair. In that case it lets you replace 3 lines of Java with 1. e.g. from the comments: If you have a simple select statement like select foo from bar where id = :barId then you would need a parameter map with a single key-value pair, barId=123. That's a great place to use singletonMap() Of course in Python you just use a dict literal in that case so it's pointless. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: 'indent'ing Python in windows bat
Jason Friedman ja...@powerpull.net wrote: I'm converting windows bat files little by little to Python 3 as I find time and learn Python. The most efficient method for some lines is to call Python like: python -c import sys; sys.exit(3) How do I indent if I have something like: if (sR=='Cope'): sys.exit(1) elif (sR=='Perform') sys.exit(2) else sys.exit(3) Some months ago I posted what I think is a similar question in the Unix world: I wanted to call a small portion of Python from within a Bash script. Someone on this list answered (for Bash): #!/bin/bash command1 command2 python -c if True: import module if condition: do_this else: do_that command4 # end code Perhaps something similar would work for a .bat file. Provided there's only one Python block in the .bat or .cmd file then there's no real problem; you just have to hide each language from the other: goto :start :start @echo off echo This is a CMD script python -x %~f0 %1 echo Back in the CMD script goto :eof import sys print(Welcome to Python) print(Arguments were {}.format(sys.argv)) print(Bye!) You can put the Python code either before or after the triple-quote string containing the CMD commands, just begin the file with a goto to skip into the batch commands and end them by jumping to eof. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorators not worth the effort
Jean-Michel Pichavant jeanmic...@sequans.com wrote: I wrote the following one, used to decorate any function that access an equipment, it raises an exception when the timeout expires. The timeout is adapted to the platform, ASIC of FPGA so people don't need to specify everytime one timeout per platform. In the end it would replace def boot(self, timeout=15): if FPGA: self.sendCmd(bootMe, timeout=timeout*3) else: self.sendCmd(bootMe, timeout=timeout) with @timeout(15) def boot(self, timeout=None): self.sendCmd(bootMe, timeout) I wrote a nice documentation with sphinx to explain this, how to use it, how it can improve code. After spending hours on the decorator + doc, feedback from my colleagues : What the F... !! I'd agree with your colleagues. How are you going to ensure that all relevant functions are decorated and yet no decorated function ever calls another decorated one? From the code you posted it would seem appropriate that the adjustment of the timeout parameter happen in the `sendCmd()` method itself and nowhere else. Alternatively use named values for different categories of timeouts and adjust them on startup so instead of a default of `timeout= 15` you would have a default `timeout=MEDIUM_TIMEOUT` or whatever name is appropriate. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing strings from the back?
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: But for the record, in principle string comparisons *could* be the bottleneck. Example: you have 1 strings, which are each created once and stored in a list. Then you iterate over the list, comparing every string against every other string. And due to some weird vagary of the data, the strings are nearly all equal. (Why would you do this? I don't know, maybe it's a programmers' challenge found on the Internet, make up your own scenario...) Total number of strings created: 1. Total number of strings compared: 1. which is exactly what I meant by doing a lot of comparisons (1) on the same string. Perhaps if I'd phrased it more as you have to be doing many more comparisons than string creation operations it would have been clearer what I meant. The overhead of creating the strings is trivial compared to the overhead of comparing them, and since each string is only created once anyway, interning them is just a waste of time. No, you created 10k strings many of which are equal and then did 10k comparisons on each most of which found 'yes' they are equal. Interning them would have reduced all the 'true' comparisons to an identity check at the cost of 1 hash and 1 full comparison per string. so at the expense of a single dictionary insertion when the string is created you can get guaranteed O(1) on all the comparisons. What interning buys you is that s == t is an O(1) pointer compare if they are equal. But if s and t differ in the last character, __eq__ will still inspect every character. There is no way to tell Python all strings are interned, if s is not t then s != t as well. Right if the strings differ only in the last character, but the rest of this thread has been about how, for random strings, the not-equal case is O(1) as well. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing strings from the back?
Oscar Benjamin oscar.j.benja...@gmail.com wrote: What interning buys you is that s == t is an O(1) pointer compare if they are equal. But if s and t differ in the last character, __eq__ will still inspect every character. There is no way to tell Python all strings are interned, if s is not t then s != t as well. I thought that if *both* strings were interned then a pointer comparison could decide if they were unequal without needing to check the characters. Have I misunderstood how intern() works? I don't think you've misunderstood how it work, but so far as I can see the code doesn't attempt to short circuit the not equal but interned case. The comparison code doesn't look at interning at all, it only looks for identity as a shortcut. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing strings from the back?
Gelonida N gelon...@gmail.com wrote: On 09/07/2012 06:06 AM, Steven D'Aprano wrote: On Thu, 06 Sep 2012 06:07:38 -0400, Dave Angel wrote: Also of some interest is the best case: O(1) for unequal strings (they differ at the first character) and O(N) for equal strings. The worst case is O(N) or N characters the average case is O(1) or two characters. For denial of service attacks or systems, that are NEVER allowed to fail the worst case is important. For most other cases the average complexity counts. However I still wonder for how many applications the complexity of string comparisons would be the limiting factor. and of course if you ever do find an application where that worst case matters there's an easy way round it: just call intern() on all the strings when they are created. For the comparison to be the limiting factor you have to be doing a lot of comparisons on the same string (otherwise creating the string would be the limiting factor), so at the expense of a single dictionary insertion when the string is created you can get guaranteed O(1) on all the comparisons. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: is implemented with id ?
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: But less exotically, Frank isn't entirely wrong. With current day computers, it is reasonable to say that any object has exactly one physical location at any time. In Jython, objects can move around; in CPython, they can't. But at any moment, any object has a specific location, and no other object can have that same location. Two objects cannot both be at the same memory address at the same time. It is however perfectly possible for one object to be at two or more memory addresses at the same time. In fact some work being done in PyPy right now is doing exactly that as part of Armin Rigo's software transactional memory implementation: when a global object is mutated a new copy is made and some threads may see the new version while other threads continue to see the old version until their transactions are comitted (or aborted). This means that global objects can be safely read from multiple threads without any semaphore locking. See http://mail.python.org/pipermail/pypy-dev/2012-September/010513.html -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Error
Jürgen A. Erhard j...@jaerhard.com wrote: Peter's right, but instead of a print before the line, put a try/except around it, like try: set1 = set(list1) except TypeError: print list1 raise This way, only the *actual* error triggers any output. With a general print before, you can get a lot of unnecessary output. Grits, J Or even better: try: set1 = set(list1) except TypeError: print list1 import pdb; pdb.set_trace() raise Then the error will print the value of list1 and drop you into the debugger so you can examine what's going on in more detail. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: code review
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Fri, 13 Jul 2012 12:30:47 +, Albert van der Horst wrote: The worst of is, of course, = for assignment instead of := . This is a convention that Python follows, to my dismay. *shrug* The worst is to use = for both equality and assignment, like some BASICs. At least Python does not allow assignment as an expression, so you can't make the typical C error of: if x = y: do_something() # oops meant x == y Technically of course Python doesn't have assignment, it just binds names. Albert raised the subject of Algol 68 which if I remember correctly used := for assignment and = to bind names (although unlike Python you couldn't then re-bind the name to another object in the same scope). -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to safely maintain a status file
Richard Baron Penman richar...@gmail.com wrote: Is there a better way? Or do I need to use a database? Using a database would seem to meet a lot of your needs. Don't forget that Python comes with a sqlite database engine included, so it shouldn't take you more than a few lines of code to open the database once and then write out your status every few seconds. import sqlite3 con = sqlite3.connect('status.db') ... with con: cur = con.cursor() cur.execute('UPDATE ...', ...) and similar code to restore the status or create required tables on startup. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictless classes
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: You can create instances without a __dict__ by setting __slots__: That is a necessary but not a sufficient condition. An instance of a class can be created without a dict only if (either you set __slots__ or you implement it in C without a __dict__ attribute) AND none of its base classes require a dict. Setting __slots__ when a base class requires that its instances have a __dict__ doesn't prevent creation of the __dict__. But the class itself still has a __dict__: The class is an instance of 'type'. Instances of 'type' require a dict therefore every class requires a dict. Except of course for old style classes in Python 2.x but they have a __dict__ too. Also you cannot create a subclass of 'type' with non-empty slots (it throws a type error nonempty __slots__ not supported for subtype of 'type'). However I don't think that's really relevant as even if they did allow you to use __slots__ it wouldn't prevent the creation of a __dict__ for the 'type' base class. I don't have a use-case for this. But I have some code which assumes that every class will have a __dict__, and I wonder whether that is a safe assumption. I believe so. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Hashable object with self references OR how to create a tuple that refers to itself
Dieter Maurer die...@handshake.de wrote: You can create a tuple in C and then put a reference to itself into it, but I am quite convinced that you cannot do it in Python itself. (Of course, you could use cython to generate C code with a source language very similar to Python). I don't think you can even do it in C without breaking the specified API: PyTuple_SetItem fails if the reference count of the tuple is not 1, but it also steals the reference of the object that is being added to the tuple so if you don't increment the reference count before attempting to add the tuple to itself you've broken the rules for reference counting. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Help doing it the python way
Scott Siegler scott.sieg...@gmail.com wrote: Hello, I am an experienced programmer but a beginner to python. As such, I can figure out a way to code most algorithms using more C style syntax. I am doing something now that I am sure is a more python way but i can't quite get it right. I was hoping someone might help. So I have a list of grid coordinates (x, y). From that list, I want to create a new list that for each coordinate, I add the coordinate just above and just below (x,y+1) and (x,y-1) right now I am using a for loop to go through all the coordinates and then separate append statements to add the top and bottom. is there a way to do something like: [(x,y-1), (x,y+1) for zzz in coord_list] or something along those lines? thanks! def vertical_neighbours(coords): for x, y in coords: yield x, y-1 yield x, y+1 new_coords = list(vertical_neighbours(coords)) -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: pyjamas / pyjs
james hedley jameskhed...@gmail.com wrote: There's also an allegation, which I am not making myself at this point - only describing its nature, that a person may have lifted data from the original mail server without authorisation and used it to recreate the mailing list on a different machine. *If* that were to be true, then the law has been broken in at least one country. I don't know whether they moved it to another machine or not, but what they definitely did do was start sending emails to all the people on the list who had sending of emails disabled (including myself) which resulted in a flood of emails and from the sound of it a lot of annoyed people. If he wanted to community support for the takeover that probably wasn't a good start. In case it isn't obvious why I might be subscribed but emails turned off, I read mailing lists like that through gmane in which case I still need to sign up to the list to post but definitely don't want to receive emails. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Gotcha's?
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: JSON expects double-quote marks, not single: v = json.loads({'test':'test'}) fails v = json.loads('{test:test}') succeeds You mean JSON expects a string with valid JSON? Quelle surprise. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: string interpolation for python
Yingjie Lan lany...@yahoo.com wrote: Both template based and dict-based formatting require writing the identifier three times: name = 'Peter' Are you %(name)s%{'name':name} ÿ If dynamic string is used: Are you $name$? Template: Template(Are you $name?).substitute(name=name) It is three to one in compactness, what a magic 3! You can avoid the duplication fairly easily: name='Peter' 'Are you {name}?'.format(**vars()) 'Are you Peter?' though if you're doing that it would be better to limit the scope to a specific namespace. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python is readable
Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 @spamschutz.glglgl.de wrote: Or, more likely, lock creates an object which keeps the lock acquired. The lock is released when we leave the block. So we could inspect the lock with with lock as l: inspect l... do_some. Or just inspect l - I don't know if a lock's __enter__ methos returns it again for assignment with as... No, if you do `with lock as l:` then l will get the boolean True. The lock's __enter__ method is the same as lock.acquire(). That method takes an optional argument which can be used to conditionally acquire the lock and return a boolean indicating success or failure. When used inside a `with` statement you can't pass in the optional argument so it acquires it unconditionally but still returns the success status which is either True or it never returns. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python is readable
Kiuhnm kiuhnm03.4t.yahoo.it wrote: BTW, aren't those ':' redundant? They are required by the grammar, but in a sense you are correct. You could modify Python's grammar to make the colons optional and still keep it unambiguous but that would make it harder for other tools (such as text editors or indeed humans) to understand. A little bit of redundancy in the grammar is seen as a good way to minimise errors. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: [RELEASED] Python 3.3.0 alpha 1
Georg Brandl ge...@python.org wrote: For a more extensive list of changes in 3.3.0, see http://docs.python.org/3.3/whatsnew/3.3.html So far as I can see the what's new don't mention that hash randomisation is enabled by default in Python 3.3. I think it would be worth adding something about that. Also the what's new doesn't mention PEP 414 although the release page does. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Please verify!!
Andrew Berg bahamutzero8...@gmail.com wrote: Yes. However, there are many editors for various platforms that handle the different line endings just fine. In fact, Notepad is the only editor I can think of off the top of my head that has an issue. The original question was about Notepad++ which is nothing at all like Notepad. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: sum() requires number, not simply __add__
Stefan Behnel stefan...@behnel.de wrote: I know that you just meant this as an example, but it's worth mentioning in this context that it's not exactly efficient to sum up lists this way because there is a lot of copying involved. Each adding of two lists creates a third one and copies all elements into it. So it eats a lot of time and space. If you search back through this group far enough you can find an alternative implementation of sum that I suggested which doesn't have the same performance problem with lists or strings and also improves the accuracy of the result with floats. In effect what it does is instead of: (a + b) + c) + d) + e) + f) it calculates the sum as: ((a + b) + (c + d)) + (e + f) i.e. in as balanced a manner as it can given that it still has to work from left to right. Of course that could still change the final result for some user defined types and never having converted my code to C I have no idea whether or not the performance for the intended case would be competitive with the builtin sum though I don't see why it wouldn't be. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: #line in python
Ross Boylan r...@biostat.ucsf.edu wrote: As an extension or alternate, could there be a decorator like @source_line(lineno, filename) for classes and methods that could do the conversion on the fly? I don't know if there's a way to go from the function (or class) object the decorator receives to the AST. No [easy] way to go from bytecodes back to AST, but I see no reason why you can't create a new code object with your filename and line numbers and then create a new function using your modified code object. If you don't have a 1:1 correspondence of lines then you'll need to pick out all the existing line numbers from the code object co_lnotab and modify them: see dis.py findlinestarts() for how to do this. Classes would be harder: the decorator doesn't run until after the class body has executed, so you can't change the line numbers that way until it's too late. The only thing I can think would be to put all of the generated code inside a function and fix up that function with a decorator that scans the bytecode to find all contained classes and fix them up. Or you could generate a .pyc file and then fix up line numbers in the whole file: see http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html for some code that shows you what's in a .pyc -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: #line in python (dirty tricks)
Ross Boylan r...@biostat.ucsf.edu wrote: No [easy] way to go from bytecodes back to AST, but I see no reason why you can't create a new code object with your filename and line numbers and then create a new function using your modified code object. Could you elaborate? I don't understand what you are suggesting. This (written for Python 3.x, needs some attribute names changing for Python 2.x: import functools def source_line(lineno, filename = None): def decorator(f): c = f.__code__ code = type(c)( c.co_argcount, c.co_kwonlyargcount, c.co_nlocals, c.co_stacksize, c.co_flags, c.co_code, c.co_consts, c.co_names, c.co_varnames, c.co_filename if filename is None else filename, c.co_name, lineno, c.co_lnotab, c.co_freevars, c.co_cellvars) f.__code__ = code return f return decorator @source_line(43, 'foo.bar') def foo(): This is foo for i in range(10): bar() @source_line(665, 'evil.txt') def bar(): raise RuntimeError(oops) if __name__=='__main__': import traceback try: foo() except RuntimeError as ex: traceback.print_exc() When you run it the output looks something like this (if you create a file evil.txt with 667 lines): Traceback (most recent call last): File C:\temp\foo.py, line 30, in module foo() File foo.bar, line 47, in foo File evil.txt, line 667, in bar Evil line 667 RuntimeError: oops -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: Entitlements [was Re: Python usage numbers]
Rick Johnson rantingrickjohn...@gmail.com wrote: On Feb 14, 5:31 am, Duncan Booth duncan.bo...@invalid.invalid wrote: Rick Johnson rantingrickjohn...@gmail.com wrote: BS! With free healthcare, those who would have allowed their immune system fight off the flu, now take off from work, visit a local clinic, and get pumped full of antibiotics so they can create a new strain of antibiotic resistant flu virus! Thanks free healthcare! Anyone who can write 'antibiotic resistant flu virus' as though they believe it really needs to read some elementary books about disease. Here's a clue: No flu viruses are treatable with antibiotics. In some cas es antibiotics may be useful for flu patients to treat secondary bacterial infections, but they are not effective against viruses. Duncan, your reading and comprehension skills are atrocious. Please re- read the paragraph you quoted, then spend some time comprehending it, then show me where i stated that antibiotics cure viral infections. psst: i NEVER said any such thing! Rick, your reading and comprehension skills are atrocious. Please re-read the paragraph you quoted, then spend some time comprehending it, then show me where I stated that you '''stated that antibiotics cure viral infections'''. I never said any such thing. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: Entitlements [was Re: Python usage numbers]
Arnaud Delobelle arno...@gmail.com wrote: On 15 February 2012 09:47, Duncan Booth duncan.booth@invalid.invalid wrote: Rick Johnson rantingrickjohn...@gmail.com wrote: [...] Perhaps it's a bit presumptuous of me but... It's tempting to react to his inflammatory posts, but after all Rick is a troll and experience shows that trolls are best left alone. Also, please spare a thought for all of us who have him in our killfiles. Yes, sorry about that. Actually, I thought it was a bit weird that I saw ChrisA's comment but not the message he was commenting on until I went and looked for it. I read this group on a couple of machines and it looks like Rick's killfile entry had expired on the other but not this one. Next time I'm back on the other machine I'll try to remember to sort out the killfile. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list