Use the python decorator to verify the configuration file example

Source: Internet
Author: User
A WriteData function is used in the project to save the configurations entered by the user. in order to verify the user input requirements, the python modifier is used without affecting the interface usage, the code snippet demonstrates how to verify that the input parameters of the WriteData function call different verification function check inputs according to different configuration files. You can change the logic of the verification function as needed.

The code is as follows:


Def VerifyData (func ):

Def VerifyInt (data ):
Assert (int (data)> 0)

Def VerifyString (data ):
Assert (len (data)> 10)

Def inner (* args, ** kvargs ):

Print args
Print kvargs

Assert (len (args)> 1)
If args [1] = "int.txt ":
VerifyInt (args [0])
Elif args [1] = "string.txt ":
VerifyString (args [0])

Func (* args, ** kvargs)

Return inner

@ VerifyData
Def WriteData (text, filepath ):
Print "WriteData"
Print text
Print filepath

WriteData ("1234567890", "int.txt ")

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.