Cute python: Dynamically reload

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

One of the great advantages of Python, compared to most other programming languages, is its powerful run-time dynamic capabilities. Thanks to the handy reload () function, we can write a program that is running continuously, but it can load the modified components during the process run (useful for services that are critical for the duration of the run). B This article describes the Run-time program modifications based on some enhancements to the txt2html front-end discussed in previous David's article. In particular, our sample program will perform a background check on the new version of the Txt2html Transformation Library on the Internet, and download and reload the new version required without any manual intervention by the user.

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 ...!

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.