Skip to content

Session 1: Tools I

Juan Gonzalez-Gomez edited this page Feb 3, 2020 · 130 revisions

Session 1: Tools-I

  • Time: 2h
  • Date: Wednesday, Jan-29th-2020
  • Goals:
    • Brief introduction to Github
    • Create your first repository
    • Learn how to access to github from the Pycharm IDE
    • Testing the "Hello world" program in Pycharm

Contents

Introduction

In the Programming in Network Environment subject we will learn how to create programs capable of communicating one to other through Internet

But first, we need to learn about the tools we are going to use: Github, Python, Firefox and Pycharm (Community Edition)

Watching the Teacher's screen on your computer

This Subject is mainly practical: the only way to learn how to program is programming! The Teacher will be using the computer all the time. In order to watch it better, you can connect to the Teacher's desktop to see all the stuff. Just follow these steps:

(Note for the Teacher: Execute the script share-screen)

  • Click on the upper left corner, in the Activities menu
  • In the search box write: "Visor". You will see the icon in the left. Click on it

  • You will seee the Desktop Viewer blank Window:

  • Click on Conectar (in the top)
  • Select the VNC protocol (Protocolo VNC)
  • Click on Buscar (Search): There appear all the machines to which desktop you can connect. Chose the one that says something like "Obijuan's remote desktop" (Escritorio remoto de Obijuan)
  • Finally, click on Conectar (Connect), in the bottom right corner

All the process can be seen in this animation:

You are watching the Teacher's screen now! My recomendation is that you place the Teacher's screen on your right in real size. Use the left space for working. The Teacher's desktop is in low resolution (because of the connection to the projector). Therefore you should have enough room in your screen for seeing all the details very clearly. You will not miss anything!

Having the Teacher's windows on your desktop will allow you to take screenshots whenever you want or even record your desktop! It is your choice!

We are now ready to start our journey!

Introduction to Github

Modern programming projects have thousand of lines of code and hundred of developers working on them. It is necessary to use powerful tools to manage it: The revision control systems. The projects are located into a what we call a repository

Currently, the two most used controls systems are Github and GitLab. Both are based on the opensource engine called git, developed in 2005 by Linux Torvalds, the creator of the Linux Kernel


In this subject we will focus on the use of Github

Our Github's account

  • Create your account on Github. Do not forget to send your real name and your username in the post in Aula Virtual

  • Once you have your account, Github will enable a link to your profile. This is my link (username: Obijuan):

  • Link: https://github.com/obijuan

You can find a repository tap in the top. It will show you all your current repos. In your profile page Github places the most popular repos

Playing with repositories

Just to get familiar with Github, Let's see some of my repositories. Click on the 3D-parts repo. This is the one I use for sharing all my 3D printable designs. Anyone can download them, and print them on a 3D printer

The designs are stored in their own folders. We can organize every repository as we want, using all the folder we need. In the bottom part you will see images and texts. They are the contents of the README.md file, that we can add optionally in every folder or repo

Let's continue our journey through the repos!. Let's enter into this folder: 2017-10-09-urjc-logo

There are 3 files and 3 folders. Most of the files can be seen directly just by clicking on them. For example, let's click on the Logo_URJC.png file

As it is a png file, Github shows it as an image. If we want to download it, just press the Download button in to top right

Any file from the repository can be downloaded. There is no need for checking in. No account is needed for browsing the report or for downloading any files from them

Let's see now this other file: logo-urjc.svg. It is a drawing, in the SVG vectorial format

As this is a text file (the SVG files are written in plain text), instead of the Download button there is a RAW Button. If we want to download it, just click with the right mouse button and select the save link as option

If we enter the STL folder and click on the file urjc-coin.stl, as it is a 3D design, Github will render it and allow us to rotate it and change the point of view

In this animation you can see our journey though this repository

Wikis

Optionally, a wiki page can be added to any repo for storing the documentation, and making the information more accesible to the users. For example, in the previous 3D-part repo, I have added a index with the most import 3D parts

If you want to access the wiki, just click on the wiki tab

The wiki's link is also available: https://github.com/Obijuan/3D-parts/wiki. It allows us to access it as if a standar. It looks like this:

Now we can browse normally. The documentation that you are reading now, from the Programming in Network Environment subject, is located its own wiki page at Github 🙂

From the 3D parts wiki, enter to the this design: URJC Keyring/coin. It is located on the same folder we saw before, but now you are looking at the wiki, that is easier for the users

In this animation you can see our journey though the wiki

Downloading all the files in a repo

You can download either individual files from the repo or all of them. Let's try the second option with this repo RISC-V-FPGA (We do not use the previous repo because is too big and it will take us more time)

In the main page of the repo, click on the Clone or Download green button

Select the DOWNLOAD ZIP option in the bottom of the new windows

The browser will show us another window with the file to download. Click on the OK button

The complete process is shown in this animation

In our Download folder there wil be the file RISC-V-FPGA.zip, which contains all of the files and folders of that repo. But, Be carefull!. It only contains the latest version of the files, not the repo itself. All the information regarding changes, revisions and versions is not there

We can decompress this file as usual to have access to all the files and folders

Creating our working repo

In this subject, all your work will be stored in a public Github repo. In this section we will create it from a template repository, which has beeen already configured

This is the repository. Click on the link to enter: 2019-2020-PNE-Practices

You can see al the folders were you should store all your practices and work, as well as the README.md file. The firs step is to create a copy of this template repo in your Github's account. Press the green button: Use this template

Now we should chose a new for the repo. In the upper left part you will see your username (in the pictures you will see mine (obijuan) but you should see yours) Use the same name of the template repo: 2019-2020-PNE-Practices

Write an optional description for the repo and click on the Create repository from Template button.

You will see a this screen:

After some seconds your repository will be ready. You can have a look at your main github page. It should looks like this, but with your username instead of mine:

Our repo is ready for working!

In this animation you can see all the process:

Working with Pycharm and Github

For developing in python we are going to use the PyCharm IDE (Community edition 2019.3.2) It is already installed in the Linux machines of the Lab

Opening PyCharm

Go to the activities menu in the top left corner and write pycharm. Click on the PyCharm icon. CAUTION! In the lab there are two version of PC. We will use the Community Edition (which is OpenSource). Make sure the icon looks like this one:

If this is the first time it is opened, some configuration windows will appear

Select Do not import setting and click on OK. Another window will apper asking you to select the Pycharm color theme

I will use the default theme: Darcula. Click on Next:Featured plugins

In this windows just click on Start using PyCharm. The initial window will show up:

Cloning our working repo

We are going to work on our working repository that is in Github. The first step is called cloning the repo. It will copy the repository from github and creates a local copy in our computer.This operation is executed only one time per computer. If you are working in two computers, one in this Lab and the other at home, you will have to clone twice: one per computer

From the main Pycharm windows we select the option Get from version Control

This window shows up. It is asking us for the location of our project in the repository:

We change to the Browser and go to our working repo main page at Github and click on the Clone or download button

Click on the copy button on the right side

Then move to the Pycharm Windows and paste the URL by pressing Ctrl-V on the keyboard:

Then select below the local folder were you want to store your project. By default it is located in the PycharmProjects folder in your home. I will use the default folder. Click on the Clone button

Now you will be asked if you want to open the new project

Click on YES. The main window appears along with a welcome message

Click on the Close button. We are ready to work on our projects!

In this animation the whole process is shown

Our first commit to the repo

If you click on the top left 2019-2020-PNE folder, you will see all the contents of your working project, with all the folder for the practices

Before doing our first python program, let's make some changes to the README file and upload into our remote repository in Github. Click on the small button in the README file for viewing it as text

Add some text. Notice how the color of the README file has changed. It is indicating that a change has been made: The current README file is different than the file stored in the repository

Let's commit that change. Click on the green tick in the upper right corner

Everytime you commit a change, you should write a comment. In this case we can write my first change. Then click on the commit button

Congrats! You've done your first commit!

But you have committed your change to the local repository in your computer. It is not yet in github. You have to execute a push command. Click on the option at VCS/Git/Push

In the next screen click on the Push button

Enter your Github username and password. Then click on Log In

After some seconds your changes will be pushed

Congrats! You've made your first contribution to your remote github repository!

If you go to your github repo page you will see your changes

Hello world with Pycharm!

Let's write our first python program in Pycharm. The first step is to configure the python environment.

Configuring the python environment

We have to chose which python interpreter to use. Go to File/Setting and click on the Project:2019-2020-PNE-Practices/Project Interpreter option

Click on the configure button on the right:

And then on the ADD option

Select the first option: Virtual Environmnet. Check New Environment and click on OK

And click again on OK for closing the Settings Windows

The python interpreter is ready! You will see the new folder called venv. It contains the python interpreter and all the related files needed

Writing the Hello world program

Let's write our first Hello world program in Pycharm. First we create a new folder called Hello for working there

Creating the Hello Folder

In the the Pycharm project windows select the 2019-2020-PNE-Practices project

and click on the File/New Menu

Then select the Directory option

and type in the new directory file: Hello. Press the Enter Key

In our project view, on the right we can see now the new empty folder: Hello

In animation the process of creating a new folder is shown:

Creating a python file

Let's create our first python file. Make sure the Hello folder is selected, as we want to create our new file in that directory. Then click on File/new/python file

Type in the filename. For example hello. It is not necessary to introduce the extesion (.py)

As we are working with the Github repo, Pycharm ask us if we want this new file to be added to our local repository. Click on ADD. Sometimes you just want to make a quick test on a temporary file. In that case you do not want it to be included in the repo. But in this case, we want to include our hello.py file into the repo for learning

We can see now the new hello.py file in the project view on the right side

Also, the file is opened on a new tap. We are now ready for typing python code!. Look at the green tick in the upper right corner. It tells us that the hello.py is ok, with no errors

Notice the color of the new file: green. It means that is a new file, added to the project, but it is not already in the repository, because we still have not committed the file

In this animation the process of creating a new python file is shown

Writing the code

It is time to write the hello world program. It consist of two print stataments:

print("This is my first python program in the Pycharm IDE")
print("Hello world!!")

In the hello.py tab type start typing in the code

When the editor detects you have written the first letters, pr, it shows below some of the functions that matches. You can continue typing or pressing the TAB key for accepting the first choice

Once the print() function is typed, the editor will show us all the possible parameters that the print function has. The first one (values) are variables or strings we want to print

We continue writing the string we want to print. Finish pressing the enter key. You should see something like this, with the green tick on the right

Imagine that you make a mistake typing the print. You delete accidentaly the final bracket. In that case you will see a red exclamation and a red mark on the location where the error is detected

If you place the mouse pointer on the red line below the red exclamation, you will get more information about the error

Let's finish our program. Write the second line

A typical error is to add a blank new line after the last line. The python style guide recommends not to do that. Therefore, you will see a warning in that case:

If you place the pointer in the upper mark, you will read that there is 1 week error

If you place the pointer in the second mark on the right, you will see the error message

In this animation the process of writing the hello world is shown

In this animation some errors are forced and corrected

Running the program

The first time a file is created, cannot be executed directly. The buttons for running and debugging cannot be pressed. Also there is an option that says: add configuration

Instead of creating a custom configuration, we will let Pycharm to create one for us. Click on Run/Run

Click on the hello option

The program will run. A new run tab is open in the bottom. There you will see the messages we have printed. Also notice that now there is a new configuration, called hello and the run and debug green buttons are activated

Once the configuration has been created, running the program is straightforward. Just click on the green run button in the toolbar

Adding the program to the github repo

Now that the program is working, it is time to add it to our repo. First we have lo add it to our local repo (commit) and then pushing it to the remote repo at Github

  • Click on the commit button

  • Add a comment to the commit and press the commit button

Notice that now the hello.py file is NOT green anymore. It has the same color than the other files in the local repo

  • Now go to VCS/Git/Push and click on Push

After some seconds, you will see the confirmation message. Our local repo has been pushed to Github!

  • Go to the browser and check that the changes are in Github

If you enter into the Hello folder, the hello.py file will be there

Congrats! You have created, running and committed to the Github repo your first python program

Exercises

The only way of mastering something is practicing, practicing and practicing!

Ex-1: hello.py

  • Add another print() statement to the hello.py program
  • Add some comments
  • Make sure you get a green tick
  • Run the program
  • Upload the changes to your github repo

Ex-2: count.py

  • In the 2019-2020-PNE-Practice project create a new folder called: Session-01
  • Inside the Session-01 folder, write a python the count.py program that prints the number from 1 to 20 in the console
  • Upload that new file in your github repo

Ex-3: sum20.py

  • Inside the Session-01 folder, write the python sum20.py program for calculating the sum of the first 20 integer numbers (1+2+3+...+20). The result should be printed on the console
  • Upload that file in your github repo

END of the session

The session is finished. Make sure, during this week, that everything in this list is checked!

  • You have a Github account
  • You have sent me your real name and Github user name
  • You have the 2019-2020-PNE-Practices working repo created in your account
  • You know how to write and run programs in Pycharm
  • You have uploaded the Hello folder with the hello.py program to the repo
  • Exercise 1 done!
  • Exercise 2 done!
  • Exercise 3 done!

Author

Credits

  • Alvaro del Castillo. He designed and created the original content of this subject. Thanks a lot :-)

License

Links

Clone this wiki locally