Re: [Tutor] 2.7.3 generator objects
On Sun, Sep 2, 2012 at 1:44 AM, Ray Jones wrote: > > I was playing with os.walk today. I can use os.walk in a for loop (does > that make it an iterator or just an irritable? ^_^), The output from os.walk is a generator, which is an iterator. os.walk actually calls itself recursively, creating a chain of generators. Take a look: print inspect.getsource(os.walk) > os.walk to 'test' (test = os.walk()), that variable becomes a > generator object that does not work in a for loop. You'll have to provide more information. That should work fine if you haven't already exhausted the generator. > it's supposed to work in a generator function using 'yield', but I'm at > a loss at how that all works. > > I suppose I should just stick with using the os.walk in the for loop, > but I'd like to make sense of the whole thing. Please someone explain > this to me? A generator function is a function that uses the keyword "yield" instead of "return" (an empty return statement is allowed). When you call a generator function, the return value is a generator object. Think of the generator function as a factory for generator objects. A return in the generator function (implicit or with a "return" statement) corresponds to the generator object raising StopIteration. A generator object is an iterator. Specifically, it has the methods __iter__ and "next" (in 3.x it's __next__), and "iter(genobject) is genobject". To be an iterable in general, it suffices to have either an __iter__ method or a __getitem__ method. Here are the glossary definitions: http://docs.python.org/glossary.html#term-iterable http://docs.python.org/glossary.html#term-iterator Also, here is the PEP for simple generators: http://www.python.org/dev/peps/pep-0255/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 2.7.3 generator objects
On 09/01/2012 11:57 PM, eryksun wrote: > To be an iterable in general, it suffices to have either an __iter__ > method or a __getitem__ method. Here are the glossary definitions: > http://docs.python.org/glossary.html#term-iterable > http://docs.python.org/glossary.html#term-iterator After a few times re-reading, I'm beginning to get a glimmer > Also, here is the PEP for simple generators: > http://www.python.org/dev/peps/pep-0255/ but this is complete Greek! ;) But didn't I read somewhere that you can reset an iterator to go through the whole process again? Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using multiprocessing efficiently to process large data file
On Sun, Sep 2, 2012 at 2:41 AM, Alan Gauld wrote: > >> if __name__ == '__main__': # <-- required for Windows > > Why? > What difference does that make in Windows? It's a hack to get around the fact that Win32 doesn't fork(). Windows calls CreateProcess(), which loads a fresh interpreter. multiprocessing then loads the module under a different name (i.e. not '__main__'). Otherwise another processing Pool would be created, etc, etc. This is also why you can't share global data in Windows. A forked process in Linux uses copy on write, so you can load a large block of data before calling fork() and share it. In Windows the module is executed separately for each process, so each has its own copy. To share data in Windows, I think the fastest option is to use a ctypes shared Array. The example I wrote is just using the default Pool setup that serializes (pickle) over pipes. FYI, the Win32 API imposes the requirement to use CreateProcess(). The native NT kernel has no problem forking (e.g. for the POSIX subsystem). I haven't looked closely enough to know why they didn't implement fork() in Win32. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 2.7.3 generator objects
On Sun, Sep 2, 2012 at 3:09 AM, Ray Jones wrote: > > But didn't I read somewhere that you can reset an iterator to go through > the whole process again? You could implement that ability in your own objects, but it's not part of the protocol. I forgot to mention generator expressions. This is an expression (typically requiring parentheses) that evaluates to a generator object. You can omit the parentheses if it's the only argument in a call. For example: >>> g = (chr(i) for i in range(65, 69)) >>> ", ".join(g) 'A, B, C, D' >>> ", ".join(chr(i) for i in range(65, 69)) 'A, B, C, D' http://docs.python.org/glossary.html#generator%20expression ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 2.7.3 generator objects
On 02/09/12 17:09, Ray Jones wrote: But didn't I read somewhere that you can reset an iterator to go through the whole process again? In general, no. The usual way to "reset" an iterator is to re-create it. walker = os.walk("/home/steve/start") # ... process files in walker walker = os.walk("/home/steve/start") # ... and process them again -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] making len() and __len__ return a non-int
Hi, If I implement __len__ in my own class, does it really have to return an int? Is there no way around this (other than modifying the source code of python itself ;-) It would be nice if len(Example(row, col)) would return a dictionary, or a two-tuple (see code below). The strange thing is that calling __len__ directly does work: Example(1, 2).__len__() returns the dictionary. I always thought len() was a convenient "shorthand" for __len__. I've even been reading about metaclasses (interesting, dark, mysterious), thinking the answer might lie there. Note that my question is not whether it's a good idea to do this, I just find it interesting to understand how it could be done. Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32 >>> class Example(object): def __init__(self, row, col): self.row = row self.col = col def __len__(self): return {self.row: self.col} >>> len(Example(1, 2)) Traceback (most recent call last): File "", line 1, in len(Example(1, 2)) TypeError: an integer is required >>> Example(1, 2).__len__() {1: 2} Regards, Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 2.7.3 generator objects
Steven D'Aprano wrote: > On 02/09/12 17:09, Ray Jones wrote: > >> But didn't I read somewhere that you can reset an iterator to go through >> the whole process again? > > In general, no. > > The usual way to "reset" an iterator is to re-create it. > > > walker = os.walk("/home/steve/start") > # ... process files in walker > walker = os.walk("/home/steve/start") > # ... and process them again Python doesn't enforce this behaviour for iterators in general, but it is part of the spec: """ - Once a particular iterator object has raised StopIteration, will it also raise StopIteration on all subsequent next() calls? Some say that it would be useful to require this, others say that it is useful to leave this open to individual iterators. Note that this may require an additional state bit for some iterator implementations (e.g. function-wrapping iterators). Resolution: once StopIteration is raised, calling it.next() continues to raise StopIteration. """ See http://www.python.org/dev/peps/pep-0234/ For illustration purposes here's a non-compliant iterator: WRONG: >>> class Iterator: ... def __init__(self, max, factor=2): ... self.max = max ... self.factor = factor ... self.value = 1 ... def __iter__(self): ... return self ... def __next__(self): ... result = self.value ... if result >= self.max: ... raise StopIteration ... self.value *= self.factor ... return result ... def reset(self): ... self.value = 1 ... >>> it = Iterator(8) >>> next(it) 1 >>> next(it) 2 >>> next(it) 4 >>> next(it) Traceback (most recent call last): File "", line 1, in File "", line 11, in __next__ StopIteration >>> next(it) Traceback (most recent call last): File "", line 1, in File "", line 11, in __next__ StopIteration >>> it.reset() >>> next(it) 1 >>> next(it) 2 BETTER (Iterator is the same as above, without* the reset() method): >>> class Iterable: ... def __init__(self, max, factor=2): ... self.max = max ... self.factor = factor ... def __iter__(self): ... return Iterator(self.max, self.factor) ... >>> it = Iterable(8) >>> next(it) Traceback (most recent call last): File "", line 1, in TypeError: 'Iterable' object is not an iterator >>> x = iter(it) >>> next(x) 1 >>> next(x) 2 >>> next(x) 4 >>> next(x) Traceback (most recent call last): File "", line 1, in File "", line 11, in __next__ StopIteration Now instead of resetting the iterator ask the iterable for a new iterator: >>> x = iter(it) >>> next(x) 1 >>> next(x) 2 >>> next(x) 4 >>> next(x) Traceback (most recent call last): File "", line 1, in File "", line 11, in __next__ StopIteration (*) Of course I cheated and left it in ;) PS: Since the advent of generators people usually write def g(max, factor=2): value = 1 while value < max: yield value value *= factor They're all lazy bastards... PPS: 'max' should rather be called 'stop' since it indicates the upper bound of a half-open interval. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] making len() and __len__ return a non-int
Albert-Jan Roskam wrote: > If I implement __len__ in my own class, does it really have to return an > int? Is there no way around this (other than modifying the source code of > python itself ;-) It would be nice if len(Example(row, col)) would return > a dictionary, or a two-tuple (see code below). The strange thing is that > calling __len__ directly does work: Example(1, 2).__len__() returns the > dictionary. I always thought len() was a convenient "shorthand" for > __len__. I've even been reading about metaclasses (interesting, dark, > mysterious), thinking the answer might lie there. Note that my question is > not whether it's a good idea to do this, Ah, you already know it's a bad idea... > I just find it interesting to understand how it could be done. It cannot be done without modifying the source. Here's the implementation of len() in Python 3.3: static PyObject * builtin_len(PyObject *self, PyObject *v) { Py_ssize_t res; res = PyObject_Size(v); if (res < 0 && PyErr_Occurred()) return NULL; return PyLong_FromSsize_t(res); } I did not successfully drill down further, but you can see that it may signal an error or return a Python long (which I think is the same as a Python int in 3.x) and that the underlying code operates on Py_size_t, so you'd have to modify that code, too. Py_ssize_t is implementation dependent -- on my 64-bit Linux valid lengths are in range(0, 2**63): >>> class A: ... def __len__(self): return self._len ... def __init__(self, len): ... self._len = len ... >>> len(A(-1)) Traceback (most recent call last): File "", line 1, in ValueError: __len__() should return >= 0 >>> len(A(2**63)) Traceback (most recent call last): File "", line 1, in OverflowError: cannot fit 'int' into an index-sized integer >>> len(A(2**63-1)) 9223372036854775807 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: Running a script in the background
forwarding accidental reply to person only -- Forwarded message -- From: Walter Prins Date: 2 September 2012 15:05 Subject: Re: [Tutor] Running a script in the background To: Michael Lewis On 2 September 2012 05:00, Michael Lewis wrote: > >> For windows not sure but for osx just add an & after the command. >> >> python myscript.py & > > > Thanks, but I know about that. I should have been more clear. What I want to > do is have the script run in the background without even seeing the > terminal. Adding the & after the command will let do other things, but the > terminal still needs to be open. Once the program is running, I don't want > either the terminal or the interpreter displayed. try: nohup python myscript.py & Then you can close the terminal afterwards. "nohup" means"no hangup". It tells the system that the python process launched as a result of this command should not be terminated when its parent shell is terminated. You can also put the above command in a shell script and run the script directly via whatever method suits you best. (Launcher, scheduled job, login script etc.) Walter -- Walter Prins Trendata Solutions Limited 26 Kirfield Drive, Hinckley, Leicestershire, LE10 1SX Tel: 01455 635 994 (fax/landline) 077 8713 1543 (mobile) Email: i...@trendatasolutions.ltd.uk Registered Office: 8 Emmanuel Court, 10 Mill Street, Birmingham, B72 1TJ Company registered in England No: 07364060 VAT No: 998 3569 37 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] making len() and __len__ return a non-int
Albert-Jan Roskam wrote: > >> If I implement __len__ in my own class, does it really have to return an >> int? Is there no way around this (other than modifying the source code of >> python itself ;-) It would be nice if len(Example(row, col)) would return >> a dictionary, or a two-tuple (see code below). The strange thing is that >> calling __len__ directly does work: Example(1, 2).__len__() returns the >> dictionary. I always thought len() was a convenient "shorthand" for >> __len__. I've even been reading about metaclasses (interesting, dark, >> mysterious), thinking the answer might lie there. Note that my question is >> not whether it's a good idea to do this, > >Ah, you already know it's a bad idea... > >> I just find it interesting to understand how it could be done. > >It cannot be done without modifying the source. > >Here's the implementation of len() in Python 3.3: > >static PyObject * >builtin_len(PyObject *self, PyObject *v) >{ > Py_ssize_t res; > > res = PyObject_Size(v); > if (res < 0 && PyErr_Occurred()) > return NULL; > return PyLong_FromSsize_t(res); >} > >===> aha. So if I do len(Example(1, 2)) the C function "builtin_len" will be >called, whereas if I formulate it like Example(1, 2).__len__(), only the >__len__ special method of my own bogus class will be called. Interesting. I >was already checking if I could find __builtin__.__len__, but that's not >possible, probably because they're all compiled. > >Thanks for your help! > >I did not successfully drill down further, but you can see that it may >signal an error or return a Python long (which I think is the same as a >Python int in 3.x) and that the underlying code operates on Py_size_t, so >you'd have to modify that code, too. Py_ssize_t is implementation dependent >-- on my 64-bit Linux valid lengths are in range(0, 2**63): > class A: >... def __len__(self): return self._len >... def __init__(self, len): >... self._len = len >... len(A(-1)) >Traceback (most recent call last): > File "", line 1, in >ValueError: __len__() should return >= 0 len(A(2**63)) >Traceback (most recent call last): > File "", line 1, in >OverflowError: cannot fit 'int' into an index-sized integer len(A(2**63-1)) >9223372036854775807 > > >___ >Tutor maillist - Tutor@python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor > > > From: Peter Otten <__pete...@web.de> To: tutor@python.org Sent: Sunday, September 2, 2012 2:36 PM Subject: Re: [Tutor] making len() and __len__ return a non-int ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] making len() and __len__ return a non-int
On 09/02/2012 03:06 PM, Albert-Jan Roskam wrote: > Albert-Jan Roskam wrote: > If you're not going to put anything of your own into the reply message, please delete all the context you're not responding to, and then delete the message instead of sending. On the other hand, if the message actually had some content, please observe convention, and let us actually find it. Your reply should follow the parts you're quoting, and those parts should have attribution, and the > indentation at the beginning of the line should be consistent with whichever attribution it matches. New stuff should be at the left margin, without > symbol. In other words, use a decent email program, and use an before and after the stuff you type. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a script in the background
On Sep 1, 2012, at 11:29 PM, Michael Lewis wrote: > Hi everyone, > > I am sorry to ask this when there are a lot of resources online regarding the > subject, but I've spent the past two days trying to figure this out and I > don't get it. > > I have a script that will run forever. Since it runs forever, I don't want to > see the interpreter or command line. I want the program to run in the > background so I don't see it at all. > > How can I do this? For some background, my script essentially check every x > minutes to see if any files have been updated and then moves them to dropbox. > > I want to be able to do this on both OSX Mountain Lion and Windows 7. If need > be, I can create two separate scripts to separate out the two OS's. > > Thanks! > > -- > Michael J. Lewis Michael, I see you have several Windows answers, but it doesn't look as though you found quite what you were hoping for on OSX. My suggestion would be to take the script and run it through py2app, which will turn it into a stand-alone application which can then be added to your list of StartUp or LogIn applications. If you never request input or produce output, it will quite happily run in the background with no window and no menu (although by default there will be an icon in the dock). At that point it is so much a background application that the ONLY way you can quit it is via the Force Quit dialog. If you generate status or housekeeping print messages, they will show up in ~/Library/Logfiles. Py2app isn't particularly memory efficient, even a minimal application tends to run close to 9-10 Mbytes, but if it spends most of its time sleeping, it will use very little in the way of system resources. If you want it to run even when you aren't logged in, you will have to go to a bit more trouble. You will have to make up an installer plist file and use launchctl to add it to the list of stuff under control of the launchd daemon. Good luck, Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: Running a script in the background
On Sun, Sep 2, 2012 at 10:06 AM, Walter Prins wrote: > > nohup python myscript.py & > > Then you can close the terminal afterwards. "nohup" means"no hangup". > It tells the system that the python process launched as a result of > this command should not be terminated when its parent shell is > terminated. bash starts a background process in a new group. When you close the terminal (SIGHUP), the OS won't forward the HUP to this group, but bash defaults to forwarding it. If you "exit", on the other hand, bash won't be around to forward anything. To skip forwarding HUP in any case, just "disown" the process. If you use nohup, you can avoid creating nohup.out files if you redirect stdout somewhere such as a log file or /dev/null. If instead you just "exit" or use "disown", remember to redirect both stdout and stderr so the program doesn't try to write to a non-existent tty. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a script in the background
> > > > Michael, I see you have several Windows answers, but it doesn't look as > though you found quite what you were hoping for on OSX. My suggestion > would be to take the script and run it through py2app, which will turn it > into a stand-alone application which can then be added to your list of > StartUp or LogIn applications. If you never request input or produce > output, it will quite happily run in the background with no window and no > menu (although by default there will be an icon in the dock). At that > point it is so much a background application that the ONLY way you can quit > it is via the Force Quit dialog. If you generate status or housekeeping > print messages, they will show up in ~/Library/Logfiles. Py2app isn't > particularly memory efficient, even a minimal application tends to run > close to 9-10 Mbytes, but if it spends most of its time sleeping, it will > use very little in the way of system resources. > > Good luck, > Bill > Thanks, Bill. That is definitely more of what I am looking for and actually found that through some googling. What I am confused about now, is what's really the difference between py2app and the python Build Applet? -- Michael J. Lewis ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a script in the background
On Sep 2, 2012, at 5:06 PM, Michael Lewis wrote: > > > Michael, I see you have several Windows answers, but it doesn't look as > though you found quite what you were hoping for on OSX. My suggestion would > be to take the script and run it through py2app, which will turn it into a > stand-alone application which can then be added to your list of StartUp or > LogIn applications. If you never request input or produce output, it will > quite happily run in the background with no window and no menu (although by > default there will be an icon in the dock). At that point it is so much a > background application that the ONLY way you can quit it is via the Force > Quit dialog. If you generate status or housekeeping print messages, they > will show up in ~/Library/Logfiles. Py2app isn't particularly memory > efficient, even a minimal application tends to run close to 9-10 Mbytes, but > if it spends most of its time sleeping, it will use very little in the way of > system resources. > > Good luck, > Bill > > Thanks, Bill. That is definitely more of what I am looking for and actually > found that through some googling. What I am confused about now, is what's > really the difference between py2app and the python Build Applet? > -- > Michael J. Lewis I've never played with Build Applet, only py2app, so this may be partially or totally bogus, but py2app uses a fairly elaborate "setup" script that allows you to specify things like a custom icon (if you want one) as well as specific libraries. Build Applet, on the other hand, is a much simpler tool that takes a single source file, pulls in any "includes" it finds and builds a default. It might in fact be sufficient for your needs. If you've downloaded the python from python.org, you will definitely find both the Build Applet and PythonLauncher in a PythonXX folder in your Applications folder. Good luck, Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a script in the background
This is only tangentially related to the thread. Someone mentioned that so long as a script didn't require user input or output to the user, it could run silently in the background. But is there a way for a Python (2.7.3) script to determine whether it was called by the user or called by something like cron or kalarm? That way user inputs could be used when called by a user, but defaults could be used if run by a bot. Or is this more of a Linux question? Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a script in the background
On 02/09/12 23:14, Ray Jones wrote: could run silently in the background. But is there a way for a Python (2.7.3) script to determine whether it was called by the user or called by something like cron or kalarm? That way user inputs could be used when called by a user, but defaults could be used if run by a bot. Yes you can query the user via the os module.(getuid or getlogin for example) Or is this more of a Linux question? The details are likely to be implementation dependant, differing even between local hosts since it depends on how the admin has set up the user for cron etc hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a script in the background
On 09/02/2012 03:30 PM, Alan Gauld wrote: > On 02/09/12 23:14, Ray Jones wrote: >> could run silently in the background. But is there a way for a Python >> (2.7.3) script to determine whether it was called by the user or called >> by something like cron or kalarm? That way user inputs could be used >> when called by a user, but defaults could be used if run by a bot. > > Yes you can query the user via the os module.(getuid or getlogin for > example) > >> Or is this more of a Linux question? > > The details are likely to be implementation dependant, differing even > between local hosts since it depends on how the admin has set up the > user for cron etc > Thanks. I'll add this to my to-learn list (It's frustrating to come up with questions and answers faster than I can assimilate it all :-p) Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a script in the background
On Sun, Sep 02, 2012 at 03:14:53PM -0700, Ray Jones wrote: > This is only tangentially related to the thread. Someone mentioned that > so long as a script didn't require user input or output to the user, it > could run silently in the background. But is there a way for a Python > (2.7.3) script to determine whether it was called by the user or called > by something like cron or kalarm? That way user inputs could be used > when called by a user, but defaults could be used if run by a bot. The usual way to detect this is by checking whether or not there is a terminal available. os.isatty(sys.stdout.fileno()) If the script is running directly in a console, isatty will return True; if it is running from cron, or via a pipe or similar, then it will return False. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a script in the background (this time Cc'd to the list)
On Sep 2, 2012, at 6:15 PM, Alan Gauld wrote: > On 02/09/12 21:30, William R. Wing (Bill Wing) wrote: > >> My suggestion would be to take the script and run it through py2app, >> which will turn it into a stand-alone application which can then >> be added to your list of StartUp or LogIn applications. > > Why not just create a one line shell script that starts the python program > and add that to the Startup? What value does using py2app add in this case? > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ Apple's mechanism for launching applications at login is picky about what it will accept as a legitimate application to add to the list. You could get there by creating an "AppleScript" that did the same thing. AppleScripts can be saved as applications and an AppleScript can call a python script (I've actually done that, but it leaves you maintaining two bits of code, not just one). To the best of my knowledge, there is no way you can convince the application-picker to accept a shell script (or a raw python script) as an application. You can add it to the list, but at login, what happens is that the editor you used to create the script gets invoked with the script opened in it. -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a script in the background (this time Cc'd to the list)
On Sun, Sep 2, 2012 at 9:04 PM, William R. Wing (Bill Wing) wrote: > > Apple's mechanism for launching applications at login is picky > about what it will accept as a legitimate application to add to > the list. Here's an Ask Different (Apple Stack Exchange) answer with a template for a launchd plist: http://apple.stackexchange.com/a/822 I don't use OS X, so I can't offer anymore held than that. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a script in the background (offshoot - sorry, OP)
On 09/02/2012 06:03 PM, Steven D'Aprano wrote: > On Sun, Sep 02, 2012 at 03:14:53PM -0700, Ray Jones wrote: >> This is only tangentially related to the thread. Someone mentioned that >> so long as a script didn't require user input or output to the user, it >> could run silently in the background. But is there a way for a Python >> (2.7.3) script to determine whether it was called by the user or called >> by something like cron or kalarm? That way user inputs could be used >> when called by a user, but defaults could be used if run by a bot. > The usual way to detect this is by checking whether or not there is a > terminal available. > > os.isatty(sys.stdout.fileno()) > > If the script is running directly in a console, isatty will return True; > if it is running from cron, or via a pipe or similar, then it will > return False. Okay. Your solution works with cron - it does not work with kalarm (KDE's system alarm). The only reason I'm using kalarm rather than cron to begin with is that kalarm is TZ aware while anacron only looks at the system TZ (i.e. with kalarm I can start each task based on its own individual time zone). I read that fcron is fully TZ aware, but when I tried to install it, it wanted to automatically remove anacron and the entire kubuntu-desktop! Now this definitely gets into the Linux side of things. I'll go hit the Ubuntu forums and see what I can find. Thanks, everyone. Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor