Created
April 29, 2013 05:33
-
-
Save satran/5479865 to your computer and use it in GitHub Desktop.
The small program used for the talk at Pune Python User Group meetup on April 2013.
Please feel free to update this script and make a pull request.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Packages and modules are imported to the scope using the import statement. | |
# If you wanted just a particular object from a module/package you can use | |
# the from <module> import <object> | |
import sys | |
import requests | |
# There are no constants in Python, still we can use a naming convention to | |
# imply that to the user of a library. | |
BASE_URL = "https://api.github.com" | |
# Functions are defined by the def keyword. | |
def get_issues(user, repository): | |
""" | |
Returns the latest 20 issues for the given repository. | |
""" | |
# Remember every thing is an object in Python. Objects have quite useful | |
# methods like the join in the following statement for the String object. | |
url = '/'.join([BASE_URL, "repos", user, repository, "issues"]) | |
response = requests.get(url) | |
if response.json: | |
return response.json | |
def display_issues(issues, details=["number", "title"]): | |
""" | |
Prints the given details of the list of issues. | |
""" | |
print("\t".join(details)) | |
for issue in issues: | |
# Hmmm, that is a complex statement. Break it down and you will see | |
# how list-comprehensions simplyfy things. A list comprehension can | |
# be created by [value for value in iterable] | |
print ("\t".join([str(issue[key]) for key in details])) | |
if __name__ == "__main__": | |
# Handling exceptions in Python. Note catching exceptions can be used | |
# for control flow in Python. | |
try: | |
user = sys.argv[1] | |
repository = sys.argv[2] | |
display_issues(get_issues(user, repository)) | |
except IndexError: | |
print("Usage: github-issues.py <user> <repository>") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment