Ejemplo n.º 1
0
def doRoot():
    tableData = {}
    with open(modelSource) as sh:
        topData = yaml.load(sh)

    for path in glob('{}/*.yaml'.format(tableSource)):
        table = os.path.splitext(os.path.basename(path))[0]
        serverprint('\treading table {}.yaml'.format(table))
        with open(path) as sh:
            tableData[table] = yaml.load(sh)

    (modelData, names, extraNames) = combine(topData, tableData)

    with open(modelFile, 'w') as mh:
        pp = pprint.PrettyPrinter(
            indent=2, width=100, compact=False, stream=mh
        )
        mh.write('model = ')
        pp.pprint(modelData)

    with open(namesFile, 'w') as nh:
        nh.write('''
class N:
''')
        for name in sorted(n for n in names if not iskeyword(n)):
            nh.write(putName(name))
        for name in sorted(n for n in names if iskeyword(n)):
            nh.write(putNameK(name))

    checkNames(names, extraNames)
Ejemplo n.º 2
0
def is_eval_safe_name(string):
    if py3:
        return all(part.isidentifier() and not keyword.iskeyword(part)
                   for part in string.split('.'))
    else:
        return all(_name.match(part) and not keyword.iskeyword(part)
                   for part in string.split('.'))
Ejemplo n.º 3
0
 def _set(self, key, val, config=False):
     if isinstance(val, basestring):
         if "\n" in val:
             val = [shlex.split(x) for x in val.split("\n")]
         else:
             val = shlex.split(val)
             if len(val) == 1:
                 val = val[0]
     if '.' in key:
         key1, key2 = key.split('.', 1)
         if keyword.iskeyword(key1):
             key1 += '_'
         if not hasattr(self, key1):
             setattr(self, key1, {})
         getattr(self, key1)[key2] = val
         if config:
             if key1 not in self.config:
                 self.config[key1] = {}
             self.config[key1][key2] = val
     else:
         if keyword.iskeyword(key):
             key += '_'
         setattr(self, key, val)
         if config:
             self.config[key] = val
Ejemplo n.º 4
0
    def check_symbols_used(self):
        self.symbolsused = set()
        all_interptext_props = []
        all_code_props = []
        for (key, propval) in self.props.items():
            if keyword.iskeyword(key):
                print('Warning: prop "%s" in %s is a keyword' % (key, None))
            if is_interp_text(propval):
                all_interptext_props.append( (propval['text'], None) )
            if is_code(propval):
                all_code_props.append( (key, propval['text'], None) )
            if is_move(propval):
                if propval['loc'] not in self.locations:
                    print('Warning: move prop "%s" in %s goes to undefined loc: %s' % (key, None, propval['loc']))
        for (lockey, loc) in self.locations.items():
            for (key, propval) in loc.props.items():
                if keyword.iskeyword(key):
                    print('Warning: prop "%s" in %s is a keyword' % (key, lockey))
                if is_interp_text(propval):
                    all_interptext_props.append( (propval['text'], lockey) )
                if is_code(propval):
                    all_code_props.append( (key, propval['text'], lockey) )
                if is_move(propval):
                    if propval['loc'] not in self.locations:
                        print('Warning: move prop "%s" in %s goes to undefined loc: %s' %(key, lockey, propval['loc']))
            
        for (key, text, lockey) in all_code_props:
            try:
                ast.parse(text, filename='%s.%s' % (lockey, key,))
            except Exception as ex:
                print('Warning: code prop "%s" in %s does not parse: %s' % (key, lockey, ex))

        for (text, lockey) in all_interptext_props:
            for nod in twcommon.interp.parse(text):
                if isinstance(nod, twcommon.interp.Link):
                    self.symbolsused.add( (nod.target, lockey) )
                if isinstance(nod, twcommon.interp.Interpolate):
                    self.symbolsused.add( (nod.expr, lockey) )

        for (symbol, lockey) in self.symbolsused:
            if not symbol.isidentifier():
                try:
                    ast.parse(symbol)
                except:
                    print('Warning: code snippet "%s" in %s does not parse.' % (symbol, lockey,))                    
                continue
            if lockey is None:
                loc = None
            else:
                loc = self.locations[lockey]
            if loc and symbol in loc.props:
                continue
            if symbol in self.props:
                continue
            print('Warning: symbol "%s" in %s is not defined.' % (symbol, lockey,))
Ejemplo n.º 5
0
def MakePublicAttributeName(className, is_global = False):
	# Given a class attribute that needs to be public, convert it to a
	# reasonable name.
	# Also need to be careful that the munging doesnt
	# create duplicates - eg, just removing a leading "_" is likely to cause
	# a clash.
	# if is_global is True, then the name is a global variable that may
	# overwrite a builtin - eg, "None"
	if className[:2]=='__':
		return demunge_leading_underscores(className)
	elif iskeyword(className): # all keywords are lower case
		return string.capitalize(className)
	elif className == 'None':
		# assign to None is evil (and SyntaxError in 2.4) - note
		# that if it was a global it would get picked up below
		className = 'NONE'
	elif is_global and __builtins__.has_key(className):
		# builtins may be mixed case.  If capitalizing it doesn't change it,
		# force to all uppercase (eg, "None", "True" become "NONE", "TRUE"
		ret = className.capitalize()
		if ret==className: # didn't change - force all uppercase.
			ret = ret.upper()
		return ret
	# Strip non printable chars
	return filter( lambda char: char in valid_identifier_chars, className)
Ejemplo n.º 6
0
 def load_module(self, fullname):
     juliapath = fullname.lstrip("julia.")
     if isamodule(self.julia, juliapath):
         mod = sys.modules.setdefault(fullname, JuliaModule(fullname))
         mod.__loader__ = self
         names = self.julia.eval("names({}, true, false)".format(juliapath))
         for name in names:
             if ismacro(name) or isoperator(name) or isprotected(name) or notascii(name):
                 continue
             attrname = name
             if name.endswith("!"):
                 attrname = name.replace("!", "_b")
             if keyword.iskeyword(name):
                 attrname = "jl".join(name)
             try:
                 module_path = ".".join((juliapath, name))
                 module_obj = self.julia.eval(module_path)
                 is_module = self.julia.eval("isa({}, Module)".format(module_path))
                 if is_module:
                     split_path = module_path.split(".")
                     is_base = split_path[-1] == "Base"
                     recur_module = split_path[-1] == split_path[-2]
                     if is_module and not is_base and not recur_module:
                         newpath = ".".join((fullname, name))
                         module_obj = self.load_module(newpath)
                 setattr(mod, attrname, module_obj)
             except Exception:
                 if isafunction(self.julia, name, mod_name=juliapath):
                     func = "{}.{}".format(juliapath, name)
                     setattr(mod, name, self.julia.eval(func))
         return mod
     elif isafunction(self.julia, juliapath):
         return getattr(self.julia, juliapath)
     else:
         raise ImportError("{} not found".format(juliapath))
Ejemplo n.º 7
0
def module_functions(julia, module):
    """Compute the function names in the julia module"""
    bases = {}
    names = julia.eval("names(%s)" % module)
    for name in names:
        if (ismacro(name) or
            isoperator(name) or
            isprotected(name) or
            notascii(name)):
            continue
        try:
            # skip undefined names
            if not julia.eval("isdefined(:%s)" % name):
                continue
            # skip modules for now
            if isamodule(julia, name):
                continue
            if name.startswith("_"):
                continue
            if not isafunction(julia, name):
                continue
            attr_name = name
            if name.endswith("!"):
                attr_name = name.replace("!", "_b")
            if keyword.iskeyword(name):
                attr_name = "jl".join(name)
            julia_func = julia.eval(name)
            bases[attr_name] = julia_func
        except:
            pass
    return bases
Ejemplo n.º 8
0
  def convert(name):
    insert_us = first_cap_re.sub(r'\1_\2', name)
    converted = all_cap_re.sub(r'\1_\2', insert_us).lower()

    if iskeyword(converted):
      return converted + "_"
    return converted
Ejemplo n.º 9
0
    def add_parens(self):
        """
        This is called if the user pressed space on the sourceview, and
        the subprocess is not executing commands (so is_callable_only can work.)
        Should return True if event-handling should stop, or False if it should
        continue as usual.
        
        Should be called only when is_callable_only can be called safely.
        """
        sb = self.sourcebuffer
        
        # Quickly discard some cases
        insert = sb.get_iter_at_mark(sb.get_insert())
        mark_it = sb.get_iter_at_mark(self.mark)
        if mark_it.equal(insert):
            return False
        it = insert.copy()
        it.backward_char()
        if it.get_char() not in LAST_CHARS:
            return False
        it.forward_char()
        it.backward_word_start()
        if iskeyword(it.get_text(insert).decode('utf8')):
            return False
        
        text = sb.get_slice(sb.get_start_iter(),
                            sb.get_end_iter()).decode('utf8')
        index = sb.get_iter_at_mark(sb.get_insert()).get_offset()
        hp = HyperParser(text, index, self.INDENT_WIDTH)

        if not hp.is_in_code():
            return False

        expr = hp.get_expression()
        if not expr:
            return False
        if '(' in expr:
            # Don't evaluate expressions which may contain a function call.
            return False
        
        is_callable_only, expects_str = self.is_callable_only(expr)
        if not is_callable_only:
            return False
        
        sb.move_mark(self.mark, insert)
        
        last_name = expr.rsplit('.', 1)[-1]
        sb.begin_user_action()
        if expects_str or last_name in self.get_expects_str():
            sb.insert(insert, '("")')
            insert.backward_chars(2)
        else:
            sb.insert(insert, '()')
            insert.backward_char()
        sb.place_cursor(insert)
        sb.end_user_action()
        
        self.show_call_tip()
        
        return True
Ejemplo n.º 10
0
    def buildattributestring(self, attr):
        """
        Builds the attribute string for the object creation

        attr is a dict containing name value pairs of the attribute and value
        """
        if not isinstance(attr, dict):
            attr = dict()

        parmlist = []
        for k, v in attr.items():
            if k not in self.EXCLUDEATTR:
                # if the name is status and value is not deleted, skip that
                # otherwise, include it
                if k == 'status' and v != 'deleted':
                    pass
                else:
                    # any properly formed xml/json should have keywords already
                    # escaped however this is just a sanity check. also, it
                    # misses 'to' which is not a keyword in python, but is
                    # treated as such in pymeta oh well
                    if keyword.iskeyword(k):
                        k += '_'

                    v = repr(v)
                    parmlist.append('%s=%s' % (k, v))

        attribstr = ', '.join(parmlist)

        return attribstr
Ejemplo n.º 11
0
def is_legal_name(name):
    '''Verifies a Pythonic legal name for use as an OpenMDAO object.'''

    match = namecheck_rgx.match(name)
    if match is None or match.group() != name or iskeyword(name) or name in _expr_dict:
        return False
    return name not in ['parent', 'self']
Ejemplo n.º 12
0
def whitespace_before_parameters(logical_line, tokens):
    """
    Avoid extraneous whitespace in the following situations:

    - Immediately before the open parenthesis that starts the argument
      list of a function call.

    - Immediately before the open parenthesis that starts an indexing or
      slicing.

    Okay: spam(1)
    E211: spam (1)

    Okay: dict['key'] = list[index]
    E211: dict ['key'] = list[index]
    E211: dict['key'] = list [index]
    """
    prev_type = tokens[0][0]
    prev_text = tokens[0][1]
    prev_end = tokens[0][3]
    for index in range(1, len(tokens)):
        token_type, text, start, end, _line = tokens[index]
        if (token_type == tokenize.OP and
            text in '([' and
            start != prev_end and
            (prev_type == tokenize.NAME or prev_text in '}])') and
            # Syntax "class A (B):" is allowed, but avoid it
            (index < 2 or tokens[index - 2][1] != 'class') and
            # Allow "return (a.foo for a in range(5))"
            (not keyword.iskeyword(prev_text))):
            return prev_end, "E211 whitespace before '%s'" % text
        prev_type = token_type
        prev_text = text
        prev_end = end
Ejemplo n.º 13
0
def register_option(key, defval, doc='', validator=None, cb=None):
    """Register an option in the package-wide ibis config object
    Parameters
    ----------
    key       - a fully-qualified key, e.g. "x.y.option - z".
    defval    - the default value of the option
    doc       - a string description of the option
    validator - a function of a single argument, should raise `ValueError` if
                called with a value which is not a legal value for the option.
    cb        - a function of a single argument "key", which is called
                immediately after an option value is set/reset. key is
                the full name of the option.
    Returns
    -------
    Nothing.
    Raises
    ------
    ValueError if `validator` is specified and `defval` is not a valid value.
    """
    import tokenize
    import keyword
    key = key.lower()

    if key in _registered_options:
        raise OptionError("Option '%s' has already been registered" % key)
    if key in _reserved_keys:
        raise OptionError("Option '%s' is a reserved key" % key)

    # the default value should be legal
    if validator:
        validator(defval)

    # walk the nested dict, creating dicts as needed along the path
    path = key.split('.')

    for k in path:
        if not bool(re.match('^' + tokenize.Name + '$', k)):
            raise ValueError("%s is not a valid identifier" % k)
        if keyword.iskeyword(k):
            raise ValueError("%s is a python keyword" % k)

    cursor = _global_config
    for i, p in enumerate(path[:-1]):
        if not isinstance(cursor, dict):
            raise OptionError("Path prefix to option '%s' is already an option"
                              % '.'.join(path[:i]))
        if p not in cursor:
            cursor[p] = {}
        cursor = cursor[p]

    if not isinstance(cursor, dict):
        raise OptionError("Path prefix to option '%s' is already an option"
                          % '.'.join(path[:-1]))

    cursor[path[-1]] = defval  # initialize

    # save the option metadata
    _registered_options[key] = RegisteredOption(key=key, defval=defval,
                                                doc=doc, validator=validator,
                                                cb=cb)
Ejemplo n.º 14
0
    def _generate_unit_names():
        import keyword
        from ... import units as u
        names = {}

        names['%'] = u.Unit(0.01)

        bases = [
            'A', 'C', 'cd', 'eV', 'F', 'g', 'H', 'Hz', 'J', 'K',
            'lm', 'lx', 'm', 'mol', 'N', 'Ohm', 'Pa', 'rad', 's', 'S',
            'sr', 'T', 'V', 'W', 'Wb']

        prefixes = [
            'y', 'z', 'a', 'f', 'p', 'n', 'u', 'm', 'c', 'd',
            '', 'da', 'h', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']

        for base in bases:
            for prefix in prefixes:
                key = prefix + base
                if keyword.iskeyword(key):
                    continue
                names[key] = getattr(u, key)

        simple_bases = [
            'a', 'AU', 'arcmin', 'arcsec', 'barn', 'bit',
            'byte', 'ct', 'D', 'd', 'deg', 'h', 'Jy', 'mag', 'mas',
            'min', 'pc', 'pix', 'Ry', 'solLum', 'solMass', 'solRad',
            'Sun', 'yr']

        for base in simple_bases:
            names[base] = getattr(u, base)

        return names
Ejemplo n.º 15
0
def whitespace_before_parameters(logical_line, tokens):
    """
    Avoid extraneous whitespace in the following situations:

    - Immediately before the open parenthesis that starts the argument
      list of a function call.

    - Immediately before the open parenthesis that starts an indexing or
      slicing.
    """
    prev_type = tokens[0][0]
    prev_text = tokens[0][1]
    prev_end = tokens[0][3]
    for index in range(1, len(tokens)):
        token_type, text, start, end, line = tokens[index]
        if (token_type == tokenize.OP and
            text in '([' and
            start != prev_end and
            prev_type == tokenize.NAME and
            (index < 2 or tokens[index - 2][1] != 'class') and
            (not iskeyword(prev_text))):
            return prev_end, "E211 whitespace before '%s'" % text
        prev_type = token_type
        prev_text = text
        prev_end = end
Ejemplo n.º 16
0
def is_python2_identifier(possible_identifier):
    """
    Returns `True` if the given `possible_identifier` can be used as an
    identifier in Python 2.
    """
    match = _python2_identifier_re.match(possible_identifier)
    return bool(match) and not iskeyword(possible_identifier)
Ejemplo n.º 17
0
Archivo: build.py Proyecto: Eih3/v0.83
def MakePublicAttributeName(className, is_global = False):
	# Given a class attribute that needs to be public, convert it to a
	# reasonable name.
	# Also need to be careful that the munging doesnt
	# create duplicates - eg, just removing a leading "_" is likely to cause
	# a clash.
	# if is_global is True, then the name is a global variable that may
	# overwrite a builtin - eg, "None"
	if className[:2]=='__':
		return demunge_leading_underscores(className)
	elif className == 'None':
		# assign to None is evil (and SyntaxError in 2.4, even though
		# iskeyword says False there) - note that if it was a global
		# it would get picked up below
		className = 'NONE'
	elif iskeyword(className):
		# most keywords are lower case (except True, False etc in py3k)
		ret = className.capitalize()
		# but those which aren't get forced upper.
		if ret == className:
			ret = ret.upper()
		return ret
	elif is_global and hasattr(__builtins__, className):
		# builtins may be mixed case.  If capitalizing it doesn't change it,
		# force to all uppercase (eg, "None", "True" become "NONE", "TRUE"
		ret = className.capitalize()
		if ret==className: # didn't change - force all uppercase.
			ret = ret.upper()
		return ret
	# Strip non printable chars
	return ''.join([char for char in className if char in valid_identifier_chars])
Ejemplo n.º 18
0
def load_script(text):
    """
    Will load the blender text file as a module in nodes.script
    """
    #global _script_modules
    #global _name_lookup

    if text.endswith(".py"):
        name = text.rstrip(".py")
    else:
        name = text

    if not name.isidentifier() or keyword.iskeyword(name):
        print("bad text name: {}".format(text))
        return

    name = make_valid_identifier(name)
    _name_lookup[name] = text

    if name in _script_modules:
        print("reloading")
        mod = _script_modules[name]
        importlib.reload(mod)
    else:
        mod = importlib.import_module("sverchok.nodes.script.{}".format(name))
        _script_modules[name] = mod

    func = _func_lookup[name]
    setattr(mod, "_func", func)
    if func._sv_properties:
        cls = class_factory(func)
        setattr(mod, "_class", cls)
    else:
        setattr(mod, "_class", None)
Ejemplo n.º 19
0
    def all(self):
        prop_names = []
        props, where, optional_clauses = self.prepare(prop_names)
        if len(prop_names) > 1:
            selectuple = namedtuple(self.source_name + '_props',
                [name + '_' if iskeyword(name) else name
                    for name in prop_names])
        select = self.build_select(props, where + optional_clauses)

        g = self._graph
        response = g.client.command(select)
        if response:
            # TODO Determine which other queries always take only one iteration
            list_query = 'count' not in self._params

            if list_query:
                if prop_names:
                    if len(prop_names) > 1:
                        return [
                            selectuple(*tuple(record.oRecordData.get(name)
                                       for name in prop_names))
                            for record in response]
                    else:
                        return [record.oRecordData[prop_names[0]]
                                for record in response]
                else:
                    return g.elements_from_records(response)
            else:
                return next(iter(response[0].oRecordData.values()))
        else:
            return []
Ejemplo n.º 20
0
def get_parameters(element):
    """Return the parameters of a callable"""
    params = []
    for elem_property in element:
        tag = etree.QName(elem_property)
        if tag.localname == 'parameters':
            for param in elem_property:
                try:
                    subtag = etree.QName(param)
                    if subtag.localname == "instance-parameter":
                        param_name = 'self'
                    else:
                        param_name = param.attrib['name']

                    parm_type = get_parameter_type(param)

                    if keyword.iskeyword(param_name):
                        param_name = "_" + param_name

                    if param_name == '...':
                        param_name = '*args'

                    if param_name not in params:
                        params.append((param_name, parm_type))
                except KeyError:
                    pass
    return params
Ejemplo n.º 21
0
    def __init__(self, table, association_tables, inflect_engine, detect_joined):
        super(ModelClass, self).__init__(table)
        self.name = self._tablename_to_classname(table.name, inflect_engine)
        self.children = []
        self.attributes = OrderedDict()

        # Assign attribute names for columns
        for column in table.columns:
            attrname = column.name + '_' if iskeyword(column.name) else column.name
            self.attributes[attrname] = column

        # Add many-to-one relationships
        pk_column_names = set(col.name for col in table.primary_key.columns)
        for constraint in sorted(table.constraints, key=_get_constraint_sort_key):
            if isinstance(constraint, ForeignKeyConstraint):
                target_cls = self._tablename_to_classname(constraint.elements[0].column.table.name, inflect_engine)
                if detect_joined and self.parent_name == 'Base' and set(constraint.columns) == pk_column_names:
                    self.parent_name = target_cls
                else:
                    self._add_relationship(ManyToOneRelationship(self.name, target_cls, constraint, inflect_engine))

        # Add many-to-many relationships
        for association_table in association_tables:
            fk_constraints = [c for c in association_table.constraints if isinstance(c, ForeignKeyConstraint)]
            fk_constraints.sort(key=_get_constraint_sort_key)
            target_cls = self._tablename_to_classname(fk_constraints[1].elements[0].column.table.name, inflect_engine)
            self._add_relationship(ManyToManyRelationship(self.name, target_cls, association_table))
Ejemplo n.º 22
0
    def suggested_names(self):
        yield self.preferred_name if not iskeyword(self.preferred_name) else self.preferred_name + '_'

        iteration = 0
        while True:
            iteration += 1
            yield self.preferred_name + str(iteration)
Ejemplo n.º 23
0
def matchkeyword(colitem, keywordexpr):
    """Tries to match given keyword expression to given collector item.

    Will match on the name of colitem, including the names of its parents.
    Only matches names of items which are either a :class:`Class` or a
    :class:`Function`.
    Additionally, matches on names in the 'extra_keyword_matches' set of
    any item, as well as names directly assigned to test functions.
    """
    mapping = KeywordMapping.from_item(colitem)
    if " " not in keywordexpr:
        # special case to allow for simple "-k pass" and "-k 1.3"
        return mapping[keywordexpr]
    elif keywordexpr.startswith("not ") and " " not in keywordexpr[4:]:
        return not mapping[keywordexpr[4:]]
    for kwd in keywordexpr.split():
        if keyword.iskeyword(kwd) and kwd not in python_keywords_allowed_list:
            raise UsageError(
                "Python keyword '{}' not accepted in expressions passed to '-k'".format(
                    kwd
                )
            )
    try:
        return eval(keywordexpr, {}, mapping)
    except SyntaxError:
        raise UsageError("Wrong expression passed to '-k': {}".format(keywordexpr))
Ejemplo n.º 24
0
def check_constraint (value, name, constraint):
	if name in constraint:
		if keyword.iskeyword(name):
			raise SystemExit, 'name conflict: ' + name + ' is a reserved word in PYTHON!'
		if type(value) in (IntType,FloatType):
			statement = name + '=' + `value`
			exec  statement
			if  not eval(constraint):
				raise SystemExit, statement + '\nconstraint ' + repr(constraint) + ' violated!'
		elif type(value) is np.ndarray:
			statement = name + ' = np.' + `value`
			exec  statement
			if not np.alltrue(eval(constraint)):
				raise SystemExit, statement + '\nconstraint ' + repr(constraint) + ' violated (comparison elementwise)!'
		#elif type(value) is Num.ArrayType:
		#	statement = name + ' = Num.' + `value`
		#	exec  statement
		#	if not Num.alltrue(eval(constraint)):
		#		raise SystemExit, statement + '\nconstraint ' + repr(constraint) + ' violated (comparison elementwise)!'
		elif type(value) is StringType:
			statement = name + '=' + `value`
			exec  statement
			if  not eval(constraint):
				raise SystemExit,  statement + '\nconstraint ' + repr(constraint) + ' violated!'
		elif isinstance(value,(Interval,PairOfInts,PairOfFloats)):
			statement = name + '=' + `value`
			exec  statement
			if  not eval(constraint):
				raise SystemExit,  statement + '\nconstraint ' + repr(constraint) + ' violated!'
		else:
			raise SystemExit, 'unknown/unsupported type ' + repr(type(value)) + ' for check_constraint'
	else:
		raise SystemExit, 'Variable name ' + repr(name) + ' not used in constraint expression ' + repr(constraint)
Ejemplo n.º 25
0
  def legal_name(name, is_param_name=False):
    """
    If this name is a legal property name.

    is_param_name determines if this name in the name of a property, or a
      param_name. See the constructor documentation for more information.

    The rules are as follows:
      * Cannot start with an underscore.
        This is for internal arguments, namely _engine (for the step module).
      * Cannot be 'self'
        This is to avoid conflict with recipe modules, which use the name self.
      * Cannot be a python keyword
    """

    if name.startswith('_'):
      return False

    if name in ('self',):
      return False

    if keyword.iskeyword(name):
      return False

    regex = r'^[a-zA-Z][a-zA-Z0-9_]*$' if is_param_name else r'^[a-zA-Z][.\w]*$'
    return bool(re.match(regex, name))
Ejemplo n.º 26
0
    def __iter__(self):
        params = self._params

        # TODO Don't ignore initial skip value
        with TempParams(params, skip='#-1:-1', limit=1):
            optional_clauses = self.build_optional_clauses(params, None)

            prop_names = []
            props = self.build_props(params, prop_names, for_iterator=True)
            if len(prop_names) > 1:
                selectuple = namedtuple(self.source_name + '_props',
                    [name + '_' if iskeyword(name) else name
                        for name in prop_names])
            wheres = self.build_wheres(params)

            g = self._graph
            while True:
                current_skip = params['skip']
                where = u'WHERE {0}'.format(
                    u' and '.join(
                        [self.rid_lower(current_skip)] + wheres))
                select = self.build_select(props, [where] + optional_clauses)

                response = g.client.command(select)
                if response:
                    response = response[0]

                    if prop_names:
                        next_skip = response.oRecordData.get('rid')
                        if next_skip:
                            self.skip(next_skip)

                            if len(prop_names) > 1:
                                yield selectuple(
                                    *tuple(self.parse_record_prop(
                                            response.oRecordData.get(name))
                                        for name in prop_names))
                            else:
                                yield self.parse_record_prop(
                                        response.oRecordData[prop_names[0]])
                        else:
                            yield g.element_from_record(response)
                            break
                    else:
                        if '-' in response._rid:
                            # Further queries would yield the same
                            # TODO Find out if any single iteration queries
                            #      return multiple values
                            yield next(iter(response.oRecordData.values()))
                            break
                        elif response._rid == current_skip:
                            # OrientDB bug?
                            # expand() makes for strange responses
                            break
                        else:
                            self.skip(response._rid)

                        yield g.element_from_record(response)
                else:
                    break
Ejemplo n.º 27
0
def read_ctes_from_file(ctes_file_name):
    """Create a list with the constants found in the file received.
    """
    
    block_comment = False
    ctes_set = set()
    
    try:
        with open(ctes_file_name, 'r') as f:
            for line in f:
                
                line, block_comment = process_comments(block_comment, line)
                
                eq_pos = line.find(EQUAL_CHR)
                
                if eq_pos > 0:
                    line_part = line[:eq_pos].rstrip()                  
                    
                    if re.match("[_a-zA-Z][_a-zA-Z0-9]*$", line_part) and \
                        not keyword.iskeyword(line_part):
                        ctes_set.add(line_part)
                
    except IOError:
        print "ERROR: Reading file '%s'" % ctes_file_name 
        
    return ctes_set
Ejemplo n.º 28
0
 def sanitise_prop_name(name):
     if iskeyword(name):
         return name + '_'
     elif name[0] == '$':
         return 'qv_'+ name[1:]
     else:
         return name
Ejemplo n.º 29
0
    def makeUnique(self, name):
        """
        Modifies a given string into a valid Python variable name
        which is not already in self.data

        Input
        -----
            name  ::  string

            self.data   (class member)

        Returns
        ------
            string containing modified name

        Modifies
        --------
            None
        """
        # First make sure the name is a valid variable name
        name = re.sub('\W|^(?=\d)', '_', name)  # Replace invalid characters with '_'

        if iskeyword(name):  # Check if name is a keyword
            name += '2'

        if name in self.data:
            # Name is already in the list. Add a number to the end to make it unique
            i = 1
            while name + "_"+str(i) in self.data:
                i += 1
            return name + "_"+str(i)
        return name
Ejemplo n.º 30
0
def rename_ast(tree, reserved=set()):
    """Change all names in an abstract syntax tree, except for a set of
    reserved names. The new names are as short as possible.

    """
    from keyword import iskeyword
    names, imports = FindNames().find(tree)

    # Add aliases for import statements if there are enough uses to
    # justify the transformation. See Rename.visit_alias for the
    # insertion of the aliases.
    for module in imports:
        if (len(module) - 1) * names[module][0] > 5:
            reserved.remove(module)

    mapping = dict()
    n = [0] * 3
    sorted_names = sorted(((i, j, k) for k, (i, j) in names.items()),
                          key = lambda (i, j, k): (-i, j, k))
    for _, _, name in sorted_names:
        if name is None or name[:2] == name[-2:] == '__' or name in reserved:
            continue
        underscores = name.startswith('_') + name.startswith('__')
        while name not in mapping:
            newname = '_' * underscores + make_name(n[underscores])
            n[underscores] += 1
            if newname not in reserved and not iskeyword(newname):
                mapping[name] = newname
    Rename(mapping).visit(tree)
Ejemplo n.º 31
0
def name_from_python(name):
    if name.endswith('_') and keyword.iskeyword(name[:-1]):
        name = name[:-1]
    return name.replace('_', '-')
Ejemplo n.º 32
0
def ispythonkeyword(word):
    return keyword.iskeyword(word) or word in ('None', 'parent', 'children',
                                               'operation', 'exec', 'entity')
Ejemplo n.º 33
0
import keyword
from functools import reduce
from itertools import repeat
from operator import or_
from string import ascii_letters

from hypothesis import strategies

from rsrc.models import URL
from rsrc.plugins import (MAX_ID_PARTS_COUNT,
                          to_id)
from tests.strategies import strings

ids = (reduce(or_, [strategies.tuples(*repeat(strings, times))
                    for times in range(1, len(URL._fields) + 1)])
       .map(lambda parts: to_id(*parts)))
invalid_ids_parts = strategies.lists(strings,
                                     min_size=MAX_ID_PARTS_COUNT + 1)
identifiers_characters = strategies.sampled_from(ascii_letters + '_')
identifiers = (strategies.text(identifiers_characters,
                               min_size=1)
               .filter(str.isidentifier)
               .filter(lambda string: not keyword.iskeyword(string)))
Ejemplo n.º 34
0
def verify_names(names):
    for name in names:
        if keyword.iskeyword(name):
            raise ValueError("%r is a keyword" % (name, ))
Ejemplo n.º 35
0
import keyword

print("The list of keywords is : ")
print(keyword.kwlist)

print("\nCheck is keyword : ")
s = "try"
if keyword.iskeyword(s):
    print(s + " is a keyword")

else:
    print(s + " is not a keyword")


Ejemplo n.º 36
0
 def safe_name(self, name):
     if keyword.iskeyword(name):
         return name + '_'
     return name
Ejemplo n.º 37
0
def map_property_name(name):
    if keyword.iskeyword(name):
        name = name + '_'
    return convert_camel_to_snake(name)
Ejemplo n.º 38
0
def fix_keywords(value: str) -> str:
    if iskeyword(value):
        return f"{value}_"
    return value
Ejemplo n.º 39
0
import math
import random
import statistics
import keyword

a = math.pow(2, 3)
print(a)
print(int(a))

b = random.randint(0, 100)
print(b)

#mean
nums = [1, 5, 33, 12, 46, 33, 2]
c = statistics.mean(nums)
print(c)

#median
c = statistics.median(nums)
print(c)

#mode
c = statistics.mode(nums)
print(c)

print(keyword.iskeyword("for"))
print(keyword.iskeyword("f"))
Ejemplo n.º 40
0
# 문자열이 식별자로 사용가능한지 알아보는 법
s = "abc"
print(s.isidentifier())

s = "try"
print(s.isidentifier())

import keyword

print(keyword.iskeyword(s))

help('del')

# 함수 다시, 필수 - 기본 - 가변 - 가변키워드
# 중첩 함수
def hello(a, g='hello', *args, **keywords):
    def pkg_install(a, g):
        print(a)

    def pkg_install2():
        print(a)

    pkg_install(2, 3)
    pkg_install2()

hello(1)

# 람다함수
# 재사용이 불가
(lambda: 1)
Ejemplo n.º 41
0
def register_option(key, defval, doc="", validator=None, cb=None):
    """Register an option in the package-wide pandas config object

    Parameters
    ----------
    key       - a fully-qualified key, e.g. "x.y.option - z".
    defval    - the default value of the option
    doc       - a string description of the option
    validator - a function of a single argument, should raise `ValueError` if
                called with a value which is not a legal value for the option.
    cb        - a function of a single argument "key", which is called
                immediately after an option value is set/reset. key is
                the full name of the option.

    Returns
    -------
    Nothing.

    Raises
    ------
    ValueError if `validator` is specified and `defval` is not a valid value.

    """
    import tokenize
    import keyword

    key = key.lower()

    if key in _registered_options:
        msg = "Option '{key}' has already been registered"
        raise OptionError(msg.format(key=key))
    if key in _reserved_keys:
        msg = "Option '{key}' is a reserved key"
        raise OptionError(msg.format(key=key))

    # the default value should be legal
    if validator:
        validator(defval)

    # walk the nested dict, creating dicts as needed along the path
    path = key.split(".")

    for k in path:
        if not bool(re.match("^" + tokenize.Name + "$", k)):
            raise ValueError("{k} is not a valid identifier".format(k=k))
        if keyword.iskeyword(k):
            raise ValueError("{k} is a python keyword".format(k=k))

    cursor = _global_config
    msg = "Path prefix to option '{option}' is already an option"
    for i, p in enumerate(path[:-1]):
        if not isinstance(cursor, dict):
            raise OptionError(msg.format(option=".".join(path[:i])))
        if p not in cursor:
            cursor[p] = {}
        cursor = cursor[p]

    if not isinstance(cursor, dict):
        raise OptionError(msg.format(option=".".join(path[:-1])))

    cursor[path[-1]] = defval  # initialize

    # save the option metadata
    _registered_options[key] = RegisteredOption(key=key,
                                                defval=defval,
                                                doc=doc,
                                                validator=validator,
                                                cb=cb)
Ejemplo n.º 42
0
print(code_files2v)
codev = []
for cv in code_files2v:
    codev += txt_file_to_list_of_strings(cv)

cv = '\n'.join(codev)
wordsv = getWords(cv)
Words = {}
for wv in wordsv:
    if wv in _ignore_words_:
        #print wv
        continue
    if wv in globals():
        #print wv
        continue
    if keyword.iskeyword(wv):
        continue
    if wv[-1] == 'v':
        continue
    if str.isupper(wv[0]):
        if len(wv) == 1:
            continue
        if not str.isupper(wv[1]):
            continue
    if represents_int(wv[0]):
        continue
    if wv[0] == '_':
        continue
    if wv in locals():
        #print wv
        continue
Ejemplo n.º 43
0
 def __init__(self, name, query, args=(), arity='table', help_=None):
     assert not keyword.iskeyword(
         name)  # TODO: more complete identifier check
     help_ = help_ or query
     self.name, self.query, self.args, self.arity, self.help = name, query, args, arity, help_
Ejemplo n.º 44
0
def is_identifier(s: str) -> bool:
    """Return True if `s` is a valid identifier (and not a keyword)."""
    try:
        return s.isidentifier() and not iskeyword(s)
    except AttributeError:
        return False
Ejemplo n.º 45
0
 def _is_safe_ident(cls, ident):
     return isinstance(ident, str) and cls._safe_ident_re.match(ident) \
         and not (keyword.iskeyword(ident) or ident == 'None')
Ejemplo n.º 46
0
 def __init__(self, mapping):
     self._data = {}
     for key, value in mapping.items():
         if keyword.iskeyword(key):
             key += "_"
         self._data[key] = value
Ejemplo n.º 47
0
    def __init__(self,
                 url,
                 url_internal=None,
                 debug=False,
                 force_compatibility=False):
        try:
            self.url, self.url_internal = url, url_internal
            # load the openapi json file from the node
            self.api_def = requests.get(f"{url}/api").json()
            self.api_version = self.api_def.get("info",
                                                {}).get("version", "unknown")
            # retrieve the version of the node we are connecting to
            match_min = semver.match(self.api_version,
                                     __compatibility__.get("from_version"))
            match_max = semver.match(self.api_version,
                                     __compatibility__.get("to_version"))
            if (not match_min or not match_max) and not force_compatibility:
                f, t = __compatibility__.get(
                    'from_version'), __compatibility__.get('to_version')
                raise UnsupportedNodeVersion(
                    f"unsupported node version {self.api_version}, supported version are {f} and {t}"
                )
        except requests.exceptions.ConnectionError as e:
            raise ConfigException(
                f"Error connecting to the node at {self.url}, connection unavailable",
                e)
        except Exception as e:
            raise UnsupportedNodeVersion(f"Unable to connect to the node: {e}")

        # enable printing debug messages
        self.debug = debug

        # check open_api_version
        if self.api_def.get('swagger', 'x') not in self.open_api_versions:
            raise OpenAPIException(
                f"Unsupported Open API specification version {self.api_def.get('swagger')}"
            )
        self.version = self.api_def.get('info', {}).get('version')

        # regexp to convert method signature to snake case
        first_cap_re = re.compile('(.)([A-Z][a-z]+)')
        all_cap_re = re.compile('([a-z0-9])([A-Z])')

        def p2s(name):
            s1 = first_cap_re.sub(r'\1_\2', name)
            return all_cap_re.sub(r'\1_\2', s1).lower()

        # prepare the baseurl
        base_path = self.api_def.get('basePath', '/')
        self.base_url = f"{url}{base_path}"
        if url_internal is None:
            # do not build internal endpoints
            self.skip_tags.add("internal")
        else:
            self.base_url_internal = f"{url_internal}{base_path}"

        # parse the api
        # definition of a field
        FieldDef = namedtuple(
            "FieldDef",
            ["required", "type", "values", "minimum", "maximum", "default"])
        Param = namedtuple("Param", ["name", "raw", "pos", "field"])
        Resp = namedtuple("Resp", ["schema", "desc"])
        Api, self.api_methods = namedtuple(
            "Api",
            ["name", "doc", "params", "responses", "endpoint", "http_method"
             ]), []

        for query_path, path in self.api_def.get("paths").items():
            for m, func in path.items():
                # exclude the paths/method tagged with skip_tags
                if not self.skip_tags.isdisjoint(func.get("tags")):
                    continue
                # get if is an internal or external endpoint
                endpoint_url = self.base_url_internal if "internal" in func.get(
                    "tags", []) else self.base_url
                api = Api(
                    name=p2s(func['operationId']),
                    doc=func.get("description"),
                    params=[],
                    responses={},
                    endpoint=f"{endpoint_url}{query_path}",
                    http_method=m,
                )
                # collect the parameters
                for p in func.get('parameters', []):

                    param_name = p.get("name")
                    param_pos = p.get("in")
                    # check if the param name is a reserved keyword
                    if keyword.iskeyword(param_name):
                        if param_pos == 'path':
                            api.query_path = api.query_path.replace(
                                "{%s}" % param_name, "{_%s}" % param_name)
                        param_name = f"_{param_name}"

                    param = Param(name=param_name,
                                  raw=p.get("name"),
                                  pos=param_pos,
                                  field=FieldDef(required=p.get("required"),
                                                 type=p.get(
                                                     "type",
                                                     p.get("schema",
                                                           {}).get("$ref")),
                                                 values=p.get("enum", []),
                                                 default=p.get("default"),
                                                 minimum=p.get("minimum"),
                                                 maximum=p.get("maximum")))
                    api.params.append(param)

                # responses
                for code, r in func.get('responses', {}).items():
                    api.responses[int(code)] = Resp(
                        desc=r.get("description"),
                        schema=r.get("schema", {
                            "$ref": ""
                        }).get("$ref", ).replace("#/definitions/", ""))
                # create the method
                self._add_api_method(api)
Ejemplo n.º 48
0
def contains_keyword(*args):
	for item in args:
		if keyword.iskeyword(item):
			return True
	return False
Ejemplo n.º 49
0
s4 = "nikhil"
s5 = "assert"
s6 = "shambhavi"
s7 = "True"
s8 = "False"
s9 = "akshat"
s10 = "akash"
s11 = "break"
s12 = "ashty"
s13 = "lambda"
s14 = "suman"
s15 = "try"
s16 = "vaishnavi"

# checking which are keywords
if keyword.iskeyword(s):
    print(s + " is a python keyword")
else:
    print(s + " is not a python keyword")

if keyword.iskeyword(s1):
    print(s1 + " is a python keyword")
else:
    print(s1 + " is not a python keyword")

if keyword.iskeyword(s2):
    print(s2 + " is a python keyword")
else:
    print(s2 + " is not a python keyword")

if keyword.iskeyword(s3):
Ejemplo n.º 50
0
 def _is_safe_ident(cls, ident):
     return isinstance(ident, str) and ident.isidentifier() \
             and not keyword.iskeyword(ident)
Ejemplo n.º 51
0
def _normalise_arg(arg: str) -> str:
    if keyword.iskeyword(arg) or arg in dir(builtins):
        return arg + '_'
    else:
        return arg
Ejemplo n.º 52
0
def valid(name):
    if (re.match("[_A-Za-z][_a-zA-Z0-9]*$", name) and not keyword.iskeyword(name)):
        return name
    else:
        return ''
Ejemplo n.º 53
0
def is_valid_identifier(value: str) -> bool:
    """Return True if the given value is a valid non-keyword Python identifier, otherwise return False."""
    return value.isidentifier() and not keyword.iskeyword(value)
Ejemplo n.º 54
0
    def handle_line(self, line):
        """
        Render a single logical line from the module, and write the
        generated HTML to C{self.out}.

        @param line: A single logical line, encoded as a list of
            C{(toktype,tokttext)} pairs corresponding to the tokens in
            the line.
        """
        # def_name is the name of the function or class defined by
        # this line; or None if no funciton or class is defined.
        def_name = None

        # def_type is the type of the function or class defined by
        # this line; or None if no funciton or class is defined.
        def_type = None

        # does this line start a class/func def?
        starting_def_block = False

        in_base_list = False
        in_param_list = False
        in_param_default = 0
        at_module_top = (self.lineno == 1)

        ended_def_blocks = 0

        # The html output.
        if self.ADD_LINE_NUMBERS:
            s = self.lineno_to_html()
            self.lineno += 1
        else:
            s = ''
        s += '  <tt class="py-line">'

        # Loop through each token, and colorize it appropriately.
        for i, (toktype, toktext) in enumerate(line):
            if type(s) is not str:
                if type(s) is six.text_type:  # only PY2 -> unicode
                    log.error('While colorizing %s -- got unexpected '
                              'unicode string' % self.module_name)
                    s = s.encode('ascii', 'xmlcharrefreplace')
                elif type(s) is six.binary_type:  # only PY3 -> bytes
                    log.error('While colorizing %s -- got unexpected '
                              'binary string' % self.module_name)
                    s = decode_with_backslashreplace(s)
                else:
                    raise ValueError('Unexpected value for s -- %s' %
                                     type(s).__name__)

            # For each token, determine its css class and whether it
            # should link to a url.
            css_class = None
            url = None
            tooltip = None
            onclick = uid = targets = None  # these 3 are used together.

            # Is this token the class name in a class definition?  If
            # so, then make it a link back into the API docs.
            if i >= 2 and line[i - 2][1] == 'class':
                in_base_list = True
                css_class = self.CSS_CLASSES['DEFNAME']
                def_name = toktext
                def_type = 'class'
                if 'func' not in self.context_types:
                    cls_name = self.context_name(def_name)
                    url = self.name2url(cls_name)
                    s = self.mark_def(s, cls_name)
                    starting_def_block = True

            # Is this token the function name in a function def?  If
            # so, then make it a link back into the API docs.
            elif i >= 2 and line[i - 2][1] == 'def':
                in_param_list = True
                css_class = self.CSS_CLASSES['DEFNAME']
                def_name = toktext
                def_type = 'func'
                if 'func' not in self.context_types:
                    cls_name = self.context_name()
                    func_name = self.context_name(def_name)
                    url = self.name2url(cls_name, def_name)
                    s = self.mark_def(s, func_name)
                    starting_def_block = True

            # For each indent, update the indents list (which we use
            # to keep track of indentation strings) and the context
            # list.  If this indent is the start of a class or
            # function def block, then self.def_name will be its name;
            # otherwise, it will be None.
            elif toktype == token.INDENT:
                self.indents.append(toktext)
                self.context.append(self.def_name)
                self.context_types.append(self.def_type)

            # When we dedent, pop the last elements off the indents
            # list and the context list.  If the last context element
            # is a name, then we're ending a class or function def
            # block; so write an end-div tag.
            elif toktype == token.DEDENT:
                self.indents.pop()
                self.context_types.pop()
                if self.context.pop():
                    ended_def_blocks += 1

            # If this token contains whitespace, then don't bother to
            # give it a css tag.
            elif toktype in (None, tokenize.NL, token.NEWLINE,
                             token.ENDMARKER):
                css_class = None

            # Check if the token is a keyword.
            elif toktype == token.NAME and keyword.iskeyword(toktext):
                css_class = self.CSS_CLASSES['KEYWORD']

            elif in_base_list and toktype == token.NAME:
                css_class = self.CSS_CLASSES['BASECLASS']

            elif (in_param_list and toktype == token.NAME
                  and not in_param_default):
                css_class = self.CSS_CLASSES['PARAM']

            # Class/function docstring.
            elif (self.def_name and line[i - 1][0] == token.INDENT
                  and self.is_docstring(line, i)):
                css_class = self.CSS_CLASSES['DOCSTRING']

            # Module docstring.
            elif at_module_top and self.is_docstring(line, i):
                css_class = self.CSS_CLASSES['DOCSTRING']

            # check for decorators??
            elif (toktype == token.NAME and (
                (i > 0 and line[i - 1][1] == '@') or
                (i > 1 and line[i - 1][0] == None and line[i - 2][1] == '@'))):
                css_class = self.CSS_CLASSES['DECORATOR']
                self.has_decorators = True

            # If it's a name, try to link it.
            elif toktype == token.NAME:
                css_class = self.CSS_CLASSES['NAME']
                # If we have a variable named `toktext` in the current
                # context, then link to that.  Note that if we're inside
                # a function, then that function is our context, not
                # the namespace that contains it. [xx] this isn't always
                # the right thing to do.
                if (self.GUESS_LINK_TARGETS and self.docindex is not None
                        and self.url_func is not None):
                    context = [n for n in self.context if n is not None]
                    container = self.docindex.get_vardoc(
                        DottedName(self.module_name, *context))
                    if isinstance(container, NamespaceDoc):
                        doc = container.variables.get(toktext)
                        if doc is not None:
                            url = self.url_func(doc)
                            tooltip = str(doc.canonical_name)
                # Otherwise, check the name_to_docs index to see what
                # else this name might refer to.
                if (url is None and self.name_to_docs is not None
                        and self.url_func is not None):
                    docs = self.name_to_docs.get(toktext)
                    if docs:
                        tooltip = '\n'.join(
                            [str(d.canonical_name) for d in docs])
                        if len(docs) == 1 and self.GUESS_LINK_TARGETS:
                            url = self.url_func(docs[0])
                        else:
                            uid, onclick, targets = self.doclink(toktext, docs)

            # For all other tokens, look up the CSS class to use
            # based on the token's type.
            else:
                if toktype == token.OP and toktext in self.CSS_CLASSES:
                    css_class = self.CSS_CLASSES[toktext]
                elif token.tok_name[toktype] in self.CSS_CLASSES:
                    css_class = self.CSS_CLASSES[token.tok_name[toktype]]
                else:
                    css_class = None

            # update our status..
            if toktext == ':':
                in_base_list = False
                in_param_list = False
            if toktext == '=' and in_param_list:
                in_param_default = True
            if in_param_default:
                if toktext in ('(', '[', '{'): in_param_default += 1
                if toktext in (')', ']', '}'): in_param_default -= 1
                if toktext == ',' and in_param_default == 1:
                    in_param_default = 0

            # Write this token, with appropriate colorization.
            if tooltip and self.ADD_TOOLTIPS:
                tooltip_html = ' title="%s"' % tooltip
            else:
                tooltip_html = ''
            if css_class: css_class_html = ' class="%s"' % css_class
            else: css_class_html = ''
            if onclick:
                if targets: targets_html = ' targets="%s"' % targets
                else: targets_html = ''
                s += ('<tt id="%s"%s%s><a%s%s href="#" onclick="%s">' %
                      (uid, css_class_html, targets_html, tooltip_html,
                       css_class_html, onclick))
            elif url:
                if isinstance(url, six.text_type):
                    url = url.encode('ascii', 'xmlcharrefreplace')
                s += ('<a%s%s href="%s">' %
                      (tooltip_html, css_class_html, url))
            elif css_class_html or tooltip_html:
                s += '<tt%s%s>' % (tooltip_html, css_class_html)
            if i == len(line) - 1:
                s += ' </tt>'  # Closes <tt class="py-line">
                s += cgi.escape(toktext)
            else:
                try:
                    s += self.add_line_numbers(cgi.escape(toktext), css_class)
                except Exception as e:
                    print((toktext, css_class, toktext.encode('ascii')))
                    raise

            if onclick: s += "</a></tt>"
            elif url: s += '</a>'
            elif css_class_html or tooltip_html: s += '</tt>'

        if self.ADD_DEF_BLOCKS:
            for i in range(ended_def_blocks):
                self.out(self.END_DEF_BLOCK)

        # Strip any empty <tt>s.
        s = re.sub(r'<tt class="[\w+]"></tt>', '', s)

        # Write the line.
        self.out(s)

        if def_name and starting_def_block:
            self.out('</div>')

        # Add div's if we're starting a def block.
        if (self.ADD_DEF_BLOCKS and def_name and starting_def_block
                and (line[-2][1] == ':')):
            indentation = (''.join(self.indents) + '    ').replace(' ', '+')
            linenum_padding = '+' * self.linenum_size
            name = self.context_name(def_name)
            self.out(self.START_DEF_BLOCK %
                     (name, linenum_padding, indentation, name))

        self.def_name = def_name
        self.def_type = def_type
Ejemplo n.º 55
0
def idcheck(flag):
    return (flag[0] in string.letters \
    or flag[0] == '_') \
    and not keyword.iskeyword(flag) \
    and not [s for s in flag[1:] \
    if s not in string.letters + string.digits + "_"]
Ejemplo n.º 56
0
def name_to_python(name):
    name = name.replace('-', '_')
    if keyword.iskeyword(name):
        return name + '_'
    return name
Ejemplo n.º 57
0
def _convert_to_valid_identifier(name):
    assert name, 'Identifier cannot be empty'
    if name[0].isdigit() or iskeyword(name):
        name = '_' + name
    return _re_invalid_identifier.sub('_', name)
Ejemplo n.º 58
0
def keyword_checker(word):
    return keyword.iskeyword(word)
Ejemplo n.º 59
0
import keyword
keyword.kwlist

keyword.iskeyword('try')

'try'.isidentifier()

test = 10
id(test)
test = 10
id(test)
test = 11
id(test)

type(test)

test = 'char'
type(test)
Ejemplo n.º 60
0
def colorize(target=None, source=None, env=None):
    "Colorize python source"
    py = str(source[0])
    html = str(target[0])

    src = open(py, 'r').read()
    raw = string.strip(string.expandtabs(src))

    out = open(html, 'w')
    out.write('''
     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
     <html>
     <head>
     <title>%s</title>
     <style type="text/css">
     div.progs {
     background-color: #DCE3C4;
     border: thin solid black;
     padding: 1em;
     margin-left: 2em;
     margin-right: 2em; }
     div.dsets {
     background-color: #E3C4DC;
     border: thin solid black;
     padding: 1em;
     margin-left: 2em;
     margin-right: 2em; }
     div.scons {
     background-color: #FFF8ED;
     border: thin solid black;
     padding: 1em;
     margin-left: 2em;
     margin-right: 2em; }
     ''' % py)
    for style in _styles.keys():
        out.write('.%s { color: %s; }\n' % (_styles[style], _colors[style]))
    out.write('''</style>
     </head>
     <body>
     <div>
     <a href="paper_html/paper.html"><img width="32" height="32"
     align="bottom" border="0" alt="up" src="paper_html/icons/up.%s"></a>
     <a href="paper.pdf"><img src="paper_html/icons/pdf.%s" alt="[pdf]"
     width="32" height="32" border="0"></a>
     </div>
     <div class="scons">
     <table><tr><td>
     ''' % (itype, itype))

    # store line offsets in self.lines
    lines = [0, 0]
    _pos = 0
    while 1:
        _pos = string.find(raw, '\n', _pos) + 1
        if not _pos: break
        lines.append(_pos)
    lines.append(len(raw))

    # parse the source and write it
    _pos = 0
    text = cStringIO.StringIO(raw)
    out.write('<pre><font face="Lucida,Courier New">')

    def call(toktype, toktext, (srow, scol), (erow, ecol), line):
        global _pos

        # calculate new positions
        oldpos = _pos
        newpos = lines[srow] + scol
        _pos = newpos + len(toktext)

        # handle newlines
        if toktype in [token.NEWLINE, tokenize.NL]:
            out.write("\n")
            return

        # send the original whitespace, if needed
        if newpos > oldpos:
            out.write(raw[oldpos:newpos])

        # skip indenting tokens
        if toktype in [token.INDENT, token.DEDENT]:
            _pos = newpos
            return

        # map token type to a color group
        if token.LPAR <= toktype and toktype <= token.OP:
            toktype = token.OP
        elif toktype == token.NAME and keyword.iskeyword(toktext):
            toktype = _KEYWORD
        elif toktype == token.NAME and toktext in _colors.keys():
            toktype = toktext

        style = _styles.get(toktype, _styles[_TEXT])

        # send text
        out.write('<span class="%s">' % style)
        out.write(cgi.escape(toktext))
        out.write('</span>')