1
0

utils.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. from taipy import Scope
  12. from taipy.core.data.data_node import DataNode
  13. from taipy.core.data.in_memory import InMemoryDataNode
  14. class FakeDataNode(InMemoryDataNode):
  15. read_has_been_called = 0
  16. write_has_been_called = 0
  17. def __init__(self, config_id, **kwargs):
  18. scope = kwargs.pop("scope", Scope.SCENARIO)
  19. super().__init__(config_id=config_id, scope=scope, **kwargs)
  20. def _read(self, query=None):
  21. self.read_has_been_called += 1
  22. def _write(self, data):
  23. self.write_has_been_called += 1
  24. @classmethod
  25. def storage_type(cls) -> str:
  26. return "fake_inmemory"
  27. write = DataNode.write # Make sure that the writing behavior comes from DataNode
  28. class FakeDataframeDataNode(DataNode):
  29. COLUMN_NAME_1 = "a"
  30. COLUMN_NAME_2 = "b"
  31. def __init__(self, config_id, default_data_frame, **kwargs):
  32. super().__init__(config_id, **kwargs)
  33. self.data = default_data_frame
  34. def _read(self):
  35. return self.data
  36. @classmethod
  37. def storage_type(cls) -> str:
  38. return "fake_df_dn"
  39. class FakeNumpyarrayDataNode(DataNode):
  40. def __init__(self, config_id, default_array, **kwargs):
  41. super().__init__(config_id, **kwargs)
  42. self.data = default_array
  43. def _read(self):
  44. return self.data
  45. @classmethod
  46. def storage_type(cls) -> str:
  47. return "fake_np_dn"
  48. class FakeListDataNode(DataNode):
  49. class Row:
  50. def __init__(self, value):
  51. self.value = value
  52. def __init__(self, config_id, **kwargs):
  53. super().__init__(config_id, **kwargs)
  54. self.data = [self.Row(i) for i in range(10)]
  55. def _read(self):
  56. return self.data
  57. @classmethod
  58. def storage_type(cls) -> str:
  59. return "fake_list_dn"
  60. class CustomClass:
  61. def __init__(self, a, b):
  62. self.a = a
  63. self.b = b
  64. class FakeCustomDataNode(DataNode):
  65. def __init__(self, config_id, **kwargs):
  66. super().__init__(config_id, **kwargs)
  67. self.data = [CustomClass(i, i * 2) for i in range(10)]
  68. def _read(self):
  69. return self.data
  70. class FakeMultiSheetExcelDataFrameDataNode(DataNode):
  71. def __init__(self, config_id, default_data_frame, **kwargs):
  72. super().__init__(config_id, **kwargs)
  73. self.data = {
  74. "Sheet1": default_data_frame,
  75. "Sheet2": default_data_frame,
  76. }
  77. def _read(self):
  78. return self.data
  79. class FakeMultiSheetExcelCustomDataNode(DataNode):
  80. def __init__(self, config_id, **kwargs):
  81. super().__init__(config_id, **kwargs)
  82. self.data = {
  83. "Sheet1": [CustomClass(i, i * 2) for i in range(10)],
  84. "Sheet2": [CustomClass(i, i * 2) for i in range(10)],
  85. }
  86. def _read(self):
  87. return self.data