This a cheatsheet for how to implement different stuff in Python. I found those instructions helpful for me, while I was writing some scripts to parse some CSV, or to deploy some Lambda functions on AWS.
Let's start:
import datetime
now = datetime.datetime.now()
print(now.isoformat())
past = now - datetime.timedelta(hours=5)
from datetime import date
date_object = date.today()
print(date_object.strftime("%b")) # Printing the Month. For example 'May'
print(date_object.strftime("%Y-%m-%d")) # Printing the date in ISO Format
print(date_object.isoformat()) # Also gives the ISO Format
date_object = date.fromisoformat("2020-06-09") # Parsing something like '2020-06-09'
date_object = date(2020, 5, 23) # Creating a new Date Object
date.today() + datetime.timedelta(days=-5)
from glob import glob
import csv
files = glob("*.csv")
for file_path in files:
with open(file_path, newline="") as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
next(reader) # Skip the first
for row in reader:
print(row)
import json
def lambda_handler(event, context):
return { 'statusCode': '200', 'body': 'Hello World!', 'header': {'Content-Type': 'application/json'} }
I will expand on this section later
How to deploy a Python Lambda Function to AWS with some PIP Packages Let's assume you have a Lambda function already with the name test. You can update it using the following
pip3 install --target ./package pyjwt
cd package
zip -r package.zip ./*
zip -g package.zip lambda_function.py
aws lambda update-function-code --function-name test --zip-file fileb://package.zip
Please see this Post Sorting in Python
My name is Omar Qunsul. I write these articles mainly as a future reference for me. So I dedicate some time to make them look shiny, and share them with the public.
You can find me on twitter @OmarQunsul, and on Linkedin.