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¶
Create a new file with
Alt-Insert -> PF -> models
and pressenter
. ThePF
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
)Answer “Yes” to put under version control. PyCharm automatically adds
.py
to the given filename.Enter
todos = []
on the first line. Pressenter
twice to give a blank line.Use Live Templates to add a main run block with
Ctrl-J main
thenenter
. (macOS:Cmd-J
)When the cursor is under the
if
, type inpopulate_todos()
.Add another line under that one, in the
if
block, withprint(todos)
. Use autocompletion to finishtodos
.Click in the
populate_todos
identifier. PyCharm gives a red lightbulb. It’s angry.Alt-Enter
to open the code intentions and chooseCreate function 'populate_todos'
.When the red box surrounds
pass
, pressenter
.Run
models.py
by right-clicking in the editor and choosingRun 'models'
. You should now have 2 tabs in the Run Tool window.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 stringlive
- Expand
Python
and click on some of the other Live Templates - Clear the
live
filter and enterintentions
- To the left of the search box, click the icon to collapse all
- Expand the
Python
section - Click
Cancel
to dismiss the Preferences dialog
Your
models.py
should match the following: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¶
- What directory does
New File
put the new file? - Does the camel case trick work with more than just the first letter?
- How to do the placeholders work in Live Templates?
- Can you share Live Templates?
- Previous topic: Productivity
- Next topic: Todo Dicts