[Tutor] range/for list change behavior

2008-09-29 Thread Don Parris
Hi all,

After a rather long (and unfortunate) break from tinkering with Python, I am
back at it.  I am working through the book Learning Python (based on 2.2/2.3
- I use 2.5), and in the chapter on while/for loops, ran across the
following example:

 L = [1, 2, 3, 4, 5]
 for i in range(len(L)):
... L[1] += 1 # this is a typo I made - should
have been L[i], not L[1].
...
 L
[1, 7, 3, 4, 5]

I did correct my typo, but what I do not understand is how range arrived at
a '7', where the '2' should be.  My best guess is that L[1] is treated as
the index of the value '2'.  I hope that learning how my error affected the
result will help me grasp the concept a little better.

Thanks!
Don
-- 
D.C. Parris
Minister, Journalist, Free Software Advocate
https://www.xing.com/profile/Don_Parris
http://www.linkedin.com/in/dcparris
sip:[EMAIL PROTECTED] [EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Replacement for 'Find' Module

2005-08-15 Thread Don Parris
On my Windows XP box running Python 2.4, I attempted to use the 'find'
module per the example in Programming Python (Ch. 2) - i.e.:
import find
find.find('*')

However, Python didn't find the find module.  The docs say it's now
deprecated, but don't point to what tool should be used to replace
find.  My Googling efforts haven't been fruitful so far.  Could
someone please point me in the right direction?

Don
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
[EMAIL PROTECTED]
Hey man, whatever pickles your list!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Question About chdir()

2005-08-08 Thread Don Parris
The book, Programming Python, shows an example of os.chdir() on the
Windows platform, as follows:

os.chdir(r'c:\temp')

What's the 'r' for?  It didn't seem to make any difference in how
Python works - at least not on the surface.

Thanks,
Don
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
[EMAIL PROTECTED]
Hey man, whatever pickles your list!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with pgbd datetime

2005-07-28 Thread Don Parris
On 7/28/05, Gabriel Farrell [EMAIL PROTECTED] wrote:
 On Wed, Jul 27, 2005 at 07:13:49PM -0400, Don Parris wrote:
  pg/pgdb are both part of the PyGreSQL distribution.  You can choose
  whichever you want to use, but pgdb supports the 2.0 API, whereas
  (according to the readme) pg does not.  I will try pg when I get back
  to my box tonight, but would prefer to use pgdb.
 
 I use pgdb as well, for the same reason.
 
  All of this should have been configured properly when I installed
  SUSE Linux 9.2 last Fall.  The MySQLdb  module works fine - the pgdb
  should as well.  Perhaps I should post this over on the DB Sig list.
 
 Debian lists a package called python-egenix-mxdatetime as a dependency
 for python-pygresql, and that in turn depends on
 python-egenix-mxtools.  I believe you need to install these modules
 for pygresql to work, as mxDateTime is not the same as datetime.  The
 site for mxDateTime is
 http://www.egenix.com/files/python/mxDateTime.html if you need to
 install it manually, but there should be a SUSE package for it.
 *Quick look through SUSE site.*  Yep, here it is:
 http://www.novell.com/products/linuxpackages/professional/python-egenix-mx-base.html
 
 I hope that gets it working.
 
 gsf
 ___

I would have thought it would be part of the SUSE install.  Thanks for
the URL.  I've downloaded the egenix package from their website, and
will try to get it installed this evening.

Don
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
[EMAIL PROTECTED]
Hey man, whatever pickles your list!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with pgbd datetime

2005-07-27 Thread Don Parris
On 7/27/05, Gabriel Farrell [EMAIL PROTECTED] wrote:
 Are you using PyGreSQL?  import pgdb works fine for me with PyGreSQL.
 I'm on Debian but I assume there's a package for it for SUSE.
 
 gsf
 
 

pg/pgdb are both part of the PyGreSQL distribution.  You can choose
whichever you want to use, but pgdb supports the 2.0 API, whereas
(according to the readme) pg does not.  I will try pg when I get back
to my box tonight, but would prefer to use pgdb.  All of this should
have been configured properly when I installed SUSE Linux 9.2 last
Fall.  The MySQLdb  module works fine - the pgdb should as well. 
Perhaps I should post this over on the DB Sig list.

Regards,
Don
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
[EMAIL PROTECTED]
Hey man, whatever pickles your list!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating MySQL table

2005-07-18 Thread Don Parris
On 7/18/05, Bernard Lebel [EMAIL PROTECTED] wrote:
 Hello,
 
 How do I create a MySQL table in Python?
 
 Here is what I'm trying:
 
 
 import MySQLdb as sql
 
 def connect2db():
   return sql.connect( blah blah blah )
 
 
 oConnection = connect2db()
 oCursor = oConnection.cursor()
 
 
 sQuery = CREATE TABLE '3DPipeline'.'TB_MT_NAME' (;\
   'ID' INTEGER UNSIGNED CHARACTER SET latin1 COLLATE latin1_swedish_ci
 NOT NULL AUTO_INCREMENT,
   'text' TINYTEXT CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
   PRIMARY KEY('ID')
 )
 ENGINE = InnoDB
 
 
Looking above, I'm not sure why there's a . after 3DPipeline.  Also,
the ; should be placed after the last command before the closing
parenthesis, not at the beginning.  I have no idea about your use of
the backslash.


 When I run that, I get this:
 
 Traceback (most recent call last):
   File stdin, line 1, in ?
   File D:\Python24\Lib\site-packages\MySQLdb\cursors.py, line 137, in 
 execute
 self.errorhandler(self, exc, value)
   File D:\Python24\Lib\site-packages\MySQLdb\connections.py, line
 33, in defaulterrorhandler
 raise errorclass, errorvalue
 _mysql_exceptions.ProgrammingError: (1064, You have an error in your
 SQL syntax; check the manual that corresponds to your MySQL server
 version for the right syntax to use near ''3DPipline'.'TB_MT_NAME' (
 'ID' INTERGER UNSIGNED CHARACTER SET latin1 COLLATE l' at line 1)
 
 
This error message points to your SQL syntax.  Notice that it wants
you to check your MySQL server version for the right syntax to use. 
That should alert you to the fact that your SQL syntax is incorrect.

HTH,
Don
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
[EMAIL PROTECTED]
Free software is like God's love - 
you can share it with anyone anywhere anytime!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating MySQL table

2005-07-18 Thread Don Parris
On 7/18/05, Bernard Lebel [EMAIL PROTECTED] wrote:
 Hello,
 
 How do I create a MySQL table in Python?
 
 Here is what I'm trying:
 
 
 import MySQLdb as sql
 
 def connect2db():
   return sql.connect( blah blah blah )
 
 
 oConnection = connect2db()
 oCursor = oConnection.cursor()
 
 
 sQuery = CREATE TABLE '3DPipeline'.'TB_MT_NAME' (;\
   'ID' INTEGER UNSIGNED CHARACTER SET latin1 COLLATE latin1_swedish_ci
 NOT NULL AUTO_INCREMENT,
   'text' TINYTEXT CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
   PRIMARY KEY('ID')
 )

O.k., it looks like 3DPipeline is your database name, and TB_MT_NAME
is your table name.  Your single quotes, if needed at all, most likely
need to look like:
'3DPipeline.TB_MT_NAME'(\
fieldone key1 key2 key3\
fieldtwo key1 key2 key3\
fieldthree key1 key2 key3
);

If you're using the backslash for line continuation, you need to add
the \ at the end of each line in your SQL statement.

Hope this is a bit more helpful.
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
[EMAIL PROTECTED]
Free software is like God's love - 
you can share it with anyone anywhere anytime!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating MySQL table

2005-07-18 Thread Don Parris
On 7/18/05, Bernard Lebel [EMAIL PROTECTED] wrote:
 See [Bernard]
 

 
 [Bernard] Well I kind of figured it was telling it's a syntax error. ;-)
 
I didn't want to assume too much. ;)

 After trial and error for an hour or two, I managed to get it sorted.
 
 First, I noticed that in Python there is no need for a semi-colon
 terminator (even for SQL commands).
 Also, removing all quotes except the opening and closing one made the code 
 work.
 
That was where my last post was headed - and I left out the part about
the semi-colons not being needed.  I was looking at my own select
queries.

 The SQL book I am using uses DOS command line syntax, not Python, so I
 had to translate the examples into Python syntax.
 
I got Python books.  I can figure out the SQL stuff pretty easily -
it's programming the front-end that twists my brain around. ;)

Don
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
[EMAIL PROTECTED]
Free software is like God's love - 
you can share it with anyone anywhere anytime!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OT python Licences

2005-07-12 Thread Don Parris
On Tue, 12 Jul 2005 10:49:22 +0100
Dave S [EMAIL PROTECTED] wrote:

 This is a bit OT but here goes.
 
 My work wants me to write a fairly large python script to analyze some
 technical ASCII data files. Python and its libraries are GPL.
 
 That being the case am I right in thinking that my script would also
 have to be GPL and I would have to inform my employer as I hand it over ?
 
 Secondly it would have to run in Windows, The results could pop up on a
 DOS window. However I was looking at QT until I read the Windows
 license. Are there any widget libraries that would allow me to program
 for windows commercially without any license or fees ?
 
 Thanks in advance
 Dave
 
 

While Python is not GPL'ed, any work you do release under a GPL license can
be used in-house without being redistributed.  In a sense, that makes it
proprietary, but it's still libre for the user - the main point of the GPL. 
As long as the program is not being redistributed, the GPL does not apply. 
IOW, your company is not forced to release the code.  As soon as your
employer distributes the code outside the company (public release), it falls
under the terms of the GPL.  

The GPL FAQ is available at the FSF website, and offers some excellent
answers to interesting questions.  As stated in another e-mail the current
Python license is compatible with the GPL.  There were a couple of versions
of Python (1.?.?) that the FSF considered incompatible.  The GPL is
copyleft, the Python License is a non-copyleft license, meaning that the
code can be made proprietary.

Don, who is preparing for his presentation on FOSS licenses at next month's
CharLUG meeting. :-)
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://www.lulu.com/dcparris
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Passing Arguments

2005-07-09 Thread Don Parris
For some reason, everytime I think I finally understand the passing
arguments thing, I prove that I don't.  However, I *am* getting a
little closer to getting it.

# Here I just created a couple of variables to print, and then
prt_Name returns g.
def prt_Name():
g = 'foo'
a = 'bar'
print g
print a
return g

# get_Name will get the argument from prt_Name:
def get_Name(b):
print 'f'
print b

get_Name(prt_Name)
# Calling get_Name with prt_Name as the argument produces this:

C:\Python24python scripts\learning.py
f
function prt_Name at 0x00AEBEF0

At least it found the function.  That's little shy of a miracle for
me. :)  However, I would like to see 'foo' printed out, since it is
the result of the function.  How can I correct this?

