Skip to content

Instantly share code, notes, and snippets.

View andymccurdy's full-sized avatar

Andy McCurdy andymccurdy

  • San Francisco, CA
View GitHub Profile
LUA Scripting with redis-py
===========================
redis-py supports the EVAL, EVALSHA, and SCRIPT commands. However, there are
a number of edge cases that make these commands tedious to use in real world
scenarios. Therefore, redis-py exposes a Script object that makes scripting
much easier to use.
To create a Script instance, use the `register_script` function on a client
instance passing the LUA code as the first argument. `register_script` returns
class Script(object):
"An executable LUA script object returned by ``register_script``"
def __init__(self, registered_client, script):
self.registered_client = registered_client
self.script = script
self.sha = None
def __call__(self, keys=[], args=[], client=None):
"Execute the script, passing any required ``args``"
<shockwave:andy : ~ > brew install python3 --framework --universal -v
==> Downloading http://python.org/ftp/python/3.2.3/Python-3.2.3.tar.bz2
Already downloaded: /Library/Caches/Homebrew/python3-3.2.3.tar.bz2
/usr/bin/tar xf /Library/Caches/Homebrew/python3-3.2.3.tar.bz2
==> ./configure --prefix=/usr/local/Cellar/python3/3.2.3 --enable-ipv6 --enable-framework=/usr/local/Cellar/python3/3.2.3/Frameworks
./configure --prefix=/usr/local/Cellar/python3/3.2.3 --enable-ipv6 --enable-framework=/usr/local/Cellar/python3/3.2.3/Frameworks
checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... darwin
checking machine type as reported by uname -m... x86_64
# == ella ==
# fetching
# you should switch ella to branch 1a1360c90efcce3fafd9016051192533c6e3ef0b:
cd /Users/andy/momme/ella; git checkout 1a1360c90efcce3fafd9016051192533c6e3ef0b || git checkout -b 1a1360c90efcce3fafd9016051192533c6e3ef0b origin/1a1360c90efcce3fafd9016051192533c6e3ef0b
HEAD is now at 1a1360c... run generate_publish_signals way more often
fatal: ambiguous argument '..origin/1a1360c90efcce3fafd9016051192533c6e3ef0b': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions
YOUR_USERNAME = 'andy'
import scout.recon.register
from django.contrib.auth.models import User
u = User.objects.get(username=YOUR_USERNAME)
u.profile['plan_id'] = 2
u.profile['next_billing_date'] = '2012-09-01'
u.save()
cat_to_remove = <category_publishable_is_being_removed_from>
categories = [all_current_leaf_categories_publishable_is_in];
categories.remove(cat_to_remove);
all_cats = set()
for cat in categories:
while cat:
all_cats.add(cat)
cat = cat.parent
# all_cats should now be a set of all categories the publishable should still be in after the removal
======================================================================
ERROR: Failure: ImportError (cannot import name SkipTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/andy/.virtualenvs/tested/lib/python2.6/site-packages/nose/loader.py", line 390, in loadTestsFromName
addr.filename, addr.module)
File "/Users/andy/.virtualenvs/tested/lib/python2.6/site-packages/nose/importer.py", line 39, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/Users/andy/.virtualenvs/tested/lib/python2.6/site-packages/nose/importer.py", line 86, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
cd ~
rm -rf redis
git clone https://github.com/antirez/redis.git
cd redis
make
cp src/redis-server /usr/local/bin/
cp src/redis-cli /usr/local/bin/
####################################################
# WARNING: This is very hacky! Use at your own risk.
#
# This assumes you have all keys in a single DB and
# want to move them all to DB 0. If you have keys
# in more than one DB, the first DB found will be
# rewritten to 0, with all others left alone.
####################################################
import shutil
class Redis(...):
def __init__(self):
# dictionary of "installed" scripts to sha's
self.loaded_scripts = {}
def load_script(self, name, code):
"Install a script, giving it a friendly name"
sha = self.execute_command('SCRIPT', 'LOAD', code)
# save the friendly name => sha
self.loaded_scripts[name] = sha