Re: Try Python update

2006-01-03 Thread browerg
I like the form, no matter what its limitations may be. Three notes:

It might be a good way to catch newbi mistakes (those are the kind I
make :P, thereby providing a feedback loop to improved error messages.

I had no trouble with from math import *  followed by print pi, but
there was no >>> prompt after the result appeared .. is that part of
the 'closures' thing mentioned earlier?

Then I followed you "type 'help'" suggestion, which had me in stitches
(sorry, it's been a long day and the Knob Creek was kicking in) -- I
got exactly the message you'd expect, about help not being recognized
and maybe help() would work. That's what triggered the idea for
trapping errors.

Nice work!

George

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


scope question in a switch mixin

2008-01-11 Thread browerg
The code that follows is the result of noodling around with switches as a 
learning tool. I've played with python for a few years, but I'm self-taught, so 
. . .

Class Switch builds a set of functions. Method switch executes one of them 
given a value of the switch variable.

My question is, why are modules imported at the top of the program not visible 
to the functions Switch builds? There is
no problem when the import is in the function, but I thought initially that 
imports at the top would be in its globals.

The import that works is at line 111 in the code.

Thanks in advance!

George

'''Mixin Switch provides function switch(key) that executes an appropriate 
function.

   Each instance can: use a different switch variable.
  be used as many places in a program as desirable.
  be changed in one place, no matte how many places it is 
used.

   Usage: inst = Switch(keys, vals, base)
   whose arguments are sequenes:
   keys has switch values ('su', 'mo', . . .),
   base has the shared fore and aft parts of instance functions, and
   vals has the individual parts of instane functions.

   Example: Suppose you want to switch on days of the week:
keys = ('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa', 'de')
vals = ('Sundayis Comic-day.', 'Mondayis Moan-day.',
'Tuesday   is Twos-day.',  'Wednesday is Hump-day.',
'Thursday  is Shop-day.',  'Fridayis TGIF-day.',
'Saturday  is Food-day.',  'Anything else is Party-day!')
fore = "def %s(self, *args):\n\tprint '"
aft  = "'\\n"

produces functions of the form:
def su(self, *args):\\n\\tprint 'Sundayis Comic-day.'\\n
or, for humans:
def su(self, *args):
print 'Sundayis Comic-day.'

Test code (below) for this example produces:
Sundayis Comic-day.
Mondayis Moan-day.
. . .
Anything else is Party-day!
key {}  keys must be hashable (immutable) objects.

   Example: Suppose you want to swith on a function and its argument.
Test code (below) returns calculated values using functions like:
def %s(self, *args):\\n\\timport math\\n\\ttmp = (args[0] / 
math.pi)\\n\\treturn tmp\\n
or, for humans:
def %s(self, *args):
import math
tmp = (args[0] / math.pi)
return tmp
that produce:
In toplevel: circ.switch(dC,10), d = 3.18309886184
In toplevel: circ.switch(Cd,  3.18), C = 9.99026463842
In toplevel: circ.switch(rC, 5), r = 0.795774715459
In toplevel: circ.switch(Cr, 0.796), C = 5.00141550451
In toplevel: circ.switch(A , 5), A = 78.5398163397

   Thanks to Jean-Paul Calderone for his post at
   http://mail.python.org/pipermail/python-list/2007-June/446648.html
   in response to a question by vasudevrama t
   http://mail.python.org/pipermail/python-list/2007-June/446618.html
   '''

#import math

class Switch(object):

   def __init__(self, keys, vals, base):
   self.dictionary = {}
   tmpd = {}
   for i in range(len(vals)):
   func = ''.join([base[0] % keys[i], vals[i], base[1]])
   compile(func, '', 'exec')
   exec(func, tmpd)
   for k, v in tmpd.items():
   if k in keys:
   self.dictionary[k] = v

   def switch(self, key, *args, **kwargs):
   try:
   result = self.dictionary[key](self, *args, **kwargs)
   except KeyError:
   result = self.dictionary['de'](self, *args, **kwargs)
   return result

if '__main__' == __name__:
   '''Case 1: execute a statement.
   '''
   keys = ('su', 'mo', 'tu', 'we', 'th', 'fr', 'sa', 'de')
   vals = ('Sundayis Comic-day.', 'Mondayis Moan-day.',
   'Tuesday   is Twos-day.',  'Wednesday is Hump-day.',
   'Thursday  is Shop-day.',  'Fridayis TGIF-day.',
   'Saturday  is Food-day.',  'Anything else is Party-day!')
   fore = "def %s(self, *args):\n\tprint '"
   aft  = "'\n"
   base = (fore, aft)
   day = Switch(keys, vals, base)
   for k in keys:
   try:
   day.switch(k)
   except TypeError:
   print 'key %s %s keys must be hashable (immutable) objects.' % (k, 
type(k))
   for k in ('xx', 1234, 12.3, {}):
   try:
   day.switch(k)
   except TypeError:
   print 'key %s %s keys must be hashable (immutable) objects.' % (k, 
type(k))

   '''Case 2: execute an expression.
   '''
   keys = ('dC', 'Cd', 'rC', 'Cr', 'A', 'de')
   vals = ("(args[0] / math.pi)",   # diameter given Circumference
   "(math.pi * args[0])",   # Circumferene given diameter
   "(args[0] / (2 * 

Re: Re: scope question in a switch mixin

2008-01-16 Thread browerg
John,
Thanks for writing, and I'm sorry it's taken so long to get back to you. Python 
is fun for me -- dinner guests and my boss got in the way.

>> The code ... is the result of noodling around with switches as a learning 
>> tool. I've played with python for a few years, but I'm self-taught, so . . .
>> My question is, why are modules imported at the top of the program not 
>> visible to the functions Switch builds? There is
>> no problem when the import is in the function, but I thought initially that 
>> imports at the top would be in its globals.

>John wrote:
>The global namespace of the to-be-exec'ed code is the dictionary that 
>you supply. That dictionary doesn't contain "math".

Thank you for the guidance about namespaces and exec.

>It's a long time since exec has been documented as a *function*. Why? 
>Because it isn't:

. . . and the code snip.

>What book/tutorial did you get that from?

Hey, I'm not going to pass that off on anyone. As usual, self-education means 
missing a lot. 

I'm exploring your suggestions and working on my code. 

Thanks again.

George

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