Skip to content

Instantly share code, notes, and snippets.

View fcasco's full-sized avatar

F fcasco

  • Greencode Software
  • Santos Lugares, Buenos Aires, Argentina
View GitHub Profile
@fcasco
fcasco / models.py
Created May 25, 2019 22:16
Extra SQL en Django (completo)
from django.db import models
from django.utils improt timezone
class Person(models.Model):
first_name = models.CharField(max_length=42)
last_name = models.CharField(max_length=42)
birth_date = models.DateField()
email = models.EmailField()
@property
@fcasco
fcasco / models.py
Created May 25, 2019 22:03
Extra SQL en Django. 4
Person.objects.extra(select={'email_provider': "(COALESCE(substring(email from '@(.*)$'), ''))"}).order_by('email_provider')
@fcasco
fcasco / models.py
Created May 25, 2019 21:35
Extra SQL en Django. 3
Person.objects.extra(select={'age': "(EXTRACT(year FROM age(birth_date)) :: int)"})\
.values('first_name', 'birth_date', 'age')
@fcasco
fcasco / models.py
Last active May 25, 2019 21:51
Extra SQL en Django. 2
from django.db import models
from django.utils improt timezone
class Person(models.Model):
first_name = models.CharField(max_length=42)
last_name = models.CharField(max_length=42)
birth_date = models.DateField()
@property
def age(self):
@fcasco
fcasco / models.py
Last active May 25, 2019 22:04
Extra SQL en Django. 1
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=42)
last_name = models.CharField(max_length=42)
birth_date = models.DateField()
email = models.EmailField()
function add_hotel_result(hotel, i, hotels) {
// Crea un nuevo elemento para mostrar los datos del hotel
var $new_hotel_result = $hotels_results.find('.template.hotel_result')
.clone()
.removeClass('template');
$new_hotel_result.find('.hotel_name').html(hotel.hotel_name);
function add_rate_option(rate) {
// Agrega el tipo de habitacion para el hotel
@fcasco
fcasco / dyntest.py
Created June 27, 2013 14:50
Dynamic test generation (just because I keep forgeting how to do it).
import unittest
class TestAutogenerated(unittest.TestCase):
values = [('a', 1), ('be', 20), ('cal', 3), ('dado', 2)]
@staticmethod
def make_test(s, n):
def new_test(self):
@fcasco
fcasco / avoid_twice.py
Created June 15, 2013 15:56
Context manager to avoid running a task twice
#!/usr/bin/env python
import unittest
class Avoid_twice():
tasks_filename = '/tmp/avoid_twice'
def __init__(self, task_name):
self.current_task = task_name
@fcasco
fcasco / fzbz.py
Last active December 18, 2015 05:59
FizzBuzz the Python way with a few magic tricks like decorators, unittest, doctest...
#!/bin/env python
# vim: set fileencoding=utf-8 :
"""
http://c2.com/cgi/wiki?FizzBuzzTest
The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job
candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment
is as follows::
"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of
def f(n):
l = []
for i in xrange(n):
l.append(i ** 2)
return l