Skip to content

Instantly share code, notes, and snippets.

@bilalozdemir
bilalozdemir / main.rs
Created January 16, 2022 13:29
Binary Search Tree - Rust
#![allow(unused_variables, dead_code)]
use std::fmt::{Display, Formatter};
#[derive(Clone, PartialEq, Eq)]
struct BST {
key: String,
value: u64,
left: Box<Option<BST>>,
right: Box<Option<BST>>
@bilalozdemir
bilalozdemir / task-queue-beat-config.py
Created June 30, 2021 12:29
Task Queue Subexample - Task Config
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
},
}
@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 June 29, 2021 15:37
Task Queue Example With Celery and Redis
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 June 28, 2021 15:20
Config management with Pydantic & Dotenv
# 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 April 5, 2021 11:33
How to Profile a Python Program - Block 5
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 April 5, 2021 11:32
How to Profile a Python Program - Block 4
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 April 5, 2021 11:31
How to Profile a Python Program - Block 3
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 April 5, 2021 11:30
How to Profile a Python Program - Block 2
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 April 5, 2021 11:27
How to Profile a Python Program - Block 1
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: