0

in my everyday job I write different models and for each model I need to create a python file like this:

class SomeModel:
    def define_params(self):
        return

    def define_input(self):
        return

    def run_model(self):
        return

I want to write a python script which will create a file with this format.

I tried:

import os

path = 'path_tol_folder_in_which_to_store_the_file'

if not os.path.exists(path):
os.makedirs(path)

with open(os.path.join(path, 'some_model.py'), 'w') as some_model:
some_model.writelines(
'class SomeModel:'
'    def define_params(csv):'
'        csv = ("PD", STRING)'
'        return csv'
)

When I run this code, the file is created but in there there is only one line:

class SomeModel: def define_params(csv): csv = ("PD", STRING) return csv (imagine this is one line)

and when I do ctrl+alt+L (format code) it does not format the code the way it should be.

Is there a way to do this so that the code format in the file is correct, i.e.

class SomeModel:
    def define_params(csv):
        csv = ("PD", STRING)
        return csv

Thanks

1
  • just a thought. You could inherit from a parent class. The parent class has methods for parameter and states definition. In the child class you only define the run method. Commented Dec 8, 2019 at 16:27

2 Answers 2

1

writelines expects an iterable of strings (but will also accept a single string, as it does here), and what you passed is actually a single string: Python implicitely concatenates strings that are separated by whitespace (spaces, newlines...)

>>> 'abc'    'def'
'abcdef'

You could pass it a list of strings (note the [] and commas separating the strings), but you would have to explicitely include a newline \n at the end of each line:

some_model.writelines([
'class SomeModel:\n',
'    def define_params(csv):\n',
'        csv = ("PD", STRING)\n',
'        return csv'
])

or more simply, a multiline string:

some_model.write("""
class SomeModel:
    def define_params(csv):
        csv = ("PD", STRING)
        return csv
""")
Sign up to request clarification or add additional context in comments.

5 Comments

writelines still requires newlines at the end of each line.
@MadPhysicist Oh, yes.. :/
Just slap x + '\n' for x in before your list
Yes... That is just so much easier to use triple quotes here...
The tripple quotes help. Thanks a lot!
1

Use triple quotes, which preserve blanks:

some_model.write(
'''class SomeModel:
    def define_params(csv):
        csv = ("PD", STRING)
        return csv
'''
)

2 Comments

writelines expects an iterable of strings ending in newlines. This magically works because strings are iterable, but you should really be calling write instead.
@MadPhysicist Right, it's copy-paste mistake :-)

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.