Exercise notebook: Python 1
We your feedback and suggestions on this notebook!
With this notebook, you can familiarize yourself with Python syntax, create and run a Python package command, create and modify a dictionary data structure, and use an external library to read BibTeX records as dictionaries. Specifically, we will cover the following learning objectives:
- Learn how to write and use Python code
- Create and modify data items
- Learn how to use external libraries
- Learn how to use functions
| Part | Label | Time (min) |
|---|---|---|
| Part I | Setup | 25 |
| Part II | Data items | 30 |
| Part III | External libraries | 30 |
| Part IV | Functions | 60 |
| Wrap-up | 2 | |
| Overall | 174 |
Part I: Setup
“How do I write and use Python code?”
Start a Codespace in the colrev-python-tutorial repository.
As a first step, we install the package dependency manager uv, which will be used in part 3:
pip install uv
Next, we reset the state of the repository to the beginning of the tutorial:
git reset --hard f0a13be2494181d56eb71a2f1aee8b4511f65919
- As the session progresses, you can checkout the current commits
- Whenever you see a
git reset --hard ...command on the following slides, you can use it to set your repository to the required state (commit).
Setting up a Python script
We implement a simple Pyhton script, run.py, which should be available through the following command:
python run.py
Task: Create the script, have it print “Hello World” and test the python run.py call.
git reset --hard 1778473e75718277ad2a1b623cde99c4fff674c0
Part II: Data items
“How do I create and modify data items?”
Data types
In this part, we focus on the data structure of dictionaries, which are often used in CoLRev. Dictionaries are efficient data structures, which can be used to handle bibliographic records, such as the following (in BibTeX format):
@article{Pare2023,
title = {On writing literature reviews},
author = {Pare, Guy},
journal = {MIS Quarterly},
year = {2023}
}
Task: Create a dictionary containing these data fields and print it when python run.py is called.
You can find the syntax for Python dictionaries (and many other data types) in the W3School.
Challenge (optional): If you have completed the previous tasks, try to use the CoLRev constants for fields like title, author, etc.. In many cases, using constants like these is preferable to so-called “magic strings”.
Changing data
Next, we need a field indicating the record’s status throughout the process.
Add a colrev_status field to the dictionary, and set its value to md_imported. Create a commit once the command prints the following:
Start simple colrev run
{'ID': 'Pare2023', 'title': 'On writing literature reviews', 'journal': 'MIS Quarterly', 'year': '2023', 'author': 'Pare, Guy', 'colrev_status': 'md_imported'}
To checkout the solution, run:
git reset --hard 728a2dbe5a3c0f15e989eac4faab7b877b2f3a0c
Part III: External libraries
“How do I use external libraries?”
Finding and adding external libraries
Next, we decide to load (parse) a BibTeX file stored in the project. Search for an appropriate Python library to parse BibTeX files. Try to figure out how to install it and how to use it.
We decide to use the BibtexParser package, which is developed actively and available under an Open Source license.
pip install bibtexparser
Using external libraries
Go to the bibtexparser tutorial and figure out how to load a BibTeX file. An example records.bib file is available here. To use the file in your codespace, it needs to be uploaded. You can simply drag and drop the records.bib into /workspaces/colrev-python-tutorial.
Bibtexparser has a pre-release (version 2), but for this session, we use version 1 of bibtexparser.
Instead of defining the dictionary in the run.py, use the bibtexparser to load the records.bib file.
Afterward, loop over the records (for ...) and print the title of each record.
To checkout the solution, run:
git reset --hard a84c9be1fb215f9cda8920bcdb86ff529bfc83d2
Part IV: Functions
Next, we would like to create a function, which adds the journal_impact_factor based on the following table:
| journal | journal_impact_factor |
|---|---|
| MIS Quarterly | 8.3 |
| Information & Management | 10.3 |
To checkout the solution, run:
git reset --hard bc94da869184485f7805dba2b9f8e32af0ff2dfb
Wrap-up
🎉🎈 You have completed the Python commit notebook - good work! 🎈🎉
In this notebook, we have learned to
- Write and execute Python code in a Python script
- Create and modify data items, such as dictionaries
- Install and use external libraries
- Write modular code by using functions
To continue using your work in the next session, stop your Codespace here. In contrast to deleting a Codespace (which removes all files, changes, settings, etc.), stopping the Codespace preserves the current state of your work and does not consume computational resources.
