2

I'm trying to write a helper function to help me generate new file with function definition. Here are part of the code:

def new_function_file(file_name, fun_name, arguments):
  f = open(file_name + ".py", 'w')
  f.write("\tdef " + fun_name + str(("self", ) + arguments) + ":\n")

new_leetcode_file("testfile", "test", ("arr1", "arr2"))

However, this would generate "def test('self', 'arr1', 'arr2'):" in testfile.py. I was wondering how to properly parse the arguments without single quote generated?

4
  • What are you actually trying to achieve? Writing Python source code to a file from Python is only useful in rare circumstances. Commented Aug 9, 2015 at 20:03
  • Well... I want to write a helper function for online coding challenge, where I need to generate a template for unit-tests of the question along with the question description and the function definition. So I can save some time when doing the coding challenge for job interview. Commented Aug 9, 2015 at 20:39
  • I'd suggest to use some templating engine for this – you'll end up with much more readable code for the code generation. Commented Aug 9, 2015 at 21:04
  • The main issue is with str(("self", ) + arguments) + ":\n") If you get rid of the str( and closing ), you are creating a tuple with ("self", ) That means the expression ("self", ) + arguments will return a tuple. Then str gives you a string representation of that tuple, which is why you get the parentheses in your output. Commented Aug 9, 2015 at 21:09

3 Answers 3

3

Formatted printing might be useful here, along with breaking things down a bit:

def new_function_file(file_name, fun_name, arguments):

    f = open(file_name + ".py", 'w')

    # expand the arguments and join with "," separators:
    args = ", ".join(arguments) 
    # use formatted print to put it all together nicely:
    write_string = "\tdef {fn}(self, {ar}):\n".format(fn=fun_name, ar=args)

    f.write(write_string)

For your demo input:

new_leetcode_file("testfile", "test", ("arr1", "arr2"))

The "writestring" will be:

'\tdef test(self, arr1, arr2):\n'

and you can write this directly to the file without any other punctuation.

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

1 Comment

I voted this up rather than the third answer rather than @ForceBru because, while both correct, I think this is more pythonic and compact.
1

Here you're writing a tuple into your file while you have to write the contents of this tuple.

def new_function_file(file_name, fun_name, arguments):
   f = open(file_name + ".py", 'w')
   f.write("\tdef " + fun_name+"(")
   arguments=["self"].extend(list(arguments))
   for x in arguments:
      if x!=arguments[-1]: f.write(x+",")
      else: f.write(x)
   f.write("):\n")

1 Comment

I upvoted the other one because using join() is more compact/pythonic. join() is the first thing I thought of as well.
1

You'd want '(' + ', '.join(('self',) + arguments) + ')' rather than str(('self',) + arguments).

With that said, there's probably a better way to accomplish whatever it is that you're trying to do ... I'll echo the comment by @SvenMarnach

"Writing Python source code to a file from Python is only useful in rare circumstances."

What are you actually trying to accomplish with this new python source file that you're generating?

2 Comments

Aren't you missing a ) after arguments?
@ForceBru -- Thanks :-)

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.