Skip to content

Instantly share code, notes, and snippets.

@augustogoulart
augustogoulart / bashrc.sh
Created June 27, 2017 16:23
Example of some django aliases
alias manage='python $(cat $(echo $VIRTUAL_ENV/.project))/manage.py'
alias test='python $(cat $(echo $VIRTUAL_ENV/.project))/manage.py test'
alias runserver='python $(cat $(echo $VIRTUAL_ENV/.project))/manage.py runserver'
alias makemigrations='python $(cat $(echo $VIRTUAL_ENV/.project))/manage.py makemigrations'
alias migrate='python $(cat $(echo $VIRTUAL_ENV/.project))/manage.py migrate'
alias shell='python $(cat $(echo $VIRTUAL_ENV/.project))/manage.py shell'
alias shellp='python $(cat $(echo $VIRTUAL_ENV/.project))/manage.py shell_plus'
@augustogoulart
augustogoulart / PowerList.py
Last active July 20, 2017 20:02
Extending python built-ins
"""
Simple example to explain how to extend built-in python objects
"""
class PowerList(list):
def search(self, name):
results = []
for obj in self:
if obj == name:
results.append(name)
return results
@augustogoulart
augustogoulart / LongNameDict.py
Created July 20, 2017 20:03
Extending Python's dictionary
class LongNameDict(dict):
def longest_value(self):
longest = None
for key, value in self.items():
if not longest or len(value) > len(longest):
longest = value
return longest
names = LongNameDict(first='josé', second='schwarzenegger')
@augustogoulart
augustogoulart / duck_typing.py
Created July 21, 2017 17:56
Example of Pythons duck typing
"""
Example do demonstrate Python's Duck Typing
In [1]: from collections import Container
In [2]: Container.__abstractmethods__
Out[3]: frozenset({'__contains__'})
"""
@augustogoulart
augustogoulart / dogs_only.py
Created July 21, 2017 21:10
Exemple of exceptions and type validation
"""
Exemple of exceptions and type validation
"""
class Dog:
def __init__(self, name):
self.name = name
@augustogoulart
augustogoulart / fstrings_performance.py
Last active July 22, 2017 15:56
Comparing f-string performance versus .format()
import timeit
def test_fstring():
for string in range(1000):
f'{string}'
def test_format():
for string in range(1000):
@augustogoulart
augustogoulart / hellofy.py
Created July 25, 2017 00:52
Simple example of how to use a @Property decorator
""""
Simple example of how to use a @property decorator
"""
class Hellofy:
def __init__(self, name, message):
self.name = name
self.message = message
@augustogoulart
augustogoulart / pillow_profiles.py
Created August 3, 2017 01:56
Simple profiler to understand Pillow memory usage working with images in memory.
from urllib.request import urlopen
from PIL import Image
from memory_profiler import profile
@profile
def run():
img = Image.open(urlopen("https://graviolastatics.s3.amazonaws.com/thumbnails/card.jpg"))
thumbnail = img.resize((300, 300), Image.ANTIALIAS)
thumbnail.show()
def show_request_data(request):
values = request.META.items()
html = []
for k, v in values:
html.append('<tr><td>%s</td><td>%s</td></tr>' % (k, v))
return HttpResponse('<table>%s</table>' % '\n'.join(html))
@augustogoulart
augustogoulart / webpack.config.js
Last active November 21, 2017 21:51
Simple webpack config
const webpack = require('webpack');
module.exports = {
entry: {
filename: './app.js',
},
output: {
filename: './build.js',
},