bool-hybrid-array 9.10.21__py3-none-any.whl → 9.11.1__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.
@@ -1,9 +1,11 @@
1
+ # -*- coding: utf-8 -*-
1
2
  import sys
2
3
  from types import ModuleType,FunctionType
3
4
  from . import core
4
5
  from .core import __builtins__,builtins
5
- from . import int_array
6
- __version__ = "9.10.21"
6
+ try:from . import int_array
7
+ except:pass
8
+ __version__ = "9.11.1"
7
9
  public_objects = []
8
10
  for name in dir(core):
9
11
  if not name.startswith("_"):
@@ -16,11 +18,11 @@ globals().update({
16
18
  for name in public_objects
17
19
  })
18
20
  try:
19
- __dict__ = ProtectedBuiltinsDict(globals())
20
- sys.modules[__name__+'.int_array'] = ProtectedBuiltinsDict(int_array.__dict__)
21
- sys.modules[__name__+'.core'] = ProtectedBuiltinsDict(core.__dict__)
22
21
  sys.modules[__name__] = ProtectedBuiltinsDict(globals())
23
- sys.modules[__name__].name = 'bool_hybrid_array'
22
+ sys.modules[__name__].name = __name__
23
+ sys.modules[__name__+'.core'] = ProtectedBuiltinsDict(core.__dict__,name = f'{__name__}.core')
24
+ __dict__ = ProtectedBuiltinsDict(globals())
25
+ sys.modules[__name__+'.int_array'] = ProtectedBuiltinsDict(int_array.__dict__,name = __name__+'.int_array')
24
26
  core.__dict__ = ProtectedBuiltinsDict(core.__dict__)
25
27
  except:
26
28
  pass
@@ -1,4 +1,8 @@
1
+ # -*- coding: utf-8 -*-
1
2
  from .__init__ import *
3
+ import io
4
+
5
+ sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='ignore')
2
6
 
3
7
  # 创建实例
4
8
 
bool_hybrid_array/core.py CHANGED
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
  import builtins
3
+ from types import MappingProxyType
3
4
  import array,bisect,numpy as np
4
5
  from collections.abc import MutableSequence,Iterable,Generator,Iterator,Sequence
5
6
  import itertools,copy,sys,math,weakref,random,mmap,os
@@ -8,6 +9,14 @@ import operator,ctypes,gc,abc,types
8
9
  from functools import lru_cache
9
10
  from typing import Union,_GenericAlias
10
11
  hybrid_array_cache = []
12
+ try:
13
+ msvcrt = ctypes.CDLL('msvcrt.dll')
14
+ memcpy = msvcrt.memcpy
15
+ except:
16
+ libc = ctypes.CDLL('libc.so.6')
17
+ memcpy = libc.memcpy
18
+ memcpy.argtypes = (ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t)
19
+ memcpy.restype = ctypes.c_void_p
11
20
  if 'UnionType' in types.__dict__:
12
21
  class Union:
13
22
  def __getitem__(self,*args):
@@ -29,12 +38,23 @@ class ResurrectMeta(abc.ABCMeta,metaclass=abc.ABCMeta):
29
38
  super_cls.__setattr__('bases', bases)
30
39
  super_cls.__setattr__('namespace', namespace)
31
40
  super_cls.__setattr__('original_dict', dict(obj.__dict__))
32
- del obj.original_dict["__abstractmethods__"]
33
- del obj.original_dict["_abc_impl"]
41
+ try:del obj.original_dict["__abstractmethods__"]
42
+ except:pass
43
+ try:del obj.original_dict["_abc_impl"]
44
+ except:pass
45
+ try:del obj.original_dict['_abc_registry']
46
+ except:pass
47
+ try:del obj.original_dict['_abc_cache']
48
+ except:pass
49
+ try:del obj.original_dict['_abc_negative_cache']
50
+ except:pass
51
+ try:del obj.original_dict['_abc_negative_cache_version']
52
+ except:pass
53
+ super_cls.__setattr__('original_dict', MappingProxyType(obj.original_dict))
34
54
  return obj
35
55
  @lru_cache
36
56
  def __str__(cls):
37
- return super().__repr__()[8:][:-2]
57
+ return f'{cls.__module__}.{cls.name}'
38
58
  @lru_cache
39
59
  def __repr__(cls,detailed = False):
40
60
  if detailed:
@@ -79,6 +99,7 @@ class ResurrectMeta(abc.ABCMeta,metaclass=abc.ABCMeta):
79
99
  original_dict["__or__"] = __or__
80
100
  except:
81
101
  pass
102
+ original_dict = MappingProxyType(original_dict)
82
103
  ResurrectMeta.__class__ = ResurrectMeta
83
104
  class BHA_Function(metaclass=ResurrectMeta):
84
105
  def __init__(self,v):
@@ -681,6 +702,9 @@ class BHA_List(list,metaclass=ResurrectMeta):
681
702
  "对比numpy节省": f"{(1 - total / (temp + 96)) * 100:.6f}%"}
682
703
  def __iter__(self):
683
704
  return BHA_Iterator(super().__iter__())
705
+ def to_ascii_art(self, width=20):
706
+ art = '\n'.join([''.join(['■' if j else ' ' for j in i]) for i in self])
707
+ return '\n'.join(art)
684
708
  class BHA_Iterator(Iterator,metaclass=ResurrectMeta):
685
709
  __module__ = 'bool_hybrid_array'
686
710
  def __init__(self,data):
@@ -703,11 +727,11 @@ class BHA_Iterator(Iterator,metaclass=ResurrectMeta):
703
727
  return arr.copy() if copy else arr.view()
704
728
  __rand__,__ror__,__rxor__ = __and__,__or__,__xor__
705
729
  class ProtectedBuiltinsDict(dict,metaclass=ResurrectMeta):
