3/9/2018
Python for Oracle
Arup Nanda
Longtime Oracle Technologist
And
Python Explorer
Oracle Database Great for Data Store
Critical for Performance?
Business
Operations Run in laptop?
But how do you
share it?
Python for Oracle Professionals
2
1
3/9/2018
What you will learn?
• Why Python
• How can you learn Python pretty fast, for Oracle Developers
• Plotting Charts
• Interaction with Oracle
• Tons of code, a free tutorial series on OTN, videos
• Three real-world example applications
• What’s next
Python for Oracle Professionals
3
Why Python
• Used in data science, machine learning, AI
– Powerful and math plotting tools, free and paid
• Spark has a PySpark
• General purpose
– Not like R, which is very data oriented
• Convenience of Interpreted Language
– Rapid Application Development
• Default language of Raspberry Pi
• Has libraries for all types of access. Including Oracle DB.
Python for Oracle Professionals
4
2
3/9/2018
Installation
• Python is freely available for most OS’es
– Download from python.org
– For many platforms, even Rasperry Pi
• Three components:
– Command line version
– Command Line in a Window
– IDLE: Interactive DeveLopment Environment
Python for Oracle Professionals
5
Bring up command line
• From OS prompt:
C:\> python
(Same command executable on any of the OS’es)
Brings up the python command line prompt:
Python prompt
Python for Oracle Professionals
6
3
3/9/2018
How difficult is learning Python?
• It’s similar—not same—as PL/SQL
• Some language elements are even the same
• Approach: Jumpstarting the learning by using examples of PL/SQL
PL/SQL Python
if Condition then if Condition:
… statement … … statement …
end if;
Python for Oracle Professionals
7
Help Me!
• Command help brings up the help interface
>>> help()
help> Command_You_Need_Help_On
• Or
>>> help Command_You_Need_Help_On
Python for Oracle Professionals
8
4
3/9/2018
Basics
• Python is case sensitive
– So, v1 and V1 are different.
• Comments
– Starts with #
• How to quit:
– It’s a function. So, quit()
– Or Control-Z
Python for Oracle Professionals
9
Learning Tool
bit.ly/python4plsql
• 5 Part Article Series
• Complete tutorials
• Video
• Quizzes
• Free!
Python for Oracle Professionals
10
5
3/9/2018
Arrays
• Three types
– List
x1 = [1,2,3]
– Can be any mix of datatypes [1,”s”,1.5]
– Address elements by x1[position]
– Tuple—same as list but immutable
x1 = (1,2,3)
– Dictionary—key-value pairs
x1 = {'k1':'v1','k2':'v2'}
Python for Oracle Professionals
11
If
• Python is positional
if Condition:
Statement… This indentation is necessary
elif Condition:
Statement
else:
Statement
Python for Oracle Professionals
12
6
3/9/2018
Looping
• FOR Loop
for i in range(1,11):
print('i=',i)
• Looping through arrays
x1 = [‘a’,’e’,’i’,’o’,’u’]
for i in range(len(x1)):
print(x1[i])
• WHILE Loop
Python for Oracle Professionals
13
Adding Modules
• Python Package Index (PyPI)
• Install it my calling it as a module in python
C:\> python -m pip ModuleName
• Modules we will install
python -m pip pandas
python -m pip numpy
python -m pip matplotlib
python –m pip scipy
Python for Oracle Professionals
14
7
3/9/2018
Data Analysis
Python for Oracle Professionals 15
Multi-dimensional Array
• Sales Data
– ProductID
– Quarter
– Territory
– Amount
Python for Oracle Professionals
16
8
3/9/2018
2D Array
Quarter 1 Quarter 2 Quarter 3 Quarter 4
Product 0 200 300 275 225
Product 1 400 600 550 450
Product 2 600 900 1000 500
Product 3 800 1200 1100 900
Python for Oracle Professionals
17
3D Array
Territory 2 Quarter 1 Quarter 2 Quarter 3 Quarter 4
Product 0 200 300 275 225
Territory 1 Quarter 1 Quarter 2 Quarter 3 Quarter 4
Product 1 400 600 550 450
Product 0 2200600
Product 300900 2751000 225500
Product 1 3400800
Product 6001200 5501100 450900
Product 2 600 900 1000 500
Product 3 800 1200 1100 900
Python for Oracle Professionals
18
9
3/9/2018
Python for Oracle Professionals
19
Numpy Array
sales = np.array([
[ [
[50,50,50,50], [150,140,160,150],
[150,150,180,120], [225,220,230,225],
[75,70,60,70], [250,200,300,250],
[55,45,65,55] [125,130,120,125]
], ],
[ [
[100,90,110,100], [200,250,150,200],
[300,350,250,300],
[150,160,130,170],
[225,230,220,225],
[145,155,150,100],
[220,225,230,200]
[110,115,105,120] ]
], ])
Python for Oracle Professionals
20
10
3/9/2018
sales.sum(axis=0)
sales.min(axis=0)
numpy1.txt
Python for Oracle Professionals
21
Visualization
Python for Oracle Professionals 22
11
3/9/2018
Plotting
• Package matplotlib
Python for Oracle Professionals
23
Oracle DB Connection
Python for Oracle Professionals 24
12
3/9/2018
Connecting to Oracle DB
• A module called cx_Oracle
• Basic Operation
>>> import cx_Oracle as cxo
>>> conn = cxo.connect(‘hr’,’hr’,’server:1521/AL122')
>>> c1 = conn.cursor()
>>> c1.execute('select * from employees where rownum <11')
>>> for row in c1:
... print(row)
• Fetch one row alone
r1 = c1.fetchone()
• Or, many
r1 = c1.fetchmany(numRows=2)
Python for Oracle Professionals
25
More cx_Oracle operations
• Set the arraysize
>>> c1.arraysize = 10
• Describe the output
>>> c1.description
• One step fetch
>>> r1 = c1.execute('select * from sales where rownum < 11')
>>> for rec in r1:
... print (rec)
• Close the cursor
>>> c1.close()
Python for Oracle Professionals
26
13
3/9/2018
Bind Variables
>>> conn = cxo.connect('sh','sh','localhost:1521/AL122')
>>> c1 = conn.cursor()
>>> c1.prepare('select * from sales where rownum < :limiting_rows')
>>> c1.execute(None, {'limiting_rows':11})
>>> c1.fetchall()
Python for Oracle Professionals
27
Dynamically Constructed Queries
• Note the query
c1.prepare('select * from...')
• You can construct the query as a character array
>>> s1 = 'select '
>>> s1 += '*'
>>> s1 += ' from sales '
>>> s1 += ' where rownum < '
>>> s1 += '11'
>>> s1
'select * from sales where rownum < 11'
>>> r1 = c1.execute(s1)
Python for Oracle Professionals
28
14
3/9/2018
Example 1
Python for Oracle Professionals 29
Creating a DB Monitor
• Objective select sn.end_interval_time,
(after.total_waits-before.total_waits) "No. of Waits",
– To measure waits on named (after.time_waited_micro-before.time_waited_micro)/
events in database (after.total_waits-before.total_waits) "Avg Wait in
us"
– From AWR repository tables from
dba_hist_system_event before,
– And plot them dba_hist_system_event after,
dba_hist_snapshot sn
where
before.event_name=:event_name
and
after.event_name=before.event_name
and
after.snap_id=before.snap_id+1
and
after.snap_id=sn.snap_id
and
(after.total_waits-before.total_waits) > 0
order by after.snap_id
Python for Oracle Professionals
30
15
3/9/2018
Example 2
Python for Oracle Professionals 31
Finding Employee Retention
• Objective
– To model how employees stay in an organization
• Hypothesis
– The tenure of the employees depends on department, the specific job, the reporting
manager.
• Methodology
– We will find the correlation between tenure and multiple other factors
Python for Oracle Professionals
32
16
3/9/2018
Example 3
Python for Oracle Professionals 33
Twitter Analysis
• Objective
– To find out how many have been tweeting about Oracle Code (i.e. with #OracleCode), and
how many times each has been retweeted.
– Store the data in an Oracle database for further processing
• Methodolgy
– A package called tweepy allows twitter interaction
Python for Oracle Professionals
34
17
3/9/2018
Tweepy
• A package for accessing twitter accounts
• You need:
– Authentication details from twitter
• Consumer key
• Consumer secret
• Authentication token
• Authentication secret
– All can be gotten from https://dev.twitter.com/apps
• To Install tweepy
• Using command python –m pip install tweepy
Python for Oracle Professionals
35
Authentication in Tweepy
• Define the values
ckey = '...'
csecret = '...'
atoken = '...'
asecret = '...'
• Authenticate
auth = tweepy.OAuthHandler(ckey,csecret)
auth.set_access_token(atoken, asecret)
• Declare the API object
api = tweepy.API(auth)
Python for Oracle Professionals
36
18
3/9/2018
Accessing objects
• Details about the account
>>> iam = api.me()
• To find out all about the account:
for i in iam._json.keys():
print("%30s = %s" % (i, iam._json[i]))
Python for Oracle Professionals
37
Finding Tweets
• The search API is used
>>> t =
tweepy.Cursor(api.search,q="#OracleCode",lang="en",since
="2018-03-01").items()
• We used the “since” parameter to limit the number of items
• The object t now contains the data on the tweets. To get the handle and text of
the tweet:
>>> for text in t:
... print(text.author.screen_name, text.text)
Python for Oracle Professionals
38
19
3/9/2018
More Uses
• Connecting to a physical device using Raspberry Pi
• Using a Event Driven Architecture to use fraud detection in transactions
• Using Oracle AQ and cx_Oracle
• Using microservices where each service talks to another using messages
Python for Oracle Professionals
39
In Summary
• Python is a general purpose language; not just data
• Interpreted, but can also be cached
• Rich data manipulation capabilities exist natively as well as via packages
• Very easy to learn for Oracle PL/SQL developers
• Multiple use cases spanning a variety of applications
Python for Oracle Professionals
40
20
3/9/2018
Thank You!
Blog: arup.blogspot.com
Tweeter: @ArupNanda
Facebook.com/ArupKNanda
Google Plus: +ArupNanda
Python for Oracle Professionals 41
21