Re: [Tutor] monitor number of files in a folder
On Aug 7, 2009, at 9:29 AM, pedro wrote: On my machine (a Mac), os.listdir does include files that begin with "." Having the while loop timeout after 10 or 20 times through as was suggested a couple posts back will work fine for my particular situation. Thanks Yes, it does include files that begin with '.', but it does not include the entries '.' and '..'. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] More type() puzzlement
On Oct 27, 2007, at 12:52 PM, Dick Moores wrote: > At 08:39 AM 10/27/2007, Aditya Lal wrote: >> Hence if type(n) is already long it does not have to get converted >> to int to accommodate something small. > > And that's not a bug? There is no need for a type change to represent zero, so, no, that's not a bug. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Turning multiple Popen calls into a function
On Nov 1, 2007, at 3:04 PM, Alan Gauld wrote: > Where does the if/else syntax come from? It doesn't > seem to work on my Python 2.4. Is it a new feature in 2.5? Yes, it's a new feature in 2.5. - Jeff Younker - [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Automating operations... os module, mysql operations, more...
On Nov 1, 2007, at 12:15 PM, John wrote: 1) Maintain a mysql database which contains information, as well as 'control' variables, pointers to directories, etc. for various shell scripts / fortran code. ->pyMySQL Look at an object relational mapper like SQLObject or SQLAlchemy. They make the talking to the database much easier. 6) Have a portion of the information available to a plone site for dynamic html generation. ->ZMySQLDBConn ->Z SQL Methods Zope might be overkill. Something like Turbogears might be better if you're just looking for reporting. (It will do a lot more, but it's really easy to get started to with turbogears.) - Jeff Younker - [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] assignment question
Append modifies the array as a side effect. It has no meaningful return value. >>> x = [1, 2, 3] >>> x.append(4) >>> print x [1, 2, 3, 4] - Jeff Younker - [EMAIL PROTECTED] - 510.798.5804 - On Nov 11, 2007, at 8:21 PM, Ryan Hughes wrote: Hello, Why does the following not return [1,2,3,4] ? >>> x = [1,2,3].append(4) >>> print x None ___ 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] adding a new folder to Python's importable modules search path
> so when I run my script from the command line, the folder does seem > to be added to Python's importable modules search path. When I > attempt to import my_module I get the error: > > ImportError: No module named my_module Do your module directories have an __init__.py file in them? - Jeff Younker - [EMAIL PROTECTED] - On Nov 25, 2007, at 9:00 PM, [EMAIL PROTECTED] wrote: ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Questions about wxEvents
While I can't help you with the wx specific part of the question, I can offer help with the reuse question. Here is one approach. Add an controller identifier to fpCallback. Then use lambda expressions for the actual callback, where the lambda expressions pass in a fixed value for the identifier. Modified code: self.fp1 = filebrowse.FileBrowseButtonWithHistory(pnl, -1, size=(300, -1), labelText='', fileMask='*.*', fileMode=wx.OPEN, dialogTitle='Select the file containing UCF claims', changeCallback= lambda x: self.fpCallback('fp1', x) ) self.fp2 = filebrowse.FileBrowseButtonWithHistory(pnl, -1, size=(300, -1), labelText='', fileMask='FI*.*', fileMode=wx.OPEN, dialogTitle='Select the form-image file - generally starts with FI', changeCallback= lambda x: self.fpCallback('fp2', x) ) [snip] def fpCallback(self, controlID, evt): print evt.__dict__ print help(evt) value = evt.GetString() [snip] - Jeff Younker - [EMAIL PROTECTED] - On Dec 6, 2007, at 9:12 AM, Marc Tompkins wrote: I have a specific question - how can I generalize a FileBrowseButtonWithHistory - and I realized, as I was trying to word my question, that my real question is a bit more generic. First, the specific question: The FileBrowseButtonWithHistory requires a callback override and some custom code to straighten out handling the history. So far, so good. However, I wish to use more than one FBBWH on my form, and I can't figure out how to re-use the custom callback so it will work for both controls. (It makes me sick to my stomach when I look at my code and see duplicate blocks!) Don't get me wrong - it's working right now, it's just that my code is fugly and I want to clean it up. In more general terms, how can I set more than one control to use the same block of code as a custom callback, and figure out at runtime which control I'm responding to? Doesn't the Event or CommandEvent carry any information about itself? I've tried this: [snip] self.fp1 = filebrowse.FileBrowseButtonWithHistory(pnl, -1, size=(300, -1), labelText='', fileMask='*.*', fileMode=wx.OPEN, dialogTitle='Select the file containing UCF claims', changeCallback= self.fp1Callback) self.fp2 = filebrowse.FileBrowseButtonWithHistory(pnl, -1, size=(300, -1), labelText='', fileMask='FI*.*', fileMode=wx.OPEN, dialogTitle='Select the form-image file - generally starts with FI', changeCallback= self.fp2Callback) [snip] def fp1Callback(self, evt): print evt.__dict__ print help(evt) value = evt.GetString() [snip] def fp2Callback(self, evt): print evt.__dict__ print help(evt) value = evt.GetString() [snip] All I get from "print evt.__dict__" is: {'_string': u'E:\\ultrahld\ \report\\FILE'} and all I get from help is: Help on instance of LocalEvent in module wx.lib.filebrowsebutton object:class instance(object) | instance(class[, dict]) | | Create an instance without calling its __init__() method. | The class must be a classic class. | If present, dict must be a dictionary or None. | | Methods defined here: | | __abs__(...) | x.__abs__() <==> abs(x) | ... in other words, I might as well have typed "help(object)". I only know that the method "evt.GetString()" exists because the example in the demo uses it. I've Googled, and Gmaned, and read the wx docs (such as they are), but I'm not seeing anything I can use. I know that this is a wxPython question, and that this is the Python list... but y'all have answered some wx questions in the past, and I hate to join another list for one question. If I must, I will... but thanks in advance for any light you can shed. -- www.fsrtechnologies.com ___ 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] logic for a tree like structure
Pipes and IO channels are buffered. The buffers are much larger than the amount of information you are writing to them, so they're never getting flushed while the program is running. The child program completes, the IO channel closes, and it flushes out the output. My advice is to forget about all the file descriptor and pipes stuff. Getting incremental IO from them via the subprocess module (or any standard IO module) is painful. You're probably better off getting the nonstandard pexpect module and using that instead. Here's your program using pexpect. #/usr/bin/python2.5 import pexpect def main(): cmd = pexpect.spawn('./writer.py') try: while True: # wait for the end of the line or ten seconds cmd.expect('\r\n', timeout=10) # get output preceding the EOF matched above line = cmd.before print '[main]', line except pexpect.TIMEOUT: print "No output received for ten seconds" except pexpect.EOF: pass finally: cmd.close() if __name__ == '__main__': main() - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Editor For Mac Os X
On Dec 13, 2007, at 8:32 PM, chinni wrote: > Hi which editor for python on Mac os x platform is best with color > syntax compiler etc... I'm pretty happy with Eclipse + Pydev. - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Program review
The procesar function is complicated. It tries to do several things that that are better done elsewhere. It needs to be broken up into several functions and methods. This also clarifies and isolates the exception handling. > def procesar(mensaje): >try : The enclosing try block isn't needed. More on this later. > >try : >fIncl = open(mensaje.direcciones) >except Exception, e : >logging.error('Error!!! No pude abrir "%s" : %s', >mensaje.direcciones, >e.strerror) >raise This is one function. It has the argument filename. > >try : >fExcl = open(mensaje.excluidas) >except Exception, e : >logging.error('No pude abrir "%s" : %s', >mensaje.excluidas, >e.strerror) >fIncl.close() >raise This is the same function with a different argument. > >except : pass >else : The outer exception block here does nothing. If an exception is raised then the following code won't be executed anyway. >mails = enumerate( >set(addr.strip() for addr in fIncl) >- set(excl.strip() for excl in fExcl)) >fIncl.close() >fExcl.close() This is a method that gets the addresses from the files specified in mensaje. It opens the files. It reads the contents. It closes the files. It should probably go in mensaje. You should use try-finally blocks to close the open files. (or with blocks in python 2.5) > miCorreo = Correo(mensaje) >miCorreo.connect() >empiezo = time.clock() >for nro, addr in mails : >if nro%mensaje.cuantos == 0 and nro > 0 : >miCorreo.disconnect() >time.sleep(mensaje.intervalo - (time.clock() - > empiezo)) >if not miCorreo.connect() : >logging.info('Terminando') >return >empiezo = time.clock() >miCorreo.enviar(addr) >miCorreo.disconnect() This is the real meat. Everything before it can be reduced to one line. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Program review
> > Maybe it is my poor understanding of exception handling. My intention > here is to open the first file, if error then report in logging and > finish normally, else open the 2nd file, if error then report in > logging > close the 1st file and finish normally. If no error then process. > Right, but it helps to break up the error handling and the cleanup. Right now the error reporting exception handlers are intermixed with your cleanup error handlers. Separating them makes the code clearer. def open_file(filename): try : return open(filename) except Exception: logging.error('No pude abrir "%s"' % filename, exec_info=True) raise # Goes into configuration class def mails(self): fIncl = open_file(self.direcciones) try: fExcl = open_file(self.excluidas) try: return enumerate( set(addr.strip() for addr in fIncl) - set(excl.strip() for excl in fExcl)) finally: fExcl.close() finally: fIncl.close() Or if you're using python 2.5 then you can use with statements: from __future__ import with_statements from contextlib import closing ... def mails(self): with closing(open_file(self.direcciones)) as fIncl: with closing(open_file(self.excluidas)) as fExcl: return enumerate( set(addr.strip() for addr in fIncl) - set(excl.strip() for excl in fExcl)) > close the 1st file and finish normally. If no error then process. If an exception is raised then the code terminates right there and then. So, if procesar is written as: def procesar(mensaje): mails = mensaje.mails() miCorreo = Correo(mensaje) then if mensaje.mails() raises an exception then the code terminates immediately, and the body of Correo will only be executed if there is no error. As you have it written it terminates, but it also silently consumes the exception. If something goes wrong with your program then there is no way of diagnosing the problem because the failure cause is never reported. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interview questions in python and wxpython
> Your meta-mission is to propose ideas, state assumptions and ask > questions. Is the track infinite in the sense that it resides on an infinite plane, or is it infinite in the sense that it is circular, wrapping around the entire planet and meeting with itself? -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] conditionals with slicing not seeming to work
- Jeff Younker - [EMAIL PROTECTED] - On Jan 27, 2008, at 3:01 PM, Rob Stevenson wrote: I'm working at a certain website's puzzles using pythonin order to learn the language, but am stuck understanding what is going on in my code (ie why it doesn't do as expected) the intention of this snippet is to only print slices where character 1 is lower case, 2-4 and 6-8 are upper. The logic here looks right to a VB eye. Here's the code... s = """kKjyaqbooOlNkAddgAazFlgKLjlXDGtlvRBYCYQiRfRIfWIYaLZxLrQzrYzBghYOKAaKgXmUpPkCaMmN GlAXokgPsdyUjsiaKSSoeCqMrMbQXeRZqkNeAQpujYJFGfbeceunpFNYjuUPiQVOZPXTKhiwPMLKZEKP NoEPPwXtRoVfGYIRyRgZWyJrMjuBQNchjZBNQUwSgIyXniXCMeXRfAcDKxskxYvMyRGyXeSlOFKFItyI wgDEIuvHFxRfQhtqLKnJfONtkcnDORkZqbtPplsjjTEIsquhSsQTwNZuPVxaTqDvwMONBfCsNJuJpJHZ dCdFLtBQPtFQuCdKOrpndJNUFQIDSbetUKylhSUjcDVtbiQrWMRQhAwGUZyPneCGUjGBBTkLqxLAXXtB KfErkDaWMFZZeuqDmXKJEGHyToPUhPphfVhgUZgbIuRAtWnroImpJKqqmEZqeNQCKzhjIkKQHURWLXFw PBuijeoTSpsVLaOGuLVjMZXkBvVXwUuHfBihziiavGSYofPNeKsTXruMUumRRPQJzvSzJkKbtSipiqBd """ h = range(len(s)-9) for i in h: j=s[i:i+8] if j[0].islower(): if j[1:3].isupper(): if j[5:7].isupper(): print j Any help much appreciated! Your slice indexes are off: >>> m = "01234567" >>> m[0] '0' >>> m[1:3] '12' >>> m[5:7] '56' >>> What you want is: >>> m[2:4] '23' >>> m[6:8] '67' -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] results not quite 100 percent yet
Here is a strict translation of your code. The algorithm remains the same, but it has been cleaned up a bit. Duplications have been removed, and the various logical bits have been broken down into separate functions. I suspect you don't know about random.randint(low, high). It generates random integers between the low and high inclusively. E.g. random.randint(0, 1) will generate either 0 or 1. import random entrance = 6 exit = 11 death_room = 13 low_treasure_value = 10 high_treasure_value = 110 treasure_rooms = 3 monster_rooms = 4 table= [[ 0, 2, 0, 0, 0, 0, 0], [ 1, 3, 3, 0, 0, 0, 0], [ 2, 0, 5, 2, 0, 0, 0], [ 0, 5, 0, 0, 0, 0, 0], [ 4, 0, 0, 3,15,13, 0], [ 0, 0, 1, 0, 0, 0, 0], [ 0, 8, 0, 0, 0, 0, 0], [ 7,10, 0, 0, 0, 0, 0], [ 0,19, 0, 0, 0, 8, 0], [ 8, 0,11, 0, 0, 0, 0], [ 0, 0,10, 0, 0, 0, 0], [ 0, 0, 0,13, 0, 0, 0], [ 0, 0,12, 0, 5, 0, 0], [ 0,15,17, 0, 0, 0, 0], [14, 0, 0, 0, 0, 5, 0], [17, 0,19, 0, 0, 0, 0], [18,16, 0,14, 0, 0, 0], [ 0,17, 0, 0, 0, 0, 0], [ 9, 0, 0,16, 0, 0, 0]] def distribute_treasure(table, number_pieces): for x in range(0, number_pieces): place(table, random_treasure()) def distribute_monsters(table, number_monsters): for x in range(-number_monsters, 0): place(table, x) def place(table, item): table[any_available_room(table)][6] = item def any_available_room(table): room = random_room(table) while not is_available(table, room): room = random_room(table) return room def random_room(table): return random.randint(0, len(table)-1) def is_available(table, room): if room in [entrance, exit, death_room]: return False elif table[room][6] != 0: return False else: return True def random_treasure(): return random.randint(low_treasure_value, high_treasure_value) distribute_treasure(table, treasure_rooms) distribute_monsters(table, monster_rooms) for x in table: print x -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SSH with Python
> What's the path to ssh under windows vista? Paramiko is a pure > python implementation of the SSH protocol, so it should run on any > platform. Paramiko rocks. I have a deployment system that works on linux, osx, and windows without a hitch. If you're writing a custom SSH server or doing tunneling then Paramiko is your choice. It is however something of a pain to use. > Besides, IMHO, expect, pexpect, Perl::Expect... the entire concept > is a horrible, horrible kludge and should only be used as a last > resort when no other interface is possible. In my experience most applications for SSH involve running other programs(via the command line on a remote machine. Pexpect gives you the tools to control those remote processes. Paramiko does not. It is a horrible, horrible hack but in 95% of the cases it works better than the sleek elegant way, and it takes far, far less time and effort. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python oddity
So the problem is that when I change bb, aa also changes even though I don't want it to. Is this supposed to happen? Yes Names are handles for objects. Assignment binds a name to an object. The same object can be bound simultaneously to many names. Python distinguishes between equality and identity, so there are two comparison operators: '==' and 'is'.Objects are equal if they have the same value (==) but they are identical only if they refer to the same object (is). The constructor expressions [], {}, and () produce new objects. >>> [1, 2] == [1, 2] # same value True >>> [1, 2] is [1, 2] # but different lists False >>> a = [1, 2] # this is assigned to a new list >>> b = [1, 2] # this is also assigned to a new list >>> a == b # the lists have the same value True >>> a is b # but they are not the same list False >>> a = [1, 2] # assign to a new list >>> b = a # attach the list in a to a new name >>> a == b # they have the same value True >>> a is b # because they are the same list True The list(FOO) function copies an list. >>> a = [1, 2] # new list >>> b = list(a) # make a copy of the list in b >>> a == b # the original and copy are equal True >>> a is b # but they are not the same list False - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python oddity
4) The typical knee-jerk reaction to this 'oddity' is "what a pain, how stupid" etc, but I'm sure there is a good explanation. Can someone explain why python acts this way? faster processing? preserve memory? etc? This comes down (largely) to a philosophical choice. Does the language work with things that are platonic mathematical entities, or does it work with things that have a concrete physical existence? Consider this: >>> k = Printer('csx2500') >>> print k.sheets_left() 200 >>> m = k >>> m.print_document(file="/tmp/my.pdf") >>> print m.sheets_left() 190 So what should k.print_sheets() show? There are many things in computers that work like this: hard drives, file systems, data bases, chunks of memory, printers, the screen, mice, keyboards, and user interfaces. Attaching a mouse to another name doesn't create a new mouse. Assigning a file to a different variable shouldn't create a new file. There are languages that work like you expect. They're called pure functional languages. They have their adherents, but they're known to have problems with certain things like, well, input and output. (On the other hand they do multiple threads of execution very well.) What it comes down to is this: does you program work with an idealized universe, or does it work with the messy innards of a computer? -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] identifying the calling module/function
On Mar 7, 2008, at 6:04 AM, [EMAIL PROTECTED] wrote: > Kent Johnson schrieb: >> >> Why do you need to do this? It sounds like a design change is needed. >> Anyway the answer to your question is yes. This recipe should point >> you >> in the right direction: >> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062 >> >> Kent >> > > First, thanks for the link. I want this functionality mainly because > it > seems like a quick way to accomplish my goals for the project (I'm > in a > hurry to get this finished). As I advance more in Python, a design > change might happen :) Telling us your goal might allow us to recommend a better and faster way of accomplishing it. - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Const on Python
On Mar 7, 2008, at 7:48 PM, Ricardo Aráoz wrote: > Alan Gauld wrote: > Well, I guess it's about what you think a programmer is. I think if > you > are a "true" programmer you'll be good in ANY language (though you may > have your preferences) and you'll be able to do 80% of your work in > any > language (and learn 80% of any language in a short time). So there > would > not really be such a problem with "foreign code", the only issues I > foresee are establishing proper "coding rules" for the company, that > might take some time and produce some flaky code. As for integration > between apps, if the languages are python and C/C++ it seems not to > be a > problem (never done it), there is : > http://www.python.org/doc/ext/intro.html It's easy to learn the basic features of a language and to use those, but developing fluency is much harder, and it takes a much longer time. - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to get response from os.system()
> Would you mind perhaps show an example running an interactive > command like su and show how to send input to the commands waiting > propmts? If you're doing that then you *really* want to be using the pexpect module. cmd = pexpect.spawn('su - SOMEINTERACTIVECOMMAND') cmd.expect('# ') # the prompt cmd.sendline('A COMMAND') cmd.expect('# ') # wait for the prompt again output = cmd.before # the stuff before the prompt cmd.sendline('exit') cmd.close() - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Which Python Script Editor of Choice?
Jeffrey Dates wrote: > So as I'm starting to get into writing some scripts, I'm looking for > recommendations for a nifty script editor. Ideally a freeware/ > shareware solution until I can justify a purchase of something more > beefy. > > Currently I'm using PSPad, however it's pretty dumb and doesn't > recognize Python context. Which, I suppose is fine, but would be > nice if there was one. Especially since I'm learning. I use Eclipse+PyDev plugin+commercial ($30) PyDev extensions (which runs for free, but nags every hour or so.) I has a feature set that no other python IDE has at this point. (Let me qualify that: I haven't looked at the Iron Python tools on windows, and they might be better, but I don't do much in the windows world so I haven't looked at it yet.) - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hoping to benefit from someone's experience...
On Apr 15, 2008, at 11:33 PM, Alan Gauld wrote: > Have you considered driving Word instead of OOo? > That way you leave the documents in their original format > and make the mods using COM from Python. I've used this approach before, and I whole heartedly suggest that you look into it. The Win32 extensions for Python make talking to office applications via COM nearly effortless. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] HTML Parsing
On Apr 21, 2008, at 6:40 AM, Stephen Nelson-Smith wrote: > On 4/21/08, Andreas Kostyrka <[EMAIL PROTECTED]> wrote: > I want to stick with standard library. > > How do you capture elements? from xml.etree import ElementTree document = """ foo and bar foo bar """ dt_elements = ElementTree.XML(document).findall('dt') -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 6x6 word square solver too slow
However I suspect any attempt to improve performance here needs a new algorithm and probably a clever data structure to minimise brute force tests. If I knew of one, I would have used it :) I'd suggest googling for 'trie'. Tries are method of indexing sets of strings by prefix. As I recall, English words are more similar at the front, so once you have an indexing scheme you'll probably to do even less work if you index and search from the back of the words towards the front. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] seeking help to a problem w/ sockets
It sounds like a process is still listening on the port. If you're on a Unix system then you can use lsof (aka list open files) to locate the process holding on the socket. Killing the process should free the socket. Also, you don't have to close the socket after every connection completes. Try looking at the SocketServer module. It takes care of a lot of the details for you. - Jeff Younker - [EMAIL PROTECTED] - On Apr 26, 2008, at 5:56 PM, James Duffy wrote: I have a problem w/ a file transfer receiver. They way it works is it binds a port for incoming transfer , when the file transfer is complete. It closes the connection and the socket, then loops back and restarts the bind and listen. I have it set so that the socket is reuseable, which is why this works. However, if the program that is using this function is closed while listening, it appears that it does not ”un-bind” because when the program is reopened and a listen attepted to start I get a “port already in use” error. Only a reboot fixes this issue. This code is imported into a main GUI script. We have it set to execute some cleanup functions on exit, I need a function that can dig down to the thread the listener is running in, stop the listen and close the connection and socket. I basically need to get to the close function and then stop the while loop. Thanks in advance for any help anyone can give. My code for the listener class follows: class Reciever ( Thread ): # the reciever class for the test tool, runs as a separate thread from the main program def __init__( this ): Thread.__init__( this ) def run(this): this.process() def bindsock( this ): # create a new socket, bid the socket to the port, listen until connection recieved this.Lport=listen this.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) this.sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) this.sock.bind(('',this.Lport)) this.sock.listen(1) print "Listening on port " +str(this.Lport) def acceptsock( this ): #when connection is incoming, accept the connection this.conn, this.addr = this.sock.accept() print 'Got connection from', this.addr def transfer( this ): #when connection is full established, begin data download global filenumber print 'Starting media transfer ' openfile="XMLrecieved"+str(filenumber)+".xml" f = open(openfile,"wb") #create and open a new file for writing incoming data to while 1: data = this.conn.recv(1024) #check for incoming data if not data: break #if not present, break the loop f.write(data) #if data is present, write it to file and loop back f.close() #when loop is broken, close the file print "Got XML file:" + openfile print 'Closing media transfer' filenumber = filenumber + 1 def close( this ): #close all connections and sockets this.conn.close() this.sock.close() def process( this ): #this is the loop of the thread, it listens, receives, closes then repeats until entire program is closed while 1: this.bindsock() this.acceptsock() this.transfer() this.close() ___ 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] Memory Leak?
I followed the advice on this page: http://mail.python.org/pipermail/python-list/2006-December/417208.html and the problem is now gone. There are two pieces of advice on that page. Which did you follow? - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Replacement Question
On May 21, 2008, at 6:42 AM, Kinuthia Muchane wrote: st = "String" print "%s " %st*3 String String String Does this help? Another approach is to use dictionaries for string replacement. >>> subst = {'some': 'thing'} >>> print "%(some)s%(some)s%(some)s" % subst thingthingthing -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Controlling applications
On Jun 9, 2008, at 8:51 AM, W W wrote: Originally I was planning to use urllib to post/retrieve the data, but as far as I can tell, the bank uses several variables and some javascript to authenticate everything, and it seems overly complicated to try and manually figure it all out. Raw urllib is a poor way to interact with web sites; it's too low level. Instead you might consider the libraries mechanize and twill. They are essentially web browsers in a library. They won't handle JavaScript, but they do let you retrieve, fill in, and send forms without worrying much about the details of the conversation. You might give them a try before completely giving up on the command line mechanisms. Alas controlling applications is very hard in Unix land and very easy in the Windows world. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDE
On Jun 10, 2008, at 4:07 AM, Sean Novak wrote: I'm looking for the perfect IDE, preferably open source. I've installed Bluefish, which I find to be a little buggy still. I'm just starting to dive into emacs, which I feel is a little daunting. If someone has tried a few different IDEs and found the one that they love.. I'd be interested in your insight! Pydev and Eclipse are an industrial strength solution. You get a full integrated IDE: - An editor with spell checking, syntax highlighting, code completion, templates, etc. - An integrated visual debugger. - Outline navigators - Remote debugging. - Refactoring support - Navigation tools like 'go to definition' - Revision control integration with just about every revision control system on the planet. - Unit test runner - Interactive python execution - Job management via Mylar plugin - SQL editing via SQLExplorer plugin - HTML/XML/Javascript development plugins - Run external tools from within the IDE - To do lists - Multi-language development - etc. - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] when is object relational mapping for Python warranted?
Yes, it's worth it. It makes it trivial to write simple database applications. The smaller the program, the more useful they are. Both SQLObject and SQLAlchemy have their strengths and weaknesses. SQLObject will get you and going up really fast. Its really simple to create a schema and to start working on a project. The most recent version is also acquiring a revision system. SQLAlchemy has a little more overhead, but it ultimately allows you to perform much more sophisticated operations. It also serves as a simpler DBI. My personal take is that SQLAchemy gives you much more growing room, and that this is well worth the initial investment. The documentation for SQLAlchemy tends to be better too. (That said I work primarily with SQLObject, and the addition of a revision system will keep me from switching our product to SQLAlchemy for a while longer.) - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Web Stats
On Jun 11, 2008, at 12:58 PM, Stephen Nelson-Smith wrote: Take a look at AWStats (not Python). Doesn't this 'only' parse weblogs? I'd still need some kind of spider to tell me all the possible resources available wouldn't I? It's a big website, with 1000s of pages. If you have pages which are no longer referenced from any root pages then a spider won't find them. These dangling pages are precisely the sort of thing you're trying to remove. Consider other options such as looking through the filesystem. - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Object attributes surviving deletion
On Jun 26, 2008, at 2:59 PM, Shrutarshi Basu wrote: self.modules.append(DisplayModule(self.img_map, (self.xOrigin, self.yOrigin), self.rows, self.columns, gram, self.type)) ... As you can see, not only do I delete gram, I also blank out everything that should be cleared. I have to manually clear gram.database because otherwise it keeps the data from previous calls to that function. I don't understand why this should be. DisplayModule is a class that packages and organizes the data into a convenient form. Thanks, You've passed gram to DisplayModule, and you've added DisplayModule to a collection. My guess is that display module still holds a reference to gram, and gram won't be collected until DisplayModule clears that reference. As an additional aside python doesn't guarantee that objects will be released as soon as they are unreachable. They can hang around for a while until a garbage collection is triggered. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Inheritance help
Hmm. I don't understand the LSP to make any requirements on the constructors. It says that instances of a subclass should be substitutable for instances of the base class, it doesn't say anthing how the instances are created. Constructors as a separate language entity is an idea born of C++, Java, etc. In Python classes are first-class values and constructors are (mostly) normal methods with a funky name, so the LSP applies to them just like any other method in a class. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] parsing sendmail logs
On Jul 14, 2008, at 12:29 AM, nibudh wrote: Hi List, I'm looking for some support libraries that will help me to parse sendmail logs. I'm confused about whether i need a "parser" per se, and if i do which parser to use. I found this website http://nedbatchelder.com/text/python-parsers.html which compares a slew of python parsers. Parsers as referenced in your link are intended for heavy-duty lifting such as parsing programming languages. Sendmail logs are very simple, so those parsers are vast overkill. Split on whitespace combined with regular expressions should be up to the job. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] parsing sendmail logs
If you run a pipeline chain from within subprocess every part of the chain will be a separate process, thats a lot of overhead. Thats why admins tend to prefer writing utilities in Perl rather than bash these days. ... Nobody I know uses perl for systems administration because it doesn't have the cost of forking a process. They use it because you have real data structures, functions, access to system calls, etc. with one *cough* consistent syntax. One of perl's big selling points for system administration is that it is *so* easy to fork off a shell and read its output. The argument that processes are too expensive might have had some truth back in '92 or '93 when machines had 8M or 16M of memory, had 500M hard drives, and ran at a fraction of the speed they do now, but that was a long time ago. -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is anybody out there who could help me with URL Authentication?
On Aug 1, 2008, at 12:39 AM, Federo wrote: Below is problem I am unable to solve. I would appreciate your advice or even better code sample. The problem is URL authorisation. I try to approaches but no luck so far. Two problems: 1.) Being able to logon using Python code 2.) Being able to send data to a form and get back server reply While it's possible to cobble together something using urllib and htmlparser, you're much better off using mechanize. It's basically an entire browser in a library. It handles http authentication, cookies, form processing, etc. For more details check out the project's page at: http://wwwsearch.sourceforge.net/mechanize/ - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this a "Class" problem?
On Aug 18, 2008, at 9:13 AM, Adrian Greyling wrote: def MainToSecond(self, event): # wxGlade: MyMainFrame. MySecondFrame(self).Show() MySecondFrame.text_ctrl_2.SetValue("This text was generated from the 'MainFrame' window") The expression MySecondFrame(self) creates a new object. It initializes the new object by calling the MySecondFrame's __init__ method. class MySecondFrame(wx.Frame): def __init__(self, *args, **kwds): # begin wxGlade: MySecondFrame.__init__ ... self.text_ctrl_2 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE) ... The __init__ method calls sets the variable text_ctrl_2 in the object m. Your function MainToSecond is trying to get the attribute MySecondFrame.text_ctrl_2. This attribute does not exist. You want to get the attribute m.text_ctrl_2. So, the method should be: def MainToSecond(self, event): # wxGlade: MyMainFrame. m = MySecondFrame(self) m.Show() m.text_ctrl_2.SetValue("This text was generated from the 'MainFrame' window") Also, method and function names should always start with a lower case letter: always mainToSecond and never MainToSecond -jeff___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is this a "Class" problem?
I'm assuming you do that so that you don't confuse what a Python built-in method (or function) is, compared to methods (or functions) that you've authored. It's done more to distinguish classes from methods and attributes. I can't claim any credit though. It's part of the python coding conventions in PEP-8. You can find the document at http://www.python.org/dev/peps/pep-0008 . The actual coding convention for attribute and variable names is lower_case_separated_by_underscores, but there are projects out there that use initialLowerCamelCase. Pick whatever your current project is using and stick with it. -jeff___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] importing strings
On Sep 11, 2008, at 2:15 PM, Patrick wrote: I have been able to import a string from another module threw a dictionary but I can't seem to figure out how to do it so that I can insert a value into the %s placeholder afterwards. The % operator is probably what you are looking for. format = "this is a %s" subst = 'test' print format % subst FYI: You shouldn't reply to a message when you have a new question. That attaches it to the previous topic. - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Threading
On Sep 11, 2008, at 7:56 AM, Oleg Oltar wrote: Hi! I need to open about 1200 urls from my database. And to check that those urls are really exists. I used urllib2.urlopen for it. But it's a little bit slow. I thought that it's might be a good idea to do it in a threads. So it can add some performance to my code. Unfortunately I can't get started with the treading module. There are no simple examples in the python docs. Not sure how to start. What I want to have: for my list of urls I want to start few threads, each one of them should do some validation. My code without threads: The first thing to do is to break your program into phases. Right now all your logic is intermixed. This isn't a good thing when working with threads. You want to isolate the parts that need parallelization from the parts that don't. A general stylistic note: Don't start your code with utility and helper functions. You're telling a story. Start with the big overarching logic that orchestrates all the bits in pieces. That's the plot of your story. The little helper functions are like details about a character. They get in the way until you've figured out what's going on. It seems to me like you have three distinct parts: - get the list of urls from the database - check the urls - report the urls status So your main method will look something like: def test_urls(): urls = urls_from_database() checked_urls = check(urls) report(checked_urls) The only method that needs to be parallelized is check(urls). One approach to threading is to start one thread for every URL, but if you have too many URLs you could bring your machine to its knees. Doing 30 things at once may be fine, but doing 1000 is quite possibly a problem, so you have to limit the number of threads. The classic way of doing this uses a pool of tasks and a number of worker threads that pull jobs out of the pool. Your master thread fills the pool, starts a number of worker threads, and then waits for them to finish. Each worker thread pulls a job from the pool, performs the job, writes the results to another pool. When the pool is empty the program continues. Your check method might be something like this: from Queue import Queue from threading import Thread ... def check(urls): unchecked_urls = Queue() # a queue full of url strings checked_urls = Queue() # a queue full of (url string, is_good boolean) tuples fill_job_pool(unchecked_urls, urls) start_worker_threads(unchecked_urls, checked_urls, number_workers=10) # waits until all the jobs have been emptied from the queue unchecked_urls.join() return results_from(checked_urls) def fill_job_pool(unchecked_urls, urls): for url in urls: unchecked_urls.put(url) def start_worker_threads(unchecked_urls, checked_urls, number_workers): for x in range(0, number_workers): # Creates a thread object that will call worker_thread(unchecked_urls, checked_urls) # when it is started. worker = Thread(target=worker_thread, args=(unchecked_urls, checked_urls)) # Python will terminate even if this thread is still alive. This means that the # thread doesn't need to kill itself. worker.setDaemon(True) # Start the worker thread worker.start() def results_from(checked_urls): results = [] while not checked_urls.empty(): results.append(checked_urls.get()) return results def worker_thread(job_pool, result_pool): while True: url = job_pool.get() is_good = check_url(url) result = (url, is_good) result_pool.put(result) job_pool.task_done() def check_url(url): # YOUR URL CHECK HERE return True Once you plug this into your program, you'll start finding ways that you can shorten the whole program. Instead of passing around arrays or urls and results you can pass around the queues directly. In addition you can run the report function as another thread. It prints the jobs from the result pool as they're completed. These will make the code more elegant, but the solution here gets at the heart of the problem. - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PySQLite vs SQLalchemy
On Sep 12, 2008, at 3:30 PM, Patrick wrote: I like SQLite, it's really easy to work with. I would like to model my database in it natively but I am having quite a bit of trouble mapping the variables in the SQLIte database via PySQLite. It appears that this sort of thing is SQLalchemy's strong suit. However I would prefer not to model my data base in it. I'm curious why you feel that way? So, I am just wondering if anyone has any feedback with regard to PySQLite and SQLalchemy. Is mapping the variables from the a database via PySQLite a common problem? Is SQLalchemy being used for desktop Applications? I bought a book on SQLalchemy but PySQLite seems to have sparse documentation. We can build Python with SQLite built right in, why is there not more documentation? I personally have never even considered PySQLite. SQLAlchemy and SQLObject have covered all my needs from the small to the large quite handily. It saves me from having to resort to learning a new interface for each database, and it largely insulates my applications from changes in the underlaying database. [This isn't an academic possibility either. Much of my code is unit- tested against sqlite, but runs against mysql in production.] -jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Win32 extensions
On Sep 11, 2008, at 2:52 PM, Spencer Parker wrote: I am tasked with a project to use WMI to script something on Windows Server 2003. I was wondering if it is possible to create a script on a linux machine and use the Win32 extensions...since I need those to use WMI python module as well. Is this actually possible? Otherwise I would have to use a windows server in order to run these scripts. I do not want to use VB is I don't have or a windows server. I am deploying Windows Servers from behind a linux PXE. One option is to use VMWare or Xen to run windows as a virtual machine on whatever flavor of unix you're using. - Jeff Younker - [EMAIL PROTECTED] - -- Spencer Parker ___ 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] sample python twill scripts?
On Oct 2, 2008, at 8:21 AM, bob gailer wrote: jeremiah wrote: Just wondering if anyone here would be interested in sharing a python twill script? I'd just like to take a gander at how others are engineering their scripts. twill? Twill is an http client automation tool. It's used quite a bit for performing functional testing from Python. - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor