Skip to content

Instantly share code, notes, and snippets.

View igorsobreira's full-sized avatar

Igor Sobreira igorsobreira

View GitHub Profile
@igorsobreira
igorsobreira / gist:323001
Created March 5, 2010 18:38
An way to add methods to an instance in Python
'''
An way to add methods to an instance.
>>> class Person(object):
... def __init__(self, name):
... self.name = name
...
>>> def upper_name(self):
... return self.name.upper()
...
@igorsobreira
igorsobreira / formatters.py
Created January 29, 2010 02:40
Django form field for Brazilian Real (R$)
from django.template.defaultfilters import floatformat
from django.contrib.humanize.templatetags.humanize import intcomma
from django.utils.encoding import force_unicode
def decimal_to_real(value, precision=2):
'''
Receives a Decimal instance and returns a string formatted as brazilian Real currency:
12,234.00. Without the "R$".
'''
value = floatformat(value, precision)
@igorsobreira
igorsobreira / gist:286051
Created January 25, 2010 17:32
wondering how dynamic fields could be added to a model...
# wondering how dynamic fields could be added to a model...
import copy
from django.db import models
class DynamicModel(models.base.ModelBase):
def _prepare(cls):
# calling a function or method to return the extra fields to
# add to the model