Skip to content

Instantly share code, notes, and snippets.

View allynt's full-sized avatar

allynt

  • Edinburgh, Scotland, UK
View GitHub Profile
def bulk_update_or_create(model_class, model_data):
"""
Performs update_or_create in bulk (w/ only 3 db hits)
Parameters
----------
model_class : django.db.models.Model
model to update_or_create
model_data : list
data to update/create. Example: [{'field1': 'value', 'field2': 'value'}, ...]
import fnmatch
import os
from functools import reduce
from itertools import islice
from math import floor
from io import BytesIO
import numpy as np
import pandas as pd
import geopandas as gpd
#!/bin/sh
# wrapper script for toggling fn keys on k380 keyboard in Linux (Xubuntu 19.04)
# `toggle_k380_functions_keys.sh` is bound to <alt>-<esc> in "keyboard -> application shortcuts"
# and I ensured that `/usr/local/bin/k380_conf` is configured to run w/ sudo's NOPASSWD option
# useful links:
# * https://askubuntu.com/questions/699138/logitech-k380-bluetooth-keyboard-make-function-keys-default
# * https://github.com/jergusg/k380-function-keys-conf
from jsonpath import JSONPath
from rest_framework.permissions import BasePermission
from rest_framework.exceptions import PermissionDenied
def TokenMatchesJSONPath(get_jsonpath_expression):
"""
This fn is a factory that returns a _dynamic_ DRF Permission based on a JSONPath Expression.
Only a request w/ a token that matches that expression is granted permission.
{"$schema":"http://json-schema.org/draft-07/schema#","title":"DataSource Schema","description":"JSONSchema defining DataSource metadata","definitions":{"component_object":{"type":"object","properties":{"name":{"type":"string"},"props":{"type":"object"}},"required":["name"]},"orbis_layer_info":{"description":"orbis-specific information to associate w/ the data layer","type":"object","properties":{"orbis":{"type":"object","properties":{"map_component":{"oneOf":[{"type":"array","items":[{"$ref":"#/definitions/component_object"}]},{"$ref":"#/definitions/component_object"}]},"sidebar_component":{"oneOf":[{"type":"array","items":[{"$ref":"#/definitions/component_object"}]},{"$ref":"#/definitions/component_object"}]},"layer":{"oneOf":[{"type":"array","items":[{"$ref":"#/definitions/component_object"}]},{"$ref":"#/definitions/component_object"}]},"categories":{"type":"object","properties":{"name":{"type":"string"},"child":{"type":"object","$ref":"#/definitions/orbis_layer_info/properties/orbis/properties/categories"}
import itertools
# must be zero-based
CATEGORIES_ORDER = {
"SSSI": 0,
"SAC": 1,
"SPA": 2,
"RAMSAR": 3,
"GCR": 4,
"LNR": 5,
@allynt
allynt / namespace_properties.py
Last active November 1, 2022 16:30
update metadata property names
import click
import json
@click.command()
@click.option(
"--file", required=True, type=click.Path(dir_okay=False, exists=True)
)
def namespace_properties(file):
input_fp = open(file, "r")
data = json.load(input_fp)