|
12 | 12 | import ssl
|
13 | 13 | import time
|
14 | 14 | from collections import defaultdict
|
| 15 | +from collections.abc import Iterable |
15 | 16 | from dataclasses import dataclass
|
16 | 17 | from datetime import datetime
|
17 | 18 | from hashlib import blake2b
|
|
51 | 52 |
|
52 | 53 |
|
53 | 54 | class ScaleObj:
|
54 |
| - def __new__(cls, value): |
55 |
| - if isinstance(value, (dict, str, int)): |
56 |
| - return value |
57 |
| - return super().__new__(cls) |
| 55 | + """Bittensor representation of Scale Object.""" |
58 | 56 |
|
59 | 57 | def __init__(self, value):
|
60 | 58 | self.value = list(value) if isinstance(value, tuple) else value
|
61 | 59 |
|
| 60 | + def __new__(cls, value): |
| 61 | + return super().__new__(cls) |
| 62 | + |
62 | 63 | def __str__(self):
|
63 | 64 | return f"BittensorScaleType(value={self.value})>"
|
64 | 65 |
|
| 66 | + def __bool__(self): |
| 67 | + if self.value: |
| 68 | + return True |
| 69 | + else: |
| 70 | + return False |
| 71 | + |
65 | 72 | def __repr__(self):
|
66 |
| - return repr(self.value) |
| 73 | + return repr(f"BittensorScaleType(value={self.value})>") |
67 | 74 |
|
68 | 75 | def __eq__(self, other):
|
69 |
| - return self.value == other |
| 76 | + return self.value == (other.value if isinstance(other, ScaleObj) else other) |
| 77 | + |
| 78 | + def __lt__(self, other): |
| 79 | + return self.value < (other.value if isinstance(other, ScaleObj) else other) |
| 80 | + |
| 81 | + def __gt__(self, other): |
| 82 | + return self.value > (other.value if isinstance(other, ScaleObj) else other) |
| 83 | + |
| 84 | + def __le__(self, other): |
| 85 | + return self.value <= (other.value if isinstance(other, ScaleObj) else other) |
| 86 | + |
| 87 | + def __ge__(self, other): |
| 88 | + return self.value >= (other.value if isinstance(other, ScaleObj) else other) |
| 89 | + |
| 90 | + def __add__(self, other): |
| 91 | + if isinstance(other, ScaleObj): |
| 92 | + return ScaleObj(self.value + other.value) |
| 93 | + return ScaleObj(self.value + other) |
| 94 | + |
| 95 | + def __radd__(self, other): |
| 96 | + return ScaleObj(other + self.value) |
| 97 | + |
| 98 | + def __sub__(self, other): |
| 99 | + if isinstance(other, ScaleObj): |
| 100 | + return ScaleObj(self.value - other.value) |
| 101 | + return ScaleObj(self.value - other) |
| 102 | + |
| 103 | + def __rsub__(self, other): |
| 104 | + return ScaleObj(other - self.value) |
| 105 | + |
| 106 | + def __mul__(self, other): |
| 107 | + if isinstance(other, ScaleObj): |
| 108 | + return ScaleObj(self.value * other.value) |
| 109 | + return ScaleObj(self.value * other) |
| 110 | + |
| 111 | + def __rmul__(self, other): |
| 112 | + return ScaleObj(other * self.value) |
| 113 | + |
| 114 | + def __truediv__(self, other): |
| 115 | + if isinstance(other, ScaleObj): |
| 116 | + return ScaleObj(self.value / other.value) |
| 117 | + return ScaleObj(self.value / other) |
| 118 | + |
| 119 | + def __rtruediv__(self, other): |
| 120 | + return ScaleObj(other / self.value) |
| 121 | + |
| 122 | + def __floordiv__(self, other): |
| 123 | + if isinstance(other, ScaleObj): |
| 124 | + return ScaleObj(self.value // other.value) |
| 125 | + return ScaleObj(self.value // other) |
| 126 | + |
| 127 | + def __rfloordiv__(self, other): |
| 128 | + return ScaleObj(other // self.value) |
| 129 | + |
| 130 | + def __mod__(self, other): |
| 131 | + if isinstance(other, ScaleObj): |
| 132 | + return ScaleObj(self.value % other.value) |
| 133 | + return ScaleObj(self.value % other) |
| 134 | + |
| 135 | + def __rmod__(self, other): |
| 136 | + return ScaleObj(other % self.value) |
| 137 | + |
| 138 | + def __pow__(self, other): |
| 139 | + if isinstance(other, ScaleObj): |
| 140 | + return ScaleObj(self.value**other.value) |
| 141 | + return ScaleObj(self.value**other) |
| 142 | + |
| 143 | + def __rpow__(self, other): |
| 144 | + return ScaleObj(other**self.value) |
| 145 | + |
| 146 | + def __getitem__(self, key): |
| 147 | + if isinstance(self.value, (list, tuple, dict, str)): |
| 148 | + return self.value[key] |
| 149 | + raise TypeError( |
| 150 | + f"Object of type '{type(self.value).__name__}' does not support indexing" |
| 151 | + ) |
70 | 152 |
|
71 | 153 | def __iter__(self):
|
72 |
| - for item in self.value: |
73 |
| - yield item |
| 154 | + if isinstance(self.value, Iterable): |
| 155 | + return iter(self.value) |
| 156 | + raise TypeError(f"Object of type '{type(self.value).__name__}' is not iterable") |
74 | 157 |
|
75 |
| - def __getitem__(self, item): |
76 |
| - return self.value[item] |
| 158 | + def __len__(self): |
| 159 | + return len(self.value) |
| 160 | + |
| 161 | + def serialize(self): |
| 162 | + return self.value |
| 163 | + |
| 164 | + def decode(self): |
| 165 | + return self.value |
77 | 166 |
|
78 | 167 |
|
79 | 168 | class AsyncExtrinsicReceipt:
|
@@ -998,10 +1087,7 @@ def __init__(
|
998 | 1087 | )
|
999 | 1088 | if pre_initialize:
|
1000 | 1089 | if not _mock:
|
1001 |
| - execute_coroutine( |
1002 |
| - coroutine=self.initialize(), |
1003 |
| - event_loop=self.event_loop, |
1004 |
| - ) |
| 1090 | + self.event_loop.create_task(self.initialize()) |
1005 | 1091 | else:
|
1006 | 1092 | self.reload_type_registry()
|
1007 | 1093 |
|
@@ -3478,9 +3564,9 @@ async def query(
|
3478 | 3564 | raw_storage_key: Optional[bytes] = None,
|
3479 | 3565 | subscription_handler=None,
|
3480 | 3566 | reuse_block_hash: bool = False,
|
3481 |
| - ) -> "ScaleType": |
| 3567 | + ) -> Optional[Union["ScaleObj", Any]]: |
3482 | 3568 | """
|
3483 |
| - Queries subtensor. This should only be used when making a single request. For multiple requests, |
| 3569 | + Queries substrate. This should only be used when making a single request. For multiple requests, |
3484 | 3570 | you should use ``self.query_multiple``
|
3485 | 3571 | """
|
3486 | 3572 | block_hash = await self._get_current_block_hash(block_hash, reuse_block_hash)
|
@@ -3524,7 +3610,7 @@ async def query_map(
|
3524 | 3610 | page_size: int = 100,
|
3525 | 3611 | ignore_decoding_errors: bool = False,
|
3526 | 3612 | reuse_block_hash: bool = False,
|
3527 |
| - ) -> "QueryMapResult": |
| 3613 | + ) -> QueryMapResult: |
3528 | 3614 | """
|
3529 | 3615 | Iterates over all key-pairs located at the given module and storage_function. The storage
|
3530 | 3616 | item must be a map.
|
@@ -3686,9 +3772,7 @@ def concat_hash_len(key_hasher: str) -> int:
|
3686 | 3772 | if not ignore_decoding_errors:
|
3687 | 3773 | raise
|
3688 | 3774 | item_value = None
|
3689 |
| - |
3690 | 3775 | result.append([item_key, item_value])
|
3691 |
| - |
3692 | 3776 | return QueryMapResult(
|
3693 | 3777 | records=result,
|
3694 | 3778 | page_size=page_size,
|
|
0 commit comments