예제 #1
0
def create_exists_clone(string):
    """Create a clone of the table statement which has the exists check
    """
    parser = get_definition_start() + restOfLine.setResultsName('definition')
    result = to_dict(parser.parseString(string))
    template = 'CREATE {temp} TABLE IF NOT EXISTS {table_name} {definition}'
    return template.format(temp='TEMP' if result['temporary'] else '',
                           table_name=result['full_name'],
                           definition=result['definition'])
예제 #2
0
    def add_sections_info(self, sections_info_dump):
        first_line = sections_info_dump.readline()

        archive_path = (Literal('In archive').suppress() +
                        White().suppress() +
                        # trim the colon and line ending characters from archive_path
                        restOfLine.setResultsName('archive_path').setParseAction(lambda s, loc, toks: s.rstrip(':\n\r ')))
        parser = archive_path

        results = None

        try:
            results = parser.parseString(first_line, parseAll=True)
        except ParseException as p:
            raise ParseException('Parsing sections info for library ' + sections_info_dump.name + ' failed. ' + p.msg)

        archive = os.path.basename(results.archive_path)
        self.sections[archive] = SectionsInfo.__info(sections_info_dump.name, sections_info_dump.read())
예제 #3
0
    def add_sections_info(self, sections_info_file):
        first_line = sections_info_file.readline()

        archive_path = (
            Literal("In archive").suppress() + White().suppress() +
            # trim the colon and line ending characters from archive_path
            restOfLine.setResultsName("archive_path").setParseAction(
                lambda s, loc, toks: s.rstrip(":\n\r ")))
        parser = archive_path

        results = None

        try:
            results = parser.parseString(first_line)
        except ParseException as p:
            raise ParseException("File " + sections_info_file.name +
                                 " is not a valid sections info file. " +
                                 p.message)

        archive = os.path.basename(results.archive_path)
        self.sections[archive] = SectionsInfo.__info(sections_info_file.name,
                                                     sections_info_file.read())
예제 #4
0
DATA_PATH = 'data'


def get_files(dirpath):
    return os.listdir(dirpath)


#PATTERN_F_BORN = re.compile(u'^Родилась\sв\s(?P<year>[0-9]{4,4})\s(г|году)(\.\s|\s|\.|)$')
#PATTERN_F_BORN = re.compile(r'^Родилась\s+в\s+(?P<year>[0-9]{4,4}).*$')
PATTERN_F_BORN = lineStart + Suppress(u'Родилась') + Suppress(u'в') + Word(
    nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года'
                                                   ]).suppress() + lineEnd
PATTERN_M_BORN = lineStart + Suppress(u'Родился') + Suppress(u'в') + Word(
    nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года'
                                                   ]).suppress() + lineEnd
PATTERN_PRIG = lineStart + Suppress(u'Приговор:') + restOfLine.setResultsName(
    'prigovor') + lineEnd


def parse_all():
    for p in range(1, 39, 1):
        thep = MIRROR_PATH + 'd' + str(p) + '\\'
        for fname in get_files(thep):
            frecords = []
            resname = 'data/d' + str(p) + '_' + fname.split('.')[0] + '.json'
            if os.path.exists(resname): continue
            f = open(os.path.join(thep, fname), 'r')
            lo = parse(f)
            objs = lo.xpath("//ul[@class='list-right']/li")
            for o in objs:
                record = {}
                names = o.xpath("p[@class='name']")
def generate_spreadsheet(correction_folder):
   
    now = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime())
    
    files = sorted(f for f in  os.listdir(correction_folder) if re.match("^question\d{3}\.txt$",f))
    
    rmat = collections.defaultdict(dict)
    
    grade =(Literal("question..........:").suppress() + Word(nums+"."+",").setResultsName("Q#") +
            Literal("points............:").suppress() + Word(nums+"."+",").setResultsName("points") +
            Literal("name..............:").suppress() + restOfLine.setResultsName("name") +
            Literal("mec...............:").suppress() + Word(alphanums).setResultsName("mec") +
            Literal("automatic grade(%):").suppress() + Word(nums+"."+",").setResultsName("autgrade") +
            Literal("manual grade(%)...:").suppress() + Optional(Word(nums+"."+",")).setResultsName("mangrade"))
    
    question_number_list = []
    name_list            = []
    points               = []
    
    
    names=[]
    
    for file_ in files:
        weight=[]
        print("processing: ", file_)
        content = open(os.path.join(correction_folder, file_),"r").read()
        for data, dataStart, dataEnd in grade.scanString(content):
            name = str(data["name"].strip())
            try:
                mec  = int(data["mec"])
            except ValueError:
                mec  = str(data["mec"])
            if not rmat[mec]:
                rmat[mec] = [name, mec]
                names.append((name, mec))
            question_number_list.append(data["Q#"])
            weight.append(data["points"])
            if "mangrade" in data:
                rmat[mec].append(float( data["mangrade"]))
            else:
                rmat[mec].append(float( data["autgrade"]))
        points.extend(list(set(weight)))
    
    

    doc = newdoc(doctype='ods', filename='grades_bioinformatics_{}.ods'.format(now))
    
    doc.sheets.append(Sheet(name="grades", size=(1, 20)))
    
    sheet = doc.sheets['grades']

    def write_row(sheet, row):
        for i,v in enumerate(row):
            sheet[(sheet.nrows()-1, i)].set_value(v)
        sheet.append_rows()

    headers = (['name','mec']+
               ["Q{}".format(no) for no in sorted(set(question_number_list))]+
               ['grade(0-20)'])
    
    write_row(sheet, headers)
    write_row(sheet, ["",""] + points)
    
    from string import ascii_uppercase as letters
    
    formula="=20*("
    #print(points)
    for c in range(len(points)):
        formula += "${letter}$2*{letter}3+".format(letter = letters[c+2])
    formula = formula.rstrip("+")
    formula +=")/{} \n".format(sum([100*int(x) for x in points]))
    
    write_row(sheet, ["Max Maximus", 99999] + [100]*len(points) + [formula]+['=IF({}3<9.5,"R","")'.format(letters[c+3])])
    
    for mec in [m for n, m in sorted(names)]:
        write_row(sheet, rmat[mec])
    
    doc.save()
    input("press return")
예제 #6
0
#define USERNAME = "******"
#define PASSWORD = "******"

a = MAX_LOCS;
CORBA::initORB("xyzzy", USERNAME, PASSWORD );

"""

#################
print("Example of an extractor")
print("----------------------")

# simple grammar to match #define's
ident = Word(alphas, alphanums + "_")
macroDef = Literal("#define") + ident.setResultsName(
    "name") + "=" + restOfLine.setResultsName("value")
for t, s, e in macroDef.scanString(testData):
    print(t.name, ":", t.value)

# or a quick way to make a dictionary of the names and values
# (return only key and value tokens, and construct dict from key-value pairs)
# - empty ahead of restOfLine advances past leading whitespace, does implicit lstrip during parsing
macroDef = Suppress("#define") + ident + Suppress("=") + empty + restOfLine
macros = dict(list(macroDef.searchString(testData)))
print("macros =", macros)
print()

#################
print("Examples of a transformer")
print("----------------------")
예제 #7
0
파일: server.py 프로젝트: Lukeid/Flexget
    def __init__(self, query):
        self._methods = {
            'and': self.evaluate_and,
            'or': self.evaluate_or,
            'not': self.evaluate_not,
            'parenthesis': self.evaluate_parenthesis,
            'quotes': self.evaluate_quotes,
            'word': self.evaluate_word,
        }

        self.line = ''
        self.query = query.lower() if query else ''

        if self.query:
            # TODO: Cleanup
            operator_or = Forward()
            operator_word = Group(Word(alphanums)).setResultsName('word')

            operator_quotes_content = Forward()
            operator_quotes_content << (
                (operator_word + operator_quotes_content) | operator_word
            )

            operator_quotes = Group(
                Suppress('"') + operator_quotes_content + Suppress('"')
            ).setResultsName('quotes') | operator_word

            operator_parenthesis = Group(
                (Suppress('(') + operator_or + Suppress(")"))
            ).setResultsName('parenthesis') | operator_quotes

            operator_not = Forward()
            operator_not << (Group(
                Suppress(Keyword('no', caseless=True)) + operator_not
            ).setResultsName('not') | operator_parenthesis)

            operator_and = Forward()
            operator_and << (Group(
                operator_not + Suppress(Keyword('and', caseless=True)) + operator_and
            ).setResultsName('and') | Group(
                operator_not + OneOrMore(~oneOf('and or') + operator_and)
            ).setResultsName('and') | operator_not)

            operator_or << (Group(
                operator_and + Suppress(Keyword('or', caseless=True)) + operator_or
            ).setResultsName('or') | operator_and)

            self._query_parser = operator_or.parseString(self.query)[0]
        else:
            self._query_parser = False

        time_cmpnt = Word(nums).setParseAction(lambda t: t[0].zfill(2))
        date = Combine((time_cmpnt + '-' + time_cmpnt + '-' + time_cmpnt) + ' ' + time_cmpnt + ':' + time_cmpnt)
        word = Word(printables)

        self._log_parser = (
            date.setResultsName('timestamp') +
            word.setResultsName('log_level') +
            word.setResultsName('plugin') +
            (
                White(min=16).setParseAction(lambda s, l, t: [t[0].strip()]).setResultsName('task') |
                (White(min=1).suppress() & word.setResultsName('task'))
            ) +
            restOfLine.setResultsName('message')
        )
예제 #8
0
                 'REG_LINK',
                 'REG_MULTI_SZ',
                 'REG_RESOURCE_LIST',
                 'REG_FULL_RESOURCE_DESCRIPTOR',
                 'REG_RESOURCE_REQUIREMENTS_LIST',
                 'REG_QWORD']).setResultsName('regType')

# Transforms the ~ or = into True or False.
equals = oneOf(['~', '=']).setParseAction(lambda orig, match, tokens: tokens[0] == '~')

# Example:
# Value=Foobar
# REG_SZ~Foobaz#2300
valueLine = Group(Literal(u'Value').suppress() + \
            equals.setResultsName('nameExpand') + \
            Optional(restOfLine.setResultsName('name') + \
            regType + equals.setResultsName('dataExpand') + \
            restOfLine.setResultsName('data')))

# A key can have many values in a row or none at all.
values = ZeroOrMore(valueLine).setResultsName('values')

# A full entry looks like this:
# isolation_writecopy HKLM\Blah
#    Value=(... as above ...)
entry = isolationMode + White().suppress() + \
        restOfLine.setResultsName('path') + values

### Data types generated by ImportRegistry

class RegistryDataError(Exception):
예제 #9
0
class ExplicitStateUpdater(StateUpdateMethod):
    '''
    An object that can be used for defining state updaters via a simple
    description (see below). Resulting instances can be passed to the
    ``method`` argument of the `NeuronGroup` constructor. As other state
    updater functions the `ExplicitStateUpdater` objects are callable,
    returning abstract code when called with an `Equations` object.
    
    A description of an explicit state updater consists of a (multi-line)
    string, containing assignments to variables and a final "x_new = ...",
    stating the integration result for a single timestep. The assignments
    can be used to define an arbitrary number of intermediate results and
    can refer to ``f(x, t)`` (the function being integrated, as a function of
    ``x``, the previous value of the state variable and ``t``, the time) and
    ``dt``, the size of the timestep.
    
    For example, to define a Runge-Kutta 4 integrator (already provided as
    `rk4`), use::
    
            k1 = dt*f(x,t)
            k2 = dt*f(x+k1/2,t+dt/2)
            k3 = dt*f(x+k2/2,t+dt/2)
            k4 = dt*f(x+k3,t+dt)
            x_new = x+(k1+2*k2+2*k3+k4)/6
    
    Note that for stochastic equations, the function `f` only corresponds to
    the non-stochastic part of the equation. The additional function `g`
    corresponds to the stochastic part that has to be multiplied with the 
    stochastic variable xi (a standard normal random variable -- if the
    algorithm needs a random variable with a different variance/mean you have
    to multiply/add it accordingly). Equations with more than one
    stochastic variable do not have to be treated differently, the part
    referring to ``g`` is repeated for all stochastic variables automatically.
     
    Stochastic integrators can also make reference to ``dW`` (a normal
    distributed random number with variance ``dt``) and ``g(x, t)``, the
    stochastic part of an equation. A stochastic state updater could therefore
    use a description like::
        
        x_new = x + dt*f(x,t) + g(x, t) * dW
    
    For simplicity, the same syntax is used for state updaters that only support
    additive noise, even though ``g(x, t)`` does not depend on ``x`` or ``t``
    in that case.    
    
    There a some restrictions on the complexity of the expressions (but most
    can be worked around by using intermediate results as in the above Runge-
    Kutta example): Every statement can only contain the functions ``f`` and
    ``g`` once; The expressions have to be linear in the functions, e.g. you
    can use ``dt*f(x, t)`` but not ``f(x, t)**2``.
     
    Parameters
    ----------
    description : str
        A state updater description (see above).
    stochastic : {None, 'additive', 'multiplicative'}
        What kind of stochastic equations this state updater supports: ``None``
        means no support of stochastic equations, ``'additive'`` means only
        equations with additive noise and ``'multiplicative'`` means
        supporting arbitrary stochastic equations.
    
    Raises
    ------
    ValueError
        If the parsing of the description failed.
    
    Notes
    -----
    Since clocks are updated *after* the state update, the time ``t`` used
    in the state update step is still at its previous value. Enumerating the
    states and discrete times, ``x_new = x + dt*f(x, t)`` is therefore
    understood as :math:`x_{i+1} = x_i + dt f(x_i, t_i)`, yielding the correct
    forward Euler integration. If the integrator has to refer to the time at
    the end of the timestep, simply use ``t + dt`` instead of ``t``. 
    
    See also
    --------
    euler, rk2, rk4, milstein
    '''

    #===========================================================================
    # Parsing definitions
    #===========================================================================
    #: Legal names for temporary variables
    TEMP_VAR = ~Literal('x_new') + Word(
        string.ascii_letters + '_', string.ascii_letters + string.digits +
        '_').setResultsName('identifier')

    #: A single expression
    EXPRESSION = restOfLine.setResultsName('expression')

    #: An assignment statement
    STATEMENT = Group(TEMP_VAR + Suppress('=') +
                      EXPRESSION).setResultsName('statement')

    #: The last line of a state updater description
    OUTPUT = Group(Suppress(Literal('x_new')) + Suppress('=') +
                   EXPRESSION).setResultsName('output')

    #: A complete state updater description
    DESCRIPTION = ZeroOrMore(STATEMENT) + OUTPUT

    def __init__(self, description, stochastic=None):
        self._description = description
        self.stochastic = stochastic

        try:
            parsed = ExplicitStateUpdater.DESCRIPTION.parseString(
                description, parseAll=True)
        except ParseException as p_exc:
            ex = SyntaxError('Parsing failed: ' + str(p_exc.msg))
            ex.text = str(p_exc.line)
            ex.offset = p_exc.column
            ex.lineno = p_exc.lineno
            raise ex

        self.statements = []
        self.symbols = SYMBOLS.copy()
        for element in parsed:
            expression = str_to_sympy(element.expression)
            # Replace all symbols used in state updater expressions by unique
            # names that cannot clash with user-defined variables or functions
            expression = expression.subs(sympy.Function('f'),
                                         self.symbols['__f'])
            expression = expression.subs(sympy.Function('g'),
                                         self.symbols['__g'])
            symbols = list(expression.atoms(sympy.Symbol))
            unique_symbols = []
            for symbol in symbols:
                if symbol.name == 'dt':
                    unique_symbols.append(symbol)
                else:
                    unique_symbols.append(_symbol('__' + symbol.name))
            for symbol, unique_symbol in zip(symbols, unique_symbols):
                expression = expression.subs(symbol, unique_symbol)

            self.symbols.update(
                dict(((symbol.name, symbol) for symbol in unique_symbols)))
            if element.getName() == 'statement':
                self.statements.append(('__' + element.identifier, expression))
            elif element.getName() == 'output':
                self.output = expression
            else:
                raise AssertionError('Unknown element name: %s' %
                                     element.getName())

    def can_integrate(self, equations, variables):
        # Non-stochastic numerical integrators should work for all equations,
        # except for stochastic equations
        if equations.is_stochastic and self.stochastic is None:
            return False
        elif (equations.stochastic_type == 'multiplicative'
              and self.stochastic != 'multiplicative'):
            return False
        else:
            return True

    def __repr__(self):
        # recreate a description string
        description = '\n'.join(
            ['%s = %s' % (var, expr) for var, expr in self.statements])
        if len(description):
            description += '\n'
        description += 'x_new = ' + str(self.output)
        r = "{classname}('''{description}''', stochastic={stochastic})"
        return r.format(classname=self.__class__.__name__,
                        description=description,
                        stochastic=repr(self.stochastic))

    def __str__(self):
        s = '%s\n' % self.__class__.__name__

        if len(self.statements) > 0:
            s += 'Intermediate statements:\n'
            s += '\n'.join([(var + ' = ' + sympy_to_str(expr))
                            for var, expr in self.statements])
            s += '\n'

        s += 'Output:\n'
        s += sympy_to_str(self.output)
        return s

    def _latex(self, *args):
        from sympy import latex, Symbol
        s = [r'\begin{equation}']
        for var, expr in self.statements:
            expr = expr.subs(Symbol('x'), Symbol('x_t'))
            s.append(latex(Symbol(var)) + ' = ' + latex(expr) + r'\\')
        expr = self.output.subs(Symbol('x'), 'x_t')
        s.append(r'x_{t+1} = ' + latex(expr))
        s.append(r'\end{equation}')
        return '\n'.join(s)

    def _repr_latex_(self):
        return self._latex()

    def _generate_RHS(self, eqs, var, eq_symbols, temp_vars, expr,
                      non_stochastic_expr, stochastic_expr):
        '''
        Helper function used in `__call__`. Generates the right hand side of
        an abstract code statement by appropriately replacing f, g and t.
        For example, given a differential equation ``dv/dt = -(v + I) / tau``
        (i.e. `var` is ``v` and `expr` is ``(-v + I) / tau``) together with
        the `rk2` step ``return x + dt*f(x +  k/2, t + dt/2)``
        (i.e. `non_stochastic_expr` is
        ``x + dt*f(x +  k/2, t + dt/2)`` and `stochastic_expr` is ``None``),
        produces ``v + dt*(-v - _k_v/2 + I + _k_I/2)/tau``.
                
        '''
        def replace_func(x, t, expr, temp_vars):
            '''
            Used to replace a single occurance of ``f(x, t)`` or ``g(x, t)``:
            `expr` is the non-stochastic (in the case of ``f``) or stochastic
            part (``g``) of the expression defining the right-hand-side of the
            differential equation describing `var`. It replaces the variable
            `var` with the value given as `x` and `t` by the value given for
            `t. Intermediate variables will be replaced with the appropriate
            replacements as well.
            
            For example, in the `rk2` integrator, the second step involves the
            calculation of ``f(k/2 + x, dt/2 + t)``.  If `var` is ``v`` and
            `expr` is ``-v / tau``, this will result in ``-(_k_v/2 + v)/tau``.
            
            Note that this deals with only one state variable `var`, given as
            an argument to the surrounding `_generate_RHS` function.
            '''

            try:
                s_expr = str_to_sympy(str(expr))
            except SympifyError as ex:
                raise ValueError('Error parsing the expression "%s": %s' %
                                 (expr, str(ex)))

            for var in eq_symbols:
                # Generate specific temporary variables for the state variable,
                # e.g. '_k_v' for the state variable 'v' and the temporary
                # variable 'k'.
                temp_var_replacements = dict(
                    ((self.symbols[temp_var], _symbol(temp_var + '_' + var))
                     for temp_var in temp_vars))
                # In the expression given as 'x', replace 'x' by the variable
                # 'var' and all the temporary variables by their
                # variable-specific counterparts.
                x_replacement = x.subs(self.symbols['__x'], eq_symbols[var])
                x_replacement = x_replacement.subs(temp_var_replacements)

                # Replace the variable `var` in the expression by the new `x`
                # expression
                s_expr = s_expr.subs(eq_symbols[var], x_replacement)

            # Directly substitute the 't' expression for the symbol t, there
            # are no temporary variables to consider here.
            s_expr = s_expr.subs(self.symbols['__t'], t)

            return s_expr

        # Note: in the following we are silently ignoring the case that a
        # state updater does not care about either the non-stochastic or the
        # stochastic part of an equation. We do trust state updaters to
        # correctly specify their own abilities (i.e. they do not claim to
        # support stochastic equations but actually just ignore the stochastic
        # part). We can't really check the issue here, as we are only dealing
        # with one line of the state updater description. It is perfectly valid
        # to write the euler update as:
        #     non_stochastic = dt * f(x, t)
        #     stochastic = dt**.5 * g(x, t) * xi
        #     return x + non_stochastic + stochastic
        #
        # In the above case, we'll deal with lines which do not define either
        # the stochastic or the non-stochastic part.

        non_stochastic, stochastic = expr.split_stochastic()
        # We do have a non-stochastic part in our equation and in the state
        # updater description
        if not (non_stochastic is None or non_stochastic_expr is None):
            # Replace the f(x, t) part
            replace_f = lambda x, t: replace_func(x, t, non_stochastic,
                                                  temp_vars)
            non_stochastic_result = non_stochastic_expr.replace(
                self.symbols['__f'], replace_f)
            # Replace x by the respective variable
            non_stochastic_result = non_stochastic_result.subs(
                self.symbols['__x'], eq_symbols[var])
            # Replace intermediate variables
            temp_var_replacements = dict(
                (self.symbols[temp_var], _symbol(temp_var + '_' + var))
                for temp_var in temp_vars)
            non_stochastic_result = non_stochastic_result.subs(
                temp_var_replacements)
        else:
            non_stochastic_result = None

        # We do have a stochastic part in our equation and in the state updater
        # description
        if not (stochastic is None or stochastic_expr is None):
            stochastic_results = []

            # We potentially have more than one stochastic variable
            for xi in stochastic:
                # Replace the g(x, t)*xi part
                replace_g = lambda x, t: replace_func(x, t, stochastic[xi],
                                                      temp_vars)
                stochastic_result = stochastic_expr.replace(
                    self.symbols['__g'], replace_g)

                # Replace x and xi by the respective variables
                stochastic_result = stochastic_result.subs(
                    self.symbols['__x'], eq_symbols[var])
                stochastic_result = stochastic_result.subs(
                    self.symbols['__dW'], xi)

                # Replace intermediate variables
                temp_var_replacements = dict(
                    (self.symbols[temp_var], _symbol(temp_var + '_' + var))
                    for temp_var in temp_vars)

                stochastic_result = stochastic_result.subs(
                    temp_var_replacements)

                stochastic_results.append(stochastic_result)
        else:
            stochastic_results = []

        RHS = []
        # All the parts (one non-stochastic and potentially more than one
        # stochastic part) are combined with addition
        if non_stochastic_result is not None:
            RHS.append(sympy_to_str(non_stochastic_result))
        for stochastic_result in stochastic_results:
            RHS.append(sympy_to_str(stochastic_result))

        RHS = ' + '.join(RHS)
        return RHS

    def __call__(self, eqs, variables=None):
        '''
        Apply a state updater description to model equations.
        
        Parameters
        ----------
        eqs : `Equations`
            The equations describing the model

        
        variables: dict-like, optional
            The `Variable` objects for the model. Ignored by the explicit
            state updater.
        
        Examples
        --------
        >>> from brian2 import *
        >>> eqs = Equations('dv/dt = -v / tau : volt')        
        >>> print(euler(eqs))
        _v = -dt*v/tau + v
        v = _v
        >>> print(rk4(eqs))
        __k_1_v = -dt*v/tau
        __k_2_v = -dt*(0.5*__k_1_v + v)/tau
        __k_3_v = -dt*(0.5*__k_2_v + v)/tau
        __k_4_v = -dt*(__k_3_v + v)/tau
        _v = 0.166666666666667*__k_1_v + 0.333333333333333*__k_2_v + 0.333333333333333*__k_3_v + 0.166666666666667*__k_4_v + v
        v = _v
        '''

        # The final list of statements
        statements = []

        # The variables for the intermediate results in the state updater
        # description, e.g. the variable k in rk2
        intermediate_vars = [var for var, expr in self.statements]

        # A dictionary mapping all the variables in the equations to their
        # sympy representations
        eq_variables = dict(((var, _symbol(var)) for var in eqs.eq_names))

        # Generate the random numbers for the stochastic variables
        stochastic_variables = eqs.stochastic_variables
        for stochastic_variable in stochastic_variables:
            statements.append(stochastic_variable + ' = ' + 'dt**.5 * randn()')

        # Process the intermediate statements in the stateupdater description
        for intermediate_var, intermediate_expr in self.statements:

            # Split the expression into a non-stochastic and a stochastic part
            non_stochastic_expr, stochastic_expr = split_expression(
                intermediate_expr)

            # Execute the statement by appropriately replacing the functions f
            # and g and the variable x for every equation in the model.
            # We use the model equations where the subexpressions have
            # already been substituted into the model equations.
            for var, expr in eqs.substituted_expressions:
                RHS = self._generate_RHS(eqs, var, eq_variables,
                                         intermediate_vars, expr,
                                         non_stochastic_expr, stochastic_expr)
                statements.append(intermediate_var + '_' + var + ' = ' + RHS)

        # Process the "return" line of the stateupdater description
        non_stochastic_expr, stochastic_expr = split_expression(self.output)

        # Assign a value to all the model variables described by differential
        # equations
        for var, expr in eqs.substituted_expressions:
            RHS = self._generate_RHS(eqs, var, eq_variables, intermediate_vars,
                                     expr, non_stochastic_expr,
                                     stochastic_expr)
            statements.append('_' + var + ' = ' + RHS)

        # Assign everything to the final variables
        for var, expr in eqs.substituted_expressions:
            statements.append(var + ' = ' + '_' + var)

        return '\n'.join(statements)
예제 #10
0
# 'Admin' commands for point modifications.
point_commands = ['addpoints', 'subpoints', 'setpoints']

# 'Admin' commands for channel metadata modifications.
admin_commands = {
    'setgame': api.set_game,
    'settitle': api.set_title,
}

# Parser to grab command keywords from chat messages.
username = Word(alphanums + '_').setResultsName('username')
irc_garb = Word(alphanums + '_!@.') + 'PRIVMSG' + '#hwangbroxd'
cmd = ':!' + Word(alphas).setResultsName('cmd')
new_cmd = Optional(Combine('!' + Word(alphanums).setResultsName('new_cmd')))
msg = restOfLine.setResultsName('msg')

parser = ':' + username + irc_garb + cmd + new_cmd + msg
chat_parser = ':' + username + irc_garb + ':' + msg


class Message:
    """Represents a chat message.

    Has methods to parse internally for different types of
    commands and to assign variables accordingly.
    """
    def __init__(self, text):
        self.username = self.message = self.command = self.metacommand = self.command_body = self.points_user = ''
        self.is_command = False
        self.is_admin = False
예제 #11
0
    def __init__(self):
        filename = os.path.join(paths.lib_dir(), 'bauble.glade')
        self.widgets = utils.load_widgets(filename)
        self.window = self.widgets.main_window
        self.window.hide()

        # restore the window size
        geometry = prefs[self.window_geometry_pref]
        if geometry is not None:
            self.window.set_default_size(*geometry)

        self.window.connect('delete-event', self.on_delete_event)
        self.window.connect("destroy", self.on_quit)
        self.window.set_title(self.title)

        try:
            pixbuf = gtk.gdk.pixbuf_new_from_file(bauble.default_icon)
            self.window.set_icon(pixbuf)
        except Exception:
            logger.warning(
                _('Could not load icon from %s') % bauble.default_icon)
            logger.warning(traceback.format_exc())

        menubar = self.create_main_menu()
        self.widgets.menu_box.pack_start(menubar)

        combo = self.widgets.main_comboentry
        model = gtk.ListStore(str)
        combo.set_model(model)
        self.populate_main_entry()

        main_entry = combo.child
        main_entry.connect('activate', self.on_main_entry_activate)
        accel_group = gtk.AccelGroup()
        main_entry.add_accelerator("grab-focus", accel_group, ord('L'),
                                   gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        self.window.add_accel_group(accel_group)

        go_button = self.widgets.go_button
        go_button.connect('clicked', self.on_go_button_clicked)

        query_button = self.widgets.query_button
        query_button.connect('clicked', self.on_query_button_clicked)

        self.set_default_view()

        # add a progressbar to the status bar
        # Warning: this relies on gtk.Statusbar internals and could break in
        # future versions of gtk
        statusbar = self.widgets.statusbar
        statusbar.set_spacing(10)
        statusbar.set_has_resize_grip(True)
        self._cids = []

        def on_statusbar_push(sb, cid, txt):
            if cid not in self._cids:
                self._cids.append(cid)

        statusbar.connect('text-pushed', on_statusbar_push)

        # remove label from frame
        frame = statusbar.get_children()[0]
        #frame.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#FF0000'))
        label = frame.get_children()[0]
        frame.remove(label)

        # replace label with hbox and put label and progress bar in hbox
        hbox = gtk.HBox(False, 5)
        frame.add(hbox)
        hbox.pack_start(label, True, True, 0)
        vbox = gtk.VBox(True, 0)
        hbox.pack_end(vbox, False, True, 15)
        self.progressbar = gtk.ProgressBar()
        vbox.pack_start(self.progressbar, False, False, 0)
        self.progressbar.set_size_request(-1, 10)
        vbox.show()
        hbox.show()

        from pyparsing import StringStart, Word, alphanums, restOfLine, \
            StringEnd
        cmd = StringStart() + ':' + Word(alphanums +
                                         '-_').setResultsName('cmd')
        arg = restOfLine.setResultsName('arg')
        self.cmd_parser = (cmd + StringEnd()) | (cmd + '=' + arg) | arg

        combo.grab_focus()
예제 #12
0
special_chars = ' -'''
allchars = alphanums + alphas8bit + extended_chars + special_chars

# Prepare months
RUS_MONTH_NAMES = [u'января', u'февраля', u'марта', u'апреля', u'мая', u'июня', u'июля', u'августа', u'сентября', u'октября', u'ноября', u'декабря']
RUS_MONTH_MAP = {}
i = 0
for k in RUS_MONTH_NAMES:
	i += 1
	RUS_MONTH_MAP[k] = i

PAT_YEAR = lineStart + Suppress(u'в') + Word(nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года']).suppress() + lineEnd
PAT_RUDATE = lineStart + Word(nums, min=1, max=2).setResultsName('day') + oneOf(RUS_MONTH_NAMES).setResultsName('month_r') + Word(nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года']).suppress() + lineEnd
PAT_RUDATE_WHO = lineStart + OneOrMore(Word(allchars)).setResultsName('who') + Word(nums, min=1, max=2).setResultsName('day') + oneOf(RUS_MONTH_NAMES).setResultsName('month_r') + Word(nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года']).suppress() + lineEnd

PAT_OBV = lineStart + Suppress(u', обв.:') + restOfLine.setResultsName('restof') + lineEnd
PAT_OBV_YEAR = lineStart + Suppress(u'в') + Word(nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года']).suppress() + Suppress(u', обв.:') + restOfLine.setResultsName('restof') + lineEnd
PAT_OBV_RUDATE = lineStart + Word(nums, min=1, max=2).setResultsName('day') + oneOf(RUS_MONTH_NAMES).setResultsName('month_r') + Word(nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года']).suppress() + Suppress(u', обв.:') + restOfLine.setResultsName('restof') + lineEnd

PAT_OBV_WHO = lineStart + OneOrMore(Word(allchars)).setResultsName('who') + Word(nums, min=1, max=2).setResultsName('day') + oneOf(RUS_MONTH_NAMES).setResultsName('month_r') + Word(nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года']).suppress() + Suppress(u', обв.:') + restOfLine.setResultsName('restof') + lineEnd


PAT_BORN_P = lineStart + Suppress(u'в') + Word(nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года']).suppress() +  Suppress(u',') + restOfLine.setResultsName('restof') + lineEnd

#PATTERN_F_BORN = re.compile(u'^Родилась\sв\s(?P<year>[0-9]{4,4})\s(г|году)(\.\s|\s|\.|)$')	
#PATTERN_F_BORN = re.compile(r'^Родилась\s+в\s+(?P<year>[0-9]{4,4}).*$')	
#PATTERN_F_BORN = lineStart + Suppress(u'Родилась') + Suppress(u'в') + Word(nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года']).suppress() + lineEnd
#PATTERN_M_BORN = lineStart + Suppress(u'Родился') + Suppress(u'в') + Word(nums, exact=4).setResultsName('year') + oneOf([u'г.', u'года']).suppress() + lineEnd
PATTERN_PRIG = lineStart + Suppress(u'Приговор:') + restOfLine.setResultsName('restof') + lineEnd
PATTERN_FBORN = lineStart + Suppress(u'Родилась') + restOfLine.setResultsName('restof') + lineEnd
PATTERN_MBORN = lineStart + Suppress(u'Родился') + restOfLine.setResultsName('restof') + lineEnd
예제 #13
0
    'REG_NONE', 'REG_SZ', 'REG_EXPAND_SZ', 'REG_BINARY', 'REG_DWORD',
    'REG_DWORD_BIG_ENDIAN', 'REG_LINK', 'REG_MULTI_SZ', 'REG_RESOURCE_LIST',
    'REG_FULL_RESOURCE_DESCRIPTOR', 'REG_RESOURCE_REQUIREMENTS_LIST',
    'REG_QWORD'
]).setResultsName('regType')

# Transforms the ~ or = into True or False.
equals = oneOf(['~', '='
                ]).setParseAction(lambda orig, match, tokens: tokens[0] == '~')

# Example:
# Value=Foobar
# REG_SZ~Foobaz#2300
valueLine = Group(Literal(u'Value').suppress() + \
            equals.setResultsName('nameExpand') + \
            Optional(restOfLine.setResultsName('name') + \
            regType + equals.setResultsName('dataExpand') + \
            restOfLine.setResultsName('data')))

# A key can have many values in a row or none at all.
values = ZeroOrMore(valueLine).setResultsName('values')

# A full entry looks like this:
# isolation_writecopy HKLM\Blah
#    Value=(... as above ...)
entry = isolationMode + White().suppress() + \
        restOfLine.setResultsName('path') + values

### Data types generated by ImportRegistry

예제 #14
0
from sympy import Symbol, Function
from pyparsing import (Literal, Group, Word, ZeroOrMore, Suppress, restOfLine,
                       ParseException)

from brian2.utils.parsing import parse_to_sympy

__all__ = ['euler', 'rk2', 'rk4', 'ExplicitStateUpdater']

#===============================================================================
# Parsing definitions
#===============================================================================
TEMP_VAR = Word(string.ascii_letters + '_',
                string.ascii_letters + string.digits + '_').setResultsName('identifier')

EXPRESSION = restOfLine.setResultsName('expression')

STATEMENT = Group(TEMP_VAR + Suppress('=') + EXPRESSION).setResultsName('statement')

OUTPUT = Group(Suppress(Literal('return ')) + EXPRESSION).setResultsName('output')

DESCRIPTION = ZeroOrMore(STATEMENT) + OUTPUT 

#===============================================================================
# Class for simple definition of explicit state updaters
#===============================================================================

# reserved standard symbols
SYMBOLS = {'x' : Symbol('x'),
           't' : Symbol('t'),
           'dt': Symbol('dt'),
예제 #15
0
#define MAX_LOCS=100
#define USERNAME = "******"
#define PASSWORD = "******"

a = MAX_LOCS;
CORBA::initORB("xyzzy", USERNAME, PASSWORD );

"""

#################
print("Example of an extractor")
print("----------------------")

# simple grammar to match #define's
ident = Word(alphas, alphanums+"_")
macroDef = Literal("#define") + ident.setResultsName("name") + "=" + restOfLine.setResultsName("value")
for t,s,e in macroDef.scanString( testData ):
    print(t.name,":", t.value)
    
# or a quick way to make a dictionary of the names and values 
# (return only key and value tokens, and construct dict from key-value pairs)
# - empty ahead of restOfLine advances past leading whitespace, does implicit lstrip during parsing
macroDef = Suppress("#define") + ident + Suppress("=") + empty + restOfLine
macros = dict(list(macroDef.searchString(testData)))
print("macros =", macros)
print()


#################
print("Examples of a transformer")
print("----------------------")
예제 #16
0
class ExplicitStateUpdater(StateUpdateMethod):
    """
    An object that can be used for defining state updaters via a simple
    description (see below). Resulting instances can be passed to the
    ``method`` argument of the `NeuronGroup` constructor. As other state
    updater functions the `ExplicitStateUpdater` objects are callable,
    returning abstract code when called with an `Equations` object.
    
    A description of an explicit state updater consists of a (multi-line)
    string, containing assignments to variables and a final "x_new = ...",
    stating the integration result for a single timestep. The assignments
    can be used to define an arbitrary number of intermediate results and
    can refer to ``f(x, t)`` (the function being integrated, as a function of
    ``x``, the previous value of the state variable and ``t``, the time) and
    ``dt``, the size of the timestep.
    
    For example, to define a Runge-Kutta 4 integrator (already provided as
    `rk4`), use::
    
            k1 = dt*f(x,t)
            k2 = dt*f(x+k1/2,t+dt/2)
            k3 = dt*f(x+k2/2,t+dt/2)
            k4 = dt*f(x+k3,t+dt)
            x_new = x+(k1+2*k2+2*k3+k4)/6
    
    Note that for stochastic equations, the function `f` only corresponds to
    the non-stochastic part of the equation. The additional function `g`
    corresponds to the stochastic part that has to be multiplied with the 
    stochastic variable xi (a standard normal random variable -- if the
    algorithm needs a random variable with a different variance/mean you have
    to multiply/add it accordingly). Equations with more than one
    stochastic variable do not have to be treated differently, the part
    referring to ``g`` is repeated for all stochastic variables automatically.
     
    Stochastic integrators can also make reference to ``dW`` (a normal
    distributed random number with variance ``dt``) and ``g(x, t)``, the
    stochastic part of an equation. A stochastic state updater could therefore
    use a description like::
        
        x_new = x + dt*f(x,t) + g(x, t) * dW
    
    For simplicity, the same syntax is used for state updaters that only support
    additive noise, even though ``g(x, t)`` does not depend on ``x`` or ``t``
    in that case.    
    
    There a some restrictions on the complexity of the expressions (but most
    can be worked around by using intermediate results as in the above Runge-
    Kutta example): Every statement can only contain the functions ``f`` and
    ``g`` once; The expressions have to be linear in the functions, e.g. you
    can use ``dt*f(x, t)`` but not ``f(x, t)**2``.
     
    Parameters
    ----------
    description : str
        A state updater description (see above).
    stochastic : {None, 'additive', 'multiplicative'}
        What kind of stochastic equations this state updater supports: ``None``
        means no support of stochastic equations, ``'additive'`` means only
        equations with additive noise and ``'multiplicative'`` means
        supporting arbitrary stochastic equations.
    
    Raises
    ------
    ValueError
        If the parsing of the description failed.
    
    Notes
    -----
    Since clocks are updated *after* the state update, the time ``t`` used
    in the state update step is still at its previous value. Enumerating the
    states and discrete times, ``x_new = x + dt*f(x, t)`` is therefore
    understood as :math:`x_{i+1} = x_i + dt f(x_i, t_i)`, yielding the correct
    forward Euler integration. If the integrator has to refer to the time at
    the end of the timestep, simply use ``t + dt`` instead of ``t``. 
    
    See also
    --------
    euler, rk2, rk4, milstein
    """ 
    
    #===========================================================================
    # Parsing definitions
    #===========================================================================
    #: Legal names for temporary variables
    TEMP_VAR = ~Literal('x_new') + Word(f"{string.ascii_letters}_",
                                        f"{string.ascii_letters + string.digits}_").setResultsName('identifier')
    
    #: A single expression
    EXPRESSION = restOfLine.setResultsName('expression')
    
    #: An assignment statement
    STATEMENT = Group(TEMP_VAR + Suppress('=') +
                      EXPRESSION).setResultsName('statement')
    
    #: The last line of a state updater description
    OUTPUT = Group(Suppress(Literal('x_new')) + Suppress('=') + EXPRESSION).setResultsName('output')
    
    #: A complete state updater description
    DESCRIPTION = ZeroOrMore(STATEMENT) + OUTPUT
     
    def __init__(self, description, stochastic=None, custom_check=None):
        self._description = description
        self.stochastic = stochastic
        self.custom_check = custom_check

        try:
            parsed = ExplicitStateUpdater.DESCRIPTION.parseString(description,
                                                                  parseAll=True)
        except ParseException as p_exc:
            ex = SyntaxError(f"Parsing failed: {str(p_exc.msg)}")
            ex.text = str(p_exc.line)
            ex.offset = p_exc.column
            ex.lineno = p_exc.lineno
            raise ex
 
        self.statements = []
        self.symbols = SYMBOLS.copy()
        for element in parsed:
            expression = str_to_sympy(element.expression)
            # Replace all symbols used in state updater expressions by unique
            # names that cannot clash with user-defined variables or functions
            expression = expression.subs(sympy.Function('f'),
                                         self.symbols['__f'])
            expression = expression.subs(sympy.Function('g'),
                                         self.symbols['__g'])
            symbols = list(expression.atoms(sympy.Symbol))
            unique_symbols = []
            for symbol in symbols:
                if symbol.name == 'dt':
                    unique_symbols.append(symbol)
                else:
                    unique_symbols.append(_symbol(f"__{symbol.name}"))
            for symbol, unique_symbol in zip(symbols, unique_symbols):
                expression = expression.subs(symbol, unique_symbol)

            self.symbols.update(dict(((symbol.name, symbol)
                                      for symbol in unique_symbols)))
            if element.getName() == 'statement':
                self.statements.append((f"__{element.identifier}", expression))
            elif element.getName() == 'output':
                self.output = expression
            else:
                raise AssertionError(f'Unknown element name: {element.getName()}')

    def __repr__(self):
        # recreate a description string
        description = '\n'.join([f'{var} = {expr}'
                                 for var, expr in self.statements])
        if len(description):
            description += '\n'
        description += f"x_new = {str(self.output)}"
        classname = self.__class__.__name__
        return f"{classname}('''{description}''', stochastic={self.stochastic!r})"

    def __str__(self):
        s = f'{self.__class__.__name__}\n'
        
        if len(self.statements) > 0:
            s += 'Intermediate statements:\n'
            s += '\n'.join([f"{var} = {sympy_to_str(expr)}"
                            for var, expr in self.statements])
            s += '\n'
            
        s += 'Output:\n'
        s += sympy_to_str(self.output)
        return s

    def _latex(self, *args):
        from sympy import latex, Symbol
        s = [r'\begin{equation}']
        for var, expr in self.statements:      
            expr = expr.subs(Symbol('x'), Symbol('x_t'))      
            s.append(f"{latex(Symbol(var))} = {latex(expr)}\\\\")
        expr = self.output.subs(Symbol('x'), 'x_t')
        s.append(f"x_{{t+1}} = {latex(expr)}")
        s.append(r'\end{equation}')
        return '\n'.join(s)

    def _repr_latex_(self):
        return self._latex()

    def replace_func(self, x, t, expr, temp_vars, eq_symbols,
                     stochastic_variable=None):
        """
        Used to replace a single occurance of ``f(x, t)`` or ``g(x, t)``:
        `expr` is the non-stochastic (in the case of ``f``) or stochastic
        part (``g``) of the expression defining the right-hand-side of the
        differential equation describing `var`. It replaces the variable
        `var` with the value given as `x` and `t` by the value given for
        `t`. Intermediate variables will be replaced with the appropriate
        replacements as well.

        For example, in the `rk2` integrator, the second step involves the
        calculation of ``f(k/2 + x, dt/2 + t)``.  If `var` is ``v`` and
        `expr` is ``-v / tau``, this will result in ``-(_k_v/2 + v)/tau``.

        Note that this deals with only one state variable `var`, given as
        an argument to the surrounding `_generate_RHS` function.
        """

        try:
            s_expr = str_to_sympy(str(expr))
        except SympifyError as ex:
            raise ValueError(f'Error parsing the expression "{expr}": {str(ex)}')

        for var in eq_symbols:
            # Generate specific temporary variables for the state variable,
            # e.g. '_k_v' for the state variable 'v' and the temporary
            # variable 'k'.
            if stochastic_variable is None:
                temp_var_replacements = dict(((self.symbols[temp_var],
                                               _symbol(f"{temp_var}_{var}"))
                                              for temp_var in temp_vars))
            else:
                temp_var_replacements = dict(((self.symbols[temp_var],
                                               _symbol(f"{temp_var}_{var}_{stochastic_variable}"))
                                              for temp_var in temp_vars))
            # In the expression given as 'x', replace 'x' by the variable
            # 'var' and all the temporary variables by their
            # variable-specific counterparts.
            x_replacement = x.subs(self.symbols['__x'], eq_symbols[var])
            x_replacement = x_replacement.subs(temp_var_replacements)

            # Replace the variable `var` in the expression by the new `x`
            # expression
            s_expr = s_expr.subs(eq_symbols[var], x_replacement)

        # If the expression given for t in the state updater description
        # is not just "t" (or rather "__t"), then replace t in the
        # equations by it, and replace "__t" by "t" afterwards.
        if t != self.symbols['__t']:
            s_expr = s_expr.subs(SYMBOLS['t'], t)
            s_expr = s_expr.replace(self.symbols['__t'], SYMBOLS['t'])

        return s_expr

    def _non_stochastic_part(self, eq_symbols, non_stochastic,
                             non_stochastic_expr, stochastic_variable,
                             temp_vars, var):
        non_stochastic_results = []
        if stochastic_variable is None or len(stochastic_variable) == 0:
            # Replace the f(x, t) part
            replace_f = lambda x, t: self.replace_func(x, t, non_stochastic,
                                                  temp_vars, eq_symbols)
            non_stochastic_result = non_stochastic_expr.replace(
                self.symbols['__f'],
                replace_f)
            # Replace x by the respective variable
            non_stochastic_result = non_stochastic_result.subs(
                self.symbols['__x'],
                eq_symbols[var])
            # Replace intermediate variables
            temp_var_replacements = dict((self.symbols[temp_var],
                                          _symbol(f"{temp_var}_{var}"))
                                         for temp_var in temp_vars)
            non_stochastic_result = non_stochastic_result.subs(
                temp_var_replacements)
            non_stochastic_results.append(non_stochastic_result)
        elif isinstance(stochastic_variable, str):
            # Replace the f(x, t) part
            replace_f = lambda x, t: self.replace_func(x, t, non_stochastic,
                                                  temp_vars, eq_symbols,
                                                  stochastic_variable)
            non_stochastic_result = non_stochastic_expr.replace(
                self.symbols['__f'],
                replace_f)
            # Replace x by the respective variable
            non_stochastic_result = non_stochastic_result.subs(
                self.symbols['__x'],
                eq_symbols[var])
            # Replace intermediate variables
            temp_var_replacements = dict((self.symbols[temp_var],
                                          _symbol(
                                              f"{temp_var}_{var}_{stochastic_variable}"))
                                         for temp_var in temp_vars)

            non_stochastic_result = non_stochastic_result.subs(
                temp_var_replacements)
            non_stochastic_results.append(non_stochastic_result)
        else:
            # Replace the f(x, t) part
            replace_f = lambda x, t: self.replace_func(x, t, non_stochastic,
                                                  temp_vars, eq_symbols)
            non_stochastic_result = non_stochastic_expr.replace(
                self.symbols['__f'],
                replace_f)
            # Replace x by the respective variable
            non_stochastic_result = non_stochastic_result.subs(
                self.symbols['__x'],
                eq_symbols[var])
            # Replace intermediate variables
            temp_var_replacements = dict((self.symbols[temp_var],
                                          reduce(operator.add, [_symbol(
                                              f"{temp_var}_{var}_{xi}")
                                                                for xi in
                                                                stochastic_variable]))
                                         for temp_var in temp_vars)

            non_stochastic_result = non_stochastic_result.subs(
                temp_var_replacements)
            non_stochastic_results.append(non_stochastic_result)

        return non_stochastic_results

    def _stochastic_part(self, eq_symbols, stochastic, stochastic_expr,
                         stochastic_variable, temp_vars, var):
        stochastic_results = []
        if isinstance(stochastic_variable, str):
            # Replace the g(x, t) part
            replace_f = lambda x, t: self.replace_func(x, t,
                                                       stochastic.get(stochastic_variable, 0),
                                                       temp_vars, eq_symbols,
                                                       stochastic_variable)
            stochastic_result = stochastic_expr.replace(self.symbols['__g'],
                                                        replace_f)
            # Replace x by the respective variable
            stochastic_result = stochastic_result.subs(self.symbols['__x'],
                                                       eq_symbols[var])
            # Replace dW by the respective variable
            stochastic_result = stochastic_result.subs(self.symbols['__dW'],
                                                       stochastic_variable)
            # Replace intermediate variables
            temp_var_replacements = dict((self.symbols[temp_var],
                                          _symbol(
                                              f"{temp_var}_{var}_{stochastic_variable}"))
                                         for temp_var in temp_vars)

            stochastic_result = stochastic_result.subs(temp_var_replacements)
            stochastic_results.append(stochastic_result)
        else:
            for xi in stochastic_variable:
                # Replace the g(x, t) part
                replace_f = lambda x, t: self.replace_func(x, t,
                                                           stochastic.get(xi, 0),
                                                           temp_vars,
                                                           eq_symbols, xi)
                stochastic_result = stochastic_expr.replace(self.symbols['__g'],
                                                            replace_f)
                # Replace x by the respective variable
                stochastic_result = stochastic_result.subs(self.symbols['__x'],
                                                           eq_symbols[var])

                # Replace dW by the respective variable
                stochastic_result = stochastic_result.subs(self.symbols['__dW'],
                                                           xi)


                # Replace intermediate variables
                temp_var_replacements = dict((self.symbols[temp_var],
                                              _symbol(f"{temp_var}_{var}_{xi}"))
                                             for temp_var in temp_vars)

                stochastic_result = stochastic_result.subs(
                    temp_var_replacements)
                stochastic_results.append(stochastic_result)
        return stochastic_results

    def _generate_RHS(self, eqs, var, eq_symbols, temp_vars, expr,
                      non_stochastic_expr, stochastic_expr,
                      stochastic_variable=()):
        """
        Helper function used in `__call__`. Generates the right hand side of
        an abstract code statement by appropriately replacing f, g and t.
        For example, given a differential equation ``dv/dt = -(v + I) / tau``
        (i.e. `var` is ``v` and `expr` is ``(-v + I) / tau``) together with
        the `rk2` step ``return x + dt*f(x +  k/2, t + dt/2)``
        (i.e. `non_stochastic_expr` is
        ``x + dt*f(x +  k/2, t + dt/2)`` and `stochastic_expr` is ``None``),
        produces ``v + dt*(-v - _k_v/2 + I + _k_I/2)/tau``.
                
        """
        
        # Note: in the following we are silently ignoring the case that a
        # state updater does not care about either the non-stochastic or the
        # stochastic part of an equation. We do trust state updaters to
        # correctly specify their own abilities (i.e. they do not claim to
        # support stochastic equations but actually just ignore the stochastic
        # part). We can't really check the issue here, as we are only dealing
        # with one line of the state updater description. It is perfectly valid
        # to write the euler update as:
        #     non_stochastic = dt * f(x, t)
        #     stochastic = dt**.5 * g(x, t) * xi
        #     return x + non_stochastic + stochastic
        #
        # In the above case, we'll deal with lines which do not define either
        # the stochastic or the non-stochastic part.
        
        non_stochastic, stochastic = expr.split_stochastic()

        if non_stochastic_expr is not None:
            # We do have a non-stochastic part in the state updater description
            non_stochastic_results = self._non_stochastic_part(eq_symbols,
                                                               non_stochastic,
                                                               non_stochastic_expr,
                                                               stochastic_variable,
                                                               temp_vars, var)
        else:
            non_stochastic_results = []

        if not (stochastic is None or stochastic_expr is None):
            # We do have a stochastic part in the state
            # updater description
            stochastic_results = self._stochastic_part(eq_symbols,
                                                       stochastic,
                                                       stochastic_expr,
                                                       stochastic_variable,
                                                       temp_vars, var)
        else:
            stochastic_results = []

        RHS = sympy.Number(0)
        # All the parts (one non-stochastic and potentially more than one
        # stochastic part) are combined with addition
        for non_stochastic_result in non_stochastic_results:
            RHS += non_stochastic_result
        for stochastic_result in stochastic_results:
            RHS += stochastic_result

        return sympy_to_str(RHS)

    def __call__(self, eqs, variables=None, method_options=None):
        """
        Apply a state updater description to model equations.
        
        Parameters
        ----------
        eqs : `Equations`
            The equations describing the model
        variables: dict-like, optional
            The `Variable` objects for the model. Ignored by the explicit
            state updater.
        method_options : dict, optional
            Additional options to the state updater (not used at the moment
            for the explicit state updaters).

        Examples
        --------
        >>> from brian2 import *
        >>> eqs = Equations('dv/dt = -v / tau : volt')
        >>> print(euler(eqs))
        _v = -dt*v/tau + v
        v = _v
        >>> print(rk4(eqs))
        __k_1_v = -dt*v/tau
        __k_2_v = -dt*(__k_1_v/2 + v)/tau
        __k_3_v = -dt*(__k_2_v/2 + v)/tau
        __k_4_v = -dt*(__k_3_v + v)/tau
        _v = __k_1_v/6 + __k_2_v/3 + __k_3_v/3 + __k_4_v/6 + v
        v = _v
        """
        extract_method_options(method_options, {})
        # Non-stochastic numerical integrators should work for all equations,
        # except for stochastic equations
        if eqs.is_stochastic and self.stochastic is None:
            raise UnsupportedEquationsException("Cannot integrate "
                                                "stochastic equations with "
                                                "this state updater.")
        if self.custom_check:
            self.custom_check(eqs, variables)
        # The final list of statements
        statements = []

        stochastic_variables = eqs.stochastic_variables

        # The variables for the intermediate results in the state updater
        # description, e.g. the variable k in rk2
        intermediate_vars = [var for var, expr in self.statements]
        
        # A dictionary mapping all the variables in the equations to their
        # sympy representations 
        eq_variables = dict(((var, _symbol(var)) for var in eqs.eq_names))
        
        # Generate the random numbers for the stochastic variables
        for stochastic_variable in stochastic_variables:
            statements.append(f"{stochastic_variable} = dt**.5 * randn()")

        substituted_expressions = eqs.get_substituted_expressions(variables)

        # Process the intermediate statements in the stateupdater description
        for intermediate_var, intermediate_expr in self.statements:
                      
            # Split the expression into a non-stochastic and a stochastic part
            non_stochastic_expr, stochastic_expr = split_expression(intermediate_expr)
            
            # Execute the statement by appropriately replacing the functions f
            # and g and the variable x for every equation in the model.
            # We use the model equations where the subexpressions have
            # already been substituted into the model equations.
            for var, expr in substituted_expressions:
                for xi in stochastic_variables:
                    RHS = self._generate_RHS(eqs, var, eq_variables, intermediate_vars,
                                             expr, non_stochastic_expr,
                                             stochastic_expr, xi)
                    statements.append(f"{intermediate_var}_{var}_{xi} = {RHS}")
                if not stochastic_variables:   # no stochastic variables
                    RHS = self._generate_RHS(eqs, var, eq_variables, intermediate_vars,
                                             expr, non_stochastic_expr,
                                             stochastic_expr)
                    statements.append(f"{intermediate_var}_{var} = {RHS}")
                
        # Process the "return" line of the stateupdater description
        non_stochastic_expr, stochastic_expr = split_expression(self.output)

        if eqs.is_stochastic and (self.stochastic != 'multiplicative' and
                                  eqs.stochastic_type == 'multiplicative'):
            # The equations are marked as having multiplicative noise and the
            # current state updater does not support such equations. However,
            # it is possible that the equations do not use multiplicative noise
            # at all. They could depend on time via a function that is constant
            # over a single time step (most likely, a TimedArray). In that case
            # we can integrate the equations
            dt_value = variables['dt'].get_value()[0] if 'dt' in variables else None
            for _, expr in substituted_expressions:
                _, stoch = expr.split_stochastic()
                if stoch is None:
                    continue
                # There could be more than one stochastic variable (e.g. xi_1, xi_2)
                for _, stoch_expr in stoch.items():
                    sympy_expr = str_to_sympy(stoch_expr.code)
                    # The equation really has multiplicative noise, if it depends
                    # on time (and not only via a function that is constant
                    # over dt), or if it depends on another variable defined
                    # via differential equations.
                    if (not is_constant_over_dt(sympy_expr, variables, dt_value)
                            or len(stoch_expr.identifiers & eqs.diff_eq_names)):
                        raise UnsupportedEquationsException("Cannot integrate "
                                                            "equations with "
                                                            "multiplicative noise with "
                                                            "this state updater.")

        # Assign a value to all the model variables described by differential
        # equations
        for var, expr in substituted_expressions:
            RHS = self._generate_RHS(eqs, var, eq_variables, intermediate_vars,
                                     expr, non_stochastic_expr, stochastic_expr,
                                     stochastic_variables)
            statements.append(f"_{var} = {RHS}")
        
        # Assign everything to the final variables
        for var, expr in substituted_expressions:
            statements.append(f"{var} = _{var}")

        return '\n'.join(statements)
예제 #17
0
EQ = WS + Suppress('=') + WS
FRT = WS + Suppress('->') + WS
HSTART = Suppress('[')
HEND = Suppress(']') + WS

comment = Literal('#') + restOfLine

sec_directories = HSTART + CaselessLiteral("directories") + HEND
sec_files = HSTART + CaselessLiteral("files") + HEND
sec_settings = HSTART + CaselessLiteral("settings") + HEND
section = HSTART + Word(printables + " ") + HEND

keyvalue = ~HSTART + Dict(Group(Word(alphanums) + EQ + Word(printables)))
fromto = Group(~HSTART + SkipTo(FRT).setResultsName("source").setParseAction(
    lambda x: x[0])  # whyyy
               + FRT + restOfLine.setResultsName("destination"))

part_directories = Dict(
    Group(sec_directories +
          Group(ZeroOrMore(fromto)))).setName("part_directories")
part_files = Dict(Group(sec_files +
                        Group(ZeroOrMore(fromto)))).setName("part_files")
part_settings = Dict(Group(sec_settings +
                           OneOrMore(keyvalue))).setName("part_settings")

g = Optional(part_directories) & Optional(part_files) & part_settings
g.ignore(comment)

# End Grammar Parsing

instr = open(os.path.expanduser("~/.backup-settings"), "r").read()
예제 #18
0
파일: parser.py 프로젝트: luistaka/linehaul
HOSTNAME = NIL ^ Word(printables)
HOSTNAME = HOSTNAME.setResultsName("hostname")
HOSTNAME.setName("Hostname")

APPNAME = Word("".join(set(printables) - {"["}))
APPNAME = APPNAME.setResultsName("appname")
APPNAME.setName("AppName")

PROCID = Combine(LBRACKET + Word("".join(set(printables) - {"]"})) + RBRACKET)
PROCID = PROCID.setResultsName("procid")
PROCID.setName("ProcID")

HEADER = PRIORITY + TIMESTAMP + SP + HOSTNAME + SP + APPNAME + PROCID

MESSAGE = restOfLine.setResultsName("message")
MESSAGE.setName("Message")

SYSLOG_MESSAGE = HEADER + COLON + SP + MESSAGE
SYSLOG_MESSAGE.leaveWhitespace()


@attr.s(slots=True, frozen=True)
class SyslogMessage:

    facility = attr.ib(
        type=Facility, converter=Facility, validator=attr.validators.in_(Facility)
    )
    severity = attr.ib(
        type=Severity, converter=Severity, validator=attr.validators.in_(Severity)
    )
예제 #19
0
#define MAX_LOCS=100
#define USERNAME = "******"
#define PASSWORD = "******"

a = MAX_LOCS;
CORBA::initORB("xyzzy", USERNAME, PASSWORD );

"""

#################
print("Example of an extractor")
print("----------------------")

# simple grammar to match #define's
ident = Word(alphas, alphanums+"_")
macroDef = Literal("#define") + ident.setResultsName("name") + "=" + restOfLine.setResultsName("value")
for t,s,e in macroDef.scanString( testData ):
    print(t.name,":", t.value)
    
# or a quick way to make a dictionary of the names and values 
# (return only key and value tokens, and construct dict from key-value pairs)
# - empty ahead of restOfLine advances past leading whitespace, does implicit lstrip during parsing
macroDef = Suppress("#define") + ident + Suppress("=") + empty + restOfLine
macros = dict(list(macroDef.searchString(testData)))
print("macros =", macros)
print()


#################
print("Examples of a transformer")
print("----------------------")
예제 #20
0
    def __init__(self):
        filename = os.path.join(paths.lib_dir(), 'bauble.glade')
        self.widgets = utils.load_widgets(filename)
        self.window = self.widgets.main_window
        self.window.hide()
        self.previous_view = None

        # restore the window size
        geometry = prefs[self.window_geometry_pref]
        if geometry is not None:
            self.window.set_default_size(*geometry)

        self.window.connect('delete-event', self.on_delete_event)
        self.window.connect("destroy", self.on_quit)
        self.window.set_title(self.title)

        try:
            pixbuf = gtk.gdk.pixbuf_new_from_file(bauble.default_icon)
            self.window.set_icon(pixbuf)
        except Exception:
            logger.warning(_('Could not load icon from %s')
                           % bauble.default_icon)
            logger.warning(traceback.format_exc())

        menubar = self.create_main_menu()
        self.widgets.menu_box.pack_start(menubar)

        combo = self.widgets.main_comboentry
        model = gtk.ListStore(str)
        combo.set_model(model)
        self.populate_main_entry()

        main_entry = combo.child
        main_entry.connect('activate', self.on_main_entry_activate)
        accel_group = gtk.AccelGroup()
        main_entry.add_accelerator("grab-focus", accel_group, ord('L'),
                                   gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
        self.window.add_accel_group(accel_group)

        self.widgets.home_button.connect(
            'clicked', self.on_home_button_clicked)

        self.widgets.prev_view_button.connect(
            'clicked', self.on_prev_view_button_clicked)

        self.widgets.go_button.connect(
            'clicked', self.on_go_button_clicked)

        self.widgets.query_button.connect(
            'clicked', self.on_query_button_clicked)

        self.set_default_view()

        # add a progressbar to the status bar
        # Warning: this relies on gtk.Statusbar internals and could break in
        # future versions of gtk
        statusbar = self.widgets.statusbar
        statusbar.set_spacing(10)
        statusbar.set_has_resize_grip(True)
        self._cids = []

        def on_statusbar_push(sb, cid, txt):
            if cid not in self._cids:
                self._cids.append(cid)

        statusbar.connect('text-pushed', on_statusbar_push)

        # remove label from frame
        frame = statusbar.get_children()[0]
        #frame.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#FF0000'))
        label = frame.get_children()[0]
        frame.remove(label)

        # replace label with hbox and put label and progress bar in hbox
        hbox = gtk.HBox(False, 5)
        frame.add(hbox)
        hbox.pack_start(label, True, True, 0)
        vbox = gtk.VBox(True, 0)
        hbox.pack_end(vbox, False, True, 15)
        self.progressbar = gtk.ProgressBar()
        vbox.pack_start(self.progressbar, False, False, 0)
        self.progressbar.set_size_request(-1, 10)
        vbox.show()
        hbox.show()

        from pyparsing import StringStart, Word, alphanums, restOfLine, \
            StringEnd
        cmd = StringStart() + ':' + Word(
            alphanums + '-_').setResultsName('cmd')
        arg = restOfLine.setResultsName('arg')
        self.cmd_parser = (cmd + StringEnd()) | (cmd + '=' + arg) | arg

        combo.grab_focus()
예제 #21
0
 def _item(self, code, name):
     return CaselessLiteral(code).suppress() + \
            restOfLine.setResultsName(name) + \
            LineEnd().suppress()
예제 #22
0
 def _item(self, code, name):
     return CaselessLiteral(code).suppress() + \
            restOfLine.setResultsName(name) + \
            LineEnd().suppress()
예제 #23
0
파일: parser.py 프로젝트: pypa/linehaul
HOSTNAME = Combine(NIL | Word(printables))
HOSTNAME = HOSTNAME.setResultsName("hostname")
HOSTNAME.setName("Hostname")

APPNAME = Word("".join(set(printables) - {"["}))
APPNAME = APPNAME.setResultsName("appname")
APPNAME.setName("AppName")

PROCID = Combine(LBRACKET + Word("".join(set(printables) - {"]"})) + RBRACKET)
PROCID = PROCID.setResultsName("procid")
PROCID.setName("ProcID")

HEADER = PRIORITY + TIMESTAMP + SP + HOSTNAME + SP + APPNAME + PROCID

MESSAGE = restOfLine.setResultsName("message")
MESSAGE.setName("Message")

SYSLOG_MESSAGE = HEADER + COLON + SP + MESSAGE
SYSLOG_MESSAGE.leaveWhitespace()


class SyslogMessage(pyrsistent.PClass):

    facility = pyrsistent.field(type=int, mandatory=True, factory=Facility)
    severity = pyrsistent.field(type=int, mandatory=True, factory=Severity)
    timestamp = pyrsistent.field(
        type=datetime.datetime,
        mandatory=True,
        factory=lambda t: arrow.get(t).datetime,
    )
예제 #24
0
    def __init__(self, query):
        self._methods = {
            'and': self.evaluate_and,
            'or': self.evaluate_or,
            'not': self.evaluate_not,
            'parenthesis': self.evaluate_parenthesis,
            'quotes': self.evaluate_quotes,
            'word': self.evaluate_word,
        }

        self.line = ''
        self.query = query.lower() if query else ''

        if self.query:
            # TODO: Cleanup
            operator_or = Forward()
            operator_word = Group(Word(alphanums)).setResultsName('word')

            operator_quotes_content = Forward()
            operator_quotes_content << (
                (operator_word + operator_quotes_content) | operator_word)

            operator_quotes = Group(
                Suppress('"') + operator_quotes_content +
                Suppress('"')).setResultsName('quotes') | operator_word

            operator_parenthesis = Group(
                (Suppress('(') + operator_or + Suppress(")")
                 )).setResultsName('parenthesis') | operator_quotes

            operator_not = Forward()
            operator_not << (
                Group(Suppress(Keyword('no', caseless=True)) +
                      operator_not).setResultsName('not')
                | operator_parenthesis)

            operator_and = Forward()
            operator_and << (
                Group(operator_not + Suppress(Keyword('and', caseless=True)) +
                      operator_and).setResultsName('and') |
                Group(operator_not + OneOrMore(~oneOf('and or') + operator_and)
                      ).setResultsName('and') | operator_not)

            operator_or << (
                Group(operator_and + Suppress(Keyword('or', caseless=True)) +
                      operator_or).setResultsName('or') | operator_and)

            self._query_parser = operator_or.parseString(self.query)[0]
        else:
            self._query_parser = False

        time_cmpnt = Word(nums).setParseAction(lambda t: t[0].zfill(2))
        date = Combine((time_cmpnt + '-' + time_cmpnt + '-' + time_cmpnt) +
                       ' ' + time_cmpnt + ':' + time_cmpnt)
        word = Word(printables)

        self._log_parser = (
            date.setResultsName('timestamp') +
            word.setResultsName('log_level') + word.setResultsName('plugin') +
            (White(min=16).setParseAction(
                lambda s, l, t: [t[0].strip()]).setResultsName('task') |
             (White(min=1).suppress() & word.setResultsName('task'))) +
            restOfLine.setResultsName('message'))
예제 #25
0
#define USERNAME = "******"
#define PASSWORD = "******"

a = MAX_LOCS;
CORBA::initORB("xyzzy", USERNAME, PASSWORD );

"""

#################
print("Example of an extractor")
print("----------------------")

# simple grammar to match #define's
ident = Word(alphas, alphanums + "_")
macroDef = (Literal("#define") + ident.setResultsName("name") + "=" +
            restOfLine.setResultsName("value"))
for t, s, e in macroDef.scanString(testData):
    print(t.name, ":", t.value)

# or a quick way to make a dictionary of the names and values
# (return only key and value tokens, and construct dict from key-value pairs)
# - empty ahead of restOfLine advances past leading whitespace, does implicit lstrip during parsing
macroDef = Suppress("#define") + ident + Suppress("=") + empty + restOfLine
macros = dict(list(macroDef.searchString(testData)))
print("macros =", macros)
print()

#################
print("Examples of a transformer")
print("----------------------")