Skip to content

Instantly share code, notes, and snippets.

@raiderrobert
raiderrobert / forms.py
Last active February 17, 2018 03:58
DEP Draft - Django Forms API Rewrite
"""
Proposed New Forms API, not currently possible in Django Forms API (as of 2.0)
"""
from django.forms import ModelForm, ModelFormListField
from .models import Author, Book
# Make this work like serailization relationships in Django Rest Framework
# http://www.django-rest-framework.org/api-guide/relations/
# or relationships in WTForms
# http://wtforms-alchemy.readthedocs.io/en/latest/relationships.html#one-to-many-relations
"""
The world is a horrible place.
Let's make it more pythonic
many ideas borrowed from Raymond Hettinger and Python Anti-Patterns
"""
# timeit is a Python built in that can test small snippets very quickly
# by default it'll run the code snippet 1 million times
# -*- coding: utf-8 -*-
"""
Created on Wed May 24 14:40:50 2017
Requirements:
1. Read URL
2. Loop function to crawl through the following pages after the first page in the data catalogue
2. For every data node, go inside and crawl the resource links
3. Test each link for 404 errors
4. Return result into a pandas dataframe
# -*- coding: utf-8 -*-
"""
Created on Wed May 24 14:40:50 2017
Requirements:
1. Read URL
2. Loop function to crawl through the following pages after the first page in the data catalogue
2. For every data node, go inside and crawl the resource links
3. Test each link for 404 errors
4. Return result into a pandas dataframe
"""
The world is a horrible place.
Let's make it better about knowing how sharp some knives are.
from Raymond Hettinger and Python Anti-Patterns
"""
@raiderrobert
raiderrobert / area.py
Created May 17, 2017 02:11
Demo Area function
def area (height, width, length):
return ((height * length) * 2) + ((height * width) * 2)
@raiderrobert
raiderrobert / views.py
Last active April 23, 2017 23:10
M2M delete Example with GET
class RemoveStudent(DeleteView):
model = Course
template_name = 'course_detail.html'
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
user = User.objects.get(pk=self.kwargs['user_pk'])
self.object.students.remove(user)
return HttpResponseRedirect(reverse_lazy('courses.detail', kwargs={'pk': self.object.pk}))
def need_approval(self):
queryset = self
queryset = queryset.annotate(
conflict_group=Concat(
'employee_id', Value('|'), 'date_applied',
output_field=models.CharField()
),
paycode_count=Count('codes')
)
conflicts = queryset.values('conflict_group').annotate(
class OrganizationSerializer(CreatedWithEpochSerializer):
id = serializers.CharField(source='client_generated_id')
address = UsAddressSerializer(required=False)
role = serializers.CharField(read_only=True, source='Membership__role')
class Meta:
model = Organization
fields = ('id', 'role', 'name', 'url', 'archived', 'created', 'address')
@raiderrobert
raiderrobert / version_checker.py
Last active January 10, 2017 01:34
Version checker
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import click
from netmiko import ConnectHandler
def normalize(v):