Język PYTHON ‑ narzędzie dla każdego...

Post on 18-Sep-2018

229 views 0 download

Transcript of Język PYTHON ‑ narzędzie dla każdego...

JĘZYK PYTHON - NARZĘDZIE DLA KAŻDEGO NAUKOWCA

Marcin Lewandowski

[ mlew@ippt.gov.pl ]

SŁOWO WSTĘPNE

2

O mnie…

• Z wykształcenia Fizyk, na co dzieo inżynier

• Zainteresowania i Praca: – Elektronika

– Software

– Ultradźwięki

– Aparatura medyczna

– …

• Doświadczenia naukowe, wdrożeniowe i komercyjne

• 20+ lat doświadczenia w Software

• Fan Pythona od ok. 10 lat

3

FORMUŁA

• Wykład z elementami dwiczeo (stąd prośba o notebooki)

• Zorganizowany dla:

– Doktorantów IPPT/Biocentrum Ochota

– Pracowników naukowych IPPT

• Nominalnie 2x 45 min

4

Dla kogo ten wykład?

• Dla każdego:

– kto chce się nauczyd Pythona i … ułatwid sobie życie wykorzystując go w codziennej pracy

– poznad konkretne narzędzia/biblioteki/metody przydatne w pracy naukowo-badawczej

• Nie jest wymagane żadne doświadczenie programistyczne!

• A jakie jest doświadczenie/potrzeby Słuchaczy???

5

Co to jest Python?

• WIKIPEDIA http://en.wikipedia.org/wiki/Python_(programming_language) – Python is a general-purpose high-level programming language.[2] Its

design philosophy emphasizes code readability.[3] Python claims to "[combine] remarkable power with very clear syntax",[4] and its standard library is large and comprehensive. Its use of indentation for block delimiters is unusual among popular programming languages.

– Python supports multiple programming paradigms (primarily object oriented, imperative, and functional) and features a fully dynamic type system and automatic memory management, similar to that of Perl, Ruby, Scheme, and Tcl. Like other dynamic languages, Python is often used as a scripting language.

– The language has an open, community-based development model managed by the non-profit Python Software Foundation, which maintains the de facto definition of the language in CPython, the reference implementation.

6

Historia Pythona

• WIKIPEDIA http://en.wikipedia.org/wiki/Python_(programming_language) – Python was conceived in the late 1980s[5] and its implementation was started

in December 1989[6] by Guido van Rossum at CWI in the Netherlands as a successor to the ABC programming language (itself inspired by SETL)[7] capable of exception handling and interfacing with the Amoeba operating system.[8] Van Rossum is Python's principal author, and his continuing central role in deciding the direction of Python.

– Python 2.0 was released on 16 October 2000, with many major new features including a full garbage collector and support for Unicode. However, the most important change was to the development process itself, with a shift to a more transparent and community-backed process.[9] Python 3.0, a major, backwards-incompatible release, was released on 3 December 2008[10] after a long period of testing. Many of its major features have been backported to the backwards-compatible Python 2.6.[11]

7

Zastosowanie Pythona

• WIKIPEDIA http://en.wikipedia.org/wiki/Python_(programming_language) – Python is often used as a scripting language for web applications, e.g. via

mod_python for the Apache web server. With Web Server Gateway Interface a standard API has been developed to facilitate these applications. Web application frameworks or application servers like Django, Pylons, TurboGears, web2py and Zope support developers in the design and maintenance of complex applications. Libraries like NumPy, Scipy and Matplotlib allow Python to be used effectively in scientific computing.

– Python has been successfully embedded in a number of software products as a scripting language, including in finite element method software such as Abaqus, 3D animation packages such as Maya, MotionBuilder, Softimage, Cinema 4D, BodyPaint 3D, modo, and Blender, and 2D imaging programs like GIMP, Inkscape, Scribus, and Paint Shop Pro.[24] ESRI is now promoting Python as the best choice for writing scripts in ArcGIS.[25] It has even been used in several videogames.[26]

8

Dlaczego PYTHON?

• Interpretowany język wysokiego poziomu, łatwy do nauczenia, pisania programów, uruchamiania i debuggowania

• Interaktywny, przenośny i nowoczesny (obiektowy, funkcjonalny) oraz czytelny

• Rozszerzalny i ”wbudowywalny” - pozwala na integrację z innym oprogramowaniem i bibliotekami

• Zawiera BARDZO bogate biblioteki („batteries included”)

• Za darmo i Open Source

9

Dlaczego PYTHON? c.d.

• Jakość software – dzięki czytelności i cechom języka (OOP, modułowośd, etc.)

• Produktywność – znacznie większa niż w przypadku klasycznych języków kompilowanych np. Java/C++ (mniej kodu-mniej błędów)

• Przenośność – skryptów, ale także wieloplatformowe biblioteki systemowe, GUI, WEB, dostępu do baz danych

• Integracja – dzięki możliwościom wbudowania/rozszerzania Pythona; integracja z C/C++, .NET, Java, interfejsy sieciowe SOAP, XML-RPC, CORBA, etc. 10

WIKIPEDIA http://en.wikipedia.org/wiki/Comparison_of_programming_languages

• 30 - Data from Code Complete. The Statements ratio column "shows typical ratios of source statements in several high-level languages to the equivalent code in C. A higher ratio means that each line of code in the language listed accomplishes more than does each line of code in C.

• 31 - The ratio of line count tests won by each language to the number won by C when using the Compare to feature at http://shootout.alioth.debian.org/debian/c.php. Last updated May, 2006. C gcc was used for C, C++ g++ was used for C++, FORTRAN G95 was used for FORTRAN, Java JDK Server was used for Java, and Smalltalk GST was used for Smalltalk.

11

Do czego można użyd Pythona?

• Programowania systemowe (tzw. shell tools) – Python posiada biblioteki/interfejs do funkcji systemowych

• GUI – przenośne/wieloplatformowe graficzne interfejsy użytkownika (Tk, wxPython, Qt, GTK)

• WEB – skrypty po stronie klienta i serwera. Pełne wsparcie protokołów sieciowych TCP/IP oraz XML. Frameworks do budowy aplikacji WEBowych (Django, Zope, Pylons, …)

• Integracja komponentów – Pyhon jako „klej” do komponentów w C/C++/Fortran/Java/.NET…

• Obsługa baz danych – interfejsy do Oracle, Informix, ODBC, MySQL, PostgreSQL, SQLite, …

• Szybkie prototypowanie • Numeryka i zastosowania naukowe – obliczenia, symulacja,

wizualizacja, …

12

Przykłady z życia (1)

• Proste skrypty do konwersji danych: – img2mat – plik binarny (obraz 2D) na plik tekstowy do Matlaba

(25 linii) – Wczytanie/konwersja/zapis pliku CSV (~20-50 linii) – Obsługa plików HDF5 – kompatybilnych z MATLAB (~20-50 linii)

• Harmonics – obliczanie harmonicznych (FFT) dla dużych

zbiorów danych (~300 linii): – Wczytywanie danych z plików pomiarów (kostka danych, dla

każdego punktu wektor 1024 próbek) – Obliczanie FFT – biblioteka Numeric – Zapis wyników w formacie CSV

13

Przykłady z życia (2)

• SG3 – system akwizycji danych ech ultradźwiękowych: – Sterowanie beamformera przez RS-232 (~ 150 linii)

– Sterowanie modułu akwizycji przez USB (~ 150 linii)

– Moduł wrappera do systemu akwizycji (~ 500 linii kodu C)

• Sterowanie i automatyzacja pomiarów przez HPIB/GPIB/IEEE-488 – Sterowanie oscyloskopów, generatorów, mierników (~ 50-

100 linii)

– Moduł wrappera do VISA (~ 600 linii kodu C)

14

Przykłady z życia (3)

Aplikacja do wprowadzania danych i uruchamiania solvera pola ultradźwiękowego (~500 linii): • GUI – biblioteka wxPython • Obsługa plików CSV • pliki dokumentacji parametrów w HTML • Zapis plików danych FORTRAN

15

Przykłady z życia (4)

• GUI – biblioteka wxPython (~350 linii) • Obsługa dataloggera, modemu i komunikacji (~600 linii)

16

Przykłady z życia (5) matplotlib

17

Dla wciąż wątpiących

• Poparcie! http://docs.python.org/howto/advocacy.html

• Kto używa? http://wiki.python.org/moin/OrganizationsUsingPython

• Gdzie jest używany? http://en.wikipedia.org/wiki/List_of_Python_software

18

Ogólny plan wykładu

• Wprowadzenie do języka Python: – cechy i możliwości, – struktura języka, – konstrukcje programowe, – struktury danych, – operacje wejścia/wyjścia.

• Narzędzia i biblioteki: – narzędzia deweloperskie, – przegląd dostępnych bibliotek, – budowa własnych rozszerzeo.

• Zastosowania: – zastosowania obliczeniowe i naukowe, – wizualizacja i tworzenie wykresów, – zbieranie i przetwarzanie danych, – obróbka i konwersja plików danych, – integracja z oprogramowaniem naukowym.

19

Książki o Pythonie (1)

• M. Lutz, Learning Python: Powerful Object-Oriented Programming, 4. wydanie, O'Reilly Media, 2009 (1216 str.)

20

Książki o Pythonie (2)

• M. Lutz, Programming Python, O'Reilly Media, 3. wydanie, 2006 (1600 str.)

21

Książki o Pythonie (3)

• P. Barry, D. Griffiths, Head First Programming, O'Reilly Media, 2009.

22

Książki – Python po polsku

• M. Lutz, Python. Wprowadzenie, 3. wydanie, Helion 2009 (736 str.)

• Python. Od podstaw, Helion 2006 (704 str.)

• G. Wilson, Przetwarzanie danych dla programistów, Helion, 2006 (264 str.)

• J. E. F. Friedl, Wyrażenia regularne, Helion, 2001

23

Książki – Python dla nauki

• H. P. Langtangen, A Primer on Scientific Programming with Python, Springer, 2009.

• H. P. Langtangen, Python Scripting for Computational Science, Springer, 2009.

• S. Vaingast, Beginning Python Visualization. Crafting Visual Transformation Scripts, Apress, 2009.

• S. Tosi, Matplotlib for Python Developers, PACKT, 2009

24

Darmowe książki o Pythonie

• A. Downey, Think Python. How to Think Like a Computer Scientist, Green Tea Press, 2008 (http://www.thinkpython.com)

• Dive Into Python (http://diveintopython.org/)

• Dive Into Python 3 (http://diveintopython3.org/)

• Learning with Python 2nd Edition (http://openbookproject.net/thinkCSpy/)

• A Byte of Python v1.92 (for Python 3.0) (http://www.swaroopch.com/notes/Python)

• Advanced Scientific Programming in Python (http://portal.g-node.org/python-summerschool-2009/)

25

Python w sieci

• Python.org: – http://python.org/ – http://docs.python.org/ – http://www.python.org.pl/

• KSIĄŻKI/TUTORIALE: – http://www.rmi.net/~lutz/ – Mark Lutz home page – http://www.mindview.net/Books/TIPython – Thinking

in Python – http://gnosis.cx/TPiP/ – Text Processing in Python

• RECEPTY: – http://code.activestate.com/recipes/langs/python/

26

PYTHON ŚRODOWISKO

27

Może już mam Pythona?

> python –V

Python 2.6.4 BINGO!

28

http://www.python.org/download/

Download Python

• The current production versions are Python 2.6.4 and Python 3.1.1.

• Start with one of these versions for learning Python or if you want the most stability; they're both considered stable production releases.

• If you don't know which version to use, start with Python 2.6.4; more existing third party software is compatible with Python 2 than Python 3 right now.

29

http://www.activestate.com/activepython/

• ActivePython 2.6.4.10 – Windows (x86) – Windows (64-bit, x64) – Mac OS X (Universal) – Linux (x86) – Linux (x86_64) – Solaris 8+ (SPARC) – Solaris 8+ (SPARC 64-bit) – Solaris 10+ (x86) – HP-UX 11.00 (PA-RISC) – AIX (PowerPC 64) – AIX (PowerPC)

Dystrybucja komercyjna!

30

Portable Python http://portablepython.com/

• Portable Python is a Python® programming language preconfigured to run directly from any USB storage device, enabling you to have, at any time, a portable programming environment. Just download it, extract to your portable storage device or hard drive and in 10 minutes you are ready to create your next Python® application.

31

Enthought Python Distribution http://enthought.com/

• Easy to use. – Python facilitates simple yet sophisticated data manipulation. – Included modules support the quick integration of GUI front-ends. – EPD's 75+ libraries (including SciPy, NumPy, matplotlib) have been

meticulously configured for maximum compatibility. – Unified and searchable documentation is available for every package. – Platform-specific one-click installers ensure immediate access to the powerful

EPD toolset.

• Easy to integrate and extend. – Read data from many formats — HDF, CDF, CSV, Matlab™, and Excel™. – Integrate your own extensions easily with EPD. – Add a custom local repository to house your additions. – Platform-agnostic scripts can be run on any OS.

• Multi-platform 32-bit & 64-bit, with updates, virtual training, and support - 199$/user

• Single-platform 32-bit Trial and Academic – free of cost

32

PyIMSL™ Studio http://www.vni.com/

• PyIMSL Studio is a collection of development tools and numerical algorithms for modelers and implementation teams creating analytic applications. The key component of PyIMSL Studio is PyIMSL, a set of Python wrappers to the algorithms in the Visual Numerics, Inc. IMSL C Library: – Linear Systems – Eigensystem Analysis – Interpolation and Approximation – Quadrature – Differential Equations – Transforms – Nonlinear Equations – Optimization – Special Functions – Statistics and Random Number Generation

33

Dystrybucje egzotyczne

• http://www.python.org/download/other/ – MS DOS

PythonD is a 32-bit, multi-threaded, networking- and OpenGL-enabled Python interpreter for DOS and Windows.

– Python for AIX, Python for AROS, Python for AS/400 (OS/400), Python for BeOS, Python for iPod, Python for OS/2, Python for OS/390 and z/OS, Python for Palm OS, Python for PlayStation and PSP, Python for Psion, Python for QNX, Python for (what used to be Acorn) RISC OS, Python for Series 60, Python for Sparc Solaris, Python for VMS, Python for VxWorks, Python for Windows CE or Pocket PC, Python for Sharp Zaurus, Python for MorphOS

34

Narzędzia deweloperskie

• Środowiska IDE – IDLE – razem z Pythonem – PythonWin (http://sourceforge.net/projects/pywin32) –

element PyWin32 – tylko dla systemów Windows! – ECLISPE + PyDev (http://pydev.org) – KOMODO IDE (http://www.activestate.com/komodo) –

komercyjny!

• Edytory – SciTE (http://www.scintilla.org/SciTE) – KOMODO Edit (http://www.activestate.com/komodo_edit) – VIM – EMACS

35

IDLE

• IDLE is the Python IDE built with the tkinter GUI toolkit.

• IDLE has the following features: – coded in 100% pure Python, using

the tkinter GUI toolkit – cross-platform: works on Windows

and Unix – multi-window text editor with

multiple undo, Python colorizing and many other features, e.g. smart indent and call tips

– Python shell window (a.k.a. interactive interpreter)

– debugger (not complete, but you can set breakpoints, view and step)

36

KOMODO IDE http://www.activestate.com/komodo/ • Multi-language Support

– Browser-side technologies: CSS, HTML, JavaScript, and XML

– Server-side languages: Perl, PHP, Python, Ruby, and Tcl

– Web template languages: RHTML, Template-Toolkit, HTML-Smarty, and Django

• Multi-language Graphical Debugger

• 295$/user

37