Skip to content

Instantly share code, notes, and snippets.

View dkhmelenko's full-sized avatar

Dmytro Khmelenko dkhmelenko

View GitHub Profile
@dkhmelenko
dkhmelenko / decorators_chain.py
Created September 19, 2023 07:57
Decorators chaining
@authenticate
@log_request
def get_employees(user):
...
@dkhmelenko
dkhmelenko / decorator_declaration.py
Last active September 20, 2023 08:48
Decorator with arguments
def authenticate(request):
def decorator(*args, **kwargs):
add_auth_headers()
request(*args, **kwargs)
def add_auth_headers():
...
return decorator
@dkhmelenko
dkhmelenko / decorator_usage.py
Last active September 20, 2023 08:46
Decorator usage
@authenticate(user)
def get_employees():
...
get_employees()
@dkhmelenko
dkhmelenko / decorator_declaration.py
Created September 18, 2023 21:01
Decorator declaration
def authenticate(user):
def decorator(request):
add_auth_headers()
request()
def add_auth_headers():
...
return decorator
import boto3
review_text = 'The service was terrible! The food was cold and the staff was rude. Def not recommended'
client = boto3.client('comprehend')
response = client.detect_sentiment(Text=review_text, LanguageCode="en")
print(response['Sentiment'])
@dkhmelenko
dkhmelenko / cdk_example.py
Last active October 12, 2021 21:19
AWS CDK example
from aws_cdk import (aws_s3, aws_s3_notifications, core, aws_sns)
# create S3 bucket
s3 = aws_s3.Bucket(self, "s3bucket_cdk")
# define SNS topic ans assign it
sns_topic = aws_sns.Topic(self, "CDK Notification")
sns_notification = aws_s3_notifications.SnsDestination(sns_topic)
# assign notification for the s3 event type when new object created
var source = new EventSource('/updates');
source.addEventListener('time_update', function(event) {
console.log(event.data);
});
...
source.close();
class UpdatesController < ApplicationController
include ActionController::Live
def updates
response.headers["Content-Type"] = "text/event-stream"
sse = SSE.new(response.stream, retry: 1000, event: "time_update")
sse.write(Time.now)
ensure
sse.close
end
city_companies = {
apple: ["Boston", "Seattle", "San Francisco"],
uniliver: ["London", "Sussex"],
microsoft: ["Seattle", "Austin", "Denver"],
amazon: ["Portland", "Denver", "Washington"],
starbucks: ["Beijing", "Seattle", "Munich"]
}
tech_giants = [:apple, :microsoft, :amazon]
@print_address("Munich, Germany")
def ship_package():
print("Package Shipped")