JSON Strict Mode

Chris Rebert clp2 at rebertia.com
Fri Aug 5 02:19:44 EDT 2011


On Thu, Aug 4, 2011 at 8:25 PM, John Riselvato <jdriselvato at gmail.com> wrote:
> I am working on a license verification script. I am rather new to the
> concept and to JSON files in general.

Based on your questions, reading a programming tutorial might be a
good idea. Here's one that uses Python and that I've heard praise for:
http://openbookproject.net/thinkcs/python/english3e/
The official tutorial is also worth reading:
http://docs.python.org/dev/tutorial/

Once you understand Python's basic datatypes, JSON is a breeze; they
are extremely similar.

> This is what my pseudocode looks like:
>>
>> licenses = meta['license']
>> for x in licenses:
>>     if licenses[x]['terms'] is not valid opensource license

I would probably instead write that as:

for license_name, license in licenses.items():
    if not is_open_source(license['terms']):
# rest same as before

>>         if in strict mode
>>             raise nonfree exception
>
> How would i go about checking if its in "strict mode"

You would have a boolean* variable somewhere indicating whether or not
the program is in strict mode. Its value would presumably be set based
on user input, or a configuration file, or whatever is applicable to
your program; e.g.:
strict = True
strict = False

config = parse_configuration_file()
if config.strict:
    # whatever

> and what does "raise nonfree exception" mean?

I regret that the Wikipedia article on the topic of exceptions isn't
too approachable. So, I'll instead refer you to the relevant section
of the Python tutorial:
http://docs.python.org/dev/tutorial/errors.html#errors-and-exceptions

(Disclaimer: The following is oversimplified and somewhat Python-specific.)
In a nutshell, an exception is type of object** used to indicate that
a program has encountered an error; it contains details about the
error and its cause.
When a program encounters an error, it stores information about the
error into a new exception object and then "raises" (using, in Python,
the "raise" statement) this object so that the program can properly
react to the error. By virtue of the "raise", normal execution of the
program stops, and control is transferred to another part of the
program which previously registered itself as being capable of
handling the kind of error in question. The code in that part of the
program then examines the exception object that was raised and takes
appropriate action to deal with the error. If no part of the program
registered itself as capable of handling the kind of error in
question, a detailed error message is output and the program exits.

In your specific example, the non-pseudo-code would look something like:

# a declaration somewhere:
class NonFreeLicenseError(ValueError):
    """License is not free according to the Open Source Definition."""

# ... back in the part of your code corresponding to your pseudocode:
if strict:
    raise NonFreeLicenseError("The '%s' license is nonfree and thus
impermissible." % x)

As written, I have NonFreeLicenseError as a kind of ValueError. This
may or may not be appropriate depending on your program; a list of
built-in types of exceptions can be found at
http://docs.python.org/dev/library/exceptions.html#bltin-exceptions

Cheers,
Chris
--
http://rebertia.com

* http://en.wikipedia.org/wiki/Boolean_data_type
** http://en.wikipedia.org/wiki/Object_%28computer_science%29



More information about the Python-list mailing list