Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@LegoStormtroopr
LegoStormtroopr / boto_auth_requests.py
Created August 31, 2017 23:06
Connecting Django/Haystack to AWS ElasticSearch using IAM rotating credentials
import boto3
import os
import requests
from botocore.auth import SigV4Auth
from requests_aws4auth import AWS4Auth
from elasticsearch import RequestsHttpConnection
class AWSRequestsHttpConnection(RequestsHttpConnection):
@LegoStormtroopr
LegoStormtroopr / Example.py
Last active January 23, 2020 09:42
'FingerTabs' - Horizontal Text, Horizontal Tabs in PyQt This [trivial fingertab gist](https://gist.github.com/LegoStormtroopr/5075267) is released as Public Domain, but boy would it beswell if you could credit me, or tweet me [@LegoStormtoopr](http://www.twitter.com/legostormtroopr) to say thanks!
from PyQt4 import QtGui, QtCore
from FingerTabs import FingerTabWidget
import sys
app = QtGui.QApplication(sys.argv)
tabs = QtGui.QTabWidget()
tabs.setTabBar(FingerTabBarWidget(width=100,height=25))
digits = ['Thumb','Pointer','Rude','Ring','Pinky']
for i,d in enumerate(digits):
@LegoStormtroopr
LegoStormtroopr / Shift-Tab_friendly_QTextEdit.py
Last active June 13, 2019 14:37
A BackTab Friendly QTextEdit for PyQt. Allows a user to use Tab/Shift-Tab to indent and un-indent text in a QTextEdit - it also works in QPlaintextEdit A workable solution to this StackOverflow question: http://stackoverflow.com/questions/13579116/qtextedit-shift-tab-wrong-behaviour/18032320#18032320
import sys
from PyQt4 import QtCore, QtGui
class TabPlainTextEdit(QtGui.QTextEdit):
def __init__(self,parent):
QtGui.QTextEdit.__init__(self, parent)
def keyPressEvent(self, event):
# Shift + Tab is not the same as trying to catch a Shift modifier and a tab Key.
# Shift + Tab is a Backtab!!
@LegoStormtroopr
LegoStormtroopr / README.md
Created May 3, 2017 01:26
Handling AWS-ELB terminating a healthy django instance when accessed from an invalid hostname

Handling AWS-ELB terminating a healthy django instance when accessed from an invalid hostname

When spinning up a new service, Amazon Elastic LoadBalancer needs to check if the service is live and running. This check is done from an IP (from any IP in a private IP range) to the service, this is done by the ELB just doing a simple GET request to a specified path, with no host information - for example GET /heatbeat.

If this instance is a Django service, regardless of the page accessed, this call will fail as in a properly setup Django it is very unlikely that the IP will be in Django's settings.ALLOWED_HOSTS settings.

There are two ways around this, either:

a. Add every IP from every private IP range into your Django project's ALLOWED_HOSTS settings

# Lets run on the new infrastructure
sudo: false
language: python
python:
- "3.5"
install:
- pip install tox codecov coveralls
@LegoStormtroopr
LegoStormtroopr / fields.py
Last active September 22, 2016 23:07
Enable pickling of Django QuerySet with abstract intermediate model
import copy_reg
from django.db.models.fields import Field, _load_field, _empty
def _load_field_for_abstract(model, field_name):
return model._meta.get_field(field_name)
@LegoStormtroopr
LegoStormtroopr / README.md
Last active August 5, 2016 05:10
Generic autocompletes for django-autocomplete-light v3.x
def view_historic_reversion(request, iid, vid):
item = get_object_or_404(MDR._concept, pk=iid).item
if not user_can_view(request.user, item):
if request.user.is_anonymous():
return redirect(reverse('friendly_login') + '?next=%s' % request.path)
else:
raise PermissionDenied
old_item = default_revision_manager.get_for_object_reference(
item.__class__,
@LegoStormtroopr
LegoStormtroopr / pyqt_xml_editor.py
Created September 1, 2013 03:00
This is a basic XML editor that shows of how to do very simple XML validation inside of a PyQT QTextEdit field. All this does is check for document well-formed-ness, but using the LXML library it shouldn't be difficult to extend this to perform Schema or DOCTYPE based validation.
import sys
from lxml import etree
from PyQt4 import QtCore, QtGui
class editor(QtGui.QMainWindow):
def __init__(self):
super(editor, self).__init__()
self.text = QtGui.QTextEdit()
self.setCentralWidget(self.text)
self.text.textChanged.connect(self.validate)
@LegoStormtroopr
LegoStormtroopr / Example_use.txt
Created February 20, 2013 10:20
A demonstration of how to read Excel files using xlrd - http://www.lexicon.net/sjmachin/xlrd.htm
Using: http://www.abs.gov.au/ausstats/meisubs.NSF/log?openagent&634501.xls&6345.0&Time%20Series%20Spreadsheet&D22A58332C098EE7CA257B17000D3976&0&Dec%202012&20.02.2013&Latest
>>> data = abs.xlrdDemo('634501 (1).xls')
>>> print data.keys()
[u'Index', u'Data1', u'Inquiries']
>>> print data['Data1'][10,5]
64.7
>>> print data['Inquiries']
{(9, 1): u'I N Q U I R I E S', (3, 0): '', (8, 0): '', (2, 1): '', (5, 1): u'Table 1. Total Hourly Rates of Pay Excluding Bonuses: Sector, Original, Seasonally Adjusted and Trend', (4, 0): '', (9, 0): '', (8, 1): '', (11, 1): u'Referral Service on 1300 135 070 or Luci Burrage on Perth (08) 9360 5151.', (5, 0): '', (10, 0): '', (4, 1): u'6345.0 Wage Price Index, Australia', (1, 1): u'Time Series Workbook', (0, 0): '', (7, 1): '', (6, 0): '', (11, 0): '', (10, 1): u'For further information about these and related statistics, contact the National Information and', (1, 0): '', (0, 1): '', (7, 0): '', (6, 1): '', (3, 1): '', (2, 0): ''}