Thanks,
Don
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
[EMAIL PROTECTED]
Free software is like God's love - 
you can share it with anyone anywhere anytime!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionary values

2005-07-08 Thread Don Parris
On 7/8/05, luke p [EMAIL PROTECTED] wrote:
 just assume all the below code is correct.
 I am not having a problem with it, it is all for example only.
 
 I have a dictionary like this:
 alpha = {'a':0,'b':0, ... 'z':0}
 and the following code
 f = file(hamlet.txt,r)
 text = f.readlines()
 f.close()
 for line in text:
   for char in line:
 try:
   alpha[char] += 1
 
 so at the end of the loop I have a dictionary eg.
 {'a':14000,'b':12000 ... 'z':100}
 
 what I want to do is find out which value in my dictionary is lowest.
 is there a dictionary function for this, like alpha.min() that will
 return a key:value pair of the lowest? I cannot find one and I
 wondered if there was a quick fix to this.
 
 what I will do instead to find the lowest is just use a list instead...
 
 alphalist = ['a','b' ... 'z']
 alphavalues = [0,0 ... 0]
 lowest = alphavalues[0]
 lowestlocation = 0
 and just do
 for x in range(26):#or is it 25? can't remember if value is included
   if alphavalues[x]  lowest:
 lowest = alphavalues[x]
 lowestlocation = x
 
 but for future reference I just wondered about the dictionary thing.
 thanks in advance.
 -Luke
 ___


