Category Archives: python

Python json.tool module

Python documentation here

Example usage:

$ echo '{"json":"obj"}' | python -mjson.tool
{
    "json": "obj"
}
$ echo '{ 1.2:3.4}' | python -mjson.tool
Expecting property name: line 1 column 2 (char 2)

All argument patterns are:

  • piped json string | python -m json.tool which syntax checks then outputs the results to stdout
  • python -m json.tool input_file.json which reads the path relative file and outputs the results
  • python -m json.tool input_file.json output_file.json only difference here is that the output is directed to the specified file

My thoughts, this could be part of some sort of data validation check, looking for corrupted json static files.

Example

$ echo '{"a":123, "foo":"bar" }' | python -m json.tool && echo "IS valid" || echo "Is not valid"
{
    "a": 123,
    "foo": "bar"
}
IS valid
$ echo '{"a"1:123, "foo":"bar" }' | python -m json.tool && echo "IS valid" || echo "Is not valid"
Expecting : delimiter: line 1 column 4 (char 4)
Is not valid

Python – batteries included

Inspired by this
AND here

I’m going to do my best to either write up some examples of how to use these or link to someone/somewhere else on the
internet where someone did a better job then my grammar handicapped self can

For those to lazy to click the above liniks, the list below is a semi-complete list of command line accessible modules
to perform utility work.

So python -m calendar prints out a pretty calendar of the year, much like the GNU linux cal command line function.

  • json.tool -> pretty prints JSON Examples
  • SimpleHTTPServer -> serve the current directory over HTTP on port 8080 Examples
  • quopri / uu / binhex / base64 -> encode / decode Quoted-Printable / UUEncoded content.
  • telnetlib -> ghetto telnet client
  • filecmp -> directory entry comparison tool
  • ftplib -> ghetto FTP client
  • smtpd -> SMTP proxy
  • timeit -> command line profiling interface. Very handy
  • calendar -> prints year calendar
  • urllib -> ghetto wget Example
  • zipfile -> ghetto info-zip
  • aifc -> dumps some info about the provided aiff file (if given two paths, also copies path1 to path2)
  • cgi -> dumps a bunch of information as HTML to stdout
  • CGIHTTPRequestHandler -> same as SimpleHTTPServer except via the CGIHTTPRequestHandler: it will executes scripts it recognizes as CGI, instead of just sending them over (has not survived the transition to Python 3)
  • compileall -> compiles a tree of Python files to bytecode, has a bunch of options. Does not compile to stripped files (pyo)
  • cProfiler -> runs the provided script file (argument) under cProfiler
  • dis -> disassembles a python script Example
  • doctest -> runs doctests on the provided files (which can be python scripts or doctest files)
  • encodings.rot_13 -> rot13 encodes stdin to stdout (has not survived the transition to Python 3)
  • fileinput -> some kind of ghetto pseudo-annotate. No idea what use that thing might be of
  • formatter -> reformats the provided file argument (or stdin) to stdout: 80c paragraphs &etc
  • gzip -> ghetto gzip (or gunzip with -d), can only compress and decompress, does not delete the archive file
  • htmllib -> strips HTML tags off of an HTML file
  • imaplib -> ghetto IMAP client
  • locale -> displays current locale information
  • mimify -> converts (mail) messages to and from MIME format
  • modulefinder -> lists the modules imported by the provided script argument, and their location
  • pdb scriptfile.py -> automatically enters PDB post-mortem mode if the script crashes
    platform, displays the platform string
  • poplib -> dumps a bunch of info on a POP mailbox
  • profile -> see cProfile
  • pstats -> opens a statistics browser (for profile files)
  • pydoc -> same as the pydoc command Example
  • sgmllib -> see htmllib (as far as I can tell)
  • shlex -> displays tokenization result of the provided file argument (one token per line prefixed with Token:)
  • SimpleXMLRPCServer -> XMLRPC server for power and addition
  • telnetlib -> telnet client
  • tokenize -> dumps tokenization result of a Python file
  • webbrowser -> opens provided URL in your default web browser (options: in a new window, in a new tab)
  • whichdb -> A helpful little tool to try and tell which DB-api driver to use on a specified DB file