Piccolo uses code generation for creating migrations. This function takes a class instance, and generates a string representation for it, which can be used in a migration file.
(class_instance: object)
| 2 | |
| 3 | |
| 4 | def repr_class_instance(class_instance: object) -> str: |
| 5 | """ |
| 6 | Piccolo uses code generation for creating migrations. This function takes |
| 7 | a class instance, and generates a string representation for it, which can |
| 8 | be used in a migration file. |
| 9 | """ |
| 10 | init_arg_names = [ |
| 11 | i |
| 12 | for i in inspect.signature( |
| 13 | class_instance.__class__.__init__ |
| 14 | ).parameters.keys() |
| 15 | if i not in ("self", "args", "kwargs") |
| 16 | ] |
| 17 | args_dict = {} |
| 18 | for arg_name in init_arg_names: |
| 19 | value = class_instance.__dict__.get(arg_name) |
| 20 | args_dict[arg_name] = value |
| 21 | |
| 22 | args_str = ", ".join( |
| 23 | f"{key}={value.__repr__()}" for key, value in args_dict.items() |
| 24 | ) |
| 25 | |
| 26 | return f"{class_instance.__class__.__name__}({args_str})" |