I'm new at this, but thought I would throw my $0.02 in the ring for
the learning experience.  I know that sequence operations won't work
on dictionaries.  I wonder if you could get away with a lambda here? 
I'm probably in way over my head, but would something like this work:

min = (lambda x, y: x  y)
min(1, 9)

Could min() take the dictionary values, as in min(dict[0], dict[9])?

Again, I'm no expert, but having my input picked apart will be a good thing. ;)

Don
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
[EMAIL PROTECTED]
Free software is like God's love - 
you can share it with anyone anywhere anytime!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Handling Unpickled Data

2005-07-05 Thread Don Parris
Greetings,

I have a script that gets a little info from the user, using a
dictionary, which I then store in a file via cPickle.  This pickle
concept must be good for something, but I haven't figured out exactly
what just yet.  At any rate, I would like to now get the code and be
able to display only one value for the user.

I thought I could do this by specifying a key in orginfo.  However,
the function throws all the data to stdout, not just the desired key. 
How can I limit the output to just the value of the key I want to
reference?  Do I need to create a for or while loop, or an if
statement to say, extract this key:value pair only?

I can also get this from a SQL query, but since I couldn't do it with
a file, I wanted to try and figure it out.  The book, Learning
Python is very helpful, now that I have it, but it kind of breezed
past this kind of thing.  I really would like to understand how to
work with the data in the text file.

### function def ###
def org_Get_Info():
orgGdata = open('orgdata.txt', 'r')
orginfo = cPickle.load(orgGdata),
orgGdata.close()
print orginfo['Name']  # this is really all I want.

### Output at DOS prompt ###
Organization Name: ocf  # Begin input
Address: 6123
City: char
State: nc
Zip: 282
City :  char   # Begin output from org_Get_Info()
State :  nc
Name :  ocf
Zip :  282
Address :  6123
ocf # this is all I want - not the rest.


Thanks,
Don
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
[EMAIL PROTECTED]
Free software is like God's love - 
you can share it with anyone anywhere anytime!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternative File I/O for Tuples (fwd)

2005-06-29 Thread Don Parris
On Wed, 29 Jun 2005 06:38:36 -0400
Kent Johnson [EMAIL PROTECTED] wrote:

 Don Parris wrote:
  On Tue, 28 Jun 2005 05:50:54 -0400
  Kent Johnson [EMAIL PROTECTED] wrote:

SNIP

Thanks for the explanation of indentation!  I kind of understood that, but
wanted to be sure I understood correctly what was happening with the text
file inside the for loop.  And if that sounds a little loopy, well... :)

 
 'Global name rows is not defined means that Python doesn't know what
 'rows' means - you haven't assigned any value to that name. I'm surprised
 your code didn't work when you add the line rows = Results as indicated
 above; what error do you get then? But a simpler way is just to use
 Results in the call to indent:
 
 mbrPhone.write(indent(Results, hasHeader=False, separateRows=False,
   prefix='| ', postfix=' |'))
 
 What happens if you try that?
 

Well this is kind of instructive.  Assigning rows = Results and using
Results as an argument to indent() produce the same traceback message.  I
was right that the rows argument needed the value of Results, so I *am*
learning something.  However, it seems that the function doesn't like the
value I give it.

### Using Results as the argument to indent() ###
Traceback (most recent call last):
  File ekklesia.py, line 165, in ?
Main()
  File ekklesia.py, line 160, in Main
RunMenu(Menu_Main)
  File ekklesia.py, line 31, in RunMenu
if len(MenuList[sel]) == 3: MenuList[sel][1](MenuList[sel][2])
  File ekklesia.py, line 32, in RunMenu
else: MenuList[sel][1]()
  File /home/donp/python/ekklesia/ekklesia_db.py, line 63, in mbr_Phone
prefix='| ', postfix=' |'))
  File /home/donp/python/ekklesia/tbl_Tabs.py, line 24, in indent
logicalRows = [rowWrapper(row) for row in rows]
  File /home/donp/python/ekklesia/tbl_Tabs.py, line 21, in rowWrapper
newRows = [wrapfunc(item).split('\n') for item in row]
AttributeError: 'NoneType' object has no attribute 'split'
### end traceback ###

My SQL query functions return a tuple, so indent() has to recognize that
much.  I suspected this has to do with the wrapfunc argument.  I had dropped
it at some point, thinking that would help me grasp the problem.  I fed it
rows(using the rows= Results at the moment), and got this traceback:

### revised function call and traceback ###
mbrPhone.write(indent(rows, hasHeader=False, separateRows=False,
   prefix='| ', postfix=' |'))wrapfunc=lambda
   x:wrap_onspace(rows, 12))

Traceback (most recent call last):
  File ekklesia.py, line 9, in ?
