9999 'deserialize' ,
100100 'get' ,
101101 'set_image_dim_ordering' ,
102+ 'normalize_data_format' ,
102103 'image_dim_ordering' ,
103104 'get_variable_shape' ,
104105}
114115PAGES = [
115116 {
116117 'page' : 'models/sequential.md' ,
117- 'functions ' : [
118+ 'methods ' : [
118119 models .Sequential .compile ,
119120 models .Sequential .fit ,
120121 models .Sequential .evaluate ,
130131 },
131132 {
132133 'page' : 'models/model.md' ,
133- 'functions ' : [
134+ 'methods ' : [
134135 models .Model .compile ,
135136 models .Model .fit ,
136137 models .Model .evaluate ,
341342ROOT = 'http://keras.io/'
342343
343344
344- def get_earliest_class_that_defined_member (member , cls ):
345- ancestors = get_classes_ancestors ([cls ])
346- result = None
347- for ancestor in ancestors :
348- if member in dir (ancestor ):
349- result = ancestor
350- if not result :
351- return cls
352- return result
353-
354-
355- def get_classes_ancestors (classes ):
356- ancestors = []
357- for cls in classes :
358- ancestors += cls .__bases__
359- filtered_ancestors = []
360- for ancestor in ancestors :
361- if ancestor .__name__ in ['object' ]:
362- continue
363- filtered_ancestors .append (ancestor )
364- if filtered_ancestors :
365- return filtered_ancestors + get_classes_ancestors (filtered_ancestors )
366- else :
367- return filtered_ancestors
368-
369-
370345def get_function_signature (function , method = True ):
371346 wrapped = getattr (function , '_original_function' , None )
372347 if wrapped is None :
@@ -395,10 +370,6 @@ def get_function_signature(function, method=True):
395370 signature = st [:- 2 ] + ')'
396371 else :
397372 signature = st + ')'
398-
399- if not method :
400- # Prepend the module name.
401- signature = clean_module_name (function .__module__ ) + '.' + signature
402373 return post_process_signature (signature )
403374
404375
@@ -409,12 +380,15 @@ def get_class_signature(cls):
409380 except (TypeError , AttributeError ):
410381 # in case the class inherits from object and does not
411382 # define __init__
412- class_signature = clean_module_name (cls .__module__ ) + '.' + cls .__name__ + '()'
383+ class_signature = "{clean_module_name}.{cls_name}()" .format (
384+ clean_module_name = clean_module_name (cls .__module__ ),
385+ cls_name = cls .__name__
386+ )
413387 return post_process_signature (class_signature )
414388
415389
416390def post_process_signature (signature ):
417- parts = re .split ('\.(?!\d)' , signature )
391+ parts = re .split (r '\.(?!\d)' , signature )
418392 if len (parts ) >= 4 :
419393 if parts [1 ] == 'layers' :
420394 signature = 'keras.layers.' + '.' .join (parts [3 :])
@@ -459,7 +433,7 @@ def code_snippet(snippet):
459433
460434
461435def count_leading_spaces (s ):
462- ws = re .search ('\S' , s )
436+ ws = re .search (r '\S' , s )
463437 if ws :
464438 return ws .start ()
465439 else :
@@ -468,7 +442,8 @@ def count_leading_spaces(s):
468442
469443def process_list_block (docstring , starting_point , leading_spaces , marker ):
470444 ending_point = docstring .find ('\n \n ' , starting_point )
471- block = docstring [starting_point :None if ending_point == - 1 else ending_point - 1 ]
445+ block = docstring [starting_point :(None if ending_point == - 1 else
446+ ending_point - 1 )]
472447 # Place marker for later reinjection.
473448 docstring = docstring .replace (block , marker )
474449 lines = block .split ('\n ' )
@@ -596,7 +571,6 @@ def process_docstring(docstring):
596571 shutil .copy (fpath , new_fpath )
597572
598573
599- # Take care of index page.
600574def read_file (path ):
601575 with open (path ) as f :
602576 return f .read ()
@@ -616,16 +590,37 @@ def collect_class_methods(cls, methods):
616590def render_function (function , method = True ):
617591 subblocks = []
618592 signature = get_function_signature (function , method = method )
619- signature = signature .replace (function .__module__ + '.' , '' )
620- level = 3
621- subblocks .append ('#' * level + ' ' + function .__name__ + '\n ' )
593+ if method :
594+ signature = signature .replace (
595+ clean_module_name (function .__module__ ) + '.' , '' )
596+ subblocks .append ('### ' + function .__name__ + '\n ' )
622597 subblocks .append (code_snippet (signature ))
623598 docstring = function .__doc__
624599 if docstring :
625600 subblocks .append (process_docstring (docstring ))
626601 return '\n \n ' .join (subblocks )
627602
628603
604+ def read_page_data (page_data , type ):
605+ assert type in ['classes' , 'functions' , 'methods' ]
606+ data = page_data .get (type , [])
607+ for module in page_data .get ('all_module_{}' .format (type ), []):
608+ module_data = []
609+ for name in dir (module ):
610+ if name [0 ] == '_' or name in EXCLUDE :
611+ continue
612+ module_member = getattr (module , name )
613+ if (inspect .isclass (module_member ) and type == 'classes' or
614+ inspect .isfunction (module_member ) and type == 'functions' ):
615+ instance = module_member
616+ if module .__name__ in instance .__module__ :
617+ if instance not in module_data :
618+ module_data .append (instance )
619+ module_data .sort (key = lambda x : id (x ))
620+ data += module_data
621+ return data
622+
623+
629624if __name__ == '__main__' :
630625 readme = read_file ('../README.md' )
631626 index = read_file ('templates/index.md' )
@@ -635,22 +630,9 @@ def render_function(function, method=True):
635630
636631 print ('Generating docs for Keras %s.' % keras .__version__ )
637632 for page_data in PAGES :
638- blocks = []
639- classes = page_data .get ('classes' , [])
640- for module in page_data .get ('all_module_classes' , []):
641- module_classes = []
642- for name in dir (module ):
643- if name [0 ] == '_' or name in EXCLUDE :
644- continue
645- module_member = getattr (module , name )
646- if inspect .isclass (module_member ):
647- cls = module_member
648- if cls .__module__ == module .__name__ :
649- if cls not in module_classes :
650- module_classes .append (cls )
651- module_classes .sort (key = lambda x : id (x ))
652- classes += module_classes
633+ classes = read_page_data (page_data , 'classes' )
653634
635+ blocks = []
654636 for element in classes :
655637 if not isinstance (element , (list , tuple )):
656638 element = (element , [])
@@ -675,20 +657,12 @@ def render_function(function, method=True):
675657 [render_function (method , method = True ) for method in methods ]))
676658 blocks .append ('\n ' .join (subblocks ))
677659
678- functions = page_data .get ('functions' , [])
679- for module in page_data .get ('all_module_functions' , []):
680- module_functions = []
681- for name in dir (module ):
682- if name [0 ] == '_' or name in EXCLUDE :
683- continue
684- module_member = getattr (module , name )
685- if inspect .isfunction (module_member ):
686- function = module_member
687- if module .__name__ in function .__module__ :
688- if function not in module_functions :
689- module_functions .append (function )
690- module_functions .sort (key = lambda x : id (x ))
691- functions += module_functions
660+ methods = read_page_data (page_data , 'methods' )
661+
662+ for method in methods :
663+ blocks .append (render_function (method , method = True ))
664+
665+ functions = read_page_data (page_data , 'functions' )
692666
693667 for function in functions :
694668 blocks .append (render_function (function , method = False ))
@@ -706,7 +680,8 @@ def render_function(function, method=True):
706680 if os .path .exists (path ):
707681 template = read_file (path )
708682 assert '{{autogenerated}}' in template , ('Template found for ' + path +
709- ' but missing {{autogenerated}} tag.' )
683+ ' but missing {{autogenerated}}'
684+ ' tag.' )
710685 mkdown = template .replace ('{{autogenerated}}' , mkdown )
711686 print ('...inserting autogenerated content into template:' , path )
712687 else :
0 commit comments