unaiverse 0.1.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.

Potentially problematic release.


This version of unaiverse might be problematic. Click here for more details.

Files changed (45) hide show
  1. unaiverse/__init__.py +19 -0
  2. unaiverse/agent.py +2008 -0
  3. unaiverse/agent_basics.py +1844 -0
  4. unaiverse/clock.py +186 -0
  5. unaiverse/dataprops.py +1209 -0
  6. unaiverse/hsm.py +1880 -0
  7. unaiverse/modules/__init__.py +18 -0
  8. unaiverse/modules/cnu/__init__.py +17 -0
  9. unaiverse/modules/cnu/cnus.py +536 -0
  10. unaiverse/modules/cnu/layers.py +261 -0
  11. unaiverse/modules/cnu/psi.py +60 -0
  12. unaiverse/modules/hl/__init__.py +15 -0
  13. unaiverse/modules/hl/hl_utils.py +411 -0
  14. unaiverse/modules/networks.py +1509 -0
  15. unaiverse/modules/utils.py +680 -0
  16. unaiverse/networking/__init__.py +16 -0
  17. unaiverse/networking/node/__init__.py +18 -0
  18. unaiverse/networking/node/connpool.py +1265 -0
  19. unaiverse/networking/node/node.py +2203 -0
  20. unaiverse/networking/node/profile.py +446 -0
  21. unaiverse/networking/node/tokens.py +79 -0
  22. unaiverse/networking/p2p/__init__.py +259 -0
  23. unaiverse/networking/p2p/golibp2p.py +18 -0
  24. unaiverse/networking/p2p/golibp2p.pyi +135 -0
  25. unaiverse/networking/p2p/lib.go +2495 -0
  26. unaiverse/networking/p2p/lib_types.py +312 -0
  27. unaiverse/networking/p2p/message_pb2.py +63 -0
  28. unaiverse/networking/p2p/messages.py +265 -0
  29. unaiverse/networking/p2p/mylogger.py +77 -0
  30. unaiverse/networking/p2p/p2p.py +963 -0
  31. unaiverse/streamlib/__init__.py +15 -0
  32. unaiverse/streamlib/streamlib.py +210 -0
  33. unaiverse/streams.py +763 -0
  34. unaiverse/utils/__init__.py +16 -0
  35. unaiverse/utils/ask_lone_wolf.json +27 -0
  36. unaiverse/utils/lone_wolf.json +19 -0
  37. unaiverse/utils/misc.py +305 -0
  38. unaiverse/utils/sandbox.py +293 -0
  39. unaiverse/utils/server.py +435 -0
  40. unaiverse/world.py +175 -0
  41. unaiverse-0.1.0.dist-info/METADATA +363 -0
  42. unaiverse-0.1.0.dist-info/RECORD +45 -0
  43. unaiverse-0.1.0.dist-info/WHEEL +5 -0
  44. unaiverse-0.1.0.dist-info/licenses/LICENSE +43 -0
  45. unaiverse-0.1.0.dist-info/top_level.txt +1 -0
unaiverse/clock.py ADDED
@@ -0,0 +1,186 @@
1
+ """
2
+ █████ █████ ██████ █████ █████ █████ █████ ██████████ ███████████ █████████ ██████████
3
+ ░░███ ░░███ ░░██████ ░░███ ░░███ ░░███ ░░███ ░░███░░░░░█░░███░░░░░███ ███░░░░░███░░███░░░░░█
4
+ ░███ ░███ ░███░███ ░███ ██████ ░███ ░███ ░███ ░███ █ ░ ░███ ░███ ░███ ░░░ ░███ █ ░
5
+ ░███ ░███ ░███░░███░███ ░░░░░███ ░███ ░███ ░███ ░██████ ░██████████ ░░█████████ ░██████
6
+ ░███ ░███ ░███ ░░██████ ███████ ░███ ░░███ ███ ░███░░█ ░███░░░░░███ ░░░░░░░░███ ░███░░█
7
+ ░███ ░███ ░███ ░░█████ ███░░███ ░███ ░░░█████░ ░███ ░ █ ░███ ░███ ███ ░███ ░███ ░ █
8
+ ░░████████ █████ ░░█████░░████████ █████ ░░███ ██████████ █████ █████░░█████████ ██████████
9
+ ░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░ ░░░░░ ░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░
10
+ A Collectionless AI Project (https://collectionless.ai)
11
+ Registration/Login: https://unaiverse.io
12
+ Code Repositories: https://github.com/collectionlessai/
13
+ Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi
14
+ """
15
+ import ntplib
16
+ import bisect
17
+ import socket
18
+ from ntplib import NTPException
19
+ from datetime import datetime, timezone
20
+
21
+
22
+ class Clock:
23
+ """
24
+ A class for managing time cycles and converting between timestamp and cycle indices.
25
+
26
+ This class interacts with an NTP server to synchronize time and supports operations
27
+ to track cycles, manage timestamps, and calculate the time differences between cycles.
28
+ """
29
+
30
+ def __init__(self, min_delta: float = -1):
31
+ """Initialize a Clock instance.
32
+
33
+ Args:
34
+ min_delta (float): Minimum time (in seconds) between consecutive cycles.
35
+ If less than or equal to zero, the cycles will be real-time-based.
36
+ """
37
+ self.min_delta = min_delta # Min-time passed between consecutive cycles (seconds) - if <=0, it is real-time
38
+ self.cycle = -1 # Internal index, not shared outside (the value -1 is only used at creation/reset time)
39
+ self.__servers = [
40
+ 'pool.ntp.org',
41
+ 'north-america.pool.ntp.org'
42
+ 'asia.pool.ntp.org',
43
+ 'europe.pool.ntp.org',
44
+ ]
45
+ self.__global_initial_t = self.__get_time_from_server() # Real-time, wall-clock
46
+ self.__local_initial_t = datetime.now(timezone.utc).timestamp() # Corresponding local time
47
+ self.__timestamps = [] # List to store timestamps for cycles
48
+ self.__time2cycle_cache = 0 # Cached cycle value for optimization
49
+
50
+ def __get_time_from_server(self) -> float:
51
+ """Get the current time from an NTP server.
52
+
53
+ Returns:
54
+ float: The time returned by the NTP server, converted to a timestamp.
55
+ """
56
+ c = ntplib.NTPClient()
57
+ response = None
58
+ for i in range(0, 10):
59
+ try:
60
+ server = self.__servers[i % len(self.__servers)]
61
+ response = c.request(server, version=3)
62
+ break
63
+ except (NTPException, socket.gaierror):
64
+ continue
65
+ return datetime.fromtimestamp(response.tx_time, timezone.utc).timestamp()
66
+
67
+ def __add_timestamp(self, timestamp: float):
68
+ """Add a timestamp to the list of timestamps for the clock cycles.
69
+
70
+ Args:
71
+ timestamp (float): The timestamp to be added to the list.
72
+
73
+ Raises:
74
+ ValueError: If the provided timestamp is not more recent than the last one.
75
+ """
76
+ if len(self.__timestamps) == 0 or self.__timestamps[-1] < timestamp:
77
+ self.__timestamps.append(timestamp)
78
+ else:
79
+ raise ValueError("Cannot add a timestamp that is NOT more recent than the already added ones")
80
+
81
+ def time2cycle(self, timestamp: float, delta: float | None = None) -> int:
82
+ """Convert a given timestamp to the corresponding cycle index.
83
+
84
+ Args:
85
+ timestamp (float): The timestamp to convert.
86
+ delta (float | None): The optional delta value for converting time to cycles.
87
+
88
+ Returns:
89
+ int: The cycle index corresponding to the given timestamp.
90
+ """
91
+ if delta is not None and delta > 0:
92
+ passed = self.get_time() - timestamp # Precision: microseconds
93
+ return self.cycle - int(passed * delta)
94
+ else:
95
+ self.__time2cycle_cache = Clock.__search(self.__timestamps, timestamp, self.__time2cycle_cache)
96
+ return self.__time2cycle_cache
97
+
98
+ def cycle2time(self, cycle: int, delta: float | None = None) -> float:
99
+ """Convert a cycle index to the corresponding timestamp.
100
+
101
+ Args:
102
+ cycle (int): The cycle index to convert.
103
+ delta (float | None): The optional delta value for converting cycles to time.
104
+
105
+ Returns:
106
+ float: The timestamp corresponding to the given cycle index.
107
+ """
108
+ if delta is not None and delta > 0:
109
+ return cycle * delta
110
+ else:
111
+ return self.__timestamps[cycle] if cycle >= 0 else -1.
112
+
113
+ def get_time(self, passed: bool = False) -> float:
114
+ """Get the current time based on the NTP server synchronization.
115
+
116
+ Returns:
117
+ float: The current synchronized time (in seconds since the Unix epoch).
118
+ """
119
+ passed_since_beginning = datetime.now(timezone.utc).timestamp() - self.__local_initial_t
120
+ return self.__global_initial_t + passed_since_beginning if not passed else passed_since_beginning
121
+
122
+ def get_time_as_string(self) -> str:
123
+ """Get the current time as a string (ISO format).
124
+
125
+ Returns:
126
+ str: A string representation of the current time (ISO format, UTC).
127
+ """
128
+ dt_object = datetime.fromtimestamp(self.get_time(), tz=timezone.utc)
129
+ return dt_object.isoformat(timespec='milliseconds')
130
+
131
+ def get_cycle(self):
132
+ """Get the current cycle index.
133
+
134
+ Returns:
135
+ int: The current cycle index.
136
+ """
137
+ return self.cycle
138
+
139
+ def get_cycle_time(self):
140
+ """Get the timestamp corresponding to the current cycle.
141
+
142
+ Returns:
143
+ float: The timestamp corresponding to the current cycle index.
144
+ """
145
+ return self.cycle2time(self.cycle)
146
+
147
+ def next_cycle(self) -> bool:
148
+ """Move to the next cycle if the minimum delta time has passed or if cycles are not constrained.
149
+
150
+ Returns:
151
+ bool: True if the cycle was successfully moved to the next one, False otherwise.
152
+ """
153
+ if self.cycle >= 0 and (self.min_delta > 0 and len(self.__timestamps) > 0 and
154
+ (self.get_time() - self.__timestamps[-1]) < self.min_delta):
155
+ return False
156
+ else:
157
+ self.cycle += 1 # Increment the cycle index
158
+ self.__add_timestamp(self.get_time())
159
+ return True
160
+
161
+ @staticmethod
162
+ def __search(_list, _target, _last_pos):
163
+ """Search for a target value in the list of timestamps and return the index of the corresponding cycle.
164
+
165
+ Args:
166
+ _list (list): The list of timestamps.
167
+ _target (float): The target timestamp to search for.
168
+ _last_pos (int): The last search position, used for optimization.
169
+
170
+ Returns:
171
+ int: The index of the found timestamp in the list, or -1 if not found.
172
+ """
173
+ if len(_list) > 0 and _target > _list[-1]:
174
+ return len(_list)
175
+ if len(_list) > _last_pos and _list[_last_pos] == _target:
176
+ return _last_pos
177
+ elif len(_list) > (_last_pos + 1) and _list[_last_pos + 1] == _target:
178
+ return _last_pos + 1
179
+ elif len(_list) == 0:
180
+ return -1
181
+ else:
182
+ ret = bisect.bisect_left(_list, _target)
183
+ if _list[ret] == _target:
184
+ return ret
185
+ else:
186
+ return -1