Safely modify a file in place -- am I doing it right?

2011-06-29 Thread steve+comp . lang . python
I have a script running under Python 2.5 that needs to modify files in place. I want to do this with some level of assurance that I won't lose data. E.g. this is not safe: def unsafe_modify(filename): fp = open(filename, 'r') data = modify(fp.read()) fp.close() fp = open(filename,

Re: using an instance of Object as an empty class

2011-06-29 Thread steve+comp . lang . python
Ulrich Eckhardt wrote: > Peter Otten wrote: >> Examples for classes that don't accept attributes are builtins >> like int, tuple, and -- obviously -- dict. You can make your own >> using the __slot__ mechanism: >> > class A(object): >> ... __slots__ = ["x", "y"] >> ... > a = A() >

Re: Python 3 syntax error question

2011-06-26 Thread steve+comp . lang . python
rzed wrote: > I've tried to install PySVG in a Python 3 setting, and I get a few > errors on the build. Most are easy to fix, but this one I can't > explain or fix: > > > Traceback (most recent call last): > File "", line 1, in > File "builders.py", line 12, in > from pysvg.shape impor

Re: those darn exceptions

2011-06-25 Thread steve+comp . lang . python
Chris Angelico wrote: > On Sat, Jun 25, 2011 at 10:25 AM, Ben Finney > wrote: >> No. The answer is *still* “why, any exception at all”. The name >> ‘os.read’ could be re-bound at run-time to any object at all, so a code >> checker that you “point at any given line of code” can't know what the >>

Re: what's the big deal for print()

2011-06-24 Thread steve+comp . lang . python
John Gordon wrote: > In > pipehappy writes: > >> Why people want print() instead of print str? That's not a big deal >> and the old choice is more natural. Anyone has some clue? > > Because the new Python uses print(). print "str" is the old way. I think you missed the point of the question

Re: Significant figures calculation

2011-06-24 Thread steve+comp . lang . python
Jerry Hill wrote: > I'm curious. Is there a way to get the number of significant digits > for a particular Decimal instance? I spent a few minutes browsing > through the docs, and didn't see anything obvious. I was thinking > about setting the precision dynamically within a function, based on >

RE: unzip problem

2011-06-24 Thread steve+comp . lang . python
Ahmed, Shakir wrote: > The problem is happening when it is coming to write option. > > The * is not the real name of the zip file, I just hide the name. Please don't waste our time by showing us fake code that doesn't do what you say it does. You said that "The script is here", but that was not