回答編集履歴

3

誤記訂正

2019/12/17 17:25

投稿

otn
otn

スコア84663

test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ```Python
12
12
 
13
- mport re
13
+ import re
14
14
 
15
15
  class tmp_Data(object):
16
16
 

2

補足

2019/12/17 17:25

投稿

otn
otn

スコア84663

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  #追記
8
8
 
9
- エラー以外の部分も見ました。エラー処理は省いて書くと、こんな感じですかね。
9
+ エラー以外の部分も見ました。未定義の名前の参照の場合の処理は省いて書くと、こんな感じですかね。
10
10
 
11
11
  ```Python
12
12
 

1

追記

2019/12/17 03:59

投稿

otn
otn

スコア84663

test CHANGED
@@ -1,3 +1,45 @@
1
1
  このエラーの部分しか見てませんが、
2
2
 
3
3
  `aaa = tmp_Data`→`aaa = tmp_Data()`ですね。
4
+
5
+
6
+
7
+ #追記
8
+
9
+ エラー以外の部分も見ました。エラー処理は省いて書くと、こんな感じですかね。
10
+
11
+ ```Python
12
+
13
+ mport re
14
+
15
+ class tmp_Data(object):
16
+
17
+ def __init__(self):
18
+
19
+ self.host = "aaa"
20
+
21
+ self.ip = "10.123.145.30"
22
+
23
+ def foo(self,match):
24
+
25
+ return self.__dict__[match.group(1).lower()]
26
+
27
+ def replace_doller(self, original):
28
+
29
+ return re.sub(r"$(.+?)$",self.foo,original)
30
+
31
+
32
+
33
+ tmp_str = "host:$HOST$\nIP:$IP$"
34
+
35
+
36
+
37
+ aaa = tmp_Data()
38
+
39
+ out = aaa.replace_doller(tmp_str)
40
+
41
+ print(tmp_str)
42
+
43
+ print(out)
44
+
45
+ ```