Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_letter_literal_convert(self, tokens):
"""Processes set literals."""
if len(tokens) == 1:
set_type = tokens[0]
if set_type == "s":
return "__coconut__.set()"
elif set_type == "f":
return "__coconut__.frozenset()"
else:
raise CoconutException("invalid set type", set_type)
elif len(tokens) == 2:
set_type, set_items = tokens
if len(set_items) != 1:
raise CoconutException("invalid set literal item", tokens[0])
elif set_type == "s":
if self.version == "3":
return "{" + set_items[0] + "}"
else:
return "__coconut__.set(" + set_to_tuple(set_items) + ")"
elif set_type == "f":
return "__coconut__.frozenset(" + set_to_tuple(set_items) + ")"
else:
raise CoconutException("invalid set type", set_type)
else:
raise CoconutException("invalid set literal tokens", tokens)
def string_repl(self, tokens):
"""Replaces string references."""
if len(tokens) == 1:
ref = self.refs[int(tokens[0])]
if isinstance(ref, tuple):
string, strchar, multiline = ref
if multiline:
string = strchar*3+string+strchar*3
else:
string = strchar+string+strchar
return string
else:
raise CoconutException("string marker points to comment/passthrough")
else:
raise CoconutException("invalid string marker", tokens)
def string_repl(self, tokens):
"""Replaces string references."""
if len(tokens) == 1:
ref = self.refs[int(tokens[0])]
if isinstance(ref, tuple):
string, strchar, multiline = ref
if multiline:
string = strchar*3+string+strchar*3
else:
string = strchar+string+strchar
return string
else:
raise CoconutException("string marker points to comment/passthrough")
else:
raise CoconutException("invalid string marker", tokens)
raise CoconutException("invalid Python version", version)
header += '''
# -*- coding: '''+ENCODING+''' -*-
'''
if usehash is not None:
header += hash_prefix + usehash + linebreak
header += '''
# Compiled with Coconut version '''+VERSION_STR+'''
'''
if which == "package":
header += r'''"""Built-in Coconut functions."""
'''
elif usehash is not None:
raise CoconutException("can only add a hash to an initial or package header, not "+str(which))
else:
header = ""
if which != "initial":
header += r'''# Coconut Header: --------------------------------------------------------------
'''
if version == "2":
header += r'''
from __future__ import with_statement, print_function, absolute_import, unicode_literals, division
'''+PY2_HEADER+r'''
'''
elif version is None:
header += r'''
from __future__ import with_statement, print_function, absolute_import, unicode_literals, division
import sys as _coconut_sys
if _coconut_sys.version_info < (3,):
'''
def match_func_proc(tokens):
"""Processes match mathematical function definitions."""
if len(tokens) == 2:
return tokens[0] + "return " + tokens[1] + linebreak + closestr
else:
raise CoconutException("invalid pattern-matching mathematical function definition tokens", tokens)
elif op == "<|=":
out += name+" = "+name+"("+item+")"
elif op == "<*|=":
out += name+" = "+name+"(*"+item+")"
elif op == "..=":
out += name+" = (lambda f, g: lambda *args, **kwargs: f(g(*args, **kwargs)))("+name+", "+item+")"
elif op == "::=":
ichain_var = lazy_chain_var+"_"+str(self.ichain_count)
out += ichain_var+" = "+name+linebreak
out += name+" = __coconut__.itertools.chain.from_iterable("+lazy_list_proc([ichain_var, item])+")"
self.ichain_count += 1
else:
out += name+" "+op+" "+item
return out
else:
raise CoconutException("invalid assignment tokens", tokens)
def headers(which, version=None, usehash=None):
"""Generates the specified header."""
if which == "none":
return ""
elif which == "initial" or which == "package":
if version is None:
header = "#!/usr/bin/env python"
elif version == "2":
header = "#!/usr/bin/env python2"
elif version == "3":
header = "#!/usr/bin/env python3"
else:
raise CoconutException("invalid Python version", version)
header += '''
# -*- coding: '''+ENCODING+''' -*-
'''
if usehash is not None:
header += hash_prefix + usehash + linebreak
header += '''
# Compiled with Coconut version '''+VERSION_STR+'''
'''
if which == "package":
header += r'''"""Built-in Coconut functions."""
'''
elif usehash is not None:
raise CoconutException("can only add a hash to an initial or package header, not "+str(which))
else:
def attr_proc(tokens):
"""Processes attrgetter literals."""
if len(tokens) == 1:
return '__coconut__.operator.attrgetter("'+tokens[0]+'")'
else:
raise CoconutException("invalid attrgetter literal tokens", tokens)
def setup(self, strict=False, version=None):
"""Initializes strict and version."""
if version in self.versions:
self.version = version
else:
raise CoconutException("unsupported target Python version " + ascii(version)
+ " (supported targets are '2' and '3', or leave blank for universal)")
self.strict = strict