Skip to content

Instantly share code, notes, and snippets.

@jsatt
jsatt / fields.py
Created June 30, 2021 20:25
Django Encrypted Data field
import base64
import json
from typing import Any, Optional, Union
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from django.conf import settings
from django.db import models
@jsatt
jsatt / gist:d844173e001be3eafbbcdad41f4617a7
Created April 20, 2021 20:27
Dot-notation access to deeply nested dict
def deep_get(path, data, default=None):
"""
Uses a provided dot-notation path to retrieve values from a deeply nested dict
"""
if isinstance(path, str):
path = path.split('.')
obj = data
for key in path:
obj = obj.get(key)
if obj is None:
@jsatt
jsatt / deep_merge.py
Last active August 26, 2020 14:38 — forked from yatsu/deep_merge.py
Python dict deep merge
from copy import deepcopy
from functools import reduce
def merge_dicts(dict1: dict, dict2: dict) -> dict:
"""
Merges dicts.
"""
for key in dict2:
if key not in dict1 or not isinstance(dict1[key], dict):
@jsatt
jsatt / parsers.py
Created August 26, 2020 14:31
Django view to capture file from multipart or body POST
from io import BytesIO
from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.files.uploadhandler import FileUploadHandler
from rest_framework.parsers import DataAndFiles, FileUploadParser
class BodyUploadHandler(FileUploadHandler):
def new_file(self, *args, **kwargs):
@jsatt
jsatt / views.py
Created February 12, 2020 20:10
DRF flex field optimization
import coreapi
from django.core.exceptions import FieldDoesNotExist
from rest_flex_fields.utils import is_expanded
from rest_framework.exceptions import APIException
from rest_framework.schemas.inspectors import AutoSchema
class FlexFieldSchema(AutoSchema):
def __init__(self, view, *args, **kwargs):
super().__init__(*args, **kwargs)
@jsatt
jsatt / serializers.py
Created February 12, 2020 20:03
Catalog serizalier and field
from catalog import CatalogMember
from collections import OrderedDict
from itertools import chain
from rest_framework.exceptions import APIException
from rest_framework.fields import ReadOnlyField
from rest_framework.serializers import Serializer
class CatalogSerializer(Serializer):
@jsatt
jsatt / main.py
Last active February 9, 2020 00:44
CircuitPy neopixel color dial
import math
import board
import neopixel
import analogio
import time
from simpleio import map_range
NEOPIXEL_PIN = board.D5
NEOPIXEL_COUNT = 20
@jsatt
jsatt / python_coc-settings.json
Created September 16, 2019 14:22
project level coc-settings.json
{
"python.pythonPath": "~/.virtualenvs/pfgym/bin/python",
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.linting.mypyEnabled": true,
"python.jediEnabled": true
}
@jsatt
jsatt / async_tests.py
Last active December 14, 2023 17:16
Samples of various asyncio patterns to understand how it works.
import asyncio
from time import time
from httpx import AsyncClient
async def async_request(url: str):
async with AsyncClient() as client:
resp = await client.get(url)
result = len(resp.read())
@jsatt
jsatt / .vimlocal
Created May 10, 2019 15:56
auto enable virtualenv on vim boot
let g:ycm_server_python_interpreter = '/home/jsatt/.virtualenvs/pfapi/bin/python'
let $PATH .= ':/home/jsatt/.virtualenvs/pfapi/bin'