[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

scope question in a switch mixin

browerg

1/11/2008 3:19:00 PM

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 = ('Sunday is Comic-day.', 'Monday is Moan-day.',
'Tuesday is Twos-day.', 'Wednesday is Hump-day.',
'Thursday is Shop-day.', 'Friday is 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 'Sunday is Comic-day.'\\n
or, for humans:
def su(self, *args):
print 'Sunday is Comic-day.'

Test code (below) for this example produces:
Sunday is Comic-day.
Monday is Moan-day.
. . .
Anything else is Party-day!
key {} <type 'dict'> 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/4...
in response to a question by vasudevrama t
http://mail.python.org/pipermail/python-list/2007-June/4...
'''

#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, '<stderr>', '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 = ('Sunday is Comic-day.', 'Monday is Moan-day.',
'Tuesday is Twos-day.', 'Wednesday is Hump-day.',
'Thursday is Shop-day.', 'Friday is 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 * math.pi))", # radius given Circumference
"(2 * math.pi * args[0])", # Circumference given radius
"(math.pi * args[0]**2)", # Area given radius
"False")
# Why are modules imported at the top not found in these functions?
fore = "def %s(self, *args):\n\timport math\n\ttmp = "
aft = "\n\treturn tmp\n"
base = (fore, aft)
circ = Switch(keys, vals, base)
vs = (10, 3.18, 5, 0.796, 5)
kvs = zip(keys, vs)
for k, v in kvs:
result = circ.switch(k, v)
print "In toplevel: circ.switch(%-2s, %5s), %s = %s" % (k, v, k[0], result)


18 Answers

John Machin

1/11/2008 8:23:00 PM

0

browerg@verizon.net wrote:
> 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 global namespace of the to-be-exec'ed code is the dictionary that
you supply. That dictionary doesn't contain "math". More comments below.

[snip]

>
> #import math
>
> class Switch(object):
>
> def __init__(self, keys, vals, base):
> self.dictionary = {}
> tmpd = {}

Uncomment the above import, remove the import from your constructed
source code and try
tmpd = {'math': math}

> for i in range(len(vals)):
> func = ''.join([base[0] % keys[i], vals[i], base[1]])
> compile(func, '<stderr>', 'exec')
> exec(func, tmpd)

compile returns a code object, which you are throwing away. Comment out
that statement, and the behaviour of your code will not change. This is
because if the first argument to exec is a string, it is compiled into a
code object.

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

>>> exec "print 'xxx'"
xxx
>>> exec("print 'xxx'")
xxx
>>> foo = compile
>>> foo
<built-in function compile>
>>> foo = exec
File "<stdin>", line 1
foo = exec
^
SyntaxError: invalid syntax
>>>

What book/tutorial did you get that from?

Cheers,
John

Liberal $500 million tax dollar disaster

7/23/2014 11:10:00 PM

0

On Wednesday, July 23, 2014 6:44:46 PM UTC-4, Obveeus wrote:
> On 7/23/2014 6:35 PM, RichA wrote:
>
> > Don't you need at least a B.S. degree to do that job? Maybe not in the U.S.
>
>
>
> Pharmacy Reps are not people who work at the pharmacy. Pharmacy Reps
> are people who go 'door to door' of doctor's offices and try to sell
> them on the idea of prescribing their elixir rather than some other>
> company's elixir.

Fascinating. I know good-looking women are often used in that capacity, for obvious reasons.

Anim8rFSK

7/23/2014 11:44:00 PM

0

In article <lqpdt2$mal$1@dont-email.me>, Obveeus <Obveeus@aol.com>
wrote:

> On 7/23/2014 6:35 PM, RichA wrote:
> > Don't you need at least a B.S. degree to do that job? Maybe not in the U.S.
>
> Pharmacy Reps are not people who work at the pharmacy. Pharmacy Reps
> are people who go 'door to door' of doctor's offices and try to sell
> them on the idea of prescribing their elixir rather than some other
> company's elixir. The Pharmacy Reps are given sales pitches to memorize
> and talking pints to spew, but they don't have to have any actual
> knowledge of the product. In fact, it probably is better if they don't
> know too much since the idea is to get the doctor to learn about the
> drug (Hey, Doc, we can sent you to a week in Club Med to 'learn' about
> our products at the drug conference if you start a 'trial' and sign on
> 100 patients in the next 3 months)...the drug samples come with the
> pertinent 'literature' if the doctor cares about those nitty gritty details.

They're almost always pretty girls, and they show up at lunch time
carrying food goodies to bribe the staff with.

--
Wait - are you saying that ClodReamer was wrong, or lying?

Michael Black

7/24/2014 12:38:00 AM

0

Steve Bartman

7/24/2014 1:53:00 AM

0

On Wed, 23 Jul 2014 18:44:46 -0400, Obveeus <Obveeus@aol.com> wrote:

>
>
>On 7/23/2014 6:35 PM, RichA wrote:
>> Don't you need at least a B.S. degree to do that job? Maybe not in the U.S.
>
>Pharmacy Reps are not people who work at the pharmacy. Pharmacy Reps
>are people who go 'door to door' of doctor's offices and try to sell
>them on the idea of prescribing their elixir rather than some other
>company's elixir. The Pharmacy Reps are given sales pitches to memorize
>and talking pints to spew, but they don't have to have any actual
>knowledge of the product. In fact, it probably is better if they don't
>know too much since the idea is to get the doctor to learn about the
>drug (Hey, Doc, we can sent you to a week in Club Med to 'learn' about
>our products at the drug conference if you start a 'trial' and sign on
>100 patients in the next 3 months)...the drug samples come with the
>pertinent 'literature' if the doctor cares about those nitty gritty details.

Your info on this job field is at least fifteen years out of date.

Steve

David Johnston

7/24/2014 2:10:00 AM

0

On 7/23/2014 4:35 PM, RichA wrote:
> Don't you need at least a B.S. degree to do that job? Maybe not in the U.S.
>


Not in Canada either. Most of them have do have one but it's less of a
requirement than an asset.

Barry Margolin

7/24/2014 9:10:00 AM

0

In article <anim8rfsk-55C199.16441223072014@news.easynews.com>,
anim8rFSK <anim8rfsk@cox.net> wrote:

> In article <lqpdt2$mal$1@dont-email.me>, Obveeus <Obveeus@aol.com>
> wrote:
>
> > On 7/23/2014 6:35 PM, RichA wrote:
> > > Don't you need at least a B.S. degree to do that job? Maybe not in the
> > > U.S.
> >
> > Pharmacy Reps are not people who work at the pharmacy. Pharmacy Reps
> > are people who go 'door to door' of doctor's offices and try to sell
> > them on the idea of prescribing their elixir rather than some other
> > company's elixir. The Pharmacy Reps are given sales pitches to memorize
> > and talking pints to spew, but they don't have to have any actual
> > knowledge of the product. In fact, it probably is better if they don't
> > know too much since the idea is to get the doctor to learn about the
> > drug (Hey, Doc, we can sent you to a week in Club Med to 'learn' about
> > our products at the drug conference if you start a 'trial' and sign on
> > 100 patients in the next 3 months)...the drug samples come with the
> > pertinent 'literature' if the doctor cares about those nitty gritty
> > details.
>
> They're almost always pretty girls, and they show up at lunch time
> carrying food goodies to bribe the staff with.

On "Anger Management", the slutty patient is a pharmaceutical rep.

--
Barry Margolin
Arlington, MA

Ashton Crusher

7/26/2014 5:38:00 AM

0

On Wed, 23 Jul 2014 18:44:46 -0400, Obveeus <Obveeus@aol.com> wrote:

>
>
>On 7/23/2014 6:35 PM, RichA wrote:
>> Don't you need at least a B.S. degree to do that job? Maybe not in the U.S.
>
>Pharmacy Reps are not people who work at the pharmacy. Pharmacy Reps
>are people who go 'door to door' of doctor's offices and try to sell
>them on the idea of prescribing their elixir rather than some other
>company's elixir. The Pharmacy Reps are given sales pitches to memorize
>and talking pints to spew, but they don't have to have any actual
>knowledge of the product. In fact, it probably is better if they don't
>know too much since the idea is to get the doctor to learn about the
>drug (Hey, Doc, we can sent you to a week in Club Med to 'learn' about
>our products at the drug conference if you start a 'trial' and sign on
>100 patients in the next 3 months)...the drug samples come with the
>pertinent 'literature' if the doctor cares about those nitty gritty details.

That's the same way biz works, or at least used to, in the IT biz.
Pretty gals fronting for the people who do the actual work. They rub
up against the clients and you know the rest.

Michael Black

7/26/2014 2:09:00 PM

0

~consul

7/30/2014 5:33:00 PM

0

'tis on this 7/23/2014 6:44 PM, wrote Obveeus thus to say:
> On 7/23/2014 6:35 PM, RichA wrote:
>> Don't you need at least a B.S. degree to do that job? Maybe not in
>> the U.S.
> Pharmacy Reps are not people who work at the pharmacy. Pharmacy Reps
> are people who go 'door to door' of doctor's offices and try to sell
> them on the idea of prescribing their elixir rather than some other
> company's elixir. The Pharmacy Reps are given sales pitches to
> memorize and talking pints to spew, but they don't have to have any
> actual knowledge of the product. In fact, it probably is better if

I've got a few friends who model or act and they do this every now and
then, mainly at conventions, but if they want more steady gig, then
they ask for more.
--
"... respect, all good works are not done by only good folk. For here,
at the end of all things, we shall do what needs to be done."
--till next time, consul -x- <<poetry.dolphins-cove.com>>