Skip to content

Instantly share code, notes, and snippets.

import re
ip = "1.2.3.4"
ipregex = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
test = ipregex.match(ip)
if test:
print("Valid")
else:
print("Not valid")
@augustogoulart
augustogoulart / post_share.py
Created May 4, 2017 20:36
Send the actual page's link by email
def post_share(request, post_id):
post = get_object_or_404(Post, id=post_id, status='published')
sent = False
if request.method == 'POST':
form = EmailPostForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
post_url = request.build_absolute_uri(post.get_absolute_url())
subject = '{} ({}) recommends you reading "{}"'.format(cd['name'], cd['email'], post.title)
@augustogoulart
augustogoulart / .html
Created May 4, 2017 18:10
Django pagination with bootstrap classes
<ul class="pagination">
{% if page.has_previous %}
<li><a href="?page={{ page.previous_page_number }}"><span>&laquo;</span></a></li>
{% else %}
<li class="disabled"><span>&laquo;</span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
@augustogoulart
augustogoulart / custom_manager_model.py
Created May 2, 2017 20:20
Simple model showing a custom manager usage
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get_queryset().filter(status='published')
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published')
)
@augustogoulart
augustogoulart / views.py
Created April 7, 2017 19:48
Show all the meta data in the request object
def home(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 / fizzbuzz.py
Created February 4, 2017 19:10
fizz-buzz using TDD
"""
Regras do FizzBuzz
1. Se a posição for multipla de 3: fizz
2. Se a posição for multipla de 5: buzz
3. Se a posição dor multipla de 3 e 5: fizzbuzz
4. Para qualquer outra posição fale o próprio numero.
"""
from functools import partial
""
Remove os numeros das páginas que estão fora de posição no texto Madame Bovary.
"""
import re
import fileinput
for linha in fileinput.input("Madame-Bovary-Gustave-Flaubert.txt", inplace=True):
print(re.sub("\d+", "", linha))