Skip to content

Instantly share code, notes, and snippets.

View cwurld's full-sized avatar

Chuck Martin cwurld

View GitHub Profile
@cwurld
cwurld / select2_for_selenium.py
Created July 7, 2020 17:07
Code to manipulate a Select2 widget from Selenium as if it were a normal Select
from collections import namedtuple
# Javascript scripts -----------------------------------------------------------------------------------------------
SELECT_BY_VALUE = \
'''
$(arguments[0]).val(arguments[1]);
$(arguments[0]).trigger('change');
'''
@cwurld
cwurld / django_model_formset.py
Last active April 22, 2022 01:56
Django model formset example
from django.forms.models import inlineformset_factory
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django import forms
from crispy_forms.helper import FormHelper
# The forms ---------------------------------------------------------------------------------------------------------
@cwurld
cwurld / xlsxwriter_cheatsheet.py
Last active November 6, 2019 06:47
xlsxwriter Cheat Sheet
# xlsxwriter Cheat Sheet
#
# xlsxwriter is an awesome package for writing Excel spreadsheets. It's got excellent documentation. And it's got
# way more functionality than I need. This cheat sheet contains the basic functionality that I use all the time.
#
# While it is possible to put formulas in the spreadsheet, I have had some problems with spreadsheets as email attachments
# when the spreadsheet has formulas and the email client is on mobile.
import xlsxwriter
@cwurld
cwurld / container_with_items.py
Last active October 19, 2017 18:57
Django Container with Items - (e.g. inlineformset_factory)
from django.forms.models import inlineformset_factory
from django.shortcuts import render
def container_with_items(request, container_id):
ItemFormSet = inlineformset_factory(
models.Container, models.Item, form=forms.InlineItemForm, fk_name='container', extra=1
)
# Use this to layout the Item forms
@cwurld
cwurld / gist:bbd138c953b416644ed66901d1e9196b
Last active July 18, 2017 10:07
Example of Python Dedupe Gazetteer
# Gazetteer ---------------------------------------------------------------------------------------------------------
def load_deduped_output_for_gazetteer(filename):
"""
Parse data from dedupe into:
1. Canonical dataset (no dupes)
2. messy data - all the dupes
3. markPairs.
:return:
@cwurld
cwurld / django_select2_mixin.py
Last active April 21, 2016 14:30
django and select2
'''
I have a model for customer intake. One of the fields is for tracking how the customer heard about us. I do
not want an endless proliferation of values, so I would like to use a multi-select. The problem is I also want the
user to add new choices as needed. It turns out this is possible with the Select2 widget:
https://select2.github.io/examples.html#tokenizer - check out this link to see how to setup your javascript.
It turns out this is a little tricky to implement. Below is a mixin you can use with a form to handle the field.
Also, if you are using bootstrap, you will want: https://fk.github.io/select2-bootstrap-css/
@cwurld
cwurld / gist:7db1a642c32ff548c184
Last active January 16, 2017 23:51
django selenium cheatsheet
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.support.select import Select
# DOES NOT WORK WITH selenium 2.5 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@cwurld
cwurld / quickstart.py
Created January 11, 2016 14:50
Google Calendar Python API with Freebusy
"""
Mostly the Google quickstart.py code with the "freebusy" service running.
Also shows how to make datetimes using pytz.
Follow these instructions to get access credentials:
https://developers.google.com/google-apps/calendar/quickstart/python
"""
@cwurld
cwurld / django_testcase_cheatsheet.py
Last active June 23, 2022 23:51
Django TestCase Cheatsheet
__author__ = 'chuck'
from urlparse import urlparse
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client
from django.conf import settings
from django.contrib.auth.models import User, Group
from django.core.cache import cache
@cwurld
cwurld / selenium_for_chosen.py
Last active January 1, 2016 17:16
Python code for handling the jquery Chosen plugin when testing using selenium.
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
class Chosen(object):
"""
For selenium testing of the jquery Chosen widget (v1.1.0 https://harvesthq.github.io/chosen/).