Data Model

In our previous step we made a list of todos, in a very simple manner. Let’s turn that into the start of a proper “models” file. As we do so, we’ll see some PyCharm productivity in action – Live Templates and Quick Fixes.

Source for this step | View video/audio walkthrough

Steps

  1. Create a new file with Alt-Insert -> PF -> models and press enter. The PF is a way PyCharm lets you narrow a list with camel case for the first letters in the words in a list. (macOS: Cmd-N)

  2. Answer “Yes” to put under version control. PyCharm automatically adds .py to the given filename.

  3. Enter todos = [] on the first line. Press enter twice to give a blank line.

  4. Use Live Templates to add a main run block with Ctrl-J main then enter. (macOS: Cmd-J)

  5. When the cursor is under the if, type in populate_todos().

  6. Add another line under that one, in the if block, with print(todos). Use autocompletion to finish todos.

  7. Click in the populate_todos identifier. PyCharm gives a red lightbulb. It’s angry.

  8. Alt-Enter to open the code intentions and choose Create function 'populate_todos'.

  9. When the red box surrounds pass, press enter.

  10. Run models.py by right-clicking in the editor and choosing Run 'models'. You should now have 2 tabs in the Run Tool window.

  11. Find the other Live Templates and Code Intentions in Preferences:

    • Ctrl-Alt-S to open Preferences (macOS: Cmd-,)
    • Start typing live to filter preferences by the string live
    • Expand Python and click on some of the other Live Templates
    • Clear the live filter and enter intentions
    • To the left of the search box, click the icon to collapse all
    • Expand the Python section
    • Click Cancel to dismiss the Preferences dialog
  12. Your models.py should match the following:

    models.py in Data Model
    todos = []
    
    
    def populate_todos():
        pass
    
    
    if __name__ == '__main__':
        populate_todos()
        print(todos)
    

Analysis

Now that we have two files, we can do more with PyCharm. We saw:

  • New File. Letting PyCharm do the work for us when creating a new file, including registering with VCS.
  • Live Templates. PyCharm can automate some of the repetitive work, and you can even write your own.
  • Quick Fixes. Rather than stop what you are doing, create your function, then come back...go ahead and type it in a usage and let PyCharm make the function. Use it, then define it.

Extra Credit

  1. What directory does New File put the new file?
  2. Does the camel case trick work with more than just the first letter?
  3. How to do the placeholders work in Live Templates?
  4. Can you share Live Templates?