Re: Overuse of try/except/else?
On Sat, May 21, 2011 at 3:40 PM, Cameron Simpson wrote: > These days I think I'd use a LateFunction (a facility of my own which is > a lot like the futures module) which returns a callable when you submit > a function; the worker thread runs the submitted function and catches the > return value or raised exception. Anyone who calls the returned callable > later gets the return value or the exception reraised as appropriate, > so one can avoid the dangerous "catch everything and log" scenario. I like this :) I guess you call this a Proxy Object although your calling it a LateFunction seems more "fitting" :) cheers James -- -- James Mills -- -- "Problems are solved by method" -- http://mail.python.org/mailman/listinfo/python-list
Re: TK program problem
bvdp wrote:
> I've just done an update to my system here to Ubuntu 11.04. Mostly no
> problems ... but I have an important (to me) python/TK program that's
> stopped working. Well, it works ... mostly.
>
> The python version is 2.7.1+ (no idea what the + means!).
>
> I _think_ I have traced the problem to certain menus which call a
> class. The calls appear to be ignored.
>
> Basically, what I have is a line like:
>
> bf = makeButtonBar(root, row=0, column=0, buttons=(
> ("Quit", self.quitall ),
> ("Stop", self.stopPmidi ),
> ("New Dir", self.chd),
> ("Load Playlist", self.playList),
> ("Favorites", selectFav),
> ("Options", setOptions) ) )
>
> To create a menu bar. The function makeButtonBar() creates the buttons
> with:
>
> for txt, cmd in buttons:
> Button(bf, text=txt, height=1, command=cmd).grid(column=c,
> row=0, pady=5)
>
>
> All this is fine (and worked perfectly before my upgrade). The menu
> items which are ordinary functions continue to work. BUT the callbacks
> which are classes are just ignored when they are clicked.
>
> A cut from one of the ignored classes:
>
>
> class selectFav:
>
> def __init__(self):
> ...
>
> And I've inserted some prints in the __init__() and nothing is
> printed. Also, converted the class to new-style () but no change there
> either.
>
> Either python/tk has changed or my system is totally $*(#*#.
> Suggestions welcome!
Here's a minimal script to reproduces the problem:
$ cat tkcallclass.py
import Tkinter as tk
root = tk.Tk()
root.withdraw()
class Classic:
def __init__(self):
print "hello"
button = tk.Button(root, command=Classic)
button.invoke()
$ python2.6 tkcallclass.py
hello
$ python2.7 tkcallclass.py
Traceback (most recent call last):
File "tkcallclass.py", line 11, in
button.invoke()
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 2081, in invoke
return self.tk.call(self._w, 'invoke')
_tkinter.TclError: invalid command name "__main__.Classic"
$
In 2.7 the Tkinter code was changed to use hasattr(obj, "__call__") instead
of callable(obj) to recognize callbacks. This gives different results for
oldstyle classes
>>> class A: pass
...
>>> callable(A)
True
>>> hasattr(A, "__call__")
False
...and they are no longer registered automatically with Tkinter. In theory
you could register them explicitly yourself
$ cat tkcallclass2.py
import Tkinter as tk
root = tk.Tk()
root.withdraw()
class Classic:
def __init__(self):
print "hello"
button = tk.Button(root, command=root.register(Classic))
button.invoke()
$ python2.7 tkcallclass2.py
hello
but in practice changing them to newstyle (i. e. have them inherit from
object) or wrapping them in a lambda appears convenient.
Personally, I would reconsider whether using a class as a callback is really
necessary. Replacing the class with a function should be a straightforward
process.
--
http://mail.python.org/mailman/listinfo/python-list
Re: hash values and equality
Ethan Furman wrote: Ulrich Eckhardt wrote: If two equal objects have different hashes, they will be stored in different places in the hash map. Looking for object1 will then not turn up with object2, even though they are equal. In this case this is the behavior I want. You can't rely on it, though. The hash value gets reduced modulo the size of the dict, so even if two objects have different hashes, in some cases they will land on the same dict slot anyway. So an object such as you're postulating would behave unpredictably when used as a dict key. Sometimes a lookup using a different but equal object would find it, and sometimes not, seemingly at random. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Python in CS1
Except at MIT, who knows some good CS1 references for teaching Python ? Thanks, franck -- http://mail.python.org/mailman/listinfo/python-list
distutils on windows ignoring package_data
Hi, I have created a distutils setup.py script that installs a list of packages (all subpackages of one main package). Some packages have package_data files (icons, translations, etc). Running setup.py install on my Linux system (python 2.6) perfectly installs all package data, but on Windows (tested with both 2.6 and 2.7) the package_data is not installed at all. Also not picked up in build, or bdist_wininst, etc. Only the Python files in the packages are installed. The setup.py file can be seen at https://github.com/wbsoft/frescobaldi/blob/master/setup.py I later added the package_dir incantation, but it doesn't help on Windows and on Linux it worked perfectly without. Does anybody have a clue why Python distutils on Windows ignore the package_data? (Note: sdist perfectly works as the data files are also listed in MANIFEST.in) tia, Wilbert Berendsen -- http://www.wilbertberendsen.nl/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Faster Recursive Fibonacci Numbers
Chris Angelico wrote: It seems strange to smoothly slide from native integer to long integer and just keep on going, and yet to be unable to do the same if there's a fractional part on it. The trouble is that if you always compute exact results by default, the number of digits required can blow up very quickly. There's a story that ABC (the predecessor to Python) calculated everything using rationals. People found that sometimes a calculation would take an unexpectedly long time, and it turned out to be because internally it was creating fractions with huge numerators and denominators. As a consequence, Guido decided that Python would *not* use rationals by default. The same problem doesn't arise with ints, because the only time you get an int with a large number of digits is when you genuinely need a large int. So expanding ints automatically to however many digits is needed doesn't do any harm. In Python 2.6 and later, there's a fractions module for when you need it. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: English Idiom in Unix: Directory Recursively
The supposed inefficiency of recursive implementations is based largely on the properties of hardware that is now obsolete. With modern processors there's no great efficiency hit. In some of the smaller microcontrollers, it's true, you do have to worry about stack overflow; but the ARM processors, for example, provide plenty of stack space. In the microcontroller world, the big performance hits come from the fact that the only available compilers are for C and sometimes C++. (And nobody uses assembly language except for the very little jobs.) The nature of the C language prevents compilers from doing optimisations that are standard in compilers for high-level languages. Most C compilers will, for example, always pass parameters on the stack, despite the generous supply of registers available in newer hardware. However, some C compilers will *also* have one or more "go faster" calling conventions that pass parameters in registers, which can be employed. Over in the PC world, such "go faster" calling conventions are even the default calling convention if no calling convention is explicitly specified, for some C and C++ compilers. http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/function-calling-conventions.html#Compiler http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/function-calling-conventions.html#Optlink http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/function-calling-conventions.html#Watcall -- http://mail.python.org/mailman/listinfo/python-list
Re: hash values and equality
Gregory Ewing wrote: > Ethan Furman wrote: >> Ulrich Eckhardt wrote: >> >>> If two equal objects have different hashes, they >>> will be stored in different places in the hash map. Looking for >>> object1 will then not turn up with object2, even though they are equal. >> >> In this case this is the behavior I want. > > You can't rely on it, though. The hash value gets reduced > modulo the size of the dict, so even if two objects have > different hashes, in some cases they will land on the same > dict slot anyway. > > So an object such as you're postulating would behave > unpredictably when used as a dict key. Sometimes a lookup > using a different but equal object would find it, and > sometimes not, seemingly at random. I think for every potential match the current dict implementation checks identity, then hashes -- and equality only if the hash values are equal. The relevant function is lookdict() in dictobject.c. While that means that the problem you describe cannot occur a simple approach that avoids relying on an implementation detail (?) would be to use (hash(obj), obj) tuples instead of just obj as the key. -- http://mail.python.org/mailman/listinfo/python-list
Re: English Idiom in Unix: Directory Recursively
2011-05-21 10:32, Jonathan de Boyne Pollard skrev: >> The supposed inefficiency of recursive implementations is based >> largely on the properties of hardware that is now obsolete. With >> modern processors there's no great efficiency hit. In some of the >> smaller microcontrollers, it's true, you do have to worry about stack >> overflow; but the ARM processors, for example, provide plenty of stack >> space. >> >> In the microcontroller world, the big performance hits come from the >> fact that the only available compilers are for C and sometimes C++. >> (And nobody uses assembly language except for the very little jobs.) >> The nature of the C language prevents compilers from doing >> optimisations that are standard in compilers for high-level languages. >> Most C compilers will, for example, always pass parameters on the >> stack, despite the generous supply of registers available in newer >> hardware. >> > However, some C compilers will *also* have one or more "go faster" > calling conventions that pass parameters in registers, which can be > employed. Over in the PC world, such "go faster" calling conventions > are even the default calling convention if no calling convention is > explicitly specified, for some C and C++ compilers. > > > http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/function-calling-conventions.html#Compiler > > > http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/function-calling-conventions.html#Optlink > > > http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/function-calling-conventions.html#Watcall > Please include attributions, in this case for Peter Moylan and rusi! -- http://mail.python.org/mailman/listinfo/python-list
Re: English Idiom in Unix: Directory Recursively
2011-05-21 11:52, Lars Enderin skrev: > > Please include attributions, in this case for Peter Moylan and rusi! Just Peter Moylan, sorry! -- http://mail.python.org/mailman/listinfo/python-list
Re: English Idiom in Unix: Directory Recursively
2011-05-21 11:54, Lars Enderin skrev: > 2011-05-21 11:52, Lars Enderin skrev: >> >> Please include attributions, in this case for Peter Moylan and rusi! > > Just Peter Moylan, sorry! Ignore the above. -- http://mail.python.org/mailman/listinfo/python-list
ANN: lfm v2.3
Hi, new version of lfm. Note that it requires python v2.5+ now, and it's incompatible with v3.x. Description: == Last File Manager is a powerful file manager for the UNIX console. It has a curses interface and it's written in Python. Licensed under GNU Public License version 3. Some of the features you could find in lfm: - console-based file manager for UNIX platforms - 1-pane or 2-pane view - tabs - bookmarks - history - vfs for compressed files - dialogs with entry completion - PowerCLI, a command line interface with advanced features - fast access to the shell - direct integration of find/grep, df and other tools - color files by extension - fast file viewer with text and binary modes - ...and many others Download it from: === http://inigo.katxi.org/devel/lfm (home server) or http://code.google.com/p/lfm/ or from http://www.terra.es/personal7/inigoserna/lfm when crap ISP updates its cache. Changes since last version: == + About the code - lfm needs python version 2.5 or upper now + New features - PowerCLI, an advanced command line interface with completion, persistent history, variable substitution and many other useful features. As this is a very powerful tool, read the documentation for examples - history . use different types of history lists: path, file, glob, grep, exec, cli for the different forms and actions . persistent history between sessions => ~/.lfm_history . controlled by a flag in configuration - find/grep . configuration options for ignorecase and regex . sort results . show results as FILE:lineno . much faster - show diff between xxx.orig and xxx files - tar files compress/uncompress - messages.EntryLine has been rewritten, with many new key shorcuts. This is the core behind most of the forms lfm shows when asking for anything. Consult the documentation + Minor changes - reorganize "un/compress file" and "compress directory xxx" in file_menu - config: sort entries when saving - improve load/save handling of new options not present in ~/.lfmrc - added new extensions - messages.error rewritten to offer better messages - added some new key shortcuts messages.SelectItem + Documentation- added a note about python v2.5+ is needed from now on - 'lfm' shell function: change "$*" to "$@" to properly handle paths containg spaces - FAQ: added information about fuse to mount ssh, ftp, smb and webdav - reorganized and fixed key bindings section - documented .lfmrc contents - added link to public BitBucket repository + lots of bugs fixed: - pyview: . last char is not shown if file size is small . last line and wrap: cursor_down or page_next . when number of lines == window height - ncurses v5.8 doesn't accept 0 as width or height - UI crashes: . time string could contain non-ascii characters (reported by Martin Steigerwald) . when filenane length is large in full pane mode . MenuWin, SelectItem: ellipsize entries if bigger than screen width - find or find&grep: . pass "-type f" to find as ".#filename" are temporary emacs files/links that break search . show wrong matches if results contain directories or files with spaces . file->goto_file: move to correct page - copy/move "/file" to "/anydir/anyplace" fails, trying to copy/move to "/" - executing non-ascii programname or args - convoluted issue with link to directory in corner cases (reported by Xin Wang) - rename/backup ".." crashes - we should not compress ".." - create_link, edit_link: don't show error if canceled - only store one copy of the same entry in history - tree: "disable" colors of active panel, "enable" at end - Config.save: work with unicode, only convert to encoding when saving Of course, all comments, suggestions, etc. are welcome. Best regards, Iñigo Serna -- http://mail.python.org/mailman/listinfo/python-list
Re: List of WindowsError error codes and meanings
Andrew Berg writes: > This is probably somewhat off-topic, but where would I find a list of > what each error code in WindowsError means? WindowsError is so broad > that it could be difficult to decide what to do in an except clause. > Fortunately, sys.exc_info()[1][0] holds the specific error code, so I > could put in an if...elif...else clause inside the except clause if I > needed to, but I don't know what all the different errors are. Since Python 2.5, the errno attribute maps the Windows error to error codes that match the attributes of module errno. http://docs.python.org/library/exceptions.html#exceptions.WindowsError So for some purposes you can use the same UNIXy error codes you can use on most other platforms. Example: import errno try: operation() except WindowsError, exc: if exc.errno != errno.ENOENT: raise print "file/directory does not exist" Obviously whether this is useful depends on the error cases you need to handle. Undocumented: when there's no useful mapping to errno, you get errno.EINVAL. John -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get PID from subprocess library
Kushal Kumaran wrote:
> That's how it is able to give you the status. So, if you
> are using getstatusoutput, you will have only one instance of your
> command running.
My intent is to launch only one program instance, which will goes as daemon.
To avoid a second call I'd like rather to use Python than
==code=
def start(self):
'''try to start aria2c as a daemon and return its handle to where it
can
proceed to issue commands'''
# aria2c is running, then don't try it again
if (chkout('ps -A |grep aria2c')[0] > 0):
try:
chkout(self.ARIA_CMD)
except:
raise SystemExit('aria2c is not working as deamon')
elif self.handle: return self.handle
# everything is good, it will return an handle
self.handle= \
xmlrpclib.ServerProxy('http://localhost:%s/rpc' %int(self.numport))
return self.handle
==code=
Here I've named subprocess.getstatusoutput as chkout, I'm calling 2 more
programs to find whether there's a running instance of aria2c. I think it's
not nice, python libraries should get the matter done.
--
goto /dev/null
--
http://mail.python.org/mailman/listinfo/python-list
Re: application level monitoring for python
Walter Chang writes: > Hi > > is there any open source library for python that can allow application > level monitoring ? For example,application can send per request level/ > aggregated monitoring events and some remote server dump it and show > in the monitoring graph in real time ? What's best way of doing > that ? Zenoss is a popular tool for that kind of thing. Here's an example of sending a Zenoss event over XML-RPC: http://dancingpenguinsoflight.com/2009/05/send-events-to-zenoss-from-scripts/ Presumably you can send it evnts over other transports somehow (e.g. UDP). Zenoss is itself implemented in Python. John -- http://mail.python.org/mailman/listinfo/python-list
Re: Overuse of try/except/else?
On Tue, May 10, 2011 at 11:40 AM, Kyle T. Jones wrote: > > It has been hard for me to determine what would constitute overuse. > Good example of abuse is catching KeyboardInterrupt or SystemExit inside some library code. PycURL does it, and its truly annoying. -- With best regards, Daniel Kluev -- http://mail.python.org/mailman/listinfo/python-list
SOLVED Re: distutils on windows ignoring package_data
Solved: the problem was right there in the packagelist() function, it replaced '/' with dots instead of using os.sep ... I'm very sorry for the noise, although it's surprising everything else works without dots in the packages names :-) with regard, Wilbert Berendsen -- http://www.wilbertberendsen.nl/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing a graph image
Bastian Ballmann writes: > Hi, > > the project sounds like the exact tool that i need but regarding the > user manual one has to mark the points on the graph manually. Therefore > it's more work to get the data out than doing it without a tool. Or may > I miss something here? > Greets Read the documentation more carefully: it has features that allow extracting points from line charts automatically. If you want something fully automated, you just want jam on it ;-) John -- http://mail.python.org/mailman/listinfo/python-list
Re: Python in CS1
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 05/21/2011 04:30 AM, Franck Ditter wrote: > Except at MIT, who knows some good CS1 references for teaching Python ? > Thanks, > >franck Check out http://www.python.org/community/sigs/current/edu-sig/, and if nothing there satisfies you jump over onto the edu-sig list. - -- Corey Richardson -BEGIN PGP SIGNATURE- Version: GnuPG v2.0.17 (GNU/Linux) iQEcBAEBAgAGBQJN18oPAAoJEAFAbo/KNFvp9MwH/0zXSTTaxAwYPLSxhyirqr3X DUxyulE5HRn+NIarWyomlDfoayip3boyUBG1GQDDKh+sIIzPT9ETfL7+ep9rwkL4 VA7XSDMLu+4DtUlnFjGlfxCz1REYKVvS4m/9w68F0kRflh5XZzDRBbTz0nXMiMM8 /UPBV8cX1XDq+RYis1baIlMSaro6sK3mHW5avBd9RxO4+IzH0TtKw510EWfRvZ8e ssdEUXZwxHmI0eRwYovynJ7VdLWwY/FLKuuoKl1IOpRwbAH8LtLtAAudHDZKOo9X ctwYfwGPCg39gz+fuJFFUGI6oYw8dkqiDi2su/QwN8JsaMXv4xeOc2ZXkZVYMiM= =7xLM -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: checking if a list is empty
Gregory Ewing writes: > Hans Georg Schaathun wrote: >> 0 is a number as real and existent as any other, >> one would think that the empty list is also as real and existent as >> any other list. > > 0 does have some special properties, though, such as > being the additive identity and not having a multiplicative > inverse. Adding falseness as another special property isn't > too much of a stretch and turns out to be useful. > > Likewise, it's useful to treat empty containers as having > a similar special property, since they're often a base or > terminating case in algorithms. > > It's especially useful in a dynamic language where any > additional operations such as finding the length or > comparing with zero has a run-time cost. > > Yes, you have to learn it, but it's a small thing to > learn with a considerable payoff. (apologies if somebody already bikeshedded this argument in this thread) In the absence of an explicit interface declaration (have any standards emerged for that in Python 3, BTW?), the use of len() does give you some information about the interface, which sometimes makes it easier to change the function. I'm sure you fully understand this, but I'll spell it out. Consider this function: def xyzzy(x): if x: print "yes" Let's say I've read the function, and I've seen this call site: xyzzy(["spam", "eggs"]) Now I want to change xyzzy: def xyzzy(x): if x: print "probably" if len(x) == 1: print "definitely" But there may be many call sites. Perhaps xyzzy even implements part of a poorly-documented external API. So can x be None? There's no way to know short of checking all call sites, which may be impossible. It may not even be feasible to check the *only* call site, if you're implementing somebody else's poorly documented closed-source API (a situation I came across at work only yesterday, when that situation resulted in a bug report). If it's written this way, it's clear that it can't be None: def xyzzy(x): if len(x) != 0: print "yes" John -- http://mail.python.org/mailman/listinfo/python-list
Re: Python in CS1
- Original Message - From: "Corey Richardson" To: Sent: Saturday, May 21, 2011 7:19 AM Subject: Re: Python in CS1 -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 05/21/2011 04:30 AM, Franck Ditter wrote: Except at MIT, who knows some good CS1 references for teaching Python ? Thanks, franck Check out http://www.python.org/community/sigs/current/edu-sig/, and if nothing there satisfies you jump over onto the edu-sig list. - -- Corey Richardson -BEGIN PGP SIGNATURE- Version: GnuPG v2.0.17 (GNU/Linux) iQEcBAEBAgAGBQJN18oPAAoJEAFAbo/KNFvp9MwH/0zXSTTaxAwYPLSxhyirqr3X DUxyulE5HRn+NIarWyomlDfoayip3boyUBG1GQDDKh+sIIzPT9ETfL7+ep9rwkL4 VA7XSDMLu+4DtUlnFjGlfxCz1REYKVvS4m/9w68F0kRflh5XZzDRBbTz0nXMiMM8 /UPBV8cX1XDq+RYis1baIlMSaro6sK3mHW5avBd9RxO4+IzH0TtKw510EWfRvZ8e ssdEUXZwxHmI0eRwYovynJ7VdLWwY/FLKuuoKl1IOpRwbAH8LtLtAAudHDZKOo9X ctwYfwGPCg39gz+fuJFFUGI6oYw8dkqiDi2su/QwN8JsaMXv4xeOc2ZXkZVYMiM= =7xLM -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list Thank you for forwarding this link Corey! It is very interesting and informative. Regards, Patty -- http://mail.python.org/mailman/listinfo/python-list
Re: English Idiom in Unix: Directory Recursively
On 2011-05-19, Peter Moylan wrote: > In the microcontroller world, the big performance hits come from the > fact that the only available compilers are for C and sometimes C++. > (And nobody uses assembly language except for the very little jobs.) > The nature of the C language prevents compilers from doing optimisations > that are standard in compilers for high-level languages. Most C > compilers will, for example, always pass parameters on the stack, > despite the generous supply of registers available in newer hardware. I've been doing microcontroller stuff for 25+ years, almost all in C, and I don't think I've never seen such a compiler. Even on the register-starved 6800 architecture, the first parameter was passed in a register. On architectures with more registers (H8, MSP430, ARM, etc.) It's usually the first 3 or so parameters that are found in registers. -- Grant Edwards grant.b.edwardsYow! Gee, I feel kind of at LIGHT in the head now, gmail.comknowing I can't make my satellite dish PAYMENTS! -- http://mail.python.org/mailman/listinfo/python-list
Re: Python in CS1
On Sat, May 21, 2011 at 09:30, Franck Ditter wrote: > Except at MIT, who knows some good CS1 references for teaching Python ? James Shuttleworth and I did a lot of this at Coventry, the book Python for Rookies came from that. We don't use Python in CS1 at Wolverhampton, but James is still actively using our old syllabus (or a variant). You can reach him here: http://dis-dot-dat.net/ Sarah -- Sarah Mount, Senior Lecturer, University of Wolverhampton website: http://www.snim2.org/ twitter: @snim2 -- http://mail.python.org/mailman/listinfo/python-list
Re: List of WindowsError error codes and meanings
Andrew Berg writes: Since Python 2.5, the errno attribute maps the Windows error to error codes that match the attributes of module errno. Good point, I completely misread that. At least the Windows error code is still available as the winerror attribute. As an aside - call me stupid, but I don't quite follow the purpose of that errno mapping. Surely if the error number can be mapped successfully then the error isn't Windows specific and an OSError should logically be thrown instead? And if it can't be mapped successfully then errno will never be valid so the mapping is pointless? I guess if the mapping is imprecise then it makes some sense as errno is a convenience to avoid people having to grasp the meaning of the many and various winerror.h values. So perhaps I've answered my own question. -- http://mail.python.org/mailman/listinfo/python-list
Re: checking if a list is empty
On 5/21/2011 7:46 AM John J Lee said... Gregory Ewing writes: Hans Georg Schaathun wrote: 0 is a number as real and existent as any other, one would think that the empty list is also as real and existent as any other list. 0 does have some special properties, though, such as being the additive identity and not having a multiplicative inverse. Adding falseness as another special property isn't too much of a stretch and turns out to be useful. Likewise, it's useful to treat empty containers as having a similar special property, since they're often a base or terminating case in algorithms. It's especially useful in a dynamic language where any additional operations such as finding the length or comparing with zero has a run-time cost. Yes, you have to learn it, but it's a small thing to learn with a considerable payoff. (apologies if somebody already bikeshedded this argument in this thread) In the absence of an explicit interface declaration (have any standards emerged for that in Python 3, BTW?), the use of len() does give you some information about the interface, which sometimes makes it easier to change the function. I'm sure you fully understand this, but I'll spell it out. Consider this function: def xyzzy(x): if x: print "yes" Let's say I've read the function, and I've seen this call site: xyzzy(["spam", "eggs"]) Now I want to change xyzzy: def xyzzy(x): if x: print "probably" if len(x) == 1: print "definitely" But there may be many call sites. Perhaps xyzzy even implements part of a poorly-documented external API. So can x be None? There's no way to know short of checking all call sites, which may be impossible. It may not even be feasible to check the *only* call site, if you're implementing somebody else's poorly documented closed-source API (a situation I came across at work only yesterday, when that situation resulted in a bug report). If it's written this way, it's clear that it can't be None: qualified: "...without having been trapped or crashing" so if I found this function in running code it would be clear to me that, given that the app is running and hasn't been crashing, either it hasn't yet been None, or the code isn't accessed at all. Emile def xyzzy(x): if len(x) != 0: print "yes" John -- http://mail.python.org/mailman/listinfo/python-list
Abandoning Python
I still like Python after using it for over a decade, but there are things I don't like. What are your favourite up-and-coming languages of the moment? Here's my wishlist (not really in any order): * A widely used standard for (optional) interface declaration -- or something better. I want it to be easier to know what interface an object has when reading code, and which objects provide that interface. * Lower memory usage and faster execution speed. Yes, this has been a price worth paying. But I do want jam on it, please: give me a language where I get most of Python's advantages but don't have to pay it. * Better support for writing correct programs in the form of better support for things like non-imperative programming, DBC, etc. (with the emphasis on "etc"). * Perhaps better built-in support for common tasks in common application domains. Concurrency, persistence, database queries come to mind. * Better refactoring tools, better code analysis tools (lint, search, etc.). * An even larger user base, contributing more and better free and commercial software. I'm prepared to compromise on the last one. Obviously, it should do all that while preserving all the nice features of Python -- surely an easy task. John -- http://mail.python.org/mailman/listinfo/python-list
Problem in using subprocess module and communicate()
Hi Gurus,
I'm having some problem in using the communicate() along with the
subprocess.I would like to invoke a command prompt and pass on a .bat file
to execute. I went through the subprocess module and understood that using
communicate, we can send the send data to stdin.
According to the documentation
http://docs.python.org/library/subprocess.html#subprocess.call, it says,
if you want to send data to the process’s stdin, you need to create the
Popen object with stdin=PIPE.
so based on this, I have used the below function but it does not seem to
work. Am I missing something here?
import subprocess
import threading
import time
def runMonitor(command):
retcode = subprocess.Popen([command, '\k', 'dir'],
cwd=
'C:\Python26\WorkSpace\FunctionExamples\src',
stdin = subprocess.PIPE,
creationflags=subprocess.CREATE_NEW_CONSOLE)
retcode.wait()
retcode.communicate('scripts_to_execute.bat')
t = threading.Thread(target = runMonitor, args = ("cmd.exe",))
t.start()
while t.is_alive():
print 'Thread is still alive'
time.sleep(0.5)
else:
print 'Thread is dead'
Vijay Swaminathan
--
http://mail.python.org/mailman/listinfo/python-list
Re: Abandoning Python
John J Lee writes: > > > I still like Python after using it for over a decade, but there are > things I don't like. > > What are your favourite up-and-coming languages of the moment? > > Here's my wishlist (not really in any order): > > * A widely used standard for (optional) interface declaration -- or >something better. I want it to be easier to know what interface an >object has when reading code, and which objects provide that >interface. > * Lower memory usage and faster execution speed. Yes, this has been a >price worth paying. But I do want jam on it, please: give me a >language where I get most of Python's advantages but don't have to >pay it. > * Better support for writing correct programs in the form of better >support for things like non-imperative programming, DBC, etc. (with >the emphasis on "etc"). > * Perhaps better built-in support for common tasks in common application >domains. Concurrency, persistence, database queries come to mind. > * Better refactoring tools, better code analysis tools (lint, search, >etc.). > * An even larger user base, contributing more and better free and >commercial software. > > I'm prepared to compromise on the last one. Obviously, it should do all > that while preserving all the nice features of Python -- surely an easy > task. A language I want to give a serious try the coming months is Haskell. -- John Bokma j3b Blog: http://johnbokma.com/Perl Consultancy: http://castleamber.com/ Perl for books:http://johnbokma.com/perl/help-in-exchange-for-books.html -- http://mail.python.org/mailman/listinfo/python-list
[Book] Python 3 Web Development Beginner's Guide
Has anyone read this one? If so, what did you think? -- Bob -- http://mail.python.org/mailman/listinfo/python-list
experiments with dictionary attacks against password hashes, in Python
Hi, I've been experimenting a little with dictionary attacks against password hashes. It turned out that Python is plenty fast for this task, if you use precomputed hash databases. I used a few rather large dictionary files (most of the words of the English language, and most of the words of the Dutch language including derived forms) for a total of almost 600,000 precomputed hashes. With that the program can "crack" 10,000 password hashes in under a second on my 3 year old PC. I've also used a list of 600 'most commonly used' passwords that I gathered from a few sources. That list is used to generate a couple of variations, such as prefixing them with a digit, or typing the word in uppercase, etc. I did this to be able to quickly scan for the most common passwords, but it turned out that using all of the 600,000 precomputed hashes isn't much slower for the experiments that I did. The variations however increase the hit rate because words like "Jennifer9" are not in a normal dictionary file. This one however *is* part of the 'most common' list. So if that is your password, go change it right now ;-) I thought the code I wrote might interest other people as well, so I share it here: (It should run on Python 2.6 and up, including Python 3.x.) Download: http://www.razorvine.net/download/dictionary_attack/ Or by Subversion: svn://svn.razorvine.net/Various/PythonStuff/trunk/dictionaryattack Have fun, Irmen de Jong -- http://mail.python.org/mailman/listinfo/python-list
Re: Abandoning Python
You have ideas, a text editor, and a computer - best get to coding. What's stopping you? You largely want Python, with modifications. Join the development team and help implement those changes, or fork your own flavor and do what you wish. Right? You imagine it's an easy task, so get after it. Also, quit saying "give me". I realize that was a troll post, but I hear people say so many things like this about lots of things. Sick of it. Shut up and do something constructive instead. Contribute with effort, rather than a lot of "wishlist" and "give me" talk. On Sat, May 21, 2011 at 10:49, John J Lee wrote: > > > I still like Python after using it for over a decade, but there are > things I don't like. > > What are your favourite up-and-coming languages of the moment? > > Here's my wishlist (not really in any order): > > * A widely used standard for (optional) interface declaration -- or > something better. I want it to be easier to know what interface an > object has when reading code, and which objects provide that > interface. > * Lower memory usage and faster execution speed. Yes, this has been a > price worth paying. But I do want jam on it, please: give me a > language where I get most of Python's advantages but don't have to > pay it. > * Better support for writing correct programs in the form of better > support for things like non-imperative programming, DBC, etc. (with > the emphasis on "etc"). > * Perhaps better built-in support for common tasks in common application > domains. Concurrency, persistence, database queries come to mind. > * Better refactoring tools, better code analysis tools (lint, search, > etc.). > * An even larger user base, contributing more and better free and > commercial software. > > I'm prepared to compromise on the last one. Obviously, it should do all > that while preserving all the nice features of Python -- surely an easy > task. > > > John > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
referring to package scope from module, using relative import?
Hi, I have a package with several modules in it. The package also has some objects created in the package scope (done in the package __init__.py). Is it possible to access those package scope objects from the modules, with relative imports or something? So that I don't have to import the package itself in its own submodules? Example: A/ __init__.py-> creates A.something thing.py -> needs to do "import A" to access A.something would like to not have to import A I think it's something simple I'm missing... Irmen -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem in using subprocess module and communicate()
On Sat, May 21, 2011 at 8:56 AM, vijay swaminathan wrote:
> Hi Gurus,
>
> I'm having some problem in using the communicate() along with the
> subprocess.I would like to invoke a command prompt and pass on a .bat file
> to execute. I went through the subprocess module and understood that using
> communicate, we can send the send data to stdin.
>
> According to the documentation
> http://docs.python.org/library/subprocess.html#subprocess.call, it says,
>
> if you want to send data to the process’s stdin, you need to create the
> Popen object with stdin=PIPE.
>
> so based on this, I have used the below function but it does not seem to
> work. Am I missing something here?
>
> import subprocess
> import threading
> import time
>
> def runMonitor(command):
> retcode = subprocess.Popen([command, '\k', 'dir'],
Shouldn't that be /k, not \k?
> cwd=
> 'C:\Python26\WorkSpace\FunctionExamples\src',
Sidenote: You really should use forward-slashes in paths like this (or
double-up the backslashes).
> stdin = subprocess.PIPE,
> creationflags=subprocess.CREATE_NEW_CONSOLE)
>
> retcode.wait()
>
> retcode.communicate('scripts_to_execute.bat')
Several issues here:
- .wait() waits for the subprocess to terminate; there's reason for
the shell to terminate here, so your program will hang forever on the
.wait() call.
- It makes no sense to call .communicate() after .wait(), since the
subprocess will have already terminated; you can't communicate with a
dead process.
- .communicate() blocks until the subprocess terminates anyway, so
calling .wait() is pointless.
- I would think a newline is needed in you input to the shell. When
one runs a command in the shell manually, one presses Enter after
typing it, no?
- `retcode` seems a rather poor variable name, since it's holding a
Popen object rather than an integer exit code.
Putting this all together, here's an improved version:
def runMonitor(command):
proc = subprocess.Popen([command, '/k', 'dir'],
cwd='C:/Python26/WorkSpace/FunctionExamples/src',
stdin=subprocess.PIPE,
creationflags=subprocess.CREATE_NEW_CONSOLE)
input_data = 'scripts_to_execute.bat\n' + 'exit\n'
stdout_data, stderr_data = proc.communicate(input_data)
I can't test it since I don't run Windows.
Cheers,
Chris
--
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list
Re: referring to package scope from module, using relative import?
On Sat, May 21, 2011 at 1:28 PM, Irmen de Jong wrote: > Hi, > > I have a package with several modules in it. The package also has some > objects created > in the package scope (done in the package __init__.py). > > Is it possible to access those package scope objects from the modules, with > relative > imports or something? So that I don't have to import the package itself in > its own > submodules? > > > Example: > > A/ > __init__.py -> creates A.something > thing.py -> needs to do "import A" to access A.something > would like to not have to import A You can do the relative import like this: from . import something Or if something were defined in A/otherthing.py, then: from .otherthing import something Note that PEP 8 discourages relative imports and encourages absolute imports, though. This would be the preferred way to do it: from A import something Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: checking if a list is empty
On 5/21/2011 10:46 AM, John J Lee wrote: In the absence of an explicit interface declaration (have any standards emerged for that in Python 3, BTW?), the use of len() does give you some information about the interface, which sometimes makes it easier to change the function. I'm sure you fully understand this, but I'll spell it out. Consider this function: def xyzzy(x): if x: print "yes" To me, this trivial procedure (it is not a mathematical function) is too unrealistic to be a basis for argument. Let's say I've read the function, and I've seen this call site: xyzzy(["spam", "eggs"]) Now I want to change xyzzy: def xyzzy(x): if x: print "probably" if len(x) == 1: print "definitely" You are not changing xyzzy, you are replacing a procedure whose domain is all python objects with a new procedure with a much reduced domain. In other words, you are changing (and contracting) the idea of the procedure. This is a somewhat crazy thing to do and if Python developers did something like that in the stdlib, I would expect multiple protest posting ;-). (Adding code to *expand* the domain is a different matter.) If xyzzy were really about sized collections, then 1. That should be documented *before* it is ever used in permanent code. 2. There should be code in the first production version that uses that fact and which would raise an error on other inputs. But there may be many call sites. Perhaps xyzzy even implements part of a poorly-documented external API. So can x be None? There's no way to know short of checking all call sites, which may be impossible. It may not even be feasible to check the *only* call site, if you're implementing somebody else's poorly documented closed-source API (a situation I came across at work only yesterday, when that situation resulted in a bug report). If it's written this way, it's clear that it can't be None: def xyzzy(x): if len(x) != 0: print "yes" I agree that the domain of a function should be defined from the start (and only expanded in the future). This means considering from the start, or certainly once it runs correctly for intended inputs, what will happen for other inputs. For instance, I believe functions defined on counts (0, 1, 2, ...) should be written to avoid infinite looping on other inputs, in particular, fractional and negative numbers. I can imagine in theory that a gratuitous call to len() might be useful for defining an interface, but as explained above, I am dubious about that in practice and do not find thid example convincing. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: referring to package scope from module, using relative import?
On 21-5-2011 22:00, Ian Kelly wrote: > Note that PEP 8 discourages relative imports and encourages absolute > imports, though. This would be the preferred way to do it: > > from A import something Right. I got rid of the silly relative import stuff. As an added bonus, this makes my original question irrelevant :) Irmen -- http://mail.python.org/mailman/listinfo/python-list
Re: Customize help output from optparse (or argparse)
* Thomas 'PointedEars' Lahn (Thu, 12 May 2011 22:22:20 +0200) > Thorsten Kampe wrote: > > I'm using optparse for a little Python script. > > > > 1. The output from "--help" is: > > """ > > Usage: script.py > > > > script.py does something > > > > Options: > > -h, --help show this help message and exit > > """ > > > > I would prefer to have the description before the usage, like... > > """ > > script.py does something > > > > Usage: script.py > > > > Options: > > -h, --help show this help message and exit > > """ > > [...] > > Is that possible with either optparse or the "new kid on the block" > > argparse. If so how? > > You can easily have #1 with optparse.OptionParser(usage="…")¹, but optparse > is deprecated in favor of argparse.ArgumentParser. I'm already using usage. That's where optparse has it from. Putting the usage message into the description and vice versa is of course not a viable way to go. Thorsten -- http://mail.python.org/mailman/listinfo/python-list
Re: hash values and equality
On 5/19/2011 11:33 PM, Ulrich Eckhardt wrote: For that reason, it is generally useful to use immutable types like integers, floats, strings and tuples thereof as keys. Since you can't change them, you basically have the guarantee that they hash the same. Right. It's something of a lack that Python doesn't have user-defined immutable objects. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: hash values and equality
On 22-5-2011 0:55, John Nagle wrote: > On 5/19/2011 11:33 PM, Ulrich Eckhardt wrote: > >> For that reason, it is generally useful to use immutable types like >> integers, floats, strings and tuples thereof as keys. Since you can't change >> them, you basically have the guarantee that they hash the same. > >Right. It's something of a lack that Python doesn't > have user-defined immutable objects. > > John Nagle > collections.namedtuple is rather powerful though... and can be used as key AFAIK. Irmen -- http://mail.python.org/mailman/listinfo/python-list
Re: TK program problem
Thanks, Peter, for the detailed explanation. I was going to write a bit of sample/minimal code to demo this, but you nicely beat me to it! > Here's a minimal script to reproduces the problem: > > $ cat tkcallclass.py > import Tkinter as tk > > root = tk.Tk() > root.withdraw() > > class Classic: > def __init__(self): > print "hello" > > button = tk.Button(root, command=Classic) > button.invoke() > $ python2.6 tkcallclass.py > hello > $ python2.7 tkcallclass.py > Traceback (most recent call last): > File "tkcallclass.py", line 11, in > button.invoke() > File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 2081, in invoke > return self.tk.call(self._w, 'invoke') > _tkinter.TclError: invalid command name "__main__.Classic" > $ Any idea why I'm not getting any traceback in my program? It just runs and appears to ignore the callback. > In 2.7 the Tkinter code was changed to use hasattr(obj, "__call__") instead > of callable(obj) to recognize callbacks. This gives different results for > oldstyle classes > > >>> class A: pass > ... > >>> callable(A) > True > >>> hasattr(A, "__call__") > > False > > ...and they are no longer registered automatically with Tkinter. In theory > you could register them explicitly yourself > > $ cat tkcallclass2.py > import Tkinter as tk > > root = tk.Tk() > root.withdraw() > > class Classic: > def __init__(self): > print "hello" > > button = tk.Button(root, command=root.register(Classic)) > button.invoke() > $ python2.7 tkcallclass2.py > hello > > but in practice changing them to newstyle (i. e. have them inherit from > object) or wrapping them in a lambda appears convenient. Yes, I can confirm that both the lambda and setting the class to: class selectFav(object): works. > Personally, I would reconsider whether using a class as a callback is really > necessary. Replacing the class with a function should be a straightforward > process. IIRC, I used the class method since it nicely encapsulates a set of operations: - create/raise a window - list a set of configurable options - have and buttons, both of which destroy the window. Having a function as the callback certainly works as well. Not sure which is the best method in the long run ... I'm trying to use classes more in my programming since it's nice to wrap a bunch of functions up like this. Thanks again. -- http://mail.python.org/mailman/listinfo/python-list
Re: TK program problem
On May 20, 4:37 pm, rantingrick wrote: > Thats sounds to me a lot like hammering square pegs though round > holes... Perhaps you should explain first in "plain english" what Ahh, but what fun would the Internet, Usenet and programming be without round holes and square pegs. I thought my English was pretty good. > problem your application is intended to solve, then how it is expected I'm trying very much to focus on the problem I'm having with a particular bit of python code. I'm not trying to have the community solve my coding problems. > to interact with the user, and finally, what exactly is NOT working > correctly. I would suffix such a documentary with the current source > code, verbatim. You want me to attach several hundred/thousand lines of source code to demonstrate that a particular bit of python has changed behavior between versions? -- http://mail.python.org/mailman/listinfo/python-list
Re: hash values and equality
On Sat, 21 May 2011 15:55:56 -0700, John Nagle wrote: > On 5/19/2011 11:33 PM, Ulrich Eckhardt wrote: > >> For that reason, it is generally useful to use immutable types like >> integers, floats, strings and tuples thereof as keys. Since you can't >> change them, you basically have the guarantee that they hash the same. > > Right. It's something of a lack that Python doesn't > have user-defined immutable objects. Although it's not that hard to write your own immutable (-ish) objects. http://northernplanets.blogspot.com/2007/01/immutable-instances-in-python.html Or see the Decimal and Fraction classes in the standard library. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: TK program problem
On 5/21/2011 8:03 PM, bvdp wrote: Yes, I can confirm that both the lambda and setting the class to: class selectFav(object): One of the purposes and advantages of Python 3 is having only one class system. Best to always use new-style classes in Python 2.2+ unless you understand and need old-style classes (and need should be never for most people). -- Terry Jan Reedy. -- http://mail.python.org/mailman/listinfo/python-list
Re: checking if a list is empty
On Sat, 21 May 2011 15:46:01 +0100, John J Lee wrote:
> In the absence of an explicit interface declaration (have any standards
> emerged for that in Python 3, BTW?), the use of len() does give you some
> information about the interface, which sometimes makes it easier to
> change the function.
Er, yes? But in any realistic example (your trivial function xyzzyx below
is not very realistic) you'll almost certainly get additional hints in
the function body. If you have your stereotypical badly-documented
function that does this:
def spam(x):
if x:
print("no")
# ...
x.append(42)
# ...
that's a hint that x is actually meant to be a list. If instead it says
x += 42
that's a good hint that x should not be a list. In either case, changing
the test from "if x" to "if x == []" or "if len(x) == 0" or "if x == 0"
doesn't gain you anything except a slightly longer average line length.
If you're being paid by the character, that's an advantage, but
otherwise, why bother?
True, if the function is sufficiently trivial, as in your xyzzyx example,
then there won't be any such hints as to what sort of input the function
expects. If that's the case, then chances are that the function accepts
*any* object:
> def xyzzy(x):
> if x:
> print "yes"
and changing its semantics to only accept (say) lists, as in your
example, is a risky thing to do.
It is *much* safer to leave that function untouched, create a new one:
def my_xyzzy(alist):
if alist:
print "probably"
if len(alist) == 1:
print "definitely"
and then change the calls to xyzzyx into my_xyzzyx when and as you
determine that it is safe to do so. That will ensure that if you miss a
call of xyzzyx(42) somewhere deep in the library, the code will continue
to run. If not, you'll have a dead function. You can always take it out
later, once you are confident that it is indeed dead code.
[...]
> If it's written this way, it's clear that it can't be None:
>
> def xyzzy(x):
> if len(x) != 0:
> print "yes"
But you're assuming that the function actually is intended to accept only
objects with a __len__ method. If that is the author's intention, there
are better ways of signaling that fact.
I'm not entirely sure what your point is... is it to encourage lazy
programmers to write "len(x)==0" instead of documentation and meaningful
names, or are you just pointing out the occasional silver-lining to a
practice which is generally and usually unnecessary?
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
Re: Abandoning Python
On Sun, May 22, 2011 at 2:49 AM, John J Lee wrote: > Here's my wishlist (not really in any order): How come pony is not listed there? Language cannot be better than python without pony! > * An even larger user base, contributing more and better free and > commercial software. According to all language popularity indexes [1-10], C# and Objective-C are only languages which have any chance to fulfill these requirements, but they arguably less flexible than python and have copyright/patent complications. As there is rather heavy inertia in software development community, expecting some language to acquire "even larger user base" is hopeless. Also, most of these complaints could be solved by using correct python dialect for particular task - RPython, Cython and so on. -- With best regards, Daniel Kluev -- http://mail.python.org/mailman/listinfo/python-list
Re: Abandoning Python
On Sun, May 22, 2011 at 12:25 PM, Daniel Kluev wrote: > According to all language popularity indexes [1-10], C# and Forgot to include references, although everyone probably already knows them, [1] https://www.ohloh.net/languages?query=&sort=projects [2] http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html [3] http://libresoft.es/debian-counting/lenny/index.php?menu=Statistics [4] http://lang-index.sourceforge.net/ [5] http://langpop.com/ and so on -- With best regards, Daniel Kluev -- http://mail.python.org/mailman/listinfo/python-list
Re: Abandoning Python
On Sat, May 21, 2011 at 8:49 AM, John J Lee wrote: > > > I still like Python after using it for over a decade, but there are > things I don't like. > > What are your favourite up-and-coming languages of the moment? > > Here's my wishlist (not really in any order): > > * A widely used standard for (optional) interface declaration -- or > something better. I want it to be easier to know what interface an > object has when reading code, and which objects provide that > interface. > I do miss this sometimes, but pylint takes things far enough for me. > * Lower memory usage and faster execution speed. Yes, this has been a > price worth paying. But I do want jam on it, please: give me a > language where I get most of Python's advantages but don't have to > pay it. > PyPy has quite good speed. Memory use, I'm willing to ignore. > * Better support for writing correct programs in the form of better > support for things like non-imperative programming, DBC, etc. (with > the emphasis on "etc"). > And here I thought Python had pretty good functional programming facilities. What do you miss? AFAIK, DBC in terms of "if condition: raise AssertionError" (or assert). What _is_ the "etc"? > * Perhaps better built-in support for common tasks in common application > domains. Concurrency, persistence, database queries come to mind. > http://wiki.python.org/moin/Concurrency/ For persistence, I tend to use gdbm or the dohdbm I just wrote. But there are at least a few other options. For database queries, why build it in? What's wrong with using a module? > * Better refactoring tools, better code analysis tools (lint, search, > etc.). > I find pylint excellent. My idea of a refactoring tool is vim's n.n.n., but have you looked at PyCharm? > * An even larger user base, contributing more and better free and > commercial software. > Gee, you want a scripting language with a larger userbase? > I'm prepared to compromise on the last one. Obviously, it should do all > that while preserving all the nice features of Python -- surely an easy > task. > -- http://mail.python.org/mailman/listinfo/python-list
Re: Abandoning Python
On Sat, May 21, 2011 at 9:00 AM, John Bokma wrote: > > A language I want to give a serious try the coming months is Haskell. > > Haskell is indeed interesting. However, do any of Haskell's implementations exploit the opportunities for parallelism that Haskell's definition allows? -- http://mail.python.org/mailman/listinfo/python-list
Re: Abandoning Python
On Sat, May 21, 2011 at 6:25 PM, Daniel Kluev wrote: > Also, most of these complaints could be solved by using correct python > dialect for particular task - RPython, Cython and so on. > Cython is an interesting dialect that I use now and then. RPython is probably just as well avoided. PyPy, which is written in RPython and Python, supports Python 2.7 nicely - there's no need to drop down to the level of RPython to get significant performance benefits from PyPy. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why did Quora choose Python for its development?
On Fri, May 20, 2011 at 1:47 PM, Dotan Cohen wrote: > They considered Haskell and OCaml and not a single mention of Perl? > Perl is like 10 languages smushed together. To write it, you need only learn one of the 10. To read someone else's, you don't know what subset of those 10 they've used until you get deep into the code. IOW, it's a kitchen sink language - the kind, I believe, that my comparative languages professor would have described as "offensive". -- http://mail.python.org/mailman/listinfo/python-list
Re: TK program problem
> One of the purposes and advantages of Python 3 is having only one class > system. Best to always use new-style classes in Python 2.2+ unless you > understand and need old-style classes (and need should be never for most > people). > Thanks for this. I'll keep it in mind! One thing I really don't understand ... is there a difference between the old/new forms: class foo: class foo(): In cases where I've played with them, they _appear_ to work the same? Also, where does one find the magic that says that for a tkinter class you should use: class foo(object): Not really sure where "object" comes from. Thanks and best, -- http://mail.python.org/mailman/listinfo/python-list
Re: checking if a list is empty
On Sun, May 22, 2011 at 11:02 AM, Steven D'Aprano wrote: > On Sat, 21 May 2011 15:46:01 +0100, John J Lee wrote: > > Er, yes? But in any realistic example (your trivial function xyzzyx below > is not very realistic) you'll almost certainly get additional hints in > the function body. True, but sometimes those hints will be buried in nested calls, making it less obvious. It might well take some digging to figure out just what sort of object it's meant to be accepting. That's why I prefer to have some sort of type declarations; or alternatively, a good docstring / autodoc comment that says what the function wants and gives. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: checking if a list is empty
On May 22, 1:11 am, Terry Reedy wrote: > I agree that the domain of a function should be defined from the start > (and only expanded in the future). I dont understand... I dont always write correct code -- otherwise called 'a bug' -- though I never let the damn bug lose intentionally. And when I see 'the bug' scampering around to my distress the only way of catching it sometimes is to strengthen an (perhaps earlier unthought, unspecified) invariant or precondition. Which amounts to contracting the domain. > This is a somewhat crazy thing to do and if Python developers > did something like that in the stdlib, I would expect multiple protest > posting ;-). That such protests happen is evidence (I dont say proof) that such contractions are sometimes necessary. In fact when I pick up some code marked as 'alpha' I understand it as saying: Please try this and send us (the developers) bug reports. But in no case are we committed to the current API. [Sometimes this is explicitly said. It is always implied by the 'alpha'] When I see 'Beta' I understand: We think this works and we will not make gratuitous changes but no guarantees. When I see 'Stable' I understand: The API is fixed (ie we will not change it) and we accept the inevitable consequence [Seventh Lehman- Belady law: http://en.wikipedia.org/wiki/Lehman%27s_laws_of_software_evolution ] Why is the C library in linux called libc6 and not just libc? -- http://mail.python.org/mailman/listinfo/python-list
Re: checking if a list is empty
On Sun, May 22, 2011 at 1:02 PM, rusi wrote: > Why is the C library in linux called libc6 and not just libc? I assume you mean this? http://www.linux-m68k.org/faq/glibcinfo.html When you dynamically link against a shared object, you save on executable size, but you have to have that shared object on the target system. That's completely different from API changes. I could compile my code using half a dozen different compilers, and use dynamic linking each time, and then those six binaries would expect six implementations of libc on the target computer. They're incompatible with each other. But they will all have a size_t strlen(const char *) that tells me how long my string is. Everyone has a different way of handling incompatible API changes. The most common is to simply deprecate the old function and create a new one. It's simple and effective. With your original xyzzy function, create a my_xyzzy as per Steven's suggestion; job done. (I'd assume that a function called "my_xyzzy" is a local override, in the same way that I might write a "my_malloc" that does some statisticking and then returns malloc(sz), but that's one of the consequences of trivia - you can't really name it descriptively.) Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Multiprocessing: don't push the pedal to the metal?
Hello again, everyone. I'm developing some custom neural network code. I'm using Python 2.6, Numpy 1.5, and Ubuntu Linux 10.10. I have an AMD 1090T six-core CPU. About six weeks ago, I asked some questions about multiprocessing in Python, and I got some very helpful responses from you all. http://groups.google.com/group/comp.lang.python/browse_frm/thread/374e1890efbcc87b Now I'm back with a new question. I have gotten comfortable with cProfile, and with multiprocessing's various Queues (I've graduated from Pool). I just ran some extensive tests of my newest code, and I've learned some surprising things. I have a pretty picture here (be sure to view the full-size image): http://www.flickr.com/photos/15579975@N00/5744093219 I'll quickly ask my question first, to avoid a TL;DR problem: when you have a multi-core CPU with N cores, is it common to see the performance peak at N-1, or even N-2 processes? And so, should you avoid using quite as many processes as there are cores? I was expecting diminishing returns for each additional core -- but not outright declines. That's what I think my data shows for many of my trial runs. I've tried running this test twice. Once, I was reading a few PDFs and web pages while my speed test was running. But even when I wasn't using the computer for these other (light) tasks, I saw the same performance drops. Perhaps this is due to the OS overhead? The load average on my system monitor looks pretty quiet to me when I'm not running my program. OK, if you care to read further, here's some more detail... My graphs show the execution times of my neural network evaluation routine as a function of: - the size of my neural network (six sizes were tried -- with varying numbers of inputs, outputs and hidden nodes), - the subprocess configuration (either not using a subprocess, or using 1-6 subprocesses), and - the size of the input data vector (from 7 to 896 sets of inputs -- I'll explain the rationale for the exact numbers I chose if anyone cares to know). Each graph is normalized to the execution time that running the evaluation routine takes on a single CPU, without invoking a subprocess. Obviously, I'm looking for the conditions which yield performance gains above that baseline. (I'll be running this particular piece of code millions of times!) I tried 200 repetitions for each combination network size, input data size, and number of CPU cores. Even so, there was substantial irregularity in the timing graphs. So, rather than connecting the dots directly, which would lead to some messy crossing lines which are a bit hard to read, I fit B-spline curves to the data. As I anticipated, there is a performance penalty that is incurred just for parceling out the data to the multiple processes and collating the results at the end. When the data set is small, it's faster to send it to a single CPU, without invoking a subprocess. In fact, dividing a small task among 3 processes can underperform a two-process approach, and so on! See the leftmost two panels in the top row, and the rightmost two panels in the bottom row. When the networks increase in complexity, the size of the data set for which break-even performance is achieved drops accordingly. I'm more concerned about optimizing these bigger problems, obviously, because they take the longest to run. What I did not anticipate was finding that performance reversal with added computing power for large data sets. Comments are appreciated! -- http://mail.python.org/mailman/listinfo/python-list
Re: checking if a list is empty
On May 22, 8:52 am, Chris Angelico wrote: > On Sun, May 22, 2011 at 1:02 PM, rusi wrote: > > Why is the C library in linux called libc6 and not just libc? > > I assume you mean this?http://www.linux-m68k.org/faq/glibcinfo.html Ha Ha! Thanks for that link! I quote: > You should not be using libc4 for anything any more. If you do use it, we > will hunt you > down and execute you as an example to others. (Not really, but you get the > point...) Recently on the emacs list there was a big flame-fest because the behavior (aka interface) of return/newline changed. The argument for change: Can we have emacs behave a little more like a 21st century application? Against: Somebody's scripting code broke badly. You may take any side you like. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why did Quora choose Python for its development?
On Sun, May 22, 2011 at 11:50 AM, Dan Stromberg wrote: > Perl is like 10 languages smushed together. To write it, you need only > learn one of the 10. To read someone else's, you don't know what subset of > those 10 they've used until you get deep into the code. +1 QOTW. Perl: The Swiss Army Knife of programming languages, including the bit where you can never find the right blade to open up. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: checking if a list is empty
On Sun, May 22, 2011 at 2:32 PM, rusi wrote: > Recently on the emacs list there was a big flame-fest because the > behavior (aka interface) of return/newline changed. > The argument for change: Can we have emacs behave a little more like a > 21st century application? > Against: Somebody's scripting code broke badly. > > You may take any side you like. The side I take is: If you want emacs, use emacs. If you want SciTE, use SciTE. If you want nano, use nano. And sometimes, you might have special circumstances that demand a different choice (can't use SciTE over ssh, afaik), so it's worth learning more than one. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: Why did Quora choose Python for its development?
i think we should end our butchering of perl on a light note (you may have already read this): EXTERIOR: DAGOBAH -- DAY With Yoda strapped to his back, Luke climbs up one of the many thick vines that grow in the swamp until he reaches the Dagobah statistics lab. Panting heavily, he continues his exercises -- grepping, installing new packages, logging in as root, and writing replacements for two-year-old shell scripts in Python. YODA: Code! Yes. A programmer's strength flows from code maintainability. But beware of Perl. Terse syntax... more than one way to do it... default variables. The dark side of code maintainability are they. Easily they flow, quick to join you when code you write. If once you start down the dark path, forever will it dominate your destiny, consume you it will. LUKE: Is Perl better than Python? YODA: No... no... no. Quicker, easier, more seductive. LUKE: But how will I know why Python is better than Perl? YODA: You will know. When your code you try to read six months from now. On Sat, May 21, 2011 at 10:01 PM, Chris Angelico wrote: > On Sun, May 22, 2011 at 11:50 AM, Dan Stromberg > wrote: > > Perl is like 10 languages smushed together. To write it, you need only > > learn one of the 10. To read someone else's, you don't know what subset > of > > those 10 they've used until you get deep into the code. > > +1 QOTW. > > Perl: The Swiss Army Knife of programming languages, including the bit > where you can never find the right blade to open up. > > Chris Angelico > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
count strangeness
tal 65% python2.7 Python 2.7.1 (r271:86832, May 21 2011, 22:52:14) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. py> class C(object): ... def __init__(self): ... self.data = [] ... def doit(self, count=0): ... for c in self.data: ... count += c.doit(count) ... count += 1 ... print count ... return count ... py> c = C() py> c.data.extend([C() for i in xrange(10)]) py> c.doit() 1 2 4 8 16 32 64 128 256 512 1024 1024 WTF? James -- http://mail.python.org/mailman/listinfo/python-list
Re: count strangeness
On Sat, May 21, 2011 at 11:02 PM, James Stroud wrote: > tal 65% python2.7 > Python 2.7.1 (r271:86832, May 21 2011, 22:52:14) > [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > py> class C(object): > ... def __init__(self): > ... self.data = [] > ... def doit(self, count=0): > ... for c in self.data: > ... count += c.doit(count) > ... count += 1 > ... print count > ... return count > ... > py> c = C() > py> c.data.extend([C() for i in xrange(10)]) > py> c.doit() > 1 > 2 > 4 > 8 > 16 > 32 > 64 > 128 > 256 > 512 > 1024 > 1024 > > WTF? Assuming your question is "Why is 1024 there twice?", the answer is that it was the return value of the initial c.doit() call, and the interactive interpreter outputs all non-None expression results; for example: [In the Python REPL] >>> 5 + 5 10 >>> So, you explicitly print the 1024 in your function once, and the interpreter implicitly outputs it the second time. Notice what happens when we use a statement instead of an expression: >>> final = c.doit() 1 2 4 8 16 32 64 128 256 512 1024 >>> final 1024 >>> And if we comment out the `print` in C.doit() and run your example again: >>> c = C() >>> c.data = [C() for i in xrange(10)] >>> c.doit() 1024 >>> Cheers, Chris -- http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Is there a better way to solve this?
Hello,
I'm a new bie to python programming and on the processing of learning python
programming. I have coded my first program of fibonnaci generation and would
like to know if there are better ways of achieving the same.
I still feel quite a few things to be improved. Just wanted experts thoughts
on this.
try:
length = input('Enter the length till which you want to generate the
fibonnaci series: \n')
print type(length)
except:
print 'Invalid input detected'
exit(0)
if type(length) is int:
result = []
a = 0
b = 1
result.append(a)
result.append(b)
if length > 2:
count = 2
while count < length:
c = a + b
a = b
b = c
result.append(c)
count += 1
else:
print result
print 'The lenght of result is : ' , len(result)
else:
print 'Fibonacci series could not be generated !!!'
--
http://mail.python.org/mailman/listinfo/python-list
Re: count strangeness
James Stroud wrote: > tal 65% python2.7 > Python 2.7.1 (r271:86832, May 21 2011, 22:52:14) > [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > py> class C(object): > ... def __init__(self): > ... self.data = [] > ... def doit(self, count=0): > ... for c in self.data: > ... count += c.doit(count) > ... count += 1 > ... print count > ... return count > ... > py> c = C() > py> c.data.extend([C() for i in xrange(10)]) > py> c.doit() > 1 > 2 > 4 > 8 > 16 > 32 > 64 > 128 > 256 > 512 > 1024 > 1024 > > > > WTF? Put the code into a file, run it -- and be enlightened ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: count strangeness
Chris Rebert wrote: WTF? Assuming your question is "Why is 1024 there twice?", the answer is The question is "Why is 1024 there at all?" It should be 10. James -- http://mail.python.org/mailman/listinfo/python-list
Re: count strangeness
Peter Otten wrote: James Stroud wrote: WTF? Put the code into a file, run it -- and be enlightened ;) tal 72% python2.7 eraseme.py 1 2 4 8tal 73% cat eraseme.py #! /usr/bin/env python class C: def __init__(self): self.data = [] def doit(self, count=0): for c in self.data: count += c.doit(count) count += 1 print count return count c = C() c.data.extend([C() for i in xrange(10)]) c.doit() tal 74% python2.7 eraseme.py 1 2 4 8 16 32 64 128 256 512 1024 Hmmm. It's still 1024. What am I missing? James -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a better way to solve this?
On Sat, May 21, 2011 at 11:28 PM, Ganapathy Subramanium
wrote:
> Hello,
>
> I'm a new bie to python programming and on the processing of learning python
> programming. I have coded my first program of fibonnaci generation and would
> like to know if there are better ways of achieving the same.
>
> I still feel quite a few things to be improved. Just wanted experts thoughts
> on this.
>
> try:
>
> length = input('Enter the length till which you want to generate the
> fibonnaci series: \n')
> print type(length)
>
> except:
> print 'Invalid input detected'
> exit(0)
Never use the input() function in Python 2.x; it does an eval(), which
is evil. Always use raw_input() and an explicit conversion instead;
e.g.
Q = 'Enter the length till which you want to generate the fibonnaci series: \n'
try:
length = int(raw_input(Q))
except ValueError:
print 'Invalid input detected'
exit(0)
Also, as a sidenote:
> if type(length) is int:
Is normally written:
if isinstance(length, int):
Cheers,
Chris
--
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list
Re: count strangeness
James Stroud wrote: Chris Rebert wrote: WTF? Assuming your question is "Why is 1024 there twice?", the answer is The question is "Why is 1024 there at all?" It should be 10. James I mean 11, not 10--but you get the point. James -- http://mail.python.org/mailman/listinfo/python-list
