Python in Business Applications
Python has become very popular recently in applications for data science and
machine learning, in fields where fast prototyping is a priority. Python is a
good candidate as it is easy to learn with its forgiving dynamic typing (I know
this can also be a disadvantage), a variety of libraries and possible direct execution at the command
prompt.
For its serverless Lambda service Amazon Web Services (AWS) offers Python as
programing language along with Java and Node.js. I have become curious about
Python. It's pretty easy to get started. The prerequisites are:
- Download and install a current Python version like Python 3.8 for your computer
- Install a package manager like PIP
- Have a suitable IDE ready like Visual Studio Code (or any other)
- For database driven applications you have a database ready like the PostgreSQL on the AWS free tier
Let's start with a few simple lines of code. Looking at the variable
assignments and the print command, old memories come up of my young IT years
with the BASIC programming language.
a = 2
b = 3
print("result", a+b)
a="hello "
print("result", a*b)
print("result", a+b)
When we run this, we get:
$ python3 types.py
result 5
result hello hello hello
Traceback (most recent call last):
File "types.py", line 6, in <module>
print("result", a+b)
TypeError: can only concatenate str (not "int") to str
This gives a first impression how Python handles types and how they are
assigned at runtime. Compared with Java, it looks pretty convenient, but bad
surprises at runtime with more complex code are not so unlikely.
For the next example that provides database access we need to install the
PostgreSQL database adapter
psycopg2.
Install it with your package manager, for example:
$ pip3 install psycopg2
Here's the database example program postgresql-connection.py that opens a
database connection, opens a cursor to a
table, fetches some records and performs some field operations. The connection is
closed safely at the end.
import psycopg2
import psycopg2.extras
import json
try:
# Credentials hardcoded in the source, this is really
# for demo purposes only
connection = psycopg2.connect(user = "postgres",
password = "yourpassword",
host = "postgresdemo.********.us-east-2.rds.amazonaws.com",
port = "5432",
database = "postgres")
# Define the cursor this way will provide named fields in the records
# This also works (a bit differently) with MySQL
cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
# Print PostgreSQL Connection properties
print ("Connection params - ", connection.get_dsn_parameters(),"\n")
# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record,"\n")
# Get resultset from db
cursor.execute("select * from awsdemo.newtable;")
# Fetch the first record
record = cursor.fetchone()
print("The first record: ", record)
# Access a field by index (like in Java start with 0)
print("Hello ", record[1])
# As we have a dictionary record we can access fields by name
print("Hello ", record['name'], "again")
# Dictionary records are mutable
record['name'] = "Kai-Uwe"
print (record)
# Fetch the remaining (!) records
results = cursor.fetchall()
for record in results:
print(record)
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
# Clean up and close the database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed")
Let's run this:
$ python3 postgresql-connection.py
Connection params - {'user': 'postgres', 'dbname': 'postgres', 'host': 'postgresdemo.********.us-east-2.rds.amazonaws
.com', 'port': '5432', 'tty': '', 'options': '', 'sslmode': 'prefer', 'sslcompression': '0', 'krbsrvname': 'postgres', 'target_session_attrs': 'any'}
You are connected to - ['PostgreSQL 11.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9),
64-bit']
The first record: [1, 'Kai']
Hello Kai
Hello Kai again
[1, 'Kai-Uwe']
[2, 'Mike']
[3, 'Andy']
[4, 'Olga']
PostgreSQL connection is closed
See how records can be retrieved from a table and getting manipulated (a
crucial task for server side code in any database driven application). The
learning curve from PL/SQL is not too steep I think.
In one of the next posts, we will write a AWS Lambda function that provisions
access to data stored in a PostgreSQL cloud instance.
Comments
Post a Comment