introduction to python on ibm i - omni...
Post on 13-Feb-2019
225 Views
Preview:
TRANSCRIPT
1© 2017 Rogue Wave Software, Inc. All Rights Reserved. 1
Introduction to Python on IBM i
2© 2017 Rogue Wave Software, Inc. All Rights Reserved. 2
Agenda
• A little about Python
• Why use Python?
• How to install/determine if installed
• Syntax101
– Variables
– Strings
– Functions
– Command Line
• DB2 Data Access
3© 2017 Rogue Wave Software, Inc. All Rights Reserved. 3
Acknowledgements
• Kevin Adler
• Tony Cairns
• Jesse Gorzinski
• Memegenerator
• Corn chips & salsa
• Clean socks
• and, of course,
– spam
4© 2017 Rogue Wave Software, Inc. All Rights Reserved. 4
Before you freak out
• Why isn’t Mike talking about PHP?
– Zend WAS the PHP company
– Rogue Wave IS the Open Source company
• Support for 300+ Open Source projects
• And, of course, PHP
5© 2017 Rogue Wave Software, Inc. All Rights Reserved. 5
A little about Python
6© 2017 Rogue Wave Software, Inc. All Rights Reserved. 6
What is it really?
General purpose programming language
Easy to get started
Simple syntax
Great for integrations
Access to C and other API
Infrastructure first, but applications too.
Thanks: Tahani Alamanie
7© 2017 Rogue Wave Software, Inc. All Rights Reserved. 7
Historically…
Python was conceptualized by Guido Van Rossum in the late 1980s.
Rossum published the first version of Python code (0.9.0) in February 1991 at the
CWI (Centrum Wiskunde & Informatica) in the Netherlands , Amsterdam.
Python is derived from ABC programming language, which is a general-purpose
programming language that had been developed at the CWI.
Rossum chose the name "Python", since he was a big fan of Monty Python's
Flying Circus.
Python is now maintained by a core development team at the institute, although
Rossum still holds a vital role in directing its progress.
Thanks: Tahani Alamanie
8© 2017 Rogue Wave Software, Inc. All Rights Reserved. 8
Python lineage
• Python 1 – 1994
• Python 2 – 2000 (Not dead yet…)
– 2.7 - 2010
• Python 3 – 2008
– 3.5 – 2015
– 3.6.1 – March 2017
9© 2017 Rogue Wave Software, Inc. All Rights Reserved. 9
Python 2 or 3?
10© 2017 Rogue Wave Software, Inc. All Rights Reserved. 10
What’s the diff?
• Example:
• Python 2 print statement replaced by function:
– Python2 – print "Hello World!"
– Python3 - print("Hello World!")
• Many more differences, tho…
11© 2017 Rogue Wave Software, Inc. All Rights Reserved. 11
Why use it?
12© 2017 Rogue Wave Software, Inc. All Rights Reserved. 12
Python==hot
• RPG is 45, COBOL is 25, ugh…
13© 2017 Rogue Wave Software, Inc. All Rights Reserved. 13
Some reasons folks use Python
• Open source is free, right?
– Yes, no…well – sort of…
– No charge LP on IBM i – 5733OPS
• Only scripting language to support multiple inheritance
• Multi-threading
– Stateful, not Async like Node
– But same motivation as Node, non-blocking
• Applications develop faster
– Extensive standard library that is constantly evolving
– Frameworks such as Bottle
14© 2017 Rogue Wave Software, Inc. All Rights Reserved. 14
Industries using Python
• Technology
– Geospatial, Machine Learning
– Honeywell - Build automation
• Manufacturing
– D-Link updates – Multithreaded
– Phillips – Robot programming and sequencing
• Entertainment
– ILM – Scripting CGI Intensive films
– Video gaming
• Transportation
– Airports – Frequentis for weather info
• US, Denmark, Iceland, Hong Kong, etc.
15© 2017 Rogue Wave Software, Inc. All Rights Reserved. 15
Companies using Python
• YouTube - Backend
• Facebook – (see next slide)
• Dropbox
• NASA – Weather and more
• IBM
• Mozilla
• Red
• Quora
– Why python?
16© 2017 Rogue Wave Software, Inc. All Rights Reserved. 16
Facebook in detail
• Top three languages
– Hack (fork of PHP)
– C++
– Python
• Infrastructure management
– Network switch setup
– Core services (DNS, etc,)
– Server imaging, burnin-in.
• Platform services: Job Engine, team workflow management
https://code.facebook.com/posts/1040181199381023/python-in-production-engineering/
17© 2017 Rogue Wave Software, Inc. All Rights Reserved. 17
Got Python?
18© 2017 Rogue Wave Software, Inc. All Rights Reserved. 18
Details at Developerworks…
• https://www.ibm.com/developerworks/community/wikis/home?lang=e
n#!/wiki/IBM%20i%20Technology%20Updates/page/Open%20Source%
20Technologies
19© 2017 Rogue Wave Software, Inc. All Rights Reserved. 19
Which one?
• Python 3 is LPP option 2
• Python 2 is LPP option 4
• Correct answer: It depends…
– Many existing libraries are Python 2
– But 90%+ are also Python 3 compliant, or on their way
• Python 2.7 will retire in less than 3 years (EOL==2020)
20© 2017 Rogue Wave Software, Inc. All Rights Reserved. 20
Need Licensed Programs
• 5733OPS Base and option 2 or 4
21© 2017 Rogue Wave Software, Inc. All Rights Reserved. 21
Python in action
• Command line via green screen (CALL QP2TERM)
22© 2017 Rogue Wave Software, Inc. All Rights Reserved. 22
Hello world?
23© 2017 Rogue Wave Software, Inc. All Rights Reserved. 23
Most prefer SSH
• Command line via SSH terminal
24© 2017 Rogue Wave Software, Inc. All Rights Reserved. 24
Hello World, again?
25© 2017 Rogue Wave Software, Inc. All Rights Reserved. 25
IDE?
26© 2017 Rogue Wave Software, Inc. All Rights Reserved. 26
Zend Studio
• No, you don’t need to buy Zend Studio
• Use Orion, etc.
• But if you have Studio or RDi…
– Consider something from Eclipse.org
– I grabbed PyDev
27© 2017 Rogue Wave Software, Inc. All Rights Reserved. 27
Eclipse
28© 2017 Rogue Wave Software, Inc. All Rights Reserved. 28
Alternatives to IBM i when learning
• What’s that? The boss won’t let you install Python?
– Consider repl.it
29© 2017 Rogue Wave Software, Inc. All Rights Reserved. 29
Alternatives to IBM i when learning
• How about your PC?
• Goto the Python site:
– Download
– Install
– Viola!
30© 2017 Rogue Wave Software, Inc. All Rights Reserved. 30
Download PyDev from Eclipse
31© 2017 Rogue Wave Software, Inc. All Rights Reserved. 31
Capture URL
• Follow prompts
32© 2017 Rogue Wave Software, Inc. All Rights Reserved. 32
Editor for Eclipse
• Select what you like
• Next
33© 2017 Rogue Wave Software, Inc. All Rights Reserved. 33
Confirm versions
• Next
34© 2017 Rogue Wave Software, Inc. All Rights Reserved. 34
Accept terms and EULA
• Finish
35© 2017 Rogue Wave Software, Inc. All Rights Reserved. 35
Watch the pretty status bar
36© 2017 Rogue Wave Software, Inc. All Rights Reserved. 36
Python in Eclipse (i.e. Zend Studio)
• I bet RDi works, too!
37© 2017 Rogue Wave Software, Inc. All Rights Reserved. 37
Hello world….again…
• Create a file like Ex01hello.py
• Open the file
• Key up some code and click save
38© 2017 Rogue Wave Software, Inc. All Rights Reserved. 38
Hello world….again…
• Change the file
• Click save
• Back to qp2term & F9
39© 2017 Rogue Wave Software, Inc. All Rights Reserved. 39
Syntax
40© 2017 Rogue Wave Software, Inc. All Rights Reserved. 40
How is it written
• Indentation means EVERYTHING
– Don’t use tab
– 4 spaces is the best practice
– Mismatched indents can cause failures. Good luck finding…
– Mismatched spaces and tabs will cause failures
• No need for scope terminators like other languages
• Colon introduces start block, then indent
• Much more readable than other languages
• Get a good editor!!!
41© 2017 Rogue Wave Software, Inc. All Rights Reserved. 41
Indentation
42© 2017 Rogue Wave Software, Inc. All Rights Reserved. 42
Operators – Similar to Java, PHP, C, etc.
• Comparison
– Assignment =
– Comparison ==
– Inequality !=
– Less than <
– Greater than >
– Less than or equal to <=
– Greater than or equal to >=
• Mathematical
– Addition +
– Multiplication *
– Division /
– Floor division //
– Modulus %
– Exponentiation **
• Booleans
– And
– Or
– Not
43© 2017 Rogue Wave Software, Inc. All Rights Reserved. 43
Variables
44© 2017 Rogue Wave Software, Inc. All Rights Reserved. 44
Data Types – yeah…about that
• Int
– Integer of unlimited size
• Float
– System defined precision
• Complex
– Complex with real and imaginary parts
• Bool
– TRUE & FALSE
45© 2017 Rogue Wave Software, Inc. All Rights Reserved. 45
Built in types
• Str
– Character string composed of Unicode
• Bytes and bytearray
– Sequences of bytes
• List and tuple (list/array/data structure)
• Range
– Start, end, step
• Set & frozenset
– Unordered set of terms
• Dict
– Associative array (dictionary, hash map)
46© 2017 Rogue Wave Software, Inc. All Rights Reserved. 46
Variables on the fly
• Case sensitive
• camelCase
• Who are you? type()
47© 2017 Rogue Wave Software, Inc. All Rights Reserved. 47
Variables in a file
48© 2017 Rogue Wave Software, Inc. All Rights Reserved. 48
Data Type?
49© 2017 Rogue Wave Software, Inc. All Rights Reserved. 49
Every variable is implemented as a class!
50© 2017 Rogue Wave Software, Inc. All Rights Reserved. 50
And now for something completely different
51© 2017 Rogue Wave Software, Inc. All Rights Reserved. 51
It’s OK….
• Monty Python references are not only acceptable…
– They are encouraged!
• Documentation is littered with references
• Examples are well covered
52© 2017 Rogue Wave Software, Inc. All Rights Reserved. 52
Back to Variables
• Numbrs - 3 Data Types
– Int 1,2,42
– Float 3.14159
– Complex: <real> + <imaginary> (not used much…)
53© 2017 Rogue Wave Software, Inc. All Rights Reserved. 53
Strings
• Immutable objects, cannot change value
• Can reassign. (dynamic typing)
• Single or Double quotes, OK (even triple…)
• Index starts at 0
54© 2017 Rogue Wave Software, Inc. All Rights Reserved. 54
String formatting
• Interpolation, of sorts
55© 2017 Rogue Wave Software, Inc. All Rights Reserved. 55
Lists
• Ordered group, similar to array
• Different data types, ok
• Multi-dimensional (sub lists)
• Mutable (changeable)
56© 2017 Rogue Wave Software, Inc. All Rights Reserved. 56
Tuples
• Similar to lists
• Immutable (don’t change once created)
• Use parenthesis instead of brackets
57© 2017 Rogue Wave Software, Inc. All Rights Reserved. 57
Dictionary
• Again, like lists but more like hash table
• Mutable
• Key value pairs
58© 2017 Rogue Wave Software, Inc. All Rights Reserved. 58
Control Structures
59© 2017 Rogue Wave Software, Inc. All Rights Reserved. 59
Ifs
60© 2017 Rogue Wave Software, Inc. All Rights Reserved. 60
For loop
61© 2017 Rogue Wave Software, Inc. All Rights Reserved. 61
While loop
62© 2017 Rogue Wave Software, Inc. All Rights Reserved. 62
Functions
63© 2017 Rogue Wave Software, Inc. All Rights Reserved. 63
Basic functions
64© 2017 Rogue Wave Software, Inc. All Rights Reserved. 64
Functions with defaults
65© 2017 Rogue Wave Software, Inc. All Rights Reserved. 65
Functions with keyword arguments
66© 2017 Rogue Wave Software, Inc. All Rights Reserved. 66
Command Line
67© 2017 Rogue Wave Software, Inc. All Rights Reserved. 67
Input from command line• Talk with the script
68© 2017 Rogue Wave Software, Inc. All Rights Reserved. 68
Database
70© 2017 Rogue Wave Software, Inc. All Rights Reserved. 70
Locate the package or “wheel”
71© 2017 Rogue Wave Software, Inc. All Rights Reserved. 71
Install commands
72© 2017 Rogue Wave Software, Inc. All Rights Reserved. 72
Find the connector
• YMMV
• With wheels
73© 2017 Rogue Wave Software, Inc. All Rights Reserved. 73
Run the pip install…
• From the command line
74© 2017 Rogue Wave Software, Inc. All Rights Reserved. 74
What version of the DB2 Extension?
75© 2017 Rogue Wave Software, Inc. All Rights Reserved. 75
Simple database access
• Import the class
• Connect (with or without options
• Open the cursor
• Set the SQL
• Read
76© 2017 Rogue Wave Software, Inc. All Rights Reserved. 76
Simple database access
77© 2017 Rogue Wave Software, Inc. All Rights Reserved. 77
Table information
78© 2017 Rogue Wave Software, Inc. All Rights Reserved. 78
Summary – Why Python?
• Lot’s of libraries
• Make it easy to do stuff
• OPC / OPO
• Education
79© 2017 Rogue Wave Software, Inc. All Rights Reserved. 79
End the session
80© 2017 Rogue Wave Software, Inc. All Rights Reserved. 80
ATTENDConnect with experts.Become an authority.Register now:zendcon.com/register-now
EXPLOREShare, inspire, educate.Network
SPONSORSpotlight your best in enterprise PHP, opensource & frameworks:sponsors@zendcon.com
Visit zendcon.com
81© 2017 Rogue Wave Software, Inc. All Rights Reserved. 81
Who was that masked man?
Mike Pavlak – Solution Consultant, Rogue Wave Software
Mike.Pavlak@roguewave.com
82© 2017 Rogue Wave Software, Inc. All Rights Reserved. 82
top related