Python [Post 05] | Lists

A list in Python is an ordered group of items. Items in a list need not be of the same type. A list is created by placing all the items inside a square bracket, separated by commas.

# Lists Examples

my_empty_list = []
number_list = [1,2,3,4,5]
str_list = ['A','B','C','D','Hello Python']
mix_list = ['A',1,'B',2,'C',12.5,5+500j]
nested_list = ['A',1,'B',2,['X','Y','Z']]
print(number_list)
print(str_list)
print(mix_list)
print(nested_list)

[1, 2, 3, 4, 5]
[‘A’, ‘B’, ‘C’, ‘D’, ‘Hello Python’]
[‘A’, 1, ‘B’, 2, ‘C’, 12.5, (5+500j)]
[‘A’, 1, ‘B’, 2, [‘X’, ‘Y’, ‘Z’]]

Some Built-in Python Functions

my_list = [1,2.5,3,4.6,5,6]

print(len(my_list)) # len Function - Gives the total length of the list.
print(max(my_list)) # max Function - Returns element from the list with maximum value.
print(min(my_list)) # min Function - Returns element from the list with minimum value.

6
6
1

### List Indexing & Slicing

Use the index operator [] to access an item in a list. We can use a : to perform slicing which extracts chunk of more than one element as per input. In slicing last index is not included.

sides = ['East','West','North','South']
# Fetch the first element of list sides

sides[0]

‘East’

# Fetch the last element of list sides

sides[-1]

‘South’

# fetch elements start from index 1st till last

sides[1:]

[‘West’, ‘North’, ‘South’]

# fetch elements upto 2nd index, but 2nd is not included

sides [:2]

[‘East’, ‘West’]

# fetch element from 1st index to 3rd index

sides [1:3]

[‘West’, ‘North’]

Unlike Python Strings, Python Lists are mutable i.e. the elements inside a list can be changed!

sides = ['East','West','North','South']
print(sides)

# Let's try to change the elements of lists sides

sides[0]= 3.0
sides[1]='Hello World'
sides[2]='Python World'
sides[3]= 12345

print(sides)

[‘East’, ‘West’, ‘North’, ‘South’]
[3.0, ‘Hello World’, ‘Python World’, 12345]

Nested Lists

A list inside a list.

some_sides = ['East','West','North','South']
more_sides1 = ['North-East','North-West']
more_sides2 = ['South-East','Soth-West']

nested_sides = [some_sides,more_sides1,more_sides2]

print(nested_sides)

[[‘East’, ‘West’, ‘North’, ‘South’], [‘North-East’, ‘North-West’], [‘South-East’, ‘Soth-West’]]

# Fetch the first element of first list

print(nested_sides[0])
print(nested_sides[0][0])

[‘East’, ‘West’, ‘North’, ‘South’]
East

# Fetch the second element of third list

print(nested_sides[2])
print(nested_sides[2][1])

[‘South-East’, ‘Soth-West’]
Soth-West

### Lists Methods

Python includes following list methods

# Creating a new list

sides = ['East','West','North','South']

Append():

Adds single element to the end of a list. Length of the list increases by 1 index.

# Example 1: Append Element in list

sides.append('North-East')
print(sides)

[‘East’, ‘West’, ‘North’, ‘South’, ‘North-East’]

# Other way to append

sides = sides + ['South-East','South-West']
print(sides)

[‘East’, ‘West’, ‘North’, ‘South’, ‘North-East’, ‘South-East’, ‘South-West’]

# Example 2: Append List with List (This will create nested list)

sides = ['East','West']
more_sides = ['North','South','North-East']

sides.append(more_sides)
print(sides)

[‘East’, ‘West’, [‘North’, ‘South’, ‘North-East’]]

Extend():

Add all elements of a list to the another list. It will not create nested list.

# Example - Extending Lists

sides = ['East','West']
more_sides = ['North','South','North-East']

sides.extend(more_sides)
print(sides)

[‘East’, ‘West’, ‘North’, ‘South’, ‘North-East’]

Insert():

