当前位置: 首页>>代码示例>>Python>>正文


Python Vector.from_size方法代码示例

本文整理汇总了Python中vector.Vector.from_size方法的典型用法代码示例。如果您正苦于以下问题:Python Vector.from_size方法的具体用法?Python Vector.from_size怎么用?Python Vector.from_size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在vector.Vector的用法示例。


在下文中一共展示了Vector.from_size方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_item_errors

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import from_size [as 别名]
def test_item_errors():
    vec = Vector.from_size(3)
    with pytest.raises(TypeError):
        vec["x"]

    with pytest.raises(TypeError):
        vec["x"] = 5
开发者ID:Potato42,项目名称:arbitrary-vector,代码行数:9,代码来源:test_vector.py

示例2: test_init

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import from_size [as 别名]
def test_init():
    # note that we try to ONLY test init here when possible, so we do things like comparing data attributes to a list

    # constructing using both args and kwargs here should give us the same result
    assert ([1, 2, 3] == 
        Vector(1, 2, 3).data ==
        Vector(x=1, y=2, z=3).data ==
        Vector(1, z=3, y=2).data)
    
    # same as kwargs, but tests that skipped-over places are filled with 0s
    assert Vector(z=4, w=2).data == [0, 0, 4, 2]

    # now we try a few combinations of creating a vector from some iterable
    assert ([1, 2, 3] ==
        Vector([1, 2, 3]).data ==
        Vector(iter([1, 2, 3])).data ==
        Vector.from_iter([1, 2, 3]).data == 
        Vector.from_iter(iter([1, 2, 3])).data)

    # now check from_size construction
    from_size = Vector.from_size(2)
    assert from_size.data == [0, 0]
    assert Vector.from_size(5, fill=42).data == [42, 42, 42, 42, 42]

    # try connecting a bunch of things together
    from_connect = Vector(from_size, 1, Vector(1, 2), [3, 5])
    assert from_connect.data == [0, 0, 1, 1, 2, 3, 5]

    # verify that from_size wasn't somehow mutated in the process
    assert from_size.data == [0, 0]

    # now lets create some vectors using from_polar
    from_polar = Vector.from_polar(42, math.pi, 0, 0)
    from_polar_3d_units = [
        Vector.from_polar(1, 0, 0), # Vector(1, 0, 0)
        Vector.from_polar(1, math.pi/2, 0), # Vector(0, 1, 0)
        Vector.from_polar(1, math.pi/2, math.pi/2) # Vector(0, 0, 1)
    ]
    
    # we won't get very accurate results, so we need to check aproximate equality
    assert all(map(aprox_equal, from_polar.data, [-42, 0, 0, 0]))

    for dim, unit in enumerate(from_polar_3d_units):
        correct_unit = Vector.from_size(3).data
        correct_unit[dim] = 1
        assert all(map(aprox_equal, unit.data, correct_unit))
开发者ID:Potato42,项目名称:arbitrary-vector,代码行数:48,代码来源:test_vector.py

示例3: test_attrs

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import from_size [as 别名]
def test_attrs():
    vec = Vector.from_size(4)
    assert vec.x == 0

    vec.x = 1
    assert vec.data == [1, 0, 0, 0]

    assert vec.yx.data == [0, 1]

    vec.yz = 42
    assert vec.data == [1, 42, 42, 0]

    assert vec.i == vec.x

    assert vec.zwxyyy.data == [42, 0, 1, 42, 42, 42]
开发者ID:Potato42,项目名称:arbitrary-vector,代码行数:17,代码来源:test_vector.py

示例4: test_items

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import from_size [as 别名]
def test_items():
    vec = Vector.from_size(5)
    assert vec[0] == 0

    vec[0] = 1
    assert vec.data == [1, 0, 0, 0, 0]

    assert vec[1, 0].data == [0, 1]

    vec[1, 2] = 42
    assert vec.data == [1, 42, 42, 0, 0]

    assert vec[1:-1:2].data == [42, 0]

    vec[1:-1] = 2 # range(*item.indices(len(self)))
    assert vec.data == [1, 2, 2, 2, 0]
开发者ID:Potato42,项目名称:arbitrary-vector,代码行数:18,代码来源:test_vector.py


注:本文中的vector.Vector.from_size方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。