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 -> modelsand pressenter. ThePFis 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
.pyto the given filename.Enter
todos = []on the first line. Pressentertwice to give a blank line.Use Live Templates to add a main run block with
Ctrl-J mainthenenter. (macOS:Cmd-J)When the cursor is under the
if, type inpopulate_todos().Add another line under that one, in the
ifblock, withprint(todos). Use autocompletion to finishtodos.Click in the
populate_todosidentifier. PyCharm gives a red lightbulb. It’s angry.Alt-Enterto open the code intentions and chooseCreate function 'populate_todos'.When the red box surrounds
pass, pressenter.Run
models.pyby 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-Sto open Preferences (macOS:Cmd-,)- Start typing
liveto filter preferences by the stringlive - Expand
Pythonand click on some of the other Live Templates - Clear the
livefilter and enterintentions - To the left of the search box, click the icon to collapse all
- Expand the
Pythonsection - Click
Cancelto dismiss the Preferences dialog
Your
models.pyshould 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¶
- What directory does
New Fileput 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