Scripting Languages Preparing for the exercises and exam Vincent - - PowerPoint PPT Presentation

scripting languages
SMART_READER_LITE
LIVE PREVIEW

Scripting Languages Preparing for the exercises and exam Vincent - - PowerPoint PPT Presentation

Scripting Languages Preparing for the exercises and exam Vincent Nys September 21, 2016 The command line Windows: PowerShell 1 OS X: Terminal 2 The command line vs. the REPL Installed programs and utilities can be run using the command line


slide-1
SLIDE 1

Scripting Languages

Preparing for the exercises and exam

Vincent Nys September 21, 2016

slide-2
SLIDE 2

The command line

slide-3
SLIDE 3

Windows: PowerShell

1

slide-4
SLIDE 4

OS X: Terminal

2

slide-5
SLIDE 5

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

slide-6
SLIDE 6

Installing Python

slide-7
SLIDE 7

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

slide-8
SLIDE 8

Get the installer

https://www.python.org

5

slide-9
SLIDE 9

Select these options

6

slide-10
SLIDE 10

Setting up a text editor

slide-11
SLIDE 11

Getting Atom

https://atom.io

7

slide-12
SLIDE 12

Use these settings

8

slide-13
SLIDE 13

Your first program

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

Your third UNIX command: ls

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

13

slide-23
SLIDE 23

Other useful tricks

  • Type in cd exe without hitting <ENTER>, then hit <TAB>.
  • Complete the command by adding a number and hitting

<ENTER>.

  • Go back to the parent directory with cd ...
  • With the command line still open, enter <UP-ARROW> twice.
  • Hit <ENTER> 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

slide-24
SLIDE 24

Learn Python the Hard Way

slide-25
SLIDE 25

Where to find it

https://learnpythonthehardway.org/

15

slide-26
SLIDE 26

How it works

“the hard way” = “by doing”

16

slide-27
SLIDE 27

How it works

“the hard way” = “by doing”

  • 1. Read the introduction.

16

slide-28
SLIDE 28

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

slide-29
SLIDE 29

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

slide-30
SLIDE 30

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

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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

slide-33
SLIDE 33

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

  • n the study drills until everyone has run their program.

17

slide-34
SLIDE 34

Fixing program errors

slide-35
SLIDE 35

Syntax errors

An example: while True print ’Hello world’ File "<stdin>", line 1, in ? while True print ’Hello world’ ^ SyntaxError: invalid syntax

18

slide-36
SLIDE 36

The caret symbol ˆ

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

19

slide-37
SLIDE 37

The caret symbol ˆ

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

19

slide-38
SLIDE 38

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

slide-39
SLIDE 39

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

slide-40
SLIDE 40

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 <module> print 5 / 0 ZeroDivisionError: integer division or modulo by zero

20

slide-41
SLIDE 41

The traceback

  • shows which code was being executed when the exception
  • ccurred

21

slide-42
SLIDE 42

The traceback

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

21

slide-43
SLIDE 43

The traceback

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

this example)

21

slide-44
SLIDE 44

The traceback

  • shows which code was being executed when the exception
  • ccurred
  • 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

slide-45
SLIDE 45

The traceback

  • shows which code was being executed when the exception
  • ccurred
  • 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

slide-46
SLIDE 46

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

slide-47
SLIDE 47

How to study for this course

slide-48
SLIDE 48

Your mantra

Knowing without doing is not knowing.

23

slide-49
SLIDE 49

Why should I attend the exercise sessions?

  • more emphasis on practice than in humanities programs

24

slide-50
SLIDE 50

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

slide-51
SLIDE 51

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

slide-52
SLIDE 52

Guidelines: how much should I study?

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

exercises

25

slide-53
SLIDE 53

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

slide-54
SLIDE 54

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

slide-55
SLIDE 55

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