Skip to content

Instantly share code, notes, and snippets.

View Apkawa's full-sized avatar
🌴
On vacation

Apkawa Apkawa

🌴
On vacation
View GitHub Profile
@Apkawa
Apkawa / nginx.conf
Created July 15, 2011 16:08
Nginx hacks
Как написать location в nginx, который срабатывает, если два условия должны выполняться. Логично было бы написать так:
if ($http_user_agent ~* "Opera" AND $http_referer ~* "yandex.ru") {
bla bla bla
}
nginx не предусматривает в условиях логические операторы
Решение есть в виде хака:
set $a "";
@Apkawa
Apkawa / utils.py
Created July 19, 2011 09:27
Get python object from path
def get_by_path(path):
'''
get python object from path
>>> get_by_path('os.path.join')
<function join at ...>
'''
spl = path.split('.')
mod = None
for n, s in enumerate(spl, start=1):
try:
@Apkawa
Apkawa / daemon.py
Created July 26, 2011 07:38
Using soaplib as django views
'''
Simple usage as cherrypy daemon as example
'''
import soaplib
from soaplib.core.server import wsgi
from cherrypy.wsgiserver import CherryPyWSGIServer
HOST = "0.0.0.0"
PORT = 45124
@Apkawa
Apkawa / code_directive.py
Created August 14, 2011 19:54
extending rst2html converter
# -*- coding: utf-8 -*-
"""
The Pygments reStructuredText directive
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This fragment is a Docutils_ 0.5 directive that renders source code
(to HTML only, currently) via Pygments.
To use it, adjust the options below and copy the code into a module
that you import on initialization. The code then automatically
@Apkawa
Apkawa / utils.py
Created November 7, 2011 10:38
Xls utils
def make_response_for_xls(xls_content, xls_filename):
'''Make xls response by xls_content as binary sttring and xls_filename
from django.http import HttpResponse
response = HttpResponse(xls_content, mimetype="application/ms-excel")
response["Content-Disposition"] = u"attachment; filename=\"{0}\"".format(xls_filename)
return response
@Apkawa
Apkawa / views.py
Created November 24, 2011 09:33
Set many views as wizard
import wizard
order_wizard = wizard.Wizard('order_wizard)
@order_wizard.register(step=1, url='/basket/')
def basket(request):
...
@order_wizard.register(step=2, url='/step_1/')
def step_1(request):
@Apkawa
Apkawa / utils.py
Created December 6, 2011 13:19
KeyAttribytedDict
class KeyAttributeDict(dict):
'''
>>> kd = KeyAttributeDict({'a':1})
>>> kd['a']
1
>>> kd.a
1
'''
def __getattribute__(self, name):
try:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define OPEN_BRACKET_STATE 1
#define CLOSE_BRACKET_STATE 2
#define NUMBER_STATE 3
#define OPERATOR_STATE 4
char OPERATORS[] = {'/', '*', '-', '+'};
@Apkawa
Apkawa / gist:1780668
Created February 9, 2012 15:26 — forked from bentappin/gist:1003397
Generate checksum for a barcode/ean-13
def calc_ean_13_checksum(ean):
import math
sum = 0
for x, c in enumerate(str(ean)[::-1]):
if (x+1) % 2 == 0:
sum += int(c)
else:
sum += int(c) * 3
return int(math.ceil(sum / 10.0) * 10) - sum
@Apkawa
Apkawa / test_data.c
Created April 17, 2012 13:57
Генератор и чекер тестовых данных
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define MAX_INT 9999999
int randint(int from, int to) {
return (from + rand() % to);