Exercise notebook: Python 2

Offered by: Digital Work at Otto-Friedrich-Universität Bamberg License: CC BY 4.0

We Edit your feedback and suggestions on this notebook!


With this notebook, you can refactor to object-oriented code, switch to a real CoLRev project and load the records using the CoLRev classes, and save changes to create a commit.

Part Label Time (min)
  Set up the example repository 10
Part V Object-oriented code 60
Part VI Programmatic use of CoLRev 30
Part VII Save and commit changes 10
  Wrap-up 10
  Overall 120



To continue working on your Codespace, resume it as follows:

To checkout the starting point (commit), run:

git reset --hard f859208f8ddaf5651f5c2b378e7e07543ca7cdd1

Set up the example repository

To use the typical CoLRev environments (objects), we clone an existing CoLRev project:

cd /workspaces
git clone https://github.com/CoLRev-Environment/example.git
code -a /workspaces/example
Opening the data (example) directory in a separate tab

Instead of running code -a /workspaces/example, you can also open the directory in a separate tab (hover over the path).

The project directory example should now be displayed. Let’s navigate to the directory and check the status:

cd /workspaces/example
colrev status

Task: Briefly check the last commits of the project.

Important

It is important to modify the code in the Python package directory and run all project-specific commands (colrev ...) in the project directory (in this case, example):

Codespace directories

Having a separate package directory is only necessary when modifying the code of the Python package. Once the development is completed, other users of CoLRev can simply install it with pip install colrev and use it in any given project directory.

Part V. Object-oriented code

“How do I write object-oriented code?”

Object-oriented programming

Our next goal is to implement the previous example (adding the journal impact factor to the records) using Python objects/methods, and using the CoLRev infrastructure.

Task: Create the JIFLabeler class, instantiate an object, and add the add_jif() method (based on the Python objects/methods syntax). Run and revise the code (if necessary).

Hint Also adapt the path of the records to data/records.bib. This is where CoLRev projects store the main records file.

To checkout the solution, run:

git reset --hard 670c56408684f8b3b466c22f25a62fe2bed88bd1

Part VI: Programmatic use of CoLRev

“How do I use CoLRev infrastructure?”

Using CoLRev infrastructure

To use the CoLRev infrastructure, take a look at the API reference and find the classes and methods that can be used to load and save records.

Task: Rewrite the code in run.py to use CoLRev classes and methods. Upon instantiating the JIFLabeler, instantiate a ReviewManager object and keep it as an object variable. When calling the main method, the records should be loaded by using the ReviewManager’s dataset methods.

Hint: Remember to import the required modules. Test your code, run the linters, and try to address potential warnings.

To checkout the solution, run:

git reset --hard 29e5cf28e3c96940e19e5af470977092ef3b8d47

Optional challenge

To improve the code, try to implement these two changes:

  • Instead of using print() statements, it is recommended to use the ReviewManager’s logger.
  • Instead of using the record dicts, it is recommended to work with the Record class and its update_field() method.

To checkout the solution, run:

git reset --hard 8a4d064771f2b527b6479ac967bde0a5aabd9b19

Part VII: Save and commit

“How do I save and commit changes?”

Save and commit changes

Task: Save the records (using the dataset attribute of ReviewManager) and create a commit (using the ReviewManager).

Review the commit and its content.

To checkout the solution, run:

git reset --hard 99f4477aabd4dfd5eaec7ddefefab2b632ef95e0

Wrap-up

🎉🎈 You have completed the Python commit notebook - good work! 🎈🎉

In this notebook, we have learned to

  • Write object-oriented code in Python
  • Use CoLRev programmatically
  • Save and commit changes in a CoLRev project
  • Use stop and resume in GitHub Codespaces

Remember to delete your codespace after the session.