1+ from __future__ import print_function
12from inspect import isclass
23from sys import getsizeof
34try :
@@ -139,13 +140,15 @@ def generate_code():
139140 print (o )
140141
141142 # generate
142- generate_from_template ('mini_lambda_template.mako' , 'generated.py' ,
143- dict (to_override = to_override , to_override_with_exception = to_override_with_exception ))
144- generate_from_template ('mini_lambda_template_2.mako' , 'generated2.py' ,
143+ generate_from_template ('tpl_magic_methods.mako' , 'generated_magic.py' ,
145144 dict (to_override = to_override , to_override_with_exception = to_override_with_exception ))
145+ generate_from_template ('tpl_magic_methods_replacements.mako' , 'generated_magic_replacements.py' ,
146+ dict (to_override = to_override , to_override_with_exception = to_override_with_exception ,
147+ generate_all = True ))
146148
147149 # (2) to-do list for the second template
148- import_lines , to_create = define_goodies ()
150+ # --(a) builtins
151+ import_lines , to_create = define_goodies_builtins ()
149152
150153 # check outside of the template that it works:
151154 for o in import_lines :
@@ -154,8 +157,23 @@ def generate_code():
154157 print (o )
155158
156159 # generate
157- generate_from_template ('goodies_template.mako' , 'goodies_generated.py' ,
158- dict (import_lines = import_lines , to_create = to_create ))
160+ generate_from_template ('tpl_preconverted_symbols.mako' , 'symbols/builtins.py' ,
161+ dict (import_lines = import_lines , to_create = to_create , generate_all = True ))
162+
163+ # --(b) math
164+ for pkg in [math , decimal ]:
165+ import_lines , to_create = define_goodies (packages = [pkg ])
166+
167+ # check outside of the template that it works:
168+ for o in import_lines :
169+ print (o )
170+ for o in to_create :
171+ print (o )
172+
173+ # generate but without the '__all__' symbol since packages may not contain the same symbols across versions of
174+ # python
175+ generate_from_template ('tpl_preconverted_symbols.mako' , 'symbols/%s_.py' % pkg .__name__ ,
176+ dict (import_lines = import_lines , to_create = to_create , generate_all = False ))
159177
160178
161179def generate_from_template (src_file , dst_file , template_vars ):
@@ -399,25 +417,36 @@ def define_what_needs_to_be_written():
399417 return to_override_2 , to_override_with_exception_2
400418
401419
402- def define_goodies ():
403- # type: (...) -> Tuple[List[str], List[Goodie]]
404- """
405-
406- :return:
407- """
408- packages = [math , decimal ]
420+ def define_goodies_builtins ():
421+ import_list = list ()
422+ goodies_list = list ()
409423 built_in_functions = ['abs' , 'all' , 'any' , 'bin' , 'bool' , 'bytearray' , 'bytes' , 'callable' , 'chr' ,
410424 'classmethod' , 'compile' , 'complex' , 'delattr' , 'dict' , 'dir' , 'divmod' , 'enumerate' ,
411425 'eval' , 'filter' , 'float' , 'format' , 'frozenset' , 'getattr' , 'globals' , 'hasattr' ,
412426 'hash' , 'hex' , 'id' , 'input' , 'int' , 'isinstance' , 'issubclass' , 'iter' , 'len' ,
413427 'list' , 'locals' , 'map' , 'max' , 'memoryview' , 'min' , 'next' , 'object' , 'oct' , 'open' , 'ord' ,
414428 'pow' , 'print' , 'property' , 'range' , 'repr' , 'reversed' , 'round' , 'set' , 'setattr' , 'slice' ,
415429 'sorted' , 'staticmethod' , 'str' , 'sum' , 'super' , 'tuple' , 'type' , 'vars' , 'zip' ]
416- # these do not work in python 2
417- protected_functions = ['ascii' , 'help' , 'exec' ]
430+ # these do not work in python 2: the statement is reserved.
431+ # protected_functions = ['ascii', 'help', 'exec']
432+
433+ for function_name in built_in_functions :
434+ if isclass (eval (function_name )):
435+ goodies_list .append (Goodie (item_name = function_name .capitalize (), class_name = function_name ))
436+ else :
437+ goodies_list .append (Goodie (item_name = function_name .capitalize (), function_name = function_name ))
438+
439+ return import_list , goodies_list
440+
441+
442+ def define_goodies (packages ):
443+ # type: (...) -> Tuple[List[str], List[Goodie]]
444+ """
445+
446+ :return:
447+ """
418448 import_list = list ()
419449 goodies_list = list ()
420-
421450 for package in packages :
422451 # import pprint
423452 # pprint(getmembers(math, callable))
@@ -442,9 +471,6 @@ def define_goodies():
442471 # we do not import anymore at the top but in each one so as to put additional try/catch around them
443472 # import_list.append(import_string[0:-1])
444473
445- for function_name in built_in_functions :
446- goodies_list .append (Goodie (item_name = function_name .capitalize (), function_name = function_name ))
447-
448474 return import_list , goodies_list
449475
450476
0 commit comments