Skip to content

Instantly share code, notes, and snippets.

View flavianmissi's full-sized avatar

Flavian Missi flavianmissi

View GitHub Profile
class CountAndSay:
def say(self, word):
numbers = {1:"one", 2:"two", 3:"three"}
say = ""
while len(word) > 0:
it = iter(word)
letter = it.next()
if letter is not " ":
say += numbers[word.count(letter)] + " " + letter + " "
word = word.replace(letter, "")
class Array
def sum
sum = 0
self.each do |s|
sum += s
end
sum
end
end
#coding: utf-8
class AuthorsName
def initialize
@ignored = ["da", "de", "do", "das", "dos"]
end
def surname=(surname)
@surname = surname
end
@flavianmissi
flavianmissi / xml_parsing.py
Created September 2, 2011 12:33
Parsing a xml string (or file) with Python's xml.etree module
xml_str = """<root>
<row><total>39</total><id_pais>BR </id_pais><id_estado>PE </id_estado>
<cidade>NADA</cidade><cidadetrad>NADA</cidadetrad><faturamento>2041343.61</faturamento>
</row>
<row><total>39</total><id_pais>BR </id_pais><id_estado>CE </id_estado><cidade>NADA</cidade>
<cidadetrad>NADA</cidadetrad><faturamento>2041343.61</faturamento>
</row>
<row><total>39</total><id_pais>BR </id_pais><id_estado>SC </id_estado><cidade>NADA</cidade>
<cidadetrad>NADA</cidadetrad><faturamento>2041343.61</faturamento>
</row>
@flavianmissi
flavianmissi / command_line_parser.py
Created September 3, 2011 17:42
Parsing command-line options
from argparse import ArgumentParser
parser = ArgumentParser(description="Process some integers")
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help="an integer for the accumulator")
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default find the max)')
@flavianmissi
flavianmissi / httplib_sample.py
Created September 8, 2011 12:25
httplib sample
import httplib
conn = httplib.HTTPConnection("www.google.com")
conn.request('GET', '/')
response = conn.getresponse()
response.status # 302
@flavianmissi
flavianmissi / create_view_sample.py
Created October 9, 2011 20:41
Django CreateView sample
from django.views.generic import CreateView
from library.forms import BookForm
class CreateBook(CreateView):
template_name = 'create_form.html'
success_url = '/books/'
form_class = BookForm
@flavianmissi
flavianmissi / django_update_view.py
Created October 10, 2011 12:37
Django UpdateView sample
#views.py
from django.views.generic import UpdateView
class UpdateBook(UpdateView):
model = Book
form_class = BookForm
template_name = 'create_form.html'
success_url = '/books/'
@flavianmissi
flavianmissi / testing_django_signals.py
Created November 6, 2011 01:56
Testing Django signals
from unittest import TestCase
from my_app.signals import foo_bar
class SignalsTestCase(TestCase):
def dummy_listener(self, sender, **kwargs):
self.times += 1
def test_deve_chamar_o_signal_foo_bar(self):
@flavianmissi
flavianmissi / django_request_factory.py
Created November 8, 2011 15:39
Testing with Django's RequestFactory
from django.test.client import RequestFactory
from django.utils.unittest import TestCase
from myapp.views import my_view
class MyViewTestCase(TestCase):
def test_my_view_should_return_200_status_code(self):
request = RequestFactory().get('url_for_the_view')