Skip to content

Instantly share code, notes, and snippets.

@z4r
z4r / bb2.md
Last active March 13, 2019 00:55

Necromantic

  • 2 Flesh Golems (Block, Mighty Blow, Guard, Break Tackle)
  • 2 Wights
  • 1 Werewolf
  • 1 Ghoul
  • 5 Zombies
  • 3 RR -> Werewolf -> Ghoul

Amazon

import Foundation
struct TeamStats {
let elements: [TeamStat]
}
extension TeamStats: Decodable {
enum CodingKeys: String, CodingKey {
case overallteamstandings
}
@z4r
z4r / zrangebyscorestore.lua
Last active October 30, 2017 16:58
ZRANGEBYSCORESTORE
redis.call('ZUNIONSTORE', KEYS[1], 1, KEYS[2])
local lbound, ubound = ARGV[1], ARGV[2]
if lbound ~= '-inf' then
lbound = lbound:sub(1, 1) == '(' and lbound:sub(2) or '('..lbound
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', lbound)
end
if ubound ~= '+inf' then
ubound = ubound:sub(1, 1) == '(' and ubound:sub(2) or '('..ubound
redis.call('ZREMRANGEBYSCORE', KEYS[1], ubound, '+inf')
end
@z4r
z4r / csrfToken.js
Created June 21, 2013 17:09
ajax csrf for django
//require jquery.cookie.js
var csrftoken = $.cookie('csrftoken');
function csrfSafeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
crossDomain: false,
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
@z4r
z4r / middleware.py
Last active December 15, 2015 10:38
stopwatch core
from time import time
from django.conf import settings
from statsd import Connection, Timer
class StatsDStopWatchMiddleware(object):
def __init__(self):
connection = Connection(
host=getattr(settings, 'STATSD_HOST', None),
port=getattr(settings, 'STATSD_PORT', None)
@z4r
z4r / palindrome.markdown
Last active December 12, 2015 09:48
Doctest isn't palindrome

#Doctest isn't palindrome# A brief introduction about [Test-driven development][1] using [doctest][2] @ [Mobile World Congress 2013][3] in Barcelona.

##Abstract## Talking about Web development, requirements are fickle and ever-changing. The most trivial request could be the straw that breaks the camel's back. [Test-driven development][1] ( TDD ) is the best weapon in our hands; the [doctest][2] is the simplest tool to explore its efficiency. And let's face it, palindromes fascinate everyone.

###Issue != Requirements### Everyday a developer has to face the problem to understand his/her requestors (employers, line managers, collegues). Unfortunately the request isn't usually clear and comprehensive, because sometimes the problem is not clear to those who ask us to solve it. Language barriers can make the problem worse. However, an example is more enlightening than thousand words. And what if we could write these examples in a language more related to us?

@z4r
z4r / middleware.py
Created December 3, 2012 10:11
Cross Middleware
from django import http
from django.conf import settings
XS_SHARING_ALLOWED_ORIGINS = getattr(settings, 'XS_SHARING_ALLOWED_ORIGINS', '*')
XS_SHARING_ALLOWED_METHODS = getattr(settings, 'XS_SHARING_ALLOWED_METHODS', ['POST', 'GET', 'OPTIONS', 'PUT', 'DELETE', 'PATCH'])
XS_SHARING_ALLOWED_HEADERS = getattr(settings, 'XS_SHARING_ALLOWED_HEADERS', ['Content-Type', 'Date', 'Location', 'Server', 'Vary'])
class XsSharing(object):
def process_request(self, request):
if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META:
@z4r
z4r / DatePattern.markdown
Last active October 11, 2015 14:08
TDD and Unittest

#TDD and Unittest#

Ispirato al tutorial di Jason Diamond:

  • [Test-Driven Development in Python - Parte 1][1]
  • [Test-Driven Development in Python - Parte 2][2]

##Problema##

@z4r
z4r / testwflask.py
Created July 10, 2012 08:16
Testing clients with Flask
from multiprocessing import Process
from flask import Flask
import requests
import unittest2 as unittest
app = Flask(__name__)
@app.route('/hello/')
@app.route('/hello/<user>')
@z4r
z4r / redcache.py
Created April 18, 2012 10:47
Cache for urllib2 [Redis, FileSystem]
import hashlib
import StringIO
import httplib
import urllib2
import os
import errno
import time
from threading import RLock
cache_lock = RLock()