Skip to content

Instantly share code, notes, and snippets.

View anthonyli358's full-sized avatar

Anthony Li anthonyli358

  • London, UK
View GitHub Profile
@anthonyli358
anthonyli358 / jsonschema.py
Last active December 2, 2023 14:32
Sanitize input data format
from flask import request
from jsonschema import validate, FormatChecker, exceptions
data = request.json
# Define the schema to check
check_schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
@anthonyli358
anthonyli358 / pi_circle.py
Last active February 18, 2023 16:18
Calculate the value of pi.
import random
def esimate_pi(n: int) -> float:
n_points_in_circle = 0
for _ in range(n):
x = random.uniform(0, 1)
y = random.uniform(0, 1)
# don't need to square root:
# if r < 1 then sqrt(r) < 1
# if r > 1 then sqrt(r) > 1
@anthonyli358
anthonyli358 / ml_docker
Last active February 4, 2023 20:34
Deploy ML model using Docker
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY src/ .
CMD ["python3", "app.py"]
@anthonyli358
anthonyli358 / ml_flask.py
Last active February 4, 2023 20:28
Deploy ML model using Flask
from flask import Flask, jsonify, request
# This code is typically imported from a separate python module
# e.g. from ml_model.predict import predict_result (from folder.pythonfile import function)
# ============
import pickle
def predict_result(transaction_id):
with open("model.pkl", "rb") as file:
model = pickle.load(file)
@anthonyli358
anthonyli358 / dbt_greatest.sql
Last active October 19, 2025 03:57
dbt greatest macro
{%- macro dbt_greatest(cols, dummy_val="'1/1/1900'") -%}
-- Returns null if all arguments are null
{%- if cols is not iterable and (cols is not string and cols is not mapping) -%}
{{ exceptions.raise_compiler_error(
"Argument invalid... dbt_greatest() expects argument format (['col1', 'col2'], 'dummy_val')"
)
}}
{% endif %}
@anthonyli358
anthonyli358 / dbt_pivot.sql
Created September 30, 2022 17:07
dbt pivot
select
product_id,
{{ dbt_utils.pivot(
'month',
dbt_utils.get_column_values(ref('product_sales'), 'month'),
then_value='amount'
) }}
from {{ ref('product_sales') }}
group by product_id
@anthonyli358
anthonyli358 / dbt_snapshot.sql
Created September 30, 2022 16:08
dbt snapshot
{% snapshot product_sales_snapshot %}
{{ config(
tags = ['hourly', 'product'], -- we can use these for airflow orchestration
target_schema = 'product',
unique_key = 'product_sale_id',
strategy = 'check',
check_cols = 'all',
)
}}
@anthonyli358
anthonyli358 / snowflake_try_to_date.sql
Created September 21, 2022 15:39
Snowflake try to date
select
coalesce(
try_to_date(freeflow_sale_date),
try_to_date(freeflow_sale_date, 'DD Mon YYYY'), -- 19 September 2020
try_to_date(freeflow_sale_date, 'DD/MM/YYYY'), -- 14/12/2020
try_to_date(freeflow_sale_date, 'YYYY/MM/DD') -- 2020/12/08
) as sale_date
from product_sales
@anthonyli358
anthonyli358 / snowflake_pivot.sql
Last active September 21, 2022 15:10
Snowflake pivot
with to_pivot as (
select
product_id,
month,
amount
from product_sales
)
select *
from to_pivot
@anthonyli358
anthonyli358 / snowflake_variables.sql
Last active October 19, 2025 03:58
Snowflake variables
select
iff(p.status in ('open', 'active'), 'active', i.status) as clean_status,
iff(clean_status = 'active', true, false) as is_active
from product_sales p