Posts

Showing posts with the label Python

AWS - SQS Demo Python

Image
  Short demo on how to use Python to create a AWS SQS producer and consumer . All we need is a SQS queue and keep the SQS URL ready. I am using boto3 library to call SQS. # SQS producer code # Code generates random greeting message # We will feed random generates messages to SQS # Message will generated for every 10sec import random; import boto3; import time; from Sitecheck import response # Initialize boto3 SQS client sqs = boto3.client( 'sqs' ); # URL for the SQS queue = 'https://sqs.us-east-1.amazonaws.com/851725408580/demo-sqs'; # Producer Code def generate_welcome_message (): greetings = [ "Hello" , "Hi" , "Welcome" , "Howdy" , "Greetings" ] compliments = [ "nice to see you" , "great to have you here" , "welcome aboard" ] greeting = random.choice( greetings ) compliment = random.choice( compliments ) message_to_send = ( greeting + " " + compliment ); d...

GitHub Integration With Pycharm IDE

Image
  The below post is the first step in learning the AWS Developers Tool.  We will see how to integrate Pycharm IDE with GitHub. First, We need a GitHub Account and we need to generate a Token. I have a GitHub repo called "aws_code_deploy". Now let's set up a secure encrypted communication between the PyCharm IDE & the GitHub Repository. To ensure that your PyCharm IDE can access your GitHub Repository, you must generate a Personal Access Token from  here . The scopes gist, read:org, and repo are the minimum that must be granted to the access token. Any additional scopes can be granted as per requirement. Now, We have the token generated. In PyCharm go to File Menu→ Settings → Version Control → GitHub. Now, the GitHub account is added. Now PyCharm is permitted to access your GitHub Repo, but we still need to technically enable the secure encrypted exchange via SSH. Unless this is done, it will not be possible to Clone the Repository or Pus...

AWS - Lambda Function

Image
  This is a two-part series. In this first post, we will see how to create a lambda function and the type of lambda function. Steps to create lambda function on AWS UI. One of the prerequisites is to create a role that as Lambda execution permission. Now we have a role created. Let's proceed with the function creation. Let's test the code by creating an event. Lambda function can be invoked ASYNC or SYNC. From UI, ASYNC is the default setting. Executing the lambda via SYCN/ASYCN can be triggered via CLI. SYNC: When a lambda is invoked via SYNC it sends the response to the calling function once the lambda execution is completed. ASYNC: When a lambda is invoked via ASYNC it sends an empty response with response code 202. As ASYNC places the lambda into an event queue and executes ASYNC-ly. > aws lambda invoke --function-name <lambda-name> --invocation-type RequestResponse/Event  --cli-binary-format raw-in-base64-out --payload <JSON> response.json - -invocation-typ...

Python Data Structures - Linked List - Part 3 - Create and Insert Node into a Linked List

Image
  # Singly linked list # Code has 2 parts # 1) Node part # 2) Linked list part # Adding node to end of the list # Creating class for Node class Node :     # Creating initializer function to invoke attributes and methods in it when an object is created.     def __init__ ( self , value ):         self . value = value ; # Assigning value to the Node.         self . next = None ; # Setting next address as None - Default # Creating class for Linked List class LinkedList :     # Creating initializer function to create a link with HEAD and TAIL to NONE     def __init__ ( self ):         self . head = None ;         self . tail = None ;     # Creating a add_link function to insert a node into Linked List     def add_link ( self , value ):         # Creating object to call Node class         new...

Python Data Structures - Linked List - Part 2

Image
 Let's see how to insert a node into a singly linked list diagrammatically: Here is my singly linked list with 2 nodes. The Linked list starts with HEAD -> Hold the address of the next node -> 001 -> Address of node1 -> Node1 holds the address of Node2 -> Node2 is the last node in the linked list and it has NULL reference. Now, Let's insert a node called "Node3" at the beginning of the linked list. So, When we add a node at the beginning of the list: 1) Make sure the HEAD is updated to point at the new node address (003). 2) New node holds the reference to the previous HEAD node (001). HEAD (003) -> Node 3 refers to 001 -> Node 1 refers to 002 -> Node 2 is NULL. What happens when the node is added to the end of the list: Let's add Node 4 to the end of the list: This is going to be straightforward. Update the previous last node pointing to the address of the new node (004) and the new node pointing to NULL as it is the last node of the list.

Python Data Structures - Linked List - Part 1

Image
  Linked List: A linked list is a form of a collection of data. Data does not have to be in any order. A linked list comprises independent nodes (data + links). Each node contains a link to the next node in the link. Imagine a linked list of a train car. How the linked list is different from the list/array? 1) List/Array uses the index to fetch the elements. 2) List/Array stores the element in a continuous memory location. Below is the linked list. Type of linked list: Single linked list: Each node holds data and references to the next node in the list. Reference is the memory location/address of the node. Node 1 holds the address of Node 2 and it goes on until the tail of the linked list. This type of linked list provides the ability to add and remove nodes in run time. Circular linked list: Reference of the last node points to the address of the first node in the linked list and it creates a circle. This is used in multiple-player games. Circular double linked lis...

Python Basic Programs

Image
# Total number of odd numbers in list numbers=[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ]; count= 0 ; for i in numbers: if i== 1 : count=count+ 1 ; else : if (i% 2 != 0 ): count=count+ 1 ; print ( "Total odd numbers: " ,count); # Using comprehension odd = [num for num in numbers if num % 2 != 0 ]; print ( "Odd Numbers: " ,odd); # Using lambda odd = list ( filter ( lambda x:x% 2 != 0 , numbers)); print ( "Odd Numbers: " ,odd); --------------------------------------------------------------------- # Remove items from a list while iterating # You need to remove items from a list while iterating but without creating a different copy of a list. # Remove numbers greater than 50 number_list = [ 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 ]; for i in range ( len (number_list)- 1 ,- 1 ,- 1 ): print (i); --------------------------------------------------------------------- # Print the following num...

Python - Data Science - Mean/Median/Mode

Image
What is a Mean? A mean is the simple mathematical average of a set of two or more numbers. Eg:  The sum of the 57 Boys weight is 231.51 and hence the mean is 231.51/57 = 4.06 What is the Median? The median is the middle number in a sorted, ascending or descending, list of numbers and can be more descriptive of that data set than the average. For example, in a data set of {3, 13, 2, 34, 11, 26, 47}, the sorted order becomes {2, 3, 11, 13, 26, 34, 47}. The median is the number in the middle {2, 3, 11, 13, 26, 34, 47}, which in this instance is 13 since there are three numbers on either side. For example, in a data set of {3, 13, 2, 34, 11, 17, 27, 47}, the sorted order becomes {2, 3, 11, 13, 17, 27, 34, 47}.  The median is the average of the two numbers in the middle which in this case is fifteen {2, 3, 11, 13, 17, 27, 34, 47} -> {(13 + 17) ÷ 2 = 15}. What is a Mode? The is the most frequent observation (or observations) in a sample. We have the sample [4, 1, 2, 2, 3, 5]...