Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python2 ---- function using dictionaries     - Let CentOS6 yum upgrade to support more source rpm package (Linux)

- Macro Analysis Performance: PHP Performance Analysis and Experiment (Programming)

- Understanding Linux firewall Iptables (Linux)

- Linux signal and orphans, and zombie process (Programming)

- GitLab remote backup of Linux Shell Scripting (Linux)

- CentOS of NFS (Server)

- Oracle 11g RMAN cross-platform transfer table space (Database)

- Using open source software to build XWiki Wiki system installed within the company (Linux)

- Using Vagrant create cross-platform development environment (Linux)

- Vim useful plugin: EasyGrep (Linux)

- Single-node Hadoop installation notes distributed pseudo & (Server)

- To install Docker under Ubuntu 15.04 (Linux)

- shell script: a key optimization CentOS system (Linux)

- DM9000 bare Driver Design (Programming)

- osprofiler use OpenStack Cinder Lane (Server)

- Linux crontab (Linux)

- Linux environment to configure Apache + Django + wsgi (Server)

- Upgrade to Node V4 seven reasons (Programming)

- Physical structure and process disk IO (Linux)

- Several start-up mode of Tomcat (Server)

  Python2 ---- function using dictionaries
  Add Date : 2017-08-31      
  Background to the issue:

Recently for sublime2 written in python2 a plug-in, which has a command function, is to enter a different command invokes different functions, but python does not support the switch, only ifelse if feeling particularly low and clearly not good design: If you are new add a command, it is necessary to finish first and then later add functions ifelse inside his party, not only increased the workload of the code, and is not well-structured design, so I think the python function name __name__, can not use the function name automatically find the appropriate function to call it?

The general idea is to use getattr, will be installed to function inside the class, like this:
>>> Class testClass ():
... Def funcA (self, name):
... Print 'funcA' + name
... Def funcB (self):
... Print 'funcB'
>>> T = testClass ()
>>> A = getattr (t, 'funcA', None)
>>> B = getattr (t, 'funcB', None)
>>> A ( 'name')
>>> B ()

-------------------------------------------------- ------------------------------

This can be easily factory pattern, but doing so is that we do not have a malpractice can dynamically manage all commands and some command points to the same function, this function also makes management becomes very easy, and the need for class management, document each update, we have repeated to modify the contents of the document, so the thought of another python powerful features: dictionaries
Dictionary can be stored functions, so only you need to find all the values in the dictionary together with the powerful function __doc__ can be convenient for function and document maintenance, and easy to find in the dictionary functions easier and more flexible to function operating
Specific examples of code is as follows:
def commandA (name):
 'CommandA is for testA'
 print name

def commandB ():
 'CommandB is for testB'
 print 'hello world'
command = { 'commandA': commandA, 'commandB': commandB}

command [ 'commandA'] ( 'testA')
command [ 'commandB'] ()

def show_doc ():
 for func in command.values ():
  print func .__ doc__

def commandf (commandstr, params = []):
 func = command [commandstr]
 if None = func!:
  func (params)

-------------------------------------------------- ------------------------------

Of course, this is not perfect, because you want to manually maintain a dict, but compared to the trouble of the document, it has been much better, but there is a very important point is that such functions do not write self parameter, a lot shorter ......
- Category prevent DoS attacks against Linux (Linux)
- Talk about the Linux ABI compatibility Application (Linux)
- Getting Started with Linux system to learn: how to install USB webcams come in raspberry (Linux)
- SSH without password (Linux)
- Linux --- manual release system cache (Linux)
- Linux, modify / etc / profile file (Linux)
- CentOS iptables firewall enabled (Linux)
- CentOS5 installation Nodejs (Linux)
- The Rabbitmq installation under CentOS 6.4 (Linux)
- Install Java, Maven, Tomcat under Linux (Linux)
- Learning C ++ Standard Template Library and data structures (Programming)
- Java synchronization mechanism: synchronized, wait, notify (Programming)
- Restore Oracle Database Cold backup and database reconstruction emca (Database)
- Ansible installation configuration and simple to use (Server)
- iptraf: A Practical TCP / UDP network monitoring tools (Linux)
- C ++ why we chose to use the smart pointer (Programming)
- 30 minutes with your Quick Start MySQL Tutorial (Database)
- Linux for enterprises to build a firewall (Linux)
- Git remote repository Explanation (Linux)
- Upgrading Oracle to (Database)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.