Created
April 22, 2016 04:22
-
-
Save kokumura/be631490f48a631b1b05854165323965 to your computer and use it in GitHub Desktop.
SQLiteにPythonでUDFを追加するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
import sqlite3 | |
import numpy as np | |
class Median: | |
def __init__(self): | |
self.values = [] | |
def step(self, value): | |
self.values.append(value) | |
def finalize(self): | |
return np.median(self.values) | |
def main(file, query): | |
con = sqlite3.connect(file, isolation_level=None) | |
con.create_aggregate("median", 1, Median) | |
c = con.cursor() | |
c.execute(query) | |
l = [] | |
for row in c: | |
print u'\t'.join(unicode(x) for x in row) | |
import sys | |
main(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment