Top Banner
LAB 1 – EXPLORING THE PYCHARM IDE Perkiraan pengerjaan lab: 45 menit ORI: https://confluence.jetbrains.com/pages/viewpage.action? pageId=51945983 https://confluence.jetbrains.com/pages/viewpage.action? pageId=51958847 PT. ABC merupakan perusahaan yang bergerak dibidang manufaktur yang berlokasi di Balikpapan. PT. ABC memiliki divisi IT yang sangat mendukung dalam proses bisnis perusahaan, salah satunya adalah dalam penyediaan aplikasi. Saat ini divisi tersebut sedang mengerjakan pembuatan web site perusahaan. Dari sekian banyak bahasa dan framework untuk kebutuhan tersebut, divisi IT PT. ABC memilih untuk menggunakan web framework Django yang berbasis bahasa python. IDE (Integrated Development Environment) yang akan digunakan dalam pembuatan web site ini adalah JetBrains PyCharm. Salah satu alasan yang mendasari penggunaan tool ini adalah karena PyCharm bukan text editor, melainkan sebuah IDE yang dilengkapi banyak fitur yang dapat meningkatkan kecepatan pembuatan web site berbasis framework Django, seperti diantaranya: IDE ini dilengkapi dengan library dari framework Django. Membantu melengkapi script yang sedang dituliskan (code completion) Memberikan tanda error secara langsung jika ada perintah yang salah diinputkan (on-the-fly syntax and error highlighting) Dapat mengganti nama variabel secara cepat dan terjaga konsistensinya dengan menggunakan refaktor (code refactoring) ExploringThe PyCharm IDE 1-1
27

Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

Jan 05, 2016

Download

Documents

Ari Pribadi

vff
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

LAB 1 – EXPLORING THE PYCHARM IDE

Perkiraan pengerjaan lab: 45 menit

ORI: https://confluence.jetbrains.com/pages/viewpage.action?pageId=51945983

https://confluence.jetbrains.com/pages/viewpage.action?pageId=51958847

PT. ABC merupakan perusahaan yang bergerak dibidang manufaktur yang berlokasi di Balikpapan. PT. ABC memiliki divisi IT yang sangat mendukung dalam proses bisnis perusahaan, salah satunya adalah dalam penyediaan aplikasi.

Saat ini divisi tersebut sedang mengerjakan pembuatan web site perusahaan. Dari sekian banyak bahasa dan framework untuk kebutuhan tersebut, divisi IT PT. ABC memilih untuk menggunakan web framework Django yang berbasis bahasa python. IDE (Integrated Development Environment) yang akan digunakan dalam pembuatan web site ini adalah JetBrains PyCharm.

Salah satu alasan yang mendasari penggunaan tool ini adalah karena PyCharm bukan text editor, melainkan sebuah IDE yang dilengkapi banyak fitur yang dapat meningkatkan kecepatan pembuatan web site berbasis framework Django, seperti diantaranya:

IDE ini dilengkapi dengan library dari framework Django.

Membantu melengkapi script yang sedang dituliskan (code completion)

Memberikan tanda error secara langsung jika ada perintah yang salah diinputkan (on-the-fly syntax and error highlighting)

Dapat mengganti nama variabel secara cepat dan terjaga konsistensinya dengan menggunakan refaktor (code refactoring)

Di dalam lab ini, Anda akan melakukan penelusuran IDE PyCharm sesuai dengan instruksi yang diberikan oleh Kepala divisi IT PT. ABC di awalah project ini

Lab ObjectivesAfter completing this lab, students will be able to:

Choose Look & Feel of my PyCharm Change color scheme of the editor Highlight code style violations Tune the PEP8 inspections Track PEP8 rules Create scopes Create inspection profile with these scopes Highlight errors Generate source code Reformat code Add documentation comments Type hinting

ExploringThe PyCharm IDE 1-1

Page 2: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

Lab ProceduresA. Choosing Look & Feel of My PyCharm

PyCharm comes with several pre-defined themes. The set of available themes depends on your platform. You can find the theme of your choice in the Appearance page of the Settings/Preferences dialog. Follow these steps to choose Look & Feel of my PyCharm.

1. On the main toolbar, click   to open the Settings/Preferences dialog, and then click Appearance. (no emng sejajar ama paragraph?) unt lab yg Django ini saja sejajar. Unt yg berikutnya ga.

2. On this page, click the drop-down list Theme, and select the one you like better:

Note the link Reset that appears on top of the dialog box - if you've changed your mind, click it to roll the changes back. Also note the button Apply that becomes enabled when you hover your mouse pointer over it:

ExploringThe PyCharm IDE 1-2

Page 3: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

You can also change the other settings that define look and feel of your PyCharm, for example, the font family and size, or windows behavior.

3. Apply changes. You might be suggested to restart the IDE (for example, when you change theme to or from Darcula):

ExploringThe PyCharm IDE 1-3

Page 4: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

B. Changing Color Scheme of the Editor

You have changed the theme of your PyCharm, but you are still not happy with the appearance of your code in the editor. For example, you would like another color for the documentation strings.

Follow these steps to change the color scheme of the editor: (font nomor beda, uda saya ganti)

1. Once more, on the main toolbar click   to open the Settings/Preferences dialog, expand the node Editor, and then click Colors and Fonts. Note that you cannot change your pre-defined color scheme - you must create its copy first: uda saya naikin

   Ini kan bisa di naikin ke atas kaya yang bapa suruh ke saya kemarenClick the button Save as, and type the name of your new color scheme: uda saya naikin

Now this new color scheme becomes editable, and you can proceed with changing it to your taste.

2. Expand the node Colors and Fonts, and find the page with the editor settings for Python:

ExploringThe PyCharm IDE 1-4

Page 5: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

First, in the list of language components, locate documentation strings. The preview pane shows a sample docstring with its current settings.

Suppose, you want to change the font color from dark green to something brighter. To do that, select the check box Foreground, and then right-click (or double-click) the color swatch:

ExploringThe PyCharm IDE 1-5

Page 6: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

Next, in the Color Picker, choose a color for the docstrings, so they really stand out:

Changes are applied, and you can enjoy the new color:

ExploringThe PyCharm IDE 1-6

Page 7: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

C. Highlighting Code Style Violations

Follow these steps to highlight the code style violations:

1. Open a new Python file for editing (F4) (for this exercise we use Solver.py). The file by default has two lines: the author and the project names. These lines appear because the file Solver.py is created by a file template that (in the case of Python files) contains definitions of these variables.

2. Start typing the keyword class. When you just start typing, PyCharm immediately shows the suggestion list to complete your code:

Refer to Code Completion page of the product documentation for details. The red curve marks the next expected entry - in this case, this is the expected identifier. Enter the class name Solver. The red curve moves after the class name. If you hover your mouse pointer over this curve, you see the error decription ("Colon expected"). Also, mind the red error stripe in the right gutter - it also marks the same error:

ExploringThe PyCharm IDE 1-7

Page 8: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

3. Type the colon (;), and press Enter. According to the Python code style, the next statement is indented. If by chance you press space after Enter, you will thus violate the code style settings.

D. Tuning the PEP8 Inspections

However, by default these violation are but week warnings, and as such, are not visible. So, at first, let's raise their importance. Follow these steps to tune the PEP8 inspections:

1. Click   on the mail toolbar, and on the Inspections page of the Settings/Preferences dialog, type PEP8 to find all PEP8-related inspections, and from the Severity drop-down list, choose Warning:

2. Apply changes and close the dialog. Now let's return to our source code.

E. Tracking PEP8 Rules

Now PyCharm stands on guard to protect your code style integrity. You immediately note that indented space is highlighted. Follow these steps to track the PEP8 rules:

1. Type the def demo(self,a,b,c):. Ensures, PyCharm shows the message from the PEP8 inspection as shown in the following figure:

ExploringThe PyCharm IDE 1-8

Page 9: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

As you can see, PyCharm supports PEP8 as the official Python style guide.

2. Press Ctrl+Alt+S, then choose Inspections, you will see that PyCharm launches the pep8.py tool on your code, and pinpoints the code style violations:

F. Creating Scopes

Look at the Inspections page  more attentively. If you have just opened this page, you see the default inspection profile   with the default settings: it means that the inspections apply to all the sources of the current project.

Let's try to customize this profile for two different scopes: In the Test scope, the spelling errors should be marked as typos

(green).

ExploringThe PyCharm IDE 1-9

Page 10: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

In the Production scope, the spelling errors should be marked as errors (red) - can we actually produce code with typos?

Follow these steps to create scopes.

1. Click   on the main toolbar, and in the Settings/Preferences dialog box, open the page Scopes to define the two scopes. (mana no 1 nya?)

2. Then click   and choose scope type Local:

3. In the Add New Scope dialog box, type the scope name (Test), and then, in the project tree, choose the directory to be included in the Test scope, test_dir.

Note that the Pattern field is filled in automatically, as you select the directory:

4. Repeat this process to create the Production scope.

ExploringThe PyCharm IDE 1-10

Page 11: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

G. Creating Inspection Profile with these Scopes

Follow these steps to create inspection profile with the scopes created before.

1. Create a copy of the default profile:

2. Give it a new name, for example, MyProjectProfile. This new profile is a copy of the default one, and has the same set of inspections.

3. With this new profile selected, let's locate the Spelling inspection and change it. To find the Spelling inspection (we've already done it before), just type spel in the search area.

4. Click   to add the Test scope, then   once more to add the Production scope:

ExploringThe PyCharm IDE 1-11

Page 12: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

5. In the scope "Test", the inspection severity is left as is (a typo); however, the the scope "Production" we'll choose "Error" from the list of severities:

Mind the color code of inspections. They are shown black if unchanged. If they are blue, then it means that they have been changed.

6. Apply changes and close the dialog. The modified inspection profile is ready. Its name is MyProjectProfile, and it has different settings for the Spelling inspection in the Test and Production scopes.

7. Choose Code Inspect Code on the main menu to inspect code against this profile

8. In the dialog box, choose the desired profile and scope:

ExploringThe PyCharm IDE 1-12

Page 13: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

9. Do it twice - for Test and Production scopes (if you want to preserve inspection results for further examination and sharing, you can export them).

10. Compare the inspection results for the two scopes:

As you can see, the typos in the files belonging to the Production scope are marked with the red curvy line, while in the Test scope the typos can be tolerated, and as such are marked green.

H. Highlighting Errors

Besides coding style violations, PyCharm highlights the other errors too, depending on the selected profile.

For example, if your inspection profile includes Python inspection "Unresolved references", and you use a symbol that not yet has been

ExploringThe PyCharm IDE 1-13

Page 14: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

imported, PyCharm underlines the unresolved reference and suggests to add import statement:

Refer to the auto-import tutorial  for the walkthrough, and to the product documentation. Anyway, you can change the error highlighting with some aid from Hector.

I. Generating Source Code

PyCharm provides lots of possibilities to automatically generate code. You can explore the auto-generation features in the product documentation and in the Auto-generating code tutorial.

Follow these steps to explore the main code generation procedures.

1. Delete all contents of the file Solver.py, and start from scratch.

2. Create an instance of a class:

ExploringThe PyCharm IDE 1-14

Page 15: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

As shown in the above figure, PyCharm has stubbed out a class:

3. Add a method to the class instance. To do that, type a dot after class instance, and then type the method name. This method does not yet exist, and PyCharm suggests to create one:

4. Type the source code. When it comes to calculate the discriminant, we have to extract a square root. There is a dedicated function sqrt in the library math, but it is not yet imported. Type it anyway, and see how PyCharm copes with it:

We've come to the source code like this:

ExploringThe PyCharm IDE 1-15

Page 16: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

However, it lacks some significant analysis. We'd like to analyze the radicand d. If it is zero or positive, then the discriminant and the equation roots will be calculated; when the radicand is negative, let's raise an exception. How PyCharm will help completing this task?

5. Surrounds a block of code with if construct. Select the statements to be completed, when d is non-negative:

6. Then, press Ctrl+Alt+T, or choose Code Surround With on the main menu. PyCharm shows a pop-up list of the possible constructs:

ExploringThe PyCharm IDE 1-16

Page 17: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

7. Select if option from the suggestion list. As you see, PyCharm automatically adds if True: and indents the selected lines:

8. We are not at all interested in a boolean expression, so let's change the selected True to d >= 0.

9. Place the caret at the end of the last line, and press Enter. The caret rests on the next line, with the same indentation as the if statement; type else clause here, and see PyCharm reporting about the expected indentation:

ExploringThe PyCharm IDE 1-17

Page 18: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

When you press Enter again, the caret rests at the indented position. Here you can type the exception expression, using PyCharm's powerful automatic code completion:

J. Reformatting code

Let's look again at our Solver.py file. Its right gutter shows lots of yellow stripes. When you hover your mouse pointer over a stripe, PyCharm shows the description of the corresponding problem in the code:

The good news is that they are but warnings, and won't affect the results. Bad news is they are too numerous to fix each one by one. PyCharm says - yes. This is the code reformatting feature.

Follow these steps to change formating of the entire file.

1. To invoke reformatting, just press Ctrl+Alt+L, or choose Code Reformat Code on the main menu.

2. In the Reformat Code dialog box, specify that you are going to change formatting of the current file only - and off we go! The PEP8-related errors are all gone.

ExploringThe PyCharm IDE 1-18

Page 19: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

3. Note that you can define formatting rules yourself. To do that, open the code style settings, select language (in this case, Python), and make the necessary changes:

K. Adding Documentation Comments

Formatting is fixed now, but there are still some yellow stripes left. If you hover your mouse pointer over sich a stripe, it is most likely to see something like "Missing docstring". The inevitable yellow light bulb   also shows the possibility to add a docstring comment:

ExploringThe PyCharm IDE 1-19

Page 20: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

Follow these steps to add documentation comments:

1. Choose Insert docstring from the suggestion list, and PyCharm adds a documentation comment in the format ReStructuredText:

Note that there are several docstring formats, and the documentation comments are created in the format, which you have selected in the Python Integrated Tools page. If you so wish, you can change the docstring format to Epytext, or plain text.

L. Type Hinting

The documentation comments can be used to define the expected types of parameters, return values, or local variables. Why do we need it all? For example, we'd like to keep under control the types of parameters we pass to the demo() method. To do that, let's add the corresponding information to the documentation comment:

ExploringThe PyCharm IDE 1-20

Page 21: Lab 1 - ExploringThe PyCharm IDE - 171015 NOK Roni

By the way, mind code completion in the documentation comments!

Next, when you look at the method invocation, you see that the wrong parameter is highlighted by the PyCharm's inspection "Type Checker":

ExploringThe PyCharm IDE 1-21