Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved code documentation in puma_utils.py with detailed docstrings and comments. #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions puma_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
def list_bib(search_string = None, owner_username=None, year=None, verbose=False):
def list_bib(search_string=None, owner_username=None, year=None, verbose=False):
"""
Fetches a list of bibliographic entries from the PUMA API based on the
search criteria provided.

Parameters
----------
search_string : str, optional
A term to search in the bibliography database. If provided, only entries
matching the search term will be returned. Default is None.
owner_username : str, optional
The username of the owner whose bibliographic entries will be retrieved.
If not provided, entries from all users will be considered. Default is None.
year : int or list of int, optional
The publication year(s) to filter the results. If a single integer is provided,
only entries from that year will be returned. If a list or a tuple is provided,
entries from the specified years (or year range) will be returned. Default is None.
verbose : bool, optional
If True, the title of each returned entry will be printed. Default is False.

Returns
-------
list
A list of bibliographic entries in BibTeX format that match the search criteria.
If no entries match, an empty list is returned.

Notes
-----
Environment variables `PUMA_USERNAME` and `PUMA_API_KEY` must be set for
authentication.

Raises
------
requests.exceptions.RequestException
If there is a problem with the network connection.
"""
import requests
import base64
import os
Expand All @@ -13,7 +48,7 @@ def list_bib(search_string = None, owner_username=None, year=None, verbose=False
base64_auth = base64.b64encode(auth_bytes).decode('ascii')
headers = {'Authorization': f'Basic {base64_auth}'}

# Get all posts of a given user
# Construct the API URL with the required parameters
url = f"https://puma.scadsai.uni-leipzig.de/api/posts?resourcetype=bibtex&format=json&end=2000"
if search_string is not None:
url += "&search=" + search_string
Expand Down Expand Up @@ -60,6 +95,5 @@ def list_bib(search_string = None, owner_username=None, year=None, verbose=False
bibtex_entries.append(bibtex)
return bibtex_entries
else:
# Handle errors by printing the response text
print(response.text)