cdxcore 0.1.5__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 cdxcore might be problematic. Click here for more details.
- cdxcore/__init__.py +15 -0
- cdxcore/config.py +1633 -0
- cdxcore/crman.py +105 -0
- cdxcore/deferred.py +220 -0
- cdxcore/dynaplot.py +1155 -0
- cdxcore/filelock.py +430 -0
- cdxcore/jcpool.py +411 -0
- cdxcore/logger.py +319 -0
- cdxcore/np.py +1098 -0
- cdxcore/npio.py +270 -0
- cdxcore/prettydict.py +388 -0
- cdxcore/prettyobject.py +64 -0
- cdxcore/sharedarray.py +285 -0
- cdxcore/subdir.py +2963 -0
- cdxcore/uniquehash.py +970 -0
- cdxcore/util.py +1041 -0
- cdxcore/verbose.py +403 -0
- cdxcore/version.py +402 -0
- cdxcore-0.1.5.dist-info/METADATA +1418 -0
- cdxcore-0.1.5.dist-info/RECORD +30 -0
- cdxcore-0.1.5.dist-info/WHEEL +5 -0
- cdxcore-0.1.5.dist-info/licenses/LICENSE +21 -0
- cdxcore-0.1.5.dist-info/top_level.txt +4 -0
- conda/conda_exists.py +10 -0
- conda/conda_modify_yaml.py +42 -0
- tests/_cdxbasics.py +1086 -0
- tests/test_uniquehash.py +469 -0
- tests/test_util.py +329 -0
- up/git_message.py +7 -0
- up/pip_modify_setup.py +55 -0
tests/test_uniquehash.py
ADDED
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Created on Tue Apr 14 21:24:52 2020
|
|
4
|
+
@author: hansb
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import unittest as unittest
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pandas as pd
|
|
10
|
+
import datetime as datetime
|
|
11
|
+
from zoneinfo import ZoneInfo
|
|
12
|
+
import hashlib as hashlib
|
|
13
|
+
|
|
14
|
+
"""
|
|
15
|
+
import importlib as imp
|
|
16
|
+
import source.util as _
|
|
17
|
+
imp.reload(_)
|
|
18
|
+
import source.prettydict as _
|
|
19
|
+
imp.reload(_)
|
|
20
|
+
import source.prettyobject as _
|
|
21
|
+
imp.reload(_)
|
|
22
|
+
import source.uniquehash as _
|
|
23
|
+
imp.reload(_)
|
|
24
|
+
"""
|
|
25
|
+
from cdxcore.uniquehash import UniqueHash, namedUniqueHashExt, uniqueLabelExt, uniqueHash, uniqueHash8, uniqueHash16, uniqueHash32, uniqueHash48, uniqueHash64, DEF_FILE_NAME_MAP, DebugTraceCollect, DebugTraceVerbose
|
|
26
|
+
from cdxcore.prettydict import PrettyDict, PrettyOrderedDict
|
|
27
|
+
from cdxcore.prettyobject import PrettyObject
|
|
28
|
+
|
|
29
|
+
uniqueHashProtected = UniqueHash(32,parse_underscore="protected")
|
|
30
|
+
uniqueHashPrivate = UniqueHash(32,parse_underscore="private")
|
|
31
|
+
uniqueHashF = UniqueHash(32,parse_functions=True)
|
|
32
|
+
|
|
33
|
+
namedUniqueHash32_8 = namedUniqueHashExt( max_length=32, id_length=8, parse_underscore="protected" )
|
|
34
|
+
uniqueLabel32_8 = uniqueLabelExt( max_length=32, id_length=8 )
|
|
35
|
+
|
|
36
|
+
uniqueLabelExt
|
|
37
|
+
|
|
38
|
+
class UtilTest(unittest.TestCase):
|
|
39
|
+
|
|
40
|
+
def __init__(self, *args, **kwargs):
|
|
41
|
+
self.maxDiff = 2000
|
|
42
|
+
super().__init__(*args, **kwargs)
|
|
43
|
+
|
|
44
|
+
def test_uniqueHash(self):
|
|
45
|
+
|
|
46
|
+
self.assertEqual( uniqueHash( 1==1 ), "c8814ea06a53a108a02015477d9ea347" )
|
|
47
|
+
self.assertEqual( uniqueHash( "test" ), "e9ddd9926b9dcb382e09be39ba403d2c" )
|
|
48
|
+
self.assertEqual( uniqueHash( "test".encode("utf-8") ), "e9ddd9926b9dcb382e09be39ba403d2c" )
|
|
49
|
+
self.assertEqual( uniqueHash( 0 ), "d566dfb39f20d549d1c0684e94949c71" )
|
|
50
|
+
self.assertEqual( uniqueHash( -1 ), "265e3aae841649db057419ac7d85d399" )
|
|
51
|
+
self.assertEqual( uniqueHash( 0x7fff ), "1770301688382dac9c6d87284d5a3111" )
|
|
52
|
+
self.assertEqual( uniqueHash( 0.1 ), "f45961d9b7e8673e7c758bcfd8af7cb9" )
|
|
53
|
+
self.assertEqual( uniqueHash( 1E-10), "9102ac1dc2adb0aa013cd897a69f74d2" )
|
|
54
|
+
self.assertEqual( uniqueHash( np.nan ), "a22bbc77f337db5e2445bd5c0235812e" )
|
|
55
|
+
|
|
56
|
+
self.assertEqual( uniqueHash( np.float16(0) ), "ff1bed336aae497f15a0d3f534609380" )
|
|
57
|
+
self.assertEqual( uniqueHash( np.float32(0) ), "c566588ea757dd31297b78f8c62a4b05" )
|
|
58
|
+
self.assertEqual( uniqueHash( np.float64(0) ), "d566dfb39f20d549d1c0684e94949c71" )
|
|
59
|
+
self.assertEqual( uniqueHash( np.int8(0) ), "9f31f3ec588c6064a8e1f9051aeab90a" )
|
|
60
|
+
self.assertEqual( uniqueHash( np.int8(-1) ), "df09d795132a032676a7ca47bcc2db61" )
|
|
61
|
+
self.assertEqual( uniqueHash( np.uint8(250) ), "5067455d73d452a04850bf2f20ee3c61" )
|
|
62
|
+
self.assertEqual( uniqueHash( np.int16(0) ), "ff1bed336aae497f15a0d3f534609380" )
|
|
63
|
+
self.assertEqual( uniqueHash( np.uint16(0xFFFF) ), "f1118c91662ef7ccc0e2f45922d41d90" )
|
|
64
|
+
self.assertEqual( uniqueHash( np.int32(0) ), "c566588ea757dd31297b78f8c62a4b05" )
|
|
65
|
+
self.assertEqual( uniqueHash( np.int64(0) ), "d566dfb39f20d549d1c0684e94949c71" )
|
|
66
|
+
self.assertEqual( uniqueHash( np.int64(-1) ), "265e3aae841649db057419ac7d85d399" )
|
|
67
|
+
|
|
68
|
+
self.assertEqual( uniqueHash( [1,2,np.float16(3),4] ), "0922f2f0d1df45dac328582df43888f2" )
|
|
69
|
+
self.assertEqual( uniqueHash( [2,1,np.float16(3),4] ), "a33b2ce355ea93a0153d53806e5b692b" )
|
|
70
|
+
self.assertEqual( uniqueHash( set([1,2,np.float16(3),4]) ), "0922f2f0d1df45dac328582df43888f2" )
|
|
71
|
+
self.assertEqual( uniqueHash( x for x in [1,2,np.float16(3),4] ), "64550d6ffe2c0a01a14aba1eade0200c" )
|
|
72
|
+
|
|
73
|
+
# dicts
|
|
74
|
+
self.assertEqual( uniqueHash( {'a':"mix"} ), "cfd3448192fa25895e52426f69d550b1" )
|
|
75
|
+
self.assertEqual( uniqueHash( {'a':"mix", 'b':1} ), "3c85e8c0142b9b3d9c78b10106e27fdc" )
|
|
76
|
+
|
|
77
|
+
r = PrettyOrderedDict()
|
|
78
|
+
r.x = 1
|
|
79
|
+
self.assertEqual( uniqueHash( r ), "5bdac013107a78ea9ca53a6df858cfa8" )
|
|
80
|
+
r.x = 2
|
|
81
|
+
hashr = uniqueHash( r )
|
|
82
|
+
self.assertEqual( hashr, "e6b10cf5d15f3ec798eb9daa516606cf" ) # (1)
|
|
83
|
+
r._x = 1
|
|
84
|
+
# elements with '_' are ignored by default
|
|
85
|
+
self.assertEqual( uniqueHash( r ), hashr ) # same as above (1)
|
|
86
|
+
self.assertEqual( uniqueHash( PrettyOrderedDict(_y=100,x=2 ) ), hashr ) # same as above (1)
|
|
87
|
+
self.assertEqual( uniqueHash( PrettyOrderedDict(__y=100,x=2 ) ), hashr ) # same as above (1)
|
|
88
|
+
# protected
|
|
89
|
+
self.assertEqual( uniqueHashProtected( PrettyOrderedDict(x=2 )), hashr ) # same as (1)
|
|
90
|
+
hashprot = uniqueHashProtected( PrettyOrderedDict(_y=100,x=2 ) )
|
|
91
|
+
self.assertEqual( hashprot, "c1b4c0a3a39540a6438e8426be81e81d" ) # (2)
|
|
92
|
+
self.assertEqual( uniqueHashProtected( PrettyOrderedDict(_y=100,__z=100,x=2 ) ), hashprot ) # same as (2)
|
|
93
|
+
self.assertEqual( uniqueHashProtected( PrettyOrderedDict(__z=100,x=2 ) ), hashr ) # same as (1)
|
|
94
|
+
# private
|
|
95
|
+
self.assertEqual( uniqueHashPrivate( PrettyOrderedDict(x=2 ) ), hashr ) # same as (1)
|
|
96
|
+
self.assertEqual( uniqueHashPrivate( PrettyOrderedDict(_y=100,x=2 ) ), hashprot ) # same as (2)
|
|
97
|
+
hash3 = uniqueHashPrivate( PrettyOrderedDict(_y=100,__z=100,x=2 ) )
|
|
98
|
+
hash4 = uniqueHashPrivate( PrettyOrderedDict(__z=100,x=2 ) )
|
|
99
|
+
self.assertEqual( hash3, "cbcbb27eb918dd7998bf4d2092bfe2a5" ) # (3)
|
|
100
|
+
self.assertNotEqual( hash3, hashprot )
|
|
101
|
+
self.assertEqual( hash4, "9cc69ec4c0e9f3f820ca63531e7ba0a2" ) # (4)
|
|
102
|
+
self.assertNotEqual( hash4, hashprot )
|
|
103
|
+
|
|
104
|
+
class Object(object):
|
|
105
|
+
def __init__(self_):
|
|
106
|
+
self_.x = [ 1,2,3. ]
|
|
107
|
+
self_.y = { 'a':1, 'b':2 }
|
|
108
|
+
self_.z = PrettyDict(c=3,d=4)
|
|
109
|
+
self_.r = set([65,6234,1231,123123,12312])
|
|
110
|
+
self_.t = (1,2,"test")
|
|
111
|
+
self_.s = {1,3,4,2,5}
|
|
112
|
+
|
|
113
|
+
def ff():
|
|
114
|
+
pass
|
|
115
|
+
|
|
116
|
+
self_.ff = ff
|
|
117
|
+
self_.gg = lambda x : x*x
|
|
118
|
+
|
|
119
|
+
self_.a = np.array([1,2,3])
|
|
120
|
+
self_.b = np.zeros((3,4,2))
|
|
121
|
+
self_.c = pd.DataFrame({'a':np.array([1,2,3]),'b':np.array([10,20,30]),'c':np.array([100,200,300]), })
|
|
122
|
+
|
|
123
|
+
u = uniqueHash(self_.b) # numpy
|
|
124
|
+
self.assertEqual( u, "de8bc8a3a92214d15e50f137565ed6a7" )
|
|
125
|
+
u = uniqueHash(self_.c) # panda frame
|
|
126
|
+
self.assertEqual( u, "6825de2c38cab22de057211b1ad01ce8" )
|
|
127
|
+
|
|
128
|
+
def f(self_):
|
|
129
|
+
pass
|
|
130
|
+
|
|
131
|
+
@staticmethod
|
|
132
|
+
def g(self_):
|
|
133
|
+
pass
|
|
134
|
+
|
|
135
|
+
@property
|
|
136
|
+
def h(self_):
|
|
137
|
+
return self_.x
|
|
138
|
+
|
|
139
|
+
x = np.array([1,2,3,4.])
|
|
140
|
+
u = uniqueHash(x)
|
|
141
|
+
self.assertEqual( u, "b45b735c27fa083d3ea50021251d178f" )
|
|
142
|
+
|
|
143
|
+
o2 = [ np.float32(0), np.float64(0), np.int32(0), np.int64(0) ]
|
|
144
|
+
u = uniqueHash(o2)
|
|
145
|
+
self.assertEqual( u, "8bae89164e1da1371c9beacc007db340" )
|
|
146
|
+
|
|
147
|
+
o = Object()
|
|
148
|
+
u = uniqueHash(o)
|
|
149
|
+
self.assertEqual( u, "150286ddf12cbfff1302f45c3ce37326" )
|
|
150
|
+
u = uniqueHash8(o)
|
|
151
|
+
self.assertEqual( u, "4b3e7cf3" )
|
|
152
|
+
u = uniqueHash16(o)
|
|
153
|
+
self.assertEqual( u, "6797b5a88f6eb87a" )
|
|
154
|
+
u = uniqueHash32(o)
|
|
155
|
+
self.assertEqual( u, "150286ddf12cbfff1302f45c3ce37326" )
|
|
156
|
+
u = uniqueHash48(o)
|
|
157
|
+
self.assertEqual( u, "3273bb211f07018d61427dadb53eb2f7b10b4b67b9d9171b" )
|
|
158
|
+
u = uniqueHash64(o)
|
|
159
|
+
self.assertEqual( u, "f08ffa55d05dca6a2a8c4c2ae78d94503481c7698534fe7c3f5efe1743dab3fa" )
|
|
160
|
+
|
|
161
|
+
class X(object):
|
|
162
|
+
__slots__ = ['x', 'y']
|
|
163
|
+
def __init__(self, x, y):
|
|
164
|
+
self.x = x
|
|
165
|
+
self.y = y
|
|
166
|
+
def __repr__(self):
|
|
167
|
+
return f"X(x={self.x},y={self.y})"
|
|
168
|
+
A = X
|
|
169
|
+
class X(object): # looks the same as X in non-protected mode !
|
|
170
|
+
__slots__ = ['x', 'y', '_z']
|
|
171
|
+
def __init__(self, x, y):
|
|
172
|
+
self.x = x
|
|
173
|
+
self.y = y
|
|
174
|
+
self._z = x*y
|
|
175
|
+
def __repr__(self):
|
|
176
|
+
return f"X(x={self.x},y={self.y},_z={self._z})"
|
|
177
|
+
B = X
|
|
178
|
+
a1 = A(1,2)
|
|
179
|
+
a2 = A(1,3)
|
|
180
|
+
b = B(1,2)
|
|
181
|
+
debug_trace = DebugTraceCollect()
|
|
182
|
+
hash1 = uniqueHash(a1, debug_trace=debug_trace)
|
|
183
|
+
trace1 = repr(debug_trace)
|
|
184
|
+
|
|
185
|
+
debug_trace = DebugTraceCollect()
|
|
186
|
+
hash2 = uniqueHash(a2, debug_trace=debug_trace)
|
|
187
|
+
trace2 = repr(debug_trace)
|
|
188
|
+
|
|
189
|
+
debug_trace = DebugTraceCollect()
|
|
190
|
+
hash3 = uniqueHash(b,debug_trace=debug_trace)
|
|
191
|
+
trace3 = repr(debug_trace)
|
|
192
|
+
|
|
193
|
+
self.assertEqual( hash1, "26aa5df9a6a96949cbc3d3bab64d1e9d" ) # (1)
|
|
194
|
+
self.assertEqual( hash2, "95122861376b8f6ef690060ae5f487c4" ) # (2) != (1)
|
|
195
|
+
self.assertNotEqual( hash1, hash2 ) # (1) != (2)
|
|
196
|
+
self.assertEqual( hash3, "26aa5df9a6a96949cbc3d3bab64d1e9d" ) # (3) == (1)
|
|
197
|
+
self.assertEqual( hash3, hash1 ) # (3) == (1)
|
|
198
|
+
self.assertEqual( uniqueHashProtected(a1), hash1 ) # == (1)
|
|
199
|
+
self.assertEqual( uniqueHashProtected(a2), hash2 ) # == (2)
|
|
200
|
+
debug_trace = DebugTraceCollect()
|
|
201
|
+
hash3p = uniqueHashProtected(b,debug_trace=debug_trace)
|
|
202
|
+
trace3p = repr(debug_trace)
|
|
203
|
+
self.assertEqual( hash3p, "1e74dccad7ca21b4c31a30b7a305e46f" ) # != (1)
|
|
204
|
+
|
|
205
|
+
r = "DebugTraceCollect([PrettyObject({'x': (X(x=1,y=2),), 'msg': None, 'child': DebugTraceCollect([PrettyObject({'x': X(x=1,y=2), 'msg': 'object with __slots__', 'child': DebugTraceCollect([PrettyObject({'x': 'UtilTest.test_uniqueHash.<locals>.X', 'msg': None, 'child': None}), PrettyObject({'x': 'x', 'msg': None, 'child': None}), PrettyObject({'x': 1, 'msg': None, 'child': None}), PrettyObject({'x': 'y', 'msg': None, 'child': None}), PrettyObject({'x': 2, 'msg': None, 'child': None})])})])})])"
|
|
206
|
+
self.assertEqual(trace1,r)
|
|
207
|
+
r = "DebugTraceCollect([PrettyObject({'x': (X(x=1,y=3),), 'msg': None, 'child': DebugTraceCollect([PrettyObject({'x': X(x=1,y=3), 'msg': 'object with __slots__', 'child': DebugTraceCollect([PrettyObject({'x': 'UtilTest.test_uniqueHash.<locals>.X', 'msg': None, 'child': None}), PrettyObject({'x': 'x', 'msg': None, 'child': None}), PrettyObject({'x': 1, 'msg': None, 'child': None}), PrettyObject({'x': 'y', 'msg': None, 'child': None}), PrettyObject({'x': 3, 'msg': None, 'child': None})])})])})])"
|
|
208
|
+
self.assertEqual(trace2,r)
|
|
209
|
+
r = "DebugTraceCollect([PrettyObject({'x': (X(x=1,y=2,_z=2),), 'msg': None, 'child': DebugTraceCollect([PrettyObject({'x': X(x=1,y=2,_z=2), 'msg': 'object with __slots__', 'child': DebugTraceCollect([PrettyObject({'x': 'UtilTest.test_uniqueHash.<locals>.X', 'msg': None, 'child': None}), PrettyObject({'x': 'x', 'msg': None, 'child': None}), PrettyObject({'x': 1, 'msg': None, 'child': None}), PrettyObject({'x': 'y', 'msg': None, 'child': None}), PrettyObject({'x': 2, 'msg': None, 'child': None})])})])})])"
|
|
210
|
+
self.assertEqual(trace3,r)
|
|
211
|
+
r = "DebugTraceCollect([PrettyObject({'x': (X(x=1,y=2,_z=2),), 'msg': None, 'child': DebugTraceCollect([PrettyObject({'x': X(x=1,y=2,_z=2), 'msg': 'object with __slots__', 'child': DebugTraceCollect([PrettyObject({'x': 'UtilTest.test_uniqueHash.<locals>.X', 'msg': None, 'child': None}), PrettyObject({'x': 'x', 'msg': None, 'child': None}), PrettyObject({'x': 1, 'msg': None, 'child': None}), PrettyObject({'x': 'y', 'msg': None, 'child': None}), PrettyObject({'x': 2, 'msg': None, 'child': None}), PrettyObject({'x': '_z', 'msg': None, 'child': None}), PrettyObject({'x': 2, 'msg': None, 'child': None})])})])})])"
|
|
212
|
+
self.assertEqual(trace3p,r)
|
|
213
|
+
|
|
214
|
+
o1 = PrettyObject(x=1)#, y=Object())
|
|
215
|
+
o2 = PrettyObject(x=1)#, y=Object())
|
|
216
|
+
o3 = PrettyObject(x=1)#, y=Object())
|
|
217
|
+
o1._test = 2
|
|
218
|
+
o1.__test = 3
|
|
219
|
+
o2._test = 2
|
|
220
|
+
|
|
221
|
+
self.assertTrue( not hasattr(o3, "_test" ) )
|
|
222
|
+
self.assertTrue( not hasattr(o3, "__test" ) )
|
|
223
|
+
|
|
224
|
+
hashbase = uniqueHash( o1 )
|
|
225
|
+
hashprot = uniqueHashProtected( o1 )
|
|
226
|
+
hashpriv = uniqueHashPrivate( o1 )
|
|
227
|
+
|
|
228
|
+
self.assertEqual( uniqueHash( o2 ) , hashbase )
|
|
229
|
+
self.assertEqual( uniqueHash( o3 ) , hashbase )
|
|
230
|
+
self.assertEqual( uniqueHashProtected( o2 ) , hashprot )
|
|
231
|
+
self.assertNotEqual( uniqueHashProtected( o3 ) , hashprot )
|
|
232
|
+
self.assertNotEqual( uniqueHashPrivate( o2 ) , hashpriv )
|
|
233
|
+
self.assertNotEqual( uniqueHashPrivate( o3 ) , hashpriv )
|
|
234
|
+
|
|
235
|
+
debug_trace = DebugTraceCollect()
|
|
236
|
+
u1 = uniqueHash( o1, debug_trace=debug_trace )
|
|
237
|
+
r = "[PrettyObject({'x': (PrettyObject({'x': 1, '_test': 2, '_UtilTest__test': 3}),), 'msg': None, 'child': DebugTraceCollect([PrettyObject({'x': PrettyObject({'x': 1, '_test': 2, '_UtilTest__test': 3}), 'msg': None, 'child': DebugTraceCollect([PrettyObject({'x': ('x', 1), 'msg': None, 'child': DebugTraceCollect([PrettyObject({'x': 'x', 'msg': None, 'child': None}), PrettyObject({'x': 1, 'msg': None, 'child': None})])})])})])})]"
|
|
238
|
+
self.assertEqual( str(debug_trace), r)
|
|
239
|
+
|
|
240
|
+
# test functions
|
|
241
|
+
f1 = lambda x : x*x
|
|
242
|
+
f2 = lambda x : x*x
|
|
243
|
+
f3 = lambda x : x+2
|
|
244
|
+
|
|
245
|
+
u0 = uniqueHashF() # nothing
|
|
246
|
+
u1 = uniqueHashF(f1)
|
|
247
|
+
u2 = uniqueHashF(f2)
|
|
248
|
+
u3 = uniqueHashF(f3)
|
|
249
|
+
self.assertEqual(u0,"64550d6ffe2c0a01a14aba1eade0200c")
|
|
250
|
+
self.assertEqual(u1,"4d85d642c797206fabc75eb34b491fa6")
|
|
251
|
+
self.assertEqual(u2,"4d85d642c797206fabc75eb34b491fa6")
|
|
252
|
+
self.assertEqual(u2,u1)
|
|
253
|
+
self.assertEqual(u3,"27e1444f90196d3589d3642ea4c6c580")
|
|
254
|
+
self.assertNotEqual(u3,u1)
|
|
255
|
+
|
|
256
|
+
u1 = uniqueHash(f1)
|
|
257
|
+
u2 = uniqueHash(f2)
|
|
258
|
+
u3 = uniqueHash(f3)
|
|
259
|
+
self.assertEqual(u1,u0)
|
|
260
|
+
self.assertEqual(u2,u0)
|
|
261
|
+
self.assertEqual(u3,u0)
|
|
262
|
+
|
|
263
|
+
# test ignore warning
|
|
264
|
+
debug_trace = DebugTraceCollect()
|
|
265
|
+
u1 = uniqueHash( f1, debug_trace=debug_trace )
|
|
266
|
+
ignore = debug_trace[0].child[0].msg
|
|
267
|
+
self.assertEqual( ignore, "Ignored function: UtilTest.test_uniqueHash.<locals>.<lambda>")
|
|
268
|
+
|
|
269
|
+
# globals, defaults
|
|
270
|
+
def test1():
|
|
271
|
+
z=10
|
|
272
|
+
|
|
273
|
+
def f(x,y):
|
|
274
|
+
return x*y*z
|
|
275
|
+
f1 = f # otherwise simply the name will trigger a difference
|
|
276
|
+
|
|
277
|
+
def f(x,y=2):
|
|
278
|
+
return x*y*z
|
|
279
|
+
f2 = f
|
|
280
|
+
|
|
281
|
+
def f(x,*,y=2):
|
|
282
|
+
return x*y*z
|
|
283
|
+
f3 = f
|
|
284
|
+
|
|
285
|
+
def f(x,y=2):
|
|
286
|
+
return x*y*z
|
|
287
|
+
f4 = f
|
|
288
|
+
|
|
289
|
+
u1 = uniqueHashF( f1 )#, debug_trace=DebugTraceVerbose())
|
|
290
|
+
u2 = uniqueHashF( f2 )#, debug_trace=DebugTraceVerbose())
|
|
291
|
+
u3 = uniqueHashF( f3 )#, debug_trace=DebugTraceVerbose())
|
|
292
|
+
u4 = uniqueHashF( f4 )#, debug_trace=DebugTraceVerbose())
|
|
293
|
+
|
|
294
|
+
z=11
|
|
295
|
+
|
|
296
|
+
u5 = uniqueHashF( f4 )
|
|
297
|
+
|
|
298
|
+
self.assertEqual( u1, "c482c286053bcb616ac722b26fc18215")
|
|
299
|
+
self.assertEqual( u2, "c73e092776d82148feeb84ea49f36c4f")
|
|
300
|
+
self.assertEqual( u3, "415545f7c1fee37faa157f66bce038a2" )
|
|
301
|
+
self.assertEqual( u4, u2 ) #
|
|
302
|
+
self.assertNotEqual( u5, u3 ) # global 'z' different !
|
|
303
|
+
test1()
|
|
304
|
+
|
|
305
|
+
# closure
|
|
306
|
+
def test1(z):
|
|
307
|
+
def f(x):
|
|
308
|
+
return x*z
|
|
309
|
+
return f
|
|
310
|
+
f1 = test1(1.)
|
|
311
|
+
f2 = test1(1.)
|
|
312
|
+
f3 = test1(2.)
|
|
313
|
+
u1 = uniqueHashF( f1 )
|
|
314
|
+
u2 = uniqueHashF( f2 )
|
|
315
|
+
u3 = uniqueHashF( f3 )
|
|
316
|
+
|
|
317
|
+
self.assertEqual( u1, "5cc72c1800a14d52b5ae7758575e3c84" )
|
|
318
|
+
self.assertEqual( u2, u1 )
|
|
319
|
+
self.assertNotEqual( u3, u1 )
|
|
320
|
+
|
|
321
|
+
# time
|
|
322
|
+
tz = ZoneInfo("America/New_York")
|
|
323
|
+
tz2 = ZoneInfo("Asia/Tokyo")
|
|
324
|
+
tz3 = ZoneInfo("GMT")
|
|
325
|
+
plain = datetime.datetime( year=1974, month=3, day=17, hour=16, minute=2, second=3 )
|
|
326
|
+
timz = datetime.datetime( year=1974, month=3, day=17, hour=16, minute=2, second=3, tzinfo=tz )
|
|
327
|
+
micro = datetime.datetime( year=1974, month=3, day=17, hour=16, minute=2, second=3, microsecond=3232 )
|
|
328
|
+
lots = datetime.datetime( year=1974, month=3, day=17, hour=16, minute=2, second=3, microsecond=3232, tzinfo=tz )
|
|
329
|
+
lots2 = datetime.datetime( year=1974, month=3, day=17, hour=16, minute=2, second=3, microsecond=0, tzinfo=tz2 )
|
|
330
|
+
lots3 = datetime.datetime( year=1974, month=3, day=17, hour=16, minute=2, second=3, microsecond=0, tzinfo=tz3 )
|
|
331
|
+
|
|
332
|
+
plainhash = uniqueHash(plain)
|
|
333
|
+
self.assertEqual( plainhash, "9142b8ffd5f12e83f94c261da5935674") # (1)
|
|
334
|
+
self.assertEqual( uniqueHash(timz), "82b58ccbe7a14a31d86aaa4ff5148ddb")
|
|
335
|
+
self.assertEqual( uniqueHash(micro), "89c16cd9eb05f2bc92ddab0a9cce96bc")
|
|
336
|
+
self.assertEqual( uniqueHash(lots), "8ad007ff97156db6ab709cead44ca5c0")
|
|
337
|
+
self.assertEqual( uniqueHash(lots2), "045730565f1ae4f07e28f2d80a4b98c9")
|
|
338
|
+
self.assertEqual( uniqueHash(lots3), plainhash) # same as (1)
|
|
339
|
+
|
|
340
|
+
hash1 = uniqueHash(datetime.date( year=1974, month=3, day=17 ))
|
|
341
|
+
self.assertEqual( hash1, "5ea7ea54a4a5e16b41be046d62092e3d") # (1)
|
|
342
|
+
self.assertEqual( uniqueHash(plain.date()),hash1 ) # same as (1)
|
|
343
|
+
self.assertEqual( uniqueHash(datetime.date( year=1974, month=3, day=2 )), "21c4c10490cb41431c6c444c4d49c287")
|
|
344
|
+
self.assertEqual( uniqueHash(datetime.date( year=2074, month=3, day=2 )), "5498e6f3519c815393f2da1d42dda196")
|
|
345
|
+
self.assertEqual( uniqueHash(datetime.time( hour=16, minute=2, second=3, microsecond=0 )), "e10f649dae2851b77f63b196b63c01c5")
|
|
346
|
+
self.assertEqual( uniqueHash(datetime.time( hour=16, minute=2, second=3 )), "e10f649dae2851b77f63b196b63c01c5")
|
|
347
|
+
|
|
348
|
+
empty = uniqueHash(datetime.timedelta())
|
|
349
|
+
self.assertEqual( empty, "d566dfb39f20d549d1c0684e94949c71")
|
|
350
|
+
self.assertEqual( uniqueHash(datetime.timedelta( seconds=0 ) ), empty)
|
|
351
|
+
self.assertEqual( uniqueHash(datetime.timedelta( days=0, seconds=0 ) ), empty)
|
|
352
|
+
self.assertEqual( uniqueHash(datetime.timedelta( days=0, seconds=0, microseconds=0 ) ), empty)
|
|
353
|
+
self.assertEqual( uniqueHash(datetime.timedelta( days=0 ) ), empty)
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
hash1 = uniqueHash(datetime.timedelta( days=2, seconds=2+60*3+60*60*4, microseconds=1 ))
|
|
357
|
+
self.assertEqual( hash1, "8fd3e750ef3e59c2526070d6ccdc3e56") # (1)
|
|
358
|
+
self.assertEqual( uniqueHash(datetime.timedelta( days=0, seconds=2+60*3+60*60*4+2*24*60*60, microseconds=1 )), hash1 )
|
|
359
|
+
hash2 = uniqueHash(datetime.timedelta( days=2, seconds=2+60*3+60*60*4, microseconds=0 ))
|
|
360
|
+
self.assertEqual( hash2, "35fcb34c6f2e3ff5f0c58e6912916990") # !=(1)
|
|
361
|
+
self.assertNotEqual( hash2, hash1 )
|
|
362
|
+
|
|
363
|
+
hash1 = uniqueHash(datetime.timedelta( seconds=0, microseconds=1 ))
|
|
364
|
+
hash2 = uniqueHash(datetime.timedelta( seconds=0, microseconds=-1 ))
|
|
365
|
+
self.assertEqual( hash1, "8b56e0808aad40a2722eadd13279e619")
|
|
366
|
+
self.assertEqual( hash2, "5a7f33307048636c4071967f95c40a85")
|
|
367
|
+
self.assertNotEqual( hash2, hash1 )
|
|
368
|
+
|
|
369
|
+
self.assertEqual( uniqueHash( pd.DataFrame({'a':[1,2,3],'b':[10,20,30],'c':[100,200,300]}) ), "6825de2c38cab22de057211b1ad01ce8")
|
|
370
|
+
self.assertEqual( uniqueHash( pd.DataFrame({'a':[1,2,3],'b':[10,20,30],'c':[100,200,300]}, index=[1,2,3]) ), "d8f7b3a49efe9c2961e54b8feb8f1eae")
|
|
371
|
+
hash1 = uniqueHash( pd.DataFrame({'a':[1.,2.,3.],'b':[10.,20.,30.],'c':[100.,200.,300.]}, index=[1,2,3]) )
|
|
372
|
+
self.assertEqual( hash1, "13c5e3d147fcca03fcb1de6b6b1bcef0") # (1)
|
|
373
|
+
|
|
374
|
+
df = pd.DataFrame({'a':[1,2,3],'b':[10,20,30],'c':[100,200,300]}, index=[1,2,3], dtype=np.float64)
|
|
375
|
+
hash2 = uniqueHash( df )
|
|
376
|
+
self.assertEqual(hash2, hash1)
|
|
377
|
+
df.attrs["test"] = 1
|
|
378
|
+
hash2 = uniqueHash( df )
|
|
379
|
+
self.assertEqual( hash2, "c038dd30487f158bd8ff638941883e8b") # != (1)
|
|
380
|
+
self.assertNotEqual( hash2, hash1 )
|
|
381
|
+
del df.attrs["test"]
|
|
382
|
+
hash2 = uniqueHash( df )
|
|
383
|
+
self.assertEqual( hash2, hash1) # == (1)
|
|
384
|
+
|
|
385
|
+
np.random.seed( 12312 )
|
|
386
|
+
a = np.exp( np.random.normal( size=(20,10) ).astype( np.float64 ) )
|
|
387
|
+
b = a.astype(np.float32, copy=True)
|
|
388
|
+
c = a.astype(np.float16, copy=True)
|
|
389
|
+
self.assertEqual( uniqueHash( a ), "1bac6e6c06b9ed1603670cb6895c1aaa")
|
|
390
|
+
flat = uniqueHash( a.flatten() )
|
|
391
|
+
self.assertEqual( flat, "a002ff5a0d035f6dc30c8a366b5cdff3") # (1)
|
|
392
|
+
self.assertEqual( uniqueHash( b ), "4dcc1a611fd718e957ef8e6bcff7a326")
|
|
393
|
+
self.assertEqual( uniqueHash( c ), "e5a2889b6aafe33389890259e73407a5")
|
|
394
|
+
|
|
395
|
+
np.random.seed( 12312 )
|
|
396
|
+
a2 = np.exp( np.random.normal( size=(20*10,) ).astype( np.float64 ) )
|
|
397
|
+
self.assertEqual( uniqueHash( a2 ), flat ) # == (1)
|
|
398
|
+
|
|
399
|
+
# named
|
|
400
|
+
# -----
|
|
401
|
+
|
|
402
|
+
namedUniqueHash32_8 = namedUniqueHashExt( max_length=32, id_length=8, parse_underscore="protected" )
|
|
403
|
+
filenamedUniqueHash32_8_1 = namedUniqueHashExt( max_length=32, id_length=8, parse_underscore="protected", filename_by=DEF_FILE_NAME_MAP )
|
|
404
|
+
|
|
405
|
+
o1 = Object()
|
|
406
|
+
o2 = Object()
|
|
407
|
+
o3 = Object()
|
|
408
|
+
o2._test = 2
|
|
409
|
+
o3._test = 2
|
|
410
|
+
o3.__test = 3
|
|
411
|
+
|
|
412
|
+
hash1 = namedUniqueHash32_8( "object:", o1 )
|
|
413
|
+
hash1f1 = filenamedUniqueHash32_8_1( "object:", o1 )
|
|
414
|
+
self.assertEqual( hash1, "object: aa6af41a" )
|
|
415
|
+
self.assertNotEqual( namedUniqueHash32_8( "object;", o1 ), hash1 ) # <-- no filename translation
|
|
416
|
+
|
|
417
|
+
self.assertEqual( hash1f1, "object; cb0c59d8" )
|
|
418
|
+
self.assertEqual( namedUniqueHash32_8( "object;", o1 ), hash1f1 ) # <-- with filename translation: ':' --> ';'
|
|
419
|
+
self.assertNotEqual( namedUniqueHash32_8( "object;", o1 ), hash1 ) # <-- filename translation
|
|
420
|
+
|
|
421
|
+
hashprot = namedUniqueHash32_8( "object:", o2 )
|
|
422
|
+
self.assertEqual( hashprot, "object: 5f758a65" )
|
|
423
|
+
self.assertEqual( namedUniqueHash32_8( "object:", o3 ), hashprot )
|
|
424
|
+
|
|
425
|
+
# __unique_hash__
|
|
426
|
+
# ----------------
|
|
427
|
+
|
|
428
|
+
class A(object):
|
|
429
|
+
""" No ID. Because members are protected, by default, this object is not properly hashed """
|
|
430
|
+
def __init__(self, seed = 12312, size = (10,) ):
|
|
431
|
+
np.random.seed( seed )
|
|
432
|
+
self._seed = seed
|
|
433
|
+
self._size = size
|
|
434
|
+
self.__data = np.random.normal( size=size ) # we do not want to hash this: it is determined by the other two parameters
|
|
435
|
+
@property
|
|
436
|
+
def data(self):
|
|
437
|
+
return self.__data
|
|
438
|
+
|
|
439
|
+
class B(A):
|
|
440
|
+
""" Compute unique ID at construction time """
|
|
441
|
+
def __init__(self, seed = 12312, size = (100,) ):
|
|
442
|
+
super().__init__(seed, size)
|
|
443
|
+
self.__unique_hash__ = uniqueHash( self._seed, self._size )
|
|
444
|
+
|
|
445
|
+
class C(A):
|
|
446
|
+
""" Use the uniqueHash object passed to this function to compute the hash """
|
|
447
|
+
def __unique_hash__( self, uniqueHash, debug_trace ):
|
|
448
|
+
return uniqueHash( self._seed, self._size, debug_trace=debug_trace )
|
|
449
|
+
|
|
450
|
+
class D(A):
|
|
451
|
+
""" Just return the members we care about """
|
|
452
|
+
def __unique_hash__( self, uniqueHash, debug_trace ):
|
|
453
|
+
return ( self._seed, self._size )
|
|
454
|
+
|
|
455
|
+
hash1 = uniqueHash( A() )
|
|
456
|
+
hash2 = uniqueHash( B() )
|
|
457
|
+
hash3 = uniqueHash( C() )
|
|
458
|
+
hash4 = uniqueHash( D() )
|
|
459
|
+
self.assertEqual( hash1, "30a4ec8c64e0d78d6706df2ae9233c21" )
|
|
460
|
+
self.assertEqual( hash2, "ae7cc6d56596eaa20dcd6aedc6e89d85" )
|
|
461
|
+
self.assertEqual( hash3, "5e387f9e86426319577d2121a4e1437b" )
|
|
462
|
+
self.assertEqual( hash4, "c9b449e95339458df155752acadbebb1" )
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
if __name__ == '__main__':
|
|
467
|
+
unittest.main()
|
|
468
|
+
|
|
469
|
+
|