Skip to content

Instantly share code, notes, and snippets.

View msalvadores's full-sized avatar

Manuel Salvadores msalvadores

View GitHub Profile
"""
Sample code to traverse RDF list with RDFLIB.
author: Manuel Salvadores (msalvadores@gmail.com)
rdf containers are a pain in general, quite annoying to handle them.
To get all the authors for a given article like in your case you could do
something like the code I am posting below.
@msalvadores
msalvadores / change_tag_beautifulsoup.py
Created March 13, 2011 13:08
How to change tag name with BeautifulSoup.
"""
author: Manuel Salvadores (msalvadores@gmail.com)
Code sample in answer from Stack Overflow:
http://stackoverflow.com/questions/5289189/how-to-change-tag-name-with-beautifulsoup/5289523#5289523
"""
import BeautifulSoup
if __name__ == "__main__":
data = """
@msalvadores
msalvadores / gist:889157
Created March 27, 2011 12:21
my git/github cheat sheet
" author: Manuel Salvadores http://msalvadores.me "
" My notes on using git/github "
#---- workflow on remote public forked repositories ----
#checkouts master branch from a forked github repo
git clone <git path to my forked repo>
#adds origin for futures merges/pulls
git remote add <name_origin> <git path to original repo>
import sys
import urllib,urllib2
import traceback
import pdb
import time
import json
import os
def query(q,epr,f='text/plain'):
params = {'query': q}
@msalvadores
msalvadores / array_append_len_known.py
Last active December 15, 2015 08:09
A benchmark of two approaches to constructing an array. Bytearray vs array+join. Limitation: one should know the length of the array. Within pypy-1.9 the bytearray approach is one order of magnitude faster. In CPython-2.7, bytearray aprox. 35% faster.
import timeit
DATA = 'abcdef'
def bytearr():
bytesn = 1024 * 1024 * 5
s = bytearray(bytesn)
i = 0
while i < bytesn:
s[i] = DATA[i%len(DATA)]
@msalvadores
msalvadores / mini_dsl.rb
Last active December 15, 2015 00:38
A mini DSL to dynamically generate objects in Ruby.
require 'pry'
module Test
module Settings
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
attr_accessor :settings
@msalvadores
msalvadores / gist:2210565
Created March 26, 2012 23:20
Java function to pass the api key to BioPortal SPARQL
public ResultSet executeQuery(String queryString) throws Exception {
Query query = QueryFactory.create(queryString) ;
QueryEngineHTTP qexec = QueryExecutionFactory.createServiceRequest(this.service, query)
qexec.addParam("apikey", this.apikey)
ResultSet results = qexec.execSelect() ;
return results;
}
@msalvadores
msalvadores / gist:2210491
Created March 26, 2012 23:12
Simple Python Script to Query sparql.bioportal.org
import json,urllib2,urllib,traceback, sys
def query(q,apikey,epr,f='application/json'):
try:
params = {'query': q, 'apikey': apikey}
params = urllib.urlencode(params)
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(epr+'?'+params)
request.add_header('Accept', f)
request.get_method = lambda: 'GET'
@msalvadores
msalvadores / gist:1473419
Created December 13, 2011 19:12
fast filtered output test
package test;
import java.io.PrintStream;
public class TestOutput {
public static void outputFiltered(String label, PrintStream out) {
for (byte b : label.getBytes()) {
if (Character.isLetterOrDigit((char)b)) {
out.print((char)b);
@msalvadores
msalvadores / test_sparql_bioportal.py
Created October 5, 2011 01:53
BioPortal SPARQL Sample Script (Python)
import json
import urllib2
import urllib
import traceback
import sys
def query(q,apikey,epr,f='application/json'):
"""Function that uses urllib/urllib2 to issue a SPARQL query.
By default it requests json as data format for the SPARQL resultset"""