Scripting Languages

Lecturer / Supervisor – dr Witold Marańda, DMCS (K-25), building B18, room.50 – Tuesdays & Fridays 10-11 Course Organization – 15x 2h, lab Course Materials / Literature – on-line tutorials ● https://docs.python.org/3/tutorial/ ● https://realpython.com/ ● https://www.tutorialspoint.com/python3/ – books ● Learning Python, by Mark Lutz ● Invent Your Own Computer Games with Python, by Al Sweigart ● ...

Form of Assessment

Final grade range: 2 (fail) and 3 … 5 (pass) ● 4 x home assignments → 10% + 20% + 20% + 50% – good practices of programming (50%) – correctness and compliance with requirements (50%) Assignment delays: ● each week lowers the assignment max. grade by 1.0 Absences: ● sick leaves + one unjustified absence are OK ● any other unjustified absence lowers final grade by 0.25

Course Content

Practical programming in modern language: – language syntax – data structures – flow control – code design – algorithms – documentation – problems & solutions ● Course Description at http://programy.p.lodz.pl

Computer Engineering

● Computer hardware ● ● Development tools – IDE's, , interpreters, linkers, assemblers, debuggers, profiles, code checkers, ...

Computer Hardware

Programmable machine – transformation of (binary) patterns/numbers according to concept of Turing and von Neumann

Memory Peripherals CPU(s) Program/Data Input/Output

Operating System

Goal: – offer hardware services to user programs Key features: – single, multi-tasking, multi-user, ... – distributed, templated, embedded, real-time, ...

Most common: ● and derivatives https://www.levenez.com/unix/

● MS Windows family https://www.levenez.com/windows/

Programming Language

Machine language → binary numbers only Assembler → human readable machine commands (one-to-one) High Level Languages – abstraction, data types, flow control – paradigms: imperative, object-oriented, ... – mode of execution: compiled or interpreted Dynamic/Scripting Languages – source-only (interpreted / compiled just-in-time) – powerful syntax, advanced data structures, rich libraries – platform-independent – focus on task and rapid prototyping - relaxed approach to paradigms

history - https://www.levenez.com/lang/ rankings - http://www.tiobe.com/tiobe-index/ - https://pypl.github.io/PYPL.html Classic vs Scripting

● Java

public class Hello { public static void main(String [] args) { System.out.println("Hello World"); } }

● Python print("Hello World")

Compilation vs Interpretation

machine code

run

Input data program Output data

run source code bytecode

Interpreter bytecode compiler + Input data virtual machine Output data

Compilation vs Interpretation

● Compilation: Write→Compile→Run (, C++, ...) – programs are fast – source and executable are independent files →long development process – source may be platform independent, executable is not ● Interpretation: Write→Run (Python, Perl, Ruby, Tcl, JavaScript, ...) – programs may be slower – only source file exists (no executable) → rapid development process – source is mostly platform independent ● Mixed approaches: – compilation into intermediate formats platform independent (Java) – precompilation of ready-made parts of code (Python)

Exercise

1. Create source file: hello.c 1. Create source file: hello.py

------#include print("Hello") int main() ------{ printf("Hello\n"); 2. Execute: return(0); $ python3 hello.py } ------

2. Compile: $ gcc hello.c

3. Execute: $ ./a.out a. Show machine code $ hexdump a.out b. Make and show assembler: $ gcc -S hello.c $ cat hello.s

Interaction with Software

User interface ● Command Line Interface – CLI – textual, information-oriented, for competent users – task automation, background jobs, computations, ... – platform independent (standard input/output → keyb./term.) ● Graphical User Interface – GUI – look-and-fill, aesthetics and ergonomics considerations – user action-oriented programming – mostly platform-dependent (hardware requirements)

Software Licenses

Propriety software – restricted in any way, according to the owner wishes “Free” software – “free” may refer to various aspects of freedom: try-before-buy, limited: time, resources, applications, right to run, display, copy, modify, embed, distribute, sell, sublicense, ... – Open-source software: source code is available to the user, but may not be "free"

● Free licenses: Public domain, GPL-style, BSD-style, … ● Foundation (www.fsf.org)

Python Software License

● Python Software Foundation License (PSFL) ● BSD-style: permissive free software license – free to use, even for commercial products – free to embed Python interpreter in products – free to sell a product written in Python and/or embedding Python interpreter

Python Language

● Full-featured, easy to learn, matured ● Efficient high-level data structures ● Simple and effective approach to OOP ● Short and elegant syntax ● Big library of standard objects and modules ● Official documentation: – Python Web site: https://www.python.org/ – Language Reference: https://docs.python.org/3/reference/ – Library Reference: https://docs.python.org/3/library/

Version 2 vs 3

● Should I use Python 2 or Python 3? – 3.x is definitely the present and the future ● Python 2 will be “End Of Life” in January 2020 – syntax is very similar - the differences mostly affect performance and improvements – 2.7 is the still intensively used in many Linux desktop and servers machines – all important Python packages are already compatible with version 3.x

Python IDE

Integrated Development Environment (IDE) ● syntax highlighting, bracket matching, ... ● automatic save/execution/compilation/check ● source structure and elements ● multi-file projects

Geany (http://www.geany.org/) - a small and lightweight IDE for C, Java, PHP, HTML, Python, Perl, Pascal, and others

Style Guide and Code Checker

PEP 8: Style Guide for Python Code pycodestyle: Python simple code style checker – IDE-geany can call pycodestyle to help you maintain your code (menu Build→Lint) Pylint: python code static analyzer – programming errors, – coding standard – etc.

Running Python scripts

● by explicitly calling the interpreter: $ python script.py ● by executing the script: $ ./script.py

System Access Control in Linux/Unix ● users and groups (/etc/passwd, /etc/groups) ● administrator/superuser: root ● permissions: rwx (read/write/execute) ● 3-level access control: rwx rwx rwx (user/group/ others) ● examples: rwx rwx rwx (777), rw- r-- r-- (644), rwx r-x r-- (751), rwx --- --x (701)

Running Python scripts

● create source (text) file with one line inside: print("Hello") $ pico script.py ● show the file permissions $ ls -l script.py ● set the execution right for the owner/user $ chmod u+x script.py $ ls -l script.py ● locate your interpreter $ which python ● edit file and make 1st line of your script as follows: #!/usr/bin/python ● run your script: $ ./script.py

Recommendation

● Use Linux Operating System – Choose from http://distrowatch.com/ ● Linux Mint is a very good choice for novices – Install the system - preferably on physical machine: ● as the only operating system (recommended) or sharing the hard-drive with other systems or in a virtual machine or as bootable live-distribution (poor performance) – Learn the basics of shell commands and shell scripting ● basic commands ● shell scripting ● and many, many other sites ...