Skip to content

Instantly share code, notes, and snippets.

View maestrofjp's full-sized avatar
🏠
Working from home

PJ Farrell maestrofjp

🏠
Working from home
View GitHub Profile
@maestrofjp
maestrofjp / convert.py
Last active February 8, 2021 03:54
Convert Microsoft LDAP ObjectGuid from base64 to Python UUID
import base64
import uuid
base64_object_guid = 'x+QSIh4FDE2aWxJ3Cpu3qw=='
object_guid = uuid.UUID(bytes_le=base64.b64decode(base64_object_guid))
print(oject_guid)
# UUID('2212e4c7-051e-4d0c-9a5b-12770a9bb7ab')
@maestrofjp
maestrofjp / migration.py
Created July 2, 2020 05:03
Django with Posgres TSVector with triggers
from django.db import migrations
import triggers
class Migration(migrations.Migration):
operations = [
migrations.RunSQL(
sql=triggers.SQL,
@maestrofjp
maestrofjp / example.py
Created May 22, 2020 01:28
Django - Arbitrary ordering of records using When/Case
from django.db import models
# Arbitrary order of Doohickey by id
arbitrary_order_ids = [10, 34, 67, 2, 99]
# Build "when" clauses for case statement
# Each id is assigned a number starting with 0 by the position they are in the iteration
when_clauses [
models.When(id=id, then=i) for i, id in enumeration(arbitrary_order_ids)
]
@maestrofjp
maestrofjp / fizzbuzz.py
Last active April 21, 2020 00:49
Fancy FizzBuzz
def fizzbuzz(nums, words):
if len(nums) != len(words):
raise 'Nums and words list must be the same length.'
pair_len = len(nums)
for i in range(1, 101):
output = []
for num, word in zip(nums, words):
if not i % num:
@maestrofjp
maestrofjp / pyuca_sort.py
Last active December 13, 2019 01:02
Python Sort List of Dictionaries with Accented Characters
from pyuca import Collator
c = Collator()
corpus = [
{'key': 'hotel', 'label': 'Hôtel'},
{'key': 'institutional', 'label': 'Institutionnel'},
{'key': 'library', 'label': 'Bibliothèque'},
{'key': 'office', 'label': 'Bureau'},
{'key': 'wall', 'label': 'Mur'},
def get(self, request, *args, **kwargs):
form_id = request.GET.get('uid', '')
if form_id and is_valid(form_id):
#get the form and check if it is valid. I restore the data to a variable called data
self.storage.reset()
self.storage.data = data
self.storage.current_step = self.steps.last
result = self.render(self.get_form())
else:
result = super(MySuperWizard, self).get(request, *args, **kwargs)
@maestrofjp
maestrofjp / gist:0f1f1ed3f582a65c2dc6
Last active August 29, 2015 14:12
ElasticSearch on Haystack unavailable
2014-12-24 08:18:49.210457+00:00 app web.1 -
Traceback (most recent call last):
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.40.0.34/net-2-40-0-34.cust.dsl.teletu.it/newrelic/hooks/framework_django.py", line 497, in wrapper return wrapped(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/decorators.py", line 99, in _wrapped_view response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/views/decorators/cache.py", line 52, in _wrapped_view_func response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 198, in inner return view(request, *args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/transaction.py", line 371, in
@maestrofjp
maestrofjp / gist:9955f587afabe27bedcf
Created December 23, 2014 20:32
gunicorn gevent with s3 boto error
014-12-23 20:12:38.711226+00:00 app web.1 - - Traceback (most recent call last): File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 137, in get_response response = response.render() File "/app/.heroku/python/lib/python2.7/site-packages/django/template/response.py", line 105, in render self.content = self.rendered_content File "/app/.heroku/python/lib/python2.7/site-packages/django/template/response.py", line 82, in rendered_content content = template.render(context) File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 140, in render return self._render(context) File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.40.0.34/net-2-40-0-34.cust.dsl.teletu.it/newrelic/api/function_trace.py", line 55, in dynamic_wrapper return wrapped(*args, **kwargs) File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 134, in _render return self.nodelist.render(context) File "/app/.heroku/python/lib/python2.7/site-pac
@maestrofjp
maestrofjp / gist:6453449
Created September 5, 2013 17:35
Anonymous AWS S3 bucket policy
{
"Version": "2008-10-17",
"Id": "Policy1378396267349",
"Statement": [
{
"Sid": "Stmt1378396007225",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},