Re: [Tutor] Changing a class into a subclass

2005-03-25 Thread Alan Gauld
> > single instance (or indeed no instances because you could
> > use a static method... or get really fancy and create a
> > meta-class!). 
> 
> or make it a static method of Building 

Yes that's actually what I meant, but reading it back it 
sounds like I meant static Factory method Ho hum.

> or get really simple and use a module-level function...

Yeah., but I did say if he wanted to be OOPish.

> > class Shack(Building):
> >def __init__(self,p1,p2...):
> >   Building.__init__(self,...)
> >   Building.register(self,'Shack', Shack)
> >   ...rest of init...
> 
> I think you want to register Shack before you ever create one. 

Yeah, you are right. My way you register it every time you 
create an instance. A single registration call right after 
the class definition is probably better.

> or you could probably get tricky and do it in a metaclass...

I would do in Smalltalk but meta classes in Python are somewhere 
that I haven't visited yet! :-)

Alan G.

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


Re: [Tutor] Defining functions

2005-03-25 Thread Alan Gauld
> If I try to change the 1, 2, 3, 4 or 5 by a letter i.e. a, b, c, d,
e the
> programme stop functionning. I get an error message saying that
>
> Traceback (most recent call last):
>   File "C:/Python24/Example/area_cir_squ_regt.py", line 39,
in -toplevel-
> print_options()
>   File "C:/Python24/Example/area_cir_squ_regt.py", line 27, in
print_options
> choice = input("Choose an option: ")
>   File "", line 0, in -toplevel-
> NameError: name 'c' is not defined
>
> What am I missing? Thanks

Some quote signs...

You need to use raw_input().
input tries to evaluate what the user types, so if they type c, input
looks for a variable called c and tries to return its value. But
you don't have a variable called c and if you had, things would
be even more confusing!

Using input is useful occasionally but potentially dangerous because
a malicious user could type python code into your program and break
it.
Use raw_input() instead and convert the result as you need it (using
int(),
float(), str(), or whatever...).

Alan G.

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


Re: [Tutor] Defining functions

2005-03-25 Thread Michael Dunn
Something I've always wondered: if input() is so dangerous, why is it
there? What valid uses does it have in the wild?

I ask this because this confusion comes up a lot: people expect
input() to return a string and it throws them when it doesn't. We all
just learn to use raw_input(), and to forget about input(). But if you
really needed the current input() function, isn't eval(raw_input())
the same thing? And it leaves you space to check the input string for
anything stupid or dangerous before you feed it to eval().

Perplexed,

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


[Tutor] SORRY (wrong adress) - (appearance of old postings)

2005-03-25 Thread Gregor Lingl
Ooops, I put the wrong address into my recent posting,
which was intended to go to the edu-sig list.
Forget it!
Sorry for the inconvenience,
Gregor

--
Gregor Lingl
Reisnerstrasse 3/19
A-1030 Wien
Telefon: +43 1 713 33 98
Mobil:   +43 664 140 35 27
Website: python4kids.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] appearance of old postings

2005-03-25 Thread Gregor Lingl
Hi,
there just appeared a couple of postings from a Jan 2004 thread
(on primes) on this list again. (two of them by me)
To me it's completely unclear why and how this could
happen.
Does anybody know ...?
Gregor
--
Gregor Lingl
Reisnerstrasse 3/19
A-1030 Wien
Telefon: +43 1 713 33 98
Mobil:   +43 664 140 35 27
Website: python4kids.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to use MySQLdb.cursor

2005-03-25 Thread Kent Johnson
Vicki Stanfield wrote:
I finally gave up and used MySQLdb to connect to my database. It connects
okay, and returns data, but now I have a new question. I use the code
below to print the data returned from my query, but I would like to make
labels at the top of the columns. How do I do this dynamically? I would
like to get the fieldnames as defined by mysql and print them before
printing each column. Is there a way to do this?
Here is the relevant portion of the code:
def getdata():
 conn = MySQLdb.Connect(
 host='localhost', user='user',
 passwd='password', db='sample',compress=1,
 cursorclass=MySQLdb.cursors.DictCursor)
 cursor = conn.cursor()
 cursor.execute("""SELECT computers.comp_location FROM computers, mice
WHERE mice.mouse_type = "USB"
AND computers.comp_location like "A%"
AND mice.mouse_comp = computers.comp_id;""")
In this case you know the name as it is in the query (comp_location). In general you can use 
cursor.description. From the DB-API docs (http://www.python.org/peps/pep-0249.html):

This read-only attribute is a sequence of 7-item
sequences.  Each of these sequences contains information
describing one result column: (name, type_code,
display_size, internal_size, precision, scale,
null_ok). The first two items (name and type_code) are
mandatory, the other five are optional and must be set to
None if meaningfull values are not provided.
So to output a row with the column names something like this should work:
print ""
for col in cursor.description:
print '%s' % col[0]
print ""
Kent
 rows = cursor.fetchall()
 cursor.close()
 conn.close()
 print '''
 
 '''
 for row in rows:
  print ""
  for cell in row:
  print " %s " % row[cell]
  print ""
Thanks for helping me get going.
Vicki
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Defining functions

2005-03-25 Thread Kent Johnson
Michael Dunn wrote:
Something I've always wondered: if input() is so dangerous, why is it
there? What valid uses does it have in the wild?
It's a mistake planned to be removed in Python 3.0, the "hypothetical future release of Python that 
can break backwards compatibility with the existing body of Python code."

Python tries very hard to maintain backward compatibility so things like 
input() are not removed.
http://www.python.org/peps/pep-3000.html#built-ins
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to use MySQLdb.cursor

2005-03-25 Thread Liam Clarke
mySQL also has it's own metadata commands - 

http://dev.mysql.com/doc/mysql/en/getting-information.html

Looks like you want to use the DESCRIBE  command.



On Fri, 25 Mar 2005 06:25:52 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Vicki Stanfield wrote:
> > I finally gave up and used MySQLdb to connect to my database. It connects
> > okay, and returns data, but now I have a new question. I use the code
> > below to print the data returned from my query, but I would like to make
> > labels at the top of the columns. How do I do this dynamically? I would
> > like to get the fieldnames as defined by mysql and print them before
> > printing each column. Is there a way to do this?
> >
> > Here is the relevant portion of the code:
> >
> > def getdata():
> >  conn = MySQLdb.Connect(
> >  host='localhost', user='user',
> >  passwd='password', db='sample',compress=1,
> >  cursorclass=MySQLdb.cursors.DictCursor)
> >  cursor = conn.cursor()
> >  cursor.execute("""SELECT computers.comp_location FROM computers, mice
> > WHERE mice.mouse_type = "USB"
> > AND computers.comp_location like "A%"
> > AND mice.mouse_comp = computers.comp_id;""")
> 
> In this case you know the name as it is in the query (comp_location). In 
> general you can use
> cursor.description. From the DB-API docs 
> (http://www.python.org/peps/pep-0249.html):
> 
>  This read-only attribute is a sequence of 7-item
>  sequences.  Each of these sequences contains information
>  describing one result column: (name, type_code,
>  display_size, internal_size, precision, scale,
>  null_ok). The first two items (name and type_code) are
>  mandatory, the other five are optional and must be set to
>  None if meaningfull values are not provided.
> 
> So to output a row with the column names something like this should work:
> print ""
> for col in cursor.description:
>  print '%s' % col[0]
> print ""
> 
> Kent
> 
> >  rows = cursor.fetchall()
> >  cursor.close()
> >  conn.close()
> >
> >  print '''
> >  
> >  '''
> >
> >  for row in rows:
> >   print ""
> >   for cell in row:
> >   print " %s " % row[cell]
> >
> >   print ""
> >
> > Thanks for helping me get going.
> > Vicki
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] max. range of list

2005-03-25 Thread jrlen balane
how many is the maximum member can a list have???
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] max. range of list

2005-03-25 Thread Max Noel
On Mar 25, 2005, at 15:50, jrlen balane wrote:
how many is the maximum member can a list have???
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
	As far as I know, there is no limit hard-coded in the language. So I 
guess the maximum number of elements in a list is either (2^63 - 1) or 
the available space in your computer's memory (RAM + swapfile), 
whichever is the smallest.
	In other words, enough that you don't have to worry about it.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"

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


Re: [Tutor] max. range of list

2005-03-25 Thread Kent Johnson
jrlen balane wrote:
how many is the maximum member can a list have???
According to this thread
http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/2ddae82bb2c1b871/e00b7903bc887a73
the number of element in a list is stored in an int, so most likely the hard limit is 2**31-1. The 
practical limit is the available memory.

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


Re: [Tutor] Defining functions

2005-03-25 Thread Jacob S.
Yeah. And they're thinking of removing raw_input() too.  I think it's good 
to have a __builtin__ user input function.  Why should we have to import sys 
everytime we want user input? Almost every program that newbies write uses 
it, and advanced programmers also if they're using console programs.  IMHO, 
I see no reason to remove it.
## end rant

Jacob

Michael Dunn wrote:
Something I've always wondered: if input() is so dangerous, why is it
there? What valid uses does it have in the wild?
It's a mistake planned to be removed in Python 3.0, the "hypothetical 
future release of Python that can break backwards compatibility with the 
existing body of Python code."

Python tries very hard to maintain backward compatibility so things like 
input() are not removed.

http://www.python.org/peps/pep-3000.html#built-ins
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] max. range of list

2005-03-25 Thread jrlen balane
thanks for the information...


On Fri, 25 Mar 2005 10:26:00 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote:
> jrlen balane wrote:
> > how many is the maximum member can a list have???
> 
> According to this thread
> http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/2ddae82bb2c1b871/e00b7903bc887a73
> the number of element in a list is stored in an int, so most likely the hard 
> limit is 2**31-1. The
> practical limit is the available memory.
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Unique elements mapping

2005-03-25 Thread Srinivas Iyyer
Hi all:

I have a question and I request groups help please.

My list has two columns:

NameState
DrewVirginia
NoelMaryland
NikiVirginia
Adams   Maryland
JoseFlorida
Monica  Virginia
Andrews Maryland


I would like to have my ouput like this:

Virginia :  Drew,Niki,Monica
Maryland:   Noel,Adams, Andrews
Florida:  Jose


Can you help how should I code :


for line in my_list:
key = line.split('\t')[0]
val = line.split('\t')[1]


dict = dict(zip(key,val))

this was my strategy ... but I could not make it
work.. 
Please help

srini



__ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/ 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Defining functions

2005-03-25 Thread Gabriel Farrell
So, as a newbie, I see this thread and I check out the PEP and I see
that for future compatibility we should use sys.stdin.readline().  So
I import sys to see how it works.  Of course, sys.stdin.readline('type
anything: ') doesn't work in quite the same way as raw_input('type
anything: ') does.  The closest I can get after a few newbie stabs is:

>>>print 'type anything: ', sys.stdin.readline()
type anything: hello
 hello

>>>

What is the easiest way to get the exact functionality of raw_input()
(i.e. a prompt, no whitespace at the front, and no trailing \n) using
sys.stdin.readline()?

gabe


On Fri, Mar 25, 2005 at 11:02:43AM -0500, Jacob S. wrote:
> Yeah. And they're thinking of removing raw_input() too.  I think it's good 
> to have a __builtin__ user input function.  Why should we have to import 
> sys everytime we want user input? Almost every program that newbies write 
> uses it, and advanced programmers also if they're using console programs.  
> IMHO, I see no reason to remove it.
> ## end rant
> 
> Jacob
> 
> 
> >Michael Dunn wrote:
> >>Something I've always wondered: if input() is so dangerous, why is it
> >>there? What valid uses does it have in the wild?
> >
> >It's a mistake planned to be removed in Python 3.0, the "hypothetical 
> >future release of Python that can break backwards compatibility with the 
> >existing body of Python code."
> >
> >Python tries very hard to maintain backward compatibility so things like 
> >input() are not removed.
> >
> >http://www.python.org/peps/pep-3000.html#built-ins
> >
> >Kent
> >
> >___
> >Tutor maillist  -  Tutor@python.org
> >http://mail.python.org/mailman/listinfo/tutor
> >
> >
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unique elements mapping

2005-03-25 Thread Sean Perry
Srinivas Iyyer wrote:
Hi all:
I have a question and I request groups help please.
My list has two columns:
NameState
DrewVirginia
NoelMaryland
NikiVirginia
Adams   Maryland
JoseFlorida
Monica  Virginia
Andrews Maryland
I would like to have my ouput like this:
Virginia :  Drew,Niki,Monica
Maryland:   Noel,Adams, Andrews
Florida:  Jose
Can you help how should I code :
for line in my_list:
key = line.split('\t')[0]
val = line.split('\t')[1]


dict = dict(zip(key,val))
this was my strategy ... but I could not make it
work.. 
You have the right idea. Just bad implementation (-:
uniques = {} # never name them 'dict', that is a type name in python
for line in my_list:
key, val = line.split('\t')
uniques.setdefault(val, []).append(key)
# in Python 2.4 the following two lines can be shortened to
# sorted_keys = sorted(unique.keys())
sorted_keys = unique.keys()
sorted_keys.sort()
for item in sort_keys:
print "%s: %s" % (item, ",".join(unique[item]))
So what we are doing is making a dict for which each element is a list 
of names. The setdefault trick above is a neater way of saying:

if not uniques.has_key(val):
uniques[val] = []
uniques[val].append(key)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] a shorter way to write this

2005-03-25 Thread jrlen balane
basically, i'm going to create a list with 96 members but with only one value:

list1[1,1,1,1...,1]

is there a shorter way to write this one???
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a shorter way to write this

2005-03-25 Thread Peter Markowsky
Hi,
On Mar 25, 2005, at 2:02 PM, jrlen balane wrote:
basically, i'm going to create a list with 96 members but with only 
one value:

list1[1,1,1,1...,1]
You might want to use a list comprehension like:
[1 for i in range(96)]
-Pete
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a shorter way to write this

2005-03-25 Thread Gabriel Farrell
how about

manyones = [ 1 for x in range(96) ]


On Sat, Mar 26, 2005 at 03:02:34AM +0800, jrlen balane wrote:
> basically, i'm going to create a list with 96 members but with only one value:
> 
> list1[1,1,1,1...,1]
> 
> is there a shorter way to write this one???
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] a shorter way to write this

2005-03-25 Thread jrlen balane
a, so thats the way to do it,  a list comprehension, thanks for the info...


On Fri, 25 Mar 2005 14:10:41 -0500, Gabriel Farrell <[EMAIL PROTECTED]> wrote:
> how about
> 
> manyones = [ 1 for x in range(96) ]
> 
> 
> On Sat, Mar 26, 2005 at 03:02:34AM +0800, jrlen balane wrote:
> > basically, i'm going to create a list with 96 members but with only one 
> > value:
> >
> > list1[1,1,1,1...,1]
> >
> > is there a shorter way to write this one???
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] a shorter way to write this

2005-03-25 Thread Ryan Davis
A comprehension and range?

#
>>> list1 = [1 for x in range(0,96)]
>>> len(list1)
96
#

Thanks,
Ryan 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of jrlen balane
Sent: Friday, March 25, 2005 2:03 PM
To: Tutor Tutor
Subject: [Tutor] a shorter way to write this

basically, i'm going to create a list with 96 members but with only one value:

list1[1,1,1,1...,1]

is there a shorter way to write this one???
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] a shorter way to write this

2005-03-25 Thread Kent Johnson
jrlen balane wrote:
basically, i'm going to create a list with 96 members but with only one value:
list1[1,1,1,1...,1]
is there a shorter way to write this one???
[1] * 96
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python and Javascript

2005-03-25 Thread Mike Hall
I'm curious on whether or not JavaScript and Python can talk to each 
other. Specifically, can a python function be called from within a JS 
function? Admittedly this is probably more of a JavaScript than Python 
question, but I'd  love to know if anyone can at least point me in a 
direction to research this.

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


Re: [Tutor] a shorter way to write this

2005-03-25 Thread Sean Perry
jrlen balane wrote:
basically, i'm going to create a list with 96 members but with only one value:
list1[1,1,1,1...,1]
is there a shorter way to write this one???
def generateN(n):
while 1:
yield n
I'll leave the actual list creation up to you (-:
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] a shorter way to write this

2005-03-25 Thread Smith, Jeff
For all the talk of Python only having one way to do something which is
why it's so much better than Perl, I've counted about 10 ways to do this
:-)

Jeff

-Original Message-
From: Sean Perry [mailto:[EMAIL PROTECTED] 
Sent: Friday, March 25, 2005 2:20 PM
To: Tutor Tutor
Subject: Re: [Tutor] a shorter way to write this


jrlen balane wrote:
> basically, i'm going to create a list with 96 members but with only 
> one value:
> 
> list1[1,1,1,1...,1]
> 
> is there a shorter way to write this one???

def generateN(n):
 while 1:
 yield n

I'll leave the actual list creation up to you (-:
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unique elements mapping

2005-03-25 Thread Danny Yoo


On Fri, 25 Mar 2005, Srinivas Iyyer wrote:


> NameState
> DrewVirginia
> NoelMaryland
> NikiVirginia
> Adams   Maryland
> JoseFlorida
> Monica  Virginia
> Andrews Maryland
>
>
> I would like to have my ouput like this:
>
> Virginia :  Drew,Niki,Monica
> Maryland:   Noel,Adams, Andrews
> Florida:  Jose


Hi Srinivas,

Sean showed you how to fix the bugs in the program; I wanted to highlight
one particular bug that you were running into:


Let's look at your program again:

> for line in my_list:
> key = line.split('\t')[0]
> val = line.split('\t')[1]

This code is trying to extract a key and value out of every line in
my_list.  This part actually looks ok.

One of the bugs, though, is that the code doesn't collect those keys and
values up: it's only holding on to one particular key and one particular
value at a time.  When we say 'key' or 'val', we're only talking about one
particular name or state.  And this means that we're probably dropping
things on the floor.


One way we can fix this is to use a container for all the keys and all the
values.  We can plunk each new key and value into their respective
containers:

##
keys = []
vals = []
for line in my_list:
key = line.split('\t')[0]
val = line.split('\t')[1]
keys.append(key)
vals.append(val)
##



By the way, we can make this into a function, to make it easier to test
out:

##
def getKeysAndValues(my_list):
keys = []
vals = []
for line in my_list:
key = line.split('\t')[0]
val = line.split('\t')[1]
keys.append(key)
vals.append(val)
return keys, vals
##


As a function, this is easier to test since we can feed the function some
sample data, and see if it breaks:

###
>>> assert (getKeysAndValues(["hello\tworld", "goodbye\tworld"]) ==
...(["hello", "goodbye"], ["world", "world"]))
>>>
###

And since Python doesn't complain here, we're reasonably sure that it's
doing the right thing.


I want to emphasize that getKeysAndValues() is probably not exactly what
you want: Sean's solution with the dictionary's setdefault()  stuff in his
previous post sounds right.

But keeping things in functions is nice because we can later just test
specific parts of a program to make sure they're happily working.


Please feel free to ask more questions about this;  we'll be happy to
help.

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


RE: [Tutor] a shorter way to write this

2005-03-25 Thread Danny Yoo

> jrlen balane wrote:
> > basically, i'm going to create a list with 96 members but with only
> > one value:
> >
> > list1[1,1,1,1...,1]
> >
> > is there a shorter way to write this one???

Hi Jrlen Balana,

I wanted to ask: why do we want to make a list of 96 members, with the
same value?  This seems awfully hardcoded.  *grin*

Can you explain why you're doing this?  Just curious.

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


RE: [Tutor] Python and Javascript

2005-03-25 Thread Ryan Davis
Depends on your environment.

If your js is on a webpage, you can have it make http calls to a python web 
service.  Look for articles on XMLHttpRequest in
javascript to see some examples.

I don't know how else that could be done, but I imagine there are other ways.

Thanks,
Ryan 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mike Hall
Sent: Friday, March 25, 2005 2:18 PM
To: tutor@python.org
Subject: [Tutor] Python and Javascript

I'm curious on whether or not JavaScript and Python can talk to each 
other. Specifically, can a python function be called from within a JS 
function? Admittedly this is probably more of a JavaScript than Python 
question, but I'd  love to know if anyone can at least point me in a 
direction to research this.


-MH

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

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


Re: [Tutor] Trying to use MySQLdb.cursor

2005-03-25 Thread Alan Gauld
> below to print the data returned from my query, but I would like to
make
> labels at the top of the columns. How do I do this dynamically?

You shouldn't, it makes your code very vulnarable to changes in the
database!
Its the same principle as using 'select * from...', a bad idea in
production code. And if you know which columns you are selecting you
by definition know what labels to use.

And another reason why its a bsad idea is that databvase columns often
have weird abbreviated names that you don't want to expose users to.

Alan G.

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


Re: [Tutor] Python and Javascript

2005-03-25 Thread Alan Gauld

> I'm curious on whether or not JavaScript and Python can talk to each
> other. Specifically, can a python function be called from within a
JS
> function? Admittedly this is probably more of a JavaScript than
Python
> question, but I'd  love to know if anyone can at least point me in a
> direction to research this.

As ever, it depends.

If you are using WSH on Windows and have the Python active scripting
installed then yes. Similarly if you use IE as web browser then it
can be done in a web page too.

If however it's server-side JavaScript the answer is probably no
unless you count using web services such as SOAP and XML/RPC.

Alan G.

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


Re: [Tutor] Python and Javascript

2005-03-25 Thread Mike Hall
Ryan, I should clarify that what I'd like to do here is unrelated to 
the web. I'm actually just interested in using a local html page as a 
simple gui to launch python calls. So a JS event handler, say a button 
click, would then call a JS function which inside of it would call a 
Python function while handing it arguments (say a path that the JS 
queried from a field in the html page.) That kind of thing. It seems 
like it should be possible, and hopefully easy, but I have no 
experience in calling Python functions from other languages so I'm just 
looking for some input on that. Thanks,

-MH
On Mar 25, 2005, at 12:01 PM, Ryan Davis wrote:
Depends on your environment.
If your js is on a webpage, you can have it make http calls to a 
python web service.  Look for articles on XMLHttpRequest in
javascript to see some examples.

I don't know how else that could be done, but I imagine there are 
other ways.

Thanks,
Ryan
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On 
Behalf Of Mike Hall
Sent: Friday, March 25, 2005 2:18 PM
To: tutor@python.org
Subject: [Tutor] Python and Javascript

I'm curious on whether or not JavaScript and Python can talk to each
other. Specifically, can a python function be called from within a JS
function? Admittedly this is probably more of a JavaScript than Python
question, but I'd  love to know if anyone can at least point me in a
direction to research this.
-MH
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

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


RE: [Tutor] Python and Javascript

2005-03-25 Thread Ryan Davis
Ok, that explains a lot, but I don't know of any easy way to do have javascript 
talk to python.

I can think of some horrible ways to do it, though.

1. Make a python web service running locally, and build up SOAP calls or HTTP 
posts to it. (same as I suggested earlier)
2. Use XUL and pyXPCOM to make a firefox extension that talks to python.  This 
is probably much more of a pain in the ass than you
want to do, but that's the only way I know of to directly call python functions 
from javascript.
3. Look into web framework Zope, that might have some of this plumbing done 
already.
4. Check out Sajax, http://www.modernmethod.com/sajax/, a framework to automate 
javascript calling your server-side functions.  It
was made for PHP, but looks to have a python version as well.

All of those but #2 require you to set up some kind of server.  Is there a 
reason it has to be an HTML page?  

If not, making a GUI might be an alternative that sidesteps this altogether.


Thanks,
Ryan 

-Original Message-
From: Mike Hall [mailto:[EMAIL PROTECTED] 
Sent: Friday, March 25, 2005 3:46 PM
To: Ryan Davis
Cc: tutor@python.org
Subject: Re: [Tutor] Python and Javascript

Ryan, I should clarify that what I'd like to do here is unrelated to 
the web. I'm actually just interested in using a local html page as a 
simple gui to launch python calls. So a JS event handler, say a button 
click, would then call a JS function which inside of it would call a 
Python function while handing it arguments (say a path that the JS 
queried from a field in the html page.) That kind of thing. It seems 
like it should be possible, and hopefully easy, but I have no 
experience in calling Python functions from other languages so I'm just 
looking for some input on that. Thanks,

-MH


On Mar 25, 2005, at 12:01 PM, Ryan Davis wrote:

> Depends on your environment.
>
> If your js is on a webpage, you can have it make http calls to a 
> python web service.  Look for articles on XMLHttpRequest in
> javascript to see some examples.
>
> I don't know how else that could be done, but I imagine there are 
> other ways.
>
> Thanks,
> Ryan
>
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On 
> Behalf Of Mike Hall
> Sent: Friday, March 25, 2005 2:18 PM
> To: tutor@python.org
> Subject: [Tutor] Python and Javascript
>
> I'm curious on whether or not JavaScript and Python can talk to each
> other. Specifically, can a python function be called from within a JS
> function? Admittedly this is probably more of a JavaScript than Python
> question, but I'd  love to know if anyone can at least point me in a
> direction to research this.
>
>
> -MH
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>

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


Re: [Tutor] Python and Javascript

2005-03-25 Thread Mike Hall
On Mar 25, 2005, at 12:41 PM, Alan Gauld wrote:
If you are using WSH on Windows and have the Python active scripting
installed then yes. Similarly if you use IE as web browser then it
can be done in a web page too.

I'm on OSX, and would be doing this through Safari most likely.
-MH
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python and Javascript

2005-03-25 Thread Mike Hall
On Mar 25, 2005, at 1:00 PM, Ryan Davis wrote:
Ok, that explains a lot, but I don't know of any easy way to do have 
javascript talk to python.

I can think of some horrible ways to do it, though.
1. Make a python web service running locally, and build up SOAP calls 
or HTTP posts to it. (same as I suggested earlier)
2. Use XUL and pyXPCOM to make a firefox extension that talks to 
python.  This is probably much more of a pain in the ass than you
want to do, but that's the only way I know of to directly call python 
functions from javascript.
3. Look into web framework Zope, that might have some of this plumbing 
done already.
4. Check out Sajax, http://www.modernmethod.com/sajax/, a framework to 
automate javascript calling your server-side functions.  It
was made for PHP, but looks to have a python version as well.

All of those but #2 require you to set up some kind of server.  Is 
there a reason it has to be an HTML page?

If not, making a GUI might be an alternative that sidesteps this 
altogether.

Yikes, that sounds pretty hairy. Maybe this kind of thing is not as 
straight forward as anticipated. Why HTML you say? Well I've been 
intrigued by Dashboard, which will be in the next OSX release. It 
allows you to create "widgets" which are essentially little html pages 
that do things. This got me thinking how I'd like to tie a small Python 
script I wrote into an html front end (ideally becoming a widget). It's 
looking like this may be trickier than anticipated. In any case, 
thanks.

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


[Tutor] readline module

2005-03-25 Thread kedar thangudu
hi,
   I hav been working on python in developing pyshell... I am using
python readline module to for the command line..  Can u help me with
how to replace or erase the current readline line-buffer..  the
readline module just provides a insert_text func which is appending
the text to the line-buffer.. but i want to replace the contents of
the line-buffer to something else.. how can i do this ?

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


Re: [Tutor] Python and Javascript

2005-03-25 Thread Danny Yoo


> Yikes, that sounds pretty hairy. Maybe this kind of thing is not as
> straight forward as anticipated. Why HTML you say? Well I've been
> intrigued by Dashboard, which will be in the next OSX release. It allows
> you to create "widgets" which are essentially little html pages that do
> things. This got me thinking how I'd like to tie a small Python script I
> wrote into an html front end (ideally becoming a widget). It's looking
> like this may be trickier than anticipated. In any case, thanks.

Hi Mike,

Interesting!

You probably know about this already, but PyObjC allows you to write Mac
OS X Cocoa applications in Python:

http://pyobjc.sourceforge.net/

and this is a well tested bridge to make Python classes integrate into
Cocoa applications.  For example,

http://www.pycs.net/bbum/2004/12/10/#200412101

mentions the use of PyObjC to make a Mac OS X screensaver.  So it appears
to embed very well.


According to the documentation from Apple's Dashboard developer site, we
can embed Cocoa bundles into Javascript (there's a brief mention of it
under "Custom Code Plug-ins":

http://developer.apple.com/macosx/tiger/dashboard.html

So in theory, we should be able to inject a Pythonified Cocoa bundle into
Dashboard, but then again, I've never tried this before.  *grin*


I haven't dived into doing Mac OS X development yet, but perhaps someone
on the PyObjC list might be able to cook up a quick-and-dirty example of
this for you.

Try asking on their list and see if you get some useful responses:

http://lists.sourceforge.net/lists/listinfo/pyobjc-dev


Best of wishes to you!

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


[Tutor] How to include Python Script

2005-03-25 Thread Terry Johnson



How does one go about including 
Python scripts in html documents. I have been looking it up on google but can't 
seem to find a suitable answer!!??
Thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python and Javascript

2005-03-25 Thread Mike Hall
Danny, great reply. I have looked a bit at pyObjC, and it does indeed 
look cool. I was however hoping to bypass that route altogether and go 
for the simplicity (I thought) that came with the html/js route. 
Perhaps a cocoa bundle is the only way to get what I'm after. Thanks,

-MH
On Mar 25, 2005, at 1:40 PM, Danny Yoo wrote:

Yikes, that sounds pretty hairy. Maybe this kind of thing is not as
straight forward as anticipated. Why HTML you say? Well I've been
intrigued by Dashboard, which will be in the next OSX release. It 
allows
you to create "widgets" which are essentially little html pages that 
do
things. This got me thinking how I'd like to tie a small Python 
script I
wrote into an html front end (ideally becoming a widget). It's looking
like this may be trickier than anticipated. In any case, thanks.
Hi Mike,
Interesting!
You probably know about this already, but PyObjC allows you to write 
Mac
OS X Cocoa applications in Python:

http://pyobjc.sourceforge.net/
and this is a well tested bridge to make Python classes integrate into
Cocoa applications.  For example,
http://www.pycs.net/bbum/2004/12/10/#200412101
mentions the use of PyObjC to make a Mac OS X screensaver.  So it 
appears
to embed very well.

According to the documentation from Apple's Dashboard developer site, 
we
can embed Cocoa bundles into Javascript (there's a brief mention of it
under "Custom Code Plug-ins":

http://developer.apple.com/macosx/tiger/dashboard.html
So in theory, we should be able to inject a Pythonified Cocoa bundle 
into
Dashboard, but then again, I've never tried this before.  *grin*

I haven't dived into doing Mac OS X development yet, but perhaps 
someone
on the PyObjC list might be able to cook up a quick-and-dirty example 
of
this for you.

Try asking on their list and see if you get some useful responses:
http://lists.sourceforge.net/lists/listinfo/pyobjc-dev
Best of wishes to you!

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


Re: [Tutor] Tkinter and keyboard output

2005-03-25 Thread Michael Lange
On Thu, 24 Mar 2005 18:05:25 -
"Igor Riabtchuk" <[EMAIL PROTECTED]> wrote:

> Hi, 
> 
> I was playing around with Tkinter bindings and I got a question which is 
> probably not particularly bright.
> 
> If I have the following code, it works because I specifically code a function 
> for  keypress:
> 
> from Tkinter import *
> 
> class CRED(Frame):
> def __init__(self):
> Frame.__init__(self)
> self.txt=Text(self)
> self.txt.bind('', self.conv)
> self.txt.pack()
> self.pack()
> self.txt.focus()
> 
> def conv(self,event):
> self.txt.insert(END,'t')
> return 'break'
> 
> app=CRED()
> app.mainloop()
> 
> What if instead of coding , I coded   - how would I implement 
> the function which would allow the code to determine which 'Key' was pressed 
> after Alt?
> 
> Thank you.
> Igor

Do you mean something like this:

>>> from Tkinter import *
>>> r=Tk()
>>> t=Text(r)
>>> t.pack()
>>> 
>>> def  test(event):
... print event.keysym
... 
>>> t.bind('', test)
'1077686180test'
>>> x # Alt-x was pressed

?

Michael


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


Re: [Tutor] Python and Javascript

2005-03-25 Thread Alan Gauld
> > If you are using WSH on Windows and have the Python active
scripting
>
> I'm on OSX, and would be doing this through Safari most likely.

oca might be capable of doing it but I know very little about oca,
maybe some other Mac users can help? But I don't think it can be
done inside Safari.

Alan G.

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


Re: [Tutor] Python and Javascript

2005-03-25 Thread Alan Gauld

> Ryan, I should clarify that what I'd like to do here is unrelated to
> the web. I'm actually just interested in using a local html page as
a
> simple gui to launch python calls. So a JS event handler, say a
button
> click, would then call a JS function which inside of it would call a
> Python function while handing it arguments

In that case an http call to a Python web service is probably the
easiest solution. Have it all running locally.

Alan G.

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


Re: [Tutor] Python and Javascript

2005-03-25 Thread Alan Gauld
> intrigued by Dashboard, which will be in the next OSX release. It
> allows you to create "widgets" which are essentially little html
pages

There is an API for Dashboard and I'm pretty sure MacPython will
support it - it covers most of the cocoa type stuff. You might be
better checking out the Apple developer site for the Dashboard
hooks and loooking at MacPythons options.

Alan G.

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


Re: [Tutor] How to include Python Script

2005-03-25 Thread Alan Gauld
> How does one go about including Python scripts in html documents. 

You can only do it on Windows based IE pages. Even then theres 
little real advantage. The snag is the person reading your pages 
has to have both Python installed and active scripting enabled. 
Very few regular users have that.

> I have been looking it up on google but can't seem to find a 
> suitable answer!!??

There is a sample in Mark Hammonds book on Python for win32.

But in general its a bad idea, better to stivck to JavaScript 
for html scripting if you want any kind of portability.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to use MySQLdb.cursor

2005-03-25 Thread Vicki Stanfield
>> below to print the data returned from my query, but I would like to
> make
>> labels at the top of the columns. How do I do this dynamically?
>
> You shouldn't, it makes your code very vulnarable to changes in the
> database!
> Its the same principle as using 'select * from...', a bad idea in
> production code. And if you know which columns you are selecting you
> by definition know what labels to use.
>
> And another reason why its a bsad idea is that databvase columns often
> have weird abbreviated names that you don't want to expose users to.
>
> Alan G.
>

I am just trying to write code to demonstrate this capability in Python.
If I am actually in a position where I have access to the database schema,
I would not do so. I agree with your comments.

Vicki

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


Re: [Tutor] Python and Javascript

2005-03-25 Thread Mike Hall
On Mar 25, 2005, at 4:53 PM, Alan Gauld wrote:
intrigued by Dashboard, which will be in the next OSX release. It
allows you to create "widgets" which are essentially little html
pages
There is an API for Dashboard and I'm pretty sure MacPython will
support it - it covers most of the cocoa type stuff. You might be
better checking out the Apple developer site for the Dashboard
hooks and loooking at MacPythons options.
Alan G.

Alan, thanks for pointing me towards a few good approaches to look at. 
Going through some of the developer information I've come across 
mention of JS extensions which allow for system calls within a JS 
function, which should pretty much do what I want. Thanks,

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


Re: [Tutor] a shorter way to write this

2005-03-25 Thread Sean Perry
Smith, Jeff wrote:
For all the talk of Python only having one way to do something which is
why it's so much better than Perl, I've counted about 10 ways to do this
:-)
Knowing you said this at least half in jest, I still feel the need to 
comment.

In any programming language, you have flexibility in how to define an 
algorithm. Think about how many different ways you can ask "is this 
string a in that string b?".

The Python motto is actually better stated: there is one obvious way and 
it is often the right one.

In the Python 1.5 days, choices were much fewer. With the new versions 
have come a more rich selection of features.

Python's "only one way" is often brought up as a counterpoint to Perls 
TIMTOWTDI. Remembering which Perl idiom is the right one, for the right 
situation *AND* matches the rest of the project can be a real nightmare. 
Not to mention that choosing the wrong one often kills your performance 
(-: This is a big reason why I stick to Python. We may have choices, but 
they are often clear and obvious once you know the language.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor