Wednesday, July 31, 2013

Reading from a JSON Dictionary

Dictionaries are useful data structures, and at their core, are a set of one-to-one mappings of data.

A loop and append pattern should work fine for getting the data into the dictionary.

(Of course using the JSON library would greatly simplify this process.)

from urllib.request import urlopen

html = urlopen("")

theDataArray = str('},{')

dictionary = {}

for i in range(100):

      dictionary[theDataArray[i].split(',')[1].split('"')[3]] = theDataArray[i].split(',')[2].split('"')[3]

That last line might look like a lot, but it's mostly just parsing away all the commas and other characters to get at the two pieces of information we're going for, key and name.

This will also take in only 100 strains, for all change to 100 to len(theDataArray), just remember to properly parse the data to avoid errors.

Parsing in Python

Let's make sense of this pile of data.

Reading and parsing data are the first two building blocks of a larger process, compiling, so it's always useful to know a couple of good methods.

A quick Google search of "python reading data" got me to, for some simple reference material.

Now, testing is always a fun way to find out what's going on behind the code, so let's see if we can just pull out a bit of the important data here so we don't overwhelm ourselves.

Now, continuing from before, lets convert the data object to a useful string/array.

>> theData = str(',')

Now let's see what this looks like.

>> for i in range(5):

         print(i, theData[i])

This smaller data structure will come in handy when setting up our larger program design.

Here's the result:

0 b'{"Key":"100-og"

1 "Name":"$100 OG"

2 "Category":"Hybrid"

3 "Symbol":"100"

4 "Abstract":"$100 OG is a hybrid cannabis strain that originally became popular in Southern California. It produces large flowers and a potent high."

Now, wait, your probably thinking, what's that b'{ doing there?

This data is meant to be parsed with the use of json modules, not bare bones python split statements, and that should massively simplify the process.

Although; you may have to redesign your parsing algorithm to completely remove all persisting characters and strings.

Monday, July 29, 2013

Reading API Data

In Python, this is as easy as running a couple of commands in IDLE.

from urllib import urlopen

html = urlopen("") 

HTTPResponse instances also have some more methods and attributes, which can be found here:

This can be additionally simplified by importing and using the json library.

Introduction to APIs

An important part of almost every web app is reading, parsing, and selecting data.

The process of doing this with a public API, can be a simple task.

Okay, hold on now, what's an API?

First, I's say do a quick read through of, but if TL;DR...

An API is an Application Programming Interface, which in simple terms is both a collection of data and commonly a means of manipulating it.

An easy way to think about APIs is that they are an interface that unifies how applications communicate with other applications.

So let's try accessing Leafly's wonderful API strain collection.

"Let's talk to the data!"

That's a good view of some of the data that's available to access.

Now here's a view of what it looks like to your code which will process it:

Well, that's clearly more convoluted... So whats really going on here?

If you took some of that and nicely spaced it out, these are what you would be left with.
    "Name":"Afghan Kush",
    "Abstract":"Originating from the Hindu Kush mountains of Afghanistan...",
    "TopActivity":"Relax at home"},
    "Abstract":"A great afghani kush cannabis strain that is mostly sativa...",
    "TopActivity":"Relax at home"