Python's Template usage guide

Source: Internet
Author: User

Tag: Returns the beginning of the pattern span pre number expression test Python

This article mainly explains the use of template in Python and the use of skills, very practical, the need for friends can refer to the following:

Template is undoubtedly a good thing, can be the format of the string to be fixed, re-use. The template can also allow developers to consider the format of the string and its contents, virtually reducing the pressure on developers.

The template belongs to a class in string, so you can call it in the following ways if you want to use it

1  from Import Template

The template has a special identifier of $, which has the following rules:

Its main implementation is $xxx, where xxx is a string that satisfies the python naming rules, i.e. cannot start with a number, cannot be a keyword, etc.

If $xxx needs to be in contact with other strings, use {} to wrap xxx (previously it seems to be using ' () ', as I wrote in a reference book, but the current version should only use ' {} '). For example, AAA${XXX}AAA

There are two important methods in the template: Substitute and Safe_substitute.

Both of these methods can return a string by getting a parameter

1>>s=template (There $a and$b)2>>PrintS.subtitute (a='Apple', b='Banana')3There Apple andBanana4>>PrintS.safe_substitute (a='Apple', b='Banbana')5There Apple andBanbana


You can also pass data directly by getting a dictionary, like this

1  and $b) 2 >>d={'a':'apple','B '  ':'banbana'}3 >>print S.substitute (d) 4  and Banbana

The difference between them is how they are handled when the parameters are missing.

The template is implemented by first initializing a string with a template. These strings contain a key. By calling substitute or Safe_subsititute, the key value corresponds to the parameter passed in the method, which allows the string to be imported at the specified location. One benefit of this approach is that the order of each parameter must be fixed, as long as the key is correct and the value is inserted correctly, in a way like print '%s '. In this way, you can take a breath of relief when you insert a lot of data. But even if there is such a lazy way, still can not guarantee that there is no error, if key less input a how to do?

Substitute is a serious method, if there is no input key, it will be an error. Although it will be difficult to see, but can find the problem.

Instead of Safe_substitute the error, the $xxx is entered directly into the result string, as

1  and $b

The advantage of this is that the program is always right, without being overwhelmed by mistakes.

The template can be inherited, and its subclasses can perform some ' personalization ' operations ...

You can change the $ character to another character, such as "#" by modifying the Delimiter field, but the new identifier needs to conform to the specification of the regular expression.

By modifying the Idpattern, you can modify key naming conventions, for example, to specify that the first character must begin with a, which is good for canonical naming. Of course, this is achieved by means of regular expressions.

1  fromStringImportTemplate2 classMyTemplate (Template):3delimiter ="#"4Idpattern ="[a][_a-z0-9]*"5 defTest ():6s='#aa is not #ab'7t=MyTemplate (s)8b=='AA':'Apple','AB':'Banbana'}9   PrintT.substitute (d)Ten if __name__=='__main__': OneTest ()

Python's Template usage guide

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.