-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathTemplate.py
120 lines (104 loc) · 3.88 KB
/
Template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
from os.path import join
from os.path import dirname
from os.path import isfile
class Template(object):
SUPPORTED_METHODS = {}
TEMPLATES = {}
def __init__(self, estimator, target_language='java',
target_method='predict', **kwargs):
# pylint: disable=unused-argument
self.target_language = str(target_language)
self.target_method = str(target_method)
# Default settings:
self.class_name = 'Brain'
self.method_name = 'predict'
self._num_format = lambda x: str(x)
self.use_file = False
def indent(self, text, n_indents=1, skipping=False):
"""
Indent text with single spaces.
Parameters
----------
:param text : string
The text which get a specific indentation.
:param n_indents : int, default: 1
The number of indentations.
:param skipping : boolean, default: False
Whether to skip the initial indentation.
Returns
-------
return : string
The indented text.
"""
lines = text.splitlines()
space = self.TEMPLATES.get(self.target_language).get('indent', ' ')
# Single line:
if len(lines) == 1:
if skipping:
return text.strip()
return n_indents * space + text.strip()
# Multiple lines:
indented_lines = []
for idx, line in enumerate(lines):
if skipping and idx is 0:
indented_lines.append(line)
else:
line = n_indents * space + line
indented_lines.append(line)
indented_text = '\n'.join(indented_lines)
return indented_text
def temp(self, name, templates=None, n_indents=None, skipping=False):
"""
Get specific template of chosen
programming language.
Parameters
----------
:param param name : string
The key name of the template.
:param param templates : string, default: None
The template with placeholders.
:param param n_indents : int, default: None
The number of indentations.
:param param skipping : bool, default: False
Whether to skip the initial indentation.
Returns
-------
return : string
The wanted template string.
"""
if templates is None:
templates = self.TEMPLATES.get(self.target_language)
keys = name.split('.')
key = keys.pop(0).lower()
template = templates.get(key, None)
if template is not None:
if isinstance(template, str):
if n_indents is not None:
template = self.indent(template, n_indents, skipping)
return template
else:
keys = '.'.join(keys)
return self.temp(keys, template, skipping=False)
else:
class_name = self.__class__.__name__
estimator_type = getattr(self, 'estimator_type') if \
hasattr(self, 'estimator_type') else 'classifier'
path = join(dirname(__file__), 'estimator',
estimator_type, class_name, 'templates',
self.target_language, name + '.txt')
if isfile(path):
with open(path, 'r') as file_:
template = file_.read()
if n_indents is not None:
template = self.indent(template, n_indents, skipping)
return template
else:
err = "Template '{}' wasn't found.".format(name)
raise AttributeError(err)
def repr(self, value):
return self._num_format(value)
def data(self, dict_):
copy = self.__dict__.copy()
copy.update(dict_) # update and extend dictionary
return copy