Python programming to achieve automatic layout of the St code in codesys (1)

Source: Internet
Author: User

CodeSys is often used for programming, so it is unavoidable to look at other people's code.

Some codes do not have the concept of code indent at all, while some code is too indented, and there is no pattern, and it looks dizzy.

It brings a lot of trouble to check bugs.

 

There are a lot of similar code formatting software on the Internet, and there is no shortage of excellent products, but it cannot be found to support the ST language in CodeSys. (A friend of mine told me !)

I am learning Python, so I tried to write a program to implement automatic code layout.

This Code only implements indentation of IF, ELSE, ELSIF, and END_IF. Being studied...

 

The implementation of the code is very simple, and the method is also stupid...

Step 1: Use the strip function in the string module to delete spaces at the beginning and end of each row

Step 2: Calculate which line of code contains keywords such as IF, ELSE, ELSIF, and END_IF. Different indentation is performed based on different keywords.

It's just an idea. Later versions will go home later to continue researching... O (∩ _ ∩) O ~

 

 

 1 # coding: gb2312
2 import string
3 text = open ("test1.txt", 'R ')
4 text1 = text. readline ()
5 lst1 = []
6 #---------------------------------
7 # Delete spaces at the beginning and end of a row
8 while (text1! = ''):
9 # print text1
10 lst1.append (string. strip (text1 ))
11 text1 = text. readline ()
12
13 indent = 0
14 for I in lst1:
15 if I [: 3] = 'if ':
16 I = ''* indent + I
17 indent + = 2
18 elif (I [: 5] = 'else') or (I [: 5] = 'else '):
19 indent-= 2
20 I = ''* indent + I
21 indent + = 2
22 elif (I [: 7] = 'end _ if') or (I [: 7] = 'end _ if '):
23 indent-= 2
24 I = ''' * indent + I
25 else:
26 I = ''* indent + I
27 print I

 

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.