from ekklesia_db import *  
  File /home/donp/python/ekklesia/ekklesia_db.py, line 64
mbrPhone.close()
   ^
SyntaxError: invalid syntax
### end function call and traceback ###

mbrPhone.close() is properly indented.  I even put the whole indent() call
on one single line.  So now I'm not sure where to go.  I know that error
messages can sometimes be misleading.  I also know that close() takes
exactly 0 arguments.  So maybe I need to look back at indent()?


  BTW, I really appreciate your patience and willingness to help me
  understand this.
 
 No problem, that's what we do here. At least on a good day :-)
 
  If I had ever dreamed that I would have a desire to program 20 years
  after the fact, I would have stopped passing notes in Math class.  I do
  it well, but hate it.  Yet, I find myself drawn further and further into
  the code, actually wanting to know more about it - why it does what it
  does.
 
 I don't know about the 'hating it' part, but you are certainly not alone
 in finding yourself fascinated with programming in Python. But snakes can
 do that, can't they? We are all trapped by its hypnotic stare... ;-)
 
You bet!

 BTW have you found a tutorial you like? There are many free Python
 tutorials, take a look at this page:
 http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
 
I've read the Python tutorial, Alan's tutorial, and have worked through some
of the others as well.  It looks simple enough, but when I try things out
for myself, I find it difficult to see how the examples apply in my
situation.  Which is why I sought out this list.  My Guess is that I need to
just do it for a while before it'll come to me.


Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternative File I/O for Tuples (fwd)

2005-06-29 Thread Don Parris
On Wed, 29 Jun 2005 12:37:45 -0400
Kent Johnson [EMAIL PROTECTED] wrote:

 Don Parris wrote:
  On Wed, 29 Jun 2005 06:38:36 -0400
  Kent Johnson [EMAIL PROTECTED] wrote:
 Don Parris wrote:

SNIP
 
 This is a tough one to interpret. The value being returned from wrapfunc()
 is expected to be a string, but instead it is None. Calling None.split()
 generates the AttributeError.
 
 Looking at wrapfunc, in defaults to an identity function that just returns
 its argument. So it looks like the tuples you get back from MySQL have
 None values in them which is confusing indent().
 
 Try with 
   wrapfunc=lambda x: x or ''
 which will convert None to an empty string, or
   wrapfunc=lambda x: str(x)
 which will convert None to the string 'None'
 
  I suspected this has to do with the wrapfunc argument.  I had dropped
  it at some point, thinking that would help me grasp the problem.  I fed
  it rows(using the rows= Results at the moment), and got this traceback:
  
  ### revised function call and traceback ###
  mbrPhone.write(indent(rows, hasHeader=False, separateRows=False,
 prefix='| ', postfix='
 |'))wrapfunc=lambda x:wrap_onspace(rows,
 12))
 
 Take another look at the line above, the commas and parens aren't right.
 
That was my own copy/paste error in the e-mail.  It doesn't look that way in
my code.  My bad.

### playing with wrapfunc (all other args are the same) ###
wrapfunc=lambda x:wrap_onspace(str(rows), x))

also
wrapfunc=lambda x:str(wrap_onspace(rows, x)))

both generate the same error:
Traceback (most recent call last):
  File ekklesia.py, line 9, in ?
from ekklesia_db import *  #Comment out this line if MySQL not
installed.  File /home/donp/python/ekklesia/ekklesia_db.py, line 64
mbrPhone.close()
   ^
SyntaxError: invalid syntax



  I've read the Python tutorial, Alan's tutorial, and have worked through
  some of the others as well.  It looks simple enough, but when I try
  things out for myself, I find it difficult to see how the examples apply
  in my situation.  Which is why I sought out this list.  My Guess is that
  I need tojust do it for a while before it'll come to me.
 
 just doing it is critical. Learning to program is like learning to write
 or learning a foreign language. It just can't be done by reading about it;
 the only way to learn is to do it. One thing to try is to just type in
 examples from a tutorial. Then think of variations on the example; try
 them out. Play with it, whatever crazy idea you have is good. This will
 give you experience with the basics and also some experience with error
 messages!
 
I realize that I'm kind of putting the cart before the horse here - trying
to accomplish a real program without understanding the tools well.  I also
feel like I should play around more with other code (the recipes and Python
programs already available).  I also think I should spend a little time
doing the line-by-line analysis of code in the snippets/programs above to be
sure I understand what I'm reading in the tutorials.  So, aside from trying
to jump to step #10 from step #1, I think I'm doing fairly well. :)

 When you become more comfortable with the basics you should start to see
 how they fit together to make useful programs. Just in the snippets we are
 working on we have touched on looping, file I/O and function calls which
 should be topics in any tutorial.
 
 Kent
 
I'll get there.

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternative File I/O for Tuples (fwd)

2005-06-29 Thread Don Parris
On Wed, 29 Jun 2005 14:09:41 -0400
Kent Johnson [EMAIL PROTECTED] wrote:

 Don Parris wrote:
  ### playing with wrapfunc (all other args are the same) ###
  wrapfunc=lambda x:wrap_onspace(str(rows), x))
  
  also
  wrapfunc=lambda x:str(wrap_onspace(rows, x)))
 
 This is way off base. wrap_onspace takes two arguments - the string to
 wrap, and the width to wrap to. You are passing it two arguments - the
 tuple of tuples to print, and the string to wrap.
 
  

Success!!!  

mbrPhone = open('mbrPhone.txt', 'w')
mbrPhone.write(indent(Results, hasHeader=False, separateRows=False,
prefix='', postfix='', justify='left', wrapfunc=lambda x:str(x)))   
mbrPhone.close()

I went back to your e-mail where you told me to use x:str(x) and re-tried
that.  I must have broken (or re-broken) something, while fixing something
else. when I tried your suggestion, it wouldn't work.  So I was trying to
fix the wrong break.  Anyway, it works beautifully- 100% accurate data:


James | Austin| 704-111-1234
Janet | Austin| 704-111-1234
James | Austin| 704-111-1234
Julie | Austin| 704-111-1234
Robert| Bates | 704-222-2345
Rachael   | Bates | 704-222-2345
Bob   | Brown | None
David | Carter| 704-111-2345

That's just a sample of the complete file.

And thanks for not giving up on me!

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternative File I/O for Tuples (fwd)

2005-06-28 Thread Don Parris
On Tue, 28 Jun 2005 05:50:54 -0400
Kent Johnson [EMAIL PROTECTED] wrote:

 Don Parris wrote:
  Just getting back to this - Mondays are always hectic.  This recipe is
  the one I saw and like.  It looks cool!  In my brief efforts tinkering
  with it, I am not really getting very far.  I saved the recipe, and
  import it into the file containing all my database functions.  In one of
  the functions, I have the following:
  
  I used the from tbl_Tabs import * approach to import the recipe.  If I
  should be doing this differently, just say so.
  
  for record in Results:
  print '%-15s\t%-15s\t%-15s' % record
  # I still want to print to screen, then...
  
  mbrPhone = open('mbrPhone.txt', 'w')
  mbrPhone.write(indent(record, hasHeader=False,
  separateRows=True)) mbrPhone.close()
 
 The problem is you are just passing one record to indent(). It processes
 the whole table at once so you have to pass the list of records, i.e.
 mbrPhone.write(indent(Results, hasHeader=False,
 separateRows=True))
 
 and do this outside the loop or you will write the file once for every
 record. So your code should look like this:
 
 for record in Results:
 print '%-15s\t%-15s\t%-15s' % record
 # I still want to print to screen, then...
 
 # Notice different indent from your code
 mbrPhone = open('mbrPhone.txt', 'w')
 mbrPhone.write(indent(Results, hasHeader=False, separateRows=True))
 mbrPhone.close()
 

O.k., I'm curious about the indentation issue here.  There is only one file,
and it has the correct information - looking at it from a text editor.  Is
my version overwriting the file every time it iterates then?  You'll get a
laugh out of this, because when I was playing with sending the file to the
printer, I did not dedent the printer call, and it did try to print a copy
of the file for each record.  

Needless to say, I learned the command lprm Saturday! I caught onto the
indentation, which fixed that.  However, I did not pay attention to the code
above - probably because I don't have 50-something files named
mbrPhone*.txt.  Go ahead and laugh, I did!

  
  I first assumed that record, and then Results could be substituted
  forrows, per the recipe.  I also tried rows, and assigning record,
  and then Results to rows.  O.k., you can laugh all you want.  I
  tried playing with the various arguments, and found that to be of little
  value.
  
  The traceback refers to a type error: iteration over a non-sequence. 
  The query returns a tuple, as I understand it.  I also understand a
  tuple to be a sequence.  If this is really as simple as the 3 lines you
  pointed out above, I know I'm missing something in the implementation.
 
 Yes, a tuple is a sequence. If you still get an error with the code above,
 please post the exact error message and traceback (copy / paste from the
 output), there is a lot of useful information in it that can be lost if
 you paraphrase.
 
 Kent
 

O.k.,

Here we go.  Global name rows is not defined.  My guess is that rows
needs to know that it should be holding Results, but maybe it should
already know that.  I tried to say rows = Results, but that did not help.
This seems to come back to the difficulty I have with passing arguments
around.

### Revised Code ###
print 'Phone List'
for record in Results:
print '%-15s\t%-15s\t%-15s' % record
# rows = Results did not work
mbrPhone = open('mbrPhone.txt', 'w')
mbrPhone.write(indent(rows, hasHeader=False, separateRows=False,
  prefix='| ', postfix=' |'))
mbrPhone.close()


### Traceback ###
Traceback (most recent call last):
  File ekklesia.py, line 165, in ?
Main()
  File ekklesia.py, line 160, in Main
RunMenu(Menu_Main)
  File ekklesia.py, line 31, in RunMenu
if len(MenuList[sel]) == 3: MenuList[sel][1](MenuList[sel][2])
  File ekklesia.py, line 32, in RunMenu
else: MenuList[sel][1]()
  File /home/donp/python/ekklesia/ekklesia_db.py, line 61, in mbr_Phone
