topologicpy 0.5.9__py3-none-any.whl → 6.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. topologicpy/Aperture.py +72 -72
  2. topologicpy/Cell.py +2169 -2169
  3. topologicpy/CellComplex.py +1137 -1137
  4. topologicpy/Cluster.py +1288 -1280
  5. topologicpy/Color.py +423 -423
  6. topologicpy/Context.py +79 -79
  7. topologicpy/DGL.py +3213 -3240
  8. topologicpy/Dictionary.py +698 -698
  9. topologicpy/Edge.py +1187 -1187
  10. topologicpy/EnergyModel.py +1180 -1152
  11. topologicpy/Face.py +2141 -2141
  12. topologicpy/Graph.py +7768 -7768
  13. topologicpy/Grid.py +353 -353
  14. topologicpy/Helper.py +507 -507
  15. topologicpy/Honeybee.py +461 -461
  16. topologicpy/Matrix.py +271 -271
  17. topologicpy/Neo4j.py +521 -521
  18. topologicpy/Plotly.py +2 -2
  19. topologicpy/Polyskel.py +541 -541
  20. topologicpy/Shell.py +1768 -1768
  21. topologicpy/Speckle.py +508 -508
  22. topologicpy/Topology.py +7060 -7002
  23. topologicpy/Vector.py +905 -905
  24. topologicpy/Vertex.py +1585 -1585
  25. topologicpy/Wire.py +3050 -3050
  26. topologicpy/__init__.py +22 -38
  27. topologicpy/version.py +1 -0
  28. {topologicpy-0.5.9.dist-info → topologicpy-6.0.0.dist-info}/LICENSE +661 -704
  29. topologicpy-6.0.0.dist-info/METADATA +751 -0
  30. topologicpy-6.0.0.dist-info/RECORD +32 -0
  31. topologicpy/bin/linux/topologic/__init__.py +0 -2
  32. topologicpy/bin/linux/topologic/libTKBO-6bdf205d.so.7.7.0 +0 -0
  33. topologicpy/bin/linux/topologic/libTKBRep-2960a069.so.7.7.0 +0 -0
  34. topologicpy/bin/linux/topologic/libTKBool-c44b74bd.so.7.7.0 +0 -0
  35. topologicpy/bin/linux/topologic/libTKFillet-9a670ba0.so.7.7.0 +0 -0
  36. topologicpy/bin/linux/topologic/libTKG2d-8f31849e.so.7.7.0 +0 -0
  37. topologicpy/bin/linux/topologic/libTKG3d-4c6bce57.so.7.7.0 +0 -0
  38. topologicpy/bin/linux/topologic/libTKGeomAlgo-26066fd9.so.7.7.0 +0 -0
  39. topologicpy/bin/linux/topologic/libTKGeomBase-2116cabe.so.7.7.0 +0 -0
  40. topologicpy/bin/linux/topologic/libTKMath-72572fa8.so.7.7.0 +0 -0
  41. topologicpy/bin/linux/topologic/libTKMesh-2a060427.so.7.7.0 +0 -0
  42. topologicpy/bin/linux/topologic/libTKOffset-6cab68ff.so.7.7.0 +0 -0
  43. topologicpy/bin/linux/topologic/libTKPrim-eb1262b3.so.7.7.0 +0 -0
  44. topologicpy/bin/linux/topologic/libTKShHealing-e67e5cc7.so.7.7.0 +0 -0
  45. topologicpy/bin/linux/topologic/libTKTopAlgo-e4c96c33.so.7.7.0 +0 -0
  46. topologicpy/bin/linux/topologic/libTKernel-fb7fe3b7.so.7.7.0 +0 -0
  47. topologicpy/bin/linux/topologic/libgcc_s-32c1665e.so.1 +0 -0
  48. topologicpy/bin/linux/topologic/libstdc++-672d7b41.so.6.0.30 +0 -0
  49. topologicpy/bin/linux/topologic/topologic.cpython-310-x86_64-linux-gnu.so +0 -0
  50. topologicpy/bin/linux/topologic/topologic.cpython-311-x86_64-linux-gnu.so +0 -0
  51. topologicpy/bin/linux/topologic/topologic.cpython-38-x86_64-linux-gnu.so +0 -0
  52. topologicpy/bin/linux/topologic/topologic.cpython-39-x86_64-linux-gnu.so +0 -0
  53. topologicpy/bin/linux/topologic.libs/libTKBO-6bdf205d.so.7.7.0 +0 -0
  54. topologicpy/bin/linux/topologic.libs/libTKBRep-2960a069.so.7.7.0 +0 -0
  55. topologicpy/bin/linux/topologic.libs/libTKBool-c44b74bd.so.7.7.0 +0 -0
  56. topologicpy/bin/linux/topologic.libs/libTKFillet-9a670ba0.so.7.7.0 +0 -0
  57. topologicpy/bin/linux/topologic.libs/libTKG2d-8f31849e.so.7.7.0 +0 -0
  58. topologicpy/bin/linux/topologic.libs/libTKG3d-4c6bce57.so.7.7.0 +0 -0
  59. topologicpy/bin/linux/topologic.libs/libTKGeomAlgo-26066fd9.so.7.7.0 +0 -0
  60. topologicpy/bin/linux/topologic.libs/libTKGeomBase-2116cabe.so.7.7.0 +0 -0
  61. topologicpy/bin/linux/topologic.libs/libTKMath-72572fa8.so.7.7.0 +0 -0
  62. topologicpy/bin/linux/topologic.libs/libTKMesh-2a060427.so.7.7.0 +0 -0
  63. topologicpy/bin/linux/topologic.libs/libTKOffset-6cab68ff.so.7.7.0 +0 -0
  64. topologicpy/bin/linux/topologic.libs/libTKPrim-eb1262b3.so.7.7.0 +0 -0
  65. topologicpy/bin/linux/topologic.libs/libTKShHealing-e67e5cc7.so.7.7.0 +0 -0
  66. topologicpy/bin/linux/topologic.libs/libTKTopAlgo-e4c96c33.so.7.7.0 +0 -0
  67. topologicpy/bin/linux/topologic.libs/libTKernel-fb7fe3b7.so.7.7.0 +0 -0
  68. topologicpy/bin/linux/topologic.libs/libgcc_s-32c1665e.so.1 +0 -0
  69. topologicpy/bin/linux/topologic.libs/libstdc++-672d7b41.so.6.0.30 +0 -0
  70. topologicpy/bin/macos/topologic/__init__.py +0 -2
  71. topologicpy/bin/windows/topologic/TKBO-f6b191de.dll +0 -0
  72. topologicpy/bin/windows/topologic/TKBRep-e56a600e.dll +0 -0
  73. topologicpy/bin/windows/topologic/TKBool-7b8d47ae.dll +0 -0
  74. topologicpy/bin/windows/topologic/TKFillet-0ddbf0a8.dll +0 -0
  75. topologicpy/bin/windows/topologic/TKG2d-2e2dee3d.dll +0 -0
  76. topologicpy/bin/windows/topologic/TKG3d-6674513d.dll +0 -0
  77. topologicpy/bin/windows/topologic/TKGeomAlgo-d240e370.dll +0 -0
  78. topologicpy/bin/windows/topologic/TKGeomBase-df87aba5.dll +0 -0
  79. topologicpy/bin/windows/topologic/TKMath-45bd625a.dll +0 -0
  80. topologicpy/bin/windows/topologic/TKMesh-d6e826b1.dll +0 -0
  81. topologicpy/bin/windows/topologic/TKOffset-79b9cc94.dll +0 -0
  82. topologicpy/bin/windows/topologic/TKPrim-aa430a86.dll +0 -0
  83. topologicpy/bin/windows/topologic/TKShHealing-bb48be89.dll +0 -0
  84. topologicpy/bin/windows/topologic/TKTopAlgo-7d0d1e22.dll +0 -0
  85. topologicpy/bin/windows/topologic/TKernel-08c8cfbb.dll +0 -0
  86. topologicpy/bin/windows/topologic/__init__.py +0 -2
  87. topologicpy/bin/windows/topologic/topologic.cp310-win_amd64.pyd +0 -0
  88. topologicpy/bin/windows/topologic/topologic.cp311-win_amd64.pyd +0 -0
  89. topologicpy/bin/windows/topologic/topologic.cp38-win_amd64.pyd +0 -0
  90. topologicpy/bin/windows/topologic/topologic.cp39-win_amd64.pyd +0 -0
  91. topologicpy-0.5.9.dist-info/METADATA +0 -86
  92. topologicpy-0.5.9.dist-info/RECORD +0 -91
  93. {topologicpy-0.5.9.dist-info → topologicpy-6.0.0.dist-info}/WHEEL +0 -0
  94. {topologicpy-0.5.9.dist-info → topologicpy-6.0.0.dist-info}/top_level.txt +0 -0
topologicpy/Dictionary.py CHANGED
@@ -1,698 +1,698 @@
1
- # Copyright (C) 2024
2
- # Wassim Jabi <wassim.jabi@gmail.com>
3
- #
4
- # This program is free software: you can redistribute it and/or modify it under
5
- # the terms of the GNU Affero General Public License as published by the Free Software
6
- # Foundation, either version 3 of the License, or (at your option) any later
7
- # version.
8
- #
9
- # This program is distributed in the hope that it will be useful, but WITHOUT
10
- # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
- # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
12
- # details.
13
- #
14
- # You should have received a copy of the GNU Affero General Public License along with
15
- # this program. If not, see <https://www.gnu.org/licenses/>.
16
-
17
- import topologic
18
- from topologic import IntAttribute, DoubleAttribute, StringAttribute, ListAttribute
19
-
20
- class Dictionary(topologic.Dictionary):
21
- '''
22
- @staticmethod
23
- def ByDGLData(item):
24
- """
25
- Parameters
26
- ----------
27
- item : TYPE
28
- DESCRIPTION.
29
-
30
- Returns
31
- -------
32
- dictionaries : TYPE
33
- DESCRIPTION.
34
-
35
- """
36
- keys = list(item.keys())
37
- vList = []
38
- for k in keys:
39
- vList.append(item[k].tolist())
40
- dictionaries = []
41
- for v in range(len(vList[0])):
42
- values = []
43
- for k in range(len(keys)):
44
- value = vList[k][v]
45
- values.append(value)
46
- dictionaries.append(Dictionary.ByKeysValues(keys, values))
47
- return dictionaries
48
- '''
49
- @staticmethod
50
- def ByKeyValue(key, value):
51
- """
52
- Creates a Dictionary from the input key and the input value.
53
-
54
- Parameters
55
- ----------
56
- key : str
57
- The string representing the key of the value in the dictionary.
58
- value : int, float, str, or list
59
- A value corresponding to the input key. A value can be an integer, a float, a string, or a list.
60
-
61
- Returns
62
- -------
63
- topologic.Dictionary
64
- The created dictionary.
65
-
66
- """
67
- if not isinstance(key, str):
68
- print("Dictionary.ByKeyValue - Error: The input key is not a valid string. Returning None.")
69
- return None
70
- return Dictionary.ByKeysValues([key], [value])
71
-
72
-
73
- @staticmethod
74
- def _ConvertValue(value):
75
- """
76
- Converts the input value to the proper attribute
77
- """
78
- from topologicpy.Topology import Topology
79
- import json
80
-
81
- def dict_to_json(py_dict):
82
- """
83
- Convert a Python dictionary to a JSON-formatted string.
84
- """
85
- return json.dumps(py_dict, indent=2)
86
-
87
- attr = topologic.StringAttribute("__NONE__")
88
- if value == None:
89
- attr = topologic.StringAttribute("__NONE__")
90
- elif isinstance(value, bool):
91
- if value == False:
92
- attr = topologic.IntAttribute(0)
93
- else:
94
- attr = topologic.IntAttribute(1)
95
- elif isinstance(value, int):
96
- attr = topologic.IntAttribute(value)
97
- elif isinstance(value, float):
98
- attr = topologic.DoubleAttribute(value)
99
- elif isinstance(value, topologic.Topology):
100
- str_value = Topology.JSONString(value)
101
- attr = topologic.StringAttribute(str_value)
102
- elif isinstance(value, dict):
103
- str_value = dict_to_json(value)
104
- attr = topologic.StringAttribute(str_value)
105
- elif isinstance(value, str):
106
- attr = topologic.StringAttribute(value)
107
- elif isinstance(value, tuple):
108
- l = [Dictionary._ConvertValue(v) for v in list(value)]
109
- attr = topologic.ListAttribute(l)
110
- elif isinstance(value, list):
111
- l = [Dictionary._ConvertValue(v) for v in value]
112
- attr = topologic.ListAttribute(l)
113
- else:
114
- attr = topologic.StringAttribute("__NONE__")
115
- return attr
116
-
117
- @staticmethod
118
- def ByKeysValues(keys, values):
119
- """
120
- Creates a Dictionary from the input list of keys and the input list of values.
121
-
122
- Parameters
123
- ----------
124
- keys : list
125
- A list of strings representing the keys of the dictionary.
126
- values : list
127
- A list of values corresponding to the list of keys. Values can be integers, floats, strings, or lists
128
-
129
- Returns
130
- -------
131
- topologic.Dictionary
132
- The created dictionary.
133
-
134
- """
135
-
136
- if not isinstance(keys, list):
137
- print("Dictionary.ByKeysValues - Error: The input keys parameter is not a valid list. Returning None.")
138
- return None
139
- if not isinstance(values, list):
140
- print("Dictionary.ByKeysValues - Error: The input values parameter is not a valid list. Returning None.")
141
- return None
142
- if len(keys) != len(values):
143
- print("Dictionary.ByKeysValues - Error: The input keys and values parameters are not of equal length. Returning None.")
144
- return None
145
- stl_keys = []
146
- stl_values = []
147
- for i in range(len(keys)):
148
- if isinstance(keys[i], str):
149
- stl_keys.append(keys[i])
150
- else:
151
- stl_keys.append(str(keys[i]))
152
- stl_values.append(Dictionary._ConvertValue(values[i]))
153
- return topologic.Dictionary.ByKeysValues(stl_keys, stl_values)
154
-
155
- @staticmethod
156
- def ByMergedDictionaries(dictionaries):
157
- """
158
- Creates a dictionary by merging the list of input dictionaries.
159
-
160
- Parameters
161
- ----------
162
- dictionaries : list
163
- The input list of dictionaries to be merges.
164
-
165
- Returns
166
- -------
167
- topologic.Dictionary
168
- The created dictionary.
169
-
170
- """
171
- if not isinstance(dictionaries, list):
172
- print("Dictionary.ByMergedDictionaries - Error: The input dictionaries parameter is not a valid list. Returning None.")
173
- return None
174
- new_dictionaries = []
175
- for d in dictionaries:
176
- if isinstance(d, topologic.Dictionary):
177
- new_dictionaries.append(d)
178
- elif isinstance(d, dict):
179
- new_dictionaries.append(Dictionary.ByPythonDictionary(d))
180
- if len(new_dictionaries) == 0:
181
- print("Dictionary.ByMergedDictionaries - Error: The input dictionaries parameter does not contain valid dictionaries. Returning None.")
182
- return None
183
- elif len(new_dictionaries) == 1:
184
- print("Dictionary.ByMergedDictionaries - Warning: The input dictionaries parameter contains only one valid dictionary. Returning that dictionary.")
185
- return new_dictionaries[0]
186
- else:
187
- dictionaries = new_dictionaries
188
- sinkKeys = []
189
- sinkValues = []
190
- d = dictionaries[0]
191
- if d != None:
192
- stlKeys = d.Keys()
193
- if len(stlKeys) > 0:
194
- sinkKeys = Dictionary.Keys(d)
195
- sinkValues = Dictionary.Values(d)
196
- for i in range(1,len(dictionaries)):
197
- d = dictionaries[i]
198
- if d == None:
199
- continue
200
- stlKeys = Dictionary.Keys(d)
201
- if len(stlKeys) > 0:
202
- sourceKeys = Dictionary.Keys(d)
203
- for aSourceKey in sourceKeys:
204
- if aSourceKey not in sinkKeys:
205
- sinkKeys.append(aSourceKey)
206
- sinkValues.append("")
207
- for i in range(len(sourceKeys)):
208
- index = sinkKeys.index(sourceKeys[i])
209
- sourceValue = Dictionary.ValueAtKey(d,sourceKeys[i])
210
- if sourceValue != None:
211
- if sinkValues[index] != "":
212
- if isinstance(sinkValues[index], list):
213
- if not sourceValue in sinkValues[index]:
214
- sinkValues[index].append(sourceValue)
215
- else:
216
- if not sourceValue == sinkValues[index]:
217
- sinkValues[index] = [sinkValues[index], sourceValue]
218
- else:
219
- sinkValues[index] = sourceValue
220
- if len(sinkKeys) > 0 and len(sinkValues) > 0:
221
- newDict = Dictionary.ByKeysValues(sinkKeys, sinkValues)
222
- return newDict
223
- return None
224
- '''
225
- @staticmethod
226
- def ByObjectProperties(bObject, keys, importAll):
227
- """
228
- Parameters
229
- ----------
230
- bObject : TYPE
231
- DESCRIPTION.
232
- keys : TYPE
233
- DESCRIPTION.
234
- importAll : TYPE
235
- DESCRIPTION.
236
-
237
- Raises
238
- ------
239
- Exception
240
- DESCRIPTION.
241
-
242
- Returns
243
- -------
244
- TYPE
245
- DESCRIPTION.
246
-
247
- """
248
- # bObject, keys, importAll = item
249
- dictKeys = []
250
- dictValues = []
251
-
252
- if importAll:
253
- dictKeys.append("Name")
254
- dictValues.append(bObject.name)
255
- dictKeys.append("Color")
256
- dictValues.append([bObject.color[0], bObject.color[1], bObject.color[2], bObject.color[3]])
257
- dictKeys.append("Location")
258
- dictValues.append([bObject.location[0], bObject.location[1], bObject.location[2]])
259
- dictKeys.append("Scale")
260
- dictValues.append([bObject.scale[0], bObject.scale[1], bObject.scale[2]])
261
- dictKeys.append("Rotation")
262
- dictValues.append([bObject.rotation_euler[0], bObject.rotation_euler[1], bObject.rotation_euler[2]])
263
- dictKeys.append("Dimensions")
264
- dictValues.append([bObject.dimensions[0], bObject.dimensions[1], bObject.dimensions[2]])
265
- for k, v in bObject.items():
266
- if isinstance(v, bool) or isinstance(v, int) or isinstance(v, float) or isinstance(v, str):
267
- dictKeys.append(str(k))
268
- dictValues.append(v)
269
- else:
270
- for k in keys:
271
- try:
272
- v = bObject[k]
273
- if v:
274
- if isinstance(v, bool) or isinstance(v, int) or isinstance(v, float) or isinstance(v, str):
275
- dictKeys.append(str(k))
276
- dictValues.append(v)
277
- except:
278
- if k.lower() == "name":
279
- dictKeys.append("Name")
280
- dictValues.append(bObject.name)
281
- elif k.lower() == "color":
282
- dictKeys.append("Color")
283
- dictValues.append([bObject.color[0], bObject.color[1], bObject.color[2], bObject.color[3]])
284
- elif k.lower() == "location":
285
- dictKeys.append("Location")
286
- dictValues.append([bObject.location[0], bObject.location[1], bObject.location[2]])
287
- elif k.lower() == "scale":
288
- dictKeys.append("Scale")
289
- dictValues.append([bObject.scale[0], bObject.scale[1], bObject.scale[2]])
290
- elif k.lower() == "rotation":
291
- dictKeys.append("Rotation")
292
- dictValues.append([bObject.rotation_euler[0], bObject.rotation_euler[1], bObject.rotation_euler[2]])
293
- elif k.lower() == "dimensions":
294
- dictKeys.append("Dimensions")
295
- dictValues.append([bObject.dimensions[0], bObject.dimensions[1], bObject.dimensions[2]])
296
- else:
297
- raise Exception("Dictionary.ByObjectProperties: Key \""+k+"\" does not exist in the properties of object \""+bObject.name+"\".")
298
-
299
- return Dictionary.ByKeysValues(dictKeys, dictValues)
300
- '''
301
-
302
- @staticmethod
303
- def ByPythonDictionary(pythonDictionary):
304
- """
305
- Creates a dictionary equivalent to the input python dictionary.
306
-
307
- Parameters
308
- ----------
309
- pythonDictionary : dict
310
- The input python dictionary.
311
-
312
- Returns
313
- -------
314
- topologic.Dictionary
315
- The dictionary equivalent to the input python dictionary.
316
-
317
- """
318
- if not isinstance(pythonDictionary, dict):
319
- print("Dictionary.ByPythonDictionary - Error: The input dictionary parameter is not a valid python dictionary. Returning None.")
320
- return None
321
- keys = list(pythonDictionary.keys())
322
- values = []
323
- for key in keys:
324
- values.append(pythonDictionary[key])
325
- return Dictionary.ByKeysValues(keys, values)
326
-
327
- @staticmethod
328
- def Keys(dictionary):
329
- """
330
- Returns the keys of the input dictionary.
331
-
332
- Parameters
333
- ----------
334
- dictionary : topologic.Dictionary or dict
335
- The input dictionary.
336
-
337
- Returns
338
- -------
339
- list
340
- The list of keys of the input dictionary.
341
-
342
- """
343
- if not isinstance(dictionary, topologic.Dictionary) and not isinstance(dictionary, dict):
344
- print("Dictionary.Keys - Error: The input dictionary parameter is not a valid topologic or python dictionary. Returning None.")
345
- return None
346
- if isinstance(dictionary, dict):
347
- return list(dictionary.keys())
348
- elif isinstance(dictionary, topologic.Dictionary):
349
- return dictionary.Keys()
350
- else:
351
- return None
352
-
353
- @staticmethod
354
- def ListAttributeValues(listAttribute):
355
- """
356
- Returns the list of values embedded in the input listAttribute.
357
-
358
- Parameters
359
- ----------
360
- listAttribute : listAttribute
361
- The input list attribute.
362
-
363
- Returns
364
- -------
365
- list
366
- The list of values found in the input list attribute
367
-
368
- """
369
- listAttributes = listAttribute.ListValue()
370
- returnList = [Dictionary._ConvertAttribute(attr) for attr in listAttributes]
371
- return returnList
372
-
373
- @staticmethod
374
- def PythonDictionary(dictionary):
375
- """
376
- Returns the input dictionary as a python dictionary
377
-
378
- Parameters
379
- ----------
380
- dictionary : topologic.Dictionary
381
- The input dictionary.
382
-
383
- Returns
384
- -------
385
- dict
386
- The python dictionary equivalent of the input dictionary
387
-
388
- """
389
- if isinstance(dictionary, dict):
390
- print("Dictionary.PythonDictionary - Warning: The input dictionary parameter is already a python dictionary. Returning that dictionary.")
391
- return dictionary
392
- if not isinstance(dictionary, topologic.Dictionary):
393
- print("Dictionary.PythonDictionary - Error: The input dictionary parameter is not a valid topologic dictionary. Returning None.")
394
- return None
395
- keys = dictionary.Keys()
396
- pythonDict = {}
397
- for key in keys:
398
- try:
399
- attr = dictionary.ValueAtKey(key)
400
- except:
401
- raise Exception("Dictionary.Values - Error: Could not retrieve a Value at the specified key ("+key+")")
402
- if isinstance(attr, topologic.IntAttribute):
403
- pythonDict[key] = (attr.IntValue())
404
- elif isinstance(attr, topologic.DoubleAttribute):
405
- pythonDict[key] = (attr.DoubleValue())
406
- elif isinstance(attr, topologic.StringAttribute):
407
- temp_str = attr.StringValue()
408
- if temp_str == "__NONE__":
409
- pythonDict[key] = None
410
- else:
411
- pythonDict[key] = (temp_str)
412
- elif isinstance(attr, topologic.ListAttribute):
413
- pythonDict[key] = (Dictionary.ListAttributeValues(attr))
414
- else:
415
- pythonDict[key]=("")
416
- return pythonDict
417
-
418
- @staticmethod
419
- def RemoveKey(dictionary, key):
420
- """
421
- Removes the key (and its associated value) from the input dictionary.
422
-
423
- Parameters
424
- ----------
425
- dictionary : topologic.Dictionary or dict
426
- The input dictionary.
427
- key : string
428
- The input key.
429
-
430
- Returns
431
- -------
432
- topologic.Dictionary or dict
433
- The input dictionary with the key/value removed from it.
434
-
435
- """
436
- def processPythonDictionary (dictionary, key):
437
- values = []
438
- keys = dictionary.keys()
439
- new_dict = {}
440
- for k in keys:
441
- if not key.lower() == k.lower():
442
- new_dict[key] = dictionary[key]
443
- return new_dict
444
-
445
- def processTopologicDictionary(dictionary, key):
446
- keys = dictionary.Keys()
447
- new_keys = []
448
- new_values = []
449
- for k in keys:
450
- if not key.lower() == k.lower():
451
- new_keys.append(k)
452
- new_values.append(Dictionary.ValueAtKey(dictionary, k))
453
- return Dictionary.ByKeysValues(new_keys, new_values)
454
-
455
- if not isinstance(dictionary, topologic.Dictionary) and not isinstance(dictionary, dict):
456
- print("Dictionary.RemoveKey - Error: The input dictionary parameter is not a valid topologic or python dictionary. Returning None.")
457
- return None
458
- if not isinstance(key, str):
459
- print("Dictionary.RemoveKey - Error: The input key parameter is not a valid string. Returning None.")
460
- return None
461
-
462
- if isinstance(dictionary, dict):
463
- return processPythonDictionary(dictionary, key)
464
- elif isinstance(dictionary, topologic.Dictionary):
465
- return processTopologicDictionary(dictionary, key)
466
- else:
467
- return None
468
-
469
- @staticmethod
470
- def SetValueAtKey(dictionary, key, value):
471
- """
472
- Creates a key/value pair in the input dictionary.
473
-
474
- Parameters
475
- ----------
476
- dictionary : topologic.Dictionary or dict
477
- The input dictionary.
478
- key : string
479
- The input key.
480
- value : int , float , string, or list
481
- The value associated with the key.
482
-
483
- Returns
484
- -------
485
- topologic.Dictionary or dict
486
- The input dictionary with the key/value pair added to it.
487
-
488
- """
489
- def processPythonDictionary (dictionary, key, value):
490
- if value == "__NONE__":
491
- value = None
492
- dictionary[key] = value
493
- return dictionary
494
-
495
- def processTopologicDictionary(dictionary, key, value):
496
- keys = dictionary.Keys()
497
- if not key in keys:
498
- keys.append(key)
499
- values = []
500
- for k in keys:
501
- if k == key:
502
- values.append(value)
503
- else:
504
- values.append(Dictionary.ValueAtKey(dictionary, k))
505
- d = Dictionary.ByKeysValues(keys, values)
506
- return d
507
-
508
- if not isinstance(dictionary, topologic.Dictionary) and not isinstance(dictionary, dict):
509
- print("Dictionary.SetValueAtKey - Error: The input dictionary parameter is not a valid topologic or python dictionary. Returning None.")
510
- return None
511
- if not isinstance(key, str):
512
- print("Dictionary.SetValueAtKey - Error: The input key parameter is not a valid string. Returning None.")
513
- return None
514
- if value == None:
515
- value = "__NONE__"
516
- if isinstance(dictionary, dict):
517
- return processPythonDictionary(dictionary, key, value)
518
- elif isinstance(dictionary, topologic.Dictionary):
519
- return processTopologicDictionary(dictionary, key, value)
520
- else:
521
- return None
522
-
523
- @staticmethod
524
- def _ConvertAttribute(attr):
525
- """
526
- Convert the found attribute into the proper value
527
- """
528
- from topologicpy.Topology import Topology
529
- import json
530
-
531
- def is_json_string(input_string):
532
- """
533
- Check if the input string is a valid JSON string.
534
- """
535
- try:
536
- json.loads(input_string)
537
- return True
538
- except json.JSONDecodeError:
539
- return False
540
-
541
- def json_to_dict(json_string):
542
- """
543
- Convert a JSON-formatted string to a Python dictionary.
544
- """
545
- return json.loads(json_string)
546
-
547
- if isinstance(attr, IntAttribute):
548
- return (attr.IntValue())
549
- elif isinstance(attr, DoubleAttribute):
550
- return (attr.DoubleValue())
551
- elif isinstance(attr, StringAttribute):
552
- temp_value = attr.StringValue()
553
- topologies = None
554
- try:
555
- topologies = Topology.ByJSONString(temp_value, progressBar=False)
556
- except:
557
- topologies = None
558
- if isinstance(topologies, list):
559
- if len(topologies) == 0:
560
- topologies = None
561
- if temp_value == "__NONE__":
562
- return None
563
- elif isinstance(topologies, topologic.Topology):
564
- return topologies
565
- elif isinstance(topologies, list):
566
- if len(topologies) > 1:
567
- return topologies
568
- elif len(topologies) == 1:
569
- return topologies[0]
570
- elif is_json_string(temp_value):
571
- ret_value = json_to_dict(temp_value)
572
- return ret_value
573
- else:
574
- return (temp_value)
575
- elif isinstance(attr, ListAttribute):
576
- return (Dictionary.ListAttributeValues(attr))
577
- elif isinstance(attr, float) or isinstance(attr, int):
578
- return attr
579
- elif isinstance(attr, str):
580
- topologies = Topology.ByJSONString(attr)
581
- if attr == "__NONE__":
582
- return None
583
- elif len(topologies) > 1:
584
- return topologies
585
- elif len(topologies) == 1:
586
- return topologies[0]
587
- elif is_json_string(attr):
588
- return json_to_dict(attr)
589
- else:
590
- return (attr)
591
- elif isinstance(attr, tuple):
592
- return Dictionary.ListAttributeValues([Dictionary._ConvertAttribute(x) for x in list(attr)])
593
- elif isinstance(attr, list):
594
- return Dictionary.ListAttributeValues([Dictionary._ConvertAttribute(x) for x in attr])
595
- elif isinstance(attr, dict):
596
- return attr
597
- else:
598
- return None
599
-
600
- @staticmethod
601
- def ValueAtKey(dictionary, key):
602
- """
603
- Returns the value of the input key in the input dictionary.
604
-
605
- Parameters
606
- ----------
607
- dictionary : topologic.Dictionary or dict
608
- The input dictionary.
609
- key : string
610
- The input key.
611
-
612
- Returns
613
- -------
614
- int , float, string, list , or dict
615
- The value found at the input key in the input dictionary.
616
-
617
- """
618
- import json
619
- from topologicpy.Topology import Topology
620
-
621
-
622
- if not isinstance(dictionary, topologic.Dictionary) and not isinstance(dictionary, dict):
623
- print("Dictionary.ValueAtKey - Error: The input dictionary parameter is not a valid topologic or python dictionary. Returning None.")
624
- return None
625
- if not isinstance(key, str):
626
- print("Dictionary.ValueAtKey - Error: The input key parameter is not a valid str. Returning None.")
627
- return None
628
- if isinstance(dictionary, dict):
629
- attr = dictionary[key]
630
- elif isinstance(dictionary, topologic.Dictionary):
631
- attr = dictionary.ValueAtKey(key)
632
- else:
633
- return None
634
- return_value = Dictionary._ConvertAttribute(attr)
635
- return return_value
636
-
637
- @staticmethod
638
- def Values(dictionary):
639
- """
640
- Returns the list of values in the input dictionary.
641
-
642
- Parameters
643
- ----------
644
- dictionary : topologic.Dictionary or dict
645
- The input dictionary.
646
-
647
- Returns
648
- -------
649
- list
650
- The list of values found in the input dictionary.
651
-
652
- """
653
- if not isinstance(dictionary, topologic.Dictionary) and not isinstance(dictionary, dict):
654
- print("Dictionary.Values - Error: The input dictionary parameter is not a valid topologic or python dictionary. Returning None.")
655
- return None
656
- keys = None
657
- if isinstance(dictionary, dict):
658
- keys = dictionary.keys()
659
- elif isinstance(dictionary, topologic.Dictionary):
660
- keys = dictionary.Keys()
661
- returnList = []
662
- if not keys:
663
- return None
664
- for key in keys:
665
- try:
666
- if isinstance(dictionary, dict):
667
- attr = dictionary[key]
668
- elif isinstance(dictionary, topologic.Dictionary):
669
- attr = Dictionary.ValueAtKey(dictionary,key)
670
- else:
671
- attr = None
672
- except:
673
- return None
674
- returnList.append(attr)
675
- '''
676
- if isinstance(attr, topologic.IntAttribute):
677
- returnList.append(attr.IntValue())
678
- elif isinstance(attr, topologic.DoubleAttribute):
679
- returnList.append(attr.DoubleValue())
680
- elif isinstance(attr, topologic.StringAttribute):
681
- temp_attr = attr.StringValue()
682
- if temp_attr == "__NONE__":
683
- returnList.append(None)
684
- else:
685
- returnList.append(attr.StringValue())
686
- elif isinstance(attr, topologic.ListAttribute):
687
- returnList.append(Dictionary.ListAttributeValues(attr))
688
- elif isinstance(attr, float) or isinstance(attr, int) or isinstance(attr, str):
689
- returnList.append(attr)
690
- elif isinstance(attr, list):
691
- returnList.append(Dictionary.ListAttributeValues(attr))
692
- else:
693
- returnList.append("")
694
- '''
695
- return returnList
696
-
697
-
698
-
1
+ # Copyright (C) 2024
2
+ # Wassim Jabi <wassim.jabi@gmail.com>
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify it under
5
+ # the terms of the GNU Affero General Public License as published by the Free Software
6
+ # Foundation, either version 3 of the License, or (at your option) any later
7
+ # version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful, but WITHOUT
10
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
12
+ # details.
13
+ #
14
+ # You should have received a copy of the GNU Affero General Public License along with
15
+ # this program. If not, see <https://www.gnu.org/licenses/>.
16
+
17
+ import topologic_core as topologic
18
+ from topologic_core import IntAttribute, DoubleAttribute, StringAttribute, ListAttribute
19
+
20
+ class Dictionary(topologic.Dictionary):
21
+ '''
22
+ @staticmethod
23
+ def ByDGLData(item):
24
+ """
25
+ Parameters
26
+ ----------
27
+ item : TYPE
28
+ DESCRIPTION.
29
+
30
+ Returns
31
+ -------
32
+ dictionaries : TYPE
33
+ DESCRIPTION.
34
+
35
+ """
36
+ keys = list(item.keys())
37
+ vList = []
38
+ for k in keys:
39
+ vList.append(item[k].tolist())
40
+ dictionaries = []
41
+ for v in range(len(vList[0])):
42
+ values = []
43
+ for k in range(len(keys)):
44
+ value = vList[k][v]
45
+ values.append(value)
46
+ dictionaries.append(Dictionary.ByKeysValues(keys, values))
47
+ return dictionaries
48
+ '''
49
+ @staticmethod
50
+ def ByKeyValue(key, value):
51
+ """
52
+ Creates a Dictionary from the input key and the input value.
53
+
54
+ Parameters
55
+ ----------
56
+ key : str
57
+ The string representing the key of the value in the dictionary.
58
+ value : int, float, str, or list
59
+ A value corresponding to the input key. A value can be an integer, a float, a string, or a list.
60
+
61
+ Returns
62
+ -------
63
+ topologic.Dictionary
64
+ The created dictionary.
65
+
66
+ """
67
+ if not isinstance(key, str):
68
+ print("Dictionary.ByKeyValue - Error: The input key is not a valid string. Returning None.")
69
+ return None
70
+ return Dictionary.ByKeysValues([key], [value])
71
+
72
+
73
+ @staticmethod
74
+ def _ConvertValue(value):
75
+ """
76
+ Converts the input value to the proper attribute
77
+ """
78
+ from topologicpy.Topology import Topology
79
+ import json
80
+
81
+ def dict_to_json(py_dict):
82
+ """
83
+ Convert a Python dictionary to a JSON-formatted string.
84
+ """
85
+ return json.dumps(py_dict, indent=2)
86
+
87
+ attr = topologic.StringAttribute("__NONE__")
88
+ if value == None:
89
+ attr = topologic.StringAttribute("__NONE__")
90
+ elif isinstance(value, bool):
91
+ if value == False:
92
+ attr = topologic.IntAttribute(0)
93
+ else:
94
+ attr = topologic.IntAttribute(1)
95
+ elif isinstance(value, int):
96
+ attr = topologic.IntAttribute(value)
97
+ elif isinstance(value, float):
98
+ attr = topologic.DoubleAttribute(value)
99
+ elif isinstance(value, topologic.Topology):
100
+ str_value = Topology.JSONString(value)
101
+ attr = topologic.StringAttribute(str_value)
102
+ elif isinstance(value, dict):
103
+ str_value = dict_to_json(value)
104
+ attr = topologic.StringAttribute(str_value)
105
+ elif isinstance(value, str):
106
+ attr = topologic.StringAttribute(value)
107
+ elif isinstance(value, tuple):
108
+ l = [Dictionary._ConvertValue(v) for v in list(value)]
109
+ attr = topologic.ListAttribute(l)
110
+ elif isinstance(value, list):
111
+ l = [Dictionary._ConvertValue(v) for v in value]
112
+ attr = topologic.ListAttribute(l)
113
+ else:
114
+ attr = topologic.StringAttribute("__NONE__")
115
+ return attr
116
+
117
+ @staticmethod
118
+ def ByKeysValues(keys, values):
119
+ """
120
+ Creates a Dictionary from the input list of keys and the input list of values.
121
+
122
+ Parameters
123
+ ----------
124
+ keys : list
125
+ A list of strings representing the keys of the dictionary.
126
+ values : list
127
+ A list of values corresponding to the list of keys. Values can be integers, floats, strings, or lists
128
+
129
+ Returns
130
+ -------
131
+ topologic.Dictionary
132
+ The created dictionary.
133
+
134
+ """
135
+
136
+ if not isinstance(keys, list):
137
+ print("Dictionary.ByKeysValues - Error: The input keys parameter is not a valid list. Returning None.")
138
+ return None
139
+ if not isinstance(values, list):
140
+ print("Dictionary.ByKeysValues - Error: The input values parameter is not a valid list. Returning None.")
141
+ return None
142
+ if len(keys) != len(values):
143
+ print("Dictionary.ByKeysValues - Error: The input keys and values parameters are not of equal length. Returning None.")
144
+ return None
145
+ stl_keys = []
146
+ stl_values = []
147
+ for i in range(len(keys)):
148
+ if isinstance(keys[i], str):
149
+ stl_keys.append(keys[i])
150
+ else:
151
+ stl_keys.append(str(keys[i]))
152
+ stl_values.append(Dictionary._ConvertValue(values[i]))
153
+ return topologic.Dictionary.ByKeysValues(stl_keys, stl_values)
154
+
155
+ @staticmethod
156
+ def ByMergedDictionaries(dictionaries):
157
+ """
158
+ Creates a dictionary by merging the list of input dictionaries.
159
+
160
+ Parameters
161
+ ----------
162
+ dictionaries : list
163
+ The input list of dictionaries to be merges.
164
+
165
+ Returns
166
+ -------
167
+ topologic.Dictionary
168
+ The created dictionary.
169
+
170
+ """
171
+ if not isinstance(dictionaries, list):
172
+ print("Dictionary.ByMergedDictionaries - Error: The input dictionaries parameter is not a valid list. Returning None.")
173
+ return None
174
+ new_dictionaries = []
175
+ for d in dictionaries:
176
+ if isinstance(d, topologic.Dictionary):
177
+ new_dictionaries.append(d)
178
+ elif isinstance(d, dict):
179
+ new_dictionaries.append(Dictionary.ByPythonDictionary(d))
180
+ if len(new_dictionaries) == 0:
181
+ print("Dictionary.ByMergedDictionaries - Error: The input dictionaries parameter does not contain valid dictionaries. Returning None.")
182
+ return None
183
+ elif len(new_dictionaries) == 1:
184
+ print("Dictionary.ByMergedDictionaries - Warning: The input dictionaries parameter contains only one valid dictionary. Returning that dictionary.")
185
+ return new_dictionaries[0]
186
+ else:
187
+ dictionaries = new_dictionaries
188
+ sinkKeys = []
189
+ sinkValues = []
190
+ d = dictionaries[0]
191
+ if d != None:
192
+ stlKeys = d.Keys()
193
+ if len(stlKeys) > 0:
194
+ sinkKeys = Dictionary.Keys(d)
195
+ sinkValues = Dictionary.Values(d)
196
+ for i in range(1,len(dictionaries)):
197
+ d = dictionaries[i]
198
+ if d == None:
199
+ continue
200
+ stlKeys = Dictionary.Keys(d)
201
+ if len(stlKeys) > 0:
202
+ sourceKeys = Dictionary.Keys(d)
203
+ for aSourceKey in sourceKeys:
204
+ if aSourceKey not in sinkKeys:
205
+ sinkKeys.append(aSourceKey)
206
+ sinkValues.append("")
207
+ for i in range(len(sourceKeys)):
208
+ index = sinkKeys.index(sourceKeys[i])
209
+ sourceValue = Dictionary.ValueAtKey(d,sourceKeys[i])
210
+ if sourceValue != None:
211
+ if sinkValues[index] != "":
212
+ if isinstance(sinkValues[index], list):
213
+ if not sourceValue in sinkValues[index]:
214
+ sinkValues[index].append(sourceValue)
215
+ else:
216
+ if not sourceValue == sinkValues[index]:
217
+ sinkValues[index] = [sinkValues[index], sourceValue]
218
+ else:
219
+ sinkValues[index] = sourceValue
220
+ if len(sinkKeys) > 0 and len(sinkValues) > 0:
221
+ newDict = Dictionary.ByKeysValues(sinkKeys, sinkValues)
222
+ return newDict
223
+ return None
224
+ '''
225
+ @staticmethod
226
+ def ByObjectProperties(bObject, keys, importAll):
227
+ """
228
+ Parameters
229
+ ----------
230
+ bObject : TYPE
231
+ DESCRIPTION.
232
+ keys : TYPE
233
+ DESCRIPTION.
234
+ importAll : TYPE
235
+ DESCRIPTION.
236
+
237
+ Raises
238
+ ------
239
+ Exception
240
+ DESCRIPTION.
241
+
242
+ Returns
243
+ -------
244
+ TYPE
245
+ DESCRIPTION.
246
+
247
+ """
248
+ # bObject, keys, importAll = item
249
+ dictKeys = []
250
+ dictValues = []
251
+
252
+ if importAll:
253
+ dictKeys.append("Name")
254
+ dictValues.append(bObject.name)
255
+ dictKeys.append("Color")
256
+ dictValues.append([bObject.color[0], bObject.color[1], bObject.color[2], bObject.color[3]])
257
+ dictKeys.append("Location")
258
+ dictValues.append([bObject.location[0], bObject.location[1], bObject.location[2]])
259
+ dictKeys.append("Scale")
260
+ dictValues.append([bObject.scale[0], bObject.scale[1], bObject.scale[2]])
261
+ dictKeys.append("Rotation")
262
+ dictValues.append([bObject.rotation_euler[0], bObject.rotation_euler[1], bObject.rotation_euler[2]])
263
+ dictKeys.append("Dimensions")
264
+ dictValues.append([bObject.dimensions[0], bObject.dimensions[1], bObject.dimensions[2]])
265
+ for k, v in bObject.items():
266
+ if isinstance(v, bool) or isinstance(v, int) or isinstance(v, float) or isinstance(v, str):
267
+ dictKeys.append(str(k))
268
+ dictValues.append(v)
269
+ else:
270
+ for k in keys:
271
+ try:
272
+ v = bObject[k]
273
+ if v:
274
+ if isinstance(v, bool) or isinstance(v, int) or isinstance(v, float) or isinstance(v, str):
275
+ dictKeys.append(str(k))
276
+ dictValues.append(v)
277
+ except:
278
+ if k.lower() == "name":
279
+ dictKeys.append("Name")
280
+ dictValues.append(bObject.name)
281
+ elif k.lower() == "color":
282
+ dictKeys.append("Color")
283
+ dictValues.append([bObject.color[0], bObject.color[1], bObject.color[2], bObject.color[3]])
284
+ elif k.lower() == "location":
285
+ dictKeys.append("Location")
286
+ dictValues.append([bObject.location[0], bObject.location[1], bObject.location[2]])
287
+ elif k.lower() == "scale":
288
+ dictKeys.append("Scale")
289
+ dictValues.append([bObject.scale[0], bObject.scale[1], bObject.scale[2]])
290
+ elif k.lower() == "rotation":
291
+ dictKeys.append("Rotation")
292
+ dictValues.append([bObject.rotation_euler[0], bObject.rotation_euler[1], bObject.rotation_euler[2]])
293
+ elif k.lower() == "dimensions":
294
+ dictKeys.append("Dimensions")
295
+ dictValues.append([bObject.dimensions[0], bObject.dimensions[1], bObject.dimensions[2]])
296
+ else:
297
+ raise Exception("Dictionary.ByObjectProperties: Key \""+k+"\" does not exist in the properties of object \""+bObject.name+"\".")
298
+
299
+ return Dictionary.ByKeysValues(dictKeys, dictValues)
300
+ '''
301
+
302
+ @staticmethod
303
+ def ByPythonDictionary(pythonDictionary):
304
+ """
305
+ Creates a dictionary equivalent to the input python dictionary.
306
+
307
+ Parameters
308
+ ----------
309
+ pythonDictionary : dict
310
+ The input python dictionary.
311
+
312
+ Returns
313
+ -------
314
+ topologic.Dictionary
315
+ The dictionary equivalent to the input python dictionary.
316
+
317
+ """
318
+ if not isinstance(pythonDictionary, dict):
319
+ print("Dictionary.ByPythonDictionary - Error: The input dictionary parameter is not a valid python dictionary. Returning None.")
320
+ return None
321
+ keys = list(pythonDictionary.keys())
322
+ values = []
323
+ for key in keys:
324
+ values.append(pythonDictionary[key])
325
+ return Dictionary.ByKeysValues(keys, values)
326
+
327
+ @staticmethod
328
+ def Keys(dictionary):
329
+ """
330
+ Returns the keys of the input dictionary.
331
+
332
+ Parameters
333
+ ----------
334
+ dictionary : topologic.Dictionary or dict
335
+ The input dictionary.
336
+
337
+ Returns
338
+ -------
339
+ list
340
+ The list of keys of the input dictionary.
341
+
342
+ """
343
+ if not isinstance(dictionary, topologic.Dictionary) and not isinstance(dictionary, dict):
344
+ print("Dictionary.Keys - Error: The input dictionary parameter is not a valid topologic or python dictionary. Returning None.")
345
+ return None
346
+ if isinstance(dictionary, dict):
347
+ return list(dictionary.keys())
348
+ elif isinstance(dictionary, topologic.Dictionary):
349
+ return dictionary.Keys()
350
+ else:
351
+ return None
352
+
353
+ @staticmethod
354
+ def ListAttributeValues(listAttribute):
355
+ """
356
+ Returns the list of values embedded in the input listAttribute.
357
+
358
+ Parameters
359
+ ----------
360
+ listAttribute : listAttribute
361
+ The input list attribute.
362
+
363
+ Returns
364
+ -------
365
+ list
366
+ The list of values found in the input list attribute
367
+
368
+ """
369
+ listAttributes = listAttribute.ListValue()
370
+ returnList = [Dictionary._ConvertAttribute(attr) for attr in listAttributes]
371
+ return returnList
372
+
373
+ @staticmethod
374
+ def PythonDictionary(dictionary):
375
+ """
376
+ Returns the input dictionary as a python dictionary
377
+
378
+ Parameters
379
+ ----------
380
+ dictionary : topologic.Dictionary
381
+ The input dictionary.
382
+
383
+ Returns
384
+ -------
385
+ dict
386
+ The python dictionary equivalent of the input dictionary
387
+
388
+ """
389
+ if isinstance(dictionary, dict):
390
+ print("Dictionary.PythonDictionary - Warning: The input dictionary parameter is already a python dictionary. Returning that dictionary.")
391
+ return dictionary
392
+ if not isinstance(dictionary, topologic.Dictionary):
393
+ print("Dictionary.PythonDictionary - Error: The input dictionary parameter is not a valid topologic dictionary. Returning None.")
394
+ return None
395
+ keys = dictionary.Keys()
396
+ pythonDict = {}
397
+ for key in keys:
398
+ try:
399
+ attr = dictionary.ValueAtKey(key)
400
+ except:
401
+ raise Exception("Dictionary.Values - Error: Could not retrieve a Value at the specified key ("+key+")")
402
+ if isinstance(attr, topologic.IntAttribute):
403
+ pythonDict[key] = (attr.IntValue())
404
+ elif isinstance(attr, topologic.DoubleAttribute):
405
+ pythonDict[key] = (attr.DoubleValue())
406
+ elif isinstance(attr, topologic.StringAttribute):
407
+ temp_str = attr.StringValue()
408
+ if temp_str == "__NONE__":
409
+ pythonDict[key] = None
410
+ else:
411
+ pythonDict[key] = (temp_str)
412
+ elif isinstance(attr, topologic.ListAttribute):
413
+ pythonDict[key] = (Dictionary.ListAttributeValues(attr))
414
+ else:
415
+ pythonDict[key]=("")
416
+ return pythonDict
417
+
418
+ @staticmethod
419
+ def RemoveKey(dictionary, key):
420
+ """
421
+ Removes the key (and its associated value) from the input dictionary.
422
+
423
+ Parameters
424
+ ----------
425
+ dictionary : topologic.Dictionary or dict
426
+ The input dictionary.
427
+ key : string
428
+ The input key.
429
+
430
+ Returns
431
+ -------
432
+ topologic.Dictionary or dict
433
+ The input dictionary with the key/value removed from it.
434
+
435
+ """
436
+ def processPythonDictionary (dictionary, key):
437
+ values = []
438
+ keys = dictionary.keys()
439
+ new_dict = {}
440
+ for k in keys:
441
+ if not key.lower() == k.lower():
442
+ new_dict[key] = dictionary[key]
443
+ return new_dict
444
+
445
+ def processTopologicDictionary(dictionary, key):
446
+ keys = dictionary.Keys()
447
+ new_keys = []
448
+ new_values = []
449
+ for k in keys:
450
+ if not key.lower() == k.lower():
451
+ new_keys.append(k)
452
+ new_values.append(Dictionary.ValueAtKey(dictionary, k))
453
+ return Dictionary.ByKeysValues(new_keys, new_values)
454
+
455
+ if not isinstance(dictionary, topologic.Dictionary) and not isinstance(dictionary, dict):
456
+ print("Dictionary.RemoveKey - Error: The input dictionary parameter is not a valid topologic or python dictionary. Returning None.")
457
+ return None
458
+ if not isinstance(key, str):
459
+ print("Dictionary.RemoveKey - Error: The input key parameter is not a valid string. Returning None.")
460
+ return None
461
+
462
+ if isinstance(dictionary, dict):
463
+ return processPythonDictionary(dictionary, key)
464
+ elif isinstance(dictionary, topologic.Dictionary):
465
+ return processTopologicDictionary(dictionary, key)
466
+ else:
467
+ return None
468
+
469
+ @staticmethod
470
+ def SetValueAtKey(dictionary, key, value):
471
+ """
472
+ Creates a key/value pair in the input dictionary.
473
+
474
+ Parameters
475
+ ----------
476
+ dictionary : topologic.Dictionary or dict
477
+ The input dictionary.
478
+ key : string
479
+ The input key.
480
+ value : int , float , string, or list
481
+ The value associated with the key.
482
+
483
+ Returns
484
+ -------
485
+ topologic.Dictionary or dict
486
+ The input dictionary with the key/value pair added to it.
487
+
488
+ """
489
+ def processPythonDictionary (dictionary, key, value):
490
+ if value == "__NONE__":
491
+ value = None
492
+ dictionary[key] = value
493
+ return dictionary
494
+
495
+ def processTopologicDictionary(dictionary, key, value):
496
+ keys = dictionary.Keys()
497
+ if not key in keys:
498
+ keys.append(key)
499
+ values = []
500
+ for k in keys:
501
+ if k == key:
502
+ values.append(value)
503
+ else:
504
+ values.append(Dictionary.ValueAtKey(dictionary, k))
505
+ d = Dictionary.ByKeysValues(keys, values)
506
+ return d
507
+
508
+ if not isinstance(dictionary, topologic.Dictionary) and not isinstance(dictionary, dict):
509
+ print("Dictionary.SetValueAtKey - Error: The input dictionary parameter is not a valid topologic or python dictionary. Returning None.")
510
+ return None
511
+ if not isinstance(key, str):
512
+ print("Dictionary.SetValueAtKey - Error: The input key parameter is not a valid string. Returning None.")
513
+ return None
514
+ if value == None:
515
+ value = "__NONE__"
516
+ if isinstance(dictionary, dict):
517
+ return processPythonDictionary(dictionary, key, value)
518
+ elif isinstance(dictionary, topologic.Dictionary):
519
+ return processTopologicDictionary(dictionary, key, value)
520
+ else:
521
+ return None
522
+
523
+ @staticmethod
524
+ def _ConvertAttribute(attr):
525
+ """
526
+ Convert the found attribute into the proper value
527
+ """
528
+ from topologicpy.Topology import Topology
529
+ import json
530
+
531
+ def is_json_string(input_string):
532
+ """
533
+ Check if the input string is a valid JSON string.
534
+ """
535
+ try:
536
+ json.loads(input_string)
537
+ return True
538
+ except json.JSONDecodeError:
539
+ return False
540
+
541
+ def json_to_dict(json_string):
542
+ """
543
+ Convert a JSON-formatted string to a Python dictionary.
544
+ """
545
+ return json.loads(json_string)
546
+
547
+ if isinstance(attr, IntAttribute):
548
+ return (attr.IntValue())
549
+ elif isinstance(attr, DoubleAttribute):
550
+ return (attr.DoubleValue())
551
+ elif isinstance(attr, StringAttribute):
552
+ temp_value = attr.StringValue()
553
+ topologies = None
554
+ try:
555
+ topologies = Topology.ByJSONString(temp_value, progressBar=False)
556
+ except:
557
+ topologies = None
558
+ if isinstance(topologies, list):
559
+ if len(topologies) == 0:
560
+ topologies = None
561
+ if temp_value == "__NONE__":
562
+ return None
563
+ elif isinstance(topologies, topologic.Topology):
564
+ return topologies
565
+ elif isinstance(topologies, list):
566
+ if len(topologies) > 1:
567
+ return topologies
568
+ elif len(topologies) == 1:
569
+ return topologies[0]
570
+ elif is_json_string(temp_value):
571
+ ret_value = json_to_dict(temp_value)
572
+ return ret_value
573
+ else:
574
+ return (temp_value)
575
+ elif isinstance(attr, ListAttribute):
576
+ return (Dictionary.ListAttributeValues(attr))
577
+ elif isinstance(attr, float) or isinstance(attr, int):
578
+ return attr
579
+ elif isinstance(attr, str):
580
+ topologies = Topology.ByJSONString(attr)
581
+ if attr == "__NONE__":
582
+ return None
583
+ elif len(topologies) > 1:
584
+ return topologies
585
+ elif len(topologies) == 1:
586
+ return topologies[0]
587
+ elif is_json_string(attr):
588
+ return json_to_dict(attr)
589
+ else:
590
+ return (attr)
591
+ elif isinstance(attr, tuple):
592
+ return Dictionary.ListAttributeValues([Dictionary._ConvertAttribute(x) for x in list(attr)])
593
+ elif isinstance(attr, list):
594
+ return Dictionary.ListAttributeValues([Dictionary._ConvertAttribute(x) for x in attr])
595
+ elif isinstance(attr, dict):
596
+ return attr
597
+ else:
598
+ return None
599
+
600
+ @staticmethod
601
+ def ValueAtKey(dictionary, key):
602
+ """
603
+ Returns the value of the input key in the input dictionary.
604
+
605
+ Parameters
606
+ ----------
607
+ dictionary : topologic.Dictionary or dict
608
+ The input dictionary.
609
+ key : string
610
+ The input key.
611
+
612
+ Returns
613
+ -------
614
+ int , float, string, list , or dict
615
+ The value found at the input key in the input dictionary.
616
+
617
+ """
618
+ import json
619
+ from topologicpy.Topology import Topology
620
+
621
+
622
+ if not isinstance(dictionary, topologic.Dictionary) and not isinstance(dictionary, dict):
623
+ print("Dictionary.ValueAtKey - Error: The input dictionary parameter is not a valid topologic or python dictionary. Returning None.")
624
+ return None
625
+ if not isinstance(key, str):
626
+ print("Dictionary.ValueAtKey - Error: The input key parameter is not a valid str. Returning None.")
627
+ return None
628
+ if isinstance(dictionary, dict):
629
+ attr = dictionary[key]
630
+ elif isinstance(dictionary, topologic.Dictionary):
631
+ attr = dictionary.ValueAtKey(key)
632
+ else:
633
+ return None
634
+ return_value = Dictionary._ConvertAttribute(attr)
635
+ return return_value
636
+
637
+ @staticmethod
638
+ def Values(dictionary):
639
+ """
640
+ Returns the list of values in the input dictionary.
641
+
642
+ Parameters
643
+ ----------
644
+ dictionary : topologic.Dictionary or dict
645
+ The input dictionary.
646
+
647
+ Returns
648
+ -------
649
+ list
650
+ The list of values found in the input dictionary.
651
+
652
+ """
653
+ if not isinstance(dictionary, topologic.Dictionary) and not isinstance(dictionary, dict):
654
+ print("Dictionary.Values - Error: The input dictionary parameter is not a valid topologic or python dictionary. Returning None.")
655
+ return None
656
+ keys = None
657
+ if isinstance(dictionary, dict):
658
+ keys = dictionary.keys()
659
+ elif isinstance(dictionary, topologic.Dictionary):
660
+ keys = dictionary.Keys()
661
+ returnList = []
662
+ if not keys:
663
+ return None
664
+ for key in keys:
665
+ try:
666
+ if isinstance(dictionary, dict):
667
+ attr = dictionary[key]
668
+ elif isinstance(dictionary, topologic.Dictionary):
669
+ attr = Dictionary.ValueAtKey(dictionary,key)
670
+ else:
671
+ attr = None
672
+ except:
673
+ return None
674
+ returnList.append(attr)
675
+ '''
676
+ if isinstance(attr, topologic.IntAttribute):
677
+ returnList.append(attr.IntValue())
678
+ elif isinstance(attr, topologic.DoubleAttribute):
679
+ returnList.append(attr.DoubleValue())
680
+ elif isinstance(attr, topologic.StringAttribute):
681
+ temp_attr = attr.StringValue()
682
+ if temp_attr == "__NONE__":
683
+ returnList.append(None)
684
+ else:
685
+ returnList.append(attr.StringValue())
686
+ elif isinstance(attr, topologic.ListAttribute):
687
+ returnList.append(Dictionary.ListAttributeValues(attr))
688
+ elif isinstance(attr, float) or isinstance(attr, int) or isinstance(attr, str):
689
+ returnList.append(attr)
690
+ elif isinstance(attr, list):
691
+ returnList.append(Dictionary.ListAttributeValues(attr))
692
+ else:
693
+ returnList.append("")
694
+ '''
695
+ return returnList
696
+
697
+
698
+