Re: subexpressions
"Peter Otten" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > What syntax would you suggest for a lambda enhanced to cover your use > case? > I suppose you will end up with roughly the same number of characters, all > crammed in one line -- or broken into lines at a random position as it > happens with overambitious list comprehensions. Agree, this argument is strong. -- http://mail.python.org/mailman/listinfo/python-list
Re: subexpressions
"Peter Otten" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Please help, is there way to use sub-expressions in lambda? For example, if I want to calculate sin(x^2)+cos(x^2) I must code: lambda x: sin(x*x)+cos(x*x) How to make x*x to be evaluated once? >>> >> (lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5) >> + >>> cos(.5*.5) >>> True >>> >>> The real answer is of course: Use a function. >> >> But what about something like >> >> lambda x: sin(y)+cos(y) where y=x*x >> >> ? >> May be this could be a PEP? If there is no straight way to do this. > > def f(x): >y = x*x >return sin(y) + cos(y) > > What is not straightforward about that? This code is needed once in a map, so I don't want 3+ extra lines. Solution seemed so simple... I always considered python as languague, where simple things do not require extensive coding. Moreover, this construction is common thing in functional programming. -- http://mail.python.org/mailman/listinfo/python-list
Re: subexpressions
"Peter Otten" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Sergey Dorofeev wrote: > >> Please help, is there way to use sub-expressions in lambda? >> For example, if I want to calculate sin(x^2)+cos(x^2) I must code: >> lambda x: sin(x*x)+cos(x*x) >> How to make x*x to be evaluated once? > >>>> (lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5) + > cos(.5*.5) > True > > The real answer is of course: Use a function. But what about something like lambda x: sin(y)+cos(y) where y=x*x ? May be this could be a PEP? If there is no straight way to do this. -- http://mail.python.org/mailman/listinfo/python-list
subexpressions
Hello all! Please help, is there way to use sub-expressions in lambda? For example, if I want to calculate sin(x^2)+cos(x^2) I must code: lambda x: sin(x*x)+cos(x*x) How to make x*x to be evaluated once? -- http://mail.python.org/mailman/listinfo/python-list
Re: f---ing typechecking
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > It's effectively a tuple with field names. I don't know when the switch > occurred (it's in 2.2, as far back as my built interpreter versions > currently go), but back in the day os.stat used to return a plain old > tuple. posix.stat_result is CLASS, not regular tuple. classes is the only way in python to approximate records/structs (may be dict can be considered as record too, if we agree with strange syntaxis). To consider tuples as struct, one must have a GREAT imagination, as tuples functionally are _arrays_, with only difference from lists as freezeness - the have NO filed names. > I have no idea if the schizophrenic personality of tuples will improve > with > drugs^H^H^H^H^H Python 3, but I wouldn't be at all surprised if it did. I think the best thing is to be more democratic when asked to add two arrays :) -- http://mail.python.org/mailman/listinfo/python-list
Re: f---ing typechecking
"James Stroud" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >[1]+(1,) >> >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: can only concatenate list (not "tuple") to list >> >> >> Its ugly and boring. > > Agreed. This would be similar to: > > py> 1 + 1.0 > > Traceback: can only add int to int. Etc. > > But then again, the unimaginative defense would be that it wouldn't be > python if you could catentate a list and a tuple. Maybe, but I don't think that it is best side of the languague. If we want constant objects like tuple why not to specify it explicitly? If we want lists as dict keys, why not to use copy-on-write? -- http://mail.python.org/mailman/listinfo/python-list
f---ing typechecking
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> (1,)+[1] Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate tuple (not "list") to tuple >>> [1]+(1,) Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate list (not "tuple") to list >>> Its ugly and boring. -- http://mail.python.org/mailman/listinfo/python-list
Re: module email
"Rob Wolfe" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >> p2=email.message.Message() >> p2.set_type("message/rfc822") >> p2.set_payload(m) > > Payload is a _list_ of Message objects (is_multipart() == True) > or a _string_ object (is_multipart() == False) but never just Message > object. 1. If I give m.as_string() into set_payload, result is the same. 2. I don't understand, how message/rfc822 section can accept a list of two or more messages. Can you explain? -- http://mail.python.org/mailman/listinfo/python-list
module email
Hello. Why does not work? -- import email.message import smtplib import time m=email.message.Message() m.set_type("multipart/mixed") m["From"]="Sergey Dorofeev <[EMAIL PROTECTED]>" m["To"]="Sergey Dorofeev <[EMAIL PROTECTED]>" m["Date"]=time.asctime() m["Subject"]="test" p1=email.message.Message() p1.set_payload("message text", "us-ascii") m.attach(p1) p2=email.message.Message() p2.set_payload("message text 2", "us-ascii") m.attach(p2) del p1,p2 print m.as_string() print "*"*50 x=email.message.Message() x.set_type("multipart/mixed") x["From"]="Sergey Dorofeev <[EMAIL PROTECTED]>" x["To"]="Sergey Dorofeev <[EMAIL PROTECTED]>" x["Date"]=time.asctime() x["Subject"]="test" p1=email.message.Message() p1.set_payload("message text !", "us-ascii") print p1 x.attach(p1) p2=email.message.Message() p2.set_type("message/rfc822") p2.set_payload(m) print p2 x.attach(p2) print "*"*50 print x.as_string() -- I'm using Python 2.5 -- http://mail.python.org/mailman/listinfo/python-list
treating MIME messages
Hello all. Anybody who has idle time, please look at my version of MIME tools at http://www.fidoman.ru/prog/mime/. Questions and critical notes are welcome. If somebody will consider that interesting, I will refine these modules and put to pypi. -- http://mail.python.org/mailman/listinfo/python-list
struct.(un)pack and ASCIIZ strrings
I can use string.unpack if string in struct uses fixed amount of bytes. But is there some extension to struct modue, which allows to unpack zero-terminated string, size of which is unknown? E.g. such struct: long, long, some bytes (string), zero, short, short, short. -- http://mail.python.org/mailman/listinfo/python-list