diff --git a/apis/api.py b/apis/api.py index 436f382f..cf836f5c 100644 --- a/apis/api.py +++ b/apis/api.py @@ -117,7 +117,7 @@ def load_from_file(self, fp): reached_dataset = True continue # Each line ends with a new-line character which we strip here - # to make the data easier usable. + # to make the data easier to use. data = line.rstrip().split() # While we are dealing with calendar data the format is simple @@ -296,7 +296,7 @@ def generate_plot(platforms, output_file): ax.bar(ind, values, width, align='center') # Format the X and Y axis labels. Also set the ticks on the x-axis slightly - # farther apart and give then a slight tilting effect. + # farther apart and give them a slight tilting effect. plt.ylabel('Adjusted price') plt.xlabel('Year / Console') ax.set_xticks(ind + 0.3) @@ -424,9 +424,8 @@ def main(): platform['adjusted_price'] = adjusted_price platforms.append(platform) - # We limit the resultset on this end since we can only here check - # if the dataset actually contains all the data we need and therefor - # can't filter on the API level. + # We can't filter on the API level, so we limit the dataset here to + # check if it contains all of the data we need. if opts.limit is not None and counter + 1 >= opts.limit: break counter += 1 diff --git a/website/_containers/apis/2013-02-01-extended.md b/website/_containers/apis/2013-02-01-extended.md index 0cfd43e0..8e2cfedf 100644 --- a/website/_containers/apis/2013-02-01-extended.md +++ b/website/_containers/apis/2013-02-01-extended.md @@ -10,9 +10,9 @@ Examples of APIs being used in real life, and where to go from here. ## In action -* Applications & clients for Twitter, Facebook, Instagram, Flickr, etc all interact with the respective API built for developers -* [Lyft](http://lyft.me) uses [Twilio](http://www.twilio.com/blog/2012/10/lyft-and-twilio.html) to send SMS messagesfor to let passengers know their ride was on its way and to communicate with drivers. -* [Trulia](http://corp.truliablog.com/2008/10/10/social-media-synergy-trulia-integrates-yelp-reviews-into-google-maps/), [Yellow Pages](http://cbsyellowpages.com/), [GrubHub](http://www.grubhub.com) all use [Yelp](http://www.yelp.com/developers/documentation/examples)’s API for integrating reviews into their websites. +* Applications & clients for Twitter, Facebook, Instagram, Flickr, etc all interact with the respective API built for developers. +* [Lyft](http://lyft.me) uses [Twilio](http://www.twilio.com/blog/2012/10/lyft-and-twilio.html) to send SMS messages to let passengers know their ride is on its way and to communicate with drivers. +* [Trulia](http://corp.truliablog.com/2008/10/10/social-media-synergy-trulia-integrates-yelp-reviews-into-google-maps/), [Yellow Pages](http://cbsyellowpages.com/), and [GrubHub](http://www.grubhub.com) all use [Yelp](http://www.yelp.com/developers/documentation/examples)’s API for integrating reviews into their websites. ## Where to go from here diff --git a/website/_containers/apis/2013-02-02-part-4.md b/website/_containers/apis/2013-02-02-part-4.md index 0765d3ba..629bb25d 100644 --- a/website/_containers/apis/2013-02-02-part-4.md +++ b/website/_containers/apis/2013-02-02-part-4.md @@ -14,23 +14,24 @@ When running a python file in the command line, Python has the ability to take a (APIProj)$ python a_python_file.py --debug --output-file ~/MyDocuments/MyLogs/python_file.log ``` -Certainly, we’d need to put some logic into our script in order to be able to parse arguments. Let’s think of all the arguments that we could possibly want to pass to our API Python script are: +Certainly, we’d need to put some logic into our script in order to be able to parse arguments. Let’s think of all the arguments that we could possibly want to pass to our API Python script: * API key for Giantbomb -* path to CPI file +* Path to CPI file * URL to get CPI data (default will be our global `CPI_DATA_URL` we defined earlier) * Path to CSV file which will contain the data output * Path to PNG file which will contain the graphed data output -* Output level for logging (whether just informational, or debugging, etc) +* Output level for logging (whether just informational, debugging, etc) * Maximum number of platforms we want to look at, if any -Python’s standard library has a great module, `argparse` that we’ll use. We’ll create separate parsing function as a helper to our main function that we’ll write out after this. +Python’s standard library has a great module, `argparse` that we’ll use. We’ll create a separate parsing function as a helper to our main function that we’ll write out after this. -First, import argparse, +First, import argparse and os, ```python import argparse +import os ``` then let’s define our parsing function, and use `argparse`’s `ArgumentParser` to initialize a `parser` class: @@ -40,9 +41,9 @@ def parse_args(): parser = argparse.ArgumentParser() ``` -The `ArgumentParser` class implicitly gives us an argument for free, the `-h` and `--help` flags for showing the usage of the script, `python platform_pricing.py [options] [args]`, as well as a list of available commands and their `help` strings we assign. +The `ArgumentParser` class implicitly gives us an argument for free, the `-h` and `--help` flags for showing the usage of the script, `python api.py [options] [args]`, as well as a list of available commands and their `help` strings we assign. -Now we should add all the arguments that could possibly be passed through from the command line with the `add_argument` method that `ArgumentParser` class gives us: +Now we should add all the arguments that could possibly be passed through from the command line with the `add_argument` method that the `ArgumentParser` class gives us: ```python def parse_args(): @@ -54,7 +55,7 @@ def parse_args(): The first parameter that we feed to `add_argument` is the flag that is used in the command line: ```bash -(APIProj)$ python platform_pricing.py --giantbomb-api-key +(APIProj)$ python api.py --giantbomb-api-key ``` We also tell `add_argument` that this is a required field by passing the `required=True` parameter. The `ArgumentParser` class will take care of erroring out for us if that argument isn't given in the command line. @@ -99,7 +100,7 @@ The Python docs have a great [tutorial](http://docs.python.org/2/howto/argparse. ### Main function -The `CPIData` and `GiantbombAPI` classes have been defined with their methods, as well as functions `generate_plot`, `generate_csv`, and `is_valid_dataset`. Let’s now make one `main()` function that runs whenever we call our `platform_pricing.py` file (with arguments) that instantiates (uses) everything. +The `CPIData` and `GiantbombAPI` classes have been defined with their methods, as well as functions `generate_plot`, `generate_csv`, and `is_valid_dataset`. Let’s now make one `main()` function that runs whenever we call our `api.py` file (with arguments) that instantiates (uses) everything. We’ll first want to take care of the arguments passed through the command line by calling our `parse_args` function. @@ -120,7 +121,7 @@ Since we did not specify any file to save our logs to, it will just write our lo Next, we’ll instantiate both the `CPIData` class and the `GiantbombAPI` class and use some of the arguments that we parsed to pass the API key, the CPI URL (if we don't want to use the default defined in our global variable, `CPI_DATA_URL`, and CPI file (if we gave it a file). -We also print to the console – using the print _function_ imported from future (Python 3), rather than the print _keyword_ in Python 2 – that gives a disclaimer to the user what the script will be doing. +We also print to the console – using the print _function_ imported from future (Python 3), rather than the print _keyword_ in Python 2 – that gives a disclaimer to the user about what the script will be doing. ```python # <-- snip --> @@ -158,8 +159,8 @@ We then take our `platforms` list and pass it to either `generate_plot` or `gene field_list=['release_date', 'original_price', 'name', 'abbreviation']): - # Some platforms don't have a release date or price yet. These we have - # to skip. + # Some platforms don't have a release date or price yet. We have to + # skip these. if not is_valid_dataset(platform): continue @@ -171,9 +172,8 @@ We then take our `platforms` list and pass it to either `generate_plot` or `gene platform['adjusted_price'] = adjusted_price platforms.append(platform) - # We limit the resultset on this end since we can only here check - # if the dataset actually contains all the data we need and therefor - # can't filter on the API level. + # We can't filter on the API level, so we limit the dataset here to + # check if it contains all of the data we need. if opts.limit is not None and counter + 1 >= opts.limit: break counter += 1 @@ -196,14 +196,14 @@ if __name__ == '__main__': In your terminal, with your `APIProj` virtual environment activated, and from within your API project directory, try your new script with different arguments (be sure to use your own directory paths instead of `~/Projects/new-coder/apis/*` unless you have that exact directory setup): ```bash -(APIProj)$ python platform_pricing.py --giantbomb-api-key [YOUR_KEY] --plot-file ~/Projects/new-coder/apis/my_plot.png -(APIProj)$ python platform_pricing.py --giantbomb-api-key [YOUR_KEY] --csv-file ~/Projects/new-coder/apis/my_csv.csv -(APIProj)$ python platform_pricing.py --giantbomb-api-key [YOUR_KEY] --plot-file ~/Projects/new-coder/apis/my_plot.png --csv-file ~/Projects/new-coder/apis/my_csv.csv -(APIProj)$ python platform_pricing.py --giantbomb-api-key [YOUR_KEY] --debug --limit 40 --csv-file ~/Projects/new-coder/apis/my_csv.csv +(APIProj)$ python api.py --giantbomb-api-key [YOUR_KEY] --plot-file ~/Projects/new-coder/apis/my_plot.png +(APIProj)$ python api.py --giantbomb-api-key [YOUR_KEY] --csv-file ~/Projects/new-coder/apis/my_csv.csv +(APIProj)$ python api.py --giantbomb-api-key [YOUR_KEY] --plot-file ~/Projects/new-coder/apis/my_plot.png --csv-file ~/Projects/new-coder/apis/my_csv.csv +(APIProj)$ python api.py --giantbomb-api-key [YOUR_KEY] --debug --limit 40 --csv-file ~/Projects/new-coder/apis/my_csv.csv ```
-On a Mac and got a pop-up regarding X11 not installed? Just click “Cancel” - no need to worry. (It may pop up more than once, too). +On a Mac and got a pop-up regarding X11 not installed? Just click “Cancel” - no need to worry (it may pop up more than once, too).
diff --git a/website/_containers/apis/2013-02-03-part-3.md b/website/_containers/apis/2013-02-03-part-3.md index f3082f70..74e527a5 100644 --- a/website/_containers/apis/2013-02-03-part-3.md +++ b/website/_containers/apis/2013-02-03-part-3.md @@ -61,7 +61,7 @@ def generate_plot(platforms, output_file): ax.bar(ind, values, width, align='center') # Format the X and Y axis labels. Also set the ticks on the x-axis slightly - # farther apart and give then a slight tilting effect. + # farther apart and give them a slight tilting effect. plt.ylabel('Adjusted price') plt.xlabel('Year / Console') ax.set_xticks(ind + 0.3) @@ -78,7 +78,7 @@ You can elect to have `plt.show(dpi=72)` instead of `plt.savefig(output_file, dp We can also make a function that takes the yielded data and produces a CSV file for us. -Let’s use a new library, `tablib` to help handle the CSV production. [tablib](http://docs.python-tablib.org/en/latest/), written in Python by the same author of [requests](http://twitter.com/kennethreitz) that allows you to format the output of data into a tabular dataset (among other things). +Let’s use a new library, `tablib` to help handle the CSV production. [tablib](http://docs.python-tablib.org/en/latest/), written in Python by the same author of [requests](http://twitter.com/kennethreitz), allows you to format the output of data into a tabular dataset (among other things). We should add an import statement for it too: @@ -86,7 +86,7 @@ We should add an import statement for it too: import tablib ``` -Similiar to `generate_plot` function, we’ll take two parameters: `platforms` which is the yielded data from our API class, and `output_file` to save the formatted data where we want to. We use the `tablib` module to assign headers to a dataset, then append each item of our platform data to the dataset. Last, we write to a file that passed in as a parameter using the `.csv` method that `tablib` gives us. Comments on process are inline: +Similiar to our `generate_plot` function, we’ll take two parameters: `platforms` which is the yielded data from our API class, and `output_file` to save the formatted data where we want to. We use the `tablib` module to assign headers to a dataset, then append each item of our platform data to the dataset. Lastly, we write to a file that is passed in as a parameter using the `.csv` method that `tablib` gives us. Comments on process are inline: ```python def generate_csv(platforms, output_file): diff --git a/website/_containers/apis/2013-02-04-part-2.md b/website/_containers/apis/2013-02-04-part-2.md index 9115f307..2426eb01 100644 --- a/website/_containers/apis/2013-02-04-part-2.md +++ b/website/_containers/apis/2013-02-04-part-2.md @@ -37,7 +37,7 @@ Next, we’ll define one method (our class’s only method besides our construct This method is a generator function (a clue is the `yield` statement instead of a `return` statement). -Follow the inline comments to undestand the process flow of `get_platforms`: +Follow the inline comments to understand the process flow of `get_platforms`: ```python def get_platforms(self, sort=None, filter=None, field_list=None): @@ -53,7 +53,7 @@ Follow the inline comments to undestand the process flow of `get_platforms`: # The following lines also do value-format conversions from what's # common in Python (lists, dictionaries) into what the API requires. # This is especially apparent with the filter-parameter where we - # need to convert a dictionary of criteria into a comma-seperated + # need to convert a dictionary of criteria into a comma-separated # list of key:value pairs. params = {} if sort is not None: @@ -118,7 +118,7 @@ Follow the inline comments to undestand the process flow of `get_platforms`:
For the Curious
-The yield keyword is similar to return. The get_platforms function, specifically the while incomplete bit, we’ve essentially built a generator (it will generate data on the fly). StackOverflow has a good explanation of what’s happening in our function: The first time the function will run, it will run from the beginning until it hits yield, then it’ll return the first value of the loop. Then, each other call will run the loop you have written in the function one more time, and return the next value, until there is no value to return. The generator is considered empty once the function runs but does not hit yield anymore. It can be because the loop had come to ends, or because you do not satisfy a “if/else” anymore. +The yield keyword is similar to return. In the get_platforms function, specifically the while incomplete bit, we’ve essentially built a generator (it will generate data on the fly). StackOverflow has a good explanation of what’s happening in our function: The first time the function will run, it will run from the beginning until it hits yield, then it’ll return the first value of the loop. Then, each other call will run the loop you have written in the function one more time, and return the next value, until there is no value to return. The generator is considered empty once the function runs but does not hit yield anymore. It can be because the loop had come to ends, or because you do not satisfy an “if/else” anymore.
diff --git a/website/_containers/apis/2013-02-05-part-1.md b/website/_containers/apis/2013-02-05-part-1.md index fe68f418..0558396d 100644 --- a/website/_containers/apis/2013-02-05-part-1.md +++ b/website/_containers/apis/2013-02-05-part-1.md @@ -229,7 +229,7 @@ def load_from_file(self, fp): pass # Each line ends with a new-line character which we strip here - # to make the data easier usable. + # to make the data easier to use. data = line.rstrip().split() # While we are dealing with calendar data the format is simple @@ -284,7 +284,7 @@ def get_adjusted_price(self, price, year, current_year=None): return float(price) / year_cpi * current_cpi ``` -In review, we’ve essentially defined the container, our `CPIData` class, to handle the the processing of our CPI data. We initialize each field for a piece of CPI data in `__init__`, we define how to load data from a given URL (of which we define as a global variable, `CPI_DATA_URL` before we defined our class), we define how to load and parse that data that we just grabbed from the URL and saved, and lastly, we define a method to grab the price for a given year (adjusted if we didn’t grab that specific year from the FRED earlier). +In review, we’ve essentially defined the container, our `CPIData` class, to handle the processing of our CPI data. We initialize each field for a piece of CPI data in `__init__`, we define how to load data from a given URL (of which we define as a global variable, `CPI_DATA_URL` before we defined our class), we define how to load and parse that data that we just grabbed from the URL and saved, and lastly, we define a method to grab the price for a given year (adjusted if we didn’t grab that specific year from the FRED earlier).