[Tutor] differences between map and partial?

source liu sourceonly at gmail.com
Tue Sep 27 22:56:59 EDT 2016


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 <dyoo at hashcollision.org> wrote:
> On Tue, Sep 27, 2016 at 2:23 AM, source liu <sourceonly at gmail.com> 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


More information about the Tutor mailing list