mbrPhone.write(indent(rows, hasHeader=False, separateRows=False,\
NameError: global name 'rows' is not defined


BTW, I really appreciate your patience and willingness to help me understand
this.  If I had ever dreamed that I would have a desire to program 20 years
after the fact, I would have stopped passing notes in Math class.  I do it
well, but hate it.  Yet, I find myself drawn further and further into the
code, actually wanting to know more about it - why it does what it does.

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternative File I/O for Tuples (fwd)

2005-06-27 Thread Don Parris
On Sun, 26 Jun 2005 16:32:08 -0400
Kent Johnson [EMAIL PROTECTED] wrote:

  From: Don Parris [EMAIL PROTECTED]
  To: Danny Yoo [EMAIL PROTECTED]
  Subject: Re: [Tutor] Alternative File I/O for Tuples
  
  I guess I'm asking two closely-related questions:
  (1) how to format the file so it can be readable, and
 
 If you like the recipe I posted, this could be as simple as 
 mbrPhone = open('mbrPhone.txt', 'w')
 mbrPhone.write(indent(...appropriate args to indent...))
 mbrPhone.close()
 
 Kent
 
 ___

Kent,

Just getting back to this - Mondays are always hectic.  This recipe is the
one I saw and like.  It looks cool!  In my brief efforts tinkering with it,
I am not really getting very far.  I saved the recipe, and import it into
the file containing all my database functions.  In one of the
functions, I have the following:

I used the from tbl_Tabs import * approach to import the recipe.  If I
should be doing this differently, just say so.

for record in Results:
print '%-15s\t%-15s\t%-15s' % record
# I still want to print to screen, then...

mbrPhone = open('mbrPhone.txt', 'w')
mbrPhone.write(indent(record, hasHeader=False, separateRows=True))
mbrPhone.close()

I first assumed that record, and then Results could be substituted for
rows, per the recipe.  I also tried rows, and assigning record, and
then Results to rows.  O.k., you can laugh all you want.  I tried
playing with the various arguments, and found that to be of little value.

The traceback refers to a type error: iteration over a non-sequence.  The
query returns a tuple, as I understand it.  I also understand a tuple to be
a sequence.  If this is really as simple as the 3 lines you pointed out
above, I know I'm missing something in the implementation.


Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternative File I/O for Tuples (fwd)

2005-06-27 Thread Don Parris
On Sun, 26 Jun 2005 16:32:08 -0400
Kent Johnson [EMAIL PROTECTED] wrote:

  From: Don Parris [EMAIL PROTECTED]
  To: Danny Yoo [EMAIL PROTECTED]
  Subject: Re: [Tutor] Alternative File I/O for Tuples
  
  I guess I'm asking two closely-related questions:
  (1) how to format the file so it can be readable, and
 
 If you like the recipe I posted, this could be as simple as 
 mbrPhone = open('mbrPhone.txt', 'w')
 mbrPhone.write(indent(...appropriate args to indent...))
 mbrPhone.close()
 
 Kent
 
 ___

I tried this also:

print 'Phone List'
for rows in Results:
mbrPhone = open('mbrPhone.txt', 'w')
mbrPhone.write(indent(rows, hasHeader=False, separateRows=False,
  prefix='| ', postfix=' |'))

I suspect this is closer to what I need to use.  I still get a non-sequence
error on the traceback, though.

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Alternative File I/O for Tuples

2005-06-25 Thread Don Parris
When I pickle a SQL result into a file (ASCII mode), it lays out the data in
an interesting format.  When I send it to the printer, I'd like to see it
arranged more like it is on the console screen - in tabbed columns.  None of
the tutorial type stuff I've seen even mentions printing files, or accessing
the files you create in a Python program.  I did manage to find an example
of a print function.

Here is some sample data from the resulting file:

((S'Everybody'
S'Anonymous'
Nt(S'James'
S'Austin'
S'704-111-1234'
t(S'Janet'
S'Austin'
S'704-111-1234'

I would like to see something more like when the file is printed:

AustinJames704-111-1234
AustinJanet704-111-1234
etc.

Is this a simple task, or am I jumping into deep water? :)

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] MySQL Connection Function

2005-06-22 Thread Don Parris
On Tue, 21 Jun 2005 19:13:43 -0400
Python [EMAIL PROTECTED] wrote:


Alright, I've had time to play with this and wanted to be sure I understand
this well.  It works, so I understand enough to make it work.  However, I
see a difference between your code and mine that makes me think I've missed
something other than the relationship between the cursor  the connection

SNIP

 
 #! /usr/bin/python
 # sqlconnect.py
 import MySQLdb as SQLdb
 

I didn't see anything in the tutorial about the import as.  Is this simply
assigning the module a variable name?

SNIP
 
 def connect( parm_name):
 parms = db_parms[parm_name]
 return SQLdb.connect( **parms)
 

O.k., I was attempting to return Con, not sql.connect() in my function.  I
think that's where part of my problem was.

 
 Use a dictionary to save the connect parameters.  Different users and
 circumstances can require different parameters.  To use this code
 
 import sqlconnect as sql
 conn = sql.connect('regular')
 
 # then get a cursor so that you can do something to the database
 curs = conn.Cursor()
 curs.execute(Some_SQL_Command)
 results = curs.fetchall()
 curs.close()
 

As is, I can copy/paste the 4 lines above into each function I've defined,
and it works like a charm. What I would like now, is to use this like so:

### function for the cursor ### 
def get_Curs():  
curs = conn.Cursor()
curs.execute(sqlCmd)
results = curs.fetchall()
curs.close()

Would I use curs.execute as an argument to get_Curs()?  And would I then
feed sqlCmd into mbr_Phone(sqlCmd)?

### Run get_Curs() for this query and give the user the results.###
def mbr_Phone():  # how to get the cursor?
get_Curs()# 
# Make SQL string and execute it.
sqlCmd = SELECT fst_name, lst_name, hphn FROM person\
order by lst_name
   print 'Phone List'
for record in Results:
print record[0] , '', record[1], '\t', record[2]

def a_Query(sqlCmd):
get_Curs()
code for a_Query

def other_Query(sqlCmd):
get_Curs()
code for other_Query
 
Kind of like this, anyway.  Thanks for your patience. :)

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] MySQL Connection Function

2005-06-21 Thread Don Parris
As a newbie developer, the easiest way for me to connect to MySQL is to
just copy  paste the connection commands into each funtion I write. 
However, I know that's far from ideal, and consumes more time than its
worth.  I would like to create a MySQL connection function that I can just
call up whenever I need it from within an other function.

### The Connection Definition ###
# def mysql_Conn():
# Create a connection object and create a cursor.
# Con = MySQLdb.Connect(host=127.0.0.1, port=3306, user=user,
  passwd=password, db=chaddb_test)   # email text wrap here
# Cursor = Con.cursor()

### The function that Calls the Connection ###
def mbr_Roster():
mysql_Conn()

# Make SQL string and execute it.
sql = SELECT fst_name, lst_name FROM person\
where env_num is not null\
order by lst_name
Cursor.execute(sql)

# Fetch all results from the cursor into a sequence and close the
# connection.
Results = Cursor.fetchall()
Con.close()

How do I get mbr_Roster() to recognize the 'Cursor' from mysql_Conn()?  Do I
need to declare the cursor as a global variable?

From the Traceback:
global name 'Cursor' is not defined


Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Numbers Characters As Dictionary Keys

2005-06-20 Thread Don Parris
On Mon, 20 Jun 2005 09:11:53 +0100
Alan G [EMAIL PROTECTED] wrote:

  raw_input, but I get H is not defined when I run the script.  
  Essentially, I'd like the user to enter a number for most items, 
  but use letters for Help, Quit, and Back to Main.
 
 Are you sure? That kind of inconsistent input is one of the 
 big no-nos of user interface design. It usually confuses 
 the heck out of users!
 

Hmmm, good point.  It was a thought.  I am trying to avoid confusing my
users.  If this kind of input will cause confusion, I should avoid it.  I
was looking to provide a consistent help/quit/main single-key scheme across
all menus. I can (and presently am) using a double-digit number for this
purpose in the current script.  On the other hand, I thought people would
easily recognize at least the H  Q for help and quit. 
Alternatively, I could link these to F1  [Ctrl]+Q, or something like
that.  Many people would understand F1.

Basically, the sub-menu offers choices:
1-3 - add/edit/delete
4-? - standard reports
10-12 - H/Q/M 


 Hoewever to the problem at hand.
 It should just be a case of changing the keys in the dictionary.
 Unfortunately you've told us the problem but shown us the code 
 that woreks, not the broken copde. So we can only guess what 
 you might have done!
 
 But basically here is a sample program that does approximately 
 what you want:
 
 def hello(): print 'hello'
 def goodbye(): print 'goodbye'
 
 menu = { '1' : ('hello', hello),
  'Q' : ('goodbye', goodbye)}
 
 for m in menu.keys():
print %s\t%s % (m,menu[m][0])
 
I am curious what the % by itself is doing.

 cmd = raw_input('pick one ').upper()
 menu[cmd][1]()
 
 Does that help?
 
I'll tinker with this a bit.

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] database app

