[BangPypers] Favorite tips/techniques

Vineet Naik naikvin at gmail.com
Tue Sep 10 13:36:58 CEST 2013


On Tue, Sep 10, 2013 at 4:48 PM, Saju M <sajuptpm at gmail.com> wrote:

> Hi,
> I have couple of doubts
>
>
> * How do you find "json" has module named "tool" ?.
>
>
> * Why dir(json) not showing "tool" ??
>
> >>> import json
> >>> dir(json)
> ['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__',
> '__doc__', '__file__', '__name__', '__package__', '__path__',
> '__version__', '_default_decoder', '_default_encoder', 'decoder', 'dump',
> 'dumps', 'encoder', 'load', 'loads', 'scanner']
> >>>
>
>
> * How to list all modules of a package in python console ?.
>
>
> * Is this what you guys are using to check modules of a package ?.
>
> ls /usr/lib/python2.7/json/ | grep .py$
> decoder.py
> encoder.py
> __init__.py
> scanner.py
> tool.py
>

Don't know how to get all modules of a package as a list but if you just
need to refer to the package contents, then pydoc gives this info. eg. run,

$ pydoc json

and then search for the section titled "PACKAGE CONTENTS" and you can find
tool listed there along with the other modules in the package.


>
> Regards
> Saju Madhavan
> +91 09535134654
>
>
> On Tue, Sep 10, 2013 at 4:21 PM, Noufal Ibrahim <noufal at nibrahim.net.in
> >wrote:
>
> > Saju M <sajuptpm at gmail.com> writes:
> >
> > > echo '{"name": "Bangpypers", "location": "Bangalore"}' | python -m
> > json.tool
> > >
> > > In this command, what is this "json.tool" ?
> > >
> > > I could not find "tool" in dir(json)
> > >
> > >>>> import json
> > >>>> dir(json)
> > > ['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__',
> > > '__doc__', '__file__', '__name__', '__package__', '__path__',
> > > '__version__', '_default_decoder', '_default_encoder', 'decoder',
> 'dump',
> > > 'dumps', 'encoder', 'load', 'loads', 'scanner']
> >
> > It needn't be.
> > >>> from json import tool
> > >>> tool.__file__
> > '/usr/lib/python2.7/json/tool.pyc'
> > >>>
> >
> > json is a package and tool is one of the modules inside that.
> >
> > To get to this, I have a little shell function that I stole from Anand
> > that has
> > been super useful repeatedly.
> >
> > epy () {
> >         cmd="import $1 as a ; print a.__file__.endswith('.pyc') and
> > a.__file__[:-1] or a.__file__"
> >         file=$(/usr/bin/env python -c $cmd)
> >         echo $file
> >         emacsclient --no-wait $file
> > }
> >
> > If you want to read the source for the json module, just do
> >
> > $ epy json
> >
> > and the json module is loaded into Emacs.
> >
> >
> > [...]
> >
> >
> > --
> > Cordially,
> > Noufal
> > http://nibrahim.net.in
> >
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>



-- 
Vineet Naik


More information about the BangPypers mailing list