Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python
help with my first use of a class
 

Index | Next | Previous | View Flat


rpdooling at gmail

Oct 19, 2006, 11:00 PM


Views: 685
Permalink
help with my first use of a class

I am a mere hobbyist. Spent several hours trying to make a class,
because I think this is an occasion where I need one. But I can't make
it work.

This code "works" (only because of the global c, which I know I'm
supposed to avoid, by using a Class). I edited the rest to leave out
the irrelevant formatting and printing of the quotations.

I've read about Classes several times, but I don't "get" them yet.
Obviously. If I can solve one real life problem like this, then maybe
I'll see the light.

If I understand the power of Classes correctly, I could make one that
would allow me to make a new instance that would connect to, say, an
SQLite3 db instead of the Access db, as well as to create more methods
that will do different SQL searches.

Thank you for any help,

rd

--------------------------------------

import mx.ODBC.Windows as odbc
import sys
import random

def connect():
global c
db='DSN=Quotations'
conn = odbc.DriverConnect(db)
c = conn.cursor()

def random_quote():
"""
Counts all of the quotes in MS Access database Quotations2005.mdb.
Picks one quote at random and displays it using textwrap.
"""
c.execute ("SELECT COUNT(Quote) FROM PythonQuoteQuery")
# Yields the number of rows with something in the quote field
total_quotes = c.fetchone()
# Get a random number somewhere between 1 and the number of total
quotes
quote_number = (random.randint(1, total_quotes[0]),)
# Select a quote where the ID matches that number
c.execute ("SELECT Author, Quote FROM PythonQuoteQuery WHERE ID=?",
quote_number)
quote = c.fetchone()
blah blah blah

def print_quote()
code to format and print the quote (which will also have to be
global, unless I learn Classes!)


if __name__ == '__main__':
if len(sys.argv) == 1:
connect()
random_quote()
print_quote()

--
http://mail.python.org/mailman/listinfo/python-list

Subject User Time
help with my first use of a class rpdooling at gmail Oct 19, 2006, 11:00 PM
    Re: help with my first use of a class rpdooling at gmail Oct 19, 2006, 11:32 PM
    Re: help with my first use of a class jstroud at mbi Oct 19, 2006, 11:34 PM
        Re: help with my first use of a class rpdooling at gmail Oct 19, 2006, 11:40 PM
        Re: help with my first use of a class fulvio at tm Oct 20, 2006, 5:59 AM
    Re: help with my first use of a class martin.witte at gmail Oct 19, 2006, 11:34 PM
    Re: help with my first use of a class onurb at xiludom Oct 20, 2006, 2:17 AM
        Re: help with my first use of a class fredrik at pythonware Oct 20, 2006, 2:27 AM
        Re: help with my first use of a class rpdooling at gmail Oct 20, 2006, 5:49 AM
    Re: help with my first use of a class onurb at xiludom Oct 20, 2006, 2:19 AM
    Re: help with my first use of a class onurb at xiludom Oct 20, 2006, 4:19 AM
    Re: help with my first use of a class rpdooling at gmail Oct 20, 2006, 5:53 AM
    Re: help with my first use of a class onurb at xiludom Oct 20, 2006, 8:17 AM
    Re: help with my first use of a class jstroud at mbi Oct 20, 2006, 11:01 AM
        Re: help with my first use of a class simon at brunningonline Oct 20, 2006, 11:12 AM
        Re: help with my first use of a class fulvio at tm Oct 21, 2006, 4:40 AM
    Re: help with my first use of a class bruno at modulix Oct 22, 2006, 3:34 AM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.