2005-06-18 Thread Don Parris
On Sat, 18 Jun 2005 13:50:56 -0700
nephish [EMAIL PROTECTED] wrote:

 Hey there,
 
 i have used the cgi module and dig it.
 heres the deal,
 my employer wants me to build a dynamic website that will access a 
 database and display customer
 information on web. ok, easy enough.
 heres the trick.
 the database is MS Access. - ick.
 what could i do to manipulate data in an access database ? can it be 
 done ?
 all the research i have done just points me to sql, sqlite, etc...
 i wouldnt mind if i can find a way to push everything from access to 
 another kind of database,
 but i dont know where to start.
 any ideas?
 
 -thanks
 ___

There is a way to work with your Access data via Python  the Jet engine,
but I have no clue about that.  Check the Python documentation - I'm sure
that's where I saw reference to it.  I may have seen it on one of the
Python-related sites, but I think this link will offer some insight:

http://www.python.org/windows/OdbcHints.html


If you want to get out of Access, you can save your data as tab-delimited
text files, and import them into MySQL.  I'm sure that goes for PostGreSQL
as well.  Just make sure you remove the quote marks - regexp type work.  (I
haven't done that part programmatically yet, just using the find/replace
feature of jEdit.)  I'll have to cover that ground myself soon, so we'll see
what happens.

HTH,
Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Clearing the Console Screen

2005-06-16 Thread Don Parris
With the console-based menu system I'm building, I'd like to clear the
screen for each menu call - something like:

def main_menu():
clear #start with a fresh console screen, menu at top
print menuitems


This way, the users won't have to get too confused by all the previous
screens.

I haven't found the correct way to do this so far.  You can point me to the
documentation, and I'll play with that.  

Thanks,
Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Clearing the Console Screen

2005-06-16 Thread Don Parris
On Thu, 16 Jun 2005 11:16:37 -0400
Kent Johnson [EMAIL PROTECTED] wrote:

 Don Parris wrote:
  With the console-based menu system I'm building, I'd like to clear the
  screen for each menu call - something like:
  
  def main_menu():
  clear #start with a fresh console screen, menu at top
  print menuitems
 
 There is no nice portable way to do this. On Windows running in a DOS
 window (not in IDLE or another GUI environment) you can use import os
 os.system('cls')
 
 Kent
 
 
 ___

Thanks!  I thought there had to be a way to call the OS' clear screen
command, but was going about it the wrong way.  I was trying to use
sys.clear instead of os.system.  Would it be difficult to test the OS,
store the result in a variable, and call the comand based on the variable
result?  Or would it be simpler to have users edit the script for their OS?

Mind you, there may be other areas where I need an OS-specific command.  I'm
beginning to get an idea of the challenges of portability though. ;)

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Clearing the Console Screen

2005-06-16 Thread Don Parris
On Thu, 16 Jun 2005 12:24:32 -0400
Kent Johnson [EMAIL PROTECTED] wrote:

SNIP 
  Mind you, there may be other areas where I need an OS-specific command. 
  I'm beginning to get an idea of the challenges of portability though. ;)
 
 Python actually gets a lot of this right, you may find it's easier than
 you think to write portable Python.
 

Hmmm, I'm using Python 2.3 on SUSE Linux 9.2 at home, and the latest Python
on WinXP at my job (I get to do some non-work-related stuff during my
downtime at work).  So far, I haven't had any problems running my script on
either box.  Frankly, I'm finding Python to be quite easy to learn.

