RiskQuantLib.Build package

Subpackages

Submodules

RiskQuantLib.Build.builder module

class builder(buildFromProjectPath='', targetProjectPath='', templateSearchPath='')[source]

Bases: object

builder is a class to use default render and router to re-construct the whole project. Any RiskQuantLib project should have a default builder. You can also add other builders to achieve different project mode or use other builder to assist your current project by specifying render channel.

builder is a class to combine router and render to re-construct the whole project, a project must have a default builder. It may also have other builders to construct project by another form.

Parameters

buildFromProjectPathstr

The path of builder class, default as Build directory in current project.

targetProjectPathstr

The path of target RiskQuantLib project which will be changed according to build information, default as current project.

templateSearchPathstr

The path of directory where .pyt file for building exists, default as Build/Component in current project. If buildFromProjectPath is specified, the default will be buildFromProjectPath/Build/Component.

Returns

None

bindContent(sourceCodeToBeInjected, bindType, persist=False)[source]

Parse the source code, find where the code should be injected into, and inject them.

Parameters

sourceCodeToBeInjectedstr

Content of python source code, with comment used as control syntax

bindTypestr

The type of binding action. Different bind type will be saved into different channel. Binding action in one channel will not influence one in another channel, unless it overwrites the same tag.

persistbool

If true, the current source code will be saved as permanent source code. It will not be influenced by building action any more. This action can not be cancelled.

Returns

None

buildContent(persist=False)[source]

Render set attribute functions and write them into related .py file.

Parameters

persistbool

If true, the current set attribute function will be saved as permanent source code. It will not be influenced by building action any more. This action can not be cancelled.

Returns

None

buildDir()[source]

Iterate through all instrument and make directory that does not exist yet, then add __init__.py into it.

Returns

None

buildFile()[source]

Render initiate .pyt file and write them into related .py file. If .py file already exists, it will be skipped and not change. If .py file does not exist, it will be created and initialized.

Returns

None

buildProject(dumpCache=True)[source]

Trigger of building. Call this function will parse instrument inherit tree into validated building information, create directory that is needed but does not exist yet, create file that is needed but does not exist yet. Generate set attribute function and write them into related instrument. And finally, write these change into building cache .pkl file.

Returns

None

static checkAndMakeDir(dir)[source]

Make directory recursively if it does not exist.

static checkAndMakeDirAndInitiate(dir)[source]

Make directory recursively if it does not exist, then add an __init__.py file in it.

static checkAndMakeFile(file, content='')[source]

Make a file is it does not exist, and write content into it.

checkPath()[source]

In old versions of RiskQuantLib, if the project and built, and then, the project dir name is changed, the builder can not recognize the new project path and will raise an error when you build it again. This function is used to check whether the path has been changed. If changed, it will replace the old path into the default project path.

This is the default behavior of RiskQuantLib, if your project does not use many guardians, this default setting will be helpful and make it convenient to change project location or run your old project in a new computer.

However, it may cause problem when you use guardian projects. The best way is always un-build a project when you want to change its location or rename it, and build it again when rename action is finished. It is safer.

checkRender(templateSearchPath='')[source]

Change the render into a new one if it does not exist, use specified templateSearchPath. templateSearchPath is default as current templateSearchPath.

Parameters

templateSearchPathstr

The path of directory where .pyt file for building exists, default as Build/Component in current project. If buildFromProjectPath is specified, the default will be buildFromProjectPath/Build/Component.

Returns

None

clearProject()[source]

Clear any code generated by building and render action. But it will not delete .py file.

Returns

None

delRender()[source]

Delete the render from current builder. This is the preparation for dumping current builder into .pkl file, since render object can not be serialized.

Returns

None

dumpInfo(savePath='')[source]

Dump current builder into disk.

Returns

None

static formatWarning(message, category, filename, lineno, file=None, line=None)[source]

Make the warning message more readable and neglect the redundant line.

initiateInstrumentTree()[source]

Initialize a new inherit tree object to store instrument information, and add default nodes into it.

Returns

None

initiateProject()[source]

Initialize the project will brand new instrument inherit tree and property inherit tree. It will also replace the render object into a new one.

This function will clear up all current instrument inherit information and property information, all current attributes will be removed.

Returns

None

initiatePropertyTree()[source]

Initialize a new inherit tree object to store property type information, and add default nodes into it.

Returns

None

linkContent(content, linkType)[source]

This function will use control declaration in source code to build the whole project again. Any instrument that is required by source code will be created, any attribute this is needed will be added. These actions will be operated on an independent builder named as mimicBuilder, which is the update version of base builder.

static loadInfo(savePath='')[source]

Load building cache from a RiskQuantLib project or a .pkl file. If there is not building cache in specified path, an exception will be raised.

Parameters

savePathstr

The path of building cache .pkl file, or the path of RiskQuantLib project.

Returns

None

persistProject(sourceCodeDirPath='', bindType='renderedSourceCode')[source]

Build and render current project according to current information, and turn the generated code into permanent code. These code will not be influenced by any building action any more.

Parameters

sourceCodeDirPathstr

The path of directory where source code exist. Any sub file in this folder or sub-folder will be rendered and parsed and injected to target project.

bindTypestr

The channel of binding action. Source code are rendered and injected into project by different channels, The source code injected by channel A will be not influenced by source code injected by channel B, unless the content of tag is overwritten by code in channel B. This is used when you have several builders and you want them to build into the same project. In this case, you should give a bindType for each render action to make sure they do not conflict with each other.

Returns

None

renderProject(sourceCodeDirPath='', bindType='renderedSourceCode', persist=False, debug=False, **kwargs)[source]

Render and inject source code into target project.

Parameters

sourceCodeDirPathstr

The path of directory where source code exist. Any sub file in this folder or sub-folder will be rendered and parsed.

bindTypestr

The channel of binding action. Source code are rendered and injected into project by different channels, The source code injected by channel A will be not influenced by source code injected by channel B, unless the content of tag is overwritten by code in channel B. This is used when you have several builders and you want them to build into the same project. In this case, you should give a bindType for each render action to make sure they do not conflict with each other.

persistbool

If true, the current source code will be saved as permanent source code. It will not be influenced by building action any more. This action can not be cancelled.

debugbool

If false, the break point in Src will not be effective, only break point within instrument class will effect. If true, the class method defined in Src directory will be dynamically bound to instrument node class. Then the program will take .py file under .Src directory as a module and import it, bind the class method into specified class. This mode is useful when your code is still under development. You will not have to change between ./Src/somecode.py and target instrument class .py file to edit any code error. The break point will stop right under ./Src/somecode.py.

Returns

None

updateBuildInfo()[source]

Parse instrument inherit tree, property type tree, attribute information and instrument dependency. This function will change these information into list to prepare for further render.

Returns

None

updatePathInfo()[source]

Render the filePath.pyt according to current parsed information to generate absolute file path and relative file path.

Returns

None

updateRender(templateSearchPath='')[source]

Change the render into a new one, use specified templateSearchPath. templateSearchPath is default as current templateSearchPath.

Parameters

templateSearchPathstr

The path of directory where .pyt file for building exists, default as Build/Component in current project. If buildFromProjectPath is specified, the default will be buildFromProjectPath/Build/Component.

Returns

None

static validateStringForJson(string)[source]

This function will parse json like string into a validated json string. All escape character will be kept as original form, which is to say, any ‘’ will not be treated as an escape sign.

This function is used to parse .pyt file when .pyt file is in a json form and contains file path. In windows, these file paths will have ‘’, and it will be wrongly seen as escape character. This function will keep the string as raw string, validateStringForJson(string) equals r’stringContent’.

class configBuilder(buildFromProjectPath='', targetProjectPath='', templateSearchPath='')[source]

Bases: stringBuilder

configBuilder is a child class of stringBuilder, it takes config.py as information of building.

builder is a class to combine router and render to re-construct the whole project, a project must have a default builder. It may also have other builders to construct project by another form.

Parameters

buildFromProjectPathstr

The path of builder class, default as Build directory in current project.

targetProjectPathstr

The path of target RiskQuantLib project which will be changed according to build information, default as current project.

templateSearchPathstr

The path of directory where .pyt file for building exists, default as Build/Component in current project. If buildFromProjectPath is specified, the default will be buildFromProjectPath/Build/Component.

Returns

None

buildProject(configFilePath)[source]

Trigger of building. Call this function will parse instrument inherit tree into validated building information, create directory that is needed but does not exist yet, create file that is needed but does not exist yet. Generate set attribute function and write them into related instrument. And finally, write these change into building cache .pkl file.

Returns

None

class databaseBuilder(buildFromProjectPath='', targetProjectPath='', templateSearchPath='')[source]

Bases: dataframeBuilder

databaseBuilder is a child class of dataframeBuilder, you should pass two sql syntax and a connection object into this class to classify building information.

builder is a class to combine router and render to re-construct the whole project, a project must have a default builder. It may also have other builders to construct project by another form.

Parameters

buildFromProjectPathstr

The path of builder class, default as Build directory in current project.

targetProjectPathstr

The path of target RiskQuantLib project which will be changed according to build information, default as current project.

templateSearchPathstr

The path of directory where .pyt file for building exists, default as Build/Component in current project. If buildFromProjectPath is specified, the default will be buildFromProjectPath/Build/Component.

Returns

None

buildProject(instrumentSql, attributeSql, con, colAsInstrumentNameForInstrumentDF='InstrumentName', colAsParentRQLClassNameForInstrumentDF='ParentRQLClassName', colAsParentQuantLibClassNameForInstrumentDF='ParentQuantLibClassName', colAsLibraryNameForInstrumentDF='LibraryName', colAsDefaultInstrumentTypeForInstrumentDF='DefaultInstrumentType', colAsInstrumentNameForAttributeDF='SecurityType', colAsAttributeNameForAttributeDF='AttrName', colAsPropertyNameForAttributeDF='AttrType')[source]

Trigger of building. Call this function will parse instrument inherit tree into validated building information, create directory that is needed but does not exist yet, create file that is needed but does not exist yet. Generate set attribute function and write them into related instrument. And finally, write these change into building cache .pkl file.

Returns

None

class dataframeBuilder(buildFromProjectPath='', targetProjectPath='', templateSearchPath='')[source]

Bases: validateBuilder

dataframeBuilder is a child class of validatedBuilder, it takes two pandas.DataFrame as information of building.

builder is a class to combine router and render to re-construct the whole project, a project must have a default builder. It may also have other builders to construct project by another form.

Parameters

buildFromProjectPathstr

The path of builder class, default as Build directory in current project.

targetProjectPathstr

The path of target RiskQuantLib project which will be changed according to build information, default as current project.

templateSearchPathstr

The path of directory where .pyt file for building exists, default as Build/Component in current project. If buildFromProjectPath is specified, the default will be buildFromProjectPath/Build/Component.

Returns

None

buildProject()[source]

Trigger of building. Call this function will parse instrument inherit tree into validated building information, create directory that is needed but does not exist yet, create file that is needed but does not exist yet. Generate set attribute function and write them into related instrument. And finally, write these change into building cache .pkl file.

Returns

None

setAttributeInfo(df, colAsInstrumentName='SecurityType', colAsAttributeName='AttrName', colAsPropertyName='AttrType')[source]

Pass a pandas.DataFrame into this function to use it as attribute building information.

setInstrumentInfo(df, colAsInstrumentName='InstrumentName', colAsParentRQLClassName='ParentRQLClassName', colAsParentQuantLibClassName='ParentQuantLibClassName', colAsLibraryName='LibraryName', colAsDefaultInstrumentType='DefaultInstrumentType')[source]

Pass a pandas.DataFrame into this function to use it as instrument building information.

class excelBuilder(buildFromProjectPath='', targetProjectPath='', templateSearchPath='')[source]

Bases: dataframeBuilder

excelBuilder is a child class of dataframeBuilder, it takes two xlsx file as information of building.

builder is a class to combine router and render to re-construct the whole project, a project must have a default builder. It may also have other builders to construct project by another form.

Parameters

buildFromProjectPathstr

The path of builder class, default as Build directory in current project.

targetProjectPathstr

The path of target RiskQuantLib project which will be changed according to build information, default as current project.

templateSearchPathstr

The path of directory where .pyt file for building exists, default as Build/Component in current project. If buildFromProjectPath is specified, the default will be buildFromProjectPath/Build/Component.

Returns

None

buildProject(instrumentExcelPath, attributeExcelPath, colAsInstrumentNameForInstrumentDF='InstrumentName', colAsParentRQLClassNameForInstrumentDF='ParentRQLClassName', colAsParentQuantLibClassNameForInstrumentDF='ParentQuantLibClassName', colAsLibraryNameForInstrumentDF='LibraryName', colAsDefaultInstrumentTypeForInstrumentDF='DefaultInstrumentType', colAsInstrumentNameForAttributeDF='SecurityType', colAsAttributeNameForAttributeDF='AttrName', colAsPropertyNameForAttributeDF='AttrType')[source]

Trigger of building. Call this function will parse instrument inherit tree into validated building information, create directory that is needed but does not exist yet, create file that is needed but does not exist yet. Generate set attribute function and write them into related instrument. And finally, write these change into building cache .pkl file.

Returns

None

class stringBuilder(buildFromProjectPath='', targetProjectPath='', templateSearchPath='')[source]

Bases: dataframeBuilder

stringBuilder is a child class of validatedBuilder, it takes string as information of building.

builder is a class to combine router and render to re-construct the whole project, a project must have a default builder. It may also have other builders to construct project by another form.

Parameters

buildFromProjectPathstr

The path of builder class, default as Build directory in current project.

targetProjectPathstr

The path of target RiskQuantLib project which will be changed according to build information, default as current project.

templateSearchPathstr

The path of directory where .pyt file for building exists, default as Build/Component in current project. If buildFromProjectPath is specified, the default will be buildFromProjectPath/Build/Component.

Returns

None

buildProject(content)[source]

Trigger of building. Call this function will parse instrument inherit tree into validated building information, create directory that is needed but does not exist yet, create file that is needed but does not exist yet. Generate set attribute function and write them into related instrument. And finally, write these change into building cache .pkl file.

Returns

None

class validateBuilder(buildFromProjectPath='', targetProjectPath='', templateSearchPath='')[source]

Bases: builder

validateBuilder is a child class of builder, it will parse the information, screen out those wrong syntax and pass validated information to base builder.

builder is a class to combine router and render to re-construct the whole project, a project must have a default builder. It may also have other builders to construct project by another form.

Parameters

buildFromProjectPathstr

The path of builder class, default as Build directory in current project.

targetProjectPathstr

The path of target RiskQuantLib project which will be changed according to build information, default as current project.

templateSearchPathstr

The path of directory where .pyt file for building exists, default as Build/Component in current project. If buildFromProjectPath is specified, the default will be buildFromProjectPath/Build/Component.

Returns

None

buildProject(instrumentName=[], parentRQLClassName=[], parentQuantLibClassName=[], libraryName=[], defaultInstrumentType=[], attributeBelongTo=[], attributeName=[], propertyName=[])[source]

Trigger of building. Call this function will parse instrument inherit tree into validated building information, create directory that is needed but does not exist yet, create file that is needed but does not exist yet. Generate set attribute function and write them into related instrument. And finally, write these change into building cache .pkl file.

Returns

None

initiateTree()[source]
static isString(string, allowComma=True, allowEmpty=True, allowSpace=True)[source]
static lowerCaseFirstLetter(string)[source]
static upperCaseFirstLetter(string)[source]
static validateParentClassName(parentClass, inheritTreeNodeDict)[source]

If class name in inheritTreeNodeDict, return it. Otherwise, parent class will be root instrument, and parent class is defaulted as blank string.

static validateRootInstrumentName(instrumentName, rootInstrumentName)[source]

You can have some words who have the same meaning with root instrument name. This function is used to identify those words.

If instrumentName in rootInstrumentName, return default root instrument name, which is blank string. otherwise, return instrumentName itself.

static validateString(string, lowerCaseFirstLetter=True)[source]

Strip any blank in string, if comma exists in string, split string according to comma, then strip any blank in every sub-string.

If lowerCaseFirstLetter is True, the first letter of every sub-string will be changed into lower-case letter.

validateTree(instrumentName=[], parentRQLClassName=[], parentQuantLibClassName=[], libraryName=[], defaultInstrumentType=[], attributeBelongTo=[], attributeName=[], propertyName=[])[source]

RiskQuantLib.Build.controller module

class controller[source]

Bases: object

static findDeclareTag(sourceCode)[source]

Find the line start with #-|, return a list whose element is each line string.

static linkController(linkToBuilder, controlSyntaxList)[source]

Given original builder, create a new builder, this new builder will copy all build path of original builder, but use additional build information declared by declaration content to modify itself. Then the new builder will trigger a build action. This function will return the new builder as a mimic one of original builder.

static parseDeclareTagAsDF(controlSyntaxList, instrumentColDefault=['InstrumentName', 'ParentRQLClassName', 'ParentQuantLibClassName', 'LibraryName', 'DefaultInstrumentType'], attributeColDefault=['SecurityType', 'AttrName', 'AttrType'])[source]

Given the list whose element is line string of declaration content, transfer instrument declaration and attribute declaration into series, and merge all series into a build dataframe. Return the buildInstrument dataframe and buildAttr dataframe.

static parseDeclareTagAsSeries(content, tagName, defaultTagName='')[source]

Transform each declaration line about instrument into Series, which contains instrument build information.

static parseDeclareTagByGivenString(content, splitLineBy=',', splitWordBy='@')[source]

Split content by given string, and split each element of line by given string. Return the list whose element is the first part of word of each line, and the list whose element if the last part of word of each line.

RiskQuantLib.Build.debugger module

class debugger[source]

Bases: object

debugger is a class used to render .py file. It will parse the functions in .py file and bound them into the class which is specified by #-> comment command. This is different with the normal render, the normal render will take .py file as a text file and take the content as it be. This debugger takes .py file as an executable file and will execute it by import the file and bind its functions into class dynamically.

static findClassMethodFunction(function)[source]

For all possible definition parsed from ast module, only those with first argument as ‘self’ can be treated as class method. For python, this is not a restriction, you can use any key word to replace ‘self’, however, for RiskQuantLib, it takes ‘self’ as the only possible one in order to debug.

Parameters

functionlist

The list whose element is the parsed ast function definition object, which is ast.FunctionDef.

static findControlCommentLineId(sourceCodeByLine)[source]

Find the line index of control comment.

static findControlCommentLineRange(controlCommentLineId, numberOfLine)[source]

For every control comment, find the line index where it starts effective and line index where it stops effective.

Parameters

controlCommentLineIdlist

The list whose element is the line index number where the control comments lie.

numberOfLineint

The total line number of source code.

static findFunction(sourceCode)[source]

Use python ast module to parse source code. This aims at finding functions that can be treated as validated python functions. ast module can be used in python>=3.5, it is the abstract-syntax-tree, used as interface to python interpreter.

static findFunctionEnd(functionObject)[source]

Find function end line index.

static findFunctionStart(functionObject)[source]

Find function start line index. For some functions with decorators, the start line will be the start line of first decorator.

static findFunctionUnderControlComment(function, controlCommentLineRange)[source]

For all possible class method, it can not be debugged unless it is distributed into a RiskQuantLib instrument node. Only those functions which is under some control comment can be debugged, this function is to find those.

Parameters

functionlist

The list whose element is the parsed ast function definition object, which is ast.FunctionDef.

controlCommentLineRangelist

The list whose element is a tuple, which is (line index where control comment start effective, line index where that control comment end effective).

static findOtherChunkOutsideGivenChunk(functionChunk, endLineNum)[source]

Given a list whose element is a tuple: (start line of chunk, end line of chunk), and given the total line number, this function will find those chunk which does not belong to given chunk and not cross with given chunk. In short, this function is to find replenishment of given set.

Parameters

functionChunklist

The list whose element is the start and end of a chunk.

endLineNumint

The total line number of source code.

static importModuleFromFile(filePath)[source]

Import module from given file and cache it into sys.modules. If this file has already been imported, it will load the cached one.

static splitSrcByChunkAndFindThoseCanBeDebugged(sourceCode)[source]

This function will check the while source code and find those functions can be treated as class method, the definition of these functions are wrapped into a chunk. Then the other source code are packed into chunks according to class-method chunk. All these source code chunk are then labelled as ‘can be debugged’ and ‘can not be debugged’, if it can be debugged, its function name will be found and returned.

static unParseSourceCode(sourceCodeByLine, startLine, endLine)[source]

Given a list of source code, whose element is each line of source code, given start line and end line, this function will merge the content between start and end line, and return the merged source code.

RiskQuantLib.Build.render module

class render(componentPathString)[source]

Bases: object

render is a class used to set up jinja2 Environment to render code templates.

Initialize a render, find jinja2 template in componentPathString

Parameters

componentPathStringstr

path of directory where templates exist. Any sub-folder will be searched, file and sub-file will be added into template directory

Returns

None

render(templateFileName, **kwargs)[source]

Render a jinja2 template will given kwargs.

Parameters

templateFileNamestr

path of template file. Any sub-folder will be searched, file and sub-file will be added into template directory

Returns

str

RiskQuantLib.Build.router module

class router[source]

Bases: object

static contentSplit(content, tagStart, tagEnd)[source]

Split content by given tagStart and tagEnd, return content before tagStart and content after tagEnd

static convertInjectTagToFilePath(injectTag, injectPath, syntacticSugarPathMap={})[source]

Parse a line of tag information. Split the line by comma, parse every sub-string into (filePath,tagName) and reform them into a list.

static findIndentOfLine(content, startTextOfLine)[source]

Find the blank before given text.

static formatTag(tagName)[source]
static indent(num=0)[source]
static injectToContent(content, **kwargs)[source]

Insert several contents into related tag. Contents will be between tagStart and tagEnd. kwargs is a dict whose key is tag name and value is content to be inserted.

static injectToFile(pythonSourceFilePath, **kwargs)[source]

Read a .py file and insert several contents into related tag position.

static insertToContent(sourceCode, tagName, content)[source]

Insert some content to given tag position

static insertToFile(sourceCode, tagName, pythonSourceFilePath)[source]

Read a .py file and insert some code into tag position.

static parseInjectTarget(sourceCode, injectPath, syntacticSugarPathMap={})[source]

Find the line of control comment, split whole content by control comment line. Parse the target file and tag name for every control comment. Finally, return a dict whose key is target file and value is (tag,content) pair.

static persistToContent(content, **kwargs)[source]

Insert several contents right before related tag. Contents will be before tagStart. kwargs is a dict whose key is tag name and value is content to be inserted.

static persistToFile(pythonSourceFilePath, **kwargs)[source]

Read a .py file and insert several contents before related tag position.

static readContent(pythonSourceFilePath, encoding='utf-8')[source]
static stripTag(content)[source]

Delete the row of tag

static validateInjectTag(injectTag, injectPath, syntacticSugarPathMap={})[source]

Parse a single tag information. If user does not specify @, use file name as tag name. Then convert dirA.dirB.dirC into dirA/dirB/dirC. Return target file path and target tag name.

static writeContent(content, pythonSourceFilePath, encoding='utf-8')[source]

RiskQuantLib.Build.tree module

class inheritNode(name)[source]

Bases: treeOperation

A link chain node used to hold inherit information.

addAttr(dictName, attrKey, attrValue)[source]

Add a (key, value) pair to current attribute dict. If this dict does not exist, it will be created.

dependOnOutside(outsideDependenceName)[source]

Specify a library which current node depends on.

destroyNode(fromDict={})[source]

Delete this node and delete any inherit relationship from its parent nodes and child nodes.

extendInheritTree(parentNode)[source]

Given a inheritNode object, this function will append the name of inheritNode after the inheritTree of that node, then return the new list as extended inheritTree.

inheritFrom(parentNode)[source]

Inherit from one or more node.

inheritFromOutside(outsideParentName)[source]

Inherit from a node which is not a node in current inherit tree.

setAttr(attrName, attrValue)[source]

Add an attribute to current node.

updateInheritTree(inheritTree)[source]

Given a list, this function will insert this list before the inheritTree of that node and its any child node.

class inheritTree[source]

Bases: treeOperation

A link chain used to hold inherit information.

addNode(name, setActive=True)[source]

Add a single node into current inherit tree, if it already exists, set that node into active, if it does not exist, a new node will be created as set as active.

delNode(name)[source]

Delete a node from current inherit tree.

getAttr(attrName, default=None)[source]

Iterate through nodes and get attribute in every node.

getInheritList()[source]

Get the inheritTree attribute of every node.

getNode(name)[source]

Select a node.

getParentInheritListSeries()[source]

Iterate through every node and get the inheritTree of every parent of that node.

inheritFrom(parentNode)[source]

Select alive node and make it inheriting from one or more nodes in current inherit tree.

replaceNode(name)[source]

Replace a node into a brand new one, and set alive into that node.

class treeOperation[source]

Bases: object

copy(deep=True)[source]

Get a copy of present tree object.

Module contents