def _analyze_agent_times_choice_method_call(self, receiver, method, args):
     same, vars = match(SUBPATTERN_AGENT_TIMES_CHOICE, args)
     if not same:
         raise ValueError, "syntax error for agent_times_choice function call"
     self._special_dataset_receivers.add(receiver)
     # 'call' is a string representing the new agent_times_choice call.  Parse it, extract the args, and then add a replacement to
     # parsetree_replacements for the old args.  We want to replace just the args and not the entire call to agent_times_choice,
     # since the way Python represents parsetrees the whole tree may include astype and exponentiation calls, and it's simpler
     # to just replace the args part.
     call = "%s.%s(%s)" % (receiver, method, quote(vars['attribute']))
     (newtree, _) = self._parse_expr(call)
     s, v = match(FULL_EXPRESSION_METHOD_CALL, newtree)
     if not s:
         raise StandardError, 'internal error - problem generating new number_of_agents expression'
     self._parsetree_replacements[args] = v['args']
 def _analyze_agent_times_choice_method_call(self, receiver, method, args):
     same, vars = match(SUBPATTERN_AGENT_TIMES_CHOICE, args)
     if not same:
         raise ValueError, "syntax error for agent_times_choice function call"
     self._special_dataset_receivers.add(receiver)
     # 'call' is a string representing the new agent_times_choice call.  Parse it, extract the args, and then add a replacement to
     # parsetree_replacements for the old args.  We want to replace just the args and not the entire call to agent_times_choice,
     # since the way Python represents parsetrees the whole tree may include astype and exponentiation calls, and it's simpler
     # to just replace the args part. 
     call = "%s.%s(%s)" % (receiver, method, quote(vars['attribute']))
     (newtree,_) = self._parse_expr(call)
     s, v = match(FULL_EXPRESSION_METHOD_CALL, newtree)
     if not s:
         raise StandardError, 'internal error - problem generating new number_of_agents expression'
     self._parsetree_replacements[args] = v['args']
    def _analyze_aggregation_method_call(self, receiver, method, args):
        same, vars = match(SUBPATTERN_AGGREGATION, args)
        if not same:
            raise ValueError, "syntax error for aggregation method call"
        arg_dict = self._get_arguments(
            ('arg1', 'arg2', 'arg3'),
            ('aggr_var', 'intermediates', 'function'), vars)
        if 'aggr_var' not in arg_dict:
            raise ValueError, "syntax error for aggregation method call (problem with argument for variable being aggregated)"
        same1, vars1 = match(SUBPATTERN_FULLY_QUALIFIED_VARIABLE_ARG,
                             arg_dict['aggr_var'])
        if same1:
            # the aggregated variable is a fully-qualified name
            pkg = vars1['package']
            dataset = vars1['dataset']
            attr = vars1['shortname']
        else:
            same2, vars2 = match(SUBPATTERN_DATASET_QUALIFIED_VARIABLE_ARG,
                                 arg_dict['aggr_var'])
            if same2:
                # the aggregated variable is a dataset-qualified name
                pkg = None
                dataset = vars2['dataset']
                attr = vars2['shortname']
            else:
                # The thing being aggregated is an expression.  Generate a new autogen variable for that expression,
                # and use the autogen variable in the aggregation call.
                subexpr = arg_dict['aggr_var']
                newvar = VariableName(parsetree_to_string(subexpr))
                pkg = None
                dataset = newvar.get_dataset_name()
                if dataset is None:
                    raise ValueError, "syntax error for aggregation method call - could not determine dataset for variable being aggregated"
                attr = newvar.get_short_name()
                # TODO DELETE BELOW:
#                replacements = {'dataset': dataset, 'attribute': attr}
#                newvar_tree = parsetree_substitute(DATASET_QUALIFIED_VARIABLE_TEMPLATE, replacements)
#                self._parsetree_replacements[subexpr] = newvar_tree
        if 'intermediates' in arg_dict:
            # make sure that it really is a list
            s, v = match(SUBPATTERN_LIST_ARG, arg_dict['intermediates'])
            if not s:
                raise ValueError, "syntax error for aggregation method call (list of intermediate datasets not a list?)"
            intermediates = tuple(
                self._extract_names(arg_dict['intermediates']))
        else:
            intermediates = ()
        if 'function' in arg_dict:
            # bind fcn to a string that is the name of the function, or to the string "None"
            s, v = match(SUBPATTERN_NAME_ARG, arg_dict['function'])
            if not s:
                raise ValueError, "syntax error for aggregation method call (problem with the function argument in the call)"
            fcn = v['name']
        else:
            fcn = None
        self._aggregation_calls.add(
            (receiver, method, pkg, dataset, attr, intermediates, fcn))
        quoted_intermediates = "" if len(intermediates) == 0 else quote(
            intermediates[0])
        for n in intermediates[1:]:
            quoted_intermediates = quoted_intermediates + ', ' + quote(n)
        # 'call' is a string representing the new aggregation call.  Parse it, extract the args, and then add a replacement to
        # parsetree_replacements for the old args.  We want to replace just the args and not the entire call to aggregate,
        # since the way Python represents parsetrees the whole tree may include astype and exponentiation calls, and it's simpler
        # to just replace the args part.
        call = "%s.%s(%s, %s,%s, [%s], %s)" % (
            receiver, method, quote(pkg), quote(dataset), quote(attr),
            quoted_intermediates, quote(fcn))
        (newtree, _) = self._parse_expr(call)
        s, v = match(FULL_EXPRESSION_METHOD_CALL, newtree)
        if not s:
            raise StandardError, 'internal error - problem generating new aggregation expression'
        self._parsetree_replacements[args] = v['args']
    def _analyze_aggregation_method_call(self, receiver, method, args):
        same, vars = match(SUBPATTERN_AGGREGATION, args)
        if not same:
            raise ValueError, "syntax error for aggregation method call"
        arg_dict = self._get_arguments( ('arg1', 'arg2','arg3'), ('aggr_var', 'intermediates','function'), vars )
        if 'aggr_var' not in arg_dict:
            raise ValueError, "syntax error for aggregation method call (problem with argument for variable being aggregated)"
        same1, vars1 = match(SUBPATTERN_FULLY_QUALIFIED_VARIABLE_ARG, arg_dict['aggr_var'])
        if same1:
            # the aggregated variable is a fully-qualified name
            pkg = vars1['package']
            dataset = vars1['dataset']
            attr = vars1['shortname']
        else:
            same2, vars2 = match(SUBPATTERN_DATASET_QUALIFIED_VARIABLE_ARG, arg_dict['aggr_var'])
            if same2:
                # the aggregated variable is a dataset-qualified name
                pkg = None
                dataset = vars2['dataset']
                attr = vars2['shortname']
            else:
                # The thing being aggregated is an expression.  Generate a new autogen variable for that expression,
                # and use the autogen variable in the aggregation call.
                subexpr = arg_dict['aggr_var']
                newvar = VariableName(parsetree_to_string(subexpr))
                pkg = None
                dataset = newvar.get_dataset_name()
                if dataset is None:
                    raise ValueError, "syntax error for aggregation method call - could not determine dataset for variable being aggregated"
                attr = newvar.get_short_name()
                # TODO DELETE BELOW:
#                replacements = {'dataset': dataset, 'attribute': attr}
#                newvar_tree = parsetree_substitute(DATASET_QUALIFIED_VARIABLE_TEMPLATE, replacements)
#                self._parsetree_replacements[subexpr] = newvar_tree
        if 'intermediates' in arg_dict:
            # make sure that it really is a list
            s, v = match(SUBPATTERN_LIST_ARG, arg_dict['intermediates'])
            if not s:
                raise ValueError, "syntax error for aggregation method call (list of intermediate datasets not a list?)"
            intermediates = tuple(self._extract_names(arg_dict['intermediates']))
        else:
            intermediates = ()
        if 'function' in arg_dict:
            # bind fcn to a string that is the name of the function, or to the string "None"
            s,v = match(SUBPATTERN_NAME_ARG, arg_dict['function'])
            if not s:
                raise ValueError, "syntax error for aggregation method call (problem with the function argument in the call)"
            fcn = v['name']
        else:
            fcn = None
        self._aggregation_calls.add( (receiver, method, pkg, dataset, attr, intermediates, fcn) )
        quoted_intermediates = "" if len(intermediates)==0 else quote(intermediates[0])
        for n in intermediates[1:]:
            quoted_intermediates = quoted_intermediates + ', ' + quote(n)
        # 'call' is a string representing the new aggregation call.  Parse it, extract the args, and then add a replacement to
        # parsetree_replacements for the old args.  We want to replace just the args and not the entire call to aggregate,
        # since the way Python represents parsetrees the whole tree may include astype and exponentiation calls, and it's simpler
        # to just replace the args part. 
        call = "%s.%s(%s, %s,%s, [%s], %s)" % (receiver, method, quote(pkg), quote(dataset), quote(attr),  quoted_intermediates, quote(fcn))
        (newtree,_) = self._parse_expr(call)
        s, v = match(FULL_EXPRESSION_METHOD_CALL, newtree)
        if not s:
            raise StandardError, 'internal error - problem generating new aggregation expression'
        self._parsetree_replacements[args] = v['args']