cool hit counter Entry point for python code_Intefrankly

Entry point for python code


https://amir.rachum.com/blog/2017/07/28/python-entry-points/

Translate the version as you like. Invasion and deletion.

This post will talk about entry points in python code. As many of you may know, entry points are typically placed in files that are packaged for use as command lines, though few people bother with them. Next, I'll describe how to use the entry point to generate python packages, which can then be used by others in code or at the command line.

Anyone who has installed a python package knows how to use it, which defines information about the package you are developing, and then can be packaged and uploaded to python's package management center for others to download. And the entry point allows the package to be used as a command line tool.

Snek,Inc.

Congratulations! You have just been appointed CEO of "snek semiconductor and software company" and your first job as CEO is to have your employees develop a prototype system for snek, and soon the engineers have done so:

ascii_snek =""" --..,_ _,.--. `'.'. .'`__ o `;__. '.'. .'.'` '---'` ` '.`'--....--'`.' `'--....--'`"""

defmain(): print(ascii_snek)

if__name__ =='__main__': main()

On the day of your company's product launch, you demonstrate this prototype system, which runs smoothly: the

$ python snek.py --..,_ _,.--. `'.'. .'`__ o `;__. '.'. .'.'` '---'` ` '.`'--....--'`.' `'--....--'`

SaaS—Snek as a Service

Unfortunately, the users didn't know how to use python, they just wanted to use Snek's services directly from the named line of their computer, so the engineers worked overtime to make the next version so that Snek automatically generates a console script on installation, so that it can be run from the command line. How do they do it? Just include the package name, dependencies, etc. in the script, like the following.

The following code specifies that the execution should start from the main function of the snek script.

fromsetuptoolsimportsetupsetup( name='snek', entry_points={

'console_scripts': [

'snek = snek:main', ], })

Engineers say that console scripts are special entry points that read the contents of your package and generate certain script files when it is installed by others . Now, let's install it from the source code (you can of course publish it as a python library and let others download and install it using pip):.

$ python setup.py developrunning developrunning egg_infowriting snek.egg-infoPKG-INFOwriting dependency_links to snek.egg-infodependency_links.txtwriting entry points to snek.egg-infoentry_points.txtwriting top-level names to snek.egg-info op_level.txtreading manifest file'snek.egg-infoSOURCES.txt'writing manifest file'snek.egg-infoSOURCES.txt'running build_extCreating c:program files (x86)py36-32libsite-packagessnek.egg-link (link to .)snek0.0.is already the active versionineasy-install.pthInstalling snek-script.py script to C:Program Files (x86)Py36-32ScriptsInstalling snek.exe script to C:Program Files (x86)Py36-32ScriptsInstalling snek.exe.manifest script to C:Program Files (x86)Py36-32ScriptsInstalled c:users achum otebooksProcessing dependenciesforsnek==0.0.Finished processing dependenciesforsnek==0.0.

At the company's annual product meeting, you presented this AMAZING product: the

$ snek --..,_ _,.--. `'.'. .'`__ o `;__. '.'. .'.'` '---'` ` '.`'--....--'`.' `'--....--'`

Snek for Everyone

No one doesn't like Snek, the company had an IPO of over $6 billion. Some advanced users would like to have an advanced version tailored for them. The engineer worked overtime again to make it.

"""Print an ASCII Snek.Usage: snek [--type=TYPE]"""

importdocoptnormal_snek =""" --..,_ _,.--. `'.'. .'`__ o `;__. '.'. .'.'` '---'` ` '.`'--....--'`.' `'--....--'`"""

fancy_snek =""" _,..,,,_ '``````^~"-,_`"-,_ .-~c~-. `~:. ^-. `~~~-.c ; `:. `-, _.-~~^^~:. `. ; _,--~~~~-._ `:. ~. .~ `. .` ;' .:` `: `:. ` _.:-,. `. .' .: :' _.-~^~-. `. `..' .: `. ' : .' _:' .-' `. :. .: .'`. : ; : `-' .:' `. `^~~^` .:. `. ; ; `-.__,-~ ~-. ,' ': '.__.` :' ~--..--' ':. .:' ':..___.:'"""

