@@ -739,9 +739,17 @@ def new_openscad_class_str(class_name: str,
739
739
def _subbed_keyword (keyword : str ) -> str :
740
740
"""
741
741
Append an underscore to any python reserved word.
742
+ Prepend an underscore to any OpenSCAD identifier starting with a digit.
742
743
No-op for all other strings, e.g. 'or' => 'or_', 'other' => 'other'
743
744
"""
744
- new_key = keyword + '_' if keyword in PYTHON_ONLY_RESERVED_WORDS else keyword
745
+ new_key = keyword
746
+
747
+ if keyword in PYTHON_ONLY_RESERVED_WORDS :
748
+ new_key = keyword + "_"
749
+
750
+ if keyword [0 ].isdigit ():
751
+ new_key = "_" + keyword
752
+
745
753
if new_key != keyword :
746
754
print (f"\n Found OpenSCAD code that's not compatible with Python. \n "
747
755
f"Imported OpenSCAD code using `{ keyword } ` \n "
@@ -751,10 +759,16 @@ def _subbed_keyword(keyword: str) -> str:
751
759
def _unsubbed_keyword (subbed_keyword : str ) -> str :
752
760
"""
753
761
Remove trailing underscore for already-subbed python reserved words.
762
+ Remove prepending underscore if remaining identifier starts with a digit.
754
763
No-op for all other strings: e.g. 'or_' => 'or', 'other_' => 'other_'
755
764
"""
756
- shortened = subbed_keyword [:- 1 ]
757
- return shortened if shortened in PYTHON_ONLY_RESERVED_WORDS else subbed_keyword
765
+ if subbed_keyword .endswith ("_" ) and subbed_keyword [:- 1 ] in PYTHON_ONLY_RESERVED_WORDS :
766
+ return subbed_keyword [:- 1 ]
767
+
768
+ if subbed_keyword .startswith ("_" ) and subbed_keyword [1 ].isdigit ():
769
+ return subbed_keyword [1 :]
770
+
771
+ return subbed_keyword
758
772
759
773
# now that we have the base class defined, we can do a circular import
760
774
from . import objects
0 commit comments