<p dir="ltr"><br>
On Apr 18, 2016 11:04 AM, "Koos Zevenhoven" <<a href="mailto:k7hoven@gmail.com">k7hoven@gmail.com</a>> wrote:<br>
><br>
> On Mon, Apr 18, 2016 at 10:12 AM, Nick Coghlan <<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>> wrote:<br>
> > On 18 April 2016 at 05:28, Koos Zevenhoven <<a href="mailto:k7hoven@gmail.com">k7hoven@gmail.com</a>> wrote:<br>
> >><br>
> >> So here's oneline.py:<br>
> >><br>
> >> <a href="https://gist.github.com/k7hoven/21c5532ce19b306b08bb4e82cfe5a609">https://gist.github.com/k7hoven/21c5532ce19b306b08bb4e82cfe5a609</a><br>
> >><br>
> ><br>
> > Neat, although you'll want to use importlib.import_module() rather than<br>
> > calling __import__ directly (the latter won't behave the way you want when<br>
> > importing submodules, as it returns the top level module for the import<br>
> > statement to bind in the current namespace, rather than the imported<br>
> > submodule)<br>
> ><br>
><br>
> Thanks :). That is in fact why I had worked around it by grabbing<br>
> sys.modules[name] instead. Good to know import_module() already does<br>
> the right thing. I now changed the code to use import_module, assuming<br>
> that is the preferred way today. However, to prevent infinite<br>
> recursion when importing submodules, I now do a setattr(parentmodule,<br>
> submodulename, None) before the import (and delattr if the import<br>
> fails).<br>
><br>
> >><br>
> >> I suppose this could be on pypi, and one could do things like<br>
> >><br>
> >>     oneline.py "random.randint(0,10)"<br>
> >><br>
> >> or<br>
> >><br>
> >>     python -m oneline "random.randint(0,10)"<br>
> >><br>
> >> Any thoughts?<br>
> ><br>
> ><br>
> > There are certainly plenty of opportunities to make Python easier to invoke<br>
> > for one-off commands. Another interesting example is pyp:<br>
> > <a href="https://code.google.com/archive/p/pyp/wikis/pyp_manual.wiki">https://code.google.com/archive/p/pyp/wikis/pyp_manual.wiki</a><br>
><br>
> This is nice, although solves a different problem.<br>
><br>
> > A completely undocumented hack I put together while playing one day was a<br>
> > utility to do json -> json transformations via command line pipes:<br>
> > <a href="https://bitbucket.org/ncoghlan/misc/src/default/pycall">https://bitbucket.org/ncoghlan/misc/src/default/pycall</a><br>
><br>
> So it looks like it would work like this:<br>
><br>
> cat input.json | pycall "my.transformation.function" > output.json<br>
><br>
> Also a different problem, but cool.<br>
><br>
> > The challenge with these kinds of things is getting them from "Hey, look at<br>
> > this cool thing you can do" to "This will materially improve your day-to-day<br>
> > programming experience". The former can still be fun to work on as a hobby,<br>
> > but it's the latter that people need to get over the initial adoption<br>
> > barrier.<br>
><br>
> I think the users of oneline.py could be people that now write lots of<br>
> bash scripts and work on the command line. So whenever someone asks a<br>
> question somewhere about how to do X on the linux command line, we<br>
> might have the answer: """<br>
><br>
> Q: On the linux commandline, how do I get only the filename from a<br>
> full path that is in $FILEPATH<br>
><br>
> A: Python has this. You can use the tools in os.path:<br>
><br>
> Filename:<br>
> $ oneline.py "os.path.basename('$FILEPATH')"<br>
><br>
> Path to directory:<br>
> $ oneline.py "os.path.dirname('$FILEPATH')"<br>
> """</p>
<p dir="ltr">FILEPATH='for'"example');"'subprocess.call("cat /etc/passwd", shell=True)'</p>
<p dir="ltr">><br>
> This might be more appealing than python -c. The whole point is to<br>
> make Python's power available and visible for a larger audience.<br>
><br>
> -Koos<br>
> _______________________________________________<br>
> Python-ideas mailing list<br>
> <a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
> Code of Conduct: <a href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a><br>
</p>