JSON Strict Mode

Chris Rebert clp2 at rebertia.com
Sat Aug 6 00:45:21 EDT 2011


> On Fri, Aug 5, 2011 at 2:19 AM, Chris Rebert <clp2 at rebertia.com> wrote:
>> 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.
<snip>
>> > 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"
<snip>
>> > and what does "raise nonfree exception" mean?
<snip>
>> 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

On Fri, Aug 5, 2011 at 3:44 PM, John Riselvato <jdriselvato at gmail.com> wrote:
> Thanks for this mate.I took what you said and made this.
>>>
>>>         licenseCheck = Syn.policy.metafile.LICENSE_CLEAN
>>>         licenses = meta['license']
>>>         for i in licenses:
>>>                 if licenses[i]['terms'] in licenseCheck == False:

More conventionally written:
        if licenses[i]['terms'] not in licenseCheck:

For that matter, I would rename `licenseCheck` to something clearer
like `acceptableLicenses`.

>>>                         try:
>>>                                 raise Exception(licenses[i]['terms'])
>>>                         except Exception as inst:
>>>                                 errors = errors + 1
>>>                                 Syn.log.l(Syn.log.CRITICAL, "Doesn't meet
>>> License requirement!!!")
>>>                                 Syn.log.l(Syn.log.CRITICAL, "License %s
>>> is **NOT** marked as clean!!" % license[i]['terms'])

It's pretty pointless to catch the exception right after you raise it
like this. Exceptions are intended to communicate the error to the
caller, which you're not doing here, so they may not be necessary.
Also, one typically raises a more specific exception than just
Exception, which is the most general and vague exception possible; see
my prior post for an example of using a custom exception class.

> the licenseCheck main infromation you need to know is:
>>>
>>> LICENSE_CLEAN = [
>>> "GPL",
>>> "GPL-1",
>>> "GPL-2",
>>> "GPL-3",
>>> "X11",
>>> "MIT",
>>> "PSFL-2"
>>> ]

If you have a recent version of Python, I'd recommend using a set instead:

LICENSE_CLEAN = set([
    "GPL",
    "GPL-1",
    # etc...
])

This is more conceptually accurate and should be faster too.

> The JSON file information is set up practically like this:
>>
>> "license" : {
>>         "*" : {
>>            "terms"  : "GPL-3",
>>            "author" : "Joe Shmo, et. al"
>>        }
>>     }
>>
>> Is what i programmed close to what I was trying to explain?

More or less. If "*" is the only entry in "license", then the for-loop
is unnecessary and you can just use reference licenses['*']['terms']
(and similar) directly; one would need a specification of the your
data format (or at least more examples) to be sure. And your use of
exceptions doesn't really match the pseudocode, although your
underlying logic is entirely reasonable, even moreso if you were to
not use exceptions.

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



More information about the Python-list mailing list