Python dynamic re-encapsulation tutorial

Source: Internet
Author: User
Tags line web
This article mainly introduces the Python dynamic re-encapsulation tutorial. This article is from the IBM official Developer Documentation. if you need a friend, refer to it. let's describe the plot of this article: assume that you want to run a process on the local machine, while some program logic is in another place. Let's assume that the program logic is updated from time to time, and you want to use the latest program logic when running the process. There are many methods that can meet the requirements just mentioned. This article will explain several of them to you.

As the "cute Python" column continues, we have discussed the ongoing enhancements to my public domain utility Txt2Html. The utility converts a "smart ASCII" text file into HTML. Previous articles discussed the Web proxy version of the utility and the curses interface of the utility. Similarly, I occasionally noticed that some ASCII tags can be converted in a more effective way, or an error is solved in processing a special tag structure.

In fact, the articles in this column are all written in ASCII, and then converted to the HTML format that you can read during the editing process. Before publishing a draft article, I ran a program similar to the following:
HTML-based command line of the article

txt2html charming_python_7.txt > charming_python_7.html

If you want to, I can specify some flag to modify the operation. However, in fact, the latest version of the converter is in my local drive and path. If you are working on another machine or for readers who want to use the utility, the process is troublesome: Please visit my website, note that compare the version number and file date (sometimes the change is too small, I will not change the version number), download the current version, copy the current version to the correct directory, and then run the command line converter. (See references later in this article .)

The above process includes several steps that require manual operation and are time-consuming. It should be simpler and can be achieved.
Command line Web access

Most people think that Web is a method of interactive page browsing in the GUI environment. This is certainly good, but there are also many functions in the command line. The lynx system of the Web browser with text mode can regard the entire Web as another file set used by the command line tool. For example, some commands are useful:
Use lynx for command line Web browsing

lynx -dump http://gnosis.cx/publish/.lynx -dump http://ibm.com/developerworks/. > ibm_developer.txtlynx -dump http://gnosis.cx/publish | wc | sed "s/( *[0-9]* *\)\([0-9]*\)\(.*\)/\2/g"

The first line says, "Display David Mertz's homepage (in ASCII text) to the console ." The second line says, "Save the ASCII version of IBM's current developerWorks homepage to a file ." The third line shows the number of words on the David homepage ." (Do not worry about details. it only displays the command line tool that is used with the pipeline .)

About lynx, note that it (when the-dump option is used) performs operations that are almost the opposite of Txt2Html: The previous tool converts HTML into text; the next tool is converted to another format. However, there is no reason not to use Txt2Html, which is as popular as lynx. You can use a very short Python script to complete this operation:
'Fetch _ txt2html. py' command line converter

 import sys from urllib import urlopen, urlencode if len(sys.argv) == 2:  cgi = 'http://gnosis.cx/cgi/txt2html.cgi'  opts = urlencode({'source':sys.argv[1], 'proxy':'NONE'})  print urlopen(cgi, opts).read()       else:  print "Please specify URL for Txt2Html conversion"

To run this script, you only need to perform the following operations:

python fetch_txt2html.py http://gnosis.cx/publish/programming/charming_python_7.txt

This does not provide you with all the switches for local Txt2Html processing, but it is also easy to add them if necessary. The output can be delivered and redirected just like any command line tool. However, in the above version, only data files that can be reached by URLs can be processed, rather than local files.

In fact, fetch_txt2html.py can complete tasks that cannot be completed by lynx (neither can Txt2Html itself): It not only retrieves data sources from URLs, but also remotely obtains program logic. If fetch_txt2html.py is used, you do not have to install Txt2Html on the local machine. remote calls (using the latest version) are processed and the results are sent back, just like running a local process. Great, right? The local version of Txt2Html can access remote URLs, just like accessing local files, but it cannot guarantee that it is the latest ......!

Dynamic initialization

The use of fetch_txt2html.py ensures that the latest program logic is always used in the conversion. However, another thing this method can accomplish is to transfer the processor (and memory) requirements to the gnosis. cx Web server. The load of this special process is not very high, but it is likely that other types of processes processed on the client will be more effective and satisfactory.

The way to organize Txt2Html-that is, the way to organize most programs-is to use some core traffic control functions provided by various practical functions. In particular, these utility functions are frequently updated functions. core functions (main () and some other functions) are changed only when major rewriting is performed. All in all, a utility function is effectively updated when each program is running. In fact, in most cases, most of the functions in the main Txt2Html module dmTxt2Html are enough.
'D2h _ textfuncs. py' dynamic Txt2Html update

"""Hot-pluggable replacement functions for Txt2Html"""      #-- Functions to massage blocks by type #def      Titleify(block):    #def Authorify(block):    # ... [more block massaging functions] ... #-- Utility functions for text transformation #def AdjustCaps(txt):    #def capwords(txt):    #def URLify(txt):    def Typographify     (txt):      # [module] names   r = re.compile(r    ""'([\(\s'/">]|^)\[(.*?)\]([<\s\.\),:;'"?!/-])""" , re.M | re.S)  txt = r.sub(    '\\1\\2\\3' ,txt)      # *strongly emphasize* words   r = re.compile(r    ""'([\(\s'/"]|^)\*(.*?)\*([\s\.\),:;'"?!/-])""" , re.M | re.S)  txt = r.sub(    '\\1\\2\\3' , txt)      # ... [more text massaging] ...           return           txt    # ... [more text transformation functions] .....

Some preparation steps are required to use the latest and most specific support modules. First, download the main Txt2Html module to the local system (this is a one-time step ). Next, create a Python script similar to the following example on the local system:
'Dyn _ txt2html. py' command line converter

from      dmTxt2Html     import      *       # Import the body of 'Txt2Html' code     from      urllib     import      urlopen    import      sys    # Check for updated functions (fail gracefully if not fetchable)     try     :  updates = urlopen(    'http://gnosis.cx/download/t2h_textfuncs.py' ).read()  fh = open(    't2h_textfuncs.py' ,     'w' )  fh.write(updates)  fh.close()    except     :  sys.stderr.write(    'Cannot currently download Txt2Html updates' )    # Import the updated functions (if available)     try     :      from      t2h_textfuncs     import      *    except     :  sys.stderr.write(    'Cannot import the updated Txt2Html functions' )    # Set options based on runmode (shell vs. CGI)     if      len(sys.argv) >= 2:  cfg_dict = ParseArgs(sys.argv[1:])  main(cfg_dict)    else     :      print"Please specify URL (and options) for Txt2Html conversion"

In the dyn_txt2html.py script, note that when the from t2h_textfuncs import * statement is executed, all previously defined functions (such as Typographify () in dmTxt2Html will be replaced by functions of the same name in t2h_textfuncs. Of course, if the t2h_textfuncs function is commented out, it will not be replaced.

Note that different systems handle data written to STDERR in different ways. In UNIX-like systems, STDERR can be redirected when a script is run. However, in the current OS/2 shell and Windows/DOS, STDERR messages are appended to the console for output. You may want to write the above errors/warnings to the log file, or just get used to redirecting STDOUT to the file (which may be more useful ). For example:
'Dyn _ txt2html 'command line session

G:\txt2html> python dyn_txt2html.py test.txt > test.htmlCannot currently download Txt2Html updates

The error is returned to the console. the converted output is returned to the file.

One more interesting thing is why dyn_txt2html.py does not download the entire dmTxt2Html module, but only the support module. Of course, this is justified. The t2h_textfuncs support module is far smaller than the main dmTxt2Html module, especially because most functions have been deleted/commented out. The modem connection speed is much faster. But the download size is not the main reason.

For Txt2Html, it does not matter if the user automatically downloads the entire latest module. But what happens if the program logic is a distributed system (especially the maintenance responsibility is also distributed? You may have Alice, Bob, and Charlie take charge of the Funcs_A, Funcs_ B, and Funcs_C modules respectively. Each of them makes regular (and independent) changes to the functions they are responsible for, and uploads the latest and best versions to their own websites (such as http://alice.com/Funcs_A.py ). In this case, it is not feasible for all three programmers to change the same master module. However, the script similar to dyn_txt2html.py can be directly extended to try to import Funcs_A, Funcs_ B, and Funcs_C at startup (if these resources cannot be obtained, they will be returned to the MainProg version ).

Long-running dynamic process

So far, the tools we have studied have obtained dynamic program logic by downloading and updating resources during initialization. This makes sense for command line processing or batch processing, but for long-running applications. This long-running application is most likely a server process that constantly responds to client requests. However, in this case, we will use curses_txt2html.py developed for the previous article to describe the reload () function of Python. The program curses_txt2html is the encapsulation of the local copy of dmTxt2Html. This is not the second time to mention curses programming. it is enough to talk about curses_txt2html that provides a set of interactive menus to configure and run multiple consecutive Txt2Html conversions.

Curses_txt2html can always run in the background. when switching to its session and running the conversion, we hope it can use the latest program logic. For this specific simple example, closing and restarting the application is not difficult and will not cause special damage. However, it is easy to think of other processes that are running all the time (it may be the processes that indicate the operation status in the session ).

In this article, a new File/Update sub-menu is added. When it is activated, only the new function update_txt2html () is called (). In addition to the curses call related to the provided validation, we have seen these steps in other examples in this article:
'Curses _ txt2html. py' dynamic update function

def update_txt2html     ():      # Check for updated functions (fail gracefully if not fetchable)   s = curses.newwin(6, 60, 4, 5) s.box()  s.addstr(1, 2,     "* PRESS ANY KEY TO CONTINUE *" , curses.A_BOLD) s.addstr(3,2,     "...downloading..." ) s.refresh()      try     :        from      urllib     import      urlopen    updates = urlopen(    'http://gnosis.cx/download/dmTxt2Html.py' ).read()    fh = open(    'dmTxt2Html.py' ,     'w' )    fh.write(updates)    fh.close() s.addstr(3,2,     "Module [dmTxt2Html] downloaded to current directory" )      except     : s.addstr(3,2,     "Download of updated [dmTxt2Html] module failed!" )  reload(dmTxt2Html)  s.addstr(4, 2,     "Module [dmTxt2Html] reloaded from current directory " ) s.refresh() c = s.getch()   s.erase()

There are two important differences between dyn_txthtml.py and update_txt2html () functions. One difference is to continue the operation and import the main dmTxt2Html module, instead of supporting functions. This mainly simplifies the import. The problem here is that we use import dmTxt2Html to access the module, rather than from dmTxt2Html import *. In many ways, this is a safer process, but the result is that it is more difficult (whether unintentional or intentional) to overwrite functions in dmTxt2Html ). If you want to attach a function from d2h_textfuncs, you must execute dir () on the imported support module and attach the member to the "dmTxt2Html" namespace as an attribute. The overwrite of this style is reserved for readers.

The main difference between the update_txt2html () function is the usage of the built-in reload () function in Python. Executing a new import dmTxt2Html statement will not overwrite the previously imported function. Please pay close attention to this! Many beginners think that the re-import module will update the version in memory. This is wrong. In fact, the method for updating the memory image of the function in the module is the reload () module.

In the above example, another tips are also executed. The download location of the updated dmTxt2Html module is the local working directory, which may or may not be the directory where the original dmTxt2Html is loaded. In fact, if it is in the Python library directory, you may not use it in this directory (maybe there is no user permission for it ). However, the reload () Call tries to load data from the current directory first, and then tries the rest of the Python path. Therefore, whether the download is successful or not, reload () should be a safe operation (although it may load new modules or not ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.