Menu
The generation of a code snippet is done by a
CodeGenerator
class.The templates are stored in the CodeObject.templater
attribute, which istypically implemented as a subdirectory of templates. The compilation andrunning of code is done by a CodeObject
. See the sections below for eachof these.Code path¶
You are happy that a function executes correctly, submit the whole function to CodeRunner: When you press the Check button in CodeRunner, you will receive immediate feedback telling you if you have passed all the tests for that function. You can submit as many times as you need. CodeRunner 2.2.2.6071 Eclipse IDE for Java EE Developers 4.6.0 JetBrains AppCode 2016.3.1 JetBrains PhpStorm 2016.3.2 JetBrains RubyMine 2016.3.1 JetBrains WebStorm 2016.3.2 Komodo IDE 10.1.1 SweetScape 010 Editor 7.0.2 Taco Software Latexian 1.2.7 Xojo 2016 R2. DVD16 macOS Sierra Bootable macOS Sierra 10.12.1 Bootable. DVD17 Apple Music Apps.
The following gives an outline of the key steps that happen for the codegeneration associated to a
NeuronGroup
StateUpdater
. The items in greyare Brian core functions and methods and do not need to be implemented tocreate a new code generation target or device. The parts in yellow areused when creating a new device. The parts in green relate to generatingcode snippets from abstract code blocks. The parts in blue relate to creatingnew templates which these snippets are inserted into. The parts in redrelate to creating new runtime behaviour (compiling and running generatedcode).In brief, what happens can be summarised as follows.
Network.run
will callBrianObject.before_run
on each of the objects in the network. Objects suchas StateUpdater
, which is a subclass of CodeRunner
use this spot togenerate and compile their code. The process for doing this is to firstcreate the abstract code block, done in the StateUpdater.update_abstract_code
method. Then, a CodeObject
is created with this code block. In doing so,Brian will call out to the currently active Device
to get the CodeObject
and CodeGenerator
classes associated to the device, and this hierarchy ofcalls gives several hooks which can be changed to implement new targets.Code generation¶
To implement a new language, or variant of an existing language, derive a classfrom
CodeGenerator
. Good examples to look at are the NumpyCodeGenerator
,CPPCodeGenerator
and CythonCodeGenerator
classes in thebrian2.codegen.generators
package. Each CodeGenerator
has a class_name
attribute which is a string used by the user to refer to this code generator(for example, when defining function implementations).The derived
CodeGenerator
class should implement the methods marked asNotImplemented
in the base CodeGenerator
class. CodeGenerator
also hasseveral handy utility methods to make it easier to write these, see theexisting examples to get an idea of how these work.Syntax translation¶
One aspect of writing a new language is that sometimes you need to translatefrom Python syntax into the syntax of another language. You are free todo this however you like, but we recommend using a
NodeRenderer
classwhich allows you to iterate over the abstract syntax tree of an expression.See examples in brian2.parsing.rendering
.Templates¶
In addition to snippet generation, you need to create templates for thenew language. See the
templates
directories in brian2.codegen.runtime.*
for examples of these. They are written in the Jinja2 templating system. Thelocation of these templates is set as the CodeObject.templater
attribute.Examples such as CPPCodeObject
show how this is done.Template structure¶
Languages typically define a
common_group
template that is the base for allother templates. This template sets up the basic code structure that will be reused byall code objects, e.g. by defining a function header and body, and adding standardimports/includes. This template defines several blocks, in particular a maincode
clock containing the actual code that is specific to each code object. The specifictemplates such as reset
then derive from the common_group
base template andoverride the maincode
block. The base template can also define additional blocksthat are sometimes but not always overwritten. For example, the common_group.cpp
template of the C++ standalone code generator defines an extra_headers
block thatcan be overwritten by child templates to include additional header files needed for thecode in maincode
.Template keywords¶
Templates also specify additional information necessary for the code generation processas Jinja comments (
{#...#}
). The following keywords are recognized by Brian:USES_VARIABLES
Lists variable names that are used by the template, even if they are not referred toin user code.
WRITES_TO_READ_ONLY_VARIABLES
Lists read-only variables that are modified by the template. Normally, read-onlyvariables are not considered to change during code execution, but e.g. synapsecreation requires changes to synaptic indices that are considered read-onlyotherwise.
ALLOWS_SCALAR_WRITE
The presence of this keyword means that in this template, writing to scalarvariables is permitted. Writing to scalar variables is not permitted by default,because it can be ambiguous in contexts that do not involve all neurons/synapses.For example, should the statement
scalar_variable+=1
in a reset statementupdate the variable once or once for every spiking neuron?ITERATE_ALL
Lists indices that are iterated over completely. For example, during the stateupdate or threshold step, the template iterates over all neurons with the standardindex
_idx
. When executing the reset statements on the other hand, not allneurons are concerned. This is only used for the numpy code generation target,where it allows avoiding expensive unnecessary indexing.Code objects¶
To allow the final code block to be compiled and run, derive a class from
CodeObject
. This class should implement the placeholder methods defined inthe base class. The class should also have attributes templater
(whichshould be a Templater
object pointing to the directory where the templatesare stored)generator_class
(which should be the CodeGenerator
class), andclass_name
(which should be a string the user can use to refer to thiscode generation target.Default functions¶
You will typically want to implement the default functions such as thetrigonometric, exponential and
rand
functions. We usually put theseimplementations either in the same module as the CodeGenerator
class orthe CodeObject
class depending on whether they are language-specific orruntime target specific. See those modules for examples of implementingthese functions.Code guide¶
brian2.codegen
: everything related to code generationbrian2.codegen.generators
: snippet generation,including theCodeGenerator
classes and default function implementations.brian2.codegen.runtime
: templates, compilation and running of code,includingCodeObject
and default function implementations.brian2.core.functions
,brian2.core.variables
: these define the valuesthat variable names can have.brian2.parsing
: tools for parsing expressions, etc.brian2.parsing.rendering
: AST tools for rendering expressions in Pythoninto different languages.brian2.utils
: various tools for string manipulation, file management, etc.
Coderunner Download
Additional information¶
For some additional (older, but still accurate) notes on code generation:
- Older notes on code generation
A simple plugin to allow you to run the current file, and show the result. ST3 support is in SublimeText3 branch
Installs
- Total10K
- Win6K
- Mac2K
- Linux2K
Dec 13 | Dec 12 | Dec 11 | Dec 10 | Dec 9 | Dec 8 | Dec 7 | Dec 6 | Dec 5 | Dec 4 | Dec 3 | Dec 2 | Dec 1 | Nov 30 | Nov 29 | Nov 28 | Nov 27 | Nov 26 | Nov 25 | Nov 24 | Nov 23 | Nov 22 | Nov 21 | Nov 20 | Nov 19 | Nov 18 | Nov 17 | Nov 16 | Nov 15 | Nov 14 | Nov 13 | Nov 12 | Nov 11 | Nov 10 | Nov 9 | Nov 8 | Nov 7 | Nov 6 | Nov 5 | Nov 4 | Nov 3 | Nov 2 | Nov 1 | Oct 31 | Oct 30 | Oct 29 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Windows | 3 | 9 | 3 | 5 | 3 | 6 | 4 | 3 | 5 | 4 | 3 | 6 | 4 | 7 | 2 | 8 | 1 | 1 | 7 | 9 | 10 | 2 | 6 | 7 | 6 | 4 | 5 | 8 | 4 | 2 | 6 | 4 | 5 | 9 | 5 | 6 | 5 | 3 | 4 | 2 | 7 | 7 | 5 | 7 | 1 | 8 |
Mac | 2 | 0 | 0 | 1 | 2 | 1 | 0 | 0 | 2 | 2 | 4 | 0 | 0 | 4 | 0 | 1 | 2 | 1 | 1 | 2 | 2 | 0 | 1 | 3 | 1 | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 2 | 0 | 2 | 0 | 0 | 0 | 0 | 2 | 2 | 0 | 1 | 2 | 2 |
Linux | 3 | 0 | 1 | 6 | 0 | 1 | 1 | 1 | 2 | 4 | 1 | 2 | 4 | 1 | 2 | 1 | 1 | 0 | 2 | 4 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 4 | 3 | 0 | 0 | 3 | 1 | 2 | 2 | 2 | 3 | 0 | 3 | 2 | 1 | 0 | 1 | 0 |
Readme
- Source
- raw.githubusercontent.com
A simple plugin to use the current file as source code, run it and show the result.It is just like the Mac App
CodeRunner
, but with less feature.ST3 support is in SublimeText3 branch
![Coderunner 2.1.1 Coderunner 2.1.1](https://www.zhinin.com/wp-content/uploads/2018/10/20181010084305_32645.jpg)
currently supported command :
Coderunner Extension
run_code
run_code_custom
Coderunner 3
you can bind your key to the command like this :
{ 'keys' : ['ctrl+r'], 'command': 'run_code' }