Skip to content

Instantly share code, notes, and snippets.

View mschulz's full-sized avatar

Mark Schulz mschulz

View GitHub Profile
@mschulz
mschulz / retry.py
Created May 31, 2025 02:38
gspread retry on 503 error
import gspread
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception
from requests.exceptions import HTTPError
def is_503_error(exception):
if isinstance(exception, HTTPError) and exception.response is not None:
return exception.response.status_code == 503
return False
@mschulz
mschulz / enum_example.py
Created May 31, 2025 01:40
Uses of Enum
# Basic Approach
from enum import Enum
class Status(Enum):
PENDING = "pending"
APPROVED = "approved"
REJECTED = "rejected"
def process(status: Status):
if status == Status.APPROVED:
print("Processing approved request")
@mschulz
mschulz / sentinel.py
Created May 31, 2025 00:01
Use a sentinel for a missing value, not None
class Sentinel:
def __init__(self, name):
self.name = name
def __repr__(self):
return f"<{self.name}>"
MISSING = Sentinel("MISSING")
UNSET = Sentinel("UNSET")
@mschulz
mschulz / gist:081254ffc0f15819801c6c0c91e9330b
Last active March 26, 2025 22:45
sqlalchemy - automatically truncate strings
#Create a TypeDecorator in your base model for an even more generic answer. Then, use this TypeDecorator instead of string.
#In base.py
from sqlalchemy.types import TypeDecorator
class TruncateString(TypeDecorator):
"""trim spaces from the string"""
impl = db.String
@mschulz
mschulz / gist:87ab88848d6ecd82d612c86ffd96cb79
Created March 26, 2025 22:33
sqlalchemy - automatically truncate string
import sqlalchemy.types as types
class LimitedLengthString(types.TypeDecorator):
impl = types.String
def process_bind_param(self, value, dialect):
return value[:self.impl.length]
def copy(self, **kwargs):
@mschulz
mschulz / Conditional.py
Created October 17, 2024 22:51
Optimize Multiple Conditions with any and all
# Bad
if (a > 5) and (b < 10) and (c == 20):
pass
# Good
if all([a > 5, b < 10, c == 20]):
pass
# Or for the opposite
if any([a > 5, b < 10, c == 20]):
@mschulz
mschulz / gist:10910f4553ada9ba07b5a46642391825
Created September 25, 2024 03:47
JSON loads of date time item
import jsondatetime as json
>>> test = '{"name": "John Doe", "born": "Thu, 1 Mar 2012 10:00:49 UTC"}'
>>> json.loads(test)
{'name': 'John Doe', 'born': datetime.datetime(2012, 3, 1, 10, 0 ,49)}
@mschulz
mschulz / gist:4e0661edbdac4cb32d5daafed05c692d
Created September 25, 2024 01:47
Validating JSON Against a Schema
from jsonschema import validate
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
}
@mschulz
mschulz / gist:bf8b61c014261ed6a75e952f0f69d1d0
Created September 25, 2024 01:33
Sorting dict objects
dict1 = [
{"Name":"Karl",
"Age":25},
{"Name":"Lary",
"Age":39},
{"Name":"Nina",
"Age":35}
]
## Using sort()
@mschulz
mschulz / gist:0118bf062db2c58d043de594bd299df7
Created September 23, 2024 04:28
JSON and datetime objects
import json
from datetime import datetime
def datetime_handler(x):
if isinstance(x, datetime):
return x.isoformat()
raise TypeError("Unknown type")
data = {'name': 'Alice', 'date': datetime.now()}
json_string = json.dumps(data, default=datetime_handler)