706
- def __init__(self, *args, protected_names = ["T", "F", "BHA_Bool", "BHA_List", "BoolHybridArray", "BoolHybridArr",
730
+ def __init__(self, *args, protected_names = ("T", "F", "BHA_Bool", "BHA_List", "BoolHybridArray", "BoolHybridArr",
707
731
  "TruesArray", "FalsesArray", "ProtectedBuiltinsDict", "builtins",
708
732
  "__builtins__", "__dict__","ResurrectMeta","math",
709
733
  "np","protected_names","BHA_Function",
710
- "__class__","Ask_BHA","Create_BHA","Ask_arr","numba_opt"],
734
+ "__class__","Ask_BHA","Create_BHA","Ask_arr","numba_opt"),
711
735
  name = 'builtins', **kwargs):
712
736
  super().__init__(*args, **kwargs)
713
737
  if name == 'builtins':
@@ -715,20 +739,27 @@ class ProtectedBuiltinsDict(dict,metaclass=ResurrectMeta):
715
739
  super().__setattr__('builtins',self)
716
740
  super().__setattr__('__builtins__',self)
717
741
  self.name = name
718
- self.protected_names = protected_names
742
+ super().__setattr__("protected_names",protected_names)
719
743
  def __setitem__(self, name, value):
720
- if name in ["T", "F"]:
721
- current_T = self.get("T")
722
- current_F = self.get("F")
723
- if isinstance(current_T, BHA_bool) and isinstance(current_F, BHA_bool):
724
- is_swap = (name == "T" and isinstance(value, BHA_bool) and value.value == current_F.value)or(name == "F" and isinstance(value, BHA_bool) and value.value == current_T.value)
725
- if is_swap:
726
- print(f"""警告:禁止交换内置常量 __{self.name}__["{name}"] 和 __builtins__["{'F' if name == 'T' else 'T'}"]!""")
727
- raise AttributeError(f"""禁止交换内置常量 __{self.name}__["{name}"] __{self.name}__["{'F' if name == 'T' else 'T'}"]""")
728
- if name in self.protected_names and name not in ["T", "F"]:
729
- print(f"警告:禁止修改内置常量 __{self.name}__['{name}']!")
730
- raise AttributeError(f"禁止修改内置常量 __{self.name}__['{name}']")
731
- super().__setitem__(name, value)
744
+ if not hasattr(self,"protected_names"):
745
+ super().__setitem__(name, value)
746
+ return
747
+ try:
748
+ if name in ["T", "F"]:
749
+ current_T = self.get("T")
750
+ current_F = self.get("F")
751
+ if isinstance(current_T, BHA_bool) and isinstance(current_F, BHA_bool):
752
+ is_swap = (name == "T" and isinstance(value, BHA_bool) and value.value == current_F.value)or(name == "F" and isinstance(value, BHA_bool) and value.value == current_T.value)
753
+ if is_swap:
754
+ print(f"""警告:禁止交换内置常量 __{self.name}__["{name}"] 和 __builtins__["{'F' if name == 'T' else 'T'}"]""")
755
+ raise AttributeError(f"""禁止交换内置常量 __{self.name}__["{name}"] 和 __{self.name}__["{'F' if name == 'T' else 'T'}"]""")
756
+ if name in self.protected_names and name not in ["T", "F"]:
757
+ print(f"警告:禁止修改内置常量 __{self.name}__['{name}']!")
758
+ raise AttributeError(f"禁止修改内置常量 __{self.name}__['{name}']")
759
+ except:
760
+ if sys.implementation.name == 'cpython':
761
+ raise
762
+ finally:super().__setitem__(name, value)
732
763
  def __delitem__(self, name):
733
764
  if name in self.protected_names:
734
765
  print(f"警告:禁止删除内置常量 __builtins__['{name}']!")
@@ -741,10 +772,12 @@ class ProtectedBuiltinsDict(dict,metaclass=ResurrectMeta):
741
772
  else:
742
773
  del self[name]
743
774
  def __getattr__(self, name):
744
- if name in self:
745
- return self[name]
746
- else:
747
- raise AttributeError(f"module 'builtins' has no attribute '{name}'")
775
+ try:
776
+ return super().__getattribute__(name)
777
+ except AttributeError:
778
+ if name in self:
779
+ return self[name]
780
+ raise AttributeError(f"module 'builtins' has no attribute '{name}'") from None
748
781
  def __setattr__(self,name,value):
749
782
  try:protected = self.protected_names
750
783
  except Exception:protected = self
@@ -786,18 +819,14 @@ def Ask_BHA(path):
786
819
  temp = mm.read().decode('utf-8').strip()
787
820
  temp = temp.split()
788
821
  temp2 = lambda x: BoolHybridArr(
789
- reduce(
790
- lambda acc, k: acc.append(0 if k < lead_zero else
791
- (n >> ((total_len - 1) - k)) & 1
792
- ) or acc,
793
- range(total_len),
794
- array.array('B',[])),
795
- hash_=F
796
- )if(
797
- n := int(x, base=16),
798
- lead_zero := len(x) - len(x.lstrip('0')),
799
- total_len := lead_zero + (n.bit_length() if n != 0 else 1)
800
- )else None
822
+ (
823
+ bit_stream := bytes(0 if k < lead_zero else (n >> ((total_len - 1) - k)) & 1 for k in range(total_len)),
824
+ arr := array.array('B', FalsesArray(total_len)),
825
+ memcpy(arr.buffer_info()[0], bit_stream, total_len),arr)[-1]
826
+ if(n := int(x, base=16),
827
+ lead_zero := len(x) - len(x.lstrip('0')),
828
+ total_len := lead_zero + (n.bit_length() if n else 1))
829
+ else array.array('B'))
801
830
  temp = BHA_List(map(temp2,temp))
802
831
  if len(temp) == 1:
803
832
  return temp[0]
@@ -1,7 +1,7 @@
1
+ # -*- coding: utf-8 -*-
1
2
  from __future__ import annotations
2
3
  from ..core import *
3
4
  import builtins
4
-
5
5
  class IntBitTag(BHA_bool, metaclass=ResurrectMeta):
6
6
  def __str__(self):
7
7
  return "'-1'" if (hasattr(self, 'is_sign_bit') and self.is_sign_bit and self) else "'1'" if self else "'0'"
@@ -80,7 +80,7 @@ class IntHybridArray(BoolHybridArray):
80
80
  block_end = block_start + self.bit_length
81
81
  if block_end > self.size:
82
82
  raise IndexError("索引超出范围")
83
- bit_chunk = [super().__getitem__(j) for j in range(block_start, block_end)]
83
+ bit_chunk = [super(self.__class__, self).__getitem__(j) for j in range(block_start, block_end)]
84
84
  num = self.to_int(bit_chunk)
85
85
  result.append(num)
86
86
  return IntHybridArray(result, self.bit_length)
@@ -91,7 +91,7 @@ class IntHybridArray(BoolHybridArray):
91
91
  block_end = block_start + self.bit_length
92
92
  if block_end > self.size:
93
93
  raise IndexError("索引超出范围")
94
- bit_chunk = [super().__getitem__(j) for j in range(block_start, block_end)]
94
+ bit_chunk = [super(self.__class__, self).__getitem__(j) for j in range(block_start, block_end)]
95
95
  return self.to_int(bit_chunk)
96
96
 
97
97
  def __setitem__(self, key, value):
@@ -145,14 +145,10 @@ class IntHybridArray(BoolHybridArray):
145
145
  carry = 0 if num_bits[j] else 1
146
146
  bool_data = [sign_bit] + num_bits
147
147
  for bit_idx in range(self.bit_length):
148
- super().__setitem__(block_start + bit_idx, bool_data[bit_idx])
148
+ super(self.__class__, self).__setitem__(block_start + bit_idx, bool_data[bit_idx])
149
149
 
150
150
  def __iter__(self):
151
- for i in range(0, self.total_bits, self.bit_length):
152
- if i + self.bit_length > self.size:
153
- break
154
- bit_chunk = [super().__getitem__(j) for j in range(i, i + self.bit_length)]
155
- yield self.to_int(bit_chunk)
151
+ return map(self.__getitem__,range(len(self)))
156
152
 
157
153
  def __str__(self):
158
154
  return f"IntHybridArray([{', '.join(map(str, self))}])"
@@ -173,7 +169,7 @@ class IntHybridArray(BoolHybridArray):
173
169
  def index(self, value):
174
170
  value = int(value)
175
171
  x = f"{value} 不在 IntHybridArray 中"
176
- for idx in range(len(self)+2>>2):
172
+ for idx in range(len(self)+1>>1):
177
173
  if self[idx] == value:
178
174
  return idx
179
175
  elif self[-idx] == value:
@@ -184,7 +180,7 @@ class IntHybridArray(BoolHybridArray):
184
180
  def rindex(self, value):
185
181
  value = int(value)
186
182
  x = f"{value} 不在 IntHybridArray 中"
187
- for idx in range(len(self)+2>>2):
183
+ for idx in range(len(self)+1>>1):
188
184
  if self[-idx] == value:
189
185
  return -idx
190
186
  elif self[idx] == value:
@@ -192,12 +188,7 @@ class IntHybridArray(BoolHybridArray):
192
188
  if x != f"{value} 不在 IntHybridArray 中":
193
189
  return x
194
190
  raise ValueError(x)
195
- def extend(self, iterable:Iterable) -> None:
196
- if isinstance(iterable, (Iterator, Generator, map)):
197
- iterable,copy = itertools.tee(iterable, 2)
198
- len_ = sum(1 for _ in copy)
199
- else:
200
- len_ = len(iterable)
201
- self.total_bits += len_*self.bit_length
202
- for i,j in zip(range(len_),iterable):
203
- self[-i-1] = j
191
+ def append(self,v):
192
+ self.self.total_bits += self.bit_length
193
+ self[-1] = v
194
+ __all__ = list(globals())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bool-hybrid-array
3
- Version: 9.10.21
3
+ Version: 9.11.1
4
4
  Summary: 一个高效的布尔数组(密集+稀疏混合存储,节省内存)
5
5
  Home-page: https://github.com/BKsell/bool-hybrid-array
6
6
  Author: 蔡靖杰
@@ -17,6 +17,9 @@ Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3.12
18
18
  Classifier: Programming Language :: Python :: 3.13
19
19
  Classifier: Programming Language :: Python :: 3.14
20
+ Classifier: Programming Language :: Python :: 3 :: Only
21
+ Classifier: Programming Language :: Python :: Implementation :: CPython
22
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
20
23
  Classifier: License :: OSI Approved :: MIT License
21
24
  Classifier: Operating System :: OS Independent
22
25
  Requires-Python: >=3.8
@@ -24,6 +27,8 @@ Description-Content-Type: text/markdown
24
27
  License-File: LICENSE
25
28
  Requires-Dist: numpy>=1.19.0
26
29
  Provides-Extra: int-array
30
+ Provides-Extra: numba-opt
31
+ Requires-Dist: numba>=0.55.0; extra == "numba-opt"
27
32
  Dynamic: author
28
33
  Dynamic: author-email
29
34
  Dynamic: classifier
@@ -40,10 +45,17 @@ Dynamic: summary
40
45
 
41
46
  # BoolHybridArray:高效的布尔混合数组库
42
47
 
48
+ <!-- 月下载量(核心推荐)+ 周下载量 + 总下载量 -->
49
+ [![月下载量](https://static.pepy.tech/badge/bool-hybrid-array/month)](https://pepy.tech/project/bool-hybrid-array)
50
+ [![周下载量](https://static.pepy.tech/badge/bool-hybrid-array/week)](https://pepy.tech/project/bool-hybrid-array)
51
+ [![总下载量](https://static.pepy.tech/badge/bool-hybrid-array)](https://pepy.tech/project/bool-hybrid-array)
52
+
43
53
  一个专为布尔值优化的数组类,能够根据数据特征自动在密集存储和稀疏存储模式间切换,兼顾性能和内存效率。
44
54
 
45
55
  ## ***注:大版本号更新并非不兼容更新,而是重大更新***
46
56
 
57
+ 注:当API完全稳定时就会发布版本规则较严谨的new-1.0.0版本,预期为10.x版本结束后
58
+
47
59
  ***注意:目前社区中若存在名称类似(如包含 “bool-hybrid-array”+ 后缀、boolean\_array、validate.io-boolean-array)的库,均与本项目(bool-hybrid-array)无关联,本项目的所有功能设计、代码实现均为独立开发;***
48
60
  ***其他平台里的类似名库绝不是我的库***
49
61
 
@@ -141,7 +153,7 @@ big_arr = BoolHybridArr([i % 100 == 0 for i in range(10000)])
141
153
 
142
154
  # 查看存储模式(此时应为稀疏模式)
143
155
 
144
- print(repr(big_arr)) # 输出: BoolHybridArray(split_index=100,size=10000,is_sparse=True,small_len=100,large_len=)不好意思large_len我不知道
156
+ print(repr(big_arr)) # 输出: BoolHybridArray(split_index=100, size=10000, is_sparse=True, small_len=101, large_len=98)
145
157
 
146
158
  # 自动优化存储
147
159
 
@@ -571,6 +583,13 @@ except OverflowError as e:
571
583
  * **9.10.19**:和9.10.18.post4相同,正式版本发布
572
584
  * **9.10.20**:优化性能,增加BHA_jit_log日志
573
585
  * **9.10.21**:优化Ask_BHA,移除BHA_jit_log日志
586
+ * **9.10.22**:进一步优化Ask_BHA的性能
587
+ * **9.11.0**:新增对7.3.10以上版本的PyPy解释器的支持
588
+ * **9.11.1**:修复PyPy解释器下的保护机制过度保护bug
589
+
590
+
591
+
592
+
574
593
 
575
594
 
576
595
  ## **彩蛋:**
@@ -585,7 +604,11 @@ except OverflowError as e:
585
604
  * A:BoolHybridArray是本库中的**核心类**,所有函数都是围绕他进行的,但需要`split_index`,`size`,`is_sparse`;
586
605
  BoolHybridArr是一个**函数**,用于把一个可迭代对象转为`BoolHybridArray`类
587
606
  * Q:为什么不建议把太大的本类型数组打印?
588
- * A:虽然本类型数组省内存,但字符串不省内存,一个`True`要4字节,一个`False`要5字节,连逗号都要占1字节(`numpy.ndarray`:我都比字符串省内存)
607
+ * A:虽然BoolHybridArray类型数组省内存,但字符串不省内存,一个`True`要4字节,一个`False`要5字节,连逗号都要占1字节(`numpy.ndarray`:我都比字符串省内存)
608
+ - Q:为什么使用在Windows终端使用PyPy时建议先调用`chcp 65001`再启动PyPy?
609
+ * A:因为本库有很多的的中文报错和打印,项目里也可能会有。Windows终端里的PyPy默认是GBK编码,有中文时会乱码。chcp 65001可以切换到UTF-8编码,防止乱码。
610
+
611
+
589
612
 
590
613
  ## 源代码和原理
591
614
 
@@ -0,0 +1,10 @@
1
+ bool_hybrid_array/__init__.py,sha256=MEv3Ygh5XtBGxhu6oQICjkWyWSFUkU3xIyZSn0RKYDY,1038
2
+ bool_hybrid_array/__main__.py,sha256=3bnJ2IQctrXWQ1WyBQvh1w882_k3b84LGLL8yUNkXws,8447
3
+ bool_hybrid_array/core.py,sha256=nZbzHWLQhQpsQ1YVgqyvcw3znKlEZZA4QrSiSraOJN8,39343
4
+ bool_hybrid_array/秘密.md,sha256=Ii2NvXmv-Ktu04zJsGLcQZvlzT4gOatByE4B2wTK1Ks,48
5
+ bool_hybrid_array/int_array/__init__.py,sha256=KejzB6xqUjLkoDhihrtyoBXL_Rqom1XBIDtXpQeIQEg,8192
6
+ bool_hybrid_array-9.11.1.dist-info/licenses/LICENSE,sha256=Sg4rnGXkBDYkwJCWyxdWp5H60rhVAxpNvFh_l3JWZdY,1070
7
+ bool_hybrid_array-9.11.1.dist-info/METADATA,sha256=42nd7b4N11jcmkzdp0Y0o0jx5FMGpkJnJmnbSOiU49Y,24794
8
+ bool_hybrid_array-9.11.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
+ bool_hybrid_array-9.11.1.dist-info/top_level.txt,sha256=vk-TD77wuVQsN1rJ6uVWZX4sC_wya_WplRDwQKJoBZM,18
10
+ bool_hybrid_array-9.11.1.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- bool_hybrid_array/__init__.py,sha256=r04Lq3_vRUkxPYT4oLIz8wLBhbRP70UvXGQKMpt8tps,955
2
- bool_hybrid_array/__main__.py,sha256=3MsUAXMj6Pe1EzP7YreuUePbXZeQvFSCnejnTeS42kU,8324
3
- bool_hybrid_array/core.py,sha256=MNxYpZdtY36ElBbdqYjNUp1fJwYEHSTsc1Y323Cytro,37984
4
- bool_hybrid_array/秘密.md,sha256=Ii2NvXmv-Ktu04zJsGLcQZvlzT4gOatByE4B2wTK1Ks,48
5
- bool_hybrid_array/int_array/__init__.py,sha256=acjFchFu3k9CQndDbvKfSIyfMMAbgprz8Skf1a4_dKY,8578
6
- bool_hybrid_array-9.10.21.dist-info/licenses/LICENSE,sha256=Sg4rnGXkBDYkwJCWyxdWp5H60rhVAxpNvFh_l3JWZdY,1070
7
- bool_hybrid_array-9.10.21.dist-info/METADATA,sha256=k037Kzz3zMnTbD-zaIfSacesqeVIThAW2Eu6_AlEiXk,23491
8
- bool_hybrid_array-9.10.21.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
9
- bool_hybrid_array-9.10.21.dist-info/top_level.txt,sha256=vk-TD77wuVQsN1rJ6uVWZX4sC_wya_WplRDwQKJoBZM,18
10
- bool_hybrid_array-9.10.21.dist-info/RECORD,,