Inserts an element at the position mentioned in its arguments. It takes two arguments, position and element.

# Example Insert

sides = ['West','North']

print(sides)

sides.insert(2,'South')
sides.insert(3,'South-East')
sides.insert(0,'East')

print(sides)

[‘West’, ‘North’]
[‘East’, ‘West’, ‘North’, ‘South’, ‘South-East’]

Pop():

Removes and returns an element at the given index. By default pop takes off the last index, but you can also specify which index to pop off.

sides.pop() # Pop off the last indexed item, by default

‘South-East’

print(sides)

[‘East’, ‘West’, ‘North’, ‘South’]

sides.pop(3) # Pop off the 3rd indexed item

‘South’

print(sides)

[‘East’, ‘West’, ‘North’]

Remove():

It is used to delete the first occurrence of item mentioned in its arguments.

sides = ['East','West','North','East','South']

sides.remove('East')
print(sides)

[‘West’, ‘North’, ‘East’, ‘South’]

Clear():

Removes all elements from the list.

sides = ['East','West','North','South']
print(sides)

[‘East’, ‘West’, ‘North’, ‘South’]

sides.clear() # Clear the list

print(len(sides))
print(sides)

0
[]

Index() :

Returns the index of the first matched item.

sides = ['EAST','West','North','East','South','East']

sides.index('East')

3

Count():

Returns the count of number of items passed as an argument.

sides.count('East')

2

Sort():

Sort items in a list in ascending order.

sides = ['West','North','East','South']

print(sides)
sides.sort()
print(sides)

[‘West’, ‘North’, ‘East’, ‘South’]
[‘East’, ‘North’, ‘South’, ‘West’]

Reverse():

Reverse the order of items in the list.

print(sides)
sides.reverse()
print(sides)

[‘East’, ‘North’, ‘South’, ‘West’]
[‘West’, ‘South’, ‘North’, ‘East’]


Thanks!

Happy Learning! Your feedback would be appreciated!

shobhitsingh.in

Github Code

Advertisements

Python [Post 04] | Strings

Folks,

In this blog post we will explore Python strings & string methods. Python strings are sequences of characters. Here are some examples of strings.

# Printing String

print('This is a string!') # Enclosed using single quote

print("This is also a string!") # Enclosed using double quote

print("""This is also a string,
also having next line.""") # Enclosed using triple quote. Multiple lines are generally put in triple quotes.

print("It's ok!") # Use double quote to enclose the string, if string is having single quote
print('P') # Python treat this single character also as string

This is a string!
This is also a string!
This is also a string,
also having next line.
It’s ok!
P

### String Indexing

Python strings are sequences of individual characters. We can access characters in strings by index. In Python index starts from 0, also if you want to start from the end, instead of the beginning then use a negative index.

Indexing Example: String x = ‘Python’

+ve Index 0 1 2 3 4 5
+ve Index 0 -5 -4 -3 -2 -1

# String Variable
x = 'Python'
x[0] # Fetch the first character of string x

‘P’

x[1] # Fetch the second character of string x

‘y’

x[-1] # Fetch the last character of string x

‘n’

Python strings are immutable. i.e. Once string is created, the elements within it can not be changed or replaced.

# Let's try to change the first letter to 'J' of string x
x[0] = 'J'

TypeError Traceback (most recent call last)

<ipython-input-6-5b400f918714> in <module>()
1 # Let’s try to change the first letter to ‘J’ of string x
—-> 2 x[0] = ‘J’

TypeError: ‘str’ object does not support item assignment

 

You can “update” an existing string by (re)assigning a variable to another string.

x = 'J' + x[1:]
print(x)

Jython

### Slicing Python String

We can use a : to perform slicing which extracts chunk of more than one character as per input. In slicing last index is not included.

String x = ‘Python’

+ve Index 0 1 2 3 4 5

+ve Index 0 -5 -4 -3 -2 -1

For Example:

# String Variable
x = 'Python'
x[0:2] # fetch the first 2 character of string x. x[0:2] is same as x[:2]

‘Py’

x[:3] # fetch the first 3 character of string x

‘Pyt’

x[2:5] # fetch character from 2nd index to 3rd index

‘tho’

x[2:] # fetch charcters start from index 2nd till last

‘thon’

x[:3] # fetch charcters upto 3rd index, but 3rd is not included

‘Pyt’

x[-5:-1] # Negative Index also works in slicing

‘ytho’

x[::] # fetch complete string

‘Python’

Skipping Characters using third parameter

x = 'Python'
x[::1] # Default is 1

‘Python’

x[::2] # Skip every 2nd character

‘Pto’

x[0:5:2] # Skip every 2nd character

‘Pto’

x[::-1] # Reverse String

‘nohtyP’

### String Methods Examples

Python String provides so many methods to do the string manipulations. See below examples.

# Split - Splits string as per provided delimiter ( default space) and returns list of substrings;

x = 'Python is fully-functional programming language that can do almost anything'
print(x.split())

[‘Python’, ‘is’, ‘fully-functional’, ‘programming’, ‘language’, ‘that’, ‘can’, ‘do’, ‘almost’, ‘anything’]

x = 'A,B,C,D,E,F'
print(x.split(',')) # Comma as delimeter

[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’]

# Upper & Lower Case

x = 'Python'
print(x.upper())
print(x.lower())

PYTHON
python

# Checking String whether contains only alpha or numeric

x = 'Python3'
print(x.isalnum())
print(x.isalpha())
print(x.isnumeric())
len(x)

True
False
False
7

Lstrip, RStrip & Strip Method – For removing white spaces

# lstrip - Remove white space from left

x = ' Python3 '
x.lstrip()

‘Python3 ‘

# rstrip - Remove white space from left

x = ' Python3 '
x.rstrip()

‘ Python3’

# rstrip - Remove white space from left & right

x = ' Python3 '
x.strip()

‘Python3’

.format() String Method

Insert objects dynamically in the string using assigment keywords or index. It can also persorm alignment, padding & precision of floating point numbers.

import datetime
d = str(datetime.datetime.now())
# Example 1 -

x = "Today's date & time is {}"

x.format(d)

“Today’s date & time is 2018-07-08 02:04:47.893499”

# Example 2

x = "Today's date & time is %s" %datetime.datetime.now()

print(x)

Today’s date & time is 2018-07-08 02:04:49.885035

# Example 3

print('a {f} in need is a {f} indeed'.format(f='friend'))
print('a {0} in need is a {0} indeed'.format('friend'))

a friend in need is a friend indeed
a friend in need is a friend indeed

# String Formatting Operator - %
# Oldest method involves placeholders using the modulo % character. %s for string, %c is for character, %i integer, %f float etc

print('a %s in need is a %s indeed' %('friend','friend'))

x = "Today's date & time is %s" %datetime.datetime.now()
print(x)

x = "My name is %s, my age is %i & my height is %f" % ('John', 21, 150.56)
print(x)

a friend in need is a friend indeed
Today’s date & time is 2018-07-08 02:04:50.634701
My name is John, my age is 21 & my height is 150.560000

Precision of Float with .format() Method

You can add precision for float point numbers in string. See below example.

x = “My height is %5.1f” % (150.5678)

  • .1f stands for how many numbers to show past the decimal point.
  • 5 would be the minimum number of characters the string should contain. Padded with whitespace if the entire number does not have this many digits.
x = "My height is %5.1f" % (150.5678)
print(x)

My height is 150.6

x = "My height is %15.2f" % (150.5678)
print(x)

My height is 150.57

Padding & Alignment with .format() Method

# Padding

print('{0:5} | {1:3}'.format('FName', 'Age'))
print('{0:5} | {1:3}'.format('A', 14))
print('{0:5} | {1:3}'.format('B', 12))

FName | Age
A | 14
B | 12

By default, .format() aligns text to the left, numbers to the right.
Pass an optional <,^, or > to set a left, center or right alignment as per requirement.

# Alignment

print('{0:^5} | {1:^3}'.format('FName', 'Age'))
print('{0:^5} | {1:^3}'.format('A', 14))
print('{0:^5} | {1:^3}'.format('B', 12))

FName | Age
A | 14
B | 12

print('{0:-^10} | {1:-^10}'.format('FName', 'Age'))

–FName— | —Age—-

There are many more string methods eg. count, find, decode, encode, index etc.


Thanks!

Happy Learning! Your feedback would be appreciated!
shobhitsingh.in

[GitHub Code]

Python [Post 02] | Variables

In Python a variable is created when we assign value to it. It does not required any explicit declaration. Use the assignment operator = to assign the value to a variable.

Variable Names Rules

  • Must start with a letter or the underscore character. Cannot start with a number.
  • Can only contain alpha-numeric characters and underscores (A-z, 0-9, & _ ).
  • Variable names are case-sensitive.
  • Avoid using Python Built-keywords. Like str, type, list etc.
#Example:

name = "John" # String Variable Assignment
age = 50 # Integer Variable Assignment
height = 150.50 # Float Variable Assignment

Using Python’s built-in function type() you can check what type of object is assigned to a variable.

Here are the list of Python data type:

  • int
  • float
  • str
  • list
  • tuple
  • dict
  • set
  • bool
# Exapmple

type(name)

str

## Dynamic Typing

In Python you can reassign variables to different data types. Python is very flexible in assigning data types.

# Example, Here Integer Assignment

x=50
type(x)

int

x="Hello, World!" # Re-assigned X Now String Assignment
type(x)

str

x=12.20+4j # Re-assigned X Now String Assignment
type(x)

complex

x=(1,2,3,4,5) # Re-assigned X Now Tuple Assignment
type(x)

tuple

## Re-assigning Variables

Python allow you to reassign variables with a reference to the same object.

a=50
print(a)

50

a = a + a
print(a)

100

a += 50
print(a)

150

a -= 10
print(a)

140

a *= 2
print(a)

280

a /= 5
print(a)

56.0

# Multiple Assignment

Python allows you to assign a value to multiple variables simultaneously.

# Example 1

length = width = height = 50
print (length)
print (width)
print (height)

50
50
50

# Example 2

length, width, height = (10,50,60)
print (length)
print (width)
print (height)

10
50
60


Thanks!

Happy Learning! Your feedback would be appreciated!

shobhitsingh.in

GitHub Code

Python [Post 03] | Numbers

Folks,
In this blog we will explore different Python numeric types, arithmetic operations, mathematical fuctions, variable assignment & number conversion.

For Python Overview & Basic – Refer this Blog Post

Python has mainly three diffrent of numeric types.

  • int
  • float
  • complex

Integers (int)


Int/Integer, is basically a whole number (+ve or -ve), without decimals & of unlimited length.

Example of Integers: -5, 500, 20001543646546231654646465465454544545454554

 

# Integer Example 1
print(-5)
type(5)

-5
int

# Integer Example 2
print(500)
type(500)

500
int

# Integer Example 3
print(20001543646546231654646465465454544545454554)
type(20001543646546231654646465465454544545454554)

20001543646546231654646465465454544545454554
int

Float


Floating point numbers is a number, (+ve or -ve), represent real numbers and they have a decimal points in them, or use an exponential (e) to define the number.

Exampe of Floats: -5.5 , 500.5, 5E2

5E2 (5 times 10 to the power of 2) is also an example of a floating point number in Python.

# Float Example 1
print(-5.5)
type(-5.5)

-5.5
float

# Float Example 2
print(500.5)
type(500.5)

500.5
float

# Float Example 3
print(5E2)
type(5E2)

500.0
float

Complex Number


Numbers in the form of (a + bj), where a and b are floats and j represents the square root of -1. Real part of the number is a, and the imaginary part is b.

Exampe of Complex Number: 5+500j

# Complex Example 1
5+500j

(5+500j)

type(5+500j)

complex

Artithmetic Operations


# Additon of numbers
5+5

10

# Substraction of numbers
10-5

5

# Multiplication of numbers
10*5

50

# Division of nuumbers
10/5

2.0

# Floor Divison (// operator truncates the decimal without rounding)
10//4

2

# Exponent
5**2

25

# Modulus (% operator returns the remainder after division)
5%2

1

Mathematical Fuctions


Python also have some functions that perform mathematical calculations

# importing "math" for mathematical operations
import math
# Ceil Function
print (math.ceil(5.6))

6

# Floor Function
print (math.floor(5.6))

5

# Exponential Function
print (math.exp(5))

148.4131591025766

# Square Root Function
print (math.sqrt(16))

4.0

# Factorial Function
print (math.factorial(5))

120

# Absolute Function - Positive Number
print(abs(-5))

5

# Round Function - Number rounded to n digits from the decimal point
print(round(5.56))

# Round Function with 2nd argument for how many digits to round
print(round(5.56,1))

6
5.6

# Minimun Function - Finds smallest of its arguments
print (min(5,10,2,22))

2

# Mamimun Function - Finds largest of its arguments
print (min(5,10,2,22))

2

Variable Assignemnt


# Create an object/Variable called "length" and assign it the number 50
length = 50
print(length)

50

# Reassignment - Overrites value
length = 10
print(length)

10

length = 10
width = 5.6
height = 10.6

print(length * width * height)

593.6

Numerical Type Conversion


# Example 1:
height = 10.6
type(height)

float

# Let's convert this variable into int
height= int(height)
print(height)
type(height)

10
int

# Example 1:
length = 10
type(length)

int

# Let's convert this variable into foat
length= float(length)
print(length)
type(length)

10.0
float


Thanks!

Happy Learning! Your feedback would be appreciated!

shobhitsingh.in
Guthub Code

Model & Datastore| Creating and Reverse-Engineering| Oracle Data Integrator 11g

Model & Datastore| Creating and Reverse-Engineering| Oracle Data Integrator 11g

Folks,

In this blog we will explore:

  • What is Model & how to create a new Model using Oracle or file technology.
  • What is Datastore & how to reverse-engineer a model to populate it with all new & existing datastores.
  • How to create manually data-store of a model.

Model:

Model is a group of tabular data structures stored in a data server defined in the topology.

  • Model is based on a Logical Schema which is defined in the topology. In a given Context, Logical Schema is mapped to a Physical Schema which contains physical data structure: such as tables, files etc.
  • Models only contain Metadata Information, i.e. description of the data structures, they do not contain actual data.

Datastores:

A datastore represents a data structure. It describe data in tabular form i.e rows & columns.  It can be a table, a flat file, XML file or any other data structure which is supported by ODI as technology.

Reverse-engineering:

Reverse-engineering is the process that populates the Model by retrieving metadata from the data server containing the data structures.


Creating and Reverse-Engineering –  Model & Data-store 

Refer this video for Technology – File System:

Refer this video for Technology – Oracle:

In next blog we will explore how to create a ODI Interface to load data from file to Oracle.


Thanks!

Happy Learning! Your feedback would be appreciated!

How to send email using Python.

How to send email using Python.

Folks,

In this blog we will explore how send email to single/multiple email id using python Script.


Python Script: See detailed comments in the Python Script.


# import the smtplib module.
import smtplib

# from user details
from_user_email_id =  [provide_your_email_id_here]
from_user_email_pwd = [provide_your_email_id_password]

# Email Subject
email_subject = [provide_your_email_subject]

# To user email ids
to_user_email_id = [provide_list_of_to_email_ids_comma_seperated]

# set up the SMTP server
s = smtplib.SMTP(host=[provide_smtp_host_address_here], port=[provide_smtp_port_here]])
s.ehlo()
s.starttls()
s.ehlo

# login the SMTP server using from email id and password.
s.login(from_user_email_id, from_user_email_pwd)

# Preparing email message header and body.
email_header = 'To:' + ", ".join(to_user_email_id)  + '\n' + 'From: ' + from_user_email_id + '\n' + 'Subject: ' +  email_subject + '  \n'
email_body = email_header + [provide_here_email_text]

# sending email
s.sendmail(from_user_email_id, to_user_email_id, email_body)

s.close()

 

After providing all the required information & save your file  in .py extension. Refer below example.

email

Execute Script:

execute

Result:

email4


Thanks!

Happy Learning! Your feedback would be appreciated!

Dataserver, Physical Schema, Logical Schema & Context | Oracle Data Integrator 11g

Dataserver, Physical Schema, Logical Schema & Context | Oracle Data Integrator 11g

Folks,

In this blog we will explore some areas of ODI Topology Navigaor.

  • We will explore Dataserver, Physical Schema, Logical Schema & Context.
  • Relationship between Physical, Logical Schema & Context.

Regarding setup & configuration of Dataserver, Physical Schema, Logical Schema in ODI using Oracle Technology please refer below video.


Let’s explore Topology Navigator in detail.

topology section
Topology Navigator

In Topology Navigator we have ‘Physical Architecture‘ section where we define the physical connection details of any Instance ( either Oracle DB, SQL Server,  IBM DB2, PostgreSQL, MySQL etc.) or any file system.

What is Dataserver?

A ODI object that defines the physical connection to any database instance or any file system. It basically store the host, username & password related details of an instance.

To define any physical connection in ‘Physical Architecture‘, you have to create a Data Server. Just click on your desired technology in Physical Architecture & click on ‘ New Data Server’.

NEWDATASERVER
Physical Architecture

After that you have to provide instance details. In case of any Database instance provide host, user/password & JDBC details.

Database server new
Data Server: DS_ORACLE_DEV

JDBC Details in case of oracle database as instance:

JDBC details
JDBC Details Data Sever: Localhost

In case of any file. Just provide the folder path from where you pick raw files in interface.

file1
Data Server File Type

JDBC Details in case of file:

file2
JDBC Details File Type

Now after adding the Data-server, next step is to add the ‘Physical Schema‘ in Data-server.

What is Physical Schema?

Physical Schema represents the actual connection to the data source (any database instance or any file system).

ODI Physical Schema corresponds to a pair of schema:

  • Main Schema (Data) – In which ODI look for the source and target objects for the interfaces.
  • Work Schema – Generally used by ODI for staging purpose, here ODI create temporary data objects associated to the sources and targets. Example C$, I$, E$ tables. It is always preferred to create & use separate work schema.

To define any physical schema, go to Data Server you created and then just click on new ‘New Physical Schema’.

PS.png
New Physical Schema

After that you have to provide Schema Name & Work Schema name using drop down. It will show you all the schema present in selected data-server instance.

PS02
Physical Schema Configuration
  • RAW_SCHEMA – schema where my source/target tables lies.
  • ODI_WORK_SCHEMA – Created for ODI staging purpose.
ps03.PNG
Physical Schema – RAW_SCHEMA present in Dataserver DS_ORACLE_DEV

Now after adding the Physical Schema, next step is to create Logical Schema & Context.

What is Logical Schema & Context?

  • Logical schema represents the logical name associated to that source/target objext.
  • One logical schema can be associated with multiple physical schema along with context, i.e. one logical schema is associated with different physical schema using different context.

Create Context:

To add any new context, go to Context Section and then just click on new ‘New Context’.

contextContext 02

Create Logical Schema:

To define any logical schema in ‘Logical Architecture‘ . Just click on your desired technology & click on ‘New Logical Schema’.

LA

LS02
Add New Logical Schema

After that you have to provide the logical schema name & Physical Schema mapping with context.

LS03
Logical Schema

So in Dev Context, Logical Schema LS_RAW_SCHEMA is pointing to Physical Schema DS_ORACLE_DEV.RAW_SCHEMA.

It may be possible that in Test Context, Logical Schema LS_RAW_SCHEMA is pointing to different Physical Schema DS_ORACLE_TEST.RAW_SCHEMA.

lS_Context.PNG

Refer this video clip for better understanding of Logical Schema & Context relationship.


Thanks!

Happy Learning! Your feedback would be appreciated!