【问题标题】:Storing UUID in a numpy array as integers将 UUID 作为整数存储在 numpy 数组中
【发布时间】:2022-01-13 11:11:54
【问题描述】:

我必须将一堆整数格式的 UUID 存储在一个 numpy 数组中。将 UUID 转换为整数格式(128 位)无效,因为可以存储在 numpy 数组中的最大整数大小为 64 位。因此,我尝试使用字段样式将 UUID 存储为 6 个单独的整数。

但是我无法从 numpy 数组值重新创建 UUID。这是问题的一个例子。

import uuid

#generate a random uuid4

my_uuid = uuid.uuid4()
my_uuid
# UUID('bf6cc180-52e1-42fe-b3fb-b47d238ed7ce')

# encode the uuid to 6 integers with fields
my_uuid_fields = my_uuid.fields
my_uuid_fields
# (3211575680, 21217, 17150, 179, 251, 198449560475598)

# recreate the uuid, this works
uuid.UUID(fields=my_uuid_fields)
# UUID('bf6cc180-52e1-42fe-b3fb-b47d238ed7ce')

# create an array with the fields 
my_uuid_fields_arr = np.array(my_uuid_fields)
my_uuid_fields_arr
# array([3211575680,21217,17150,179,251,198449560475598])

# convert array back to tuple and check if its the same as the initial fields tuple
assert tuple(my_uuid_fields_arr) == my_uuid_fields

# this fails however
uuid.UUID(fields = tuple(my_uuid_fields_arr))

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/scratch/6084717/ipykernel_43199/1198120499.py in <module>
----> 1 uuid.UUID(fields = tuple(my_uuid_fields_arr))

/hpc/compgen/users/mpages/software/miniconda3/envs/babe/lib/python3.7/uuid.py in __init__(self, hex, bytes, bytes_le, fields, int, version, is_safe)
    192         if int is not None:
    193             if not 0 <= int < 1<<128:
--> 194                 raise ValueError('int is out of range (need a 128-bit value)')
    195         if version is not None:
    196             if not 1 <= version <= 5:

ValueError: int is out of range (need a 128-bit value)

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: python python-3.x numpy uuid


    【解决方案1】:

    tuple(my_uuid_fields_arr)np.int64 的元组,而my_uuid_fieldsint 的元组。显然uuid 无法正确处理 numpy 整数。

    只需将 numpy 整数转换为 python 整数。

    uuid.UUID(fields = tuple([int(i) for i in my_uuid_fields_arr]))
    

    当您检查任一元组中第一项的type 时,您可以验证这是问题所在。

    【讨论】:

    • 实际问题是uuid.UUIDintnumpy.int64 上执行的位移。 int(3211575680) &lt;&lt; 96 的计算结果为 254447239901898999706735475910225428480,而 np.int64(3211575680) &lt;&lt; 96 的计算结果为 0This 是一个很好的答案,可以解释为什么会发生这种情况。
    • 非常感谢!刚刚发现uuid.UUID(fields = my_uuid_fields_arr.tolist())也可以,我猜这个方法会自动转换成int
    猜你喜欢
    • 2023-03-07
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    相关资源
    最近更新 更多