Skip to content

Instantly share code, notes, and snippets.

View ajitsamudrala's full-sized avatar

ajitsamudrala

View GitHub Profile
@ajitsamudrala
ajitsamudrala / rolling_avg.sql
Last active March 21, 2023 04:24
SQL Solution
WITH total_transaction_amount AS (SELECT DATE(transaction_time) AS transaction_date, SUM(transaction_amount) AS date_total FROM transactions
WHERE transaction_time BETWEEN '2021-01-29' AND '2021-02-01'
GROUP BY DATE(transaction_time))
SELECT rolling_3_day_transaction_average AS jan_31_rolling_3_day_avg FROM
(SELECT transaction_date, date_total, AVG(date_total) OVER(ORDER BY transaction_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
AS rolling_3_day_transaction_average
FROM total_transaction_amount) AS rolling_avg
WHERE transaction_date = '2021-01-31';
import concurrent.futures as cf
import logging
import math
import numpy as np
import time
import threading
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
import concurrent.futures as cf
import logging
import math
import numpy as np
import time
import threading
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
@ajitsamudrala
ajitsamudrala / threading_lock.py
Created August 21, 2019 04:04
Lock gives exclusive right to a thread.
import concurrent.futures as cf
import logging
import time
import threading
LOCK = threading.Lock()
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
@ajitsamudrala
ajitsamudrala / race_condition_w_asyncio.py
Created August 21, 2019 03:46
both coroutines enter dead lock
import asyncio
async def foo():
await boo()
async def boo():
await foo()
async def main():
await asyncio.gather(*[foo(), boo()])
@ajitsamudrala
ajitsamudrala / rece_condition_asyncio.py
Created August 21, 2019 03:37
Race conditions are race with asyncio
import asyncio
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
class DbUpdate:
def __init__(self):
self.value = 0
@ajitsamudrala
ajitsamudrala / race_condition.py
Created August 21, 2019 03:29
Race conditions can lead to unintended results.
import concurrent.futures as cf
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
class DbUpdate:
def __init__(self):
self.value = 0
@ajitsamudrala
ajitsamudrala / delay_message_w_blocker.py
Created August 20, 2019 18:09
A blocker task can stall the progress of the program.
import asyncio
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
num_word_mapping = {1: 'ONE', 2: 'TWO', 3: "THREE", 4: "FOUR", 5: "FIVE", 6: "SIX", 7: "SEVEN", 8: "EIGHT",
9: "NINE", 10: "TEN"}
@ajitsamudrala
ajitsamudrala / delay_message_cf.py
Created August 20, 2019 17:43
delays and prints message using ThreadPool
import concurrent.futures as cf
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
num_word_mapping = {1: 'ONE', 2: 'TWO', 3: "THREE", 4: "FOUR", 5: "FIVE", 6: "SIX", 7: "SEVEN", 8: "EIGHT",
9: "NINE", 10: "TEN"}
@ajitsamudrala
ajitsamudrala / delay_message_w_asyncio_gather.py
Last active March 30, 2021 09:21
Using asyncio.gather to create multiple tasks in one shot.
import asyncio
import logging
import time
logger_format = '%(asctime)s:%(threadName)s:%(message)s'
logging.basicConfig(format=logger_format, level=logging.INFO, datefmt="%H:%M:%S")
num_word_mapping = {1: 'ONE', 2: 'TWO', 3: "THREE", 4: "FOUR", 5: "FIVE", 6: "SIX", 7: "SEVEN", 8: "EIGHT",
9: "NINE", 10: "TEN"}