回答編集履歴

3

Add comment in code

2020/07/19 06:00

投稿

y_shinoda
y_shinoda

スコア3272

test CHANGED
@@ -30,6 +30,12 @@
30
30
 
31
31
  def scan(self, line):
32
32
 
33
+ """
34
+
35
+ 状態によって行う処理を切り替えて実行します
36
+
37
+ """
38
+
33
39
  line = line.rstrip()
34
40
 
35
41
  if not self.is_elapsed:
@@ -56,6 +62,14 @@
56
62
 
57
63
  def search_elapsed(self, line):
58
64
 
65
+ """
66
+
67
+ Elapsed stime の行が見つかるまでひたすら読み飛ばします
68
+
69
+ Elapsed stime の行が見つかった場合、読み飛ばしをやめるために状態を変更します
70
+
71
+ """
72
+
59
73
  if self.pattern_elapsed.match(line):
60
74
 
61
75
  self.is_elapsed = True
@@ -64,6 +78,14 @@
64
78
 
65
79
  def search_prev(self, line):
66
80
 
81
+ """
82
+
83
+ pattern_prev の行が見つかるまでひたすら読み飛ばします
84
+
85
+ pattern_prev の行が見つかった場合、読み飛ばしをやめるために状態を変更します
86
+
87
+ """
88
+
67
89
  if self.pattern_prev.match(line):
68
90
 
69
91
  self.is_extracting = True
@@ -71,6 +93,16 @@
71
93
 
72
94
 
73
95
  def emit_sql(self):
96
+
97
+ """
98
+
99
+ 仮の list に読み貯めた複数行の SQL 文を 1 行文字列に変換して
100
+
101
+ 結果の list に追加し、仮の list をクリアします
102
+
103
+ 再び Elapsed stime の行が見つかるまでひたすら読み飛ばすために状態をもとに戻します
104
+
105
+ """
74
106
 
75
107
  self.is_elapsed = False
76
108
 

2

Fix answer

2020/07/19 05:59

投稿

y_shinoda
y_shinoda

スコア3272

test CHANGED
@@ -132,9 +132,7 @@
132
132
 
133
133
  select owner#,name,namespace,remoteowner,linkname,p_timestamp,p_obj#, nvlp_obj#=obj#(+) order by order#
134
134
 
135
- select p_obj#, nvlp_obj#=obj#(+) order by order#
136
-
137
- 200 200 200 select /*+ rule */ bucket_cnt, row_cnt, cache_cnt, null_cnt, timestamp#, sample_ size, minimum, maximum, distcnt, lowval, hival, density, col#, spare1, spare2, a vgcln, minimum_enc, maximum_enc from hist_head$ where obj#=:1 and intcol#=:2
135
+ select /*+ rule */ bucket_cnt, row_cnt, cache_cnt, null_cnt, timestamp#, sample_ size, minimum, maximum, distcnt, lowval, hival, density, col#, spare1, spare2, a vgcln, minimum_enc, maximum_enc from hist_head$ where obj#=:1 and intcol#=:2
138
136
 
139
137
  ```
140
138
 

1

Fix answer

2020/07/19 03:13

投稿

y_shinoda
y_shinoda

スコア3272

test CHANGED
@@ -1,4 +1,4 @@
1
- こん感じどうでしょう?:
1
+ 要件が複雑クラスを使います:
2
2
 
3
3
 
4
4
 
@@ -8,47 +8,99 @@
8
8
 
9
9
 
10
10
 
11
+ class Scanner:
12
+
13
+ def __init__(self, pattern_prev, pattern_next):
14
+
11
- filepath = 'C:\Users\Desktop\新しいフォルダー\sample.txt'
15
+ self.pattern_elapsed = re.compile(r"SQL\sorder\sby\sElapsed\stime")
16
+
17
+ self.pattern_prev = re.compile(pattern_prev)
18
+
19
+ self.pattern_next = re.compile(pattern_next)
20
+
21
+ self.extracted_text_array = []
22
+
23
+ self.is_elapsed = False
24
+
25
+ self.is_extracting = False
26
+
27
+ self.sql = []
28
+
29
+
30
+
31
+ def scan(self, line):
32
+
33
+ line = line.rstrip()
34
+
35
+ if not self.is_elapsed:
36
+
37
+ self.search_elapsed(line)
38
+
39
+ return
40
+
41
+ if not self.is_extracting:
42
+
43
+ self.search_prev(line)
44
+
45
+ return
46
+
47
+ if self.pattern_next.match(line):
48
+
49
+ self.emit_sql()
50
+
51
+ return
52
+
53
+ self.sql.append(line.rstrip())
54
+
55
+
56
+
57
+ def search_elapsed(self, line):
58
+
59
+ if self.pattern_elapsed.match(line):
60
+
61
+ self.is_elapsed = True
62
+
63
+
64
+
65
+ def search_prev(self, line):
66
+
67
+ if self.pattern_prev.match(line):
68
+
69
+ self.is_extracting = True
70
+
71
+
72
+
73
+ def emit_sql(self):
74
+
75
+ self.is_elapsed = False
76
+
77
+ self.is_extracting = False
78
+
79
+ self.extracted_text_array.append(' '.join(self.sql))
80
+
81
+ self.sql = []
82
+
83
+
12
84
 
13
85
 
14
86
 
15
87
  def extract_text_in_file(filepath, pattern_prev, pattern_next):
16
88
 
17
- extracted_text_array = []
89
+ scanner = Scanner(pattern_prev, pattern_next)
18
90
 
19
91
  with open(filepath, "r", encoding="utf-8") as f:
20
92
 
21
- is_extracting = False
22
-
23
- sql = []
24
-
25
93
  for line in f:
26
94
 
27
- line = line.rstrip()
28
-
29
- if not is_extracting:
95
+ scanner.scan(line)
30
-
31
- if re.match(pattern_prev, line):
32
-
33
- is_extracting = True
34
-
35
- continue
36
-
37
- if re.match(pattern_next, line):
38
-
39
- extracted_text_array.append(' '.join(sql))
40
-
41
- sql = []
42
-
43
- is_extracting = False
44
-
45
- continue
46
-
47
- sql.append(line.rstrip())
48
96
 
49
97
 
50
98
 
51
- return extracted_text_array
99
+ return scanner.extracted_text_array
100
+
101
+
102
+
103
+ filepath = 'C:\Users\Desktop\新しいフォルダー\sample.txt'
52
104
 
53
105
 
54
106