Lab: Getting started with VSCode, Python and RDFlib: Difference between revisions

From info216
No edit summary
No edit summary
(14 intermediate revisions by the same user not shown)
Line 2: Line 2:


==Topics==  
==Topics==  
Today we have 2 goals.
1. To setup a programming environment that you will use for the lab excercised and group project (if you don't already have one).
2. To start some basic programming of RDF.
Installation of and introduction to Python, Pycharm, Pip and RDFlib.
Installation of and introduction to Python, Pycharm, Pip and RDFlib.


'''PyCharm''' is a Python Integrated Development Environment (IDE) that can be used for writing python code.
'''PyCharm''' is a Python Integrated Development Environment (IDE) that can be used for writing python code.


'''Pip''' is used to easily install additional python packages for your coding environment.
The '''Pip''' command is used to easily install additional python packages for your coding environment.


'''RDFLib''' is a Python package for working with RDF. With this we can create RDF graphs, parse and serialize RDF, perform SPARQL queries on graphs and more.
'''RDFLib''' is a Python package for working with RDF. With this we can create RDF graphs, parse and serialize RDF, perform SPARQL queries on graphs and more.
Line 20: Line 24:
Otherwise Download and Install the free and open source '''Pycharm Community Edition''' IDE [https://www.jetbrains.com/pycharm/download/ here.]
Otherwise Download and Install the free and open source '''Pycharm Community Edition''' IDE [https://www.jetbrains.com/pycharm/download/ here.]


 
Start Pycharm and create a new project with a new .py file.
'''Pip On Windows:'''
Download pip here: https://bootstrap.pypa.io/get-pip.py. (Right click on this link and click "Save link as". You can save it to you desktop or somewhere else if you would like)
 
Open a command prompt and navigate to the folder containing get-pip.py. (e.g enter 'cd Desktop' to get to the desktop)
 
Now run the following command in the promt 'python get-pip.py'
 
'''Pip On Mac:'''
 
Open the terminal and first run: 'curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py'
 
Now run this: 'python get-pip.py'
 


'''RDFlib'''  
'''RDFlib'''  


Using the Pycharm terminal, install RDFlib by simply entering: pip install rdflib
Using the Pycharm terminal, install RDFlib by simply entering: pip install rdflib
Start Pycharm and create a new project with a new .py file.


To import rdflib to your .py file, write 'import rdflib' at the top of the file.  
To import rdflib to your .py file, write 'import rdflib' at the top of the file.  
Alternatively to import certain modules of rdflib, you can write e.g 'from rdflib import Graph, URIRef
Alternatively to import certain modules of rdflib, you can write e.g 'from rdflib import Graph'




'''Coding Tasks'''
'''Coding Tasks'''


When solving the coding tasks you can look at the readings below.
When solving the coding tasks you can look at the readings below if needed.
Use the Classes/interfaces and methods listed at the bottom of the page.  
Use the Classes/interfaces and methods listed at the bottom of the page. ()




'''1.''' Write a program that creates an RDF graph containing the triples from the following sentences. Note that one sentence could result in more than one triple.
'''1.''' Write a program that creates an RDF graph containing the triples from the following sentences. Note that one sentence could result in more than one triple.
When solving these particular tasks I would try to avoid using Blank Nodes, and instead use full URI's instead. For URIs you can just use an example URI like "http://example.org/".
This means that if you talk about a person called Cade, the URI could be  "http://example.org/Cade". Remember the Namespaces can be used so that you don't have to write the full URI everytime.


* Cade is married to Mary  
* Cade is married to Mary  
* The capital of France is Paris
* Cade is 27 years old
* Cade is 27 years old
* The capital of France is Paris
* 26 years is the age of Mary
* Marys interests include hiking, chocolate and biology
* Marys interests include hiking, chocolate and biology
* Mary is a student
* Paris is a City in France
* Paris is a City in France
* Cade and Mary are kind people
* Cade and Mary are kind people


Now you can print the contents of the graph by using this code:


==If you have more time...==
==If you have more time...==
If you have more time you can continue extending your graph with some of the stuff from lab 2:
If you have more time you can continue extending your graph with some of the stuff from lab 2:
''"Cade Tracy lives in 1516 Henry Street, Berkeley, California 94709, USA. He has a B.Sc. in biology from the University of California, Berkeley from 2011. His interests include birds, ecology, the environment, photography and travelling. He has visited Canada and France."''
''"Cade has the full name Cade Tracey. He lives in 1516 Henry Street, Berkeley, California 94709, USA. He has a B.Sc. in biology from the University of California, Berkeley from 2011. His interests include birds, ecology, the environment, photography and travelling. He has visited Canada and France."''
Try to use as many different methods as possible to create the triples.
Try to use as many different methods as possible to create the triples.


Line 84: Line 77:
*Graph (add)
*Graph (add)
*URIRef
*URIRef
*Bnode
*Literal
*Literal
*NameSpace
*NameSpace
*Bnode
*RDF, FOAF
*RDF, FOAF


All RDFlib modules can be found [https://rdflib.readthedocs.io/en/stable/py-modindex.html here]
All RDFlib modules can be found [https://rdflib.readthedocs.io/en/stable/py-modindex.html here]
Browser search (often Ctrl-F) is useful here to find the module that you want.
Browser search (often Ctrl-F) is useful here to find the module that you want.

Revision as of 10:52, 2 March 2020

Lab 1: Getting started with Pycharm, Python and RDFlib

Topics

Today we have 2 goals. 1. To setup a programming environment that you will use for the lab excercised and group project (if you don't already have one). 2. To start some basic programming of RDF.

Installation of and introduction to Python, Pycharm, Pip and RDFlib.

PyCharm is a Python Integrated Development Environment (IDE) that can be used for writing python code.

The Pip command is used to easily install additional python packages for your coding environment.

RDFLib is a Python package for working with RDF. With this we can create RDF graphs, parse and serialize RDF, perform SPARQL queries on graphs and more.

Tasks

You likely already have Python installed on your computer (it comes with MAC for instance). To find out, type 'python' in the commandline on windows or type 'python --version' in the terminal on your MAC. If you don't already have Python, download and install the Python for your operative system here. We recommend downloading the most recent version.

If you are already using a different Python IDE than Pycharm that you are comfortable with, then you are free to use that program instead.

Otherwise Download and Install the free and open source Pycharm Community Edition IDE here.

Start Pycharm and create a new project with a new .py file.


RDFlib

Using the Pycharm terminal, install RDFlib by simply entering: pip install rdflib

To import rdflib to your .py file, write 'import rdflib' at the top of the file. Alternatively to import certain modules of rdflib, you can write e.g 'from rdflib import Graph'


Coding Tasks

When solving the coding tasks you can look at the readings below if needed. Use the Classes/interfaces and methods listed at the bottom of the page. ()


1. Write a program that creates an RDF graph containing the triples from the following sentences. Note that one sentence could result in more than one triple. When solving these particular tasks I would try to avoid using Blank Nodes, and instead use full URI's instead. For URIs you can just use an example URI like "http://example.org/". This means that if you talk about a person called Cade, the URI could be "http://example.org/Cade". Remember the Namespaces can be used so that you don't have to write the full URI everytime.

  • Cade is married to Mary
  • The capital of France is Paris
  • Cade is 27 years old
  • 26 years is the age of Mary
  • Marys interests include hiking, chocolate and biology
  • Mary is a student
  • Paris is a City in France
  • Cade and Mary are kind people


If you have more time...

If you have more time you can continue extending your graph with some of the stuff from lab 2: "Cade has the full name Cade Tracey. He lives in 1516 Henry Street, Berkeley, California 94709, USA. He has a B.Sc. in biology from the University of California, Berkeley from 2011. His interests include birds, ecology, the environment, photography and travelling. He has visited Canada and France." Try to use as many different methods as possible to create the triples.

Documentation and other useful reading

If needed here is an introduction of how to create projects and python files in Pycharm Pycharm - Tutorial by jetbrains.com.

RDFLib Documentation- Todays focus are the following:

Additional recommended in depth documentation:

Slides from the lecture

Relevant RDFlib interfaces (and methods)

import rdflib:

  • Graph (add)
  • URIRef
  • Literal
  • NameSpace
  • Bnode
  • RDF, FOAF

All RDFlib modules can be found here Browser search (often Ctrl-F) is useful here to find the module that you want.