Python-based self-description script generation (interesting Program) and python-based self-description

Source: Internet
Author: User

Python-based self-description script generation (interesting Program) and python-based self-description

A self-describing statement refers to a statement that describes itself. (Nonsense ......) For example, the following sentence:
Copy codeThe Code is as follows:
This is a self-describing statement. Besides the DoT number, it contains 125 characters, including 33 ", 29" 2 ", and 5" 3 ", 3 "characters", 3 "5", 2 "1", 2 "it", 2 "packets", and 2 ", 2 "tags", 2 "tags", 2 "tags", 2 "tags", 2 "tags", 2 "tags", 2 "tags ", 2 "yes", 2 "1", 2 "segments", 2 "points", 2 "strokes", and 2 "9 ", 2 "Words", 2 "this", 2 "sentences", 2 "division", 2 "self", and 2 "languages ", 2 "in total", 2 "statements", 2 "signs", and 2 "statements ".

This sentence is generated using a Python script. The generation principle is roughly as follows:

1. Provide a template to let the content of a sentence know where it should appear;
2. Generate sentences based on the current information;
3. Use the current sentence as the input and perform Step 1 again;
4. The information in each part of the sentence is correct.

In short, it is a process of continuous iteration and correction.

It should be noted that during each iteration, only one place should be modified as much as possible to avoid the mutual influence between the two places, resulting in an endless loop. In addition, if there are multiple places in the sentence that need to be corrected, try to randomly select one of them for correction, instead of correcting them in a certain order, to reduce the risk of endless loops.

However, even so, in some cases, it is still possible to fall into an endless loop. For example, if a certain step gets the following sentence:
Copy codeThe Code is as follows:
This sentence contains 3 "2 ".

The above sentence is obviously incorrect, because there are only two "2 ". So, should we change "3" to "2? It is easy to find that if we make such a change, the sentence will become:
Copy codeThe Code is as follows:
This sentence contains 2 "2 ".

At this time, the sentence contains three "2. A sentence like this does not seem to be easy to change to a correct self-describing statement, because it will be in an endless loop no matter how it is changed.

Finally, the Python script used to generate the top self-describing statement is as follows:

#-*-Coding: UTF-8-*-import randomclass SelfDesc (object): ignore_chars = u ",. "" Def _ init _ (self, template): self. template = template self. length = 0 self. detail = "" self. content = "" self. chars = "" self. char_count = {} self. makeContent () self. char_count = self. getCharCount () self. getCharCount () self. makeContent () def _ str _ (self): return self. content def makeContent (self): self. makeDetail () self. content = self. template. replace (u "{length}", u "% d" % self. length ). replac E (u "{detail}", self. detail) self. getChars () def getChars (self): chars = self. content for c in self. ignore_chars: chars = chars. replace (c, "") self. chars = chars return chars def getLength (self): self. length = len (self. chars) def getCharCount (self): d ={} for c in self. chars: if c in self. ignore_chars: continue d. setdefault (c, 0) d [c] + = 1 return d def makeDetail (self): d = self. char_count items = D. items () items. sort (key = lambda x:-x [1]) s = [] for c, n in items: s. append (u "% d" % s "% (n, c) self. detail = u ",". join (s) def correct (self): print "-" * 50 char_count = self. getCharCount () items = char_count.items () random. shuffle (items) for c, n in items: if n <= 1 and c in self. char_count: del self. char_count [c] continue if self. char_count.get (c) = n: continue else: self. char_count [c] = n r Eturn True else: len = self. length self. getLength () if len! = Self. length: return True return False def generate (self): icount = 0 while self. correct (): icount + = 1 self. makeContent () print u "# % d % s" % (icount, self) def main (): template = u "this is a self-described statement, except for the DoT number, it contains {length} characters in total, including {detail }. "Sd = SelfDesc (template) sd. generate () print u" % s "% sdif _ name _ =" _ main _ ": main ()


Ladies and gentlemen, I recently started to learn python. I wrote a very simple pickle operation program, but I am prompted that there is a mistake. My program is as follows:

Use python3.
Use python3 to read the default returned bytes instead of str
(If it seems to be all in English, str is returned, and it is UTF-8 encoded, python3.3 for Windows 64-bit)
If bytes is returned, it needs to be decoded as str.
Bytes. decode ('encoding format ').

Compile a python script to complete the following operations: (1) call a python program and execute the program; (2) Start a shell;

Cd directory

Caller:
Import shell

If _ name __= = "_ main __":
Print 'call'

Shell. startshell ()

The executed program shell. py:

Import OS
Def startshell ():
Likefile = OS. popen ("dir ./")
For I in likefile:
Print I
No debugging, But this is probably the meaning. If there is a returned result, the OS. popen method must be used. The two steps must be placed under the same directory.

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.