Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Python

Certainly! Here are all the Python concepts with examples in a single page for easy copying:

```python
### Basic Syntax

# Comments
# This is a single-line comment
"""
This is a
multi-line comment
"""

# Variables
x = 10
name = "Alice"

# Data Types
integer = 5
floating = 5.5
string = "Hello"
boolean = True

# Basic Operators
sum = 5 + 3
difference = 5 - 3
product = 5 * 3
quotient = 5 / 3
modulus = 5 % 3
exponent = 5 ** 3
floor_division = 5 // 3

# Input and Output
name = input("Enter your name: ")
print("Hello, " + name)

# Indentation
if True:
    print("This is indented")

### Data Structures

# Lists
# Creating Lists
fruits = ["apple", "banana", "cherry"]

# Accessing Elements
print(fruits[1])

# List Methods
fruits.append("orange")
fruits.extend(["mango", "grape"])
fruits.insert(1, "kiwi")
fruits.remove("banana")
print(fruits.pop())
fruits.clear()
print(fruits.index("cherry"))
print(fruits.count("apple"))
fruits.sort()
fruits.reverse()

# List Comprehensions
squares = [x**2 for x in range(10)]

# Tuples
# Creating Tuples
my_tuple = ("apple", "banana", "cherry")

# Accessing Elements
print(my_tuple[1])

# Tuple Methods
print(my_tuple.count("apple"))
print(my_tuple.index("banana"))

# Sets
# Creating Sets
my_set = {"apple", "banana", "cherry"}

# Set Methods
my_set.add("orange")
my_set.remove("banana")
my_set.discard("banana")
print(my_set.pop())
my_set.clear()
union_set = my_set.union({"mango", "grape"})
intersection_set = my_set.intersection({"banana", "cherry"})
difference_set = my_set.difference({"banana", "cherry"})
sym_diff_set = my_set.symmetric_difference({"banana", "cherry"})

# Dictionaries
# Creating Dictionaries
person = {"name": "John", "age": 30}

# Accessing Elements
print(person["name"])

# Dictionary Methods
print(person.keys())
print(person.values())
print(person.items())
print(person.get("age"))
person.update({"age": 31})
print(person.pop("name"))
person.clear()

### Control Structures

# If Statements
if x > 5:
    print("x is greater than 5")
elif x == 5:
    print("x is equal to 5")
else:
    print("x is less than 5")

# For Loops
for i in range(5):
    print(i)

# While Loops
i = 0
while i < 5:
    print(i)
    i += 1

# Break and Continue Statements
for i in range(5):
    if i == 3:
        break
    print(i)

for i in range(5):
    if i == 3:
        continue
    print(i)

### Functions

# Defining Functions
def greet(name):
    return f"Hello, {name}!"

print(greet("Alice"))

# Arguments and Parameters
def add(a, b):
    return a + b

print(add(3, 5))

# Lambda Functions
square = lambda x: x**2
print(square(4))

# Built-in Functions
print(len("Hello"))
print(range(5))
print(type(5))
print(isinstance(5, int))

### Modules and Packages

# Importing Modules
import math
from math import sqrt
import math as m

print(math.pi)
print(sqrt(16))
print(m.e)

# Creating and Using Modules
# Assume a file called mymodule.py with function:
# def hello():
#     print("Hello, World!")
import mymodule
mymodule.hello()

# Installing Packages with pip
# pip install requests

### File Handling

# Opening Files
file = open("example.txt", "r")

# Reading Files
content = file.read()
line = file.readline()
lines = file.readlines()

# Writing Files
file = open("example.txt", "w")
file.write("Hello, World!")

# Closing Files
file.close()

# Context Manager
with open("example.txt", "r") as file:
    content = file.read()

### Error and Exception Handling

# Try and Except Block
try:
    x = 1 / 0
except ZeroDivisionError:
    print("You can't divide by zero!")

# Finally Clause
try:
    x = 1 / 0
except ZeroDivisionError:
    print("You can't divide by zero!")
finally:
    print("This will always execute.")

# Raising Exceptions
if x < 0:
    raise ValueError("x must be non-negative")

### Object-Oriented Programming (OOP)

# Classes and Objects
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def bark(self):
        return f"{self.name} is barking."

dog1 = Dog("Buddy", 3)
print(dog1.bark())

# Inheritance
class Puppy(Dog):
    def __init__(self, name, age, breed):
        super().__init__(name, age)
        self.breed = breed

puppy1 = Puppy("Max", 1, "Labrador")
print(puppy1.bark())

# Polymorphism
class Cat:
    def speak(self):
        return "Meow"

class Dog:
    def speak(self):
        return "Bark"

animals = [Cat(), Dog()]
for animal in animals:
    print(animal.speak())

# Encapsulation
class Car:
    def __init__(self):
        self.__speed = 0

    def set_speed(self, speed):
        self.__speed = speed

    def get_speed(self):
        return self.__speed

car = Car()
car.set_speed(50)
print(car.get_speed())

# Dunder (Magic) Methods
class Person:
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return f"Person named {self.name}"

    def __repr__(self):
        return f"Person({self.name})"

person = Person("Alice")
print(str(person))
print(repr(person))

### Libraries and Frameworks

# NumPy
import numpy as np
array = np.array([1, 2, 3, 4])
print(array)
matrix = np.matrix([[1, 2], [3, 4]])
print(matrix)

# Pandas
import pandas as pd
data = {'name': ['Alice', 'Bob'], 'age': [25, 30]}
df = pd.DataFrame(data)
print(df)

# Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()

# Requests
import requests
response = requests.get('https://api.github.com')
print(response.status_code)

# Flask (Minimal Example)
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask!"

if __name__ == "__main__":
    app.run()

# Django (Minimal Example)
# Install Django and run: django-admin startproject mysite
# Then define views and URLs accordingly.

### Advanced Topics

# List Comprehensions
squares = [x**2 for x in range(10)]

# Generators and Iterators
def generator():
    yield 1
    yield 2
    yield 3

gen = generator()
print(next(gen))
print(next(gen))
print(next(gen))

# Decorators
def decorator_function(original_function):
    def wrapper_function():
        print("Wrapper executed this before {}".format(original_function.__name__))
        return original_function()
    return wrapper_function

@decorator_function
def display():
    print("Display function ran")

display()

# Context Managers
with open('example.txt', 'w') as file:
    file.write('Hello, World!')

# Multithreading and Multiprocessing
import threading
import multiprocessing

def print_numbers():
    for i in range(5):
        print(i)

# Threading
thread = threading.Thread(target=print_numbers)
thread.start()

# Multiprocessing
process = multiprocessing.Process(target=print_numbers)
process.start()

# Regular Expressions
import re
pattern = re.compile(r'\d+')
result = pattern.match('123abc')
print(result.group())

# File I/O (JSON, CSV)
import json
import csv

# JSON
data = {'name': 'John', 'age': 30}
json_str = json.dumps(data)
data = json.loads(json_str)

# CSV
with open('example.csv', mode='w') as file:
    writer = csv.writer(file)
    writer.writerow(['name', 'age'])
    writer.writerow(['Alice', 30])

### Testing and Debugging

# Debugging Tools
import pdb
pdb.set_trace()

# Unit Testing
import unittest

class TestStringMethods(unittest.TestCase):
    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

if __name__ == '__main__':
    unittest.main()

``` 

