Hacker News new | ask | show | jobs
by Scaevolus 2852 days ago
Neat! If you wanted to extend this more (string diffs, non-json encodings), a custom function would be the next logical step. In Python it might be:

    import sqlite3

    def delta(*args):
        ret = {}
        for name, old, new in zip(args[::3], args[1::3], args[2::3]):
            if old != new:
                ret[name] = old
        if ret:
            return ret

    db = sqlite3.connect(':memory:')
    db.create_function('delta1', delta, 3)
    db.create_function('delta2', delta, 6)
    db.create_function('delta3', delta, 9)

    print db.execute('select delta2("b", "20", "20", "c", 3.4, 3.6)').fetchone()
    >> (u'{"c": 3.4}',)
1 comments

in python 3.6

    import sqlite3
    import json

    def delta(*args):
        ret = {}
        for name, old, new in zip(args[::3], args[1::3], 
            args[2::3]):
            if old != new:
               ret[name] = old
        if ret:
           return json.dumps(ret)

    db = sqlite3.connect(':memory:')

    db.create_function('delta1',3, delta)
    db.create_function('delta2',6, delta)
    db.create_function('delta3',9, delta)
    print(db.execute('select delta2("b", 20, 20, "c", 3.4, 
    3.6)').fetchone())
    # ('{"c": 3.4}',)