Re: [Tutor] list packing
John Fouhy wrote: > On 27/02/06, kevin parks <[EMAIL PROTECTED]> wrote: > >>snd = [f for f in os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] > > > If this is all you need, then you could do something like: > > snd = ['/Users/kevin/snd/%s' % f for f in > os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] > > Or, slightly more robustly (?), > > snd = [os.path.join('/Users/kevin/snd', f) for f in > os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] > os.path.join() is self-documenting. I find this to be a better reason to use it than anything else. But then my code only ever runs on Unix of some flavor. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list packing
On 27/02/06, kevin parks <[EMAIL PROTECTED]> wrote: > snd = [f for f in os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] If this is all you need, then you could do something like: snd = ['/Users/kevin/snd/%s' % f for f in os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] Or, slightly more robustly (?), snd = [os.path.join('/Users/kevin/snd', f) for f in os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] list packing
howdy, I am using the os module to do some of my heavy lifting for me. I am tried of building lists by hand so i decided that i would get python to look in a bunch of directories and stuff all the things it find there into a list depending on it's extension. Works great ... one problem sometimes i need just the filenames and that is fine, but more often i need to feed the full path to my other functions yet i don't see *any* documentation on os.listdir() at all. I don't know how to give me the full path ... snd = [f for f in os.listdir('/Users/kevin/snd/') if f.endswith('.aif')] cheers, kevin ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New Tutorial topic available
On 2/26/06, Alan Gauld <[EMAIL PROTECTED]> wrote: > I've just uploaded the latest tutorial topic covering inter-process > communications. It covers pipes and the use of fork(). > > Enjoy, as ever feedback is welcomed. Thanks Alan, your pages are a great source of information for us newbies. -- _ ( ) 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] Problem with threads
David Cohen wrote: > Hi, > > Here is the code attached. It is a simple test program that open a gtk > window and connect to a server (might be an irc or telnet server). > When the button "Connect" is clicked, the thread (should) starts and > read what is coming from the server. But it never happens... just when > I click on the "Disconnect" button and the join() function is called. > Did you get any problem like that? Ok. I've never worked with gtk, but in GUI frameworks it can be a bad idea to change the gui from a thread other than the event thread, which is what you are doing. My guess is that the socket thread is running but the gui is not updating until you click disconnect and change the gui from the event thread. The first thing I would try is, put a print in GetSockData.run() to see if it is doing anything (my guess is you will see the print). If that works, then you need to learn how to make threads and gtk play together. Here is a tutorial that might help - the first example is pretty close to what you are doing: http://www.pardon-sleeuwaegen.be/antoon/python/page0.html This FAQ entry (from Google's cache, the main site is not responding) shows another way: http://64.233.179.104/search?q=cache:fVAyfmzgtFoJ:www.async.com.br/faq/pygtk/index.py%3Freq%3Dshow%26file%3Dfaq20.006.htp+pygtk+thread&hl=en&gl=us&ct=clnk&cd=7&client=firefox-a HTH, Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with threads
Hi, Here is the code attached. It is a simple test program that open a gtk window and connect to a server (might be an irc or telnet server). When the button "Connect" is clicked, the thread (should) starts and read what is coming from the server. But it never happens... just when I click on the "Disconnect" button and the join() function is called. Did you get any problem like that? BR, David On 2/25/06, Kent Johnson <[EMAIL PROTECTED]> wrote: > David Cohen wrote: > > Hi all, > > > > I have a problem trying to use thread on python. > > I import the threading module and execute this: > > > > def func_thread(): > > something... > > > > new_thread = threading.Thread(target = func_thread) > > new_thread.start() > > > > But the thread never really starts, just when I call the > > new_thread.join() method. > > Does anybody could help me? > > More code would help. What you have shown looks fine. What does > func_thread() do? How do you know it isn't doing it? Here is a very > simple, working example based on your code: > > import threading, time > > def func_thread(): > while True: > print 'threading' > time.sleep(1) > > new_thread = threading.Thread(target = func_thread) > new_thread.start() > > print "main thread going away now" > > Kent > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > #!/usr/bin/python2.4 import pygtk pygtk.require('2.0') import gtk import socket import threading class GetSockData(threading.Thread): def __init__(self, sock, tview): threading.Thread.__init__(self) self.sock = sock self.tview = tview self.i_am_alive = True def run(self): buf = self.tview.get_buffer() while self.i_am_alive: msg = self.sock.recv(1024) buf.insert_at_cursor(msg) def stop(self): self.i_am_alive = False class MainWindow: def delete_event(self, widget, event, data=None): print "delete event occurred" return False def destroy(self, widget, data=None): if self.is_connected: self.sock.close() self.getsock.stop() self.getsock.join() self.is_connected = False gtk.main_quit() def text_sent_event(self, entry): text = entry.get_text() buf = self.tview_main.get_buffer() if self.is_connected: self.sock.send(text + "\r\n") else: buf.insert_at_cursor("Your are not connected!\n") buf.insert_at_cursor(text + "\n") entry.set_text("") def clicked_disconnect_event(self, button): self.sock.close() self.is_connected = False self.tview_main.get_buffer().insert_at_cursor("Disconnected\n") self.getsock.stop() self.getsock.join() def clicked_connect_event(self, button): if self.is_connected: print "Client already connected!\n" return self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect(("localhost", 6667)) self.is_connected = True self.tview_main.get_buffer().insert_at_cursor("Connected!\n") self.getsock = GetSockData(self.sock, self.tview_main) self.getsock.start() def __init__(self): window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.connect("delete_event", self.delete_event) window.connect("destroy", self.destroy) window.set_border_width(5) window.set_default_size(640, 480) window.set_title("Test Socket") window.set_resizable(True) self.is_connected = False vbox = gtk.VBox(False, 5) window.add(vbox) hbox = gtk.HBox(True, 5) vbox.pack_start(hbox, False, False) btn_connect = gtk.Button("Connect") hbox.pack_start(btn_connect, True, False) btn_connect.connect("clicked", self.clicked_connect_event) btn_disconnect = gtk.Button("Disconnect") hbox.pack_start(btn_disconnect, True, False) btn_disconnect.connect("clicked", self.clicked_disconnect_event) vbox.pack_start(gtk.HSeparator(), False, False) ws_main = gtk.ScrolledWindow() ws_main.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) ws_main.set_placement(gtk.CORNER_BOTTOM_LEFT) tbuf = gtk.TextBuffer() self.tview_main = gtk.TextView(tbuf) self.tview_main.set_editable(False) ws_main.add_with_viewport(self.tview_main) vbox.pack_start(ws_main, True, True) self.entry_cmd = gtk.Entry() self.entry_cmd.connect("activate", self.text_sent_event) self.entry_cmd.grab_focus() vbox.pack_start(self.entry_cmd, False, False) list_focus = [ self.entry_cmd ] vbox.set_focus_chain(list_focus) window.show_all() def main(self): gtk.main() if __name__ == "__main__": window = MainWindow() window.main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] New Tutorial topic available
I've just uploaded the latest tutorial topic covering inter-process communications. It covers pipes and the use of fork(). Enjoy, as ever feedback is welcomed. 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] software carpentry
> But the Software Carpentry Workshop was a revelation. It gives a great > deal of advice based on empirical studies on effective programming > practices. I´m partway through - it's a multiweek course. University > lecturer Greg Wilson offers thorough notes to accompany the .mp3 lectures, > all of which is downloadable: > > www.third-bit.com/swc/ I haven't come across this befoire, thanks for the link. > convincingly argues that certain things which require more time up front > save a great deal of time down the road. He stresses using a version > control system, a debugger, test-driven development, and reading lots of > code. I'd agree with all of those, although for the debugger I'd substitute the interactive prompt in Pythons case. I certaimly find it to be a very powerful tool for finding out exactly what a library function does for example. > 1) Is subversion really a good tool for the beginning programmmer, or a > single programmer? If not which version control system would you > recommend? I haven't used SV but it gets good crits. For solo use I tend to use the ancient but simple RCS. For group projects I often use CVS but I'm less keen on it. Professionally I've used lots of tools and the best by far is ClearCase, with almost magical version management capability, but unless you have very deep pockets or, nore realistically, access to a corporately purchased version, its probably unnattainable. > 2) Which debugger should I use if I do most of my work in XP with IDLE? Up > til now I've just been using print statements. who knew? The IDLE debugger is good enough because in Python you rarely need to dive that deep. Coupled to the interactive prompt and a few print statements you shouldn't need anything more. > for (well-written) code? Is it a good idea to look in the standard > libraries for ideas, or code to modify? Thats a tougher one. The code in the libraries is good quality but it is library code and as such is not necessarily representative of how you should write application code. Libraries are designed to be both highly efficient and quite generic. These are normally mutually exclusive qualities To achieve both requires some fairly clever programming tricks and some compromises. If you were to write your own code the wayt libraries get written you would probably wind up writiong a lot more code than was necessary and possibly implementing premature efficiencies which compromise maintainability. So in summary: by all means read the library cpode but don;t necesarily use it as a model for your own. Build the code you need, taking account of good library practice around exception handing etc, but do not over optimise it nor over genericise it. Do the simplest things first, add complexity as you need it. If it becomes a frequent source of change thats the time to consider revisiting the design and adding more generic capability or fine tuning the time critical parts. > How are large applications usually organized, and how do you find > a specific part of it? Using component or subsystyems or modules - different terms that all encpompass the same concept of divide and conquer. Python has very good support for modules at the file level and packages at the next level up. And finally you can create whole collaborations of processes as happens in Web Application Frameworks. Component Diagrams combined with Deployment diagrams in UML are a good way of documenting these relationships should you need to. A basic example can be seen here: http://www.smartdraw.com/examples/software-uml/package_diagram_encapsulation.htm And for an 8 page short intro to UML: http://www-128.ibm.com/developerworks/rational/library/769.html#N1014C And in full detail in the UML specification (PDF format) here: http://www.omg.org/cgi-bin/doc?ptc/2004-10-14 REQUEST FOR FEEDBACK As a general question. I've been toying with the idea of doing a tutorial topic on design & documentation using UML. Focussing on the aspects of UML that I believe are genuinely useful for beginner porogrammers. Is there any demand for that kind of stuff in my tutorial? Feedback is welcomed. ++ > How can you find code specific to certain tasks? Google is your friend... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Grepping a file for words in a list
"John Purser" <[EMAIL PROTECTED]> wrote in message > I'm writing a system admin script in python that checks recently > accessed files for keywords like "failed, denied, error,..." etc. I'm > using popen to call grep -F but it's VERY slow. Can > anyone suggest a faster method to do this? are you trying to locate the actual strings in the files or only testing to see if the strings occur? If the latter then you could use something like files = [f for f in glob(pattern) if mystring in open(f).read()] or for multiple patterns construct a single regex: files = [f for f in glob(pattern) if regex.search(open(f).read())] That might be slightly faster than spawning sub processes with popen. But it won't be a radical difference and if you actually want the line numbers of the patterns I suspect grep will be at least as fast as using python. 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] Problem with threads
David Cohen wrote: > Hi all, > > I have a problem trying to use thread on python. > I import the threading module and execute this: > > def func_thread(): > something... > > new_thread = threading.Thread(target = func_thread) > new_thread.start() > > But the thread never really starts, just when I call the > new_thread.join() method. > Does anybody could help me? More code would help. What you have shown looks fine. What does func_thread() do? How do you know it isn't doing it? Here is a very simple, working example based on your code: import threading, time def func_thread(): while True: print 'threading' time.sleep(1) new_thread = threading.Thread(target = func_thread) new_thread.start() print "main thread going away now" Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor