Dec 8, 2018

Python Sparkpost sample

How to send emails from Python using Sparkpost module


In this tutorial, we will see how to send emails using Sparkpost.

First you need to register with Sparkpost and get your API key, so that you can use this key in sending e-mails.

The advantage/beauty of using sparkpost module is

  • You can send thousands of emails even in the free-tier.
  • You can send attachments & you can use html.
  • You can schedule the emails for future date & time.
  • You can also delete the future emails if not required.
  • It has lot more flexible features than inbuilt smtplib.
  • You can check the delivery status of your emails in Sparkpost dashboard (once you login, you can able to see this)
  • Python API is very simple to use, they also support their APIs in multiple languages.


from sparkpost import SparkPost

emails_to_send = ['test@gmail.com']
sp = SparkPost('XXXXXXXXXXXXXXXXXXXXXXXX') #Key

response = sp.transmissions.send(
          recipients=emails_to_send,
          html='',
          from_email='noreply@test.com',
          subject='test'
)
print(response)


Sep 5, 2018

Boto - Uploading file to a specific location on Amazon S3

When I upload a file from my local system to S3
  • Say media/downloads/logo.png it writes to <bucket>/media/downloads/logo.png
  • Suppose if I want to write to <bucket>/logo.png instead of media/downloads, please find the below script

import subprocess
import mimetypes
from boto.s3.connection import S3Connection, Location
from boto.s3.key import Key
import boto
import os

S3_BUCKET = ''

S3_KEY = ''
S3_SECRET = ''
conn = S3Connection(S3_KEY, S3_SECRET, calling_format=boto.s3.connection.OrdinaryCallingFormat())
bucket = conn.get_bucket(S3_BUCKET)
print bucket

key_name = 'logo.png'
path = 'media/img/'
full_key_name = os.path.join(path, key_name)
new_key = Key(bucket)
new_key.key = 'logo.png'
ctype = mimetypes.guess_type(full_key_name)[0] or "application/x-octet-stream"
new_key.set_metadata('Content-Type', ctype)
new_key.set_contents_from_filename(full_key_name)
if new_key.exists() == True:
   bucket.set_acl("public-read",new_key.key)
   url = new_key.generate_url(0, 'GET', None, False)
   print url



Thanks for reading.


Sep 2, 2018

How can you terminate custom/external HTTPS SSL certificate in AWS ELB and EC2

How can you terminate custom/external HTTPS SSL certificate in AWS ELB & EC2?
    1) at ELB level
        use AWS certificate manager, create a certificate & upload your existing certificate
        In ELB Listener rules, configure HTTPS(443 port) & attach the above certificate
        Limitation: You can add only one certificate per an ELB
    2) at EC2 level
        Suppose if you have multiple sites under EC2 (multi-tenant) & want to terminate HTTPS certificates for all the sites
        Having an ELB for each site will be costly solution, then you need to use TCP pass through solution
            https://test1.com
            https://test2.com
            https://test2.com
        In ELB Listener rules, configure TCP (443 port) pass through
        You could not obtain the clients IP address if the ELB was configured for TCP load balancing, so enable proxy protocol
        Enable proxy protocol in ELB through CLI (not available in AWS console), which allows X-Forwarded-For headers  
        Then the termination happens at you EC2 server level (Nginx/Apache)
       
        Nginx:
            server {
              listen *:443 ssl proxy_protocol;
              server_name *.site.com;
              set_real_ip_from 0.0.0.0/0;
              real_ip_header proxy_protocol;

              ssl on;
              ssl_certificate /opt/site/conf/ssl_keys/nginx_site.crt;
              ssl_certificate_key /opt/site/conf/ssl_keys/site.pem;

              location / {
                proxy_pass            http://127.0.0.1:80;
                proxy_read_timeout    90;
                proxy_connect_timeout 90;
                proxy_redirect        off;

                proxy_set_header      X-Real-IP $proxy_protocol_addr;
                proxy_set_header      X-Forwarded-For $proxy_protocol_addr;
                proxy_set_header      X-Forwarded-Proto https;
                proxy_set_header      X-Forwarded-Port 443;
                proxy_set_header      Host $host;
                proxy_set_header      X-Custom-Header nginx;
              }
            }

Sep 1, 2018

S3 Direct Upload Python Django

Using Pyhton Boto API, we can interact with Amazon S3 servers (for GET, PUT, POST etc)
We can also directly upload files to Amazon S3 from the client browser using Browser Uploads to S3 using HTML POST Forms.

Ref:
https://aws.amazon.com/articles/Java/1434

Prerequisites:
S3_BUCKET
S3_KEY
S3_SECRET
S3_URL

Note:
As per Amazon API, we need to encode policy format to base64 and further generate signature with SHA1
Both Policy and Signature need to be posted
Expiration : You can define the expiration time
acl: public-read/private
$key: upload path startes with
Check the POST S3 Url in HTML
Once the document is successfully uploaded to S3, S3 URL is shown on the page

Django Pyhton View:

import base64
import hmac, hashlib
import re

def direct_s3_upload(request):
  my_bucket = None
  policy_document = None
  policy_base_64 = signature = cors_xml = ""
  try:
    policy_document = '{"expiration": "2020-01-01T00:00:00Z", \
                       "conditions": [ \
                         {"bucket": "%s"}, \
                         {"acl": "public-read"}, \
                         ["starts-with", "$key", "uploads/"], \
                         ["content-length-range", 0, 524288000] \
                       ] \
                    }' % (<S3_BUCKET>)

    whitespace = re.compile(r'\s+')
    policy_document = whitespace.sub('', policy_document)
    policy_base_64 = base64.b64encode(whitespace.sub('', policy_document))
    signature = base64.b64encode(hmac.new(S3_SECRET, policy_base_64, hashlib.sha1).digest()) 
    dic1 = { 
           'MY_BUCKET_NAME': <S3_BUCKET>,
           'MY_AWS_KEY_ID': <S3_KEY>, 
           'MY_POLICY' : policy_base_64,
           'MY_SIGNATURE' : signature,
        }
  except:
    write_exception("direct_s3_upload")
  return render_to_response('test_s3_upload.html', context_instance=RequestContext(request, dic1))


HTML:  (test_s3_upload.html)
<html> 
  <head>
    <title>S3 POST Form</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script>
      var bucketName = '{{MY_BUCKET_NAME}}';
      var AWSKeyId   = '{{MY_AWS_KEY_ID}}';
      var policy     = '{{MY_POLICY}}';
      var signature  = '{{MY_SIGNATURE}}';

      function S3ToolsClass() {
        var _handle_progress = null;
        var _handle_success  = null;
        var _handle_error    = null;
        var _file_name       = null;

        this.uploadFile = function(file, progress, success, error) {
          _handle_progress = progress;
          _handle_success  = success;
          _handle_error    = error;
          _file_name       = file.name;

          console.log(file.name)
          var fd = new FormData();
          fd.append('key', "uploads/" + file.name);
          fd.append('AWSAccessKeyId', AWSKeyId);
          fd.append('acl', 'public-read');
          fd.append('policy', policy);
          fd.append('signature', signature);
          fd.append("file",file);

          var xhr = new XMLHttpRequest({mozSystem: true});
          xhr.upload.addEventListener("progress", uploadProgress, false);
          xhr.addEventListener("load", uploadComplete, false);
          xhr.addEventListener("error", uploadFailed, false);
          xhr.addEventListener("abort", uploadCanceled, false);
          xhr.open('POST', 'https://s3.amazonaws.com/' + bucketName + '/');

          xhr.send(fd);
        }

        function uploadProgress(evt) {
          if (evt.lengthComputable) {
            var percentComplete = Math.round(evt.loaded * 100 / evt.total);
            _handle_progress(percentComplete);
          }
        }

        function uploadComplete(evt) {
          if (evt.target.responseText == "") {
            console.log("Upload complete - success") 
            _handle_success(_file_name);
          } else {
            console.log("Upload complete - not success") 
            _handle_error(evt.target.responseText);
          }
        }

        function uploadFailed(evt) {
          console.log("upload Failed")
          _handle_error("There was an error attempting to upload the file." + evt);
        }

        function uploadCanceled(evt) {
          console.log("upload cancelled")
          _handle_error("The upload has been canceled by the user or the browser dropped the connection.");
        }
      }
      var S3Tools = new S3ToolsClass();
      
      function uploadFile() {
        var file = document.getElementById('file').files[0];
        S3Tools.uploadFile(file, handleProgress, handleSuccess, handleError);
      }

      function handleProgress(percentComplete) {
        document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
      }

      function handleSuccess(fileName) {
        document.getElementById('progressNumber').innerHTML = 'Done!';
        document.getElementById('resultant_s3_url').innerHTML = 'https://s3.amazonaws.com/' + bucketName + '/uploads/' + fileName;
      }

      function handleError(message) {
        document.getElementById('progressNumber').innerHTML = 'Error: ' + message;
      }
    </script>
  </head>
  <body>
    <form id="form" enctype="multipart/form-data" method="post">
      <div class="row">
        1. Select a File<br>
        <input type="file" name="file" id="file"/>
      </div>
      <br>
      <div class="row">
        2. Upload File<br>
        <input type="button" onclick="uploadFile()" value="Upload" />
        <br/>
        <span id="progressNumber"></span>
      </div>
    </div>
    <br>
    <div class="row">
      3. Result S3 Url
      <br>
      <div id="resultant_s3_url"> </div>

    </div>
  </body>
</html>
Thank you for reading this article.

Aug 31, 2018

AWS S3 Bucket action doesn't apply to any resources

I am trying to add a bucket policy to avoid bucket deletion & avoid deletion of objects in the bucket as well.

So I need to add a bucket policy to achieve my requirement mentioned above.

The following bucket policy giving error like
'Action does not apply to any resource(s) in statement'
{
  "Id": "Policy1527043264306",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1527043262106",
      "Action": [
        "s3:DeleteBucket",
        "s3:DeleteBucketPolicy",
        "s3:DeleteObject"
      ],
      "Effect": "Deny",
      "Resource": "arn:aws:s3:::prabhath-delete1",
      "Principal": {
        "AWS": [
          "XXXXXXXX"
        ]
      }
    }
  ]
}

We will discuss what caused the issue & how to resolve this.

Reason:

The following will apply on the bucket level only, so you need to define Resource as arn:aws:s3:::prabhath-delete-test
s3:DeleteBucket
s3:DeleteBucketPolicy

The following will apply on the bucket object level, so you need to define Resource as arn:aws:s3:::prabhath-delete-test/*
s3:DeleteObject

Solution:
You need to create two statements to cater the different types of actions as mentioned below:
One statement defines the following

  • s3:DeleteBucket
  • s3:DeleteBucketPolicy

Another statement defines the following
  • s3:DeleteBucketPolicy
 Correct version of bucket policy looks like below:

{
    "Version": "2012-10-17",
    "Id": "Policy1526996283460",
    "Statement": [
        {
            "Sid": "Stmt1526996142070",
            "Effect": "Deny",
            "Principal": {
                "AWS": "arn:aws:iam::XXXXXXX:root"
            },
            "Action": [
                "s3:DeleteBucket",
                "s3:DeleteBucketPolicy"
            ],
            "Resource": "arn:aws:s3:::prabhath-delete-test"
        },
        {
            "Sid": "Stmt1526996279916",
            "Effect": "Deny",
            "Principal": {
                "AWS": "arn:aws:iam::XXXXXXXXX:root"
            },
            "Action": "s3:DeleteObject",
            "Resource": "arn:aws:s3:::prabhath-delete-test/*"
        }
    ]
}

Jul 2, 2018

Python Operators

#!/usr/local/bin/python2.7

'''
    #Learning variables in Python
    File name: python_operators.py
    Author: Prabhath Kota
    Date: June 29, 2018
    Python Version: 2.7
'''

'''
Python Arithmetic Operators: +, -, *, /, % (modulus), ** (exponent), // (Floor Division)
Python Comparison Operators: ==, !=, <> (not equals), >, <, >=, <=
Python Assignment Operators: =, +=, -=, *=, /=, %=, //=
Logical: and, or, not
Python Identity Operators: Identity operators compare the memory locations of two objects  
Python Operators Precedence: 
Exponentiation (raise to the power)
Multiply, divide, modulo and floor division
Addition and subtraction
Comparison operators
Equality operators
'''

a = 100
b = 200
c = 10
print '-----------------Python Arithmetic Operators-----------------'
print a+b #300
print a-b #-100
print a*b #20000
print b/a #2
print b%a #0
print b**a #....
print '-----------------Python Comparision Operators-----------------'
print a == b #False
print a>b #False
print a != b #True
print a<>b #True #Not equals to
print a>b #False
print a < b #True
print '-----------------Python Assignment Operators-----------------'
b += 100
print b
b *= 10
print b
b  = 200
b %= 10
print b
print '-----------------Python Membership Operators-----------------'
print 10 in [100, 200, 10, 300]
print 111 not in [100, 200, 10, 300]

print '-----------------Python Operators Precedence-----------------'
a = 20
b = 10
c = 15
d = 5
e = 0

e = (a + b) * c / d       #( 30 * 15 ) / 5
print "Value of (a + b) * c / d is ",  e

e = ((a + b) * c) / d     # (30 * 15 ) / 5
print "Value of ((a + b) * c) / d is ",  e

e = (a + b) * (c / d);    # (30) * (15/5)
print "Value of (a + b) * (c / d) is ",  e

e = a + (b * c) / d;      #  20 + (150/5)
print "Value of a + (b * c) / d is ",  e


Learning control flow statements in Python

#!/usr/local/bin/python2.7

'''
    #Learning control flow statements in Python
    File name: python_control_flow_statements.py
    Author: Prabhath Kota
    Date: June 29, 2018
    Python Version: 2.7
'''

'''
Sequential
Selection (Python Conditional Statements) 
if
if...else
if..elif..else statements
nested if statements
not operator in if statement
and operator in if statement
in operator in if statement
Repetetion
for loop
while loop
'''

print('-------------------Python if statements----------------')
x=20
y=10
if x > y :
  print("X is bigger")
#X is bigger

print('-------------------Python if...else statements----------------')
x=10
y=20
if x > y :
  print("X is bigger")
else :
  print("Y is bigger")
#Y is bigger

print('-------------------Python if...else...else statements----------------') 
x=500
if x > 500 :
  print("X is greater than 500")
elif x < 500 :
  print("X is less than 500")
elif x == 500 :
  print("X is 500")
else :
  print("X is not a number")
#X is 500

print('-------------------Python Nested if statements----------------') 
mark = 72
if mark > 50:
  if mark >= 80:
    print ("You got A Grade !!")
  elif mark >= 60 and mark < 80 :
    print ("You got B Grade !!")
  else:
    print ("You got C Grade !!")
else:
  print("You failed!!")
#You got B Grade !!

print('-------------------not operator in if statement----------------') 
mark = 100
if mark != 100: #mark != 100
  print("mark is not 100")
else:
  print("mark is 100")
#mark is 100

print('-------------------and operator in if statement----------------') 
mark = 72
if mark > 80:
  print ("You got A Grade !!")
elif mark >= 60 and mark < 80 :
  print ("You got B Grade !!")
elif mark >= 50 and mark < 60 :
  print ("You got C Grade !!")
else:
  print("You failed!!")
#You got B Grade !!

print('-------------------in operator in if statement----------------') 
color = ['Red','Blue','Green']
selColor = "Red"
if selColor in color:
  print("Red is in the list")
else:
  print("Not in the list")
#Red is in the list4

print '----------------For loop---------------------'
freedom_fighters = ["Lala Lajipati Rai", "Dadabhai Naoroji", "Rajendra Prasad", "Sarojini Naidu", "Dadabhai Naoroji", "Lal Bahadur Shastri"]
print freedom_fighters
for each in freedom_fighters:
print each

#['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
#Lala Lajipati Rai
#Dadabhai Naoroji
#Rajendra Prasad
#Sarojini Naidu
#Dadabhai Naoroji
#Lal Bahadur Shastri

print '----------------While loop---------------'
#Try to avoild While loops, use only unless otherwise reequired
#If you sometimes don't handle it, it may end up in infinite loop
i = 0
squares = []
while(i < 10):
squares.append(i**2)
i += 1
print squares
#[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Jul 1, 2018

Learning variables in Python

#!/usr/local/bin/python2.7

'''
    #Learning variables in Python
    File name: python_variables.py
    Author: Prabhath Kota
    Date: June 29, 2018
    Python Version: 2.7
'''

'''
Unlike other programming languages, Python there is no need to declare a variable.
A variable is created when you first assign a value to it.
A variable can have a short or uder understandbale name

Rules for Python variables:
A variable name cannot start with a number
Variable names are case-sensitive (height, Height and HEIGHT are three different)
A variable name must start with a letter or the underscore character
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
'''

var_a = 100
print var_a
#100

var_a = 'Hi'
print var_a
#Hi

#1_var = 'Hi'
#wrong declaration

#Case-Sensitive
height = 5.0
print height

Height = 5.2
print Height

HEIGHT = 5.6
print HEIGHT

Jun 24, 2018

Python Commad line arguments

#!/usr/local/bin/python2.7

'''
    File name: python_command_line.py
    Author: Prabhath Kota
    Date: June 22, 2018
    Python Version: 2.7
'''

import sys

def multiply_function(val1, val2):
    return_val = 0
    if val1 and val2:
        return_val = val1 * val2
    return return_val


if __name__ == '__main__':
    program_name = sys.argv[0]
    arguments = sys.argv[1:]
    count = len(arguments)
    if count == 2:
        print multiply_function(sys.argv[1], sys.argv[1])
    else:
        print 'Please pass two arguments like "python python_command_line.py 10 20"'

Python Variables Scope

#!/usr/local/bin/python2.7

'''
    File name: python_scope.py
    Author: Prabhath Kota
    Date: June 22, 2018
    Python Version: 2.7
'''

'''
The scope of a variable refers to the places that you can see or access a variable.
If you define a variable at the top level of your script or module, this is a global variable
Module/global scope is a read-only shadow within the local function/method scope:
You can access it, providing there's nothing declared with the same name in local scope, but you can't change it.
'''

variable = 100
variable_new = 1111

def func_with_global_access():
    global variable
    print('variable inside function is : ', variable) #100
    variable = 200
    variable_new = 2222 #Local
    print('variable inside function - changed to : ', variable) #200
    print('variable_new inside function - changed to : ', variable_new) #2222

def func_without_global_access():
    try:
        print('variable_new inside fucntion - changed to : ', variable_new) #1111
        variable_new += 100
        """
        #Local variable 'variable_new' referenced before assignment
        #This is because module/global scope is a read-only shadow within the local function/method scope:
        #You can access it, providing there's nothing declared with the same name in local scope,
        #but you can't change it.
        """
        print('variable_new inside fucntion - changed to : ', variable_new) #2222
    except Exception, e:
        print e #local variable 'variable_new' referenced before assignment

print '----------------Global variable----------------------'
print ('variable outside is : ', variable) #100
func_with_global_access()
print ('variable outside is : ', variable) #200
print ('variable_new outside is : : ', variable_new) #1111

print '----------------Local variable----------------------'
print ('variable_new outside is : ', variable_new) #1111
func_without_global_access()
print ('variable_new outside is : ', variable_new) #1111


Python Functions

#!/usr/local/bin/python2.7

'''
    File name: functions_python.py
    Author: Prabhath Kota
    Date: June 22, 2018
    Python Version: 2.7
'''

'''
Functions are meant to re-use & reduce repeated code
Python functions are passed by reference by default
Non-default argument should never follows default argument
ie.,
    def getEvenOddNumbers(type="even", list): #Wrong
    def getEvenOddNumbers(list, type="even", list): Right
Python Fucntons support Default arguments
Anonymous functions
Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.
Return statements
'''

def getEvenOddNumbers(list=[], type="even"):
    result_list = []
    try:
        if type == 'odd':
            result_list = [i for i in list if i%2 != 0]
        else:
            result_list = [i for i in list if i%2 == 0]
    except Exception, e:
        print '**** Error: ' + str(e)   
    return result_list

def sumUpAll(*kargs):
    print kargs
    return_val = 0
    for each_argument in kargs:
        return_val += each_argument
    return return_val

#Anonymous Fucntion
sum_func = lambda arg1, arg2: arg1 + arg2

#When you run this as a stand alone script, the following will get executed
#When you import this file, the following will not get executed
if __name__ == "__main__":
    localList = [3, 5, 7, 10, 20 , 36, 98, 33, 23, 76]
    print '------------Function - with default args-------------------'
    print  localList
    print getEvenOddNumbers(localList)
    print '------------Function - with default & non - default args-------------------'
    print getEvenOddNumbers(localList, "odd")
    print '------------Function - with non-default args-------------------'
    print getEvenOddNumbers(type="odd", list=localList)
    localList1 = ['Hi', 'Friend', 'lets', 'rock']
    print localList1
    print getEvenOddNumbers(localList1)
    print '------------Function variable arguments-------------------'
    print sumUpAll(10)
    print sumUpAll(10, 20)
    print sumUpAll(10, 20, 30)
    print sumUpAll(10, 20, 30, 40)
    print sumUpAll(10, 20, 30, 40, 50)
    print '------------Anonymous Function-------------------'
    print sum_func(100, 200)
    print sum_func('India', 'is Great')
    print sum_func([10,20], [30,40])


Output:

------------Function - with default args-------------------
[3, 5, 7, 10, 20, 36, 98, 33, 23, 76]
[10, 20, 36, 98, 76]
------------Function - with default & non - default args-------------------
[3, 5, 7, 33, 23]
------------Function - with non-default args-------------------
[3, 5, 7, 33, 23]
['Hi', 'Friend', 'lets', 'rock']
**** Error: not all arguments converted during string formatting
[]
------------Function variable arguments-------------------
(10,)
10
(10, 20)
30
(10, 20, 30)
60
(10, 20, 30, 40)
100
(10, 20, 30, 40, 50)
150
------------Anonymous Function-------------------
300
Indiais Great
[10, 20, 30, 40]
[Finished in 0.2s]

Python regular expressions

import re

print '-----------------regex search/match/compile---------------------'
match = re.search('hello', 'hi hello world')
if match:
    print match.group()
else:
    print 're.search not found'

match = re.match('hello', 'hi hello world')
if match:
    print match.group()
else:
    print 're.match not found'

match = re.compile('hello').match('hello world')
if match:
    print match.group()  #hello
else:
    print 're.compile not found'

print '-----------------regex search---------------------'
#Case Insensitive Search
str = "Mother Teresa"
print("Input sting : " +  str);
match = re.search(r"mother", str, re.IGNORECASE)  #Case Insensitive Search
if (match):
    print("Matched string : " +  match.group())   
#Matched string : Mother
else:
    print("NOT Matched");   


print '-----------------regex findall in a string---------------------'
str1 = 'Send upport related queries to support@organization.com, send admin related queries to admin@organization.com'
emails = re.findall(r'[\w\.-]+@[\w\.-]+', str1)
for email in emails:
    print email

Output:
support@organization.com
admin@organization.com   

print '-----------------regex findall in file---------------------'
f = open('regex.txt', 'r')
strings = re.findall(r'Python', f.read())
for string in strings:
    print('Each String : ', string)

Output:
('Each String : ', 'Python')
('Each String : ', 'Python')
('Each String : ', 'Python')
('Each String : ', 'Python')

print '-----------------regex groups---------------------'
str = "alice@gmail.com"
match = re.match(r'([\w.-]+)@([\w.-]+)', str)
if match:
    print('Match found: ', match.group())
    print('Match found: ', match.group(1))
    print('Match found: ', match.group(2))
else:
    print('No match')

Output:

('Match found: ', 'alice@gmail.com')
('Match found: ', 'alice')
('Match found: ', 'gmail.com')

print '-----------------regex compile---------------------'
# Case Insensitive Search using compile
# Compile is more useful in loops & iterations, this will avoid re-building regex everytime
str = "Mother Teresa"
print("\n Input Str : ", str)
match = re.compile(r"mother ", re.IGNORECASE)  #Case Insensitive Search
if (match.search(str)):
    print("Matched Str")
else:
    print("NOT Matched Str")



Output:

-----------------regex search/match/compile---------------------
hello
re.match not found
hello
-----------------regex search---------------------
Input sting : Mother Teresa
Matched string : Mother
-----------------regex findall in a string---------------------
support@organization.com
admin@organization.com
-----------------regex findall in file---------------------
('Each String : ', 'Python')
('Each String : ', 'Python')
('Each String : ', 'Python')
('Each String : ', 'Python')
-----------------regex groups---------------------
('Match found: ', 'alice@gmail.com')
('Match found: ', 'alice')
('Match found: ', 'gmail.com')
-----------------regex compile---------------------
('\n Input Str : ', 'Mother Teresa')
Matched Str

Read direcory in Python

'''
    File name: dir_read.py
    Author: Prabhath Kota
    Date: June 22, 2018
    Python Version: 2.7
'''

import glob
import os

rootdir = 'Exercise'

print '------------glob------------------'
#List only python files
glob_root_dir = os.path.join(rootdir, '*.py')
arr_list = glob.glob(glob_root_dir)
for each_file in arr_list:
    print(each_file)

print '------------listdir------------------'
#List only txt files
for each_file in os.listdir(rootdir):
    file_path = os.path.join(rootdir,each_file)
    file_base, extension = os.path.splitext(file_path)
    #print extension
    if os.path.isfile(file_path) and extension == '.txt':
        print each_file
print '######'
for each_file in os.listdir(rootdir):
    print(each_file)

print '------------os.walk------------------'
for root, dirs, files in os.walk(rootdir):
    print '---Each loop'
    print root
    if dirs:
        print '###### Dirs'
        for file in dirs:
            print os.path.join(root, file)
    print '###### Files'
    for file in files:
        print os.path.join(root, file)

Jun 23, 2018

Learning For While loops in Python

#!/usr/local/bin/python2.7

'''
    Learning For/While loops in Python
    File name: for_while_loop_python.py
    Author: Prabhath Kota
    Date: June 22, 2018
    Python Version: 2.7
'''

'''
Topics:
For loop
While loop
range
break
continue
enumerate
'''

print '----------------For loop---------------------'
freedom_fighters = ["Lala Lajipati Rai", "Dadabhai Naoroji", "Rajendra Prasad", "Sarojini Naidu", "Dadabhai Naoroji", "Lal Bahadur Shastri"]
print freedom_fighters
for each in freedom_fighters:
print each

print '----------------For loop Break---------------------'
print freedom_fighters
#Print only first 4 elements using Break statement
#Enumerate will get you index of the element as well - starting from 0
#enumerate(freedom_fighters) will start the index from 0
#enumerate(freedom_fighters, 1) will start the index from 1
for index,each in enumerate(freedom_fighters, 1):
print index, each
if index == 4:
break
print '----------------For loop Continue---------------------'
print freedom_fighters
#Print only even elements from the list
for index,each in enumerate(freedom_fighters, 1):
if index % 2 == 1: #Odd elements
continue
else: #Even Elements
print index, each

print '----------------For loop Range---------------' 
squares = []
print range(1,10)
for x in range(0,10):
squares.append(x**2)
print (squares)

print '----------------While loop---------------'
#Try to avoild While loops, use only unless otherwise reequired
#If you sometimes don't handle it, it may end up in infinite loop
i = 0
squares = []
while(i < 10):
squares.append(i**2)
i += 1
print squares

Output:

----------------For loop---------------------
['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
Lala Lajipati Rai
Dadabhai Naoroji
Rajendra Prasad
Sarojini Naidu
Dadabhai Naoroji
Lal Bahadur Shastri
----------------For loop Break---------------------
['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
1 Lala Lajipati Rai
2 Dadabhai Naoroji
3 Rajendra Prasad
4 Sarojini Naidu
----------------For loop Continue---------------------
['Lala Lajipati Rai', 'Dadabhai Naoroji', 'Rajendra Prasad', 'Sarojini Naidu', 'Dadabhai Naoroji', 'Lal Bahadur Shastri']
2 Dadabhai Naoroji
4 Sarojini Naidu
6 Lal Bahadur Shastri
----------------For loop Range---------------
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
----------------While loop---------------
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[Finished in 0.2s]

Learning Strings in Python

#!/usr/local/bin/python2.7

'''
#Learning Strings in Python
    File name: string_all_operations.py
    Author: Prabhath Kota
    Date: June 22, 2018
    Python Version: 2.7
'''

'''
Python Strings are immutable
Python uses single quotes ' double quotes " and triple quotes """
['capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
'''

string = "india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world."
print string

#The capitalize() function returns a string with the first character in capital.
#Immutable
string_cap = string.capitalize()
print string_cap
print string

print '---------------Slice---------------'
print string[0:20]
print string[-5:]

#india also called th
#orld.

print '---------------Forming String with variables---------------'
print "The item {} is repeated {} times".format('test',100)
#The item test is repeated 100 times

print "The item %s is repeated %d times"% ('test',100)
#The item test is repeated 100 times

# default arguments
print("Hello {}, your balance is {}.".format("Adam", 230.2346))
#Hello Adam, your balance is 230.2346.

# positional arguments
print("Hello {0}, your balance is {2}.".format("Adam", 230.2346, 100))
#Hello Adam, your balance is 230.2346.

# keyword arguments
print("Hello {name}, your balance is {blc}.".format(name="Adam", blc=230.2346))
#Hello Adam, your balance is 230.2346.

# mixed arguments
print("Hello {0}, your balance is {blc}.".format("Adam", blc=230.2346))
#Hello Adam, your balance is 230.2346.

#print("{:2.2f}".format(234567.2346768778))
print("{:.3f}".format(12.2346768778))
#12.235

print '---------------check if Substring Exists---------------'
print 'democracy' in string
#True

print '---------------String Copy---------------'
string_1 = string
string_1 += ' India is great'
print string
print string_1
#india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.
#india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world. India is great

print '---------------String Upper/Lower Case---------------'
print string_1.upper()
print string_1.lower()

#INDIA ALSO CALLED THE REPUBLIC OF INDIA IS A COUNTRY IN SOUTH ASIA. INDIA IS THE SEVENTH-LARGEST COUNTRY BY AREA, THE SECOND-MOST POPULOUS COUNTRY (WITH OVER 1.2 BILLION PEOPLE), AND THE MOST POPULOUS DEMOCRACY IN THE WORLD. INDIA IS GREAT
#india also called the republic of india is a country in south asia. india is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world. india is great

print '---------------String Strip---------------'
string_2 = '   India is a democratic country       '
print string_2
string_2 =  string_2.strip()
print string_2

#   India is a democratic country       
#India is a democratic country

print '---------------String Split---------------'
print string
lista = string.split('country')
print lista

#india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.
#['india also called the Republic of India is a ', ' in South Asia. India is the seventh-largest ', ' by area, the second-most populous ', ' (with over 1.2 billion people), and the most populous democracy in the world.']

print '---------------String Join---------------'
print lista
print "," .join(lista)
#print string

#['india also called the Republic of India is a ', ' in South Asia. India is the seventh-largest ', ' by area, the second-most populous ', ' (with over 1.2 billion people), and the most populous democracy in the world.']
#india also called the Republic of India is a , in South Asia. India is the seventh-largest , by area, the second-most populous , (with over 1.2 billion people), and the most populous democracy in the world.

print '---------------String Title---------------'
print string.title()
#India Also Called The Republic Of India Is A Country In South Asia. India Is The Seventh-Largest Country By Area, The Second-Most Populous Country (With Over 1.2 Billion People), And The Most Populous Democracy In The World.

print '---------------String find---------------'
print string
print string.find('country') #returns lowest index
print string.rfind('republic') #returns highest index, if not found, retunns -1
#india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.
#45
#-1

print '---------------String functions---------------'
print string.capitalize()
#India also called the republic of india is a country in south asia. india is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.

print string.count('country') #No of times subsrting has repeated
#3

print string.isspace() #returns true if there are only whitespace characters
#False

string_2 = string.swapcase()
print string_2
#INDIA ALSO CALLED THE rEPUBLIC OF iNDIA IS A COUNTRY IN sOUTH aSIA. iNDIA IS THE SEVENTH-LARGEST COUNTRY BY AREA, THE SECOND-MOST POPULOUS COUNTRY (WITH OVER 1.2 BILLION PEOPLE), AND THE MOST POPULOUS DEMOCRACY IN THE WORLD.

print string_2.swapcase()
#india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.

print string.center(250, '-')
#-------------india also called the Republic of India is a country in South Asia. India is the seventh-largest country by area, the second-most populous country (with over 1.2 billion people), and the most populous democracy in the world.-------------