Skip to content

Instantly share code, notes, and snippets.

@bilalozdemir
bilalozdemir / task-queue-beat-config.py
Created Jun 30, 2021
Task Queue Subexample - Task Config
View task-queue-beat-config.py
app.conf.beat_schedule = {
'renew-expired-subscriptions': {
'task': 'runner.renew_expired_subscriptions',
'schedule': 5.0, # Runs in every 5 seconds
#'schedule': crontab(minute=5) # Runs in every 5 minutes
#'args': (arg_1, arg_2, ...), # Run the function with arguments
},
}
View task-queue-triggered-task.py
@app.task(
name='send-email-to-user',
default_retry_delay=300,
max_retry=5,
soft_time_limit=30
)
def send_email_to_user(
user_email: str,
email_template: str,
extra_info: dict
@bilalozdemir
bilalozdemir / task-queue-example-celery-redis.py
Created Jun 29, 2021
Task Queue Example With Celery and Redis
View task-queue-example-celery-redis.py
import logging
import datetime
import smtplib
import ssl
from typing import Optional
from celery import Celery
#from celery.schedules import crontab
REDIS_BASE_URL = 'redis://localhost:6379'
@bilalozdemir
bilalozdemir / config_helper.py
Created Jun 28, 2021
Config management with Pydantic & Dotenv
View config_helper.py
# src/helpers/config_helper.py
import os
import logging
from typing import Literal, Optional, Union
from functools import lru_cache
from pydantic import BaseSettings, HttpUrl, SecretStr, validator, StrictInt
from dotenv import dotenv_values
class Settings(BaseSettings):
@bilalozdemir
bilalozdemir / how-to-profile-a-python-program-5.txt
Created Apr 5, 2021
How to Profile a Python Program - Block 5
View how-to-profile-a-python-program-5.txt
Filename: example_2.py
Line # Mem usage Increment Occurences Line Contents
============================================================
3 18.6 MiB 18.6 MiB 1 @profile
4 def wasteful_sum_function() -> int:
5 19.1 MiB 0.5 MiB 10003 return sum([x**2 for x in range(10**4)])
Filename: example_2.py
Line # Mem usage Increment Occurences Line Contents
============================================================
@bilalozdemir
bilalozdemir / how-to-profile-a-python-program-4.py
Created Apr 5, 2021
How to Profile a Python Program - Block 4
View how-to-profile-a-python-program-4.py
from memory_profiler import profile
@profile
def wasteful_sum_function() -> int:
return sum([x**2 for x in range(10**4)])
@profile
def resource_efficient_sum_function() -> int:
return sum((x**2 for x in range(10**4)))
@bilalozdemir
bilalozdemir / how-to-profile-a-python-program-3.py
Created Apr 5, 2021
How to Profile a Python Program - Block 3
View how-to-profile-a-python-program-3.py
import timeit
import requests
from bs4 import BeautifulSoup
def get_word_count_of(url: str) -> int:
res = requests.get(url)
if res.status_code == 200:
_data = res.text
return count_words(_data)
return 0
@bilalozdemir
bilalozdemir / how-to-profile-a-python-program-2.py
Created Apr 5, 2021
How to Profile a Python Program - Block 2
View how-to-profile-a-python-program-2.py
import requests
from bs4 import BeautifulSoup
def get_word_count_of(url: str) -> int:
res = requests.get(url)
if res.status_code == 200:
_data = res.text
return count_words(_data)
return 0
@bilalozdemir
bilalozdemir / how-to-profile-a-python-program-1.py
Last active Apr 5, 2021
How to Profile a Python Program - Block 1
View how-to-profile-a-python-program-1.py
import cProfile
from math import sqrt
BIG_NUMBER = 10**25
def slow_function(n: int) -> None:
for _ in range(10**5):
sqrt(n)
def fast_function(n: int) -> None: