[Tutor] differences between map and partial?
Hi, List the test code as attached first i tried map, and failed, then i turned to partial from functools the latter one works, i wonder if there is unseen tricky from me between them :) this one works print p.map(partial(file_op,lineop=unity),input) this one doesn't work print p.map(lambda x:file_op(x,unity), input) Thanks. -- Liu An Institution of modern physics, Shanghai, China ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] differences between map and partial?
Hi, Danny, Thank you for your reply I checked the mail, the attachment is attached, i don't know why you can't see it ( I change the extension of .tar.gz to .tar.gz_src, as gmail can't pass the security check of the .tar.gz) I would like to inline the code as well as attached again( the are all simple files ) -- accouting -log1 -log2 logtool -__init__.py -tools.py run.py log1,log2 ( write anything you like, it pretend to be log file ) __init__.py :blank --tools.py #!/bin/env python def identy(string): print string; def unity(string): return 1 def line_op(string,op=identy): return apply(op,(string,)) def file_op(filename,lineop): f=open(filename) res=None; try: res=map(lambda x:line_op(x,lineop),f) finally: f.close() return res run.py-- import os #log_location="/home/source/python/pg/accounting" log_location='accounting' from multiprocessing import Pool from functools import partial if __name__=="__main__": from logtool.tools import * p=Pool(processes=4); for root,dir,file in os.walk(log_location): input=map(lambda x: os.path.join(root,x),file) print p.map(partial(file_op,lineop=unity),input) #print p.map(lambda x:file_op(x,unity), input) == THAT'S ALL OF THE CODE Thank you On Wed, Sep 28, 2016 at 3:26 AM, Danny Yoo wrote: > On Tue, Sep 27, 2016 at 2:23 AM, source liu wrote: >> Hi, List >> >> the test code as attached > > > Unfortunately, it didn't attach. If you can inline the content of the > test code, that would be helpful. > > >> this one works >>>>print p.map(partial(file_op,lineop=unity),input) >> this one doesn't work >>>>print p.map(lambda x:file_op(x,unity), input) > > > I don't know what the function signature of file_op is. This is an > important detail! > > > > I would expect: > > p.map(partial(file_op,lineop=unity),input) > > and: > > p.map(lambda x:file_op(x,lineop=unity), input) > > to be functionality equivalent. > > > > However, I do not know, without seeing file_op, whether: > > p.map(lambda x:file_op(x, lineop=unity), input) > > versus: > > p.map(lambda x:file_op(x, unity), input) > > Depending on method signature, this might not be equivalent, > especially if file_op takes in multiple keyword arguments. > > > > Also, I don't know what you mean by "works" vs. "doesn't work": those > are human expressions that are ambiguous enough that I only know > something is wrong, but I don't know what. :) Can you be more > specific? If you see error messages or stack traces, please post > them, as they are additional "signal" that may be helpful in figuring > out what's happening. -- Liu An Institution of modern physics, Shanghai, China ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Feature or ... after python 2.7
Hi,list Yesterday, someone hacked in my server and running some bad thing , but I do find something interesting After I inspected processes, I found if you are using python version above 2.7. (2.7.16 if I remembered it correctly), one could run python script with perl $cat test.py #!/bin/env python print "I am python" $perl test.py I am python But it would fail if you tried to use python2.6(which go with rhel6.8) really interesting ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Feature or ... after python 2.7
On Wed, 25 Jan 2017 at 17:07 Alan Gauld via Tutor wrote: > On 25/01/17 02:55, source liu wrote: > > > 2.7. (2.7.16 if I remembered it correctly), one could run python script > > with perl > > > > $cat test.py > > #!/bin/env python > > print "I am python" > > > > > > $perl test.py > > I am python > > > > That is just because this particular print statement > is compatible with Perl. If you had tried anything > more complex then it would have bombed. > How to you define complex? The script import lots of modules such as urllib2 > > > But it would fail if you tried to use python2.6(which go with rhel6.8) > > I'm not sure what you mean here but you could run the > code above in any version of Python v2 or perl and > it should work. The first6 line is a comment so > ignored by both perl and python. And the print > statement works in both languages. My fault, the PYTHONPATH is not correct with my Python 2.6 , which misleading me to think Python 2.6 can't do this > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Liu An Institution of modern physics, Shanghai, China ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor