Skip to content

Instantly share code, notes, and snippets.

View ybressler's full-sized avatar
🖤

Yaakov Bressler ybressler

🖤
View GitHub Profile
@ybressler
ybressler / user_input.py
Created May 24, 2024 19:16
Stubbed interface for testing getting a user input from the CLI
from abc import ABC, abstractmethod
import builtins
from typing import Optional, Dict
import pytest
class BaseInput(ABC):
@abstractmethod
@ybressler
ybressler / main.py
Last active September 19, 2022 17:35
A simple API (using FastAPI) to be deployed to Deta Cloud.
import os
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def hello():
"""Simple hello world function"""
"""
665. Non-decreasing Array [Medium]
https://leetcode.com/problems/non-decreasing-array/
# PROBLEM:
Given an array nums with n integers, your task is to check if it could become
non-decreasing by modifying at most one element.
We define an array is non-decreasing if nums[i] <= nums[i + 1] holds
"""
787. Cheapest Flights Within K Stops
https://leetcode.com/problems/cheapest-flights-within-k-stops/solution/
DFS Solution in python
"""
from collections import deque, defaultdict
class Solution:
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
@ybressler
ybressler / get_first_result.sql
Last active June 27, 2021 17:09
Get first result from a table with a filter, using SQL
SELECT
html
FROM
url_content
WHERE
url=url
AND date_created >= '1 week ago'
ORDER BY
date_created DESC
LIMIT 1;
@ybressler
ybressler / get_first_result_sqlalchemy.py
Last active April 26, 2022 14:13
Get first result from a table with a filter, using SQLAlchemy
from datetime import datetime
# Create a helpful lambda to get timestamp for n hours ago
n_hours_ago = lambda x: datetime.utcnow() - datetime.timedelta(hours=x)
# Your query
# Note: I don't bother properly importing `session` or `UrlContent` here
query = session.query(UrlContent)\
.filter_by(
url==url,
@ybressler
ybressler / pseudo_code_for_get_or_create.py
Created June 27, 2021 17:06
Pseudo-code for "get or create" method for retrieving a web page's html content
import requests
url = 'https://www.python.org/'
# Query with pseudo-code
result = """
SELECT
html
FROM
url_content
@ybressler
ybressler / html_table.py
Created June 27, 2021 17:00
Example of a mapped class for an SQL table in SQLAlchemy
import datetime
from sqlalchemy.orm import declarative_base
# declarative base class
Base = declarative_base()
class UrlContent(Base):
__tablename__ = 'url_content'
id = Column(Integer, primary_key=True, autoincrement=True)
url = Column(String(500), nullable=False, unique=False)
import pandas as pd
from nameparser import HumanName
from functools import lru_cache
# sample data
records = [{"name":"Mr. Lin-Manuel Miranda"}, {"name":"Ms. Katrina Lenk"},
{"name":"Renée Elise Goldsberry"}, {"name":"Scott Leo \"Taye\" Diggs"},
{"name":"Ms. Rebecca \"The Boss\" Teichman"}]
# Load into a dataframe
name name_title name_first name_middle name_last name_suffix name_nickname
0 Mr. Lin-Manuel Miranda Mr. Lin-Manuel Miranda
1 Mz. Katrina Lenk Mz. Katrina Lenk
2 Renée Elise Goldsberry Renée Elise Goldsberry
3 Scott Leo "Taye" Diggs Scott Leo Diggs Taye
4 Mz. Rebecca "The Boss" Teichman Mz. Rebecca Teichman The Boss