Real-time tagging of Project exercise notes for python

Source: Internet
Author: User
This article mainly introduces the real-time tagging project exercise notes for python. This article is a hands-on practice project for reading the book "Basic python tutorial, for more information, see the practice behind "Basic python tutorial". Write the code as needed. On the one hand, you can familiarize yourself with the python code method, on the other hand, you can practice basic and non-basic syntaxes in python to make it easier.

This project was relatively simple at the beginning, but it was complicated after reconstruction, but it was more flexible.

According to the book, the restructured program is divided into four modules: processing program module, filter module, rules (in fact, processing rules), and syntax analyzer.

First, the processing program module has two functions: one is to provide the output of fixed html tags (each tag has a start and end ), the other is to provide a friendly access interface for the start and end of the output. Let's take a look at the program handlers. py:

The Code is as follows:


Class Handler:
'''
'''
Def callback (self, prefix, name, * args ):
Method = getattr (self, prefix + name, None)
If callable (method): return method (* args)
Def start (self, name ):
Self. callback ('start _ ', name)
Def end (self, name ):
Self. callback ('end _ ', name)
Def sub (self, name ):
Def substitution (match ):
Result = self. callback ('sub _ ', name, match)
If result is None: match. group (0)
Return result
Return substitution

Class HTMLRenderer (Handler ):
'''

'''
Def start_document (self ):
Print'...'
Def end_document (self ):
Print''
Def start_paragraph (self ):
Print'

'
Def end_paragraph (self ):
Print'

'
Def start_heading (self ):
Print''
Def end_heading (self ):
Print''
Def start_list (self ):
Print'
    '
    Def end_list (self ):
    Print'
'
Def start_listitem (self ):
Print'
  • '
    Def end_listitem (self ):
    Print'
  • '
    Def start_title (self ):
    Print''
    Def end_title (self ):
    Print''
    Def sub_emphasis (self, match ):
    Return' % S'% Match. group (1)
    Def sub_url (self, match ):
    Return '% s' % (match. group (1), match. group (1 ))
    Def sub_mail (self, match ):
    Return '% s' % (match. group (1), match. group (1 ))
    Def feed (self, data ):
    Print data

    This program is the cornerstone of the entire "project": Provides tag output and string replacement. It is easy to understand.

    Let's take a look at the second module "filter". This module is simpler. It is actually a regular expression string. The related code is as follows:

    The Code is as follows:


    Self. addFilter (R' \ * (. + ?) \ * ', 'Emphasa ')
    Self. addFilter (R' (http: // [\. a-z0-9A-Z/] +) ', 'url ')
    Self. addFilter (R '([\. a-zA-Z] + @[\. a-zA-Z] + [a-zA-Z] +) ', 'mail ')

    These are the three filters: The card filter (marked by X), the url filter, and the email filter. Those familiar with regular expressions do not have pressure to understand.

    Let's take a look at the "Rules" of the third module. In this module, let alone the grandfather class. The other classes should have two methods: condition and action, the former is used to determine whether the read string conforms to its own rules, and the latter is used to perform operations. The so-called execution operation refers to calling the "processing program module ", output the front tag, content, and back tag. Let's take a look at the code of this module. In fact, the relationship between several classes in this module will be clearly illustrated in the class diagram. Rules. py:

    The Code is as follows:


    Class Rule:
    Def action (self, block, handler ):
    Handler. start (self. type)
    Handler. feed (block)
    Handler. end (self. type)
    Return True

    Class HeadingRule (Rule ):
    Type = 'heading'
    Def condition (self, block ):
    Return not '\ n' in block and len (block) <= 70 and not block [-1] = ':'

    Class TitleRule (HeadingRule ):
    Type = 'title'
    First = True

    Def condition (self, block ):
    If not self. first: return False
    Self. first = False
    Return HeadingRule. condition (self, block)

    Class ListItemRule (Rule ):
    Type = 'listitem'
    Def condition (self, block ):
    Return block [0] = '-'
    Def action (self, block, handler ):
    Handler. start (self. type)
    Handler. feed (block [1:]. strip ())
    Handler. end (self. type)
    Return True

    Class ListRule (ListItemRule ):
    Type = 'LIST'
    Inside = False
    Def condition (self, block ):
    Return True
    Def action (self, block, handler ):
    If not self. inside and ListItemRule. condition (self, block ):
    Handler. start (self. type)
    Self. inside = True
    Elif self. inside and not ListItemRule. condition (self, block ):
    Handler. end (self. type)
    Self. inside = False
    Return False

    Class ParagraphRule (Rule ):
    Type = 'paragraph'
    Def condition (self, block ):
    Return True

    Supplement utils. py:

    The Code is as follows:


    Def line (file ):
    For line in file: yield line
    Yield '\ N'

    Def blocks (file ):
    Block = []
    For line in lines (file ):
    If line. strip ():
    Block. append (line)
    Elif block:
    Yield ''. join (block). strip ()
    Block = []

    Finally, let's take a big look at the "syntax analyzer module". The role of this module is to coordinate the relationship between the read text and other modules. Specifically, two lists are provided for storing "rules" and "filters". The advantage of this is that the flexibility of the entire program is greatly improved, the hot swapping method that changes rules and filters into, of course, this is also due to the fact that each type of rule (filter) is written into a class separately when writing rules and filters, instead of using if .. else. Check the Code:

    The Code is as follows:


    Import sys, re
    From handlers import *
    From util import *
    From rules import *

    Class Parser:
    Def _ init _ (self, handler ):
    Self. handler = handler
    Self. rules = []
    Self. filters = []

    Def addRule (self, rule ):
    Self. rules. append (rule)

    Def addFilter (self, pattern, name ):
    Def filter (block, handler ):
    Return re. sub (pattern, handler. sub (name), block)
    Self. filters. append (filter)

    Def parse (self, file ):
    Self. handler. start ('document ')
    For block in blocks (file ):
    For filter in self. filters:
    Block = filter (block, self. handler)
    For rule in self. rules:
    If rule. condition (block ):
    Last = rule. action (block, self. handler)
    If last: break
    Self. handler. end ('document ')

    Class BasicTextParser (Parser ):
    Def _ init _ (self, handler ):
    Parser. _ init _ (self, handler)
    Self. addRule (ListRule ())
    Self. addRule (ListItemRule ())
    Self. addRule (TitleRule ())
    Self. addRule (HeadingRule ())
    Self. addRule (ParagraphRule ())

    Self. addFilter (R' \ * (. + ?) \ * ', 'Emphasa ')
    Self. addFilter (R' (http: // [\. a-z0-9A-Z/] +) ', 'url ')
    Self. addFilter (R '([\. a-zA-Z] + @[\. a-zA-Z] + [a-zA-Z] +) ', 'mail ')

    Handler = HTMLRenderer ()
    Parser = BasicTextParser (handler)

    Parser. parse (sys. stdin)

    The processing idea in this module is to traverse all the rules and filters inserted by the client (that is, the program execution entry) to process the read text.

    There is a detail to mention, in fact, it is in line with the previous writing, that is, when traversing the rule, the condition is called to determine whether it meets the current rule.

    I think this program is like the command line mode. If you have time, you can review this mode to maintain the robustness of the memory network node.

    Finally, let's talk about what I thought was the purpose of this program:

    1. It is used for code highlighting and analysis. If it is rewritten to the js version, you can create an online code editor.
    2. It can be used for learning and Writing blog posts.

    There are other ideas for you to stay informed.
    Adding a class chart is simple, but it should be able to describe the relationship between them. In addition, it is recommended that you draw a picture by yourself if you cannot see the unclear relationship between the codes so that you can familiarize yourself with the entire structure.

    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.