<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Sevkin's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 21:19:43 GMT</pubDate>
    <description>DZone Snippets: Sevkin's Code Snippets</description>
    <item>
      <title>Pattern Matching based WSGI-enabled URL routing tool.</title>
      <link>http://snippets.dzone.com/posts/show/5620</link>
      <description>Actual version on http://pypi.python.org/pypi/decoroute&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;easy_install decoroute&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import decoroute&lt;br /&gt;    &lt;br /&gt;app = decoroute.App()&lt;br /&gt;    &lt;br /&gt;def render_response(status = '200 OK', **kw):&lt;br /&gt;    # try your favorite templating engine here&lt;br /&gt;    return status, [('Content-Type', 'text/plain')], [str(kw)]&lt;br /&gt;&lt;br /&gt;def redirect_to(env, endpoint, **kw):&lt;br /&gt;    return '302 FOUND', [('Content-Type', 'text/plain'), \&lt;br /&gt;    ('Location', '%s://%s%s' % (env['wsgi.url_scheme'], env['HTTP_HOST'], \&lt;br /&gt;    env['decoroute.app'].url_for(endpoint, **kw)))], ['']&lt;br /&gt;    &lt;br /&gt;@app.expose('/node', id = '1')&lt;br /&gt;@app.expose('/node/&lt;id:\d+&gt;')&lt;br /&gt;def node(env, id):&lt;br /&gt;    return render_response(id = id)&lt;br /&gt;    &lt;br /&gt;@app.expose('/url_for')&lt;br /&gt;def url_for(env):&lt;br /&gt;    return render_response( \&lt;br /&gt;        url = env['decoroute.app'].url_for(node, id = 666))&lt;br /&gt;    &lt;br /&gt;@app.expose('/302')&lt;br /&gt;def found(env):&lt;br /&gt;    return redirect_to(env, not_found)&lt;br /&gt;    &lt;br /&gt;@app.expose('/404')&lt;br /&gt;def not_found(env):&lt;br /&gt;    raise decoroute.NotFound()&lt;br /&gt;    &lt;br /&gt;@app.not_found()&lt;br /&gt;def not_found_handler(env):&lt;br /&gt;    return render_response('404 NF', url = env['PATH_INFO'])&lt;br /&gt;    &lt;br /&gt;from wsgiref.simple_server import make_server&lt;br /&gt;    &lt;br /&gt;make_server('', 5555, app).serve_forever()&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Complete source &lt; 100 lines of code:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;# vim:ts=4:sw=4:et&lt;br /&gt;# -*- coding: utf-8 -*-&lt;br /&gt;# $Id: decoroute.py,v 6eb37aeee5cd 2008/06/09 07:38:26 vsevolod $&lt;br /&gt;#&lt;br /&gt;# Pattern Matching based WSGI-enabled URL routing tool.&lt;br /&gt;# Actual version on http://pypi.python.org/pypi/decoroute&lt;br /&gt;# (C) 2008 by Vsevolod S. Balashov &lt;vsevolod@balashov.name&gt;&lt;br /&gt;# under terms of GNU LGPL v2.1 http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt&lt;br /&gt;&lt;br /&gt;__author__ = "Vsevolod Balashov"&lt;br /&gt;__email__ = "vsevolod at balashov dot name"&lt;br /&gt;&lt;br /&gt;import re&lt;br /&gt;from sets import ImmutableSet&lt;br /&gt;import wsgistraw&lt;br /&gt;&lt;br /&gt;__all__ = ['NotFound', 'App']&lt;br /&gt;&lt;br /&gt;class NotFound(Exception):&lt;br /&gt;    pass&lt;br /&gt;&lt;br /&gt;_pattern_re = re.compile(r'''&lt;br /&gt;    ([^&lt;]+)                     # static rule data&lt;br /&gt;    (?:&lt;&lt;br /&gt;        ([a-zA-Z][a-zA-Z0-9_]*) # variable name&lt;br /&gt;        \:&lt;br /&gt;        ([^&gt;]+)                 # regexp constraint&lt;br /&gt;    &gt;)?&lt;br /&gt;''', re.VERBOSE)&lt;br /&gt;&lt;br /&gt;def pattern2regexp(pattern, f, s = lambda x: re.escape(x)):&lt;br /&gt;    def parser():&lt;br /&gt;        for t in _pattern_re.findall(pattern):&lt;br /&gt;            yield s(t[0])&lt;br /&gt;            if t[1] != '':&lt;br /&gt;                yield f(t[1], t[2])&lt;br /&gt;    return parser()&lt;br /&gt;&lt;br /&gt;make_url_for = lambda p: ''.join(pattern2regexp(p, lambda v, r: '%%(%s)s' % v, lambda s: s))&lt;br /&gt;make_variables = lambda p: filter(lambda x: x, pattern2regexp(p, lambda v, r: v, lambda s: None))&lt;br /&gt;make_pattern = lambda p: r''.join(pattern2regexp(p, lambda v, r: r'(?P&lt;%s&gt;%s)' % (v, r)))&lt;br /&gt;make_selector_fragment = lambda p: r''.join(pattern2regexp(p, lambda v, r: r'(?:%s)' % r))&lt;br /&gt;make_selector = lambda i: re.compile(r'(^%s$)' % r'$)|(^'.join(map(make_selector_fragment, i)))&lt;br /&gt;&lt;br /&gt;class UrlMap(object):&lt;br /&gt;    def __init__(self):&lt;br /&gt;        self._endpoints = {}&lt;br /&gt;        self._patterns = {}&lt;br /&gt;        self._pattern_selector = make_selector(self._patterns.iterkeys())&lt;br /&gt;    &lt;br /&gt;    def add(self, pattern, endpoint, **kw):&lt;br /&gt;        if self._patterns.has_key(pattern):&lt;br /&gt;            raise Exception('duplicate pattern', pattern)&lt;br /&gt;        self._endpoints[(endpoint, ImmutableSet(make_variables(pattern)))] = make_url_for(pattern)&lt;br /&gt;        self._patterns[pattern] = (re.compile(make_pattern(pattern)), endpoint, kw)&lt;br /&gt;        self._pattern_selector = make_selector(self._patterns.iterkeys())&lt;br /&gt;    &lt;br /&gt;    def route(self, url):&lt;br /&gt;        try:&lt;br /&gt;            p = self._patterns.values()[re.match(self._pattern_selector, url).lastindex - 1]&lt;br /&gt;            d = re.match(p[0], url).groupdict().copy()&lt;br /&gt;            d.update(p[2])&lt;br /&gt;            return p[1], d&lt;br /&gt;        except:&lt;br /&gt;            raise NotFound('route not found', url)&lt;br /&gt;    &lt;br /&gt;    def url_for(self, endpoint, **kw):&lt;br /&gt;        return self._endpoints[(endpoint, ImmutableSet(kw.keys()))] % kw&lt;br /&gt;&lt;br /&gt;class App(object):&lt;br /&gt;    def __init__(self, key = 'decoroute.app'):&lt;br /&gt;        self._key = key&lt;br /&gt;        self._map = UrlMap()&lt;br /&gt;        self._not_found = lambda e: ('404 NOT FOUND', [("Content-Type", "text/plain")], [''])&lt;br /&gt;    &lt;br /&gt;    @wsgistraw.app&lt;br /&gt;    def __call__(self, env):&lt;br /&gt;        try:&lt;br /&gt;            env[self._key] = self&lt;br /&gt;            endpoint, kw = self._map.route(env['PATH_INFO'])&lt;br /&gt;            return endpoint(env, **kw)&lt;br /&gt;        except NotFound:&lt;br /&gt;            return self._not_found(env)&lt;br /&gt;    &lt;br /&gt;    def expose(self, pattern, **kw):&lt;br /&gt;        def decorate(f):&lt;br /&gt;            self._map.add(pattern, f, **kw)&lt;br /&gt;            return f&lt;br /&gt;        return decorate&lt;br /&gt;    &lt;br /&gt;    def not_found(self):&lt;br /&gt;        def decorate(f):&lt;br /&gt;            self._not_found = f&lt;br /&gt;            return f&lt;br /&gt;        return decorate&lt;br /&gt;    &lt;br /&gt;    def url_for(self, endpoint, **kw):&lt;br /&gt;        return self._map.url_for(endpoint, **kw)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 09 Jun 2008 07:10:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5620</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
    <item>
      <title>Thread Pool and Threaded Python decorators</title>
      <link>http://snippets.dzone.com/posts/show/4425</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;# -*- coding: utf-8 -*-&lt;br /&gt;# vim:ts=4:sw=4:et&lt;br /&gt;# (c) 2007 under terms of LGPL v 2.1&lt;br /&gt;# by Vsevolod S. Balashov &lt;vsevolod@balashov.name&gt; &lt;br /&gt;&lt;br /&gt;from threading import Thread&lt;br /&gt;&lt;br /&gt;def threaded(func):&lt;br /&gt;    def proxy(*args, **kwargs):&lt;br /&gt;        thread = Thread(target=func, args=args, kwargs=kwargs)&lt;br /&gt;        thread.start()&lt;br /&gt;        return thread&lt;br /&gt;    return proxy&lt;br /&gt;&lt;br /&gt;from Queue import Queue&lt;br /&gt;&lt;br /&gt;class Pool(Queue):&lt;br /&gt;    def __init__(self, maxsize):&lt;br /&gt;        assert maxsize &gt; 0, 'maxsize &gt; 0 required for Pool class'&lt;br /&gt;        Queue.__init__(self, maxsize)&lt;br /&gt;        for i in range(maxsize):&lt;br /&gt;            thread = Thread(target = self._worker)&lt;br /&gt;            thread.setDaemon(True)&lt;br /&gt;            thread.start()&lt;br /&gt;&lt;br /&gt;    def _worker(self):&lt;br /&gt;        while True:&lt;br /&gt;            try:&lt;br /&gt;                func, args, kwargs = self.get()&lt;br /&gt;                func(*args, **kwargs)&lt;br /&gt;            except:&lt;br /&gt;                self.task_done()&lt;br /&gt;                self.join()&lt;br /&gt;                raise&lt;br /&gt;            self.task_done()&lt;br /&gt;&lt;br /&gt;    def addJob(self, func, *args, **kwargs):&lt;br /&gt;        self.put((func, args, kwargs))&lt;br /&gt;&lt;br /&gt;    def __enter__(self):&lt;br /&gt;        pass&lt;br /&gt;&lt;br /&gt;    def __exit__(self, exc_type, exc_value, traceback):&lt;br /&gt;        self.join()&lt;br /&gt;&lt;br /&gt;def threadpool(pool):&lt;br /&gt;    assert pool.__class__ == Pool, 'threadpool decorator require a Pool object'&lt;br /&gt;    def decorator(func):&lt;br /&gt;        def proxy(*args, **kwargs):&lt;br /&gt;            pool.put((func, args, kwargs))&lt;br /&gt;            return pool&lt;br /&gt;        return proxy&lt;br /&gt;    return decorator&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;example usage&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from threadpool import *&lt;br /&gt;&lt;br /&gt;from time import sleep&lt;br /&gt;from random import random&lt;br /&gt;&lt;br /&gt;pool = Pool(3)&lt;br /&gt;    &lt;br /&gt;@threadpool(pool)&lt;br /&gt;def test_threadpool(i):&lt;br /&gt;    print 'threadpool %i enter' % i&lt;br /&gt;    sleep(random())&lt;br /&gt;    print 'threadpool %i exit' % i&lt;br /&gt;&lt;br /&gt;print 'threadpool example'&lt;br /&gt;for i in range(6):&lt;br /&gt;    test_threadpool(i)&lt;br /&gt;pool.join()&lt;br /&gt;print 'done'&lt;br /&gt;print ''&lt;br /&gt;&lt;br /&gt;@threaded&lt;br /&gt;def test_threaded(i):&lt;br /&gt;    print 'threaded %i enter' % i&lt;br /&gt;    sleep(random())&lt;br /&gt;    print 'threaded %i exit' % i&lt;br /&gt;&lt;br /&gt;print 'threaded example'&lt;br /&gt;for i in range(5):&lt;br /&gt;    test_threaded(i)&lt;br /&gt;print 'done'&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;result output&lt;br /&gt;&lt;br /&gt;threadpool example&lt;br /&gt;threadpool 0 enter&lt;br /&gt;threadpool 1 enter&lt;br /&gt;threadpool 2 enter&lt;br /&gt;threadpool 0 exit&lt;br /&gt;threadpool 3 enter&lt;br /&gt;threadpool 1 exit&lt;br /&gt;threadpool 4 enter&lt;br /&gt;threadpool 2 exit&lt;br /&gt;threadpool 5 enter&lt;br /&gt;threadpool 3 exit&lt;br /&gt;threadpool 5 exit&lt;br /&gt;threadpool 4 exit&lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;threaded example&lt;br /&gt;done&lt;br /&gt;threaded 0 enter&lt;br /&gt;threaded 1 enter&lt;br /&gt;threaded 2 enter&lt;br /&gt;threaded 3 enter&lt;br /&gt;threaded 4 enter&lt;br /&gt;threaded 1 exit&lt;br /&gt;threaded 2 exit&lt;br /&gt;threaded 3 exit&lt;br /&gt;threaded 0 exit&lt;br /&gt;threaded 4 exit</description>
      <pubDate>Thu, 16 Aug 2007 12:55:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4425</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
    <item>
      <title>Storm ORM by Canonical in WSGI enabled applications</title>
      <link>http://snippets.dzone.com/posts/show/4408</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;# vim:ts=4:sw=4:et&lt;br /&gt;# (c) 2007 Vsevolod Balashov &lt;vsevolod@balashov.name&gt; under terms of LGPL 2.1&lt;br /&gt;# use Storm ORM &lt;https://storm.canonical.com&gt; in WSGI enabled applications&lt;br /&gt;#&lt;br /&gt;# actual version on http://pypi.python.org/pypi/middlestorm&lt;br /&gt;&lt;br /&gt;"""WSGI middleware for Storm.&lt;br /&gt;&lt;br /&gt;This is the database access inteface for WSGI enabled (PEP 333) web applications.&lt;br /&gt;&lt;br /&gt;Pylons framework example:&lt;br /&gt;&lt;br /&gt;in wsgiapp.py&lt;br /&gt;&lt;br /&gt;from storm.database import create_database&lt;br /&gt;from middlestorm import MiddleStorm&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;# CUSTOM MIDDLEWARE HERE&lt;br /&gt;app = MiddleStorm(app, create_database(config['app_conf']['sqlalchemy.default.uri']))&lt;br /&gt;&lt;br /&gt;in controller:&lt;br /&gt;&lt;br /&gt;class DemoController(BaseController):&lt;br /&gt;    def index(self):&lt;br /&gt;        store = request.environ['storm.store']&lt;br /&gt;        ...&lt;br /&gt;"""&lt;br /&gt;&lt;br /&gt;from storm.database import Database&lt;br /&gt;from storm.store import Store&lt;br /&gt;from threading import local&lt;br /&gt;&lt;br /&gt;__all__ = ["MiddleStorm"]&lt;br /&gt;__author__ = "Vsevolod Balashov &lt;http://vsevolod.balashov.name&gt;"&lt;br /&gt;__version__ = "0.1"&lt;br /&gt;&lt;br /&gt;class SingleConn(Database):&lt;br /&gt;    """Database proxy class.&lt;br /&gt;    Share single database connection between all Store object instances in threads.&lt;br /&gt;    If you have readonly database or not use transactions why not?&lt;br /&gt;    """&lt;br /&gt;    def __init__(self, database):&lt;br /&gt;        self._database = database&lt;br /&gt;        self._connection = database.connect()&lt;br /&gt;    def connect(self):&lt;br /&gt;        return self._connection&lt;br /&gt;&lt;br /&gt;class MiddleStorm(object):&lt;br /&gt;    """WSGI middleware.&lt;br /&gt;    Add Store object instance in environ['storm.store']. Each thread contains own instance.&lt;br /&gt;    """&lt;br /&gt;&lt;br /&gt;    def __init__(self, app, database, single = False):&lt;br /&gt;        """Create WSGI middleware.&lt;br /&gt;        @param app: up level application or middleware.&lt;br /&gt;        @param database: instance of Database returned create_database.&lt;br /&gt;        @param: single: use single database connection in all threads. &lt;br /&gt;        """&lt;br /&gt;        assert isinstance(database, Database), \&lt;br /&gt;            'database must be subclass of storm.database.Database'&lt;br /&gt;        if single:&lt;br /&gt;            self._database = SingleConn(database)&lt;br /&gt;        else:&lt;br /&gt;            self._database = database&lt;br /&gt;        self._app = app&lt;br /&gt;        self._local = local()&lt;br /&gt;&lt;br /&gt;    def __call__(self, environ, start_response):&lt;br /&gt;        try:&lt;br /&gt;            environ['storm.store']  = self._local.store&lt;br /&gt;        except AttributeError:&lt;br /&gt;            environ['storm.store']  = \&lt;br /&gt;                self._local.__dict__.setdefault('store', Store(self._database))&lt;br /&gt;        return self._app(environ, start_response)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 10 Aug 2007 11:39:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4408</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
    <item>
      <title>Ruby port of range2cidr. Convert IP ranges to set of CIDR.</title>
      <link>http://snippets.dzone.com/posts/show/3798</link>
      <description>See rfc1878 for more detail about CIDR&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;# -*- coding: utf-8 -*-&lt;br /&gt;# $Hg: range_cidr.rb,v af9566d89389 2007-04-12 20:28 +0400 $&lt;br /&gt;# (C) 2007 under terms of LGPL v2.1&lt;br /&gt;# by Vsevolod S. Balashov &lt;vsevolod@balashov.name&gt;&lt;br /&gt;#&lt;br /&gt;# backported from perl code&lt;br /&gt;# http://www.irbs.net/internet/postfix/0401/att-3032/cidr_range.pl.gz&lt;br /&gt;&lt;br /&gt;def range_cidr(first, last, &amp;block)&lt;br /&gt;  if first &lt; last&lt;br /&gt;    idx1 = 32&lt;br /&gt;    idx1 -= 1 while first[idx1] == last[idx1]&lt;br /&gt;    prefix = first &gt;&gt; idx1+1 &lt;&lt; idx1+1&lt;br /&gt;&lt;br /&gt;    idx2 = 0&lt;br /&gt;    idx2 += 1 while idx2 &lt;= idx1 and first[idx2] == 0 and last[idx2] == 1&lt;br /&gt;&lt;br /&gt;    if idx2 &lt;= idx1&lt;br /&gt;      range_cidr(first, prefix | 2**idx1-1, &amp;block)&lt;br /&gt;      range_cidr(prefix | 1 &lt;&lt; idx1, last, &amp;block)&lt;br /&gt;    else&lt;br /&gt;      yield prefix, 32-idx2&lt;br /&gt;    end&lt;br /&gt;  else&lt;br /&gt;    yield first, 32&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example Usage&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;# $Hg: range2cidr.rb,v 2142c33ada8b 2007-04-11 23:14 +0400 $&lt;br /&gt;# (C) 2007 under terms of GPL v2&lt;br /&gt;# by Vsevolod S. Balashov &lt;vsevolod@balashov.name&gt;&lt;br /&gt;#&lt;br /&gt;# example usage of range_cidr.rb&lt;br /&gt;&lt;br /&gt;require 'lib/range_cidr'&lt;br /&gt;require 'ipaddr'&lt;br /&gt;require 'socket'&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt;  if ARGV.size == 2&lt;br /&gt;    range_cidr(IPAddr.new(ARGV[0]).to_i, IPAddr.new(ARGV[1]).to_i) { |subnet, mask|&lt;br /&gt;      puts "#{IPAddr.new(subnet, Socket::AF_INET).to_s}/#{mask}"  }&lt;br /&gt;  else&lt;br /&gt;    puts "usage: range2cidr &lt;first_ip&gt; &lt;last_ip&gt;"&lt;br /&gt;    puts "example: range2cidr 192.168.1.0 192.168.2.255"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Type in terminal and look result&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ ruby range2cidr.rb 192.168.1.0 192.168.2.255&lt;br /&gt;192.168.1.0/24&lt;br /&gt;192.168.2.0/24&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I hate perl.</description>
      <pubDate>Wed, 11 Apr 2007 19:32:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3798</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
    <item>
      <title>Google PageRank Ruby Checker</title>
      <link>http://snippets.dzone.com/posts/show/3284</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;# -*- coding: utf-8 -*-&lt;br /&gt;# $Id: google-pr.rb,v b205c14e4ef5 2007-01-15 13:53 +0300 $&lt;br /&gt;# (C) 2006-2007 under terms of LGPL v2.1 &lt;br /&gt;# by Vsevolod S. Balashov &lt;vsevolod@balashov.name&gt;&lt;br /&gt;# based on 3rd party code snippets (see comments)&lt;br /&gt;&lt;br /&gt;require 'uri'&lt;br /&gt;require 'open-uri'&lt;br /&gt;&lt;br /&gt;module SEO&lt;br /&gt;&lt;br /&gt;  # http://blog.outer-court.com/archive/2004_06_27_index.html#108834386239051706&lt;br /&gt;  class GooglePR&lt;br /&gt;&lt;br /&gt;    def initialize(uri)&lt;br /&gt;      @uri = uri&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    M=0x100000000 # modulo for unsigned int 32bit(4byte)&lt;br /&gt;&lt;br /&gt;    def m1(a,b,c,d)&lt;br /&gt;      (((a+(M-b)+(M-c))%M)^(d%M))%M # mix/power mod&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def i2c(i)&lt;br /&gt;      [i&amp;0xff, i&gt;&gt;8&amp;0xff, i&gt;&gt;16&amp;0xff, i&gt;&gt;24&amp;0xff]&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def c2i(s,k=0)&lt;br /&gt;      ((s[k+3].to_i*0x100+s[k+2].to_i)*0x100+s[k+1].to_i)*0x100+s[k].to_i&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def mix(a,b,c)&lt;br /&gt;      a = a%M; b = b%M; c = c%M&lt;br /&gt;      a = m1(a,b,c, c &gt;&gt; 13); b = m1(b,c,a, a &lt;&lt;  8); c = m1(c,a,b, b &gt;&gt; 13)&lt;br /&gt;      a = m1(a,b,c, c &gt;&gt; 12); b = m1(b,c,a, a &lt;&lt; 16); c = m1(c,a,b, b &gt;&gt;  5)&lt;br /&gt;      a = m1(a,b,c, c &gt;&gt;  3); b = m1(b,c,a, a &lt;&lt; 10); c = m1(c,a,b, b &gt;&gt; 15)&lt;br /&gt;      [a, b, c]&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def old_cn(iurl = 'info:' + @uri)&lt;br /&gt;      a = 0x9E3779B9; b = 0x9E3779B9; c = 0xE6359A60&lt;br /&gt;      len = iurl.size &lt;br /&gt;      k = 0&lt;br /&gt;      while (len &gt;= k + 12) do&lt;br /&gt;        a += c2i(iurl,k); b += c2i(iurl,k+4); c += c2i(iurl,k+8)&lt;br /&gt;        a, b, c = mix(a, b, c)&lt;br /&gt;        k = k + 12&lt;br /&gt;      end&lt;br /&gt;      a += c2i(iurl,k); b += c2i(iurl,k+4); c += (c2i(iurl,k+8) &lt;&lt; 8) + len&lt;br /&gt;      a,b,c = mix(a,b,c)&lt;br /&gt;      return c&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def cn&lt;br /&gt;      ch = old_cn&lt;br /&gt;      ch = ((ch/7) &lt;&lt; 2) | ((ch-(ch/13).floor*13)&amp;7)&lt;br /&gt;      new_url = []&lt;br /&gt;      20.times { i2c(ch).each { |i| new_url &lt;&lt; i }; ch -= 9 }&lt;br /&gt;      ('6' + old_cn(new_url).to_s).to_i&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def request_uri&lt;br /&gt;      # http://www.bigbold.com/snippets/posts/show/1260 + _ -&gt; %5F&lt;br /&gt;      "http://toolbarqueries.google.com/search?client=navclient-auto&amp;hl=en&amp;ch=#{cn}&amp;ie=UTF-8&amp;oe=UTF-8&amp;features=Rank&amp;q=info:#{URI.escape(@uri, /[^-.!~*'()a-zA-Z\d]/)}"&lt;br /&gt;    end&lt;br /&gt; &lt;br /&gt;    def page_rank(uri = @uri)&lt;br /&gt;      @uri = uri if uri != @uri&lt;br /&gt;      open(request_uri) { |f| return $1.to_i if f.string =~ /Rank_1:\d:(\d+)/ }&lt;br /&gt;      nil&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    private :m1, :i2c, :c2i, :mix, :old_cn&lt;br /&gt;    attr_accessor :uri&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0 and 1 == ARGV.size&lt;br /&gt;  puts SEO::GooglePR.new(ARGV[0]).page_rank&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 15 Jan 2007 15:59:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3284</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
    <item>
      <title>nowww!</title>
      <link>http://snippets.dzone.com/posts/show/3188</link>
      <description>set redirect from www.example.com to example.com for nginx web server&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;server {&lt;br /&gt;    listen       80;&lt;br /&gt;    server_name  example.com www.example.com;&lt;br /&gt;    if ( $host = www.example.com ) {&lt;br /&gt;        rewrite ^\/(.*)$ http://example.com/$1 permanent;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    .....&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 21 Dec 2006 17:57:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3188</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
    <item>
      <title>backup subversion repository over email</title>
      <link>http://snippets.dzone.com/posts/show/2635</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;#&lt;br /&gt;# $Id: repodiff 3 2006-09-21 18:48:39Z sevkin $&lt;br /&gt;#&lt;br /&gt;# subversion repository incremental backup over e-mail&lt;br /&gt;#&lt;br /&gt;# (c) 2006 Vsevolod Balashov under terms of GNU GPL v.2 or later&lt;br /&gt;&lt;br /&gt;SVNROOT=/var/svn&lt;br /&gt;EMAIL=your@email.here&lt;br /&gt;STORE=`mktemp -d`&lt;br /&gt;GPGCRYPT=n&lt;br /&gt;&lt;br /&gt;for REPO in `ls $SVNROOT`; do &lt;br /&gt;	REPOPATH=$SVNROOT/$REPO;&lt;br /&gt;	if [ -r $REPOPATH/youngest ]; then&lt;br /&gt;		LATEST=`cat $REPOPATH/youngest`&lt;br /&gt;		YOUNGEST=`svnlook youngest $REPOPATH`&lt;br /&gt;		if [ $LATEST -lt $YOUNGEST ]; then&lt;br /&gt;			svnadmin dump $REPOPATH --incremental -r $LATEST:$YOUNGEST &gt;$STORE/$REPO 2&gt;/dev/null&lt;br /&gt;		fi&lt;br /&gt;	else&lt;br /&gt;		svnadmin dump $REPOPATH --incremental &gt;$STORE/$REPO 2&gt;/dev/null&lt;br /&gt;	fi &lt;br /&gt;	echo $YOUNGEST &gt;$REPOPATH/youngest &lt;br /&gt;done&lt;br /&gt;&lt;br /&gt;if [ `ls $STORE | wc -w` -gt 0 ]; then&lt;br /&gt;	BACKUP=repodiff_`date -u +%Y%m%d%H%M%S`.tar.bz2&lt;br /&gt;	ATTACH=$STORE/../$BACKUP&lt;br /&gt;	tar  -C $STORE -cjf $ATTACH .&lt;br /&gt;	if [ $GPGCRYPT = y ]; then&lt;br /&gt;		gpg -e -r $EMAIL $ATTACH&lt;br /&gt;		ATTACH=$ATTACH.gpg&lt;br /&gt;	fi&lt;br /&gt;	echo "." | mutt -c $EMAIL -a $ATTACH -s "repository incremental backup"&lt;br /&gt;	rm -f $ATTACH&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;rm -rf $STORE&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 21 Sep 2006 22:58:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2635</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
    <item>
      <title>Subversion service for low-load (personal?) sources repository</title>
      <link>http://snippets.dzone.com/posts/show/2623</link>
      <description>Create separate user and insert svnserve into inetd.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# useradd -g root -s /bin/false -d /dev/null -c "SubVersion Daemon" svnserve&lt;br /&gt;# mkdir /var/svn&lt;br /&gt;# chown -R svnserve /var/svn&lt;br /&gt;# update-inetd --add 'svn\tstream\ttcp\tnowait\tsvnserve\t/usr/sbin/tcpd\t/usr/bin/svnserve --inetd --root /var/svn'&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;/var/svn - root of repository.&lt;br /&gt;update-inetd - standart tool in debian and ubuntu linux distros&lt;br /&gt;&lt;br /&gt;You must run svnadmin as svnserve user for manage your repository&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ sudo sudo -u svnserve svnadmin &lt;command&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 19 Sep 2006 23:48:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2623</guid>
      <author>sevkin (Vsevolod Balashov)</author>
    </item>
  </channel>
</rss>
