Skip to content

Instantly share code, notes, and snippets.

View joshourisman's full-sized avatar

Josh Ourisman joshourisman

View GitHub Profile
#
# Cookbook Name:: solr
# Recipe:: default
#
# Copyright 2012, Example Com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
try:
django_obj = self.django_model.objects.get(my_field=value)
except self.django_model.DoesNotExist:
try:
django_obj = self.django_model.objects.get(**{
'other_field1': other_value1,
'other_field2': other_value2,
'other_field3': other_value3,
'other_field4': other_value4,
})
for Model, relations in RELATIONSHIPS.items():
fields = [relations[relation]['field'] \
for relation in relations \
if relations[relation]['model'] is Constituent]
relationship_fields = {field: '{}_constituent_id'.format(field) \
for field in fields}
or_filters = [Q(**{'{}__isnull'.format(field): True}) \
for field in fields]
null_relations = reduce(lambda x, y: x | y, or_filters)
filters = null_relations
import sys
def equal(triplet):
return len(set(triplet)) == 1
def finished(world):
for i in range(len(world)):
if len(world[i:]) < 3:
(virtualenv)project :: (±branch) » ./manage.py shell ~/Projects/project
Python 2.7.3 (default, Aug 11 2012, 18:22:59)
[GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.60))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from exceptions import UnicodeEncodeError
>>> from app.module import RECORD_TYPES
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/josho/Projects/project/app/module.py", line 2, in <module>
@classmethod
def import_events_to_django(klas, events):
num_created = 0
num_updated = 0
num_skipped = 0
events_dict = {int(event['id']): event for event in events}
existing_django_events = klas.objects.in_bulk(events_dict.keys())
for key in existing_django_events:
>>> from django.conf import settings
>>> settings.DATABASES
{'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2', 'TEST_MIRROR': None, 'NAME': '', 'TEST_CHARSET': None, 'TIME_ZONE': 'UTC', 'TEST_COLLATION': None, 'OPTIONS': {}, 'HOST': 'localhost', 'USER': None, 'TEST_NAME': None, 'PASSWORD': None, 'PORT': None}}
<form method="POST" action="{% url accounts_admin_bulk_upload account.pk %}" enctype="multipart/form-data">
<input type="file" name="bulk" />
<input class="button" type="submit" name="" value="Bulk Upload Sub-accounts" />
</form>
filters = reduce(lambda x, y: x | y,
[Q(**{'account_name__istartswith': letter}) for
letter in letter_range])
return self.object.get_children().filter(filters)
@joshourisman
joshourisman / gist:1918906
Created February 26, 2012 20:45 — forked from gsiegman/gist:1918836
Rounding with string homework
#Given a variable, x, that stores
#the value of any decimal number,
#write Python code that prints out
#the nearest whole number to x.
#You can assume x is not negative.
# x = 3.14159 -> 3 (not 3.0)
# x = 27.63 -> 28 (not 28.0)