Re: [Tutor] handling of tabular data
> with the little I know about classes, I assume that then I would have > a list of class instances as representation of my tabular data > but given such a list of class instances, i would still need for > loops to get to e.g. the minimal value of a certain attribute in all > classes in that list. OR? OR you could define class level member for the stats and have the constructors update those values at instance creation time. A very simple totaling example: class Number: total = 0 def __init__(self, n): self.value = n Number.total += n def __del__(self): Number.total -= self.value # now create some instances: numbers = [] for n in range(10): numbers.append(Number(n)) print Number.total > and what would be the benefit of using classes then? Actually not that much for this kind of app and personally I rarely use OOP for MIS type functions, SQL is usually more powerful, either direct;ly or mixed with a scripting language like Python. But if the class could be used for other functions within the application - ie MIS was only one aspect - then classes could be a very useful single mechanism to crack both problems in one go. 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] code improvement for beginner ?
Danny Yoo wrote: > Looking at pageimgs(): I'm not sure what 't' means in the open statement: > > f = open(filename, "rt") > > and I think that 't' might be a typo: I'm surprised that Python doesn't > complain. Can anyone confirm this? I think you may have tried to do "r+" > mode, but even then, you probably don't: you're just reading from the > file, and don't need to write back to it. It's not a typo. 't' opens the file in text mode. It doesn't make any difference on Unix, but it does on Windows. As you might know, lines in text file end with '\r\n' in Windows rather than only '\n'. If you open a file in text mode, the line endings are translated to '\n' on input and back to '\r\n' on output. One can also specify 'b' instead to open the file in binary mode instead, which will not do any translation. I'm not sure which one of the two is the default. -- If I have been able to see further, it was only because I stood on the shoulders of giants. -- Isaac Newton Roel Schroeven ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] handling of tabular data
Hi Frank, you can use Numeric.py it is also powerfull to handle averages, min, max, matrix algebra, etc.. see: http://www.pfdubois.com/numpy/ You just need to use a list. If your data is big and you need more power I suggest you use database like mysqldb for python. It is also fun to combine database and python all together. Cheers, pujo On 10/8/05, Frank Hoffsümmer <[EMAIL PROTECTED]> wrote: HelloI often find myself writing python programs to compute averages, min,max, top10 etc of columns in a table of data In these programs, I always capture each row of the table in a tuplethe table is then represented by a list of tuplescomputing averages, min, max and other meta-information is then donewith for loops or some list comprehension. now I wonder, whether I shouldn't be using classes instead of liststo capture the table rowswith the little I know about classes, I assume that then I would havea list of class instances as representation of my tabular data but given such a list of class instances, i would still need forloops to get to e.g. the minimal value of a certain attribute in allclasses in that list. OR?and what would be the benefit of using classes then? what is the best practice, can anyone shed some light on thisthanks-frank___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] AttributeError: 'str' object has no attribute 'geturl'
Javier wrote: > Class Data has a class attribute, 'f', defined as an empty string. Check if > Data.f is initialized before calling getImg. > Also, why initialize with an empty str? Put a None in there or don't define > the attribute at all. > A tip: in line 126 there is a print Data.f. This will show nothing in this > case. When printing for debugging, error reporting or user input, always > place quotes around values so that it is easier to spot empty strings or non > printable characters. > Finally, keep in mind that you can always use pdb to see what's up. > >Javier > > Thanks, I'll try to clean up what had gotten past me. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] AttributeError: 'str' object has no attribute 'geturl'
Ok, new version (sorry to bug you). This time I've edited the program so that you can only download todays (it won't work for any other date). Now I can connect to the server but it sticks on "Downloading image!" Thanks for your prevoius help. Joe #! /usr/bin/env python ## # Created by Joseph Quigley # # This program is under the GNU GPL Licence # # Either version 2 or any higher version. # # Garfield and the Garfield trade mark are # # Copyrighted by Paws Inc. # ## # Import modules import time import urllib2 import os import sys from Tkinter import * import Image import ImageTk import getpass class Data: # Define time and date todays_date = time.localtime(time.time()) todayDay = time.strftime("%d", todays_date) todayMonth = time.strftime("%m", todays_date) todayYear = time.strftime("%y", todays_date) todayYearFull = time.strftime("%Y", todays_date) getDate = None month = None day = None yearFull = None year = None # More file location junk stripName ="http://images.ucomics.com/comics/ga/2005"# % (yearFull) todayStrip = "ga051009.gif"#ga%s%s%s.gif" % (todayYear, todayMonth, todayDay) otherStrip = None download = None f = None VERSION = '0.9.3' try: if sys.argv[1] == "--version": print """\nGacor %s A Garfield Comic Downloader and reader. Copyright (C) 2005 Joseph Quigley This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n""" % Data.VERSION raise SystemExit except IndexError: print "\n" * 100 user = getpass.getuser() name = 'gacor' if os.name == 'posix': imgDir = '/home/%s/.gacor/' % user elif os.name == 'nt': if os.path.exists('C:\Documents and Settings'): imgDir = "C:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('D:\Documents and Settings'): imgDir = "D:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('E:\Documents and Settings'): imgDir = "E:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('F:\Documents and Settings'): imgDir = "F:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('G:\Documents and Settings'): imgDir = "G:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('H:\Documents and Settings'): imgDir = "H:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('I:\Documents and Settings'): imgDir = "I:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('J:\Documents and Settings'): imgDir = "J:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('K:\Documents and Settings'): imgDir = "K:\\Documents and Settings\\%s\\.gacor\\" % user else: setImgDir = raw_input("Please specify your Documents and Settings Drive\n(eg: C:\\ not C)\n> ") imgDir = "%sDocuments and Settings\\%s\\.gacor\\" % user if not os.path.exists(imgDir): os.mkdir(imgDir) def Connect(): print "Connecting to server..." try: Data.f = urllib2.urlopen("%s/%s" % (Data.stripName, Data.todayStrip)) except urllib2.URLError: print """Temporary failure in name resolution. This means that you may not be online or the site is down. You may want to try again later.\n\n This could also mean that you have entered a bad date in gettoher.""" print "\n\nProgram will quit in 3 seconds." time.sleep(3) raise SystemExit print "Connected." def Dsp_Image(pic): root = Tk() root.title("GaCoR (Garfield Comic Reader)") app = Frame(root) app.grid() img = Image.open(pic) imgPrep = ImageTk.PhotoImage(img) imgShow = Label(app, image=imgPrep).grid() info = Label(app, text="Displaying a Garfield for %s\%s\%s." % (Data.month, Data.day, Data.year)).grid() app.mainloop() def getImg(pic): print "Dowloading Image" Data.f.geturl() pict_Data = Data.f.read() pict = file("%s" % (pic), 'w') pict.write(pict_Data) pict.close() print "Finished Download" def ShowImg(pic): Dsp_Image("%s%s" % (imgDir, pic)) def comicDate(): while True: print "\nEnter comic date in mmdd format (eg: 09122002 "
[Tutor] Code Inspection
Hey list, I've been working on my spider program (albeit not very much :P) and I would appreciate it if I could get some comments on the code. I'm fairly sure I haven't chosen the best method to do what I want to do, but if we can just assume that I have, that'll make things easier. ;) In particular, comments on the "Things to do" list at the end would be much appreciated. The file is attached. Cheers, Dan #!/usr/bin/python ### sPyder - A program designed to retrieve a particular, related set of web pages, using a spider-type search. ### sPyder is released under the GPL. import re import urllib import distutils.dir_util import os from optparse import OptionParser DEFAULT_DEFS = '[cf]=".*html"|c=".*jpg"|c=".*gif"|c=".*bmp"|[cf]="/\w*/' # These should be moved to a configuration file, ideally. def main(): ### This is the class which brings all of the other classes together, as well as providing the command line parsing interface. # This is all command line parsing usage = "usage: %prog -s -d -f -defs " parser = OptionParser(usage=usage) parser.add_option("-s","--site",help="the site to be copied (must start with 'http://'") parser.add_option("-l","--local_destination",dest="localdir",help="the destination the site will be copied to") parser.add_option("-f","--file",dest="source",help="the file to start at (assumed to be 'index.html' if not specified)",default="index.html") parser.add_option("-d","--definitions",help="a file containing definitions of what files to copy (reverts to defaults if not specified)",default=None) (options, args) = parser.parse_args() # Preparing and tidying up the input for use by the actual fetching classes (copy_items) site = options.site localdir = options.localdir source = options.source if options.definitions: definitions = options.definitions # This is not implemented correctly, it is just a placeholder. else: definitions = DEFAULT_DEFS if not re.search('/\Z',site): site = site + "/" if not re.search('/\Z',localdir): localdir = localdir + "/" if site and localdir: copy_items(site,localdir,source,definitions) def get_page_items (site,localdir,source,defs): ### This class returns a list of all the items within a page which fit the definitions. next = [] text = urllib.urlopen(site+source).read() if re.search(defs,text): for i in re.findall(defs,text): i = i[3:-1] if re.search('/\w*/\Z',i): j = os.listdir(site+i) next.append(j) else: next.append(i) src = [source] + next print src return src def get_list (site,localdir,source,defs): ### This class returns a list of the pages which need to be copied. items = [] next = get_page_items (site,localdir,source,defs) for i in next: if i not in items: items.append (i) next.extend (get_page_items (site,localdir,i,defs)) return items def copy_items (site,localdir,source,defs): ### This class copies the list of files which has been compiled. items = get_list(site,localdir,source,defs) for i in items: distutils.dir_util.create_tree(localdir, items) original = urllib.urlopen(site+i) local = open(localdir+i,'w') body = original.read() local.write(body) local.close() main() #Things to do: # - Create 'definitions' file # - Create support for straight references to a folder # - Look into compiling RE for greater efficiency ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] subclass problem: __names and type-checking
Python said unto the world upon 2005-10-08 12:32: Thanks for the response, Llyod. (And to Alan, too.) > I think that a sub-class *needs* to support the same programming > interface as the parent class. > If B inherits from A then every context where A or an A instance appears > should work correctly with B or a B instance. Since the B constructor > *requires* more arguments, it violates that ideal. In other words, it > would be OK to allow additional arguments. It is not OK to require > them. The code I posted was an abstracted example. What I actually have is a base class with 2 arguments with default values and the subclass having those 2 (different defaults) and two more, again with defaults. So, unless I misunderstand, what I have meets the ideal here. > In other words sub-class should really be a different class that adapts > or possibly acts as a proxy for the _BaseClass. Obviously you have > picked names that presuppose inheritance. > > I've abused inheritance in the past in an attempt to reuse code and have > usually regretted it. My actual context is a pretty central case of inheritance, I think. I'm making a Counter class will all sorts of bells and whistles that I've found myself deploying in other programs. Counter inherits from a sparse _Basic_Counter class. _Basic_Counter gives the common minimal interface I want, while not including the CPU-cycle consuming bells added in the subclass Counter. I'm doing things like this because usually the cpu-cycles for the bells won't matter. But, with _Basic_Counter, for the cases where I do want something more efficient, the basic interface will remain the same. So, I believe that your good advice was already being headed. Thanks, though! Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] subclass problem: __names and type-checking
Python said unto the world upon 2005-10-08 12:40: > Traceback (most recent call last): >File "C:/Python24/foofoofoo.py", line 26, in -toplevel- > s2 = Sub2() >File "C:/Python24/foofoofoo.py", line 22, in __init__ > super(Sub2, self).__init__() >File "C:/Python24/foofoofoo.py", line 10, in __init__ > if type(self) == __TwoUnderBase: # What to write here > NameError: global name '_TwoUnderBase__TwoUnderBase' is not defined > > Within a class, __ prefixes mangle the name to include the class name as > part of the name. However, it appears that within a class statement, > the __ prefix is not mangled. This is good for you because otherwise > the inheritance would not work. However, it appears to make it > impossible to directly reference the class name from within the class. > You may want to rethink the idea of using __ prefixes in the class name. > Normally __ is used to "privatize" attributes within a class. Hi all, I did give up the use of __ names in the case at hand. But I just posted a question about how to reference a __ class name from with the class on comp.lang.python. I used the code up-thread here. The F-bot suggested: class __TwoUnderBase(object): def __init__(self): if self.__class__.__name__ == "__TwoUnderBase": print "From __TwoUnderBase" else: print "From subclass", which does the trick. Thought I'd post it here in case of future googling. Best to all, Brian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Regex help
I'm looking to get the size (width, length) of a PDF file. Every pdf file has a 'tag' (in the file) that looks similar to this Example #1 MediaBox [0 0 612 792] or this Example #2 MediaBox [ 0 0 612 792 ] I figured a regex might be a good way to get this data but the whitespace (or no whitespace) after the left bracket has me stumped. If I do this pattern = re.compile('MediaBox \[\d+ \d+ \d+ \d+') I can find the MediaBox in Example #1 but I have to do this pattern = re.compile('MediaBox \[ \d+ \d+ \d+ \d+') to find it for Example #2. How can I make *one* regex that will match both cases? Thanks for the help, Bill ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] is there any Python code for spatial tessellation?
There are four points with coordinates: 2,3;4,9;1,6;3,10. How to use Python to draw one perpendicular bisector between (2,3) and (4,9); the other perpendicular bisector between (1,6)和(3,10); then, makes the output like: l1 a b c l2 a b c (Note: l indicates the perpendicular bisector with equation ax + by = c.) Plus the intersection coordinates of the two perpendicular bisectors: x,y On 10/8/05, Danny Yoo <[EMAIL PROTECTED]> wrote: > > > On Sat, 8 Oct 2005, Shi Mu wrote: > > > is there any Python code for spatial tessellation? > > Are you looking for code to generate voronoi diagrams? > >http://en.wikipedia.org/wiki/Voronoi_diagram > > From initial Google searches, it appears that there is a package called > Qhull that pepole use to do Voronoi tesselations. > >http://www.qhull.org/ > > and Chris Myers has written a module around Qhull: > >http://www.tc.cornell.edu/~myers/PyXL/ > > > Otherwise, I don't think we at Tutor can help you that much; you may want > to ask on a more math-oriented forum. Good luck to you! > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] line question
There are four points with coordinates: 2,3;4,9;1,6;3,10. How to use Python to draw one perpendicular bisector between (2,3) and (4,9); the other perpendicular bisector between (1,6)和(3,10); then, makes the output like: l1 a b c l2 a b c (Note: l indicates the perpendicular bisector with equation ax + by = c.) Plus the intersection coordinates of the two perpendicular bisectors: x,y ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regex help
At 09:10 PM 10/9/2005, Bill Burns wrote: >I'm looking to get the size (width, length) of a PDF file. Every pdf >file has a 'tag' (in the file) that looks similar to this > >Example #1 >MediaBox [0 0 612 792] > >or this > >Example #2 >MediaBox [ 0 0 612 792 ] > >I figured a regex might be a good way to get this data but the >whitespace (or no whitespace) after the left bracket has me stumped. > >If I do this > >pattern = re.compile('MediaBox \[\d+ \d+ \d+ \d+') > >I can find the MediaBox in Example #1 but I have to do this > >pattern = re.compile('MediaBox \[ \d+ \d+ \d+ \d+') > >to find it for Example #2. > >How can I make *one* regex that will match both cases? pattern = re.compile('MediaBox \[ *\d+ \d+ \d+ \d+') ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regex help
If the format is consistent enough, you might get away with something like: >>> p = re.compile('MediaBox \[ ?\d+ \d+ (\d+) (\d+) ?\]') >>> print p.search(s).groups() ('612', '792') The important bits being: ? means "0 or 1 occurences", and you can use parentheses to group matches, and they get put into the tuple returned by the .groups() function. So you can match and extract what you want in one go. http://www.amk.ca/python/howto/regex/ is a fairly gentle introduction to regular expressions in Python if you want to learn more. Having said all that, usually you would use a library of some sort to access header information, although I'm not sure what Python has for PDF support, and if that's -all- the information you need, and the -only- variation you'll see, regex probably won't be too bad :) On 10/9/05, Bill Burns <[EMAIL PROTECTED]> wrote: I'm looking to get the size (width, length) of a PDF file. Every pdffile has a 'tag' (in the file) that looks similar to thisExample #1MediaBox [0 0 612 792]or thisExample #2MediaBox [ 0 0 612 792 ] I figured a regex might be a good way to get this data but thewhitespace (or no whitespace) after the left bracket has me stumped.If I do thispattern = re.compile('MediaBox \[\d+ \d+ \d+ \d+') I can find the MediaBox in Example #1 but I have to do thispattern = re.compile('MediaBox \[ \d+ \d+ \d+ \d+')to find it for Example #2.How can I make *one* regex that will match both cases? Thanks for the help,Bill___Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor