Dave99
Dave99

Reputation: 105

Python, how can I run 2 or more functions with different values using map function?

I have a working function that uses a library that I want have multiple of this function ( 10 or more) having some different values in each function, in one Python file. B_id= and file.csv has different values in each function.

how can I do this?

here is an example code having only 2 of same functions:

from abc.zzz  import xyz
def func1(id_1='', B_id='12345678'):  
                return zzz.func1(B_id=B_id, id_1=id_1)

with open('file1.csv', 'r') as f:
                        for i, line in enumerate(f, start=1):
                            if i in range(0, 3):      
                                func1(line.rstrip()) 
                                time.sleep(25)

# second function,:

from abc.zzz  import xyz
def func1(id_1='', B_id='44556677'):         # here B_id is different 
                return zzz.func1(B_id=B_id, id_1=id_1)

with open('file2.csv', 'r') as f:                # here csv file is different
                        for i, line in enumerate(f, start=1):
                            if i in range(0, 3):      
                                func1(line.rstrip()) 
                                time.sleep(25)

Updated:

I have tried this to use map function, the B_id= may work, but I am not sure how to replace the file name, any help would be great.

def func1(id_1='', B_id=''):         # here B_id is different 
    bb = map(func1, [{'file1.csv':'66778899'}, {'file2.csv': '22334455'}])
                return zzz.func1(B_id=B_id, id_1=id_1)

with open('', 'r') as f:                # here csv file is different
                        for i, line in enumerate(f, start=1):
                            if i in range(0, 3):      
                                func1(line.rstrip()) 
                                time.sleep(25)

Updated 2:

Ok thanks for the code, I applied it, but I get no result nor errors, I have forgotten one piece in my code above, the missing part is section_id=

this is upgraded above code:

from abc.zzz  import xyz
    def func1(id_1='', B_id='12345678' ,section_id=None):  
               return zzz.func1(B_id=B_id, id_1=id_1, section_id=section_id)

    with open('file1.csv', 'r') as f:
                            for i, line in enumerate(f, start=1):
                                if i in range(0, 3):      
                                    func1(line.rstrip()) 
                                    time.sleep(25)

here is code that I ran and had no result:

path_id_map = [
    {'path':'file1.csv', 'id': '12345678'},
    {'path':'file2.csv', 'id': '44556677'}]
section_id = None
for pair in path_id_map:
    with open(pair['path'], 'r') as f:
     if pair in range(0, 3):      
        zzz.func1(id_1=f.readline().rstrip(), B_id=pair['id'], section_id=section_id)
            time.sleep(25)

Upvotes: 0

Views: 76

Answers (1)

001
001

Reputation: 13533

Here's an example using a dictionary and no wrapper function.

path_id_map = [
    {'path':'file1.csv', 'id': '12345678'},
    {'path':'file2.csv', 'id': '44556677'}]

for pair in path_id_map:
    with open(pair['path'], 'r') as f:
        for _ in range(0, 3):      
            zzz.func1(id_1=f.readline().rstrip(), B_id=pair['id'])
            time.sleep(25)

Upvotes: 1

Related Questions