This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sentinel: | |
def __init__(self, name): | |
self.name = name | |
def __repr__(self): | |
return f"<{self.name}>" | |
MISSING = Sentinel("MISSING") | |
UNSET = Sentinel("UNSET") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from jsonschema import validate | |
schema = { | |
"type": "object", | |
"properties": { | |
"name": {"type": "string"}, | |
"age": {"type": "integer"} | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dict1 = [ | |
{"Name":"Karl", | |
"Age":25}, | |
{"Name":"Lary", | |
"Age":39}, | |
{"Name":"Nina", | |
"Age":35} | |
] | |
## Using sort() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder