Scripting Languages Preparing for the exercises and exam - dtai kuleuven

Page created by Paula Terry
 
CONTINUE READING
Scripting Languages Preparing for the exercises and exam - dtai kuleuven
Scripting Languages
Preparing for the exercises and exam

Vincent Nys
September 21, 2016
Scripting Languages Preparing for the exercises and exam - dtai kuleuven
The command line
Scripting Languages Preparing for the exercises and exam - dtai kuleuven
Windows: PowerShell

                      1
OS X: Terminal

                 2
The command line vs. the REPL

  Installed programs and utilities can be run using the command line.
  Short snippets of Python code can be run using the
  read-evaluate-print-loop (which is, itself, a program).

                                                                        3
Installing Python
Check your version

     • First number should be 2
     • Second number should be 7
     • Third number does not matter
   3 > 2?
   Python 3 is a perfectly usable language, but it is not fully
   backwards-compatible with Python 2. If you have installed Python
   3, you will also need to install Python 2.                         4
Get the installer

                    https://www.python.org

                                             5
Select these options

                       6
Setting up a text editor
Getting Atom

               https://atom.io

                                 7
Use these settings

                     8
Your first program
Hello, World!

     • Create a new file in Atom
     • Type print "Hello, World!"
     • Save it in a separate folder for Scripting Languages as
       “helloworld.py”

                                                                 9
Run it!

   Assuming helloworld.py was saved under C:\Scripting Languages

     • Open a command line
     • Type cd "C:\Scripting Languages"
     • Type python helloworld.py

                                                                   10
A word of explanation

    • The command line “sees” a specific directory
    • The “path” to this directory is typically shown before what
      you type (like an address in your file system)
    • To change a directory, use cd (stands for “change directory”)
    • There is a space between “Scripting” and “Languages”, so
      put quotes around the entire path
    • cd "C:\Scripting Languages" works, cd C:\Scripting
      Languages does not

                                                                      11
Your second UNIX command: mkdir

    • Open your home folder (or “My Documents”) in your file
      browser and navigate to the same folder on the command line
      using cd.

                                                                    12
Your second UNIX command: mkdir

    • Open your home folder (or “My Documents”) in your file
      browser and navigate to the same folder on the command line
      using cd.
    • Type in mkdir something something.

                                                                    12
Your second UNIX command: mkdir

    • Open your home folder (or “My Documents”) in your file
      browser and navigate to the same folder on the command line
      using cd.
    • Type in mkdir something something.
    • Observe.

                                                                    12
Your second UNIX command: mkdir

    • Open your home folder (or “My Documents”) in your file
      browser and navigate to the same folder on the command line
      using cd.
    • Type in mkdir something something.
    • Observe.
    • What do you think mkdir stands for?

                                                                    12
Your second UNIX command: mkdir

    • Open your home folder (or “My Documents”) in your file
      browser and navigate to the same folder on the command line
      using cd.
    • Type in mkdir something something.
    • Observe.
    • What do you think mkdir stands for?
    • Navigate to your Scripting Languages folder and use mkdir to
      create directories “exercise session ...” for the next 8 exercise
      sessions.

                                                                          12
Your third UNIX command: ls

  Once you have created directories for the next sessions, enter ls.
  What do you think it stands for?

                                                                       13
Other useful tricks

     • Type in cd exe without hitting , then hit .
     • Complete the command by adding a number and hitting
       .
     • Go back to the parent directory with cd ...
     • With the command line still open, enter  twice.
     • Hit  to re-enter the directory.

   Powershell is not UNIX
   These tricks and commands are common to Powershell and UNIX
   shells and they should be sufficient for now, but do not rely on
   UNIX documentation for Powershell commands or vice versa.

                                                                      14
Learn Python the Hard Way
Where to find it

             https://learnpythonthehardway.org/

                                                  15
How it works

               “the hard way” = “by doing”

                                             16
How it works

                    “the hard way” = “by doing”

    1. Read the introduction.

                                                  16
How it works

                    “the hard way” = “by doing”

    1. Read the introduction.
    2. Copy the code shown, by typing (not through copy-paste).
       Pay attention to details.

                                                                  16
How it works

                    “the hard way” = “by doing”

    1. Read the introduction.
    2. Copy the code shown, by typing (not through copy-paste).
       Pay attention to details.
    3. Think about what the code will do.

                                                                  16
How it works

                    “the hard way” = “by doing”

    1. Read the introduction.
    2. Copy the code shown, by typing (not through copy-paste).
       Pay attention to details.
    3. Think about what the code will do.
    4. Check the actual output and the expected output.

                                                                  16
How it works

                    “the hard way” = “by doing”

    1. Read the introduction.
    2. Copy the code shown, by typing (not through copy-paste).
       Pay attention to details.
    3. Think about what the code will do.
    4. Check the actual output and the expected output.
    5. Explain any differences between what you thought the code
       would do, what it really does and what it should do according
       to the book.

                                                                       16
How it works

                     “the hard way” = “by doing”

    1. Read the introduction.
    2. Copy the code shown, by typing (not through copy-paste).
       Pay attention to details.
    3. Think about what the code will do.
    4. Check the actual output and the expected output.
    5. Explain any differences between what you thought the code
       would do, what it really does and what it should do according
       to the book.
    6. Do the extra study drills and read the frequently asked
       questions.

                                                                       16
Try it out!

   Do exercise 1 and 2 of LPtHW now. Notify the T.A. when you
   have reasoned about the code and run your program. Then work
   on the study drills until everyone has run their program.

                                                                  17
Fixing program errors
Syntax errors

   An example:

   while True print ’Hello world’
   File "", line 1, in ?
     while True print ’Hello world’
                    ^
   SyntaxError: invalid syntax

                                      18
The caret symbol ˆ

    • points to last character that adheres to Python’s grammar

                                                                  19
The caret symbol ˆ

    • points to last character that adheres to Python’s grammar
    • typically, the error is just after this character

                                                                  19
The caret symbol ˆ

    • points to last character that adheres to Python’s grammar
    • typically, the error is just after this character
    • while True:       print ’Hello world’ is correct

                                                                  19
The caret symbol ˆ

    • points to last character that adheres to Python’s grammar
    • typically, the error is just after this character
    • while True:       print ’Hello world’ is correct
    • error often directly follows the caret symbol, sometimes
      precedes it, is never any further down

                                                                  19
Exceptions

   Grammatically correct programs execute code which contains a
   semantic error. Put this in a file, “exception.py”, and run it:
   print 5 / 0

   Traceback (most recent call last):
     File "exception.py", line 1, in 
       print 5 / 0
   ZeroDivisionError: integer division or modulo by zero

                                                                     20
The traceback

    • shows which code was being executed when the exception
      occurred

                                                               21
The traceback

    • shows which code was being executed when the exception
      occurred
    • names the type of exception (e.g. ZeroDivisionError)

                                                               21
The traceback

    • shows which code was being executed when the exception
      occurred
    • names the type of exception (e.g. ZeroDivisionError)
    • code at the end of the traceback may contain a bug (as in
      this example)

                                                                  21
The traceback

    • shows which code was being executed when the exception
      occurred
    • names the type of exception (e.g. ZeroDivisionError)
    • code at the end of the traceback may contain a bug (as in
      this example)
    • code higher up in the traceback may contain a bug (e.g. other
      code supplies a denominator)

                                                                      21
The traceback

    • shows which code was being executed when the exception
      occurred
    • names the type of exception (e.g. ZeroDivisionError)
    • code at the end of the traceback may contain a bug (as in
      this example)
    • code higher up in the traceback may contain a bug (e.g. other
      code supplies a denominator)
    • worst case scenario: bug may not be in the traceback (e.g.
      invalid global settings)

                                                                      21
Simple and effective logging

   At the top of your file:

   import logging
   logging.basicConfig(level=logging.DEBUG)

   In your code:

   logging.debug("This is for very detailed info.")
   logging.info("Info which is likely to be relevant.")
   logging.warning("Signals potential problems.")
   logging.error("Something is definitely wrong.")
   logging.critical("Your power plant has a meltdown.")

   Change logging.DEBUG to logging.INFO,. . . to hide unnecessary
   details. Consult this slide when you are working on the projects!
                                                                       22
How to study for this course
Your mantra

      Knowing without doing is not knowing.

                                              23
Why should I attend the exercise sessions?

     • more emphasis on practice than in humanities programs

                                                               24
Why should I attend the exercise sessions?

     • more emphasis on practice than in humanities programs
     • online solutions provide answers; T.A. provides
       problem-solving tools

                                                               24
Why should I attend the exercise sessions?

     • more emphasis on practice than in humanities programs
     • online solutions provide answers; T.A. provides
       problem-solving tools
     • you can get tech support and ask questions about how to
       approach the projects (at the end of the session)

                                                                 24
Guidelines: how much should I study?

    • for a maximum grade (at the exam): until you can do all
      exercises

                                                                25
Guidelines: how much should I study?

    • for a maximum grade (at the exam): until you can do all
      exercises
    • for a solid grade: until you can do all the non-challenge
      exercises

                                                                  25
Guidelines: how much should I study?

    • for a maximum grade (at the exam): until you can do all
      exercises
    • for a solid grade: until you can do all the non-challenge
      exercises
    • to be a digital humanist: you do not stop

                                                                  25
Questions and feedback

  See https://dtai.cs.kuleuven.be/education/sl/ for
  contact info. If you are struggling with a certain concept, the T.A.
  can provide additional exercises. If some aspect of the course seems
  too difficult, pointless,. . . , let us know directly. We can take it.

                                                                           26
You can also read