Skip to content

Instantly share code, notes, and snippets.

@Safrone
Safrone / models.py
Last active September 11, 2023 18:58
Pre-release Django polymorphic support for PR "Support select_related and prefetch_related for inherited models and custom queryset for child models #531"
from polymorphic.managers import PolymorphicManager
from polymorphic.models import PolymorphicModel
from .query import RelatedPolymorphicQuerySet, RelatedPolymorphicModel, RelatedPolymorphicManager
class ProjectManager(RelatedPolymorphicManager):
pass
class ProjectManager2(PolymorphicManager):
pass
@Safrone
Safrone / haversine.py
Created January 26, 2022 21:29
Django ordering by Haversine Distance for model with latitude and longitude
from django.db.models import CASCADE, SET_DEFAULT, SET_NULL, PROTECT, QuerySet, Q
from django.db.models.expressions import F
from django.db.models.functions import Cos, Sqrt, ASin
class ExampleModel(models.Model):
# Example model that could use this function
latitude = models.FloatField()
longitude = models.FloatField()
@Safrone
Safrone / gist:d3894caf03985cf9d3395c52405525f7
Created June 24, 2021 21:49
Elsys Chirpstack Decoder
// Decode decodes an array of bytes into an object.
// - fPort contains the LoRaWAN fPort number
// - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// The function must return an object, e.g. {"temperature": 22.5}
/*
@reference https://elsys.se/public/documents/Sensor_payload.pdf
Sensors Name Designation
------------------------------------------------------------
Sound Sensor ERS Sound
@Safrone
Safrone / utils.py
Last active June 10, 2023 09:11
django-yasg Polymorphic Serializer Inspector
### settings
"""
SWAGGER_SETTINGS = {
...
'DEFAULT_FIELD_INSPECTORS': [
'project.utils.PolymorphicSerializerInspector',
'drf_yasg.inspectors.CamelCaseJSONFilter',
'drf_yasg.inspectors.ReferencingSerializerInspector',
'drf_yasg.inspectors.RelatedFieldInspector',
'drf_yasg.inspectors.ChoiceFieldInspector',
@Safrone
Safrone / gist:800b44ce9632f68e7639d93ac0438076
Last active August 2, 2019 17:53
loraserver.io lora-app-server custom Javascript Cayenne LPP Decoder (With added tektelic data types)
// Decode decodes an array of bytes into an object.
// - fPort contains the LoRaWAN fPort number
// - bytes is an array of payload, e.g. [225, 230, 255, 0]
// The function must return an object, e.g. {"temperature": 22.5}
function Decode(fPort, bytes) {
/**
* @reference https://github.com/myDevicesIoT/cayenne-docs/blob/master/docs/LORA.md
*
* Added types for Tektelic Sensor
* Adapted for lora-app-server from https://gist.github.com/iPAS/e24970a91463a4a8177f9806d1ef14b8
@Safrone
Safrone / conditional_date_formatter.py
Last active March 13, 2018 21:18
A formatter for dates in matplotlib plots where the output format string changes based on a condition.
from matplotlib import ticker
from matplotlib.dates import DateFormatter, _from_ordinalf
class ConditionalDateFormatter(ticker.Formatter):
def __init__(self, fmt: str, fmt_conditional: str, date_condition: Callable[[datetime], bool], tz=None):
"""
A formatter for dates in matplotlib plots where the output format string changes based on a condition.
Args:
fmt (str): regular format string