Re: [Tutor] MySQLdb: at a complete loss

2006-02-18 Thread Brian Gustin


Servando Garcia wrote:
> Hello List
> I am at a complete loss. Here is a recap, just to be clear, I have 
> been unable insert into a database using MySQLdb. I can only view the 
> database.
> My current configuration:
> 1. Windows XP
> 2. Python 2.4.2
> 3 MySQLdb current version
> 4. mysql  5.0.18-nt
> 5. DEV-c++ current version.
> 
> My last script was very sloppy here is my current version
> #!/usr/bin/python
> import MySQLdb,pdb
> 
> #pdb.set_trace()
> try:
> 
> db=MySQLdb.connect(host='localhost',user='root',passwd='081300',db='tut')
> cursor=db.cursor()
> name = raw_input("Please enter a Name: ")
> color =raw_input("Please enter a Color: ")
> cursor.execute("""INSERT INTO horses(name,color) 
> VALUES("%s","%s")"""%(name,color))
> cursor.close()
I might suggest single quoting the values as in
return = cursor.execute("""INSERT INTO horses(name,color)
VALUES('%s','%s')"""%(name,color))
print return # get a return value of the operation and print it - helps 
in debuugging

>
> except MySQLdb.OperationalError,message:
> errorMessage ='Error %d:n %s'%(message[0],message[1])
> print errorMessage
> 
> this script runs without error and does not affect the database in any 
> manner.  The database does exist. User/Password are valid. I have run 
> the above script , unchanged, on both Macintosh and Ubuntu with success.
> 
I notice that some of your activities have no return value - these are 
highly useful in debugging- typically I will capture return values into 
a list or dict so I can examine them after the script executes, or empty 
them out after wards if I set debugging = off
in my scripts.

Bri!

> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:43f74a3f294671847412610!
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] File handling: open a file at specified byte?

2006-02-19 Thread Brian Gustin
HI. This is one I cant seem to find a solid answer on:

First, some background:
I have a log file that I wrote a python parser for it, and it works 
great , but in the interest of saving time and memory , and also to be 
able to read the currently active log file, say every 10 minutes , and 
update the static file, I was trying to find some way that I can get 
python to do this:

Open log file, read lines up to end of file, and *very important* make a 
note of the bytes read, and stash this somewhere (I.E. "mark" the file) ,
and then handle the parsing of said file, until all lines have been read 
and parsed, write the new files, and close the handler.
  say, 10 minutes later, for example, the script would then check the 
bytes read , and *very important* start reading the file *from* the 
point it marked (I.E. pick up at the point it bookmarked) and read from 
that point.
Since the log file will be active (webserver log file) it will have new 
data to be read, but I dont want to have to read through the *entire* 
log file all over again, just to get to the new data- I want to be able 
ot "bookmark" where the log file was read "up to" last time, and then 
open the file later at that point.

My current script works well, but only reads the "day old" log file 
(post log rotate) , and it does very well, parsing as much as 3 GB in as 
little as 2 minutes if the server isnt heavily loaded when the parser 
runs.   basically, the webserver runs Tux , which writes a log file for 
*all* domains on a server, and the script takes the tux log, and parses 
it, extracting the domain for which the log entry is for, and writes a 
new line into the domain's apache format CLF log file (this way we are 
able to run awstats on individual domains, and get relatively accurate 
stats)

So.. my question is- is there any way to do what I want ?

Open a live log file, read lines to x bytes, (say 845673231 bytes) , 
make a note of this, and 10 miutes later open the same file again *AT* 
845673232 bytes - starting with the next byte after the bookmarked 
point, read to end of file, and update the bookmark.


Thanks for any pointers- Advice appreciated.
Bri!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Repeating a routine

2006-02-22 Thread Brian Gustin
yeah GOTO , in basic (I used to write software in Commodore BASIC 
myself) is similar to a for or while loop -

In BASIC, you would write a program, *including* line numbers  thus:

10 a=32
20 b="hello"
30 print a
40 print b
50 a=a-1
60 if a > 0 goto 20
70 end

(It's probably not 100% syntactically correct, but the general idea is
it would print "hello" 33 times and then end..)

in python, you would do :

a=32
b=hello
c=0
while c <= a:
   print b
   c = c+1

for the same results- that's what GOTO in BASIC would be used for
BASIC also had subroutines, which are GOSUB, which equate to perl's sub, 
php's function, or python's def

Just my 2 cents worth :)

Bri!



Pawel Kraszewski wrote:
> Dnia środa, 22 lutego 2006 12:43, John Connors napisał:
> 
> 
> 
>>I know goto and gosub are evil, bad habits but I'm starting to miss them.
> 
> 
> You SHOULD miss them. GOSUB is called a function call in Python (you call a 
> function by its name, rather than by its starting line number)
> 
> Someone misinformed you: GOSUB is perfectly good. It's GOTO that's evil.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:43fc986c278471677385699!
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mysql BLOB strangeness?

2006-03-17 Thread Brian Gustin
if the data is not binary, you can use TEXT type - accepts all readable 
characters and data, BLOB type is more for binary data storage, and 
MYSQL's Varchar type only stores up to 255 characters. (65,536 bits, or 
64Kbits)

If you are storing Binary data (DES-3 encrypted data, or image data, for 
example (a silly idea, IMHO, but some people store image data in 
databases), then you would use BLOB, but I prefer to use TEXT type for 
plain ol' storage of text or characters (say an html page or template, 
etc) rather than the binary BLOB type, although BLOB would be a space 
savings if the data will be quite large, and you will have many rows)

HTH
Bri!


Kent Johnson wrote:
> Adam Cripps wrote:
> 
>>On 3/17/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
>>
>>>Why are you using a BLOB when the content is text?
>>
>>I'm using BLOB because I want them to be able to write content that is
>>longer than 255 chars.
> 
> 
> I'm no MySQL expert but the docs say VARCHAR fields can be up to 65,535 
> chars.
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:441acc98175353568816312!
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mysql BLOB strangeness?

2006-03-18 Thread Brian Gustin
you'd grab a blob the same way you'd grab a text data type- the only 
difference being the data you insert. for example, make a string of 
letters only, insert it into both a blob and a text data type- you can 
get the data out exactly as is either way , however if you attempt to 
put binary data into a text data type, it gets corrupted (char types 
will pad data with spaces to fill out the length specifier, varchar will 
not pad, but *will* truncate trailing spaces, as will text , and in 
binary data- trailing spaces are critical - they are a binary character, 
and the alteration of a single byte in a binary file will render it 
useless)  - it's much like taking a compiled C binary and trying to read 
it in a text editor - you get a long string of incomprehensible 
characters - putting such data in a text field in mysql would render the 
code un-executable, however Blob fields dont care about spaces either 
leading or trailing- it just stores *precisely* what you give it to store.

Ideally, if you think your data will exceed 255 characters in any given 
data type and it will be alphanumeric, non-binary (say, a text file) 
you would use TEXT field type, if you were gonna do something like store 
a JPG image , or even a compiled binary (cant imagine why) you would use 
BLOB field.

Most of this information is readily available at the mysql manual, and 
as far as I know, Python could care less what database type you are 
using, it can get data from mysql as binary or integer or string or 
float, whatever you specify it to be.. I know that with php (my primary 
language, still learning python) data types are automatically converted, 
and with my limited work to date with python/mysql (using both 
python-mysqldb and the ADOdb abstraction layer for python) I have had no 
issues with handling data - when I get an integer from mysql that is of 
type integer, the value I get in python is also integer..

One thing I have been trying to research/find out is a de-cryption 
algorithm using our known key, to reverse mysql's own des_encrypt 
function - we have data stored in a mysql 4.0 table that uses that 
function, which is not supported in mysql 4.1 and 5 , preventing us from 
upgrading that one machine, and Ive been trying to find a way to match 
the DES3 encryption algorithm that mysql uses, (with our known key/seed 
of course) with little luck so far .. but in all my work, Ive never 
noticed any problem in handling data types- but then I read the mysql 
manual pretty throughly, and in my work (extremely high traffic 
production websites) , MySQL optimization and understanding is 
critical.. so maybe something seems trivially simple and obvious to me, 
that may actually need some explanation ?

In short, whether I am working with blob or text, I would query teh 
table and properly type cast the variable or object to the data type I 
am extracting (if I know data is binary, I get it as a raw string, for 
example, if I know data is like an image or a compiled binary file, I 
would handle it as such, rather than making python process it - the 
object would just contain a reference to the location of the data which 
was extracted in the mysql query...)  but then again, I do very little 
work with binary, and dont store files or images to a database in the 
first place.. :)

in case I misunderstood your question- (it could also be read as how you 
extract the data itself from the mysql array result set) - that depends 
heavily on the type of query and method of fetching data, but as far as 
I know, you can just fetch a single piece of data from mysql as a single 
object, and assign it a reference... but that depends heavily on how you 
structure your query and has more to do with mysql than with python 
itself :)

Bri!



Adam Cripps wrote:
> On 3/17/06, Brian Gustin <[EMAIL PROTECTED]> wrote:
> 
>>if the data is not binary, you can use TEXT type - accepts all readable
>>characters and data, BLOB type is more for binary data storage, and
>>MYSQL's Varchar type only stores up to 255 characters. (65,536 bits, or
>>64Kbits)
>>
>>If you are storing Binary data (DES-3 encrypted data, or image data, for
>>example (a silly idea, IMHO, but some people store image data in
>>databases), then you would use BLOB, but I prefer to use TEXT type for
>>plain ol' storage of text or characters (say an html page or template,
>>etc) rather than the binary BLOB type, although BLOB would be a space
>>savings if the data will be quite large, and you will have many rows)
>>
>>HTH
>>Bri!
>>
> 
> 
> Thanks - this makes sense. I will convert the table to text and see
> how I get on.
> 
> However, there is still a learning point that might be missed here -
> how does Python grab BLOB data-types, and how do you manipulate them?
> If it were a file, would

Re: [Tutor] Mysql BLOB strangeness?

2006-03-18 Thread Brian Gustin
Oh. just found the original question.. :)

OK perhaps this would be more helpful if you were to manually query 
mysql on command line and paste the results it outputs here.

what I am betting is your method to get the data out of teh query is 
doing exactly what you tell it to.. :)

but this hinges on the answer to "what is the original row of data 
returned by commandline mysql query"

keep in mind Mysql returns a result set as an array (a list or 
dictionary, when it is associative, if you will)

what your code is doing is taking the row (array) and appending it to an 
additional list..

and where you print the whole, you are basically printing out the 
multi-dimensional array, and I am betting the *last element* returned 
non-null in the mysql query is the content field.. :)

but again, it depends on what the actual content is..

run these two in mysql command line:

mysql> show create table report;
mysql> select * from report limit 1;

and let me know the results.. :)

I doubt that blob vs. text has anything to do with this issue :)

Bri!

Adam Cripps wrote:
> I'm trying to build a mini-CMS for my class to manage a single
> webpage, using CGI and mysql. The children will be storing their main
> content in a BLOB within a Mysql database.
> 
> When I query the content column, I get a strange return like this:
> 
> array('c', 'This is a test ')
> 
> - when the only text I entered in was 'This is a test'.
> 
> When I query mysql directly through a prompt, I get 'This is a test'.
> 
> Does a CGI query get mangled text from a blob? My other VARCHAR
> columns are fine.
> 
> Code:
> 
> def query(statement):
> results = []
> mycursor.execute(statement)
> myrowcount = int(mycursor.rowcount)
> for i in range (0, myrowcount):
> myrow = mycursor.fetchone()
> results.append(myrow)
> return results
> 
> reportquery = "select id, title, content, published from report"
> reportlist = query(reportquery)
> print "" + str(reportlist) + ""
> 
> 
> id = primary key, integer, not null
> title = varchar(255)
> content = blob
> published = char(1), default ='n'
> 
> I've tried using looking at
> reportlist[0][2][1], but that just returns 'T' - which is obviously
> the T in 'This'.
> 
> TIA
> Adam
> --
> http://www.monkeez.org
> PGP key: 0x7111B833
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:441ac610153321413855301!
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Mysql BLOB strangeness?

2006-03-19 Thread Brian Gustin
OK so I guess you know what you need to do now :)  something like this 
perhaaps :

(modified your original code)

reportlist = query(reportquery)
mystring = ''
for row in reportlist:
id = row[0]
title = row[1]
content = row[2]
published = row[3]
mystring = mystring+id+"\t"+title+"\t"+content+"\t"+published+""

print mystring

should get you the data - you may want to force the values to str() type 
  just in case.. anyhow..  when you append an array to an array, you 
have a multi-dimensional array, so when you iterate, you need to handle 
two arrays, in essence.. what python is doing is just what it told you 
to do - convert the multi dimensional list object into a string and 
print it. :) , therefore, the output would be 
array("listitem","listitem") basically :) you need to extract your list 
elements and convert those to strings, then you can work with the whole 
as a string :) otherwise,

do print reportlist  (without the str() stuff) and you will print out 
the entire list of data so you can see what you will be working with :)


what I would do is a function that returns the result set, instead, and 
then iterate over that list object..

Bri!


Adam Cripps wrote:
> On 3/18/06, Brian Gustin <[EMAIL PROTECTED]> wrote:
> 
>>Oh. just found the original question.. :)
>>
>>OK perhaps this would be more helpful if you were to manually query
>>mysql on command line and paste the results it outputs here.
>>
>>what I am betting is your method to get the data out of teh query is
>>doing exactly what you tell it to.. :)
>>
>>but this hinges on the answer to "what is the original row of data
>>returned by commandline mysql query"
>>
>>keep in mind Mysql returns a result set as an array (a list or
>>dictionary, when it is associative, if you will)
>>
>>what your code is doing is taking the row (array) and appending it to an
>>additional list..
>>
>>and where you print the whole, you are basically printing out the
>>multi-dimensional array, and I am betting the *last element* returned
>>non-null in the mysql query is the content field.. :)
>>
>>but again, it depends on what the actual content is..
>>
>>run these two in mysql command line:
>>
>>mysql> show create table report;
>>mysql> select * from report limit 1;
>>
>>and let me know the results.. :)
>>
>>I doubt that blob vs. text has anything to do with this issue :)
>>
>>Bri!
> 
> 
> Thanks again Bri, for both your responses.
> 
> As requested -
> 
> mysql> show create table report:
> | report | CREATE TABLE `report` (
>   `id` int(11) NOT NULL auto_increment,
>   `title` varchar(255) default NULL,
>   `content` blob,
>   `author` int(10) default NULL,
>   `published` varchar(10) default 'n',
>   `rejected` char(1) default NULL,
>   PRIMARY KEY  (`id`)
> ) TYPE=MyISAM |
> 
> mysql> select * from report limit 1;
> ++---+-++---+--+
> | id | title | content | author | published | rejected |
> ++---+-++---+--+
> |  1 | Test  | This is a test  |  0 | n | NULL |
> ++---+-++---+--+
> 1 row in set (0.02 sec)
> (shame this doesn't monospace in Gmail)
> 
> 
> You are right to bet that the last non-null field (part of the array)
> is the content field (select id, title, content from report)
> Adam
> --
> http://www.monkeez.org
> PGP key: 0x7111B833
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:441d42ee268352025918492!
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Opening .py files in firefox

2006-03-19 Thread Brian Gustin
server needs to be set up with a handler to tell it what to do with the 
file , otherwise it will look at local host to determine what to do with 
it , via the extension, and if no extension is found, then it will 
simply offer to download the file to your computer . if you want it to 
display as text, the webserver needs to be set up to do so :)  If it is 
Apache Webserver, you need to use the AddHandler or SetHandler 
directives (note these directives differ between apache 1.3.x and 2.x)

HTH



Andre Roberge wrote:
> Hi everyone-
> 
> This is not strictly speaking a Python question but it's probably
> something that other pythonistas have encountered and, hopefully
> solved :-)
> 
> When I click on a link to a ".py" file (either remotely or on my
> computer) using firefox, it gives me two options: running the script
> with the default Python app, or saving the file.  What I would like is
> to display the file as text in the browser.  Any ideas?  This is on
> Windows XP.
> 
> André
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:441dcd08315282096420017!
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] date conversion

2006-04-05 Thread Brian Gustin
I wrote something similar on my blog some time back..
http://phplogix.com/codefool/?postid=13
This is for converting Apache logfiles to a ISO formatted date for
mysql..  It could easily be reversed, with a little tweaking.. :)


to wit:
def mreplace(s, chararray, newchararray):
 for a, b in zip(chararray, newchararray):
 s = s.replace(a, b)
 return s

olddatestring =
('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')

newdatestring =
('01','02','03','04','05','06','07','08','09','10','11','12')

def clftosql(date):
  ttime = date.split(":",1)
  time = ttime[1]
  datelist = ttime[0].split('/')
  #should be , E.G DD,MM, we need -MM-DD for sql
  day = datelist[0]
  month = datelist[1]
  year = datelist[2]
  newdate = year+'-'+month+'-'+day+' '+time
  return newdate

then you can take a date stamp from an Apache CLF log like this :
01/Oct/2000:00:00:00 (after you regex it out of the log with:

datechk =
re.compile('([0-9]+/.../[0-9][0-9][0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9])')
 

of course)

and run the functions thus:

mydate = mreplace(dated,olddatestring,newdatestring)
mydate1 = clftosql(mydate)


Ray Allen wrote:
> I would like Python to convert a date returned by MySQL (2006-04-05) to a 
> user readable format such as 05-Apr-2006 for display and then to convert it 
> back to ISO format for update.  What is the most convenient way of doing 
> this?  I'm struggling to understand the datetime module's functionality.
> Ray Allen 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:4433d45e204351006614580!
> 
> 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] date conversion

2006-04-05 Thread Brian Gustin
Oh yeah - you can also simply use Mysql's date_format() function when 
you query the database ??

select date_format(column_name,'%d-%b-%Y') as formatted_date from table 
where blah;

would output the date as 05-Apr-2006
http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html




Brian Gustin wrote:
> I wrote something similar on my blog some time back..
> http'://phplogix.com/codefool/?postid=13
> This is for converting Apache logfiles to a ISO formatted date for
> mysql..  It could easily be reversed, with a little tweaking.. :)
> 
> 
> to wit:
> def mreplace(s, chararray, newchararray):
>  for a, b in zip(chararray, newchararray):
>  s = s.replace(a, b)
>  return s
> 
> olddatestring =
> ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
> 
> newdatestring =
> ('01','02','03','04','05','06','07','08','09','10','11','12')
> 
> def clftosql(date):
>   ttime = date.split(":",1)
>   time = ttime[1]
>   datelist = ttime[0].split('/')
>   #should be , E.G DD,MM, we need -MM-DD for sql
>   day = datelist[0]
>   month = datelist[1]
>   year = datelist[2]
>   newdate = year+'-'+month+'-'+day+' '+time
>   return newdate
> 
> then you can take a date stamp from an Apache CLF log like this :
> 01/Oct/2000:00:00:00 (after you regex it out of the log with:
> 
> datechk =
> re.compile('([0-9]+/.../[0-9][0-9][0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9])')
>  
> 
> of course)
> 
> and run the functions thus:
> 
> mydate = mreplace(dated,olddatestring,newdatestring)
> mydate1 = clftosql(mydate)
> 
> 
> Ray Allen wrote:
> 
>>I would like Python to convert a date returned by MySQL (2006-04-05) to a 
>>user readable format such as 05-Apr-2006 for display and then to convert it 
>>back to ISO format for update.  What is the most convenient way of doing 
>>this?  I'm struggling to understand the datetime module's functionality.
>>Ray Allen 
>>
>>___
>>Tutor maillist  -  Tutor@python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
>>
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:4433db1a224961964068235!
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What's the invalid syntax?

2006-04-05 Thread Brian Gustin
define year first

I.E.
year=0
int(year)
or leave the int year out of your code completely - you do not need to 
typecast - it is set as an integer in the following line.



Nathan Pinno wrote:
> Hey all,
>  
> What's the invalid syntax in the following code? I tried figuring it out 
> by myself, and I couldn't.
>  
> # This program finds the date of Easter Sunday between the years 1900 & 
> 2099.
> # By Nathan Pinno
> # Inspired by Programming and Problem Solving with Java, 1st ed., 
> question 3, page 207.
> def date():
> int year
> year = int(raw_input("Year please: "))
> if (year < 1900 || year > 2099):
> print year, " is invalid. Please enter a year between 1900 & 2099."
> else:
> if (year == 1954 || year == 1981 || year == 2049 || year == 
> 2076): # For the years when the calculation would be 1 week late.
> int a, b, c, d, e, f
> a = year % 19
> b = year % 4
> c = year % 7
> d = (19 * a + 24) % 30
> e = (2 * b + 4 * c + 6 * d + 5) % 7
> f = (22 + d + e) - 7
> if (f < 31): # To see if the date is in March or April.
> print "Easter Sunday is March ", f, "."
> else:
> print "Easter Sunday is April ", (f-31), "."
> else: # For all other years.
> int a, b, c, d, e, f
> a = year % 19
> b = year % 4
> c = year % 7
> d = (19 * a + 24) % 30
> e = (2 * b + 4 * c + 6 * d + 5) % 7
> f = (22 + d + e)
> if (f < 31): # Same as above.
>print "Easter Sunday is March ", f, "."
> else:
>print "Easter Sunday is April ", (f-31), "."
> date()
>
> The editor highlights the word year in the line:
>  
> int year
>  
> after I hit okay when this message pops up:
>  
> Invalid syntax.
>  
> Thanks in advance,
> Nathan Pinno
> Web Surfer's Store http://www.websurfstore.ca 
> 
> MSN Messenger: [EMAIL PROTECTED] 
> Yahoo! Messenger: spam_swatter31
> ICQ: 199020705  
> !DSPAM:443450a9253311285612896!
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> !DSPAM:443450a9253311285612896!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] School Boy Error

2006-04-15 Thread Brian Gustin
would be helpful to paste the entire error information that python gives ..
including the lines before and after.
When pytho sets an error , it will tell you *exactly* where the error is 
at (or it should) I do all my development work on Linux, havent worked 
with ODBC databases, but the reference to a list of tuples , almost 
makes me thing the SQL needs to be a full insert:
(insert into tablename (field1,field2.) values (value1,value2) )




John CORRY wrote:
> Thanks Brian for the help on the last one.  I couldn’t see the wood for 
> the trees.  I have a tougher one for you.
> 
>  
> 
> I am reading in a CSV file.  Each line represents a line that I want to 
> upload into my database.  I am trying to upload the first line in the 
> file to get myself started.  The code is below:-
> 
>  
> 
> import string, re
> 
> path = "c:/test/import.csv"
> 
> listy = []
> 
> input = file(path, "r")
> 
> for line in input.readlines():
> 
> line = line.split(",")
> 
> listy.append(line)
> 
>
> 
> print listy[-1]
> 
>  
> 
> stat = """Insert into cost_grid values 
> (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
> 
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
> 
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
> 
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)"""
> 
> t = 5000
> 
> d = "PF2"
> 
> b = 91.4
> 
> a = 95.00
> 
>  
> 
> import mx.ODBC
> 
> import mx.ODBC.Windows
> 
> db = mx.ODBC.Windows.DriverConnect('DSN=vfp')
> 
> c = db.cursor()
> 
> c.execute(stat, listy[-1])
> 
>   
> 
> db.commit()
> 
> c.close()
> 
>  
> 
> I get the following error:
> 
>  
> 
> TypeError: parameters must be a list of tuples
> 
>  
> 
> Any suggestions would be greatly appreciated.
> 
>  
> 
> Thanks,
> 
>  
> 
> John.
> 
> !DSPAM:444178de174651249418164!
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> !DSPAM:444178de174651249418164!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] School Boy error

2006-04-16 Thread Brian Gustin
instead of
c.execute(stat, listy[-1])
try
sql_list = listy[-1]
c.execute(stat, sql_list)

Also
what does print stat give you?

maybe one of those two would tell us ..
Bri!

John CORRY wrote:
> Hi,
> 
>  
> 
> I couldn’t sleep last night with all the code running though my head.  
> Counting sheep didn’t work as I kept wanting to turn them into a loop!
> 
>  
> 
> listy[-1]
> 
>  
> 
> Outputs the following:-
> 
>  
> 
> ['432', 'TM BLIND', 'RO', 'PF1', 'Plain Finish Range One', '304.8', '', 
> '45.7', '80', '90', '0', '39', '61', '15.03', '33', '0', '46', '81.3', 
> '19.38', '42', '0', '60', '101.6', '22.39', '49', '0', '69', '121.9', 
> '26.39', '58', '0', '81', '142.2', '30.4', '67', '0', '93', '162.6', 
> '34.41', '75', '0', '105', '182.9', '38.08', '83', '0', '117', '198.1', 
> '41.42', '90', '0', '127', '223.5', '48.77', '106', '0', '149', '243.8', 
> '53.12', '117', '0', '163', '274.3', '60.8', '133', '0', '186', '304.8', 
> '66.14', '145', '0', '202', '0', '0', '0', '0', '0', '0', '0', '0', '0', 
> '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 
> '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0\n']
> 
>  
> 
> The full error is
> 
>  
> 
> Traceback (most recent call last):
> 
>   File "C:\Python24\Lib\site-packages\databasemanager.py", line 30, in ?
> 
> c.execute(stat, listy[-1])
> 
> TypeError: parameters must be a list of tuples
> 
>  
> 
> Thanks,
> 
>  
> 
> John.
> 
> !DSPAM:444211df223121389011208!
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> !DSPAM:444211df223121389011208!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] School Boy Error

2006-04-16 Thread Brian Gustin
I wonder if it could not be the extra  comma (,) at the end of your sql ?

thus: ..?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)<--- see trailing 
comma, and no matching ?  could be a value count != column count 
mis-match   make sure you have the correct number of values to each 
column :)

On the other hand, it is also possible, if ODBC is strict about taking 
strings and ints, that you need to run a function to convert list items 
to integer values where an integer is required..



John CORRY wrote:
> Bri,
> 
>  
> 
> Print stat gives
> 
>  
> 
> Insert into cost_grid values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
> 
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
> 
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
> 
> ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)
> 
> Traceback (most recent call last):
> 
>   File "C:\Python24\Lib\site-packages\databasemanager.py", line 31, in ?
> 
> c.execute(stat, sql_list)
> 
> TypeError: parameters must be a list of tuples
> 
>  
> 
> Unfortunately sql_list gives the same error.
> 
>  
> 
> Thanks,
> 
>  
> 
> John.
> 
> !DSPAM:44421f56263591105852714!
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> !DSPAM:44421f56263591105852714!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Didn't take long to hit my next wall!

2006-04-16 Thread Brian Gustin
python in this form uses BIND variables..
 >>query = '''UPDATE cost_grid
 >>   SET cost_1 = %s <--- %s = the bind variable 
placeholder/formatter
 >>   WHERE cost_grid_id = %s
 >>   AND finish_dro = %s  % ( a,c,b) <--- the raw tuple


  That is, what is provided to python in the tuple following , is 
formatted as specified by the %s , and as such, is a formatted string 
(special characters are properly esscaped), and as far as sql query is 
concerned, it is escaped safely..

I tested this before, when I first started working with python - Not a 
problem at all. In fact, I also wrote a custom database class in PHP 
that mimics this exact same functionality.. and in all my testing, not a 
single SQL injection has succeeded :)

Basically whatever the value may be is formatted to a string, and 
escaped if necessary for special chars.

Kent Johnson wrote:
> Alan Gauld wrote:
> 
>>Hi John,
>>
>>I've no idea why its not working but this illustrates why I prefer to create
>>the sql string outside the execute - its a lot easier to debug when you can
>>print the string exactly as passed to execute. I know many others like to
>>leave execute to do the escaping stuff but I prefer to see what I'm doing
>>and put in a little extra effort.
>>
>>So I would write it as:
>>
>>query = '''UPDATE cost_grid
>>   SET cost_1 = %s
>>   WHERE cost_grid_id = %s
>>   AND finish_dro = %s  % ( a,c,b)
>>c.execute(query)
> 
> 
> Yikes! Alan! Certainly you know what an SQL injection attack is? And 
> what if the data contains special characters?
> 
> For those who don't know, imagine what happens in the above if
> b = '91.4; drop table cost_grid;'
> 
> or even
> b = 'a;b;"c"update'
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:44422c98294101990911452!
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Didn't take long to hit my next wall!

2006-04-16 Thread Brian Gustin
Interesting.. I'll have to dig out my unit test scripts that I wrote to 
test the situation to my satisfaction...
The way I had it , I wrote the query exactly as was done in this case, 
but I wrote it directly in the cursor.execute() function as in:

cursor.execute('''insert into tablea (id,name,number) values 
(%s,%s,%s)'''%(clean_number,injected_text,injected_number))

and the sql injection attacks I had set as testing, were inserted to the 
database properly escaped, and never broke.

However, I did not test it as a separate string  to be inserted as
cursor.execute(sql_string,%(tuplea,tupleb)), etc.

thanks for the heads up.
Bri!





Kent Johnson wrote:
> Brian Gustin wrote:
> 
>>python in this form uses BIND variables..
>> >>query = '''UPDATE cost_grid
>> >>   SET cost_1 = %s <--- %s = the bind variable 
>>placeholder/formatter
>> >>   WHERE cost_grid_id = %s
>> >>   AND finish_dro = %s''''  % ( a,c,b) <--- the raw tuple
>>
>>
>>  That is, what is provided to python in the tuple following , is 
>>formatted as specified by the %s , and as such, is a formatted string 
>>(special characters are properly esscaped), and as far as sql query is 
>>concerned, it is escaped safely..
> 
> 
> No. There are two ways to write this that look very similar but act very 
> differently. To simplify the example a bit, suppose the query is
> query = '''update cost_grid set cost_1 = %s where cost_grid_id = %s'''
> and the values for cost_1 and cost_grid_id are in variables a and b.
> 
> If you write
> cursor.execute(query % (a, b))
> 
> then you are using string formatting to put the values into the query. 
> This is problematic when a or b needs to be escaped in any way, and is 
> open to SQL injection attacks.
> 
> On the other hand, if you write
> cursor.execute(query, (a, b))
> 
> then the values are passed as a separate parameter to execute(). In this 
> case the DB wrapper will properly escape the values.
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> !DSPAM:44426e80136686683119212!
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] I Give Up.

2006-06-17 Thread Brian Gustin
I have had it. I give up.

Python's documentation sucks beyond belief.

all I want to do is parse a *SIMPLE* config file of name = value pairs 
and have python objects named by the name holding the value signified by 
value , and I want it to be able to work *WITHOUT* someone having to 
download and install additional modules, etc, so I looked up ConfigParser

OK, cool, at least it appears capable. however:
*section of code relevant to issue*

from ConfigParser import SafeConfigParser
cfg = SafeConfigParser("/etc/local-config/myconfig.cfg")
mystuff = cfg.items("Parameters",)#kept giving me an exception that 2 
values required
print mystuff


[EMAIL PROTECTED] python_snips]$ ./pymon.py
Traceback (most recent call last):
   File "./pymon.py", line 20, in ?
 main()
   File "./pymon.py", line 15, in main
 myname = cfg.items("Parameters",)
   File "/usr/lib/python2.3/ConfigParser.py", line 532, in items
 d = self._defaults.copy()
OK.. so items doesnt appear to work (the above is my tenth attempt to 
get it working) So.. "RTSL!" (Read The Source, Luke) - I grokked 
/usr/lib/python2.3/ConfigParser.py" to have a look see at how it does 
what it does, and found additional stuff that isnt documented *AT ALL* 
.. So..


[EMAIL PROTECTED] python_snips]$ ./pymon.py
Traceback (most recent call last):
   File "./pymon.py", line 20, in ?
 main()
   File "./pymon.py", line 15, in main
 myname = cfg.section()
AttributeError: SafeConfigParser instance has no attribute 'section'

OK Obviously I have no clue how the heck this is all supposed to work, 
and I have wasted 3 hours of development time on this thing (it aint the 
first time I have had issues with Python's Documentation)

Whatever. I give up..

I'll just go write it in Perl. Maybe some day when Python actually has 
well structured documentation with *actual working code examples* I 
might take another look at trying to learn more of it, but at this 
point, time is money, and I can develop the same application in Perl 
(probably would have it *done* by now, it's really simple)..

I just needed to vent - I cannot believe how Python ever managed to get 
*anywhere* with the state of documentation at python.org

If you want an example of what I would call quality online documentation 
- need look no further than php.net (or dev.mysql.com) .. or even 
cpan.org (or heck, just run perldoc in commandline.. ) I tried pydoc.. 
it just doesnt cut the mustard..

OK..
/end rant

Now can someone explan how exactly (preferrably with an actual real 
world example) that I can read a configuration file in , say 
/etc/local-config/myconfig.cfg  into my python script running in 
/usr/bin/localscripts , and able to actually use the names as variables 
(objects) with the configured values assigned to them?

This drove me nuts the past couple hours, and truthfully, as much as I 
like python (I really  LIKE python) .. the documentation sucks *SO* bad, 
I just cannot justify attempting to learn more and use it for more 
projects because of situations like this that do nothing but waste my time.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I Give Up.

2006-06-17 Thread Brian Gustin
Thanks-  I had to sit back and with the perl script running nicely, I 
took another shot at this to see if I could get it right.. so far so 
good, at least no more exceptions being raised..

The perl script runs on a cron, I am thinking I will write this in 
python and then wrap it up into a main function, and run it as a daemon, 
eliminating the cron..

So, I guess I'll keep trying, now that the project is done, and the 
script working, and client happy , and me paid, I have a little free 
time to mess around with this..

I am thinking very seriously about building a new set of python 
documentation with capability for user contributions and notes, etc, and 
putting it online (with searchability to boot) .. maybe a project for 
another day..

The one amazing thing I found ridiculously funny:

Python "official" forums- and they actually run phpBB forums?  You mean 
to tell me no one has written a solid, stable forum software in Python?

OK.. maybe another project to tackle.. I guess I just need to start 
building the python documentation the way I think it outta be done... 
and see how it goes over..



help(cfg.items)
> 
> 
> Help on method items in module ConfigParser:
> 
> items(self, section, raw=False, vars=None) method of
> ConfigParser.SafeConfigParser instance
> Return a list of tuples with (name, value) for each option
> in the section.
> 
> All % interpolations are expanded in the return values, based on the
> defaults passed into the constructor, unless the optional argument
> `raw' is true.  Additional substitutions may be provided using the
> `vars' argument, which must be a dictionary whose contents overrides
> any pre-existing defaults.
> 
> The section DEFAULT is special.
> 
This is still not 100% clear, and I had to experiement a little bit to 
get it to work, but finally got it working.

It does *NOT* say that you must send arguments of the function as 
strings, (or as object variables defined elsewhere) , and for the life 
of me, I have not been able to get interpolation to work, despite the 
examples I have seen.. it is quite unclear.

It would seem all of this python documentation is written assuming that 
the reader thinks exactly the same way as the author, and is already 
familiar with the function and objects and documentation is written as 
if it's just a "reminder of how stuff works"...

Example: I had no idea as to how to do this, and if I had not already 
done similar parsing of config files (.ini for example) in other 
languages (perl and php both) I would not even have been able to find 
this module class on google search , unless I was really lucky ..

It seems sad that the only way I can look up "what kind of function or 
module might there be to do x task in Python" has to be found via a 
google search.. In the PHP documentation, I can just search out a 
"similar" function (say, I want to know how to take two arrays and 
combine them, rejecting non-uniques.. I might go to php.net and search 
the function list for array()  and then I have a list of *all* array 
functions that may be used, and able to pick out, for example, 
array_merge() .. With python, I dont see any way of doing that, so lots 
of really cool stuff that can be done in python is not found..

> 
> myname = cfg.sections()
> ^
> Will work for you and provide a list of sections.  You will need those
> sections to feed into the items call.
> 
Yeah - serves me right for not paying attention to spelling.. :)
> 

> 
> No don't!
> 
> You are almost there.
> 

Close.. we'll see how it goes from here, now that Im not under pressure 
to get this script done :)

> I came to Python after Perl and lot's of other languages.  I think
> Python is better, but it helps to have someone to talk to when things
> are going badly.
>
Very true.. and sometimes talking about teh issue the solution suddenly 
becomes evident :)

> 
> I liked the old layout better, but presumably you found this:
> http://docs.python.org/lib/RawConfigParser-objects.html
> 
Actually, no I didnt find that.. another area where Documentation sucks- 
  It's so very easy to miss some important thing in the way this stuff 
is categorized and structured..


In fact, I actually searched for python.org Documentation 
ConfigParser.read   and still didnt see anything come up for python.org 
documentation

Oh well. I'll see if I can get this thing to work like the perl script I 
just wrote...


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I Give Up. - Follow up post

2006-07-04 Thread Brian Gustin
OK see, what I was doing originally (and I found time to finally get it 
partially working)

I have a configuration file that may be edited by webmaster thus

[Ports]

http = 80
https = 443
http1 = 81
smtp = 25
smtp2 = 587

(the above is a small example, it could be *anything* )

Now I have a function that takes machine, protocol, and a dictionary of 
last alerts (would be empty if none)

The whole is a small multiple machine monitoring script, By the way...

The configuration file needed to be read by config parser and assign the 
config name as if it were a variable, and the value along with it

I tried it by opening a file , but could find no way to do variable 
variables

(In PHP, you can do something to the effect of:
name = value
name1 = value1

and then read it in as a file , which builds an array (AKA dictionary)
and then just assign thus:

foreach ($filearray as $key => $value)
{
$$key = $value
}

in python I could see no way to do so, so I ended up with configparser, 
which I *finally* got working - working code below
I am still working on this , as I wanna do it in python (currently 
operational in Perl , and running as a daemon, which I want this code to 
do eventually)

So, the below is a working example of reading a config file in python 
using configparser

Also note: I found that ConfigParser != configparser (it is case sensitive)

Just wanted to post this back in case anyone else needs to figure out 
how to use configparser in python.

import ConfigParser
cfg = ConfigParser.SafeConfigParser()
 cfg.read("/home/brian/pymon.cfg")
 if cfg.has_section("Parameters"):
 myparams = cfg.items("Parameters")
 for item in myparams:
 parameter[item[0]] = item[1]
 else:
 log_error("Parameters","not found")
 if cfg.has_section("Ports"):
 ports = cfg.items("Ports")
 for port in ports:
 watch_port[port[0]] = port[1]
 else:
 log_error("Ports","Not Found")
 if cfg.has_section("Hosts"):
 hostnames = cfg.items("Hosts")
 for hostname in hostnames:
 watch_hosts[hostname[0]] = hostname[1]
 else:
 log_error("Hosts","Not Found")
 if cfg.has_section("IP Addresses"):
 ips = cfg.items("IP Addresses")
 for ip in ips:
 watch_ips[ip[0]] = ip[1]
 else:
 log_error("IP Addresses","Not Found")
 if cfg.has_section("Alerts"):
 alerts_to = cfg.items("Alerts")
 else:
 log_error("Hosts","Not Found")
 print parameter
 print watch_port
 print watch_hosts
 print watch_ips
 print alerts_to
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I Give Up. - Follow up post

2006-07-04 Thread Brian Gustin


Danny Yoo wrote:
>> I tried it by opening a file , but could find no way to do variable 
>> variables
> 
> 
> 
> Hi Brian,
> 
> Look for the concept of dictionaries in Python.  "Variable variables" in 
> languages like PHP and Perl are doable in Python, but a dictionary 
> usually handles such situations in a safer way.

Yeah I am familiar with the concept , and it would work nicely for some 
applications, but I couldnt make it work the way I need it to for this 
application :)

Reason: It's more to do with the further functions in the application 
that I did not paste in the follow up :)

The issue, again is that I want to call a variable by *name* , but I do 
not *KNOW* what that variable's name is, exactly

For example, say I have apache running on port 80, apache-ssl on 443, 
apache2 on port 81, and Caudium on port 8080 (yes I do have a machine 
running not only those servers, but AOLServer, simultaneously)

If I want to configure a protocol to check, *for multiple machines*  I 
might do

(pseudo function)

Check_Host(machine,protocol,port,last_results)

Now, if I want to iterate over a list of machines , and check each 
machine for whatever it was set for (in the config file)

I cannot obviously have a config that says
(Protocol = Port)
http = 80
http = 81
http = 8080
smtp = 25

So the config file requires http to be the first part of the string , 
followed by something to make it unique (I.E. http1, http2, http3)

Now, if I pass each value in turn to check_host, how do I tell what type 
of check to make ? (Obviously if I want to get a string from a web page, 
I need to SEND a GET request, but if I am checking smtp, I cannot use 
that same function, SO )

I check for the base of the configuration (if http in protocol: then run 
http request, else something else)

so that I can run the correct request type on the correct port , and 
maintain a log, but I cannot do that as effectively if it is a 
dictionary or list or even a foreach ..

As I said, I have looked at this from every angle I could see, but using 
dictionary (which was my *first* choice) did not work the way I needed 
it to, nor did simply reading a file to get name value pairs  I needed 
the name to be an actual variable name..

believe me I have tried dictionaries, Ive tried parsing the file by 
other means, but the only way I could get results I needed was through 
configparser

> 
>> and then read it in as a file , which builds an array (AKA dictionary)
>> and then just assign thus:
>>
>> foreach ($filearray as $key => $value)
>> {
>> $$key = $value
>> }

> 
> This is not safe in Python, because the $key given could easily be the 
> name of something that shouldn't be treated as configuration, such as 
> the built-in functions.  It also makes debugging a bit harder if we have 
> variables in a program that aren't explicitely named.
Yes, I am well aware of the dangers of  taking values unchecked into an 
array such as this method shows (simplified).. I rarely ever use it, or 
if I do, it is on *known* clean data (example, pulling config data out 
of a database , reading a .ini file, etc.. not untrusted user input)

I have seen people use this method to extract POST and GET data for 
example (untrusted user input) , which I find horrifying :)

> 
> The safe way to do this is to sandbox these variables in a dictionary. 
> Conceptually, the pseudocode looks like:
> 
> 
> config_options = {}
> for (name, value) in the file:
> config_options[name] = value
> 
> 
> and the real code to do this doesn't look too much different.
> 
Yeah, basically you carry values in a dictionary named by keyname , 
but..  there have been situations where I need the key name as the 
variable name , I.E. config_options[name] = value could become

name = value as if it was explicitly defined that way

It's difficult to grasp or even effectively explain the concept or idea, 
but sometimes you need to KNOW the key name, *without* knowing the key 
name :)  (confused yet? hahaha)

> 
> 
> Let's look at some of the config-reading code:
> 
>> if cfg.has_section("Parameters"):
>> myparams = cfg.items("Parameters")
>> for item in myparams:
>> parameter[item[0]] = item[1]
>> else:
>> log_error("Parameters","not found")
>> if cfg.has_section("Ports"):
>> ports = cfg.items("Ports")
>> for port in ports:
>> watch_port[port[0]] = port[1]
>> else:
>> log_error("Ports","Not Found")
>> if cfg.has_section("Hosts"):
>> hostnames = cfg.items("Hosts")
>> for hostname in hostnames:
>> watch_hosts[hostname[0]] = hostname[1]
>> else:
>> log_error("Hosts","Not Found")
>> if cfg.has_section("IP Addresses"):
>> ips = cfg.items("IP Addresses")
>> for ip in ips:
>> watch_ips[ip[0]] = ip[1]
>> else:
>> log_error("IP Addresses","Not Found")
>>

Re: [Tutor] I Give Up. - Follow up post

2006-07-04 Thread Brian Gustin
OK .. so far so good.. :)
> ultimately want is not meshing well together.
> 
> Let's clarify the requirement: you want to have a mapping from services 
> to their configurations.  Ignoring the format of the configuration file 
> for the moment, it sounds like you ultimately want to parse the 
> configruation and get:

> 
> ### machine_config.py
> machines = { 'apache': ('http', 80),
>  ## fill me in with the config of other machines
> }
> 
> 
> could be used as your configuration file format.  The value portion of a 
> dictionary can be an arbitrary value.  Here, we map a machine's name to 
> a tuple containing the protocol and port.  No string hackery is involved 
> here.
Umm well it would work nicely . *if* the configuration never changed- on 
the other hand, if it is to do as intended, the user of the application 
needs to be able to add and edit the configuration.

Unless they are well versed with python and/or programming and syntax, 
(in which case they probably arent going to use this script anyway)
it is easier for a non programmer to edit a configuration that makes 
some sort of logical sense and is easy to edit, without screwing up 
(need to make it more "forgiving" of things like line endings, spacing 
between name , = and value elements, etc)

So the ideal format is a configuration file where they can set up 
(within documented settings parameters) additional machines or protocols 
or ports to suit their own network or layout..

Easiest = a simple file where I set name = value and import it.
HOWEVER, if it is to be imported, it must follow proper python syntax, 
correct? so if it doesnt, they break it

an INI file on the other hand is a bit more forgiving, thus the choice 
to use it.

> 
> 
> 
>> believe me I have tried dictionaries, Ive tried parsing the file by 
>> other means, but the only way I could get results I needed was through 
>> configparser
> 
> 
> I think you're making this problem too hard for yourself.
> 
perhaps, when I get back to the project I plan to do a code review of 
code to date..
I have no problem with refactoring if I can find a better, momre 
effective way, but it *must* also be user friendly (ease of use and 
configuration for a non-programmer)

Another option I had was to do a web based interface for setting up 
additional machines, etc, but web based is non-ideal for this application.


> 
> 
>> Yeah, basically you carry values in a dictionary named by keyname , 
>> but..  there have been situations where I need the key name as the 
>> variable name , I.E. config_options[name] = value could become
>>
>> name = value as if it was explicitly defined that way
> 
> 
> 
> Can you show us the situation you're talking about that requires this?

if cfg.has_section("Hosts"):
 hostnames = cfg.items("Hosts")
 for hostname in hostnames:
 watch_hosts[hostname[0]] = hostname[1]

would result in like
watch_hosts = {"http" : "80","https" : "443"}

Sure, I could also do this by reading a config file and parsing it , but ..

Say you split at the = , and the config file is like

http = 80 #standard http port
  https=443#https port

Are you going to be 100% certain that when you call watch_hosts[http], 
that you are gonna GET string "80"?
  what about the space between http and =  ?

if you wanted to check if watch_hosts[keyname] == "http" , it would 
never work, right?(because if you split at =, you have string "http " <- 
see the space char?)

if I, however use configparser, I will always get "http" and "80" where 
I expect them to be , I dont have to worry (or send values through a 
cleanup or trim() function) if it has a comment for the line or not, I 
dont need to worry if there is whitespace. right?

This is intended for an end user to be able to edit a config file with a 
minimum of trouble, and be able to leave comments so they remember what 
each is for.. :)
(and if they do mess up somehow, I can catch that kind of thing in the 
code)

I just didnt see any more effective way to get this than to use 
ConfigParser as opposed to attempting to parse a file using regular 
expressions , trim, split, etc

> 
> 
> 
>> It's difficult to grasp or even effectively explain the concept or idea,
> 
> 
> Try to do so.  I think this is a real point that needs to be cleared up.

I understand the concept/idea behind it, I think above is about as good 
an explanation as it gets - I tried to express how Im 
thinking/approaching this issue. Keeping in mind this is going to be 
edited by someone who may or may not know how to write a single line of 
program code.. :)
> 

> Take a look at:
> 
> http://www.python.org/doc/faq/general.html#why-isn-t-there-a-switch-or-case-statement-in-python
>  
> 
Thanks. reading up on it now.

> 
> If you need more examples, ask, and someone here on the list will be 
> happy to help.
> 
> !DSPAM:44aae7ac185203757830825!
> 
> 
_

Re: [Tutor] I Give Up. - Follow up post

2006-07-05 Thread Brian Gustin

> 
> The other alternative is to provide a tool for editing the file in which
> case the format is less important sionce hiumans "never" need to
> touch it.
> 
Heh. good analysis.. now I feel a little dumb- I should have thought of 
this *LONG* ago. yeah I knew about the xml parser , and how to do xml 
markup (it's one of the methods we use on intranet communications 
channels for certain scriptable tasks) ...

My sticking point was, I wanted it to be super easy for a newbie or 
beginner or "sysadmin trainee" to install and set up the monitoring 
without needing to know much more than maybe basic command line..

That's when it finally hit me (thanks to your commentary below) , why 
use a config file at all? all I have to do is create a simple "setup" 
utility that will write the "configuration" data I need in *correct and 
perfect* python syntax, which I can simply reload, or re-import into the 
daemon ... no need to even have a text editor, all I need is a little 
shell script or python utility to ask for and wait for the user's input 
at each prompt, and check data at each step to get it right..

Yep, I think I was *absolutely* over-thinking the issue here. :)

> How about command line tools?
> Somewhat like the commonly found adduser command in Unix?
> 
 Yeah, basically you carry values in a dictionary named by keyname , 
 but..  there have been situations where I need the key name as the 
 variable name , I.E. config_options[name] = value could become

 name = value as if it was explicitly defined that way
> 
> 
> While I understand the usefulness of this in an interactive environment
> I'm puzzled about how this would work in a pre-written script. If you are
> creating variables from a config file how do you know what those variables
> are called? If you don;lt know how can you reference them later in the
> code? And if you don;t reference them of what value is the variable
> name? Therefore the logical conclusion(to me!) is that you must
> know what names you expect to read and therefore can use a dictionary?
> 
> I'm not sure I understand where exactly you are having the problems
> (other than the admnittedly poor configparser documentation! - the
> quality of python docs tends to be directly proportional to its frequency
> of use and inversely proportional to age - more recent modules tend to
> be better documented than ancient ones. Unfortunately for you config
> parser doesn't seem to be used that much and has been there for ever!)

Yeah this kind of confused the whole issue- and snowballed a little 
bit.. got off track ..

Point originally was, I wanted to be able to read in a config file as a 
name => value pair , and then actually have a variable named by name in 
the script, which you cannot do using a dictionary, you could use the 
key, yes, but at the time it didnt make sense to me to have 4 or 5 
dictionaries laying around in memory when they only have one or two keys 
, and I wanted to use single variables..

Comes from wanting to minimize how much stuff I have loaded into memory, 
and my impression is, a dictionary of 2 elements (name:value pairs)
takes up more space in memory than two single variables (name points to 
value in memory), and the conversation just ultimately got mixed up, and 
I couldnt even remember, in the end why I needed the variable names Vs. 
a dict, until I went back to SubVersion and dug out the first 2 versions 
of my code :)

(Got to love Revision control systems! )

Anyhow, that's now a moot point. :) I have no valid reason at this point 
to insist on knowing variable names.

However to make a point, my original thinking was along the lines of a 
way to do variable variables, (there are certain instances where they 
can be handy) , however since python has no $ to start a variable, I saw 
no way it was possible... Oh well

I have yet to get back in this project, it's working as needed in Perl 
(with a somewhat kludgy configuration, and really, IMHO, more code than 
is really necesary for something so simple as this) but it does the job, 
and I have other things I must do :)

> 
> HTH,
> 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
> 
> 
> !DSPAM:44ab7d4d293088057919449!
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] forum

2006-08-05 Thread Brian Gustin
Signed up, but it never send me the confirmation email (yes, I used a 
valid email address) . It's been 2 hours, and still no email.

How the HECK does one contact the forum admin to ask them to activate my 
account?  I signed up with [EMAIL PROTECTED]

Grrr.. I really hate forums that have admins that dont post contact 
information or provide some way to contact them when there's a 
registration issue. :(



josip wrote:
> look at this
> www.python-forum.org 
> 
> 
> Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great 
> rates starting at 1¢/min. !DSPAM:44d500e691801941725006! 
> 
> 
> 
>  
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> !DSPAM:44d500e691801941725006!
>  
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor