0

I'm trying to find the most efficient way to create different function name myfunction_a ,.. b , c with slightly different code ( input file name 'app/data/mydata_a.csv' ) so here below is the a function I got

def myfunction_a(request):

    os.getcwd()  # Should get this Django project root (where manage.py is)
    fn = os.path.abspath(os.path.join(os.getcwd(),'app/data/mydata_a.csv'))
    # TODO: Move to helper module
    response_data = {}
    data_format = 'tsv'
    if data_format == 'json':
        with open(fn, 'rb') as tsvin:
            tsvin = csv.reader(tsvin, delimiter='\t')

            for row in tsvin:
                print 'col1 = %s  col2 = %s' % (row[0], row[1])
                response_data[row[0]] = row[1]
        result = HttpResponse(json.dumps(response_data), content_type = 'application/json')
    else:
        with open(fn, 'rb') as tsvin:
            buff = tsvin.read()
        result = HttpResponse(buff, content_type = 'text/tsv')
    return result

I want to be able to loop through my list and create multiple function name:

mylist = ['a','b','c' ... 'z' ]

def myfunction_a(request): ... ( 'app/data/mydata_a.csv' ) return request

to get final result of :

  • def myfunction_a => taking 'app/data/mydata_a.csv'
  • def myfunction_b => taking 'app/data/mydata_b.csv'
  • def myfunction_c => taking 'app/data/mydata_c.csv'

right now I just copy and past and change it. is there a better to do this ? Any recommendation would be appreciated. Thanks.

2
  • Why do you need to do this? mywiki.wooledge.org/XyProblem Commented Nov 7, 2014 at 20:43
  • @gnibbler I use this function name as part of views.py in Django and use url to pull each function as needed, calling views app from Django template. Commented Nov 7, 2014 at 20:46

2 Answers 2

3

you can add a variable to a string with

"app/data/mydata_%s.csv" % (character)

so

for character in mylist:
    print "app/data/mydata_%s.csv" % (character)

should append everytime another charcter at the place of %s

So since you want for every function use another string to get another file you can do something like this:

def myfunction(label, request):
    return "app/data/mydata_%s.csv" % (label) 

so you get the function label at the end of your documentpath. Since you described that you only want to change the name so that it equals to the function label, you only need another parameter and not a new function name

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @muthan that's part of it, but i'm also looking for create that function name def "myfunction_a" "myfunction_b" .. as well.
for the function you cann put the a,b,c into a parameter so that you have a functioncall like def myfunction(character,request), the function do every time the same besides the different naming, this should work. I will add that to my answer.
2

If you must have a special function name, you could do this. Though why you'd need to I'm not sure.

import functools, sys
namespace = sys._getframe(0).f_globals

def myfunction(label, request):
    print request
    return "app/data/mydata_%s.csv" % (label)

my_labels = ['a','b','c']
for label in my_labels:
    namespace['myfunction_%s'%label] = functools.partial(myfunction, label)

print myfunction_a('request1')
print myfunction_b('request2')

Output is this:

request1
app/data/mydata_a.csv
request2
app/data/mydata_b.csv

Or possibly a better implementation would be:

class MyClass(object):
    def __init__(self, labels):
        for label in labels:
            setattr(self, label, functools.partial(self._myfunction, label))
    def _myfunction(self, label, request):
        print request
        return "app/data/mydata_%s.csv" % (label)

myfunction = MyClass(['a','b','c'])
print myfunction.c('request3')

Output is this:

request3
app/data/mydata_c.csv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.