The dynamic encapsulation of Python tutorial _python

Source: Internet
Author: User
Tags html to text line web urlencode python script

Let's sketch the plot of this article: Suppose you want to run a process on the local machine, and some of the program logic is in another place. Let's make a special assumption that this program logic will be updated periodically, and you want to use the latest program logic when you run the process. There are many ways to meet the requirements just mentioned, and this article will show you several of them.

As the "lovely Python" column continues, the ongoing enhancements to my common domain utility txt2html are discussed. The utility converts the smart ASCII text file into HTML. Previous articles discussed the WEB Proxy version of the utility and the curses interface of the utility. Again, I occasionally notice that some ASCII tags can be converted in a more efficient way, or that an error is resolved in the processing of a particular tag structure.

In fact, the articles in this column are written in ASCII and then converted into HTML format that you can read during the editing process. Before I published my draft, I ran a program similar to this:
Command line HTML for the article

txt2html charming_python_7.txt > charming_python_7.html

If you prefer, I can specify some flags to modify the operation, but in any case, the latest version of the converter is actually 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 cumbersome: please visit my website to compare the version number and file date (sometimes the changes are 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 resources later in this article.) )

The above process includes several steps that require manual operation and are time-consuming. It should be simpler and can do this.
command line WEB access

Most people think of the Web as a way of interactively browsing pages in a GUI environment. That's fine, of course, but there are also many features on the command line. A system with a text-mode Web browser Lynx can consider the entire web as another set of files used by command-line tools. For example, I find that some commands are useful:
Using Lynx for command line Web browsing

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

The first line says: "Display the homepage of David Mertz (in ASCII text) to the console." The second line says, "Save the ASCII version of IBM's current DeveloperWorks home page to a file." "The third line example says:" Displays the number of words in the David home page. (without worrying about details, it shows only command-line tools that are combined with pipelines.) )

With regard to lynx, one thing to note about it (when using the-dump option) is to perform almost exactly the opposite of txt2html: The former tool converts HTML to text; the latter tool converts to another format. But there is no reason not to use txt2html as popular as lynx. You can do this with a very short Python script:
' 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, just do the following:

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 if necessary. You can transport and redirect output as you would with any command-line tool. However, in the previous version, only data files that could be reached by URLs could be processed, and local files could not be processed.

In fact, fetch_txt2html.py can accomplish a task that Lynx cannot accomplish (txt2html itself cannot): It takes not only the data source from the URL, but also the program logic remotely. If you use fetch_txt2html.py, you do not have to install txt2html on the local machine, you will use remote calls (with the latest version), and you will send the results back, just as if you were running a local process. Isn't it great? A local version of txt2html can access a remote URL just as it would access a local file, but it does not guarantee that it is up to date ...!

Dynamic initialization

Using fetch_txt2html.py ensures that the latest program logic is always used in transformations. However, another thing that this method can do is to transfer the processor (and memory) requirements to the Gnosis.cx Web server. The load on this particular process is not particularly high, but it is likely that other types of processes processed on the client are more efficient and satisfying.

The way organizations txt2html-that is, how most programs are organized-is a core flow control function that is provided by a variety of utility functions. In particular, these utility functions are frequently updated functions, and core functions (main () and some other functions) are changed only when a major rewrite is made. All in all, the utility functions are effectively updated every time the program is run. 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 bloc KS by Type #def titleify (blocks): #def authorify (block): # ... [More blocks massaging functions] ... #--Utility functions for text transformation #def adjustcaps (TXT): #def capwor DS (TXT): #def urlify (TXT): def typographify (TXT): # [module] Names R = Re.compile (r "" ' ([\ \s ' /">]|^" \[(. *?) \] ([<\s\.\),:; ' "?! /-]) "" ", Re. M | Re. 
  S) txt = r.sub (' \\1<em><code>\\2</code></em>\\3 ', txt) # *strongly emphasize* words R = Re.compile (r "" "([\ \s '/"]|^) \* (. *?) \* ([\s\.\),:; ' "?! /-]) "" ", Re. M | Re. S) txt = r.sub (' \\1<strong>\\2</strong>\\3 ', txt) # ... [More text massaging] ... return txt # ... 
[More text transformation functions] ... 

The

To use the latest and most specific support modules requires some preparation steps. First, download the main txt2html module to the local system (this is a one-time step). Second, create a Python script on the local system similar to the following example:
' dyn_txt2html.py ' command-line converter

 from dmtxt2html import * # import the ' txt2html ' code From urllib import urlopen import sys # Check for updated functions (fail gracefully if n 
    OT 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 cu 
     rrently 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.arg V[1:] Main (cfg_dict) else:print "Please specify URL (with options) for txt2html conversion" 

In the dyn_txt2html.py script, note that when the FROM T2h_textfuncs import * statement is executed, all functions previously defined in dmtxt2html (such as typographify ()) will be made by the T2h_textfuncs version This function is replaced with the same name. Of course, if the T2h_textfuncs function is commented out, it will not be replaced.

One small problem to note is that different systems handle write STDERR in different ways. In a UNIX-like system, the STDERR can be redirected when the script is run, but STDERR messages are appended to the console output in the current OS/2 shell and Windows/dos. You may want to write the above error/warning to a log file, or just be used to directing STDOUT to a file (which may be more useful). For example:
The ' dyn_txt2html ' Command Guild session

g:\txt2html> python dyn_txt2html.py test.txt > test.html cannot currently download txt2html updates


Errors go to the console, and the converted output is transferred to the file.

A more interesting thing is dyn_txt2html.py why not download the entire dmtxt2html module and download only the support module. Of course there is a reason for that. The T2h_textfuncs support module is much smaller than the main dmtxt2html module, especially since most functions have been truncated/commented out. On a modem connection, it is significantly faster. But the download size is not the main reason.

For txt2html, it doesn't matter if the user automatically downloads the entire latest module. But what happens when the program logic is a distributed system (especially where the maintenance responsibility is distributed)? You may have Alice, Bob, and Charlie responsible for modules Funcs_a, Funcs_b, and Funcs_c respectively. Each of them makes periodic (and independent) changes to the functions they are responsible for, and uploads the latest and best versions to their own sites (such as http://alice.com/Funcs_A.py). In this case, it is not feasible to have three programmers change the same master module. However, you can extend a script similar to dyn_txt2html.py to try to import funcs_a, Funcs_b, and Funcs_c at startup (if these resources are not available, it will fall back to the Mainprog version).

Long-running dynamic processes

To date, the tools we have studied have obtained dynamic program logic by downloading updated resources at initialization time. This makes sense for command-line processing or batch processing, but also for long-running applications. This long-running application is most likely the server process that is constantly responding to client requests. In this case, however, we will use the curses_txt2html.py developed for the previous article to illustrate the Python reload () function. Program Curses_txt2html is a wrapper for dmtxt2html local replicas. This is not the second time to mention curses programming. Curses_txt2html provides a set of interactive menus to configure and run multiple consecutive txt2html transformations is sufficient.

Curses_txt2html can run in the background all the time, and when switching to its session and running the transformation, we want it to be able to use the latest program logic. For this particular simple example, shutting down and restarting the application is not difficult and does not cause special damage. But this can easily be reminiscent of other processes that have been running (possibly a process that describes the state of action performed in a session).

In this article, a new File/update submenu is added. When it is activated, only the new function update_txt2html () is invoked. In addition to the curses calls that are related to providing the acknowledgement that occurred, we have seen these steps in the 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, 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://gnos is.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, "do
  Wnload of updated [dmtxt2html] Module failed! ") Reload (dmtxt2html) S.addstr (4, 2, "Module [dmtxt2html] Reloaded from current directory") S.refresh () c = s.getc H () s.erase () 

There are two important differences between the dyn_txthtml.py and the update_txt2html () functions. One of the differences is to continue the operation and import the main dmtxt2html module instead of just importing the support functions. This is primarily a simplification of the import. The problem here is that we use the import dmtxt2html to access the module, not from dmtxt2html import *. This is a more secure process in many ways, but the result is that it becomes more difficult to overwrite the functions in dmtxt2html (either unintentionally or intentionally). If we are attaching a function from D2h_textfuncs, you must perform Dir () on the imported support module and attach the member as an attribute to the "dmtxt2html" namespace. Performing this style of overlay is left to the reader's practice.

The main difference that the update_txt2html () function brings is the use of the built-in reload () function of Python. Only the new import dmtxt2html will not overwrite functions that were previously imported. Please pay close attention to this point! Many beginners believe that the Re-import module will update the in-memory version. This is wrong. In fact, the method of updating the memory image of a function in a module is the reload () module.

Another little trick has been performed in the previous example. The download location of the update dmtxt2html module is the local working directory, which may or may not be the originally mounted dmtxt2html directory. In fact, if it's in the Python Library directory, then you may not be using it in that directory (perhaps without user permission for it). However, the reload () call attempts to mount from the current directory before attempting the remainder of the Python path. So, whether or not the download succeeds, reload () should be a safe operation (although it may be loaded into a new module, it may not be loaded).

Related Article

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.