Re: Writing to a file
On Mar 25, 8:07 am, wrote:
> Just curious how others view the 2 examples below for creating and
> writing to a file in Python (in OS X). Is one way better than the other?
> If it was a large amount of text, would the 'os.system' call be a bad
> way to do it?
>
> Thanks.
>
> Jay
>
>
>
> >>> f = open('~/Desktop/test.txt', 'w')
> >>> f.write('testing 1... 2... 3...')
> >>> f.close()
>
> >>> import os
> >>> os.system('echo "Testing a... b... c..." > "~/Desktop/test2.txt"')
> 0
>
>
I personally consider each use of os.system(..) as something that
needs to be eliminated.
Maybe 'echo' isn't too bad... but (for example) is it subject to
limited argument lengths?
Does it perform differently on different OSs? And if it's not
something intrinsic to the
OS, might there be 'PATH' issues: where is the called program?
Finally, there may be
some security issues (in general) though perhaps not in your specific
example.
Of course, if speed is a real issue there may be some value in
buffering a long string
before using whatever method to save it in a file. OTOH these
functions usually include
system buffering (which the incremental os.system(..) call clearly
won't have).
Hope that helps...
--
http://mail.python.org/mailman/listinfo/python-list
Proper way to delete/kill a logger?
I've been trying to migrate some code to using the standard python logging classes/objects. And they seem quite capable of doing what I need them to do. Unfortunately there's a problem in my unit tests. It's fairly common to have to create quite a few entities in the course of a series of tests. What appears to be happening is that the loggers don't get entirely eliminated, so that new invocations end up spewing output messages from each of the loggers that were started with this invocation of the python interpreter. I haven't found a function within the logging module that completely kills a given instance. The removeHandler() function seems to leave a stdout/stderr output even if all the assigned handlers have been removed. I've tried a few workarounds, such as increasing the level to an absurdly large level, which mostly kinda works, and killing the old entity before creating the new test version. There is probably something simple that I'm missing. If you know what that is, please point me to that fine manual - I'd appreciate it. Thanks! -f -- http://mail.python.org/mailman/listinfo/python-list
Re: Proper way to delete/kill a logger?
Thanks Chris and JM, I will explore how much work it's going to take to change the various scripts to _always_ starting the logger from main(). As further explanation - this code predates the logging module, and many of the functions/classes had an optional argument for the home- made logger - and where the caller didn't provide it, it was locally created. The unit tests were carefully enough put together to provide almost complete coverage, resulting in creating the logger possibly a dozen or more times in the course of the tests. So I appreciate what "doing it the right way" means, I was just hoping not to have to go there, as I have a lot of code that uses the old structures. It makes it harder to justify moving to the "newer" module. I'll have to try the logger.handlers=[] approach, thanks. -- http://mail.python.org/mailman/listinfo/python-list
smtplib problem with newly rebuilt Debian/lenny system
A hard drive failure forced me to rebuild my main system. Just a few
things haven't been restored; one of them is a python script which is
used to email users of important events.
In attempting to diagnose the cause, I tried directly executing the
lines inside the python2.5 interpreter:
import smtplib
s= smtplib.SMTP('localhost')
but the second line causes a traceback:
File "", line 1, in
File "/usr/lib/python2.5/smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.5/smtplib.py", line 310, in connect
raise socket.error, msg
socket.error: (97, 'Address family not supported by protocol')
This is with exim4 and python2.5 on a newly installed lenny system.
No error messages appear in /var/log or /var/log/exim4 directories.
Helpful clues or pointers to relevant documentation would be
appreciated!
-f
--
http://mail.python.org/mailman/listinfo/python-list
Re: smtplib problem with newly rebuilt Debian/lenny system
On Mar 27, 11:29 am, [email protected] (Aahz) wrote: > [posted & e-mailed, please respond to newsgroup] > > In article , > > > > cassiope wrote: > > >In attempting to diagnose the cause, I tried directly executing the > >lines inside the python2.5 interpreter: > > > import smtplib > > s= smtplib.SMTP('localhost') > > >but the second line causes a traceback: > > > File "", line 1, in > > File "/usr/lib/python2.5/smtplib.py", line 244, in __init__ > > (code, msg) = self.connect(host, port) > > File "/usr/lib/python2.5/smtplib.py", line 310, in connect > > raise socket.error, msg > > socket.error: (97, 'Address family not supported by protocol') > > >This is with exim4 and python2.5 on a newly installed lenny system. > >No error messages appear in /var/log or /var/log/exim4 directories. > > What happens if you > telnet localhost 25 > > This looks like a network setup issue. > -- > Aahz ([email protected]) <*> http://www.pythoncraft.com/ > > "At Resolver we've found it useful to short-circuit any doubt and just > refer to comments in code as 'lies'. :-)" > --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-3-22 Yes, that's what it was. Exim4 setup was bungled, so wasn't accepting localhost connections. Fixing that resolved the "python" error. -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple keypress sensor
On Apr 9, 6:55 pm, Soumen banerjee wrote: > Hello, > I am writing a little program where i have an array the elements of > which i want to display upon keypresses by the user. The only way on > linux seems like using getch() from the curses library. I wanted a > simpler solution than having to use curses for such a simple thing. > any ideas? > Regards > Soumen A more "natural" solution - within Linux - would be to use the termios library functions. Simple, no curses necessary. HTH... -f -- http://mail.python.org/mailman/listinfo/python-list
Re: Socket question
On Dec 3, 7:45 am, perlsyntax wrote: > On 12/03/2009 09:28 AM, [email protected] wrote:> On 02:52 pm, > [email protected] wrote: > >> Is there away in python i can connect to a server in socket to two > >> servers at the same time or can't it be done? > > > I'm not sure what you're asking. Can you clarify? > > > Jean-Paul > > I just want to know could it be done makeing my own socket tool that > connect to two server at the same time.And what link do i need to look at? Should be pretty straightforward in a multithreadded application, at least in a Unix variant - can't say for sure about windows, but probably there too. Have you checked out the 'socket' module? Interesting name... -- http://mail.python.org/mailman/listinfo/python-list
IOError - cannot create file (linux daemon-invoked script)
I have a daemon on a Linux system that supports a number of Windows clients. Among the functions is to send e-mails, which is sufficiently complicated that I fork() a separate process which gets setuid to a lesser user, and calls a python script which does the actual formatting and emailing (the daemon is written in C). I want to save a copy of the email in a particular directory which is accessible to the Windows clients via samba. The strange thing is that even with the right user-id, I cannot seem to write to the directory, getting an IOError exception. Changing the directory to world-writable fixes this. I can confirm the uid and gid for the script by having the script print these values just before trying to create/write the file. Becoming the same lesser user, I have no problem writing a file to the same directory. Is there anything that I can do to diagnose why this script is failing? For various reasons I don't want to make the directory world- writable. This is on a Debian "squeeze" system, with python 2.5. Thanks for any insights! -- http://mail.python.org/mailman/listinfo/python-list
Re: IOError - cannot create file (linux daemon-invoked script)
On Jan 2, 3:46 pm, Steve Holden wrote: > cassiope wrote: > > I have a daemon on a Linux system that supports a number of Windows > > clients. Among the functions is to send e-mails, which is > > sufficiently complicated that I fork() a separate process which gets > > setuid to a lesser user, and calls a python script which does the > > actual formatting and emailing (the daemon is written in C). I want > > to save a copy of the email in a particular directory which is > > accessible to the Windows clients via samba. > > > The strange thing is that even with the right user-id, I cannot seem > > to write to the directory, getting an IOError exception. Changing the > > directory to world-writable fixes this. I can confirm the uid and gid > > for the script by having the script print these values just before > > trying to create/write the file. Becoming the same lesser user, I > > have no problem writing a file to the same directory. > > Have you looked at the IOError's errno attribute to find out exactly why > the Python subprocess is unable to write to the directory? It's errno=13 ... "permission denied". > > Is there anything that I can do to diagnose why this script is > > failing? For various reasons I don't want to make the directory world- > > writable. > > I'd concur on that decision. > > > This is on a Debian "squeeze" system, with python 2.5. > > > Thanks for any insights! > > Take a closer look at the exception, that might stimulate a thought or two. > > regards > Steve > -- > Steve Holden +1 571 484 6266 +1 800 494 3119 > PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ > Holden Web LLC http://www.holdenweb.com/ > UPCOMING EVENTS: http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: IOError - cannot create file (linux daemon-invoked script)
On Jan 2, 6:40 pm, Christian Heimes wrote: > cassiope wrote: > > The strange thing is that even with the right user-id, I cannot seem > > to write to the directory, getting an IOError exception. Changing the > > directory to world-writable fixes this. I can confirm the uid and gid > > for the script by having the script print these values just before > > trying to create/write the file. Becoming the same lesser user, I > > have no problem writing a file to the same directory. > > Are you able to write to the directory with the user id when you tried > to create a file manually? Yes. Sorry that wasn't clear. > How are you changing the uid and gid of your script? IIRC you have to > set the effective user id with os.seteuid() and os.setegid(). I'm changing the uid and gid in the daemon (which runs with root permissions until the fork and uid/gid change). The uid and gid are confirmed by printing os.getuid() and os.getgid() in the script. > Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: IOError - cannot create file (linux daemon-invoked script)
On Jan 2, 8:02 pm, Cameron Simpson wrote: > On 02Jan2010 15:21, cassiope wrote: > | [...] I want > | to save a copy of the email in a particular directory which is > | accessible to the Windows clients via samba. > | > | The strange thing is that even with the right user-id, I cannot seem > | to write to the directory, getting an IOError exception. Changing the > | directory to world-writable fixes this. I can confirm the uid and gid > | for the script by having the script print these values just before > | trying to create/write the file. Becoming the same lesser user, I > | have no problem writing a file to the same directory. > > Can you show us: > - the directory user and group ownership and permissions > - the daemon's user and group values? Directory permissions: 774 Directory ownership: "lesser user", "special group" where /etc/group has "special group" members including the "lesser user", as well as those who are expected to use the daemon, but not root. Script ownership: "lesser user"; permissions 755 Daemon ownership: root; permissions: 755 (always started by root). The script also has to connect to a postgresql database for part of its work - that part works, > You can also strace your daemon: > > strace -f -e trace=file your-daemon your-daemon-args... 2>strace.out > > and then examine the log for the precise UNIX-level failure. > > Cheers, > -- > Cameron Simpson DoD#743http://www.cskk.ezoshosting.com/cs/ > > Money won't buy happiness, but it will pay the salary of a large research > staff to study the problem. - Bill Vaughan Thanks, Cameron (and Steve and Christian). My first shot with strace (it's been awhile since I've used that - I think your syntax may be a tiny bit off - but it's probably the tool I need to use. Will explore further... -- http://mail.python.org/mailman/listinfo/python-list
Re: IOError - cannot create file (linux daemon-invoked script)
On Jan 3, 3:00 pm, Cameron Simpson wrote: > On 03Jan2010 14:20, cassiope wrote: > | On Jan 2, 8:02 pm, Cameron Simpson wrote: > | > Can you show us: > | > - the directory user and group ownership and permissions > | > - the daemon's user and group values? > | > | Directory permissions: 774 > > That's unusual - why the "4"? Directories with read but no search (1) > are of limited use. (Not none - it's only unusual, not insane). > > | Directory ownership: "lesser user", "special group" where /etc/group > | has "special group" members including the "lesser user", as well as > | those who are expected to use the daemon, but not root. > | Script ownership: "lesser user"; permissions 755 > | Daemon ownership: root; permissions: 755 (always started by root). > > And the script/daemon _runs_ as the "lesser user"? > > If so, superficially the permissions look like they should work. > -- > Cameron Simpson DoD#743http://www.cskk.ezoshosting.com/cs/ > > I couldn't think of anything else to do with it, so I put it on the web. Strace confirms the uid and gid == "lesser user". Changing the directory permissions to 775 changes nothing. Clearly get EACCES error on the attempted file creation. The only other thing is that as part of the python interpreter call, I provide a "reduced environment", just UID,GID,TMP,PWD,USER, and HOME. Is anything else needed? Thanks again, Cameron! -- http://mail.python.org/mailman/listinfo/python-list
Re: IOError - cannot create file (linux daemon-invoked script)
On Jan 4, 7:46 am, Nobody wrote: > On Sun, 03 Jan 2010 13:56:24 -0800, cassiope wrote: > > I'm changing the uid and gid in the daemon (which runs with root > > permissions > > until the fork and uid/gid change). The uid and gid are confirmed by > > printing os.getuid() and os.getgid() in the script. > > Those tell you the *real* UID/GID. Filesystem checks are performed using > the *effective* UID/GID. > > You need to set them with seteuid/setegid and check them with > geteuid/getegid. To Cameron: the file doesn't (yet) exist; and it has the correct full path. To "Nobody" : hey, this seems interesting. First test, invoking seteuid() and setegid() didn't help - but strange thing was these calls didn't show up in the strace, so perhaps I wasn't testing what I thought I was. I'll have to check this later as people are now back to work and needing the daemon for its other functions... Thanks for your ideas! I'll be checking it more later... -- http://mail.python.org/mailman/listinfo/python-list
Re: IOError - cannot create file (linux daemon-invoked script)
On Jan 4, 4:23 pm, Cameron Simpson wrote:
> On 04Jan2010 09:16, cassiope wrote:
> | To Cameron: the file doesn't (yet) exist; and it has the correct full
> | path.
>
> Can you show us the strace output of the failing open() call?
Ah...presumably you mean:
[pid 1976] open("/var/tmp/share/lvrq-Robert.Smith", O_WRONLY|
O_CREAT|O_TRUNC, 0666) = -1 EACCES (Permission denied)
> | To "Nobody" : hey, this seems interesting. First test, invoking
> | seteuid()
> | and setegid() didn't help - but strange thing was these calls didn't
> | show
> | up in the strace, so perhaps I wasn't testing what I thought I was.
>
> If you're using the "-e trace=file" option it won't. That constrains the
> output to file operations to make the log easier to read. Discard the -e
> option to get everything.
>
> Cheers,
> --
> Cameron Simpson DoD#743http://www.cskk.ezoshosting.com/cs/
Wasn't using the -e option. It turns out that the compiler was
changing the
code to use the linux functions setresgid() and setresuid(). That's
why I
didn't see it previously.
If I only use the seteuid/setegid, it "works" - it is able to write
the file.
Unfortunately this may be partly due to its incompletely dropping
priviledges -
the file has root ownership, not "lesser user" ownership.
Using setuid/setgid or setresuid/setresgid where just real OR both
real and
effective identities are set to "lesser user" - it still doesn't work
- no file
is written. Again, "lesser user" has no problem writing a file into
this
directory.
I remain mystified :(
Thanks for your valiant efforts!
--
http://mail.python.org/mailman/listinfo/python-list
Re: IOError - cannot create file (linux daemon-invoked script)
One more tidbit observed: my last note, that it works when using seteuid/setegid? Well - that only applies if the daemon is running under strace (!). It fails if started directly by root, or if the strace session has ended, leaving the main body of the daemon running in its normal headless manner. I wonder if running under "strace -f" - might setegid/seteuid be prevented from having their normal effect? Sigh. -- http://mail.python.org/mailman/listinfo/python-list
Re: IOError - cannot create file (linux daemon-invoked script)
On Jan 5, 10:58 am, Nobody wrote: > On Mon, 04 Jan 2010 21:30:31 -0800, cassiope wrote: > > One more tidbit observed: my last note, that it works when using > > seteuid/setegid? > > Well - that only applies if the daemon is running under strace (!). > > It fails > > if started directly by root, or if the strace session has ended, > > leaving the > > main body of the daemon running in its normal headless manner. > > > I wonder if running under "strace -f" - might setegid/seteuid be > > prevented from > > having their normal effect? > > Possibly. The ptrace() syscall on which strace depends will fail if you > try to trace a "privileged" process and you aren't root, so it's possible > that a ptrace()d process will refuse to become privileged. > > Here, "privileged" includes a process which has changed any of its UIDs or > GIDs (this prevents a normal user from tracing, killing, etc an otherwise > privileged process which has switched to the user's UID for the time being). Thanks. I guess it's time to trim this beast to a simpler state to isolate what's going wrong. This may take awhile amidst everything else I have to be doing... -- http://mail.python.org/mailman/listinfo/python-list
Re: Does a tiny Python distribution exist?
On Mar 25, 5:45 am, Johny wrote: > Does anyone know if there is a tiny Python distribution available > running in a Linux environment? Debian has a package: "python-minimal". HTH... -- http://mail.python.org/mailman/listinfo/python-list
Re: decouple copy of a list
On Dec 10, 6:06 am, Jean-Michel Pichavant wrote: > Dirk Nachbar wrote: > > I want to take a copy of a list a > > > b=a > > > and then do things with b which don't affect a. > > > How can I do this? > > > Dirk > > In [1]: a = [1,2,3] > > In [2]: b = a[:] > > In [3]: b[0] = 5 > > In [4]: a > Out[4]: [1, 2, 3] > > In [5]: b > Out[5]: [5, 2, 3] > > Alternatively, you can write > > import copy > a = [1,2,3] > b = a.copy() > > if the list a contains mutable objects, use copy.deepcopy > (http://docs.python.org/library/copy.html) > > JM I'm not a pyguru, but... you didn't use copy quite right. Try instead: b= copy.copy(a) The other issue that the original person has noticed is that a list may include a reference to something. When a list is copied - if the reference is copied (not "deepcopied"], changes to the referred object will be visible in both lists, even if they are different lists. For more information, refer to the docs in the module. HTH... -- http://mail.python.org/mailman/listinfo/python-list
Re: Engineering numerical format PEP discussion
On Apr 25, 10:19 pm, Chris Rebert wrote: > On Sun, Apr 25, 2010 at 9:42 PM, Keith wrote: > > On Apr 26, 12:02 am, Chris Rebert wrote: > >> On Sun, Apr 25, 2010 at 8:36 PM, Keith wrote: > >> > I am considering writing a PEP for the inclusion of an engineering > >> > format specifier, and would appreciate input from others. > > snip > >> Relevant related information: > >> The Decimal datatype supports engineering format > >> directly:http://docs.python.org/library/decimal.html#decimal.Decimal.to_eng_st... > > > Thanks for pointing that out. Does the engineering community get by > > with the decimal module? > > > Even though this uses the to_eng_string() function, and even though I > > am using the decimal.Context class: > > c = decimal.Context(prec=5) > decimal.Decimal(1234567).to_eng_string(c) > > '1234567' > > > That is not an engineering notation string. > > Apparently either you and the General Decimal Arithmetic spec differ > on what constitutes engineering notation, there's a bug in the Python > decimal library, or you're hitting some obscure part of the spec's > definition. I don't have the expertise to know which is the case. > > The spec:http://speleotrove.com/decimal/decarith.pdf > (to-engineering-string is on page 20 if you're interested) > > Cheers, > Chris > --http://blog.rebertia.com As has been pointed out, the Decimal implementation may not have all the features desired (i.e. forcing a particular "exponent", or a particular number of significant digits). Perhaps it could be enhanced. The other concern is computational complexity - having to perform complex engineering calculations in Decimal form is presumably more burdensome than in floats. What I've done for years is to have a function that converts an arbitrary float into a string. The function has an optional parameter that specifies the number of digits of the output. Isn't as "automatic", of course, probably showing its C origins. -f -- http://mail.python.org/mailman/listinfo/python-list
Re: Cross-platform file paths
On May 7, 7:32 am, utabintarbo wrote: > Until now, I have used the UNC under Windows (XP) to allow my program > to access files located on a Samba-equipped *nix box (eg. > os.path.normpath(r"\\serverFQDN\sharename\dir\filename")). When I try > to open this file under Linux (Red Hat 5), I get a file not found > error. > > Is there a cross-platform method for accessing files on a network > share using Python 2.X? > > TIA normpath will convert forward slashes to backslashes on WinXX systems, but does not seem to do the reverse on posix systems...so try changing your string to use forward slashes. Also- is the path otherwise the same on your Linux system? HTH.. -f -- http://mail.python.org/mailman/listinfo/python-list
Re: python and filter design: calculating "s" optimal transform
On Jun 1, 2:58 pm, Terry Reedy wrote: > On 6/1/2010 2:18 PM, robert somerville wrote: > > > Hi; > > this is an airy question. > > > does anybody have some code or ideas on how to calculate the optimal "S" > > transform of user specified order (wanting the coefficients) for a > > published filter response curve, ie. > > > f(s) = 1.0/(a1*S^2 + a2*S + a3) > > If you do not get an answer here, try the scipy list. Say what There are tables of coefficients for Butterworth, Bessel, and other standard filter forms. Could you be a little clearer on what you're trying to do? -- http://mail.python.org/mailman/listinfo/python-list
Re: Recipes for trace statements inside python programs?
On Jun 25, 1:33 am, Francesco Bochicchio wrote: > Hi all, > > as many - I think - python programmers, I find muself debugging my > scripts by placing print statements in strategic places rather than > using the python debugger, and commenting/uncommenting them according > to myy deugging needs. After a time, these prints staements start to > evolving in some ad-hoc half-baked framework ... so I wonder if there > is somewhere there is a full-baked trace statement support framework > which I can use. I'm aware of the logging module, but for me it its > more geared toward application logging rather than toward trace for > debugging purpose. > > Having googlet and found nothing (or too much but nothing relefìvant), > I'm now asking The List. > > Here is what I have in mind: > > Each module, function, class and method should have an attribute, say > trace_flag, which can be set to true or false value. > > there should be a function TRACE which does something like this: > > if __debug__ : > def TRACE(*args): > if trace_enabled(): print "TRACE(%s) : %s " % ( context(), " > ".join( str(x) for x in args ) ) > > where trace_enabled() should return the value of the innermost > trace_flag (checking current function/method then current class (if > any) then current module) and context() shoud return a string like > "module.function" or "module.class.method" ). > > At this point I could in my test code enable the trace in the > function/class that gives me trouble and disable it after I fixed it, > without having to touch the actual code under test. > > I guess it should not be too hard do using python introspection > modules, but of couse I first would like to know if something like > this already exists. > > I'm aware that this imposes a performance penalty, but my scripts are > not operformance-critical. And if I put an if __debug__ switch What are you trying to achieve that could not be accomplished with the logging module? http://docs.python.org/library/logging.html#module-logging -f -- http://mail.python.org/mailman/listinfo/python-list
