Skip to content

Instantly share code, notes, and snippets.

View gromgull's full-sized avatar

Gunnar Aastrand Grimnes gromgull

View GitHub Profile
@gromgull
gromgull / unmatchedgroup.py
Created October 20, 2012 06:01
Python "unmatched group" regex replace workaround
import re
def re_sub(pattern, replacement, string):
def _r(m):
# Now this is ugly.
# Python has a "feature" where unmatched groups return None
# then re.sub chokes on this.
# see http://bugs.python.org/issue1519638
# this works around and hooks into the internal of the re module...
@gromgull
gromgull / gist:5418745
Created April 19, 2013 07:38
rdflib sparql example
from rdflib import Graph
g = Graph()
g.parse('http://datahub.io/dataset/semanticquran');
query = """
SELECT ?preposition sample(?verseTextAr) WHERE {
?word gold:PartOfSpeechProperty olia-ar:Preposition;
skos:prefLabel ?preposition;
dcterms:isPart ?verse.
@gromgull
gromgull / rdflib-all-queries-8000
Last active December 16, 2015 13:28
rdflib store comparison
{
"metadata": {
"name": "allqueries"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
@gromgull
gromgull / N3 source-highlight language definition
Created May 24, 2013 13:39
N3 source-highlight language definition file, usage: source-highlight --lang-def N3.lang -s N3 -f latexcolor -i {my_sparql_file} -o {myoutput.tex}
# N3 source-highlight language definition file
# Author: Gunnar Aastrand Grimnes
# e-Mail: gunnar.grimnes < AT > dfki.de
# the fields used for things is pretty arbitrary, but it makes for nice HTML output
# source-highlight -f latexcolor --lang-def n3.lang thesis/examples/bibtexrdf2.n3
keyword = "prefix|a"
@gromgull
gromgull / SPARQL source-highlight language definition
Created May 24, 2013 13:42
SPARQL source-highlight language definition file Usage: source-highlight --lang-def SPARQL.lang -s SPARQL -f latexcolor -i {inputfile} -o {outputfile}
# SPARQL source-highlight language definition file
# Author: Gunnar Aastrand Grimnes
# e-Mail: gunnar.grimnes < AT > dfki.de
# the fields used for things is pretty arbitrary, but it makes for nice HTML output
# source-highlight -f latexcolor --lang-def SPARQL.lang -s SPARQL inputdoc.sparql
keyword = "SELECT|WHERE|INSERT|DELETE|FILTER|GROUP BY"
@gromgull
gromgull / find_duplicate_photos.py
Last active December 19, 2015 16:58
finding duplicate images by name and exif data exif parser from https://github.com/ianare/exif-py
def getdate(f):
try:
with file(f) as x:
exif=EXIF.process_file(x, stop_tag="DateTimeOriginal")
return exif["EXIF DateTimeOriginal"].values
except:
return None
def noext(f):
""" remove ALL extensions from filename (os.path.splitext only removes last) """
@gromgull
gromgull / sparql_generator.py
Last active November 15, 2017 13:49
RDFLib SPARQLGenerator
from __future__ import unicode_literals
from collections import defaultdict
from io import StringIO
from rdflib import URIRef, Literal, Variable
def unroll(triples, vars=None):
"""
Convert list of tuples specifying triples with repeat subjects or
from rdflib import Graph, Literal, URIRef
from rdflib.collection import Collection
g = Graph()
g.parse(data = '''<urn:thing> <urn:prop> ( 1 2 3 ) . ''', format="turtle")
collection_resource = g.value(URIRef('urn:thing'), URIRef('urn:prop'))
@gromgull
gromgull / processpool_tester.py
Created May 16, 2017 18:43
concurrent.futures.ProcessPoolExecutor bug in 2.7.12
"""
This script will break in various ways in python 2.7 with the concurrent.futures backport.
The script will either:
* run just fine, and exit cleanly
* do all the work, but not exit.
* and more rarely, not actually start any work.
@gromgull
gromgull / doctor.md
Last active June 9, 2017 09:12
a conversation with the emacs doctor

I am the psychotherapist. Please, describe your problems. Each time you are finished talking, type RET twice.

I am writing this article about chatbots

Why do you say you are writing this article about chatbots?

because it's true.

Can you elaborate on that?