Re: [Tutor] Launching a file browser
On Mar 31, 2005, at 01:56, Kent Johnson wrote: Mike Hall wrote: I looked over the global module index and the closest thing I could find relating to my os (osx) was EasyDialogs, which has a few functions pertaining to this, "AskFileForOpen()" being one. Calling any function within EasyDialogs however yields an Apple Event error: AE.AEInteractWithUser(5000) MacOS.Error: (-1713, 'no user interaction is allowed') It's been too long since I used Python on MacOSX, but IIRC you can't just run a Python GUI program from the shell. Or something like that...you should ask this one on the python-mac SIG mailing list: http://www.python.org/sigs/pythonmac-sig/ Kent You have to launch your script with pythonw, not with python. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a FIFO with fixed capacity?
On Wed, 30 Mar 2005, Marcus Goldfish wrote: > I need to implement a FIFO with a fixed maximum capacity. Hi Marcus, Out of curiosity, why do you require a first-in-first-out queue with a maximum capacity? > Would limiting the max capacity of the FIFO improve performance by > allowing one to preallocate the FIFO buffer? Possibly, but at the cost of having a FIFO that can get full. Depending on the domain, this might be ok for you. But most programs suffer from hardcoded limits that really shouldn't have been hardcoded in the first place. You may want to see if having a queue with unlimited size is really a performance drag on your system: have you done any profiling yet? The second implementation that you quoted earlier: > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/210459 is similar to a nicer one by Jeremy Fincher: ## class ListSubclassFifo(list): __slots__ = ('back',) def __init__(self): self.back = [] def enqueue(self, elt): self.back.append(elt) def dequeue(self): if self: return self.pop() else: self.back.reverse() self[:] = self.back self.back = [] return self.pop() ## (See: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/68436) Since these implementations guarantee O(1) "constant" time access for a queue, even without a hardcoded bound limit, you aren't losing much. Can you use this implementation? Finally, there's a 'deque' in the 'collections' Standard Library module that you might be able to use: http://www.python.org/doc/lib/module-collections.html which also should define constant-time guarantees for insertion and removal. So you might just be able to use that, and not have to copy-and-paste any code at all. *grin* Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Cryptography Toolkit
Does anyone have some examples on the use of A.M. Kuchling's Python Cryptography Toolkit? I've tried his examples but get "AttributeError" and "TypeError". What I'm trying to do is encrypt/decrypt a file. I'm using Python 2.3 on xp pro. Thanks -- _ ( ) Mark Thomas ASCII ribbon campaign X www.theswamp.org - against HTML email / \ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Cryptography Toolkit
Mark Thomas wrote: Does anyone have some examples on the use of A.M. Kuchling's Python Cryptography Toolkit? I've tried his examples but get "AttributeError" and "TypeError". What I'm trying to do is encrypt/decrypt a file. I'm using Python 2.3 on xp pro. If you post your code and the complete error message including the stack trace we may be able to help. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Cryptography Toolkit
On Thu, 31 Mar 2005 09:14:03 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote: > If you post your code and the complete error message including the stack > trace we may be able to help. > > Kent Thanks Ken I'm getting closer to making this work using the XOR cipher. Here's what I'm doing. from Crypto.Cipher import XOR obj_xor = XOR.new("string") str_encrypt = "encrypt this string" xored = obj_xor.encrypt(str_encrypt) xored '\x16\x1a\x11\x1b\x17\x17\x07T\x06\x01\x07\x14S\x07\x06\x1b\x07\t\x14' obj_xor.decrypt(xored) "bhxupds&oo`g'uou`z`" <== *confused by this output* Close but no cigar!! *grin* -- _ ( ) Mark Thomas ASCII ribbon campaign X www.theswamp.org - against HTML email / \ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Number of socketdescriptors > 250 and open() failed with to many open files
Hi! In a class derived from thread I open a socket-Connection to a remote Server. Here I start about 500 Thread's on a solaris-System. Sofar there is no problem, when the filedescriptorlimit is set high enough with ulimit -n. My Problem is, when a function in the class tries to open a regular file in the filesystem. Because of some limitations of the FILE-Structure ( in C ) it's not possible to open more files than 255. I think, that this is the problem here in python, when open() delivers "too many open files". In my C-Daemon I duplicated the socket-descriptors with fcntl to start after a offset with e.g. fcntl( socket, F_DUP, 100 ). And after this I use the new descriptor and close the old one. So there are everytime some descriptors below 255 ready for opening regular files. I tried the same thing in python. socket.fileno() delivers the descriptor of the opened socket. The fcntl-Call in the module fcntl does also succeed. But after this I need a socket-Object for working on with the new filedescriptor. Constructing a new socket-Object with socket.fromfd() does not work, because this takes a new descriptor at the same low offset. Is there any possibility to get a socket-Object which uses the allready obtained filedescriptor from fcntl()? Thanks Ewald ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Float precision untrustworthy~~~
> between binary and decimal representation. Just as a phrase that's > translated from English into Russian and then back to English again > can have its meaning shifted, Urban legend ,maybe but illustrates the point well: An early language translator app was fed 'Out of sight, out of mind' and then the result fed back in for reverse translation. The output was: 'Invisible, lunatic' :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class and methods?
> I am sorta starting to get it. So you could use __init__ to ask for a > file name to see if there is one in a folder or not if there is then > open that file and conitue where that file left off. If its not there > create a new file with that name, then start the program? Or do I have > that all wrong? There is nothing to stop you doing that, but in general init should not include interactive operations. It would be better to capture the filemname before creating the object and pass the filename into init as a parameter. Once inside init you can check if the filename is valid and either open the file or issue an error(raise an exception maybe?) or open a default filename instead, whatever you like. init is just a normal method except that it gets called first and is *intended* to initialise the internal attributes of the object. I'm not sure what you mean by "start the program", in general, after init completes, your program will continue from where you created the object. class C: def __init__(self, val): self.val = val print 'finished initialising' def main():# my main program... v = int(raw_input('Type a number> ')) c = C(v) # pass in the value for n in range(c.val): print 'hello there' main() So programme flow here is that we call main(), main calls C(v) which internally calls C.__Init__(c,v) We then return to main() to print out the hello messages and terminate HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am puzzled - help needed
> exercise. I need to rewrite the high_low.py program (see below) to use the > last two digits of time at that moment to be the "random number". This is > using the import time module. > > I just can't work out how to do that, I have been looking at it for the past > 2,5 hours but can't break it. Has anyone done it in order for me to study > it. > There are several ways, one is to convert the time number into a string and then extract the last two digits and convert back to a number. n = time.time() s = str(n) ran = s[-2:] # last two characters of the string ran = int(ran) # convert back to a number Another option is to divide by 100 and take the remainder... HTH, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a FIFO with fixed capacity?
Danny, Thanks for the informative response. After I sent the email I realized that a circular buffer is a FIFO with fixed capacity, and that is what I want to implement. I think I recall seeing a recipe in the Python Cookbook (1st). If you or anyone else know of other recipes/implementations please let me know so I can save on the cut-and-paste. :) Marcus ps -- as for the need for a circular buffer vs. FIFO: I think my dsp background pushed me toward the CB. My app involves data acquisition for extended periods of time. I can't grow the FIFO infinitely, but it is no big deal if a few samples get overwritten. Does this make sense? On Thu, 31 Mar 2005 01:19:24 -0800 (PST), Danny Yoo <[EMAIL PROTECTED]> wrote: > > > On Wed, 30 Mar 2005, Marcus Goldfish wrote: > > > I need to implement a FIFO with a fixed maximum capacity. > > Hi Marcus, > > Out of curiosity, why do you require a first-in-first-out queue with a > maximum capacity? > > > > Would limiting the max capacity of the FIFO improve performance by > > allowing one to preallocate the FIFO buffer? > > Possibly, but at the cost of having a FIFO that can get full. Depending > on the domain, this might be ok for you. But most programs suffer from > hardcoded limits that really shouldn't have been hardcoded in the first > place. You may want to see if having a queue with unlimited size is > really a performance drag on your system: have you done any profiling yet? > > The second implementation that you quoted earlier: > > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/210459 > > is similar to a nicer one by Jeremy Fincher: > > ## > class ListSubclassFifo(list): >__slots__ = ('back',) >def __init__(self): >self.back = [] >def enqueue(self, elt): >self.back.append(elt) >def dequeue(self): >if self: >return self.pop() >else: >self.back.reverse() >self[:] = self.back >self.back = [] >return self.pop() > ## > > (See: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/68436) > > Since these implementations guarantee O(1) "constant" time access for a > queue, even without a hardcoded bound limit, you aren't losing much. Can > you use this implementation? > > Finally, there's a 'deque' in the 'collections' Standard Library module > that you might be able to use: > >http://www.python.org/doc/lib/module-collections.html > > which also should define constant-time guarantees for insertion and > removal. So you might just be able to use that, and not have to > copy-and-paste any code at all. *grin* > > Best of wishes to you! > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am puzzled - help needed
Alan and John thanks for the help. I have now this bit of script but it is not running. -- from time import * n = time() s = str(n) numb = s[-2:] # last two characters of the string numb = int(numb) # convert back to a number guess = (raw_input('Enter a number: ')) if guess == numb: print ("Bravo, you have just won the right to play again!") elif guess < numb: print "You are just a bit too low, try again" else: print "You are just a bit too high, try again" print "The End" -- If i write the following line: "n = time.time() I get the following error message: --- Traceback (most recent call last): File "C:/Python24/Example/high_lowtest.py", line 3, in -toplevel- n = time.time() AttributeError: 'builtin_function_or_method' object has no attribute 'time' I feel like an idiot asking what is probably very basics questions but my desire to learn is quite high right now and I don't want to lose it, thanks again ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a FIFO with fixed capacity?
Marcus Goldfish wrote: Danny, Thanks for the informative response. After I sent the email I realized that a circular buffer is a FIFO with fixed capacity, and that is what I want to implement. I think I recall seeing a recipe in the Python Cookbook (1st). If you or anyone else know of other recipes/implementations please let me know so I can save on the cut-and-paste. :) Here is a simple ring buffer implemented on top of deque: from collections import deque class ring_buffer(deque): def __init__(self, capacity): deque.__init__(self) assert capacity > 0 self.capacity = capacity def append(self, x): while len(self) >= self.capacity: self.popleft() deque.append(self, x) rb = ring_buffer(3) for i in range(5): rb.append(i) print list(rb) Kent Marcus ps -- as for the need for a circular buffer vs. FIFO: I think my dsp background pushed me toward the CB. My app involves data acquisition for extended periods of time. I can't grow the FIFO infinitely, but it is no big deal if a few samples get overwritten. Does this make sense? On Thu, 31 Mar 2005 01:19:24 -0800 (PST), Danny Yoo <[EMAIL PROTECTED]> wrote: On Wed, 30 Mar 2005, Marcus Goldfish wrote: I need to implement a FIFO with a fixed maximum capacity. Hi Marcus, Out of curiosity, why do you require a first-in-first-out queue with a maximum capacity? Would limiting the max capacity of the FIFO improve performance by allowing one to preallocate the FIFO buffer? Possibly, but at the cost of having a FIFO that can get full. Depending on the domain, this might be ok for you. But most programs suffer from hardcoded limits that really shouldn't have been hardcoded in the first place. You may want to see if having a queue with unlimited size is really a performance drag on your system: have you done any profiling yet? The second implementation that you quoted earlier: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/210459 is similar to a nicer one by Jeremy Fincher: ## class ListSubclassFifo(list): __slots__ = ('back',) def __init__(self): self.back = [] def enqueue(self, elt): self.back.append(elt) def dequeue(self): if self: return self.pop() else: self.back.reverse() self[:] = self.back self.back = [] return self.pop() ## (See: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/68436) Since these implementations guarantee O(1) "constant" time access for a queue, even without a hardcoded bound limit, you aren't losing much. Can you use this implementation? Finally, there's a 'deque' in the 'collections' Standard Library module that you might be able to use: http://www.python.org/doc/lib/module-collections.html which also should define constant-time guarantees for insertion and removal. So you might just be able to use that, and not have to copy-and-paste any code at all. *grin* Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am puzzled - help needed
You are just a little confused about imports. If I >>> import time then the name 'time' is bound to the time module: >>> time The time() function is an attribute of the time module: >>> time.time >>> time.time() 1112296322.9560001 Alternatively, I can import the time function directly: >>> from time import time Now the name 'time' is bound to the time() function: >>> time >>> time() 1112296332.8610001 and time.time gives an error: >>> time.time Traceback (most recent call last): File "", line 1, in ? AttributeError: 'builtin_function_or_method' object has no attribute 'time' When you say 'from time import *' that means bring *all* the names from the time module into the current namespace. It's like saying from time import accept2dyear from time import altzone from time import asctime ... from time import time ... so then when you try to access time.time you get the AttributeError. Kent John Carmona wrote: Alan and John thanks for the help. I have now this bit of script but it is not running. -- from time import * n = time() s = str(n) numb = s[-2:] # last two characters of the string numb = int(numb) # convert back to a number guess = (raw_input('Enter a number: ')) if guess == numb: print ("Bravo, you have just won the right to play again!") elif guess < numb: print "You are just a bit too low, try again" else: print "You are just a bit too high, try again" print "The End" -- If i write the following line: "n = time.time() I get the following error message: --- Traceback (most recent call last): File "C:/Python24/Example/high_lowtest.py", line 3, in -toplevel- n = time.time() AttributeError: 'builtin_function_or_method' object has no attribute 'time' I feel like an idiot asking what is probably very basics questions but my desire to learn is quite high right now and I don't want to lose it, thanks again ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am puzzled - help needed
Thanks Kent I kind of see what you are trying to explain to me, it makes it easier by trying the different combination. So even is I change the first line to import time My script is still not working properly, I am obviously missing a statement somewhere, the script return: Enter a number: 25 You are just a bit too high, try again The End The script exits and don't give another try, could you enlight me in this one, thanks JC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am puzzled - help needed
> My script is still not working properly, I am obviously missing a > statement somewhere, the script return: > > >>> > Enter a number: 25 > You are just a bit too high, try again > The End > >>> > > The script exits and don't give another try, could you enlight me in > this one, thanks Hi John, What does your program look like now? Just copy-and-paste it in in your reply, and we can take a fresh look at it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A simple question about creating a program
> I was wondering, can you make a program the uses alot of classes do > the exact same thing with out useing classes? Yes you can always write a program without classes but it may be a lot more work and its likely to be a lot harder to maintain. Especially if its a big program. However if you use a language like Python its very difficult to write a program that doesn't use any classes because the built in types are clases, or objects. And many of the modules in the library have classes in them so you would need to reinvent all of that functionality yourself! Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a FIFO with fixed capacity?
> Would limiting the max capacity of the FIFO improve performance by > allowing one to preallocate the FIFO buffer? In a language like C it would help but in Python there's not much likliehood of advantage. BUt if you were writing a C module to integrate with Python then yes it might be an idea to allow the size to be set. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A simple question about creating a program
Just to be picky... > code to be executed by the processor. Machine language is not > object-oriented. In some cases it is. The Rekursiv computer by Linn systems had a CPU that had an OO machine language which supported parallelism by exposing 'threads' as active objects at the machine code level. But if we leave experimental architectures aside and deal with the 99.99% of mainstream you are of coure correct :-) > It's not even procedural, or anything. And again some chips have been built with Forth as their native machine code (or more accurately with a FOrth intetpreter as their microcode) and it is procedural. And again such chips, while available commercially never exactly made it to mainstream. And if Sun ever get round to finishing their JVM on a chip we'll have a chip that is both OO and procedural! > Any program can be written in any (Turing-complete) programming > language. And again thats only true for algorithmically biased programs, event driven code integrated with the external environment needs to be more than simply Turing complete. A Turing complete language may have no I/O capability or interface to OS or hardware events, thus making event driven code impossible. But again I'm being picky, it must be the time of night! Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a FIFO with fixed capacity?
> ps -- as for the need for a circular buffer vs. FIFO: I think my dsp > background pushed me toward the CB. My app involves data acquisition > for extended periods of time. I can't grow the FIFO infinitely, but it > is no big deal if a few samples get overwritten. Does this make sense? Hi Marcus, Let me make sure I understand. Let's imagine that we have such a CircularQueue, with methods: push(element) pop() isEmpty() Would the following test code capture how you expect the buffer to behave? ## Warning, untested code, since I don't have a circular queue ## implementation handy yet. *grin* Let's do some test-driven ## development. ## import unittest class TestCircularQueue(unittest.testCase): def testSimpleQueue(self): queue = CircularQueue(capacity=3) queue.push("a") queue.push("b") queue.push("c") self.assertEquals(False, queue.isEmpty()) self.assertEquals("a", queue.pop()) self.assertEquals("b", queue.pop()) self.assertEquals("c", queue.pop()) self.assertEquals(True, queue.isEmpty()) def testOverflowWrapsQueue(self): queue = CircularQueue(capacity=3) queue.push("a") queue.push("b") queue.push("c") queue.push("d") self.assertEquals("b", queue.pop()) self.assertEquals("c", queue.pop()) self.assertEquals("d", queue.pop()) self.assertEquals(True, queue.isEmpty()) ## I'm writing this as a unit test so that we can concretely see if my understanding of the behavior is the same at the one you expect to see. Does this match? Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a FIFO with fixed capacity?
> Let me make sure I understand. Let's imagine that we have such a > CircularQueue, with methods: > >push(element) >pop() >isEmpty() > > [example unittest code] Danny, Yes, it looks like that is a valid unittest for a circular buffer. An enhancement is to modify the accessors: push(element) --> push(sequence) pop() --> pop(N) If pushing caused an overwrite, the consumer could be notified, perhaps by an overwrite exception. Marcus ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Launching a file browser
On Mar 31, 2005, at 12:21 AM, Max Noel wrote: It's been too long since I used Python on MacOSX, but IIRC you can't just run a Python GUI program from the shell. Or something like that...you should ask this one on the python-mac SIG mailing list: http://www.python.org/sigs/pythonmac-sig/ Kent You have to launch your script with pythonw, not with python. I'm unclear on why a command like webbrowser.open() will comfortably launch your default web browser (in my case Safari), but something as ubiquitous to an OS as a file browser has special needs to launch. Perhaps each application has custom written their file browser, and I'm assuming they are each essentially doing system calls to the same thing...? -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am puzzled - help needed
I need to rewrite the high_low.py program (see below) to use the last two digits of time at that moment to be the "random number". Be sure you understand what format the time number has and that you understand the problem statement. Here are two time values: 1112306463.0 1112306463.01 Do you see what is going to happen if you blindly take the last two characters of the time in the first case and try to convert it to an integer? Are you suppose to take the last two digits of the integer portion? Also, a neat way to get the last 2 digits of a number is to use the "remainder/modulus" operator: ### >>> from time import * >>> t=int(time()) >>> t 1112306749 >>> t%10 #last 1 9 >>> t%100 #last 2 49 >>> t%1000 #last 3 749 ### Also, note that when you "import *" you no longer need (or can) use the "time." prefix for times functions; when you import them that way it is as if they are part of your own function set: ### >>> from time import * >>> time.time() Traceback (most recent call last): File "", line 1, in ? AttributeError: 'builtin_function_or_method' object has no attribute 'time' ### ..you get this error because time's function "time()" is part of your programs functions; there is not module "time" for you to interact with, only time's functions. So go ahead and just use the function: ### >>> time() 1112306900.8461709 ### If you import the whole module, though, and try the same thing... ### >>> import time >>> time() Traceback (most recent call last): File "", line 1, in ? TypeError: 'module' object is not callable ### python knows about the *module* time but you are now treating the module like a function. Instead, you should access the function through the module's name: ### >>> time.time() 1112306919.518116 ### /c ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] test
test! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am puzzled - help needed
C Smith, Danny and Kent, thanks for the replies. Here is my programme (I have reinstated the "while loop" C Smith I like very much your method to get the last digits of a number. It is WORKING NOW!! You can imagine how long I have spent on that, but I have learnt so much. Many thanks to all the people that have helped me, you will probably see me around asking a zillion other (very basics) questions. from time import * b = int(time()) b%100 running = True while running: guess = int(raw_input ("Guess a number: ")) if guess == b%100: print "Bravo, you guessed the right number" elif guess > b%100: print "Too high" else: print "Too low" else: print "just right" print "Done" ___ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A simple question about creating a program
On Mar 31, 2005, at 23:07, Alan Gauld wrote: And if Sun ever get round to finishing their JVM on a chip we'll have a chip that is both OO and procedural! At that point it would be a JRM, then, wouldn't it? :D -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Launching a file browser
On Apr 1, 2005, at 00:14, Mike Hall wrote: On Mar 31, 2005, at 12:21 AM, Max Noel wrote: It's been too long since I used Python on MacOSX, but IIRC you can't just run a Python GUI program from the shell. Or something like that...you should ask this one on the python-mac SIG mailing list: http://www.python.org/sigs/pythonmac-sig/ Kent You have to launch your script with pythonw, not with python. I'm unclear on why a command like webbrowser.open() will comfortably launch your default web browser (in my case Safari), but something as ubiquitous to an OS as a file browser has special needs to launch. Perhaps each application has custom written their file browser, and I'm assuming they are each essentially doing system calls to the same thing...? No, the reason for that, IIRC, is that for the program to be able to interact with the window manager, it has to be launched with pythonw. When the program starts to display stuff elsewhere than in STDOUT or STDERR, an application launch is somehow triggered (icon appears in the Dock), which for some reason enables the user to interact with the program. Launching a web browser requires no interaction whatsoever with the WM, and can therefore be done with python. Yes, the python/pythonw distinction in Mac OS X is stupid, I'll give you that. I don't even know why it exists in the first place. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A Newbie Printing Question
1) For plain text use the old DOS trick of sending output direct to the PRN: file/device - I can't remember if this still works in XP but I can't think why not... The only reason I can think of is that Windows XP is not directly based on DOS, wereas the other versions were. In so doing, they have lost a lot of compatibility with many things. Old DOS games no longer work, some DOS commands do not work in their mock command prompt, etc. Jacob ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A Newbie Printing Question
Cool! Does anybody know of... I guess a rather *thorough* tutorial of win32? for the very reason that I don't know that this existed, and there may be other things I can use that I'm missing... TIA, Jacob Richard Lyons wrote: I have little experience with programming. I have Python installed on a Windows XP system. What code do I need to use to send output from a Python script to a local printer attached to my workstation? to a network printer? The win32print module (in Matt Hammond's windows modules) may help. eg: pid = win32print.OpenPrinter(win32print.GetDefaultPrinter()) win32print.StartDocPrinter(pid, 1, ('My print job', None, 'raw')) win32print.WritePrinter(pid, s) win32print.EndDocPrinter(pid) win32print.ClosePrinter(pid) When I did this, I was printing to a postscript printer, and I wasn't printing anything that required complex layout. So I generated the postscript code myself, and then just sent it straight to the printer... -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Float precision untrustworthy~~~
I understand what you are talking about, but I tend toward just making it one of the things to remember when working with floats. (I've been bitten a lot when I forget to use '==' instead of '=', too!) Yeah, but it threw me for a loop, because I could find *no*e way to compare a float and an integer and get it to come out right before I remembered round. As you say, the problems arise when you try to make comparisons. To get around the difficulties with comparisons, I wrote a helper function: ### Python 2.3 (#2, Jul 30 2003, 11:45:28) [GCC 3.1 20020420 (prerelease)] Type "copyright", "credits" or "license" for more information. MacPython IDE 1.0.1 >>> from math import floor, log10 >>> def Round(x, n=0, sigfigs=False): '''An enhanced rounder which rounds to the nth significant digit (the nth digit from the first non-zero digit, not the nth decimal place) if the parameter sigfigs is not zero. This uses "round-up" decisions when the digit following the one of interest is a 5, i.e. 1.35 and 1.45 Round to 1.4 and 1.5 when sigfigs = 2. ''' if x==0: return 0 if not sigfigs: return round(x,n) else: return round(x,n-1-int(floor(log10(abs(x) ### And here's a helper that uses it to check for equality of two numbers (floats, or otherwise) to whatever number of digits you desire --starting from the first non-zero digit, The default is for high precision. ### >>> def same(x,y,prec=9): """Return True if x and y are the same after being rounded to the same degree of precision.""" return Round(x,prec,1)==Round(y,prec,1) .. >>> print same(1.3,1.31,3) #at the 3rd digit these are different False >>> print same(1.3,1.31,2) #at the second digit they are (rounded to) the same digit number True >>> same(64.0**(1/3.0) ,4) #here's your example True ### You can be bitten by any comparison, not only tests for equality, too. ### >>> while 1: .. i+=.1 .. if i>2.1:break .. >>> print i 2.1 ### It doesn't look like the last value of i was really greater than 2.1--it's actually 2.1005 so the result is correct (though not what we expected). We don't have the problem with a step size of 0.7 in this case, though, because the value before 2.8 was 2.0996--just a little smaller than 2.1. Here, we specify the precision and get the desired result. ### >>> i=0 >>> while 1: .. i+=.1 .. if round(i,9)>round(2.1,9):break .. >>> print i 2.2 ### Wish granted in version 2.4 ;-) I don't have it on the mac, but there is a write-up at I meant in the scripts -- even better, as a builtin. Your above helper function is fine... but I believe that it should be part of the __cmp__ method of the float types. Perhaps with optional parameter? Such as oldway=True Jacob Schmidt ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Float precision untrustworthy~~~
An early language translator app was fed 'Out of sight, out of mind' and then the result fed back in for reverse translation. The output was: 'Invisible, lunatic' Cute, Alan. I like it! Jeff - thanks for the insight. I guess I think more in theory than in reality sometimes. Kent - thanks for your help. Okay, I guess it works. I'll just have to indirectly call str method instead of repr or something. For all of you, I had a stroke of genius one day and wrote my own (limited) rational class and called it Fract (fraction) --Jacob ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A Newbie Printing Question
Quoting "Jacob S." <[EMAIL PROTECTED]>: > Cool! Does anybody know of... I guess a rather *thorough* tutorial of > win32? for the very reason that I don't know that this existed, and there may > be other things I can use that I'm missing... I don't know of anything online ... It seems a very poorly-documented corner of Python. Oreilly have a book which may be of help to you if you do a lot of programming on Windows: http://www.oreilly.com/catalog/pythonwin32/ The only other thing I can suggest is try running pydoc on the win32 modules and looking at the docstrings. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Launching a file browser
Ah, so it has to do with access to the window manager. That answers a lot, thanks. On Mar 31, 2005, at 4:09 PM, Max Noel wrote: On Apr 1, 2005, at 00:14, Mike Hall wrote: On Mar 31, 2005, at 12:21 AM, Max Noel wrote: It's been too long since I used Python on MacOSX, but IIRC you can't just run a Python GUI program from the shell. Or something like that...you should ask this one on the python-mac SIG mailing list: http://www.python.org/sigs/pythonmac-sig/ Kent You have to launch your script with pythonw, not with python. I'm unclear on why a command like webbrowser.open() will comfortably launch your default web browser (in my case Safari), but something as ubiquitous to an OS as a file browser has special needs to launch. Perhaps each application has custom written their file browser, and I'm assuming they are each essentially doing system calls to the same thing...? No, the reason for that, IIRC, is that for the program to be able to interact with the window manager, it has to be launched with pythonw. When the program starts to display stuff elsewhere than in STDOUT or STDERR, an application launch is somehow triggered (icon appears in the Dock), which for some reason enables the user to interact with the program. Launching a web browser requires no interaction whatsoever with the WM, and can therefore be done with python. Yes, the python/pythonw distinction in Mac OS X is stupid, I'll give you that. I don't even know why it exists in the first place. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting more than one list
An alternative way of doing this (if you have python 2.4): >>> ppl = ['john', 'mary', 'lary', 'jane']>>> age = [15, 30, 23, 25]>>> height= [160, 165, 178, 170]>>> sortby = lambda a, b: [a[b.index(x)] for x in sorted(b)]>>> sortby(ppl, age)['john', 'lary', 'jane', 'mary']>>> sortby(ppl, height)['john', 'mary', 'jane', 'lary'] >>> age # age is not changed[15, 30, 23, 25] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A Newbie Printing Question
First off, print stills works from an XP cmd.exe, but only for LPT printers, not USB. Secondly, Win32's methods are well documented, using them isn't. There are some tutorials included with the download, and you get a chm help file filled with the objects and methods, but as far as tutorials go, buy Mark Hammond's book if you want the full picture. For what it's worth, PyWin32 is one of the simplest interfaces to the Windows API / COM I've seen, so if you intend to do a lot of XP stuff, you'll need it. Oh yeah, and there's bugger all docstrings. Regards, Liam Clarke On Fri, 01 Apr 2005 12:53:07 +1200 (NZST), [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Quoting "Jacob S." <[EMAIL PROTECTED]>: > > > Cool! Does anybody know of... I guess a rather *thorough* tutorial of > > win32? for the very reason that I don't know that this existed, and there > > may > > be other things I can use that I'm missing... > > I don't know of anything online ... It seems a very poorly-documented corner > of > Python. > > Oreilly have a book which may be of help to you if you do a lot of programming > on Windows: http://www.oreilly.com/catalog/pythonwin32/ > > The only other thing I can suggest is try running pydoc on the win32 modules > and > looking at the docstrings. > > -- > John. > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am puzzled - help needed
John Carmona wrote: It is WORKING NOW!! You can imagine how long I have spent on that, but I have learnt so much. Many thanks to all the people that have helped me, you will probably see me around asking a zillion other (very basics) questions. Congratulations! I have one^H^H^Htwo small notes below. from time import * b = int(time()) b%100 If you say b = b%100 then you remember the target number and you don't have to keep writing b%100, just use b. running = True while running: guess = int(raw_input ("Guess a number: ")) if guess == b%100: print "Bravo, you guessed the right number" elif guess > b%100: print "Too high" else: print "Too low" else: print "just right" print "Done" OK one more note, I don't see how you will ever get out of the loop, you never set running to False or break. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting more than one list
What's b.index(x) do? I'm guessing the for a list Delta = ["a,"b","c"], you get Delta.index("b") 1 Am I right? On Apr 1, 2005 1:16 PM, py <[EMAIL PROTECTED]> wrote: > > > An alternative way of doing this (if you have python 2.4): > > >>> ppl = ['john', 'mary', 'lary', 'jane'] > >>> age = [15, 30, 23, 25] > >>> height= [160, 165, 178, 170] > > >>> sortby = lambda a, b: [a[b.index(x)] for x in sorted(b)] > >>> sortby(ppl, age) > ['john', 'lary', 'jane', 'mary'] > >>> sortby(ppl, height) > ['john', 'mary', 'jane', 'lary'] > > >>> age# age is not changed > [15, 30, 23, 25] > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Sorting more than one list
> What's b.index(x) do? > > I'm guessing the for a list Delta = ["a,"b","c"], you get > > Delta.index("b") > > 1 > > Am I right? Yes. For future use, the easiest way to answer a question like that is to do: >>> help([].index) Help on built-in function index: index(...) L.index(value, [start, [stop]]) -> integer -- return first index of value Or, if you have an idea, just try it out: >>> Delta = ["a","b","c"] >>> Delta.index("b") 1 =Tony.Meyer ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor