Skip to content

Instantly share code, notes, and snippets.

@prathamesh-sonpatki
Forked from satran/github-issues.py
Created April 29, 2013 06:08
Show Gist options
  • Save prathamesh-sonpatki/5479953 to your computer and use it in GitHub Desktop.
Save prathamesh-sonpatki/5479953 to your computer and use it in GitHub Desktop.
# 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