[BangPypers] Favorite tips/techniques

Saju M sajuptpm at gmail.com
Tue Sep 10 13:18:24 CEST 2013


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

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
>


More information about the BangPypers mailing list