@@ -262,9 +262,10 @@ def __init__(self, params_type, **kwargs):
262262 self .__dict__ .update (__params_type__ = params_type , __signatures__ = None )
263263
264264 def __repr__ (self ):
265- return "Params({})" . format (
266- ", " . join (( f"{ k } :{ type (self [k ]).__name__ } :{ self [k ]} " ) for k in sorted (self ) )
265+ args = ", " . join (
266+ ( f"{ k } :{ type (self [k ]).__name__ } :{ self [k ]} " ) for k in sorted (self )
267267 )
268+ return f"Params({ args } )"
268269
269270 def __getattr__ (self , key ):
270271 if key not in self :
@@ -422,9 +423,10 @@ def __getattr__(self, key):
422423 return super ().__getattr__ (self , key )
423424
424425 def __repr__ (self ):
425- return "ParamsType<{}>" . format (
426- ", " . join (( f"{ self .fields [i ]} :{ self .types [i ]} " ) for i in range (self .length ) )
426+ args = ", " . join (
427+ f"{ self .fields [i ]} :{ self .types [i ]} " for i in range (self .length )
427428 )
429+ return f"ParamsType<{ args } >"
428430
429431 def __eq__ (self , other ):
430432 return (
@@ -730,25 +732,24 @@ def c_support_code(self, **kwargs):
730732 struct_init = "\n " .join (c_init_list )
731733 struct_cleanup = "\n " .join (c_cleanup_list )
732734 struct_extract = "\n \n " .join (c_extract_list )
733- struct_extract_method = """
735+ args = "\n " .join (
736+ f"case { i } : extract_{ self .fields [i ]} (object); break;"
737+ for i in range (self .length )
738+ )
739+ struct_extract_method = f"""
734740 void extract(PyObject* object, int field_pos) {{
735741 switch(field_pos) {{
736742 // Extraction cases.
737- {}
743+ { args }
738744 // Default case.
739745 default:
740746 PyErr_Format(PyExc_TypeError, "ParamsType: no extraction defined for a field %d.", field_pos);
741747 this->setErrorOccurred();
742748 break;
743749 }}
744750 }}
745- """ .format (
746- "\n " .join (
747- ("case %d: extract_%s(object); break;" % (i , self .fields [i ]))
748- for i in range (self .length )
749- )
750- )
751- final_struct_code = """
751+ """
752+ final_struct_code = f"""
752753 /** ParamsType { struct_name } **/
753754 #ifndef { struct_name_defined }
754755 #define { struct_name_defined }
@@ -790,17 +791,7 @@ def c_support_code(self, **kwargs):
790791 }};
791792 #endif
792793 /** End ParamsType { struct_name } **/
793- """ .format (
794- ** dict (
795- struct_name_defined = struct_name_defined ,
796- struct_name = struct_name ,
797- struct_declare = struct_declare ,
798- struct_init = struct_init ,
799- struct_cleanup = struct_cleanup ,
800- struct_extract = struct_extract ,
801- struct_extract_method = struct_extract_method ,
802- )
803- )
794+ """
804795
805796 return [* sorted (c_support_code_set ), final_struct_code ]
806797
@@ -813,9 +804,9 @@ def c_code_cache_version(self):
813804 # pointers.
814805
815806 def c_declare (self , name , sub , check_input = True ):
816- return """
817- {struct_name }* {name};
818- """ . format ( ** dict ( struct_name = self . name , name = name ))
807+ return f """
808+ { self . name } * { name } ;
809+ """
819810
820811 def c_init (self , name , sub ):
821812 # NB: It seems c_init() is not called for an op param.
@@ -831,40 +822,33 @@ def c_cleanup(self, name, sub):
831822 """
832823
833824 def c_extract (self , name , sub , check_input = True , ** kwargs ):
834- return """
825+ fields_list = ", " .join (f'"{ x } "' for x in self .fields )
826+ return f"""
835827 /* Seems c_init() is not called for a op param. So I call `new` here. */
836- {name} = new {struct_name };
828+ { name } = new { self . name } ;
837829
838830 {{ // This need a separate namespace for Clinker
839831 const char* fields[] = {{{ fields_list } }};
840832 if (py_{ name } == Py_None) {{
841833 PyErr_SetString(PyExc_ValueError, "ParamsType: expected an object, not None.");
842- {fail}
834+ { sub [ ' fail' ] }
843835 }}
844- for (int i = 0; i < {length}; ++i) {{
836+ for (int i = 0; i < { self . length } ; ++i) {{
845837 PyObject* o = PyDict_GetItemString(py_{ name } , fields[i]);
846838 if (o == NULL) {{
847839 PyErr_Format(PyExc_TypeError, "ParamsType: missing expected attribute \\ "%s\\ " in object.", fields[i]);
848- {fail}
840+ { sub [ ' fail' ] }
849841 }}
850842 { name } ->extract(o, i);
851843 if ({ name } ->errorOccurred()) {{
852844 /* The extract code from attribute type should have already raised a Python exception,
853845 * so we just print the attribute name in stderr. */
854846 fprintf(stderr, "\\ nParamsType: error when extracting value for attribute \\ "%s\\ ".\\ n", fields[i]);
855- {fail}
847+ { sub [ ' fail' ] }
856848 }}
857849 }}
858850 }}
859- """ .format (
860- ** dict (
861- name = name ,
862- struct_name = self .name ,
863- length = self .length ,
864- fail = sub ["fail" ],
865- fields_list = '"{}"' .format ('", "' .join (self .fields )),
866- )
867- )
851+ """
868852
869853 def c_sync (self , name , sub ):
870854 # FIXME: Looks like we need to decrement a reference count our two.
0 commit comments