Python 3.0中的循环导入

时间:2018-10-29 02:01:46

标签: python math vector

谁能告诉我为什么这个python代码不起作用?我在网上阅读的内容似乎暗示它应该可以工作,但我不断收到ImportError。

Vector.py

class Vector(OperableEntity):
    def __init__(self, point=[]):

        super(Vector, self).__init__(VectorOperator.getInstance())
        self.point = copy.copy(point)

    def __len__(self):
        return len(self.point)

    def __getitem__(self, index):
        return self.point[index]

    def __setitem__(self, index, axis):
        self.point[index] = axis

    def __neg__(self):
        return self * -1

    def equalDimensions(self, vector):
        return len(self) == len(vector)

    def normalize(self):
        return self / self.magnitude()

    def dot(self, vector):
        if not self.equalDimensions(vector):
            raise ArithmeticError("Vectors must be of equal dimensions")

        dotProduct = 0.0

        for axis1, axis2 in zip(self, vector):
            dotProduct += axis1 * axis2

        return dotProduct

    def cross(self, vector):
        if len(self) == 3 and len(vector) == 3:
            newVec = Vector()

            newVec.point.append(self[1] * vector[2] - self[2] * vector[1])
            newVec.point.append(self[2] * vector[0] - self[0] * vector[2])
            newVec.point.append(self[0] * vector[1] - self[1] * vector[0])

            return newVec
        else:
            raise ArithmeticError("Both vectors must be 3 dimensional")

    def magnitude(self):
        magnitude = 0.0

        for axis in self:
            magnitude += axis ** 2

        return math.sqrt(magnitude)

    def angleBetween(self, vector):
        return math.acos(self.dot(vector) / (self.magnitude() * vector.magnitude()))

    def __iter__(self):
        return self.point.__iter__()

    def __str__(self):
        strRep = "vec("
        index = 0

        for axis in self:
            strRep += str(axis)

            if index == len(self) - 1:
                strRep += ")"
            else:
                strRep += ", "

            index += 1

        return strRep


x = Vector([7, 4, 2])
y = Vector([5, 3, 5])
print(x)
print(y)

VectorOperator

from cal.Operator import Operator
from cal.Vector import Vector
from cal.IncompatibleTypeException import IncompatibleTypeException


class VectorOperator(Operator):
    __instance = None

    def __init__(self):
        if VectorOperator.__instance is not None:
            raise Exception("This is a singleton class")
        else:
            VectorOperator.__instance = self

    @staticmethod
    def getInstance():
        if VectorOperator.__instance is None:
            VectorOperator.__instance = VectorOperator()

        return VectorOperator.__instance

    def doAdd(self, val1, val2):
        typeOfArg = type(val2)

        if typeOfArg == Vector:
            return VectorOperator.__subtractVectors(val1, val2)
        else:
            raise IncompatibleTypeException(Vector, typeOfArg, "+")

    @staticmethod
    def __addVectors(vector1, vector2):
        if not vector1.equalDimensions(vector2):
            raise ArithmeticError("Vectors must be of equal dimensions")

        newVec = Vector()

        for axis1, axis2 in zip(vector1, vector2):
            newVec.point.append(axis1 + axis2)

        return newVec

    def doSubtract(self, val1, val2):
        typeOfArg = type(val2)

        if typeOfArg == Vector:
            return VectorOperator.__subtractVectors(val1, val2)
        else:
            raise IncompatibleTypeException(Vector, typeOfArg, "-")

    @staticmethod
    def __subtractVectors(vector1, vector2):
        if not vector1.equalDimensions(vector2):
            raise ArithmeticError("Vectors must be of equal dimensions")

        newVec = Vector()

        for axis1, axis2 in zip(vector1, vector2):
            newVec.point.append(axis1 - axis2)

        return newVec

    def doMultiply(self, val1, val2):
        typeOfArg = type(val2)

        if (typeOfArg == int) or (typeOfArg == float):
            return VectorOperator.__multiplyRealNumber(val1, val2)
        else:
            raise IncompatibleTypeException(Vector, typeOfArg, "*")

    @staticmethod
    def __multiplyRealNumber(vector1, realNum):
        newVec = Vector()

        for axis in vector1:
            newVec.point.append(axis * realNum)

        return newVec

    def doDivide(self, val1, val2):
        typeOfArg = type(val2)

        if (typeOfArg == int) or (typeOfArg == float):
            return VectorOperator.__divideRealNumber(val1, val2)
        else:
            raise IncompatibleTypeException(Vector, typeOfArg, "/")

    @staticmethod
    def __divideRealNumber(vector1, realNum):
        newVec = Vector()

        for axis in vector1:
            newVec.point.append(axis / realNum)

        return newVec

    def doFloorDivide(self, val1, val2):
        typeOfArg = type(val2)

        if (typeOfArg == int) or (typeOfArg == float):
            return VectorOperator.__floorDivideRealNumber(val1, val2)
        else:
            raise IncompatibleTypeException(Vector, typeOfArg, "//")

    @staticmethod
    def __floorDivideRealNumber(vector1, realNum):
        newVec = Vector()

        for axis in vector1:
            newVec.point.append(axis // realNum)

        return newVec

    def doEqual(self, vector1, vector2):
        return vector1.point == vector2.point

似乎问题是由两个模块之间的循环依赖性引起的,但是我认为将“ from cal.VectorOperator import VectorOperator”放入需要的函数中就足以解决依赖性。导入也无法在模块末尾进行

0 个答案:

没有答案