defget_sneks():

return{

'normal': normal_snek,

'fancy': fancy_snek, }

defmain(): args = docopt.docopt(__doc__) snek_type = args['--type']or'normal'print(get_sneks()[snek_type])

if__name__ =='__main__': main()

They added a deluxe version of Snek which made the premium users especially happy .

$ snek --..,_ _,.--. `'.'. .'`__ o `;__. '.'. .'.'` '---'` ` '.`'--....--'`.' `'--....--'`$ snek --type fancy _,..,,,_ '``````^~"-,_`"-,_ .-~c~-. `~:. ^-. `~~~-.c ; `:. `-, _.-~~^^~:. `. ; _,--~~~~-._ `:. ~. .~ `. .` ;' .:` `: `:. ` _.:-,. `. .'.: :' _.-~^~-. `. `..'.: `.' : .'_:' .-'`. :. .: .'`. : ; : `-'.:' `. `^~~^` .:. `. ; ; `-.__,-~ ~-. ,'': '.__.` :' ~--..--'':. .:'':..___.:'

Snek International Community

Millions of people worldwide use snek, there is a growing demand for different versions of snek, and some professional users of snek (programmers) are demanding that snek be customizable and they want to develop their own version of snek.

"""Print an ASCII Snek.Usage: snek [--type=TYPE]"""

importdocopt

importpkg_resourcesnormal_snek =""" --..,_ _,.--. `'.'. .'`__ o `;__. '.'. .'.'` '---'` ` '.`'--....--'`.' `'--....--'`"""

fancy_snek =""" _,..,,,_ '``````^~"-,_`"-,_ .-~c~-. `~:. ^-. `~~~-.c ; `:. `-, _.-~~^^~:. `. ; _,--~~~~-._ `:. ~. .~ `. .` ;' .:` `: `:. ` _.:-,. `. .' .: :' _.-~^~-. `. `..' .: `. ' : .' _:' .-' `. :. .: .'`. : ; : `-' .:' `. `^~~^` .:. `. ; ; `-.__,-~ ~-. ,' ': '.__.` :' ~--..--' ':. .:' ':..___.:'"""

defget_sneks(): sneks = {

'normal': normal_snek,

'fancy': fancy_snek, }

forentry_pointinpkg_resources.iter_entry_points('snek_types'): sneks[entry_point.name] = entry_point.load()

returnsneks

defmain(): args = docopt.docopt(__doc__) snek_type = args['--type']or'normal'print(get_sneks()[snek_type])

if__name__ =='__main__': main()

They added the snek infrastructure to register other types of snek using the called entry point when the snek is running, so that different versions of snek can be dynamically output on the console.

Specifically, the in iterates over all the snek names registered to the entry point and then adds them to the snek version. So, the developers added a cute version of snek, named

cute_snek =r""" /^/^ _|__| O| / /~ \_/ \____|__________/ \_______ ` | | / / / / / / / / / / _----_ / / _-~ ~-_ | | ( ( _-~ _--_ ~-_ _/ | ~-____-~ _-~ ~-_ ~-_-~ / ~-_ _-~ ~-_ _-~ ~--______-~ ~-___-~"""

How do I find this cute version of snek? Define it like the following.

The code specifies the script to look for cute_snek from.

fromsetuptoolsimportsetupsetup( name='cute_snek', entry_points={

'snek_types': [

'cute = cute_snek:cute_snek', ], })

Then pack as before.

$cdcute_snek && python setup.py developrunning developrunning egg_infowriting cute_snek.egg-infoPKG-INFOwriting dependency_links to cute_snek.egg-infodependency_links.txtwriting entry points to cute_snek.egg-infoentry_points.txtwriting top-level names to cute_snek.egg-info op_level.txtreading manifest file'cute_snek.egg-infoSOURCES.txt'writing manifest file'cute_snek.egg-infoSOURCES.txt'running build_extCreating c:program files (x86)py36-32libsite-packagescute-snek.egg-link (link to .)cute-snek0.0.is already the active versionineasy-install.pthInstalled c:users achumcute_snekProcessing dependenciesforcute-snek==0.0.Finished processing dependenciesforcute-snek==0.0.

Now we'll be able to successfully run this lovely version of snek from the command line, which is dynamically loaded from.

$ snek --type cute /^/^ _|__| O| / /~ \_/ \____|__________/ \_______ ` | | / / / / / / / / / / _----_ / / _-~ ~-_ | | ( ( _-~ _--_ ~-_ _/ | ~-____-~ _-~ ~-_ ~-_-~ / ~-_ _-~ ~-_ _-~ ~--______-~ ~-___-~

Since cute_snek can be loaded dynamically, all types of snek should be able to be loaded dynamically, so the engineers modified the code again to

All snek are loaded from it.

fromsetuptoolsimportsetupsetup( name='cute_snek', entry_points={

'snek_types': [

'cute = cute_snek:cute_snek',

'normal = snek:normal_snek',

'fancy = snek:fancy_snek', ], })

# Also modify the get_sneks() function in the previous file.

defget_sneks():

#sneks = {# 'normal': normal_snek,# 'fancy': fancy_snek,#}# willsnek Modify to empty dictionarysnek = {}

forentry_pointinpkg_resources.iter_entry_points('snek_types'): sneks[entry_point.name] = entry_point.load()

returnsneks

Now repack snek:

$ python setup.py developrunning developrunning egg_infowriting snek.egg-infoPKG-INFOwriting dependency_links to snek.egg-infodependency_links.txtwriting entry points to snek.egg-infoentry_points.txtwriting top-level names to snek.egg-info op_level.txtreading manifest file'snek.egg-infoSOURCES.txt'writing manifest file'snek.egg-infoSOURCES.txt'running build_extCreating c:program files (x86)py36-32libsite-packagessnek.egg-link (link to .)snek0.0.is already the active versionineasy-install.pthInstalling snek-script.py script to C:Program Files (x86)Py36-32ScriptsInstalling snek.exe script to C:Program Files (x86)Py36-32ScriptsInstalling snek.exe.manifest script to C:Program Files (x86)Py36-32ScriptsInstalled c:users achum otebooksProcessing dependenciesforsnek==0.0.Finished processing dependenciesforsnek==0.0.

Great job!!!

$ snek --..,_ _,.--. `'.'. .'`__ o `;__. '.'. .'.'` '---'` ` '.`'--....--'`.' `'--....--'`$ snek --type fancy _,..,,,_ '``````^~"-,_`"-,_ .-~c~-. `~:. ^-. `~~~-.c ; `:. `-, _.-~~^^~:. `. ; _,--~~~~-._ `:. ~. .~ `. .` ;' .:` `: `:. ` _.:-,. `. .'.: :' _.-~^~-. `. `..'.: `.' : .'_:' .-'`. :. .: .'`. : ; : `-'.:' `. `^~~^` .:. `. ; ; `-.__,-~ ~-. ,'': '.__.` :' ~--..--'':. .:'':..___.:'$ snek --type cute /^/^ _|__| O| / /~ \_/ \____|__________/ \_______ ` | | / / / / / / / / / / _----_ / / _-~ ~-_ | | ( ( _-~ _--_ ~-_ _/ | ~-____-~ _-~ ~-_ ~-_-~ / ~-_ _-~ ~-_ _-~ ~--______-~ ~-___-~

At this point, you probably know how to use the entry point in python.


Recommended>>
1、Xduncom acquired by terminal for 115000
2、iOS Project Project Development Environment Setup
3、Visualizing Random Samples of Probability Density Functions and Distributions
4、Label control with HTML text support in iOS MDHTMLLabel
5、Java implementation of maximum entropy

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号