程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

An environment that can execute both shell and python

編輯:Python

xonsh It's based on Python cross-platform Unix Shell Language and command prompt .

The language is Python 3.6+ Superset , With other shell The original language .

xonsh Is a shell language and command prompt . And others Shell Different ,xonsh be based on Python, And added other Syntax , These syntax make it possible to invoke subprocess commands , It's easy to manipulate the environment and work with file systems .xonsh The command prompt gives the user interactive access to xonsh Language .


One 、 install :

pip install xonsh[full]

Two 、 Basic grammar :

xonsh Language based Python, for instance :

>>> 1 + 1
2

Use... From here >>> The prefix indicates any xonsh Input . It follows Python Appointment , It also helps to highlight the deception syntax ,( The difference in shell Use $

Because it's also Python, Therefore, the module can be imported , Print values and use other built-in Python function :

>>> import sys
>>> print(sys.version)
3.4.2 |Continuum Analytics, Inc.| (default, Oct 21 2014, 17:16:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]

We can also create and use text data types , for example int,float, list , Collections and dictionaries

>>> d = {'xonsh': True}
>>> d.get('bash', False)
False

xonsh shell Multi line input is also supported , For more advanced flow control . As long as the first line of input itself is syntactically invalid , It will automatically enter the multiline mode . When the cursor is in the first column , If the Enter( or Return), Exit the multiline mode .

>>> if True:
... print(1)
... else:
... print(2)
...
1

Flow control, of course, includes loops .

>>> for i, x in enumerate('xonsh'):
... print(i, x)
...
0 x
1 o
2 n
3 s
4 h

You can also define and call functions and classes .

>>> def f():
... return "xonsh"
...
>>> f()
'xonsh'

To simplify indenting ,Shift + Tab Enter 4 A space . About this part , Please see the “ Basic knowledge of ” part . It's like Python.

3、 ... and 、 environment variable

environment variable $ Write after name . for example , $HOME,$PWD, and $PATH.

>>> $HOME
'/home/snail'

It can be set as Python Set as any other variable in ( And export ) environment variable . The same goes for deleting them .

>>> $GOAL = 'Become the Lord of the Files'
>>> print($GOAL)
Become the Lord of the Files
>>> del $GOAL

2.1 The environment itself ${...}

All environment variables exist in the built-in ${...}(aka __xonsh__.env) In the map . You can access this mapping directly , But in most cases , You don't need to .

for example , If you want to check whether environment variables exist in the current session :

>>> 'HOME' in ${...}
True

To get information about specific environment variables , have access to help() Method .

>>> ${...}.help('XONSH_DEBUG')

A useful method on ${...} yes swap(). It can be used to temporarily set environment variables :

>>> with ${...}.swap(SOMEVAR='foo'):
... echo $SOMEVAR
...
...
foo
>>> echo $SOMEVAR
>>>

2.1.1 Be careful :

stay bash,$NAME and ${NAME} Grammatically equivalent . stay xonsh in , They have different meanings .

We can put any effective Python The expression is enclosed in curly braces in ${<expr>}. then , The result of this expression will be used to find the value in the environment . Here are some practical examples :

>>> x = 'USER'
>>> ${x}
'snail'
>>> ${'HO' + 'ME'}
'/home/snail'

3、 ... and 、 Run the command

As shell,xonsh The purpose of is to make it easy and interesting to run commands . Running the subprocess command should be like any other shell In the same job .

>>> echo "Yoo hoo"
Yoo hoo
>>> cd xonsh
>>> ls
build docs README.rst setup.py xonsh __pycache__
dist license scripts tests xonsh.egg-info
>>> dir scripts
xonsh xonsh.bat
>>> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: docs/tutorial.rst
no changes added to commit (use "git add" and/or "git commit -a")
>>> exit

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved