Skip to content

Instantly share code, notes, and snippets.

@koma5
Created January 22, 2017 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koma5/6b34d20f3820da2d2726504e24bf3424 to your computer and use it in GitHub Desktop.
Save koma5/6b34d20f3820da2d2726504e24bf3424 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import rdflib, arrow"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"g = rdflib.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<Graph identifier=N307357be80a24711b8555357f44af739 (<class 'rdflib.graph.Graph'>)>"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.parse(format=\"turtle\", data=\"\"\"\n",
"@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n",
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
"@prefix xml: <http://www.w3.org/XML/1998/namespace> .\n",
"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n",
"@prefix owl: <http://www.w3.org/2002/07/owl#> .\n",
"@prefix dc: <http://purl.org/dc/elements/1.1/> .\n",
"@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n",
"@prefix tzus: <http://www.w3.org/2006/timezone-us#> .\n",
"@prefix : <http://www.w3.org/2006/time#> .\n",
"@base <http://files.5th.ch/ncis> .\n",
"\n",
"<#BetterCallSaulPilot> a foaf:Agent; dc:date [a :Instant; :inXSDDateTime \"2015-02-08T22:00:00-05:00\";].\n",
"<#BCS0102> a foaf:Agent; dc:date [a :Instant; :inXSDDateTime \"2015-02-09T22:00:00-05:00\";].\n",
"\n",
"<#1213> a foaf:Agent ;\n",
" dc:date <#startEarliest> ;\n",
" dc:date <#startSlot> .\n",
"\n",
"<#startEarliest> a :Instant ;\n",
" \n",
" :inXSDDateTime \"2015-02-03T20:00:00-05:00\" ;\n",
" \n",
" :inDateTime [ a :DateTimeDescription ;\n",
" :unitType :unitHour ;\n",
" :timeZone tzus:EST ;\n",
" :year 2015 ;\n",
" :month 2 ;\n",
" :day 3 ;\n",
" :hour 20 \n",
" ] . \n",
"\n",
"<#startSlot> a :Interval ;\n",
" :hasBeginning <#startEarliest> ;\n",
" :hasDurationDescription [ a :DurationDescription ; :hours 1 ] . \n",
" \n",
"\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#print g.serialize()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#print g.serialize(format=\"nt\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"http://files.5th.ch/ncis#1213 will be shown in 12 days, 2:03:34.636730\n",
"local time will be 2:00 on 4. February 2015\n"
]
}
],
"source": [
"results = g.query(\"\"\"\n",
"PREFIX time: <http://www.w3.org/2006/time#>\n",
"PREFIX dc: <http://purl.org/dc/elements/1.1/>\n",
"PREFIX fifth: <http://files.5th.ch/ncis#>\n",
"SELECT ?s ?string\n",
"WHERE {\n",
" ?s dc:date ?inst .\n",
" ?inst time:inXSDDateTime ?string .\n",
"}\n",
"\"\"\")\n",
"\n",
"for uri, dateString in results:\n",
" date = arrow.get(dateString)\n",
" print str(uri) + \" will be shown in \" + str(date - arrow.utcnow())\n",
" print \"local time will be \" + str(date.to(\"CET\").format(\"H:mm on D. MMMM YYYY\"))"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"http://files.5th.ch/ncis#BetterCallSaulPilot will be shown in 8 days, 10:19:06.382183\n",
"local time will be 4:00 on 9. February 2015\n",
"http://files.5th.ch/ncis#1213 will be shown in 3 days, 8:19:06.381502\n",
"local time will be 2:00 on 4. February 2015\n",
"http://files.5th.ch/ncis#BCS0102 will be shown in 9 days, 10:19:06.380919\n",
"local time will be 4:00 on 10. February 2015\n"
]
}
],
"source": [
"results = g.query(\"\"\"\n",
"PREFIX time: <http://www.w3.org/2006/time#>\n",
"PREFIX dc: <http://purl.org/dc/elements/1.1/>\n",
"PREFIX fifth: <http://files.5th.ch/ncis#>\n",
"SELECT ?s ?string\n",
"WHERE {\n",
" ?s dc:date ?inst .\n",
" ?inst time:inXSDDateTime ?string .\n",
"}\n",
"\"\"\")\n",
"\n",
"for uri, dateString in results:\n",
" date = arrow.get(dateString)\n",
" print str(uri) + \" will be shown in \" + str(date - arrow.utcnow())\n",
" print \"local time will be \" + str(date.to(\"CET\").format(\"H:mm on D. MMMM YYYY\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment