Saturday 25 February 2023

Aws Lambda and Python Automation

This is not a tutorial for Lambda

A script that takes Guardian Home Page headlines and send it automatically Using Lambda (AWS Lambda)


1. Wrote the code, I won't share the full code, but you can have a look at this small snippet or in the YouTube video. 


 2. Create a Lambda Function:


We also created EventBridge Cloudwatch trigger for our cron which will run the code every minutes. 

3. Packages uploaded as python.zip
If it is not zipped in this structure, you might face some issues depending on your system config.

4. Used docker to create an instance of python3.9 container, connected to it from inside


Going into the folder where I want to download my packages, e.g. I want to do pip install requests -t .
Meaning, I want to install in that particular folder.

5. Now I want to access the container from the vscode. 

 

These steps helped me create volume that helped my container python3.6 access the local folder. I installed packages here and zipped to upload as a lambda layer. The picture of WatchCloud shows my code ran just fine. 














Wednesday 15 February 2023

Let's do something fun with python

Suppose you have $100, which you can invest with a 10% return each year. Add code to calculate how much money you end up with after 7 years, and print the result.

# Addition, subtraction
print(5 + 5)
print(5 - 5)


# Multiplication, division, modulo, and exponentiation
print(3 * 5)
print(10 / 2)
print(18 % 7)
print(4 ** 2)


# How much is your $100 worth after 7 years?

print(100 * (1.1) ** 7)


Write a function to calculate bmi, it should be able to convert numbers into float. 

def bmi_cal():

    x = float(input("what is your height in meters"))

    y = float(input("what is your weight in kg?"))

    bmi = y/x ** 2

    print(bmi)


bmi_cal()


Create a conditional sublist of dictionary. 


# Create a dictionary with some key-value pairs
my_dict = {"key1": "value1", "key2": "value2", "key3": "value3"}

# Get a sublist of the dictionary containing key-value pairs for "key1" and "key2"
sublist = {key: value for key, value in my_dict.items() if key in ["key1", "key2"]}

# Print each key and value in the sublist
for key, value in sublist.items():
    print(f"Key: {key}, Value: {value}")