Skip to content

Instantly share code, notes, and snippets.

View pauloxnet's full-sized avatar
🐍
https://www.paulox.net

Paolo Melchiorre pauloxnet

🐍
https://www.paulox.net
View GitHub Profile
@pauloxnet
pauloxnet / signals.py
Last active April 1, 2016 08:45 — forked from jefftriplett/signals.py
Django management command to trace / list all signals. My fork is just a few cleanups where CommandError wasn't referenced, etc.
# coding:utf-8
import gc
import inspect
import weakref
from django.core.management.base import BaseCommand, CommandError
from django.dispatch import Signal
from django.dispatch.weakref_backports import WeakMethod
from optparse import make_option
@arthuralvim
arthuralvim / permissions.py
Created September 10, 2014 14:27
Django Rest Framework - Only ajax requests are permitted.
from rest_framework.permissions import BasePermission
class IsAjaxPermission(BasePermission):
"""
Check is request is ajax.
"""
def has_object_permission(self, request, view, obj):
return request.is_ajax()
@lockie
lockie / trigram_migration.py
Last active January 14, 2018 11:45
A Django migration to add PostgreSQL trigram search, aka pg_trgm
# -*- coding: utf-8 -*-
# XXX this is actually useful for very special use cases, e.g. misspel suggestions:
# https://www.postgresql.org/docs/9.6/static/pgtrgm.html#AEN180626
from __future__ import unicode_literals
from django.db import migrations
from django.contrib.postgres.operations import TrigramExtension
class Migration(migrations.Migration):
operations = [
from django.db import models
from django.db.models import Avg
class Blog(models.Model):
name = models.CharField(max_length=100)
rating = models.DecimalField(max_digits=5, decimal_places=2, null=True)
def __str__(self):
return self.name
@diyan
diyan / uwsgi_json_access_log.md
Last active October 29, 2018 14:44
uWSGI logging configuration that could be used with central logging servers such as Logstash, Fluentd, etc.

uWSGI logging configuration that could be used with central logging servers such as Logstash, Fluentd, etc.

[uwsgi]
project = some_project
...

# file: prefix is required for req-logger
req-logger = file:/var/log/%(project)/%(project)_access.log
log-format = "method": "%(method)", "uri": "%(uri)", "proto": "%(proto)", "status": %(status), "referer": "%(referer)", "user_agent": "%(uagent)", "remote_addr": "%(addr)", "http_host": "%(host)", "pid": %(pid), "worker_id": %(wid), "core": %(core), "async_switches": %(switches), "io_errors": %(ioerr), "rq_size": %(cl), "rs_time_ms": %(msecs), "rs_size": %(size), "rs_header_size": %(hsize), "rs_header_count": %(headers)
import pandas as pd
import numpy as np
@bzamecnik
bzamecnik / process_memory_usage_osx.sh
Last active January 24, 2021 15:37
Process memory usage on OSX using GNU time
# On Linux we can use native GNU time, on OSX we have to install gnu-time
# manually, since native time doesn't measure memory.
# http://man7.org/linux/man-pages/man1/time.1.html
# http://braumeister.org/formula/gnu-time
$ brew install gnu-time
# can be called via gtime, since time is a bash command
# %M - Maximum resident set size of the process during its lifetime, in Kbytes.
@rajeshr188
rajeshr188 / models.py
Last active January 26, 2021 10:21
Creating ArrayField of custom basefield in Django
from moneyed import Money
from psycopg2.extras import register_composite
from psycopg2.extensions import register_adapter, adapt, AsIs
MoneyValue = register_composite(
'money_value',
connection.cursor().cursor,
globally=True
).type
@apollo13
apollo13 / chromelogger.py
Last active May 12, 2022 01:09
Django logging to Firefox/Chrome devtools
"""
License: MIT - https://opensource.org/licenses/MIT
ChromeLogger is a protocol which allows sending logging messages to the Browser.
This module implements simple support for Django. It consists of two components:
* `LoggingMiddleware` which is responsible for sending all log messages
associated with the request to the browser.
* `ChromeLoggerHandler` a python logging handler which collects all messages.
@marcgibbons
marcgibbons / concat_json_expression.py
Last active September 12, 2023 19:05
Expression to concatenate Django Postgres JSONField
from django.db.models.expressions import CombinedExpression, F, Value
from django.contrib.postgres.fields import JSONField
expression = CombinedExpression(
F('my_json_field'),
'||',
Value({'fizz': 'buzz'}, JSONField())
)