Skip to content

Instantly share code, notes, and snippets.

@pwin
pwin / REST-API.py
Last active October 18, 2024 13:43
A REST-ful API script using Bottle.py
#!/usr/bin/env python
"""
REST-API.py
The MIT License (MIT)
Copyright (c) 2014 P.Winstanley, C.Taylor
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@pwin
pwin / ProductForge_SPARQL_exploration_1.md
Last active May 2, 2019 08:46
A script of an introduction to SPARQL and http://statistics.gov.scot for the ProductForge Hack event - 26 Jan 2018
@prefix : <http://globe.org#> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix fn: <http://www.w3.org/2005/xpath-functions#> .
@prefix grddl: <http://www.w3.org/2003/g/data-view#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sesame: <http://www.openrdf.org/schema/sesame#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@pwin
pwin / GolspieDayHalfHour.html
Created January 24, 2018 20:46
Illustration of using javascript and AMCharts to access and present data from RDF data cube
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html id="Scottish_Government" xmlns="http://www.w3.org/1999/xhtml" lang="en-GB" xml:lang="en-GB">
<head>
<title>Golspie Half Hourly</title>
<!--
This produces plots of half hourly consumption values
The dropdown for the buildings shows 45 consumers
-->
@pwin
pwin / utilityByBldgYearMonthDay
Created December 14, 2013 00:06
SPARQL query to get utility consumption for each building by year, month and day
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix gol: <http://cofog01.data.scotland.gov.uk/def/golspie/>
prefix qb: <http://purl.org/linked-data/cube#>
prefix buildingCode: <http://cofog01.data.scotland.gov.uk/id/facility/>
prefix fn: <http://www.w3.org/2005/xpath-functions#>
prefix util: <http://cofog01.data.scotland.gov.uk/def/golspie/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
@pwin
pwin / utilityByYearMonthDate
Last active December 31, 2015 07:09
Utility Value by year, month and date ordered. Uses AFN functions
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix gol: <http://cofog01.data.scotland.gov.uk/def/golspie/>
prefix qb: <http://purl.org/linked-data/cube#>
prefix buildingCode: <http://cofog01.data.scotland.gov.uk/id/facility/>
prefix fn: <http://www.w3.org/2005/xpath-functions#>
prefix util: <http://cofog01.data.scotland.gov.uk/def/golspie/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>
select ?year ?month ?day (sum(?o) as ?val )
@pwin
pwin / getSites.sparql
Last active December 31, 2015 02:19
SPARQL to get sites
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX org: <http://www.w3.org/ns/org#>
PREFIX v: <http://www.w3.org/2006/vcard/ns#>
SELECT * WHERE {
SERVICE <http://cofog01.data.scotland.gov.uk/sparql> {
select distinct ?fn ?code {
graph ?g {
@pwin
pwin / utilityValueByMonth
Last active December 31, 2015 00:49
SPARQL query to get the total utility consumption per month for a building
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix gol: <http://cofog01.data.scotland.gov.uk/def/golspie/>
prefix qb: <http://purl.org/linked-data/cube#>
prefix buildingCode: <http://cofog01.data.scotland.gov.uk/id/facility/>
prefix fn: <http://www.w3.org/2005/xpath-functions#>
prefix util: <http://cofog01.data.scotland.gov.uk/def/golspie/>
prefix skos: <http://www.w3.org/2004/02/skos/core#>
select ?month (sum(?o) as ?val )
@pwin
pwin / SG_scraper.py
Last active December 29, 2015 07:39
Scrapy spider for scotland.gov.uk
import uuid
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.spiders import Rule, CrawlSpider
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from SG_2013_10_29.items import Sg20131029Item
from pybloom import BloomFilter
bloom = BloomFilter(capacity=500000, error_rate=0.000001)
@pwin
pwin / IATI-select-1.xquery
Last active December 26, 2015 08:49
some selections from IATI files - suitable for creating dashboards etc
xquery version "3.0";
declare namespace functx = "http://www.functx.com";
declare function functx:sort
( $seq as item()* ) as item()* {
for $item in $seq
order by $item
return $item
} ;