With a little help from the standard tutorials, this list, and my co-worker
(experienced developer), I've managed to get my menu system functioning
rather well, and can even connect to the MySQL database to print off a
member roster to the console.  All this in less than two weeks.  I even
found a better way to print the menus to the console.  These successes are
what makes developing a program exciting.

Meanwhile, I'll check out that site/code.  Again, I appreciate the help.

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Controlling Where My Program Ends

2005-06-15 Thread Don Parris
On Wed, 15 Jun 2005 07:46:40 -0600
Mike Hansen [EMAIL PROTECTED] wrote:

  Subject:
  Re: [Tutor] Controlling Where My Program Ends
  From:
  Don Parris [EMAIL PROTECTED]
  Date:
  Tue, 14 Jun 2005 23:03:59 -0400
  To:
  tutor@python.org
  
  To:
  tutor@python.org
  
  
  On Wed, 15 Jun 2005 00:59:24 -
  DC Parris [EMAIL PROTECTED] wrote:
  
  
 Never mind.  I found it - sys.exit()
 
 Sorry to have wasted the bandwidth/time.
 -- 
  
  
  This was in reference to a post about exiting from a program.  I
  couldn't figure out why my program wouldn't let me exit from within a
  sub-menu of the console interface.  Since my webmail client goofed up
  the from header, it never showed up, and I've cancelled it to avoid
  wasting everyone's time further. I found sys.exit() in the library
  reference, which allows me to do what I want.
  
  Don
  
 
 If you use the if __name__ == '__main__': idiom, then you can just use
 return instead of sys.exit()
 
 def main():
  lotsa interesting python code
  if somethinorother:
  # sys.exit()
  return
  more interesting python code
 
 if __name__ == '__main__':
  main()
 
 ___

Thanks.  I'll try that out a little later (when I get back to this). 
Incidentally, my menu system started off quite klunky, but I've thrown my
menu items into dictionaries and iterate through them to display the menu.
Thus, instead of:
print 'menuitem' 
print 'menuitem'
etc., etc., etc.

I now have:
for i in menu.iteritems():
print repr(key), '', 'value'


Now if I can figure out how to iterate through the if -- elif statements,
I'll be able to shorten the code a bit further.  First things first though. 
Let me just understand these basic things, and then I'll see if I can get a
little smarter.  

Maybe I'll start a thread trials of a Python newbie.  It'll be hilarious
for the old timers, I'm sure.  It might help other newbies as well.

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Controlling Where My Program Ends

2005-06-14 Thread Don Parris
On Wed, 15 Jun 2005 00:59:24 -
DC Parris [EMAIL PROTECTED] wrote:

 Never mind.  I found it - sys.exit()
 
 Sorry to have wasted the bandwidth/time.
 -- 

This was in reference to a post about exiting from a program.  I couldn't
figure out why my program wouldn't let me exit from within a sub-menu of the
console interface.  Since my webmail client goofed up the from header, it
never showed up, and I've cancelled it to avoid wasting everyone's time
further. I found sys.exit() in the library reference, which allows me to do
what I want.

Don
-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Counting MySQL Fields

2005-06-11 Thread Don Parris
Greetings,

I've dabbled in programming/scripting a little, but never really seem to get
the hang of it.  I thought I'd try out Python for a front-end to my
SQL database project.  I've worked through the tutorial, and a few others to
some extent.  At the moment, I'm really just tinkering, but have succesfully
connected Python to MySQL and performed a simple query.

I'd like to use the result.numfields() function to count the fields on a
given table, but apparently don't quite grasp how to implement it.  Below is
my lame attempt.  I searched the comp.lang.python group via Google, but
didn't find any relevant info.  The MySQLdb User Guide didn't give an
example, nor does the module source.

My attempt here was to retrieve a single row, and count the fields.  That
obviously didn't work. If there's a good example of this, I'd appreciate a
pointer.  If you can explain this operation a bit, that would be useful
also.  I'm running SUSE Linux 9.2 Pro on an AMD box.  



- My Test Script 
sql = SELECT * FROM person order by lst_name
Cursor.execute(sql)

# Fetch all results from the cursor into a sequence and close the connection
result = Cursor.fetchone()
Con.close()

# Count the fields
result.num_fields()


-- Relevant info from the resulting Traceback -
result.num_fields()
AttributeError: 'Tuple' object has no attribute 'num_fields'


Using variations of this gives errors to the effect that result or
num_fields is undefined.

Thanks,
Don
-- 
DC Parris   GNU Evangelist
http://matheteuo.org/  http://lulu.com/dcparris
Read the highly rated, widely recommended Penguin in the Pew!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Counting MySQL Fields

2005-06-11 Thread Don Parris
Oops!  Didn't mean to reply off-list.

On Sat, 11 Jun 2005 23:16:47 +0100
Alan G [EMAIL PROTECTED] wrote:

 HI Don,
 
result = Cursor.fetchone()
Con.close()
   
# Count the fields
result.num_fields()
 
  Should I have done something more like this?
a = result
the DB code
a.num_fields()
 
 No I'd have expected something like:
 
 n = result.num_fields()
 
 Although taking a guess I susprect num_fields may be a method
 of the cursor object? After all it knows how many felds each row
 has...
 
 So it may be you need:
 
 n = Cursor.num_fields()
 
 But thats pure guesswork based on how I would have designed it!
 I'm gonna be getting into some Python DB programming soon so I'll
 need to find out then I guess.
 
 Alan G.
 


-- 
evangelinuxGNU Evangelist
http://matheteuo.org/   http://chaddb.sourceforge.net/
Free software is like God's love - you can share it with anyone anytime
anywhere.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor