Skip to content

Instantly share code, notes, and snippets.

View gregroberts's full-sized avatar

Greg Roberts gregroberts

View GitHub Profile
@gregroberts
gregroberts / cypher.py
Last active January 3, 2018 15:06
A function for pandas to get results of a cypher query directly into a DataFrame
from pandas.core.api import DataFrame
from pandas.tseries.tools import to_datetime
#save me at site-packages\pandas\io\cypher.py
def read_cypher(cypher, con, index_col=None, params = {},parse_dates = None, columns= None):
'''
Run a Cypher query against the graph at con, put the results into a df
Parameters
@gregroberts
gregroberts / database_report.py
Last active January 3, 2018 15:06
Py2neo Write a report on the contents of a graph database, describe what nodes are in, what sort of properties they (seem to) have, and what edges come in and out of each
import py2neo
import datetime
#where we write it
f_name = 'DBREPORT_%s.txt' % datetime.datetime.today().strftime('%Y-%m-%d')
#overwrite anything previous
with open(f_name,'wb') as f:
f.write('REPORT COMPILATION STARTED AT %s' % datetime.datetime.now())
#odd quirk found on Python
#say you have a list of values, and you want a dict with keys for each item, and each value an empty array.
#I found myself in this situation, and I decided to do it like this:
l = ['a','b','c']
gg = dict(zip(l,[[]]*3))
gg
#however, when you try and put a value into one of those arrays:
gg['a'].append(1)
gg
#1: {'a': [1], 'b': [1], 'c': [1]}
import sqlite3
#this one line is all you need to set up a database
connection = sqlite3.connect('example')
#a cursor object is used to execute queries against the db
cursor = connection.cursor()
#create a table
cursor.execute('CREATE TABLE example (id real, value text)')
import httplib
import urllib
import urllib2
import re
import csv
import logging
from cookielib import CookieJar
class pyGTrends(object):
"""
@gregroberts
gregroberts / gist:8666134
Last active January 4, 2016 19:19
For an ordered list, remove permutations and non-unique elements & preserve order
from itertools import permutations
def ret_unique(a):
'''returns a copy of the list with:
order preserved,
permutations removed,
repeat elements removed'''
for i,el in enumerate(a):
b = permutations(el,3)
for el1 in b: