Skip to content

Instantly share code, notes, and snippets.

View babo's full-sized avatar

Attila Babo babo

View GitHub Profile
(ns pw.bot
(:gen-class :main true :prefix "-")
(:use [clojure.string :only (trim split-lines)]
pw.planetwars))
;; Helpers for your bot
(defn my-strongest-planet
[pw]
((first (sort-by #(> (:num-ships %1) (:num-ships %2))
(my-planets pw))) :planet-id))
@babo
babo / redirect_uri.py
Created October 29, 2010 14:57
Example code to show that localhost:8888/test redirects to localhost:8888/ after authentication.
#! /usr/bin/env python
import datetime
import tornado.auth
import tornado.httpserver
import tornado.web
class LoginHandler(tornado.web.RequestHandler):
def get_current_user(self):
return self.get_secure_cookie("user")
babo@attila% find /apps/ocaml -name ocamlbuild\*
/apps/ocaml/bin/ocamlbuild
/apps/ocaml/bin/ocamlbuild.byte
/apps/ocaml/lib/ocaml/ocamlbuild
/apps/ocaml/lib/ocaml/ocamlbuild/ocamlbuild.cmo
/apps/ocaml/lib/ocaml/ocamlbuild/ocamlbuild_executor.cmi
/apps/ocaml/lib/ocaml/ocamlbuild/ocamlbuild_executor.cmo
/apps/ocaml/lib/ocaml/ocamlbuild/ocamlbuild_executor.cmx
/apps/ocaml/lib/ocaml/ocamlbuild/ocamlbuild_executor.o
/apps/ocaml/lib/ocaml/ocamlbuild/ocamlbuild_pack.cmi
import riak
def f(n=100):
client = riak.client.RiakClient()
b = client.bucket('ize')
for i in xrange(n):
try:
k = b.new_binary(str(i), str(i))
x = k.store(w=3, dw=1)
@babo
babo / gist:3866694
Created October 10, 2012 16:24
python os.py difference
--- python2.6/os.py 2010-04-16 16:41:14.000000000 +0200
+++ /usr/lib/python2.6/os.py 2012-10-02 00:31:53.000000000 +0200
@@ -742,22 +742,3 @@
_make_statvfs_result)
except NameError: # statvfs_result may not exist
pass
-
-if not _exists("urandom"):
- def urandom(n):
- """urandom(n) -> str
val raw = io.Source.fromFile("/tmp/proba.json").mkString

Python Number Conversion Chart

From To Expression
@babo
babo / django_test.py
Created June 4, 2013 12:46
print raw sql queries from django
from django.db.backends import util, BaseDatabaseWrapper
class LoggingCursorWrapper(util.CursorDebugWrapper):
def execute(self, sql, *args):
print sql
return super(LoggingCursorWrapper, self).execute(sql, *args) if self.cursor.__class__ == util.CursorDebugWrapper else self.cursor.execute(sql, *args)
def create_wrapper_factory(old_cursor):
def cursor(self, *args, **kwargs):
return LoggingCursorWrapper(old_cursor(self, *args, **kwargs), self)
#!/usr/bin/env python
"""This is a demonstration of sharing file descriptors across processes.
It uses Tornado (need a recent post-2.0 version from github) and the
multiprocessing module (from python 2.6+). To run it, start one copy
of fdserver.py and one or more copies of testserver.py (in different
terminals, or backgrounded, etc). Fetch http://localhost:8000 and
you'll see the requests getting answered by different processes (it's
normal for several requests to go to the same process under light
load, but under heavier load it tends to even out).
package main
import (
"flag"
"fmt"
"net"
"net/rpc"
"strings"
"time"