Code Feedback
Hello - Last night I wrote my first code in Python -- a little producer/consumer toy to help me begin to understand things. The "Library" only has a few books. You can choose how many Readers are trying to check out the books. When no books are available, they have to wait until some other Reader returns one. Readers are assigned random reading speeds and wait times between It runs fine, but I have a few questions about it. 1) Is this good Python code? What should be changed to make it more Pythonesque? 2) Anybody know how to alter the "while 1: pass" section to make the app stoppable? 3) The synchronization I'm using works, but is there a better way to do it? Thanks for any insight/pointers, etc. Also, for any newbies like me, feel free to post variations of this code. In the next version, I'm going to make it so that readers can't check out the same book twice. Here's the code: #!/usr/bin/python # Filename: Library.py # author: MWT # 5 Feb, 2006 import thread import time import threading import random class Library: #The goal here is to create a synchronized list of books def __init__(self): self.stacks = ["Heart of Darkness", "Die Verwandlung", "Lord of the Flies", "For Whom the Bell Tolls", "Dubliners", "Cyrano de Bergerac"] self.cv = threading.Condition() def checkOutBook(self): #remove book from the front of the list, block if no books are available self.cv.acquire() while not len(self.stacks) > 0: self.cv.wait() print "waiting for a book..." bookName = self.stacks.pop(0) self.cv.release() return bookName def returnBook(self, nameOfBook): #put book at the end of the list, notify that a book is available self.cv.acquire() self.stacks.append(nameOfBook) self.cv.notify() self.cv.release() class Reader(threading.Thread): def __init__(self, library, name, readingSpeed, timeBetweenBooks): threading.Thread.__init__(self) self.library = library self.name = name self.readingSpeed = readingSpeed self.timeBetweenBooks = timeBetweenBooks self.bookName = "" def run(self): while 1: self.bookName = self.library.checkOutBook() print self.name, "reading", self.bookName time.sleep(self.readingSpeed) print self.name, "done reading", self.bookName self.library.returnBook(self.bookName) self.bookName = "" time.sleep(self.timeBetweenBooks) if __name__=="__main__": library = Library() readers = input("Number of Readers?") for i in range(1,readers): newReader = Reader(library, "Reader" + str (i), random.randint(1,7), random.randint(1,7)) newReader.start() while 1: pass -- http://mail.python.org/mailman/listinfo/python-list
Re: Code Feedback
Thanks for all the feedback. Interestingly, I can't seem to get Dan M's code: [code] try: while 1: pass except KeyboardInterrupt: break [/code] to work, no matter how many variations I try (including adding in "time.sleep(0.1)" as Peter Hansen suggested. The program just continues to execute, ignoring the command to stop. I'm guessing that this has something to do with the fact that the Reader threads are still running? A further question: Can anyone point me to a good description of the best way to write/use doc strings in Python? -- http://mail.python.org/mailman/listinfo/python-list
Re: Code Feedback
Jorgen Grahn wrote: > You might want to look into using doc strings properly. You have comments at > the start of functions, but they read more like implementation notes than > "what this method does and why". But I only had a quick look. Yeah. They were just my scaffolding notes to myself. I'd hurry up with that Conrad if I were you, and get on with the Hemingway. ;) -- http://mail.python.org/mailman/listinfo/python-list
450 Pound Library Program
So in a further attempt to learn some Python, I've taken the little Library program (http://groups.google.com/group/comp.lang.python/browse_thread/thread/f6a9ccf1bc136f84) I wrote and added several features to it. Readers now quit when they've read all the books in the Library. Books know how many times they've been read. Best of all, you can now create your own list of books to read! Again, the point of all this is to get used to programming in Python. So although the program is trivial, any feedback on style, structure, etc. would be much appreciated. I'm a convert from Java, so I've probably got some unconscious Javanese in there somewhere. Help me get rid of it! Here's the new, improved program: [code] #!/usr/bin/python # Filename: Library.py # author: mwt # Feb, 2006 import thread import time import threading import random class Library2: def __init__(self, listOfBooks, totalBooks): self.stacks = listOfBooks self.cv = threading.Condition() self.totalBooks = totalBooks def checkOutBook(self, readerName): "'Remove book from the front of the list, block if no books are available'" self.cv.acquire() while len(self.stacks) == 0: self.cv.wait() print "%s waiting for a book..." %readerName book = self.stacks.pop(0) self.cv.release() return book def returnBook(self, returnedBook): "'put book at the end of the list, notify that a book is available'" returnedBook.wasRead() self.cv.acquire() self.stacks.append(returnedBook) self.cv.notify() self.cv.release() class Reader(threading.Thread): def __init__(self, library, name, readingSpeed, timeBetweenBooks): threading.Thread.__init__(self) self.library = library self.name = name self.readingSpeed = readingSpeed self.timeBetweenBooks = timeBetweenBooks self.book = "" self.numberOfBooksRead = 0 def run(self): "'Keep checking out and reading books until you've read all in the Library'" while self.numberOfBooksRead < self.library.totalBooks: self.book = self.library.checkOutBook(self.name) print "%s reading %s" %(self.name, self.book.title), time.sleep(self.readingSpeed) self.numberOfBooksRead += 1 self.library.returnBook(self.book) print "%s done reading %s" %(self.name, self.book.title), print"Number of books %s has read: %d" %(self.name, self.numberOfBooksRead) self.bookName = "" time.sleep(self.timeBetweenBooks) print "%s done reading." %self.name class Book: def __init__(self, author, title): self.author = author self.title = title self.numberOfTimesRead = 0 #print "%s,%s" % (self.author, self.title),#print as books are loaded in def wasRead(self): self.numberOfTimesRead += 1 print "Number of times %s has been read: %d" %(self.title, self.numberOfTimesRead) if __name__=="__main__": print "\nWELCOME TO THE THURMOND STREET PUBLIC LIBRARY" print "Checking which books are avialable...\n" try: theBookFile = open("books.txt", "r")#Create your own list of books! stacks = []#a place to put the books for line in theBookFile.readlines(): L = line.split (",") # a comma-delimited list author = L[0] bookName = L[1] newBook = Book(author, bookName) stacks.append(newBook) theBookFile.close() except IOError: print "File not found!" #string = "How many books would you like in the Library?[1-" + str(len(stacks)) + "]" totalBooks = input("How many books would you like in the Library?[1-" + str(len(stacks)) + "]") stacks[totalBooks: len(stacks)] = [] print "Number of books in the Library is:", len(stacks) library = Library2(stacks, totalBooks) readers = input("\nHow many readers would you like?") print "Number of readers is:", readers, "\n" for i in range(0,readers): newReader = Reader(library, "Reader" + str (i), random.randint(1,7), random.randint(1,7)) newReader.start() [/code] And here's a handy text file of books for you, so you don't have to make your own: [code] Conrad, Heart of Darkness Kafka, Die Verwandlung Hemingway, For Whom the Bell Tolls James Joyce, Dubliners Moliere, Cyrano de Bergerac William Golding, Lord of the Flies Dostoevski, Crime and Punishment Cervantes, Don Quixote Camus, L'Etranger Tolstoy, War and Peace Poe, Tales Faul
Re: 450 Pound Library Program
A million thanks for in-depth critique. I look forward to figuring out half of what you're talking about ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: 450 Pound Library Program
BTW - I can't find any code examples of how to interrupt a beast like this. A regular ctrl-c doesn't work, since the separate threads just keep executing. Is this a case where you need to iterate through all threads and stop each one individually? How would that look? -- http://mail.python.org/mailman/listinfo/python-list
CLI
I want to do programmatic terminal commands on unix with python - i.e. I want my program to issue commands to start and stop scripts, other programs, etc. I'm sure this must be fairly straightforward, but haven't been able to find a reference for it. Any help? -- http://mail.python.org/mailman/listinfo/python-list
Clearing the screen
I'm doing some python programming for a linux terminal (just learning). When I want to completely redraw the screen, I've been using os.system("clear") This command works when using python in terminal mode, and in IDLE. However, when running a little .py file containing that command, the screen doesn't clear. What to do? -- http://mail.python.org/mailman/listinfo/python-list
Re: Clearing the screen
I can't seem to get that to behave properly. It works fine in a python shell, like you're demonstrating it, but not as a command in a module. -- http://mail.python.org/mailman/listinfo/python-list
Re: Clearing the screen
No guessing needed. If I just use os.system("clear") on its own, no problem. Also, if I use the magic formula you gave on its own, that works to. But in the app, (see below), neither command works. I'm missing something obvious, but I'm... missing it. def run(self, userinfo): """Time when to do stuff""" self.teamname = userinfo[0] self.username = userinfo[1] self.refreshinterval = userinfo[2] self.statsfile = userinfo[3] print self.username, "", self.teamname self.minutes = 0 while 1: try: """ Checks for internet stats once per hour """ if self.minutes == 0: self.get_online_stats(self.statsfile) self.minutes += 1 if self.minutes == 60: self.minutes = 0 self.output() time.sleep(60) #nite-nite except KeyboardInterrupt:#user types Ctrl-C print "Bye bye." os.system("clear") sys.exit() def output(self): os.system("clear") #clear the screen print "" print "* Monitor *" print "" print "* *" self.get_unit_info('/opt/foldingathome/1/unitinfo.txt')#print the current unit info print "\n last updated at", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) print "* *" print "*Stats**" print "* *" self.get_saved_stats(self.statsfile) print "* *" print "" -- http://mail.python.org/mailman/listinfo/python-list
Re: Clearing the screen
Arrgghh... Is there any way to edit posts on this thing? The os.system("clear") doesn't work at all in a module. -- http://mail.python.org/mailman/listinfo/python-list
Downloading Large Files -- Feedback?
This code works fine to download files from the web and write them to the local drive: import urllib f = urllib.urlopen("http://www.python.org/blah/blah.zip";) g = f.read() file = open("blah.zip", "wb") file.write(g) file.close() The process is pretty opaque, however. This downloads and writes the file with no feedback whatsoever. You don't see how many bytes you've downloaded already, etc. Especially the "g = f.read()" step just sits there while downloading a large file, presenting a pregnant, blinking cursor. So my question is, what is a good way to go about coding this kind of basic feedback? Also, since my testing has only *worked* with this code, I'm curious if it will throw a visibile error if something goes wrong with the download. Thanks for any pointers. I'm busily Googling away. -- http://mail.python.org/mailman/listinfo/python-list
Re: Downloading Large Files -- Feedback?
Pardon my ignorance here, but could you give me an example of what would constitute file that is unreasonably or dangerously large? I'm running python on a ubuntu box with about a gig of ram. Also, do you know of any online examples of the kind of robust, real-world code you're describing? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Downloading Large Files -- Feedback?
Thanks for the explanation. That is exactly what I'm looking for. In a way, it's kind of neat that urlopen just *does* it, no questions asked, but I'd like to just know the basics, which is what it sounds like urlretrieve covers. Excellent. Now, let's see what I can whip up with that. -- just bought "cookbook" and "nutshell" moments ago btw -- http://mail.python.org/mailman/listinfo/python-list
Re: Downloading Large Files -- Feedback?
>It isn't written in C, but get your hands on wget. It >is probably already on your Linux distro, but if not, >check it out here: >http://www.gnu.org/software/wget/wget.html Thanks. I'm checking it out. -- http://mail.python.org/mailman/listinfo/python-list
Re: Downloading Large Files -- Feedback?
So, I just put this little chunk to the test, which does give you feedback about what's going on with a file download. Interesting that with urlretrieve, you don't do all the file opening and closing stuff. Works fine: -- import urllib def download_file(filename, URL): f = urllib.urlretrieve(URL, filename, reporthook=my_report_hook) def my_report_hook(block_count, block_size, total_size): total_kb = total_size/1024 print "%d kb of %d kb downloaded" %(block_count * (block_size/1024),total_kb ) if __name__ == "__main__": download_file("test_zip.zip","http://blah.com/blah.zip";) -- http://mail.python.org/mailman/listinfo/python-list
Re: Clearing the screen
It clears the screen by scrolling all the characters out of sight at the top of the terminal window. So the screen is blank, but not cleared in the sense that I mean it. The behavior I want is for the display to be effectively "erased" and ready to receive the next wave of data -- like you would do in a UI -- which is what I'm used to working with, which is why this is mildly frustrating, and at least 1/3 of why I sound like an idiot with this question. Anyway, I don't want it to scroll, just, in essense "refresh". As a total Python noob, my approach has been os.system("clear") ,then print "something". -- http://mail.python.org/mailman/listinfo/python-list
Re: Clearing the screen
You know, for now, I just would like it to work in a standard Gnome terminal (my version is 2.12.0). -- http://mail.python.org/mailman/listinfo/python-list
Default Section Values in ConfigParser
I want to set default values for a ConfigParser. So far, its job is very small, so there is only one section heading, ['Main']. Reading the docs, I see that in order to set default values in a ConfigParser, you initialize it with a dictionary or defaults. However, I'm not quite sure of the syntax to add the section headings in to the dictionary of defaults. For now, I'm doing it like this: default_values = {'username' : 'put username here', 'teamnumber': 'team number here', 'update_interval' : 'update interval'} self.INI = ConfigParser.ConfigParser(default_values) self.INI.add_section('Main') This works, but clearly won't last beyond the adding of a second section. What is the correct way to initialize it with a full dictionary of defaults, including section names? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Default Section Values in ConfigParser
Thanks, Terry. That's an interesting way to go about it. -- http://mail.python.org/mailman/listinfo/python-list
Method Call in Exception
In my latest attempt at some Python code, I've been tempted to write something in the form of: try: [...] #doing some internet stuff except IOError: alternate_method_that_doesnt_need_internet() This works when I try it, but I feel vaguely uneasy about putting method calls in exception blocks. So tell me, Brave Pythoneers, is this evil sorcery that I will end up regretting, or is it just plain good ol' Python magic? -- http://mail.python.org/mailman/listinfo/python-list
Re: Method Call in Exception
Felipe Almeida Lessa wrote: > Em Qua, 2006-04-19 às 16:54 -0700, mwt escreveu: > > This works when I try it, but I feel vaguely uneasy about putting > > method calls in exception blocks. > > What do you put in exception blocks?! Usually I just print an error message. > > > > So tell me, Brave Pythoneers, is this > > evil sorcery that I will end up regretting, or is it just plain good > > ol' Python magic? > > IMHO, the exception block in Python is used a lot in places where you > could use an if-then-else, like your example that could be written as > > if internet_available(): > [...] #doing some internet stuff > else: > alternate_method_that_doesnt_need_internet() > > So yes, I think there's no problem there. > Normally I don't like to use exception blocks to do condition-statement stuff. At least that is how my Java training has programmed me. But in this case, the condition is whether the internet connection is working or not, and I don't see any other way to do it. And furthermore, I don't know if the exceptions-as-logic is a no-no in Python. Hence the question. -- http://mail.python.org/mailman/listinfo/python-list
Finding Module Dependancies
When I'm rewriting code (cutting and pasting pieces from earlier modules), is there a quick way to determine if I have imported all the necessary modules? I am used to working in Java, where the compiler will tell you if you haven't imported everything, and also Eclipse, which has the handy "organize imports" feature. This is not the case in Python, since it's not compiled, of course, and also running it might not insure you've got all the imports, unless you go through every possible usage scenario -- which in some cases is quite a few, and could take a long time. So what I'm looking for is a command, like "check dependencies" or something, which will list all the modules needed for a source module to run. -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding Module Dependancies
John Machin wrote: > > This is called "testing". Yes, it could take a long time. Thanks for the clarification. ;) Actually, I've done hellish amounts of testing on these code pieces, which is why I don't want to have to do it all over again just to check the imports. > > Consider pychecker and pylint. I haven't tried pylint, but pychecker > does what you want and a whole lot more: > > C:\junk>type miss_import.py > # need to import re, but forgot > def my_isdigit(s): > return bool(re.match(r"\d+", s)) > > C:\junk>pychecker miss_import > > C:\junk>c:\python24\python.exe > c:\python24\Lib\site-packages\pychecker\checker.py miss_import > Processing miss_import... > > Warnings... > > miss_import.py:3: No global (re) found > > C:\junk> That sounds really useful. I'll check it out. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding Module Dependancies
Wow! Just ran pychecker on a couple of modules. I'm blown away by how much information I'm getting. Thanks again! -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding Module Dependancies
Wow! Just ran pychecker on a couple of modules. I'm blown away by how much information I'm getting. Thanks again! -- http://mail.python.org/mailman/listinfo/python-list
Re: urllib.urlopen() with pages that requires cookies.
Fredrik Lundh wrote: > Øyvind Østlund wrote: > > > I am trying to visit a limited amount of web pages that requires cookies. I > > will get redirected if my application does not handle them. I am using > > urllib.urlopen() to visit the pages right now. And I need a push in the > > right direction to find out how to deal with pages that requires cookies. > > Anyone have any idea how to go about this? > > use urllib2 and cookielib. here's an outline: > > import urllib2, cookielib > > # set things up > jar = cookielib.CookieJar() > handler = urllib2.HTTPCookieProcessor(jar) > opener = urllib2.build_opener(handler) > urllib2.install_opener(opener) > > data = urllib2.urlopen(someurl).read() > > # ... > > data = urllib2.urlopen(someotherurl).read() > > # ... > > # dump cookie jar contents (for debugging) > for cookie in jar: > print cookie > > How would this outline be changed/expanded if it also needed to handle basic authentication? -- http://mail.python.org/mailman/listinfo/python-list
Re: New python.org website
Wow. That does look fantastic. Thumbs up! -- http://mail.python.org/mailman/listinfo/python-list
Python Love :)
I've only been goofing around with Python for about a month now, but already I am in love. I never get that feeling -- so common with Java -- that I'm swimming upstream, struggling to force the language to do what I want. Python makes it feel effortless and easy. -- http://mail.python.org/mailman/listinfo/python-list
Help Create Good Data Model
Hi. I'm reworking a little app I wrote, in order to separate the data from the UI. As a start, I wanted to create a iron-clad data recepticle that will hold all the important values, and stand up to being queried by various sources, perhaps concurrently. In all likelihood, the app will never need anything that robust, but I want to learn to write it anyway, as an exercise. So here is my code. It's really simple, and I'm sure you can see my Java background. Are there any problems here? Something I'm missing or screwing up? I welcome any and alll feedback, especially if it includes the *why's.* Thanks! #!/usr/bin/python # author mwt # Mar '06 import copy, threading class FAHData(object): """The data model for the [EMAIL PROTECTED] monitor.""" def __init__(self): self.data = {}#this dict will hold all data self.mutex = threading.RLock() def get_all_data(self): """Returns a COPY of entire data dict.""" #not sure deepcopy() is really necessary here #but using it for now #might cause some weird synchronization problems... try: self.mutex.acquire() return copy.deepcopy(self.data) finally: self.mutex.release() def get_data(self, key): """Returns a COPY of data element.""" try: self.mutex.acquire() return copy.deepcopy(self.data[key]) finally: self.mutex.release() def set_value(self, key, value): """Sets value of data element.""" try: self.mutex.acquire() self.data[key] = value finally: self.mutex.release() def set_data(self, data): """Sets entire data dictionary.""" try: self.mutex.acquire() self.data = data finally: self.mutex.release() def clear_data(self): """Clears entire data dictionary.""" try: self.mutex.acquire() self.data = {} finally: self.mutex.release() -- http://mail.python.org/mailman/listinfo/python-list
Re: Help Create Good Data Model
fumanchu: Interesting. I'm trying to understand atomicity. Also, since I want this class to work using the Observer pattern, I've complicated things, as shown below. I'll look into Dejavu for persistence (although most of the basic values are persisted elsewhere, so this app will mainly need only in-memory values and configuration stuff (for which I think a ConfigParser will probably be enough). Sybren: That's a cool choice, but I don't think it's right for what I'm trying to do. Even more massive overkill than what I'm already doing. Plus, I'm trying to write this thing so that I can hand it *anything* (a value, a list, another dict, whole objects, etc.), which might be tough with a DB. Here's what I've got cooking at this point (adapted heavily from Bruce Eckel, as well as incorporating fumanchu's corrections). If you get a chance, please let me know what you think. #!/usr/bin/python # author mwt # Mar '06 import copy, threading, observable class FAHData(Observable): """The data model for the [EMAIL PROTECTED] monitor.""" def __init__(self): Observable.__init__(self) self.data = {}#this dict will hold all data self.mutex = threading.RLock() def get_all_data(self): """Returns a COPY of entire data dict.""" #not sure deepcopy() is really necessary here #but using it for now #might cause some weird synchronization problems... try: self.mutex.acquire() return copy.deepcopy(self.data) finally: self.mutex.release() def get_data(self, key): """Returns a COPY of data element.""" try: self.mutex.acquire() return copy.deepcopy(self.data[key]) finally: self.mutex.release() #these three methods don't need a mutex because they are atomic (I think): #> def set_value(self, key, value): """Sets value of data element.""" self.data[key] = value Observable.notifyObservers(self, arg = 'ELEMENT_CHANGED') def set_data(self, data): """Sets entire data dictionary.""" self.data = data Observable.notifyObservers(self, arg = 'DATA_CHANGED') def clear_data(self): """Clears entire data dictionary.""" self.data = {} Observable.notifyObservers(self, arg = 'DATA_CHANGED') #<- #!/usr/bin/python # author mwt # Mar '06 import threading class Observer(object): def update(observable, arg): """OVERRIDE ME""" pass class Observable(object): def __init__(self): self.obs = [] self.mutex = threading.RLock() def addObserver(self, observer): self.mutex.aquire() try: if observer not in self.obs: self.obs.append(observer) finally: self.mutex.release() def notifyObservers(self, arg = None): self.mutex.aquire() try: localArray = self.obs[:] finally: self.mutex.release() for observer in localArray: observer.update(self, arg) #these methods don't need a mutex because they are atomic (I think): #> def deleteObserver(self, observer): self.obs.remove(observer) def deleteObservers(self): self.obs = [] def countObservers(self): return len(self.obs) #<- mwt -- http://mail.python.org/mailman/listinfo/python-list
Re: Help Create Good Data Model
Well, thank the gods for unit testing. Here's the fah_data module with fewer errors: import copy, threading, observable class FAHData(observable.Observable): """The data model for the [EMAIL PROTECTED] monitor.""" def __init__(self): observable.Observable.__init__(self) self.data = {}#this dict will hold all data self.mutex = threading.RLock() def get_all_data(self): """Returns a COPY of entire data dict.""" #not sure deepcopy() is really necessary here #but using it for now #might cause some weird synchronization problems... try: self.mutex.acquire() return copy.deepcopy(self.data) finally: self.mutex.release() def get_data(self, key): """Returns a COPY of data element.""" try: self.mutex.acquire() return copy.deepcopy(self.data[key]) finally: self.mutex.release() #these three methods don't need a mutex because they are atomic (I think): #> def set_value(self, key, value): """Sets value of data element.""" self.data[key] = value observable.Observable.notifyObservers(self, arg = 'ELEMENT_CHANGED') def set_data(self, data): """Sets entire data dictionary.""" self.data = data observable.Observable.notifyObservers(self, arg = 'DATA_CHANGED') def clear_data(self): """Clears entire data dictionary.""" self.data = {} observable.Observable.notifyObservers(self, arg = 'DATA_CHANGED') #<- -- http://mail.python.org/mailman/listinfo/python-list
Re: Help Create Good Data Model
I get what you're saying fumanchu (or should I say Robert?). I've been working and reworking this code. It's in a lot better shape now (although I hestitate to keep flooding the conversation with each iteration of the file). At the level this app should be operating, I doubt I'll hit performance issues, and it's good to learn the basics first. However, I doubt this would scale very well, so the next step will be to educate myself aobut the performance-enhancing alternatives you're talking about. One thing I'm still not sure about -- and I suspect that there is no right answer -- is the fact that although I am writing the code in Python, the idiom is purely Java. Having my data bucket in the form of, essentially, a bean with setters and getters, and each method surrounded by (the Python version of) a "synchronized" piece, and so on all comes from my Java background. It's ending up working well as code (I'm a lot further along today), and it's accomplishing the decoupling of front and back end I was looking for, so that's excellent. However I do have the vague feeling that I am doing the equivalent of, say, writing Greek hexameters in English (i.e. it works but it is stylistically clunky). Anyway, thanks for your insight. I will probably be posting more of the code later, if you are interested in checking it out. The app is a [EMAIL PROTECTED] client monitor (for Gnome) -- one of those applications, like a web spider, that lots of people want to create, even though there are already a zillion perfectly working versions out there. It's just about the right level of complexity for me now. mwt (Michael Taft) -- http://mail.python.org/mailman/listinfo/python-list
Re: Help Create Good Data Model
The Queue won't work for this app, because it removes the data from the Queue when you query (the way I understand it). -- http://mail.python.org/mailman/listinfo/python-list
Re: Help Create Good Data Model
fumanchu wrote: > If you used a Queue, it wouldn't be the container itself; rather, it > would be a gatekeeper between the container and consumer code. A > minimal example of user-side code would be: > > class Request: > def __init__(self, op, data): > self.op = op > self.data = data > self.reply = None > req = Request('get', key) > data_q.put(req, block=True) > while req.reply is None: > time.sleep(0.1) > do_something_with(req.reply) > > The container-side code would be: > > while True: > request = data_q.get(block=True) > request.reply = handle(request) > > That can be improved with queue timeouts on both sides, but it shows > the basic idea. > > > Robert Brewer > System Architect > Amor Ministries > [EMAIL PROTECTED] I get it. That's cool. Here's the latest incarnation of this code. I haven't implemented the lock you suggested yet, although this version seems to be working fine. #!/usr/bin/python # author mwt # Mar '06 import copy, threading, observable class FAHData(observable.Observable): """The data model for the [EMAIL PROTECTED] monitor.""" def __init__(self): observable.Observable.__init__(self) self.data = {}#this dict will hold all data self.mutex = threading.RLock() def get_all_data(self): """Returns a COPY of entire data dict.""" #not sure deepcopy() is really necessary here #but using it for now #might cause some weird synchronization problems... self.mutex.acquire() try: return copy.deepcopy(self.data) finally: self.mutex.release() def get_data(self, key): """Returns a COPY of data element.""" self.mutex.acquire() try: return copy.deepcopy(self.data[key]) finally: self.mutex.release() def set_value(self, key, value): """Sets value of data element.""" self.mutex.acquire() try: self.data[key] = value observable.Observable.notifyObservers(self, event = 'VALUE_CHANGED', key = key) finally: self.mutex.release() def set_values(self, values): """Sets a list of values""" self.mutex.acquire() try: for value in values: self.data[value] = values[value] #print 'values input are: %s' %self.data observable.Observable.notifyObservers(self, event = 'VALUES_CHANGED', keys = values.keys()) finally: self.mutex.release() def set_data(self, data): """Sets entire data dictionary.""" self.mutex.acquire() try: self.data = data observable.Observable.notifyObservers(self, event = 'DATA_SET') finally: self.mutex.release() def clear_data(self): """Clears entire data dictionary.""" self.mutex.acquire() try: self.data = {} observable.Observable.notifyObservers(self, event = 'DATA_CLEARED') finally: self.mutex.release() -- http://mail.python.org/mailman/listinfo/python-list
Little Help with Exceptions and ConfigParser
Hi - I'm having a little trouble using exceptions properly. I currently have an initialization function on a little app that looks like this: def __init__(self, config_file): self.fahdata = fahdata.FAHData() self.INI = ConfigParser.ConfigParser() if os.path.exists(config_file): try: self.INI.read(config_file) except ConfigParser.ParsingError: print "Cannot parse configuration file!" This is just a hack I came up with to load the configuration file and try to test for serious problems with it. Trouble is, it doesn't really do that, and furthermore, if there are small problems with the file (such as a NoSectionError) the whole program bombs. Instead, I would like to test for the relevant Errors and Exceptions, and have the program deal with them gracefully. Would one of you gurus be so kind as to point me in the right direction here? I know that there are many possibilities, but what would be a sane and reasonable set of exceptions to throw? Meanwhile, I will be reading pages 337-339 in my copy of Python in a Nutshell to try to get clearer about exceptions and how to use them. Thanks. mwt -- http://mail.python.org/mailman/listinfo/python-list
Re: Little Help with Exceptions and ConfigParser
Whoops. Those page numbers are from "Cookbook." As well as the Except section in "Nutshell." Still stewing away here. ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: Little Help with Exceptions and ConfigParser
Would something like this be more useful? def __init__(self, config_file): self.fahdata = fahdata.FAHData() self.INI = ConfigParser.ConfigParser() if os.path.exists(config_file): try: self.INI.read(config_file) except ConfigParser.Error, err: print "Cannot parse configuration file. %s" %err except IOError: print "Problem opening configuration file. %s" %err except Error: print "Problem with with configuration file. %s" %err -- http://mail.python.org/mailman/listinfo/python-list
Re: Little Help with Exceptions and ConfigParser
(Whoops, again.) def __init__(self, config_file): self.fahdata = fahdata.FAHData() self.INI = ConfigParser.ConfigParser() if os.path.exists(config_file): try: self.INI.read(config_file) except ConfigParser.Error, err: print "Cannot parse configuration file. %s" %err except IOError, err: print "Problem opening configuration file. %s" %err except Error, err: print "Problem with with configuration file. %s" %err -- http://mail.python.org/mailman/listinfo/python-list
Parsing Hints
Hi - I'm working on parsing a file that has data that looks like the sample below. Obviously, I can't just split the string by colons. I'm pretty new to regex, but I was thinking of something that would essentially "split" by colons only if the are preceded by alpha characters -- thus eliminating problems of splitting up times, etc. Still, I'm nagged by the spectre of you gurus knowing a powerful way to approach this problem. Am I on the right track here with this regex idea? Any hints as to the sanest angle on parsing this would be appreciated. Thanks. Here's a sample of the data: Index 4: folding now server: 171.65.199.158:8080; project: 1809 Folding: run 17, clone 19, generation 35; benchmark 669; misc: 500, 400 issue: Wed Mar 15 18:32:19 2006; begin: Wed Mar 15 18:32:25 2006 due: Fri Apr 28 19:32:25 2006 (44 days) core URL: http://www.stanford.edu/~pande/Linux/x86/Core_82.fah CPU: 1,0 x86; OS: 4,0 Linux assignment info (le): Wed Mar 15 18:32:19 2006; A0F3AAD2 CS: 171.65.103.100; P limit: 5241856 user: MWT; team: 0; ID: 1A2BFB75B7B; mach ID: 2 work/wudata_04.dat file size: 82814; WU type: [EMAIL PROTECTED] Average download rate 97.552 KB/s (u=4); upload rate 38.718 KB/s (u=3) Performance fraction 0.950453 (u=3) -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing Hints
OK. I think the solution was much easier than I thought. The key is the semicolon. I'm doing it in 3 steps: 1) Break string into 13 lines 2) Split each line by the semi-colon 3) Ummm... done already. Time to wake up. ;) -- http://mail.python.org/mailman/listinfo/python-list
"Locate" command in Python
Is there a function in python that does what "locate" does in a bash shell? I know I could do it by using os.popen('locate'), but I'm curious if there's a Python "native" way to go about it. Only needs to work in Unix, but would be interesting if it was cross-platform. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: "Locate" command in Python
On my system, files.cache ended up being 45.9 Mb (800,000+ lines)! Pretty fun script. I can imagine some interesting variations. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter vs PyGTK
I've had a good experience with Pygtk. I made a small app called "Protein Think" that monitors a [EMAIL PROTECTED] client. The front end is extremely simple - basically a notebook with five small pages. It runs well and was a breeze to create. Looks much better than Tkinter, if that matters at all to you. -- http://mail.python.org/mailman/listinfo/python-list