@@ -51,6 +51,7 @@ class EvaluableExpression(str):
51
51
EvaluableExpression is a string that can be evaluated to a value during MDF execution. This class inherits from
52
52
str, so it can be used as a string.
53
53
"""
54
+
54
55
def __init__ (self , expr ):
55
56
self .expr = expr
56
57
@@ -68,7 +69,7 @@ def print_(text: str, print_it: bool = False):
68
69
if not isinstance (text , str ):
69
70
text = ("%s" % text ).decode ("ascii" )
70
71
if print_it :
71
- print ("%s%s" % (prefix , text .replace ("\n " , "\n " + prefix )))
72
+ print ("{}{}" . format (prefix , text .replace ("\n " , "\n " + prefix )))
72
73
73
74
74
75
def print_v (text : str ):
@@ -372,7 +373,9 @@ def _is_child_field(cls, field_name: str) -> bool:
372
373
try :
373
374
f = attr .fields_dict (cls )[field_name ]
374
375
except KeyError :
375
- raise ValueError (f"Field '{ field_name } ' not found in modelspec class '{ cls .__name__ } '" )
376
+ raise ValueError (
377
+ f"Field '{ field_name } ' not found in modelspec class '{ cls .__name__ } '"
378
+ )
376
379
377
380
# Check if the type of the field is a list or dict
378
381
collection_arg = None
@@ -430,13 +433,13 @@ def _is_evaluable_expression(value: Any) -> bool:
430
433
431
434
@classmethod
432
435
def _is_base_type (
433
- cls ,
434
- value ,
435
- can_be_list = False ,
436
- can_be_dict = False ,
437
- can_be_ndarray = False ,
438
- can_be_none = False ,
439
- can_be_eval_expr = False ,
436
+ cls ,
437
+ value ,
438
+ can_be_list = False ,
439
+ can_be_dict = False ,
440
+ can_be_ndarray = False ,
441
+ can_be_none = False ,
442
+ can_be_eval_expr = False ,
440
443
):
441
444
442
445
import numpy
@@ -452,16 +455,16 @@ def _is_base_type(
452
455
value = get_origin (value )
453
456
454
457
return (
455
- value == int
456
- or value == str
457
- or value == bool
458
- or value == float
459
- or (can_be_list and value == list )
460
- or (can_be_dict and value == dict )
461
- or (can_be_ndarray and value == numpy .ndarray )
462
- or (can_be_none and value is type (None ))
463
- or (can_be_eval_expr and cls ._is_evaluable_expression (value ))
464
- or value == Union
458
+ value == int
459
+ or value == str
460
+ or value == bool
461
+ or value == float
462
+ or (can_be_list and value == list )
463
+ or (can_be_dict and value == dict )
464
+ or (can_be_ndarray and value == numpy .ndarray )
465
+ or (can_be_none and value is type (None ))
466
+ or (can_be_eval_expr and cls ._is_evaluable_expression (value ))
467
+ or value == Union
465
468
)
466
469
467
470
@staticmethod
@@ -488,7 +491,11 @@ def _type_to_str(type_: Any) -> str:
488
491
elif get_origin (type_ ) == dict and len (get_args (type_ )) > 0 :
489
492
return Base ._type_to_str (get_args (type_ )[1 ])
490
493
elif get_origin (type_ ) == Union and len (get_args (type_ )) > 0 :
491
- return "Union[" + ", " .join ([Base ._type_to_str (arg ) for arg in get_args (type_ )]) + "]"
494
+ return (
495
+ "Union["
496
+ + ", " .join ([Base ._type_to_str (arg ) for arg in get_args (type_ )])
497
+ + "]"
498
+ )
492
499
493
500
# Fallback to returning just the string representation. Drop any occurrence of typing
494
501
return str (type_ ).replace ("typing." , "" )
@@ -520,9 +527,9 @@ def _cls_generate_documentation(cls, format: str = MARKDOWN_FORMAT):
520
527
allowed_fields = cls ._parse_allowed_fields ()
521
528
allowed_children = cls ._parse_allowed_children ()
522
529
523
- print (" - %s (%s)" % ( cls .__name__ , definition ) )
530
+ print (f " - { cls .__name__ } ( { definition } )" )
524
531
525
- rst_url_format = ' `%s <%s>`_'
532
+ rst_url_format = " `%s <%s>`_"
526
533
527
534
def insert_links (text , format = MARKDOWN_FORMAT ):
528
535
if not "_" in text :
@@ -534,12 +541,15 @@ def insert_links(text, format=MARKDOWN_FORMAT):
534
541
for i in range (int (len (split ) / 2.0 )):
535
542
pre = split [i * 2 ]
536
543
type = split [i * 2 + 1 ]
537
- if format == MARKDOWN_FORMAT :
538
- text2 += '%s<a href="#%s">%s</a>' % (pre , type .lower (), type )
539
- elif format == RST_FORMAT :
540
- #text2 += ('%s'+rst_url_format) % (pre, type, '#'+type.lower # problem with handling links ending with s e.g. _Graph_s
541
-
542
- text2 += ('%s%s' ) % (pre , type ) # temp hack... problem with handling links ending with s e.g. _Graph_s
544
+ if format == MARKDOWN_FORMAT :
545
+ text2 += f'{ pre } <a href="#{ type .lower ()} ">{ type } </a>'
546
+ elif format == RST_FORMAT :
547
+ # text2 += ('%s'+rst_url_format) % (pre, type, '#'+type.lower # problem with handling links ending with s e.g. _Graph_s
548
+
549
+ text2 += ("%s%s" ) % (
550
+ pre ,
551
+ type ,
552
+ ) # temp hack... problem with handling links ending with s e.g. _Graph_s
543
553
if int (len (split ) / 2.0 ) != len (split ) / 2.0 :
544
554
text2 += split [- 1 ]
545
555
return text2
@@ -551,7 +561,7 @@ def insert_links(text, format=MARKDOWN_FORMAT):
551
561
if definition is not None :
552
562
doc_string += "%s\n \n " % insert_links (definition )
553
563
elif format == RST_FORMAT :
554
- doc_string += "%s \n %s \n %s \n " % ("=" * len (name ), name , "=" * len (name ))
564
+ doc_string += "{} \n {} \n {} \n " . format ("=" * len (name ), name , "=" * len (name ))
555
565
if definition is not None :
556
566
doc_string += "%s\n \n " % insert_links (definition , format = RST_FORMAT )
557
567
elif format == DICT_FORMAT :
@@ -576,19 +586,17 @@ def insert_links(text, format=MARKDOWN_FORMAT):
576
586
type_ , can_be_eval_expr = True , can_be_dict = True
577
587
)
578
588
type_str = Base ._type_to_str (type_ )
579
- print (" Allowed parameter: %s %s" % (f , (description , type_str )))
589
+ print (" Allowed parameter: {} {}" . format (f , (description , type_str )))
580
590
581
591
if format == DICT_FORMAT :
582
592
doc_dict [name ]["allowed_parameters" ][f ] = {}
583
593
doc_dict [name ]["allowed_parameters" ][f ]["type" ] = type_str
584
- doc_dict [name ]["allowed_parameters" ][f ][
585
- "description"
586
- ] = description
594
+ doc_dict [name ]["allowed_parameters" ][f ]["description" ] = description
587
595
588
596
elif format == MARKDOWN_FORMAT :
589
- doc_string += "<tr><td><b>%s </b></td><td>%s </td>" % (
597
+ doc_string += "<tr><td><b>{} </b></td><td>{} </td>" . format (
590
598
f ,
591
- '<a href="#%s">%s </a>' % ( type_str . lower (), type_str )
599
+ f '<a href="#{ type_str . lower () } "> { type_str } </a>'
592
600
if referencable
593
601
else type_str ,
594
602
)
@@ -598,14 +606,12 @@ def insert_links(text, format=MARKDOWN_FORMAT):
598
606
599
607
elif format == RST_FORMAT :
600
608
n = "**%s**" % f
601
- t = "%s" % (
602
- rst_url_format % (type_ , '#' + type_str .lower ())
609
+ t = "{}" . format (
610
+ rst_url_format % (type_ , "#" + type_str .lower ())
603
611
if referencable
604
612
else type_str ,
605
613
)
606
- d = "*%s*" % (
607
- insert_links (description , format = RST_FORMAT )
608
- )
614
+ d = "*%s*" % (insert_links (description , format = RST_FORMAT ))
609
615
table_info .append ([n , t , d ])
610
616
611
617
if referencable :
@@ -615,7 +621,13 @@ def insert_links(text, format=MARKDOWN_FORMAT):
615
621
if format == MARKDOWN_FORMAT :
616
622
doc_string += "\n </table>\n \n "
617
623
elif format == RST_FORMAT :
618
- doc_string += "%s\n \n " % (tabulate (table_info , ['Allowed field' , 'Data Type' , 'Description' ], tablefmt = "rst" ))
624
+ doc_string += "%s\n \n " % (
625
+ tabulate (
626
+ table_info ,
627
+ ["Allowed field" , "Data Type" , "Description" ],
628
+ tablefmt = "rst" ,
629
+ )
630
+ )
619
631
620
632
if len (allowed_children ) > 0 :
621
633
if format == MARKDOWN_FORMAT :
@@ -629,23 +641,21 @@ def insert_links(text, format=MARKDOWN_FORMAT):
629
641
630
642
for c , (description , type_ ) in allowed_children .items ():
631
643
type_str = Base ._type_to_str (type_ )
632
- print (" Allowed child: %s %s" % (c , (description , type_str )))
644
+ print (" Allowed child: {} {}" . format (c , (description , type_str )))
633
645
634
- referencable = not Base ._is_base_type (
635
- type_ , can_be_dict = True
636
- )
646
+ referencable = not Base ._is_base_type (type_ , can_be_dict = True )
637
647
638
648
if format == DICT_FORMAT :
639
649
doc_dict [name ]["allowed_children" ][c ] = {}
640
650
doc_dict [name ]["allowed_children" ][c ]["type" ] = type_str
641
- doc_dict [name ]["allowed_children" ][c ][
642
- "description"
643
- ] = allowed_children [ c ] [0 ]
651
+ doc_dict [name ]["allowed_children" ][c ]["description" ] = allowed_children [
652
+ c
653
+ ][0 ]
644
654
645
655
elif format == MARKDOWN_FORMAT :
646
- doc_string += "<tr><td><b>%s </b></td><td>%s </td>" % (
656
+ doc_string += "<tr><td><b>{} </b></td><td>{} </td>" . format (
647
657
c ,
648
- '<a href="#%s">%s </a>' % ( type_str . lower (), type_str )
658
+ f '<a href="#{ type_str . lower () } "> { type_str } </a>'
649
659
if referencable
650
660
else type_str ,
651
661
)
@@ -655,14 +665,12 @@ def insert_links(text, format=MARKDOWN_FORMAT):
655
665
656
666
elif format == RST_FORMAT :
657
667
n = "**%s**" % c
658
- t = "%s" % (
659
- rst_url_format % (type_str , '#' + type_str .lower ())
668
+ t = "{}" . format (
669
+ rst_url_format % (type_str , "#" + type_str .lower ())
660
670
if referencable
661
671
else type_str ,
662
672
)
663
- d = "*%s*" % (
664
- insert_links (description , format = RST_FORMAT )
665
- )
673
+ d = "*%s*" % (insert_links (description , format = RST_FORMAT ))
666
674
table_info .append ([n , t , d ])
667
675
668
676
# Get the contained type
@@ -677,7 +685,13 @@ def insert_links(text, format=MARKDOWN_FORMAT):
677
685
if format == MARKDOWN_FORMAT :
678
686
doc_string += "\n </table>\n \n "
679
687
elif format == RST_FORMAT :
680
- doc_string += "%s\n \n " % (tabulate (table_info , ['Allowed child' ,'Data Type' ,'Description' ], tablefmt = "rst" ))
688
+ doc_string += "%s\n \n " % (
689
+ tabulate (
690
+ table_info ,
691
+ ["Allowed child" , "Data Type" , "Description" ],
692
+ tablefmt = "rst" ,
693
+ )
694
+ )
681
695
682
696
for r in referenced :
683
697
if format in (MARKDOWN_FORMAT , RST_FORMAT ):
0 commit comments