Skip to content

Instantly share code, notes, and snippets.

View andresmachado's full-sized avatar

Andre Machado andresmachado

View GitHub Profile
@andresmachado
andresmachado / Random Quote Generator App.markdown
Last active September 1, 2015 16:55
Random Quote Generator App
@andresmachado
andresmachado / Weather APP by Andre Machado.markdown
Last active September 1, 2015 16:40
Weather APP by Andre Machado
import sys
def count_words(text_file):
file = open(textfile,'r+')
word_list = {}
for word in file.read().split():
if word not in word_list:
word_list[word] = 1
else:
class TenantFormMixin(object):
def get_context_data(self, *args, **kwargs):
context = super(TenantFormMixin, self).get_context_data(**kwargs)
context['tenant_form'] = TenantForm(prefix='tenant')
return context
def create_tenant_form(self, request, *args, **kwargs):
estate = get_object_or_404(Property, pk=self.kwargs['estate_id'])
self.tenant_form = TenantForm(self.request.POST, prefix='tenant')
if self.tenant_form.is_valid():
#!/usr/bin/python3
from urllib.request import Request, urlopen
busca = input('O que deseja buscar? :')
headers = {'User-Agent': 'Mozilla/5.0'}
url = Request('http://www.google.com.br/search?q=' + busca, headers=headers)
response = urlopen(url).read()
response = response.decode('utf8')
def format_result_color(self, obj):
if obj.media == 'A':
color = 'green'
elif obj.media == 'R':
color = 'red'
else:
color = 'orange'
return '<p style="color: {}">{}</p>'.format(color, obj.media)
format_result_color.allow_tags = True
num = input("Digite um número inteiro: ")
total = 0
i = 0
while i < len(num):
total += int(num[i])
i += 1
print(total)
@andresmachado
andresmachado / beautiful_idiomatic_python.md
Created December 27, 2016 23:57 — forked from JeffPaine/beautiful_idiomatic_python.md
Transforming Code into Beautiful, Idiomatic Python: notes from Raymond Hettinger's talk at pycon US 2013. The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Transforming Code into Beautiful, Idiomatic Python

Notes from Raymond Hettinger's talk at pycon US 2013 video, slides.

The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Looping over a range of numbers

for i in [0, 1, 2, 3, 4, 5]:
@andresmachado
andresmachado / serializers.py
Created April 1, 2017 02:36
nested_serializer.py
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
fields = (
'field1',
'field2'
)
class CategorySerializer(serializers.ModelSerializer):
class OccurencesInRadiusDistance(generics.ListAPIView):
"""API endpoint that returns all objects in a radius given a set of coordinates in url_params."""
serializer_class = MyCustomSerializer
permissions_classes = (permissions.IsAuthenticatedOrReadOnly, )
def get_queryset(self):
"""This queryset returns all objects in a specified radius distance. We use PostGIS to create POINT objects and create the query.
How to use postGIS on Django: https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/postgis/