@@ -712,9 +712,17 @@ def new_openscad_class_str(class_name: str,
712
712
def _subbed_keyword (keyword : str ) -> str :
713
713
"""
714
714
Append an underscore to any python reserved word.
715
+ Prepend an underscore to any OpenSCAD identifier starting with a digit.
715
716
No-op for all other strings, e.g. 'or' => 'or_', 'other' => 'other'
716
717
"""
717
- new_key = keyword + '_' if keyword in PYTHON_ONLY_RESERVED_WORDS else keyword
718
+ new_key = keyword
719
+
720
+ if keyword in PYTHON_ONLY_RESERVED_WORDS :
721
+ new_key = keyword + "_"
722
+
723
+ if keyword [0 ].isdigit ():
724
+ new_key = "_" + keyword
725
+
718
726
if new_key != keyword :
719
727
print (f"\n Found OpenSCAD code that's not compatible with Python. \n "
720
728
f"Imported OpenSCAD code using `{ keyword } ` \n "
@@ -724,10 +732,16 @@ def _subbed_keyword(keyword: str) -> str:
724
732
def _unsubbed_keyword (subbed_keyword : str ) -> str :
725
733
"""
726
734
Remove trailing underscore for already-subbed python reserved words.
735
+ Remove prepending underscore if remaining identifier starts with a digit.
727
736
No-op for all other strings: e.g. 'or_' => 'or', 'other_' => 'other_'
728
737
"""
729
- shortened = subbed_keyword [:- 1 ]
730
- return shortened if shortened in PYTHON_ONLY_RESERVED_WORDS else subbed_keyword
738
+ if subbed_keyword .endswith ("_" ) and subbed_keyword [:- 1 ] in PYTHON_ONLY_RESERVED_WORDS :
739
+ return subbed_keyword [:- 1 ]
740
+
741
+ if subbed_keyword .startswith ("_" ) and subbed_keyword [1 ].isdigit ():
742
+ return subbed_keyword [1 :]
743
+
744
+ return subbed_keyword
731
745
732
746
# now that we have the base class defined, we can do a circular import
733
747
from . import objects
0 commit comments