Skip to content

Instantly share code, notes, and snippets.

@mhluongo
mhluongo / gist:1943810
Created February 29, 2012 19:30
An example neo4django Person model with connections() based on rest traversals
class Person(models.NodeModel):
name = models.StringProperty()
knows = models.Relationship('Person',
rel_type = neo4django.Outgoing.KNOWS,
related_name = 'is_known')
def connections(self, depth=1):
return [Person._neo4j_instance(node) \
for node in self.node.traverse(types=[client.All.KNOWS], stop=depth)]
@mhluongo
mhluongo / gist:1943955
Created February 29, 2012 19:52
An example neo4django Person model with connections() based on select_related
class Person(models.NodeModel):
name = models.StringProperty()
knows = models.Relationship('Person',
rel_type = neo4django.Outgoing.KNOWS,
related_name = 'is_known')
def connections(self, depth=1):
from neo4django.db.models import query
from itertools import chain
query.execute_select_related(models=[self], max_depth=depth)
@mhluongo
mhluongo / soft_jaccard.py
Created March 12, 2012 19:25
An implementation of a "soft Jaccard" set similarity measure
>>> import jellyfish
>>> from soft_jaccard import soft_jaccard
>>> c1 = set(['CL Isbell','C. L. Isbell'])
>>> c2 = set(['C Isbell','C Isbell, Jr.'])
>>> soft_jaccard(c1, c2, jellyfish.jaro_winkler)
0.75848950260673509
@mhluongo
mhluongo / lazy_neorest_primitives.py
Created April 30, 2012 20:33
A quick hack for lazy-loaded nodes and relationships using neo4jrestclient (as of 12/2012).
class LazyBase(object):
"""
A mixin to make elements of the REST client lazy.
"""
def __init__(self, url, dic):
self._dont_update = True
super(LazyBase, self).__init__(url, create=False)
self._dic = dic.copy()
self._dont_update = False
@mhluongo
mhluongo / gist:2628389
Created May 7, 2012 15:24
QuerySet relational filtering workaround (as of 5/7/2012. neo4django 03b247d7911753)
# a hack for `NodeA.objects.filter(nodeb__in=nodebnodes)`
from neo4django.db import connections
from neo4django.db.models.script_utils import LazyNode
# ...
b_nodes = set([...]) #your NodeB instances
b_node_ids = [b.id for b in b_nodes]
type_node_a = NodeA._type_node()
@mhluongo
mhluongo / download_neo.sh
Created May 31, 2012 14:33
A bit of bash we use to download Neo and install the cleandb extension on development machines.
#!/bin/bash
VERSION='1.6'
DIR="neo4j-community-$VERSION"
FILE="$DIR-unix.tar.gz"
SERVER_PROPERTIES_FILE="lib/neo4j/conf/neo4j-server.properties"
if [[ ! -d lib/$DIR ]]; then
wget http://dist.neo4j.org/$FILE
tar xvfz $FILE &> /dev/null
from neo4django.db import models
import neo4django
class Concept(models.NodeModel):
name = models.StringProperty(indexed=True)
class ContainerConcept(Concept):
class Meta:
abstract = True
@mhluongo
mhluongo / gist:3251313
Created August 3, 2012 20:39
Attempt to replicate http://travis-ci.org/#!/mhluongo/neo4django/jobs/2013936 using the travis-php box.
vagrant@nettuno:~$ cd ~/builds
vagrant@nettuno:~/builds$ export TRAVIS_PULL_REQUEST=false
vagrant@nettuno:~/builds$ export TRAVIS_SECURE_ENV_VARS=false
vagrant@nettuno:~/builds$ export NEO4J_VERSION="1.6.3"
vagrant@nettuno:~/builds$ git clone --depth=100 --quiet git://github.com/mhluongo/neo4django.git mhluongo/neo4django
vagrant@nettuno:~/builds$ cd mhluongo/neo4django
vagrant@nettuno:~/builds/mhluongo/neo4django$ git checkout -qf 155c266129d2d587b39e5d7acff86b123c94dda7
vagrant@nettuno:~/builds/mhluongo/neo4django$ export TRAVIS_PYTHON_VERSION=2.6
vagrant@nettuno:~/builds/mhluongo/neo4django$ source ~/virtualenv/python2.6/bin/activate
vagrant@nettuno:~/builds/mhluongo/neo4django$ python --version
@mhluongo
mhluongo / gist:4009325
Created November 3, 2012 23:37
another neo4django relational filtering workaround
# a hack for pete.groups.filter(created_date__gt = datetime.date(2012,1,1), type__type = "Chess Club")
import datetime
import neo4django
from neo4django.db import models
class Person(models.NodeModel):
auto_id = models.AutoProperty(indexed=True)
groups = models.Relationship('Group', related_name='people',
rel_type=neo4django.Outgoing.MEMBER_OF)
@mhluongo
mhluongo / gist:4199327
Created December 4, 2012 00:22
A quick orcid-python example.
>>> import orcid
>>> cs_authors = orcid.search('computer science')
>>> print cs_authors.next()
<Author Olga Zhelenkova, ORCID 0000-0003-0028-2469>