MCPcopy Create free account
hub / github.com/ScottfreeLLC/AlphaPy / vparse

Function vparse

alphapy/variables.py:155–214  ·  view source on GitHub ↗

r"""Parse a variable name into its respective components. Parameters ---------- vname : str The name of the variable. Returns ------- vxlag : str Variable name without the ``lag`` component. root : str The base variable name without the parameter

(vname)

Source from the content-addressed store, hash-verified

153#
154
155def vparse(vname):
156 r"""Parse a variable name into its respective components.
157
158 Parameters
159 ----------
160 vname : str
161 The name of the variable.
162
163 Returns
164 -------
165 vxlag : str
166 Variable name without the ``lag`` component.
167 root : str
168 The base variable name without the parameters.
169 plist : list
170 The parameter list.
171 lag : int
172 The offset starting with the current value [0]
173 and counting back, e.g., an offset [1] means the
174 previous value of the variable.
175
176 Notes
177 -----
178
179 **AlphaPy** makes feature creation easy. The syntax
180 of a variable name maps to a function call:
181
182 xma_20_50 => xma(20, 50)
183
184 Examples
185 --------
186
187 >>> vparse('xma_20_50[1]')
188 # ('xma_20_50', 'xma', ['20', '50'], 1)
189
190 """
191
192 # split along lag first
193 lsplit = vname.split(LOFF)
194 vxlag = lsplit[0]
195 # if necessary, substitute any alias
196 root = vxlag.split(USEP)[0]
197 alias = get_alias(root)
198 if alias:
199 vxlag = vxlag.replace(root, alias)
200 vsplit = vxlag.split(USEP)
201 root = vsplit[0]
202 plist = vsplit[1:]
203 # extract lag
204 lag = 0
205 if len(lsplit) > 1:
206 # lag is present
207 slag = lsplit[1].replace(ROFF, '')
208 if len(slag) > 0:
209 lpat = r'(^-?[0-9]+$)'
210 lre = re.compile(lpat)
211 if lre.match(slag):
212 lag = int(slag)

Callers 3

apply_transformsFunction · 0.90
vwalkFunction · 0.85
vexecFunction · 0.85

Calls 1

get_aliasFunction · 0.90

Tested by

no test coverage detected