Skip to content

Commit ab961e2

Browse files
authored
Merge pull request #10 from planetterp/updates-for-pypi-package
Prepare for pypi package publishing
2 parents b079526 + 97c1611 commit ab961e2

8 files changed

Lines changed: 204 additions & 119 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
.idea/*
22
.DS_Store
33
*.pyc
4+
dist/
5+
build/
6+
*.egg-info
7+
__pycache__/

PlanetTerp.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
# PlanetTerp API Python Wrapper
2-
This API Example includes an object, **PlanetTerp.py**, which can be created and called in order to access information from the PlanetTerp API.
32

4-
The `PlanetTerp` object uses the `requests` and `json` packages. One or both of these may already be installed for you. To install them to your computer, use something like `pip3` or `brew`.
3+
This is a Python wrapper around the [PlanetTerp API](http://api.planetterp.com).
54

6-
The `requests` package allows the `PlanetTerp` object to access data from the internet, enabling it to make API calls. The `json` package converts the JSON data provided by the PlanetTerp API into a Python dictionary for easier use.
5+
To install, either use [the code on GitHub](https://github.com/planetterp/PlanetTerp-API-Python-Wrapper), or install with `pip install planetterp` (you might need to run `pip3 install planetterp`).
76

8-
You can find examples of how to use a PlanetTerp object in the **sample_usage.py** file.
7+
### Example usage
98

10-
To use this code to access the API, simply download or clone this repository, and make your own file in the same folder as **PlanetTerp.py** and **sample_usage.py**.
9+
```python
10+
import planetterp
11+
12+
course = planetterp.course(name="MATH140", reviews=True)
13+
courses = planetterp.courses(department="MATH", limit=2)
14+
# replace Jon Snow with the name of any professor
15+
prof = planetterp.professor(name="Jon Snow", reviews="true")
16+
profs = planetterp.professors(type_="ta", limit=2)
17+
# same here
18+
grades = planetterp.grades(course="MATH140", professor="Jon Snow")
19+
20+
print(course)
21+
print(courses)
22+
print(prof)
23+
print(profs)
24+
print(grades)
25+
```

planetterp/PlanetTerp.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import requests
2+
import json
3+
from urllib.parse import urlencode, quote
4+
5+
BASE_URL = 'https://api.planetterp.com/v1/'
6+
7+
def course(name, reviews = False):
8+
"""
9+
Get a course.
10+
11+
Parameters
12+
----------
13+
name: string
14+
The name of the course.
15+
reviews: bool, optional
16+
Whether to also return reviews for the course, specifically reviews for
17+
professors that taught the course and have the course listed as the one
18+
being reviewed. Defaults to False.
19+
"""
20+
params = {"name" : name, "reviews": "true" if reviews else "false"}
21+
url = BASE_URL + "course?" + urlencode(params)
22+
23+
return requests.get(url).json()
24+
25+
26+
def courses(department = None, reviews = False, limit = 100, offset = 0):
27+
"""
28+
Get all courses meeting some criteria.
29+
30+
Parameters
31+
----------
32+
department: string, optional
33+
Only return courses in the given department. The department name must be
34+
four characters. Defaults to all departments.
35+
reviews: bool, optional
36+
Also return reviews for the course, specifically reviews for
37+
professors that taught the course and have the course listed as the one
38+
being reviewed. Defaults to False.
39+
limit: int, optional
40+
Maximum number of courses to return. Must be between 1 and 1000
41+
inclusive. Defaults to 100.
42+
offset: int, optional
43+
Number of courses to skip (offered for pagination). Defaults to 0.
44+
"""
45+
params = {"department" : department,
46+
"reviews": "true" if reviews else "false",
47+
"limit": limit, "offset": offset}
48+
49+
# filter out None args
50+
params = {k:v for k, v in params.items() if v is not None}
51+
url = BASE_URL + "courses?" + urlencode(params)
52+
53+
return requests.get(url).json()
54+
55+
56+
def professor(name, reviews = False):
57+
"""
58+
Get a professor.
59+
60+
Parameters
61+
----------
62+
name: str
63+
The name of the professor.
64+
reviews: bool, optional
65+
Whether to also return reviews for the given professor. Defaults to
66+
false.
67+
"""
68+
params = {"name" : name, "reviews": "true" if reviews else "false"}
69+
url = BASE_URL + "professor?" + urlencode(params)
70+
71+
return requests.get(url).json()
72+
73+
74+
def professors(type_ = None, reviews = False, limit = 100, offset = 0):
75+
"""
76+
Get all professors meeting some criteria.
77+
78+
Parameters
79+
----------
80+
type_: {"professor", "ta"}, optional
81+
Only return reviews for the specified instructor type, either professors
82+
or TAs. Defaults to returning both.
83+
reviews: bool, optional
84+
Whether to also return reviews for the instructors. Defaults to false.
85+
limit: int, optional
86+
Maximum number of instructors to return. Must be between 1 and 1000
87+
inclusive. Defaults to 100.
88+
offset: int, optional
89+
Number of instructors to skip (offered for pagination). Defaults to 0.
90+
"""
91+
if type_ and type_ not in ["professor", "ta"]:
92+
raise ValueError("Expected type to be one of ['professor', 'ta'], got "
93+
+ str(type_))
94+
95+
params = {"type" : type_, "reviews": "true" if reviews else "false",
96+
"limit": limit, "offset": offset}
97+
98+
# filter out None args
99+
params = {k:v for k, v in params.items() if v is not None}
100+
url = BASE_URL + "professors?" + urlencode(params)
101+
102+
return requests.get(url).json()
103+
104+
105+
def grades(course = None, professor = None, semester = None, section = None):
106+
"""
107+
Get grades for a given course, professor, semester, section, or combination
108+
thereof.
109+
110+
Parameters
111+
----------
112+
course: str
113+
Limit the grades returned to only this course. Defaults to all courses.
114+
Either `course` or `professor` must be passed.
115+
professor: str
116+
The name of the professor to return grades for. Defaults to all
117+
professors. Either `course` or `professor` must be passed.
118+
semester: str, optional
119+
The semester to return grades for. This should be passed as the year
120+
followed by the semester code. The spring semester code is 01 and the
121+
fall semester code is 08. For example, 202001 is Spring 2020.
122+
section: str, optional
123+
Limit the grades returned to only this section. Defaults to all
124+
sections.
125+
126+
Notes
127+
-----
128+
Either `course` or `professor` must be passed.
129+
"""
130+
params = {"course" : course, "professor": professor, "semester": semester,
131+
"section": section}
132+
133+
# filter out None args
134+
params = {k:v for k, v in params.items() if v is not None}
135+
url = BASE_URL + "grades?" + urlencode(params)
136+
137+
return requests.get(url).json()

planetterp/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from planetterp.PlanetTerp import (course, courses, professor, professors,
2+
grades)
3+
from planetterp.version import __version__
4+
5+
__all__ = ["course", "courses", "professor", "professors", "grades",
6+
"__version__"]

planetterp/version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "1.0.0"

sample_usage.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

setup.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import setuptools
2+
import re
3+
4+
with open("README.md", "r", encoding="utf-8") as f:
5+
long_description = f.read()
6+
7+
# https://stackoverflow.com/a/7071358
8+
VERSION = "Unknown"
9+
VERSION_RE = r"^__version__ = ['\"]([^'\"]*)['\"]"
10+
11+
with open("planetterp/version.py") as f:
12+
match = re.search(VERSION_RE, f.read())
13+
if match:
14+
VERSION = match.group(1)
15+
else:
16+
raise RuntimeError("Unable to find version string in planetterp/version.py")
17+
18+
19+
setuptools.setup(
20+
name="planetterp",
21+
version=VERSION,
22+
author="PlanetTerp",
23+
author_email="admin@planetterp.com",
24+
description="PlanetTerp API Python wrapper",
25+
long_description=long_description,
26+
long_description_content_type="text/markdown",
27+
url="https://github.com/planetterp/PlanetTerp-API-Python-Wrapper",
28+
packages=setuptools.find_packages(),
29+
classifiers=[
30+
"License :: OSI Approved :: MIT License",
31+
"Operating System :: OS Independent",
32+
],
33+
install_requires=[
34+
"requests"
35+
]
36+
)

0 commit comments

Comments
 (0)