=========================re================

import psycopg2
import csv
import boto3
import pandas as pd
import datetime
import os
import json
from io import StringIO
from botocore.exceptions import ClientError
import random
import pytz
import logging

logging.basicConfig(format='%(asctime)s,%(msecs)s,%(levelname)-8s'
                    '[%(filename)s:%(lineno)d] %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
logger = logging.getLogger()
logger.setLevel(logging.INFO)

timez = pytz.timezone('Europe/London')
current_datetime = datetime.datetime.now(timez)
datetime_str = current_datetime.strftime("%Y-%m-%d-%H-%M-%S")
sql_frmt_date = current_datetime.strftime("%d/%m/%Y %H:%M:%S")
secret_name = os.environ['secret_name']
cdp_db_table = os.environ['cdp_db_table']

# sns_client = boto3.client('sns', region_name='eu-west-1')
s3_bucket = 'raj-postgress-data-test'
s3_client = boto3.client('s3')

def lambda_handler(event, context):
    session = boto3.session.Session()

    client = session.client(service_name='secretsmanager', region_name='eu-west-1')
    logger.info("Retrieving the secret data")
    get_secret_value_response = client.get_secret_value(SecretId=secret_name)
    secret_db = json.loads(get_secret_value_response['SecretString'])
   
    DB_HOST = secret_db["DB_HOST"]
    DB_NAME = secret_db['DB_NAME']
    DB_USER = secret_db['DB_USER']
    DB_PASSWORD = secret_db['DB_PASSWORD']
    DB_PORT = secret_db['DB_PORT']

    try:
        logger.info("Establishing Database Connection")
        conn = psycopg2.connect(host=DB_HOST, database=DB_NAME, user=DB_USER, password=DB_PASSWORD, port=DB_PORT)
        raj_get_all_userdata(conn)
    except Exception as dbex:
        logger.info("Unable to connect database due to: {}".format(str(dbex)))
        raise dbex
    return

def raj_get_all_userdata(conn):
    cursor = conn.cursor()
    column_names = ['channelId', 'CreationDateTime', 'sessionId', 'instructionType', 'channelUserId', 'channelCustomerName', 'customer']
    query = f"SELECT * FROM {cdp_db_table}"
    cursor.execute(query)
    data = cursor.fetchall()
    raj_create_data_csv(data, column_names, conn)

def raj_create_data_csv(data, column_names, conn):
    try:
        csv_data = [column_names]
        csv_data.extend(data)
        csv_string = '\n'.join([','.join(map(str, row)) for row in csv_data])

        datetime_str = current_datetime.strftime("%Y-%m-%d-%H-%M-%S")
        pg_key = f'raj_postgres_data_{datetime_str}.csv'
        data = StringIO(csv_string)
        df = pd.read_csv(data, sep=",")
        response = s3_client.put_object(Body=csv_string, Bucket=s3_bucket, Key=pg_key)
        raj_create_control_csv(data, df, pg_key, conn)
    except Exception as dataex:
        logger.info("Unable to create CSV file due to: {}".format(str(dataex)))
        raise dataex

def raj_create_control_csv(data, df, pg_key, conn):
    try:
        bussiness_date = current_datetime.strftime("%Y-%m-%d")
        previous_date = current_datetime - datetime.timedelta(days=1)
        previous_date_str = previous_date.strftime("%Y-%m-%d")
        row_count = len(data)
        control_file_column_names = ['Portfolio Code', 'Business Date', 'Row Count', 'File Name']
        control_file_csv_data = [control_file_column_names]
        control_file_csv_info = ['Raj PostGres', previous_date_str, row_count, pg_key]
        control_file_csv_data.append(control_file_csv_info)
        cl_csv_string = '\n'.join([','.join(map(str, ct_row)) for ct_row in control_file_csv_data])
        pg_control_key = f'raj_postgres_controlfile_{datetime_str}.csv'
        response = s3_client.put_object(Body=cl_csv_string, Bucket=s3_bucket, Key=pg_control_key)
    except Exception as ctrlex:
        logger.info("Unable to create CSV file due to : {}".format(str(ctrlex)))
        raise ctrlex




Post a Comment

0 Comments

Ad Code

Responsive Advertisement