Python Forum

Full Version: now i wish Python had a way to add commands
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
now i wish Python had a way to add commands. this would be particularly useful in interactive mode. the idea is that i would not need to type "()" just to run custom stuff in interactive mode.
Great, now I can't lose that thought tonight... Dodgy

There must be some way.. right?
How would you see the concept of adding commands? I was thinking there should be something possible, in a not so elegant way.

def random():
    return 'random stuff'
If you would load/import such functions as above when working interactive. And somehow create variables or something.

a = custom()
Like a said, it is not really added as a command, but maybe there is a way to further implement such functionality?
(Dec-26-2021, 08:56 PM)Jeff900 Wrote: [ -> ]maybe there is a way to further implement such functionality?

how i would design such a thing is have a way to register a new command for the internal command table with a reference to the function to be called when that command is to be run. this could also be done as a special dictionary indexed by command name strings with values being the function to call. the command line would be passed to that function some way (yet to be defined).

def boom():
    exit('**** BOOM ****')
__command_table__['die'] = boom
die
that's just an overly simplistic example.
I think this comes pretty close to what you're asking. First, put this code in a file callednewcommands. Then start the interactive interpreter with this:PYTHONSTARTUP=newcommands python3. Then you can typec.dieorc.lsorc.pwd. Try it Smile and then add all the commands you like.
import os
class Commands :
	@property
	def ls (self) :
		directory_list = os.listdir ()
		for filename in directory_list :
			print (filename)

	@property
	def pwd (self) :
		print (os.getcwd())

	@property
	def die (self) :
		exit ()

c = Commands ()
'''
	c.ls
	c.pwd
	c.die
'''
I think you want the xonsh shell. In this shell you can write both python code and shell commands, and of course you can define your own commands and load whatever code you want at startup.
xonsh looks interesting.

i just installed it from the Ubuntu repository. got version 0.6.0.

the web page does not describe it thoroughly. is it an implementation of the Python language or a user of a specific existing implementation you need to already have such as CPython or a user of any implementation of the Python language?
My xonsh shell uses /usr/bin/python (which is linked to python3) as Python interpreter, in Ubuntu 20.04. So it uses the distribution's Python 3 interpreter. Of course Xonsh contains a specific parser for the xonsh language, which is a mix of python and shell constructs.
~/P/S/2021-12 import sys
~/P/S/2021-12 sys.executable
'/usr/bin/python'
~/P/S/2021-12 echo "Hello xonsh!"
Hello xonsh!
first time i started it, it blew up. now to figure what of my huge shell configuration may have confused it. i to push bash to extremes. also, i'd like to find the etymology of the name.

Output:
lt2a/forums /home/forums 8> xonsh Traceback (most recent call last): File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 20658, in main return main_xonsh(args) File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 20682, in main_xonsh print_welcome_screen() File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 14494, in print_welcome_screen print_color(line) File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 5001, in print_color builtins.__xonsh_shell__.shell.print_color(string, **kwargs) File "/usr/lib/python3/dist-packages/xonsh/ptk/shell.py", line 273, in print_color proxy_style = PygmentsStyle(pyghooks.xonsh_style_proxy(self.styler)) File "/usr/lib/python3/dist-packages/xonsh/pyghooks.py", line 476, in xonsh_style_proxy class XonshStyleProxy(Style): File "/usr/local/lib/python3.6/dist-packages/pygments/style.py", line 122, in __new__ ndef[0] = colorformat(styledef) File "/usr/local/lib/python3.6/dist-packages/pygments/style.py", line 79, in colorformat assert False, "wrong color format %r" % text AssertionError: wrong color format '#ansidarkgreen' Xonsh encountered an issue during launch Failback to /bin/sh \h/\u /home/forums \!> \h/\u /home/forums \!> exit lt2a/forums /home/forums 9>
Yes you cannot use the linux terminal exactly the same way. For example I cannot run in xonsh programs that use a module like pythondialog. I guess one would have problems running curses based programs as well, because the xonsh shell already uses some terminal capabilities.

Start with little configuration at first.
Pages: 1 2