考慮到一些VBA代碼(存儲在文本文件中)根據條件調用規則,我想解析代碼並創建所有代碼的小代碼片段需要知道生成該規則(以更容易理解規則的本質)。使用Python將VBA代碼解析爲較小的代碼片段
我開始嘗試在Python中將一些規則正則表達,但如果它存在,不想重新創建輪子。我知道像Retrieving JSON objects from a text file (using Python)這樣的例子來覆蓋基類來創建客戶解析器。我不確定是否有最適合這種情況的包,並且沒有找到任何運氣。
背景是,有〜5000分的規則,我要「縮水」這樣的,更簡單地說明圍繞規則的邏輯,看東西有多少規則由某個變量的影響等
輸入:
Sub One(position As Long)
Dim y As Long
With TEMP_ARRAY(position)
'Comments
If .VAR_A = "A" And .VAR_B = "B" And .VAR_C = "C" Then
Call Some_Rule("Rule 1")
End If
'More Comments
If IsEmpty(.SUB_ARRAY) Then
Call Some_Rule("Rule 2")
Else
If .VAR_A = 2 Then
If .VAR_B <> "" Then
'Some more comments
For y = 0 To UBound(.SUB_ARRAY)
If .SUB_ARRAY(y, 2) = 1 Or .SUB_ARRAY(y, 2) = 2 Then Exit For
Next y
If y = UBound(.SUB_ARRAY, 1) + 1 Then
Call Some_Rule("Rule 3")
End If
Else
'Still more comments
Select Case .SUB_ARRAY(0, 2)
Case 3
Call Some_Rule("Rule 4")
Case 4
Call Some_Rule("Rule 5")
End Select
End If
End If
End If
End With
End Sub
所需的輸出:
## RULE 1
Sub One(position As Long)
With TEMP_ARRAY(position)
'Comments
If .VAR_A = "A" And .VAR_B = "B" And .VAR_C = "C" Then
Call Some_Rule("Rule 1")
End If
End With
End Sub
## RULE 2
Sub One(position As Long)
With TEMP_ARRAY(position)
'More Comments
If IsEmpty(.SUB_ARRAY) Then
Call Some_Rule("Rule 2")
End If
End With
End Sub
## RULE 3
Sub One(position As Long)
Dim y As Long
With TEMP_ARRAY(position)
'More Comments
If IsEmpty(.SUB_ARRAY) Then
Else
If .VAR_A = 2 Then
If .VAR_B <> "" Then
'Some more comments
For y = 0 To UBound(.SUB_ARRAY)
If .SUB_ARRAY(y, 2) = 1 Or .SUB_ARRAY(y, 2) = 2 Then Exit For
Next y
If y = UBound(.SUB_ARRAY, 1) + 1 Then
Call Some_Rule("Rule 3")
End If
End If
End If
End If
End With
End Sub
## RULE 4
Sub One(position As Long)
With TEMP_ARRAY(position)
'More Comments
If IsEmpty(.SUB_ARRAY) Then
Else
If .VAR_A = 2 Then
If .VAR_B <> "" Then
Else
'Still more comments
Select Case .SUB_ARRAY(0, 2)
Case 3
Call Some_Rule("Rule 4")
End Select
End If
End If
End If
End With
End Sub
## RULE 5
Sub One(position As Long)
With TEMP_ARRAY(position)
'More Comments
If IsEmpty(.SUB_ARRAY) Then
Else
If .VAR_A = 2 Then
If .VAR_B <> "" Then
Else
'Still more comments
Select Case .SUB_ARRAY(0, 2)
Case 4
Call Some_Rule("Rule 5")
End Select
End If
End If
End If
End With
End Sub
編輯:這是我迄今爲止所做的(更多代碼,但這是它的核心)。基本上,找到一個以「Some_Rule」開始的行(使用正則表達式),然後從向上的方向調用該函數。當它找到一個開放的標籤時,它會改變方向並開始尋找它的結束標籤,然後選擇它離開的地方再次上升等。我成功地獲得規則1,然後是凌晨4點,所以我去睡覺:) ......我在周圍扔東西在這一點所以還真馬虎,但是想如果你的VBA代碼編寫最Excel的代碼似乎要寫入的方式,例如,快意恩仇我的進步
def compile_rule(lines, j, direction, statement_open=False):
"""
lines : total lines in file
j : current position
direction : 1 is up, -1 is down
statement_open : vba syntax not yet closed ex: if without end if
"""
global rule
j -= direction
if line_type(lines[j]) in [0, 3] and not statement_open:
rule.append(lines[j], j, direction)
elif line_type(lines[j]) == 1 and not statement_open:
rule.append(lines[j], j, direction)
rule.start_looking_for(line_check(lines[j]))
statement_open = True
direction *= -1
elif line_type(lines[j]) == 2 and rule.looking_for() == line_check(lines[j]) and statement_open:
rule.append(lines[j], j, direction)
statement_open = False
direction *= -1
else:
rule.set_position(j, direction)
if (j > 0 and j < len(lines) - 1) or (j == 0 and statement_open):
compile_rule(lines, rule.get_position(direction), direction, statement_open)
所以你有1個規則通過黑客工作,與4999去? –
爲什麼「Python」是問題的根本部分? –
沒有理由 - 認爲可能有一些更簡單的Python解析器可用。規則將(可能/最終)從VBA轉移到Python,以便具有Python體驗的數據分析師可以維護規則。認爲如果規則是獨立的,它可能使移動更容易 - 這不是要求,所以不打算花太多時間在它上面。 – NikT