|
20 | 20 | "9": "nine", |
21 | 21 | } |
22 | 22 |
|
23 | | -name_map = { |
24 | | - "scope": "model_scope", # this is because 'scope' is a keyword in fairgraph |
25 | | - # we could rename the 'scope' keyword to 'stage' |
26 | | - # but we would have the same problem, as there is |
27 | | - # a property named 'stage' |
28 | | - # Suggested resolution: rename the property "scope" in openMINDS |
29 | | - # to "modelScope" or "hasScope" |
30 | | -} |
31 | | - |
32 | 23 |
|
33 | 24 | def generate_python_name(json_name, allow_multiple=False): |
34 | | - if json_name in name_map: |
35 | | - python_name = name_map[json_name] |
36 | | - else: |
37 | | - python_name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", json_name.strip()) |
38 | | - python_name = re.sub("([a-z0-9])([A-Z])", r"\1_\2", python_name).lower() |
39 | | - replacements = [ |
40 | | - ("-", "_"), |
41 | | - (".", "_"), |
42 | | - ("'", "_prime_"), |
43 | | - ("+", "plus"), |
44 | | - ("#", "sharp"), |
45 | | - (",", "comma"), |
46 | | - ("(", ""), |
47 | | - (")", ""), |
48 | | - ] |
49 | | - for before, after in replacements: |
50 | | - python_name = python_name.replace(before, after) |
51 | | - if python_name[0] in number_names: # Python variables can't start with a number |
52 | | - python_name = number_names[python_name[0]] + python_name[1:] |
53 | | - if not python_name.isidentifier(): |
54 | | - raise NameError(f"Cannot generate a valid Python name from '{json_name}'") |
| 25 | + python_name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", json_name.strip()) |
| 26 | + python_name = re.sub("([a-z0-9])([A-Z])", r"\1_\2", python_name).lower() |
| 27 | + replacements = [ |
| 28 | + ("-", "_"), |
| 29 | + (".", "_"), |
| 30 | + ("'", "_prime_"), |
| 31 | + ("+", "plus"), |
| 32 | + ("#", "sharp"), |
| 33 | + (",", "comma"), |
| 34 | + ("(", ""), |
| 35 | + (")", ""), |
| 36 | + ] |
| 37 | + for before, after in replacements: |
| 38 | + python_name = python_name.replace(before, after) |
| 39 | + if python_name[0] in number_names: # Python variables can't start with a number |
| 40 | + python_name = number_names[python_name[0]] + python_name[1:] |
| 41 | + if not python_name.isidentifier(): |
| 42 | + raise NameError(f"Cannot generate a valid Python name from '{json_name}'") |
55 | 43 | return python_name |
56 | 44 |
|
57 | 45 |
|
|
0 commit comments