0

I've created several functions

#generate .h file template for class
def generateHeaderClassTemplate(name):
    template = open('header.template', 'r')
    code = parseHeaderTemplate(template.read(), name)
    template.close()
    return code

#generate .cpp file template for class
def generateImplClassTemplate(name):
    template = open('class.template', 'r')
    code = parseImplTemplate(template.read(), name)
    template.close()
    return code

#generate main.cpp file template
def generateMainTemplate():
    template = open('main.template', 'r')
    code = parseMainTemplate(template.read())
    template.close()
    return code

But the problem is that they are all basically the same

1. open file
2. write there some function result
3. close file
4. return data

So I want to create some universal function which will have function as parameter, filename as parameter and parameter for the function. And then function like generateHeaderClassTemplate will be something like

def generateHeaderClassTemplate(name):
    return generateTemplate(parseHeaderTemplate, 'header.template', name)

Basically I want function like this

def generateTemplate(func, templateFile, name):
    template = open(templateFile, 'r')
    code = func(template.read(), name) #or code = func(template.read()) if name was not passed
    template.close()
    return code

But how to make it work as in comment I wrote - If I do not pass name - perform func(template.read()) and if I pass it - perform func(template.read(), name)?

3 Answers 3

1

You can use default value for the parameters:

def generateTemplate(func, templateFile, name=None):

this way you can use generateTemplate with or without a third argument:

generateTemplate(parseImplTemplate, 'f')
generateTemplate(parseMainTemplate, 'f', 'foo)

And in the generateTemplate definition, you can do something like:

def generateTemplate(func, templateFile, name=None):
    template = open(templateFile, 'r')
    if name:
        code = func(template.read(), name) 
    else:
        code = func(template.read())
    template.close()
    return code
Sign up to request clarification or add additional context in comments.

2 Comments

So I need to check presense of name parameter anyway?
If you want to handle the case that No function is specified in the function call, Yes you have to check name.
1

Using python optional arguments

http://www.diveintopython.net/power_of_introspection/optional_arguments.html

When you declare the function you can pass default values to arguments, so if you don't pass them into the function when you call it by default it will use those parameters, like this example:

def info(object, spacing=10, collapse=1):

spacing defaults to 10 and collapse to 1

3 Comments

Oh! So I should make parseMainTemplate have name parameter with default value?
That way you can unify everything in one function. One great thing about python is that python has higher-order functions, which means that name can even be a function! For example when you use the sentence sorted(), one of the parameters, called key, defines a function which the algorithm uses to sort the components of one list. Maybe you can use that to write independent functions and then pass them to the higher order one as you need it
Exactly, but also consider @fredtantini's code example on how to handle the default case.
1

Perhaps a default value to the parameter name would help you

def test(a, b = ""):
    print (a, b)

test ("hello")
test ("hello", "world")

Your template methids must always have the same prototype, generateTemplateFun(name="") but when calling them you can skip the name part making the code cleaner

2 Comments

Jup, I was too slow when writing, started writing whithout answers
I know that situation, very disappointing.. While you're writing an answeryou can always check the area below the question. It shows seomthing like "2 new answers", even if you do not refresh the page :)

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.