cli tool to print value, similar to pydoc

Hi, what do you think about adding a tool to print a specified object, similar to pydoc shows its help? I regularly find myself checking the contents of variables to get a quick grasp on what the value looks like during development, and then type something like this: python -c 'import os; print(os.pathsep)' # or even python >>> import os >>> os.pathsep other examples may be MODULE.__version__, sys.path, or sys.platform, but really this happens a lot for a lot of different things. It would be nice to just type, e.g. any of: pyval os.pathsep python -m pprint os.pathsep python -p os.pathsep pydoc --value os.pathsep There is already a tool like this on PyPI [1] (sadly py2 only atm), but if you agree that this is a common pattern, I believe it would be a lot more useful to have it in the stdlib. [1] https://pypi.org/project/pyeval/ Additional considerations: It might be useful to add an option to pass a format specification, which would then do the equivalent of: print(format(value, spec)) Note that [1] is even more powerful, because it can directly evaluate expressions, e.g.: pyeval 'math.sin(math.pi/5)' This can be really useful at times, but maybe the base version would be enough for a first step. What do you think? Best, Thomas

On Sun, Apr 07, 2019 at 01:42:46PM +0200, Thomas Gläßle wrote:
It would be nice to just type, e.g. any of:
pyval os.pathsep
How will it know what object os is, without guessing, if you haven't imported it?
There is already a tool like this on PyPI [1] (sadly py2 only atm), but if you agree that this is a common pattern,
I always have at least one REPL open for precisely this sort of thing, and the interactive interpreter is infinitely more flexible and powerful than a tool to print one value. -- Steven

Steven D'Aprano wrote on 4/8/19 4:35 AM:
How will it know what object os is, without guessing, if you haven't imported it?
Like pydoc/help does it. I assume it splits by dots and imports the longest importable subsplit (try...except ImportError in a loop), then iteratively getattrs the rest. I admit that there is a little ambiguity here in that you could also import the shortest possible subsplit such that the rest of the name can be resolved (which is definitely not what help does). In practice it wouldn't be different in most cases, but if you're concerned about the ambiguity, one could separate module and attribute part by colon, like with entry points, e.g. "os:pathsep".
Sure, but I find that most uses are covered by either pydoc, or printing a value, and more complex snippets are often better tried by putting it into a script file - which makes it easier to make small modifications and then re-execute. Furthermore, often a single REPL is not enough, since you may want to see the value in different python interpreters or environments. In any case, it is surely not "needed" per se, but merely convenience. Same applies for the default-usage of pydoc, and still I tend pydoc a lot over opening a REPL and calling help(), because it combines two or three steps into one.

On Sun, 7 Apr 2019 at 12:44, Thomas Gläßle <t_glaessle@gmx.de> wrote:
There's another tool on PyPI, I think, that does all you mention and more. I can't recall its name now (it's short, one letter or two, which makes for easy command line use, but sucks for searching :-() but as a data point I thought it was a really cool idea, but in practice I never used it and no loner install it when I use Python. So I don't know that it's a sufficiently useful idea in practice to be worth going in the stdlib. But if you can find that project again, and it has a reasonable number of users, it may be that I'm wrong in my assessment. Paul

Could it be q <https://pypi.org/project/q/> that you are thinking about? Op ma 8 apr. 2019 om 09:10 schreef Paul Moore <p.f.moore@gmail.com>:

On Mon, 8 Apr 2019 at 08:30, Daniel Bradburn <moagstar@gmail.com> wrote:
Could it be q that you are thinking about?
Thanks, but no - that's debug output in your code. The tool I recall was much more like the OP's suggestion
foo sys.version 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
Auto-import of sys, print the value by default, etc. Plus quite a bit more i think. Paul

On Sun, Apr 07, 2019 at 01:42:46PM +0200, Thomas Gläßle wrote:
It would be nice to just type, e.g. any of:
pyval os.pathsep
How will it know what object os is, without guessing, if you haven't imported it?
There is already a tool like this on PyPI [1] (sadly py2 only atm), but if you agree that this is a common pattern,
I always have at least one REPL open for precisely this sort of thing, and the interactive interpreter is infinitely more flexible and powerful than a tool to print one value. -- Steven

Steven D'Aprano wrote on 4/8/19 4:35 AM:
How will it know what object os is, without guessing, if you haven't imported it?
Like pydoc/help does it. I assume it splits by dots and imports the longest importable subsplit (try...except ImportError in a loop), then iteratively getattrs the rest. I admit that there is a little ambiguity here in that you could also import the shortest possible subsplit such that the rest of the name can be resolved (which is definitely not what help does). In practice it wouldn't be different in most cases, but if you're concerned about the ambiguity, one could separate module and attribute part by colon, like with entry points, e.g. "os:pathsep".
Sure, but I find that most uses are covered by either pydoc, or printing a value, and more complex snippets are often better tried by putting it into a script file - which makes it easier to make small modifications and then re-execute. Furthermore, often a single REPL is not enough, since you may want to see the value in different python interpreters or environments. In any case, it is surely not "needed" per se, but merely convenience. Same applies for the default-usage of pydoc, and still I tend pydoc a lot over opening a REPL and calling help(), because it combines two or three steps into one.

On Sun, 7 Apr 2019 at 12:44, Thomas Gläßle <t_glaessle@gmx.de> wrote:
There's another tool on PyPI, I think, that does all you mention and more. I can't recall its name now (it's short, one letter or two, which makes for easy command line use, but sucks for searching :-() but as a data point I thought it was a really cool idea, but in practice I never used it and no loner install it when I use Python. So I don't know that it's a sufficiently useful idea in practice to be worth going in the stdlib. But if you can find that project again, and it has a reasonable number of users, it may be that I'm wrong in my assessment. Paul

Could it be q <https://pypi.org/project/q/> that you are thinking about? Op ma 8 apr. 2019 om 09:10 schreef Paul Moore <p.f.moore@gmail.com>:

On Mon, 8 Apr 2019 at 08:30, Daniel Bradburn <moagstar@gmail.com> wrote:
Could it be q that you are thinking about?
Thanks, but no - that's debug output in your code. The tool I recall was much more like the OP's suggestion
foo sys.version 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
Auto-import of sys, print the value by default, etc. Plus quite a bit more i think. Paul
participants (4)
-
Daniel Bradburn
-
Paul Moore
-
Steven D'Aprano
-
Thomas Gläßle