Lambda alternative?

2009-04-15 Thread mousemeat
Hello all,

I really like the lambda function for a particular task i have: I am
writing some simulation software, working with many different
materials. Each material with many different properties, some are
temperature dependent, others aren't, some are defined by a function,
some are a lookup table, some are constant. Here is a simplified
version:

class material(object):
def __init__(self,density):
self.density=density

airdensity=lambda T:10/(287*T)
air=material(airdensity)

steeldensity=lambda T:interp(T,[0,1000],[7856,7813])
steel=material(steeldensity)

rockdensity=lambda T:5000
rock=material(rockdensity)

I really like doing things this way, because i can then define other
properties within the class and i can always use the syntax:
specificgravity_at_273K=stone.specificgravity(273), whether
specificgravity is a function or a parameter. (not sure thats the
right nomenclature, but i think you'll know what i mean.)

But, lambda functions can't be pickled. I would like to pickle my
objects, and i would really like to use parallel python (which
requires pickling).

The best way i can see to do that is to make a new class which
inheirits the 'material' class for each physical material i want to
model, but surely i should be creating instances rather than classes.

Is this going to present some new problems that anyone can forsee? Can
anyone think of a better way to do this?  I keep reading that lambda
functions aren't particularly pythonic and that there are always
alternatives, but they seem very elegant to me.

Thanks for your time,

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


Re: Lambda alternative?

2009-04-15 Thread mousemeat
On 15 Apr, 16:27, Piet van Oostrum  wrote:
> > [email protected] (m) wrote:
> >m> Hello all,
> >m> I really like the lambda function for a particular task i have: I am
> >m> writing some simulation software, working with many different
> >m> materials. Each material with many different properties, some are
> >m> temperature dependent, others aren't, some are defined by a function,
> >m> some are a lookup table, some are constant. Here is a simplified
> >m> version:
> >m> class material(object):
> >m>     def __init__(self,density):
> >m>         self.density=density
> >m> airdensity=lambda T:10/(287*T)
> >m> air=material(airdensity)
> >m> steeldensity=lambda T:interp(T,[0,1000],[7856,7813])
> >m> steel=material(steeldensity)
> >m> rockdensity=lambda T:5000
> >m> rock=material(rockdensity)
>
> The following isn't much different (only 6 chars more per def):
>
> def airdensity(T): return 10/(287*T)
> air=material(airdensity)
>
> def steeldensity(T): return interp(T,[0,1000],[7856,7813])
> steel=material(steeldensity)
>
> def rockdensity(T): return 5000
> rock=material(rockdensity)
>
> --
> Piet van Oostrum 
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: [email protected] Hide quoted text -
>
> - Show quoted text -

Will i then be able to pickle 'rock', and any object that may contain
'rock' as one of it's fields?  I'm not sure that i will, but i'll give
it a shot.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda alternative?

2009-04-16 Thread mousemeat
On 16 Apr, 09:39, Duncan Booth  wrote:
> Paul Rubin  wrote:
> > Duncan Booth  writes:
> >> dumped = dumps(air)
> >> t = loads(dumped)
> >> print t # works fine
>
> > Hmm, well, that doesn't really pickle the function; it pickles a class
> > instance that records where the class definition was and (on
> > unpickling) imports that module.  Maybe that is sufficient for this
> > purpose.
>
> It pickles a class instance which contains a reference to a function and
> on unpickling it recreates the class instance with a new reference to
> the function of the same name.
>
> That's how pickling works: it records where the class or function is
> defined and creates a new reference to the class or function of the same
> name when you unpickle. The classes and functions have to be defined at
> module scope otherwise it won't be able to reference them. You don't
> ever actually get either a class or function in the pickle so if you
> want you can update the code and the unpickled object gets the new one.
>
> >>> import pickle
> >>> class C(object):
>
>         def __init__(self, fn):
>                 self.fn = fn
>         def callfn(self):
>                 self.fn()
>
> >>> def foo():
>
>         print "Hello I'm foo"
>
> >>> inst = C(foo)
> >>> inst.callfn()
> Hello I'm foo
> >>> p = pickle.dumps(inst)
> >>> def foo():
>
>         print "I'm a new foo"
>
> >>> inst2 = pickle.loads(p)
> >>> inst2.callfn()
> I'm a new foo
> >>> inst.callfn()
>
> Hello I'm foo
>
> --
> Duncan Boothhttp://kupuguy.blogspot.com


Thank you for everyone's explanations, help and interest on this one.
I have reworked my code as described and promised myself not to use
lambdas ever again (i still think they are an elegant idea, but if
they are incompatible with frequently used modules, then the
inelegance of reality quickly overshadows the elegance of the theory).

I think my problem now is that parallel python will want to unpickle
my objects but can't find the modules.  I've asked the pp forum for
insight, but i think i'm going to have to plug away with this one.

Thanks again,

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


Re: What IDE support python 3.0.1 ?

2009-04-16 Thread mousemeat
Use eclipse with the pydev module.  I use python(x,y) which is a big
bundle of most of the python stuff you could possibly want (including
scientific stuff, but its not mandatory to use it) configured to work
together.  It uses python 2.5.

You can have the best of both worlds.  Search for 'from __future__
import'  to see how to get 3.0 features into 2.x, minimizing the
eventual upgrade hassles.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lambda alternative?

2009-04-16 Thread mousemeat
On 16 Apr, 10:21, Hrvoje Niksic  wrote:
> mousemeat  writes:
> > Thank you for everyone's explanations, help and interest on this
> > one.  I have reworked my code as described and promised myself not
> > to use lambdas ever again (i still think they are an elegant idea,
> > but if they are incompatible with frequently used modules, then the
> > inelegance of reality quickly overshadows the elegance of the
> > theory).
>
> I think you're going too far concluding that lambdas are unusable.
> lambdas are a problem only when they are stored as data attributes
> that need to be pickled, but that's far from being the only use case.
> You can still use them for what they're meant to be used: tiny anonymous
> function-expressions, typically passed as parameters to functions that
> expect a callback.
>
> Avoiding lambdas because they're unpicklable is like avoiding bound
> methods because they're just as unpicklable.

Correct me if i am wrong, but i can pickle an object that contains a
bound method (it's own bound method).  I cannot pickle an object that
contains a lambda function.  Doesn't that make lambda's less
pickleable?  (I don't mean to be argumentative, i'm trying to
understand python's syntax a little better.)
--
http://mail.python.org/mailman/listinfo/python-list