Python从一级目录导入模块的正确方法是什么?该目录是一个包含所有这些模块的Python包,我有一个子目录,其中包含需要这些模块的代码.
以下工作正常,但这只是一个黑客.我想要推荐/ pythonic方式.
import sys
sys.path.append("../")
from fruit import Fruit
print("OK")
Run Code Online (Sandbox Code Playgroud)
目录结构:
pkg1
__init__.py
fruit.py
+sub_pkg
__init__.py
recipe.py
Run Code Online (Sandbox Code Playgroud)
fruit.py的内容
class Fruit:
def get_name(self):
print("Fruit name")
Run Code Online (Sandbox Code Playgroud)
sub_pkg/recipe.py
..的内容只是一个导入行:
from fruit import Fruit
Run Code Online (Sandbox Code Playgroud)
当我跑:
python recipe.py
Run Code Online (Sandbox Code Playgroud)
它给出了以下错误.
Traceback (most recent call last):
File "recipe.py", line 2, in <module>
from fruit import Fruit
ImportError: No module named fruit
Run Code Online (Sandbox Code Playgroud)
我也试过:from pkg1.fruit import Fruit
,不行.还看了其他类似的问题..
python -m recipe.py
或python -m sub_pkg/recipe.py
没有奏效.
我正在尝试使用模拟库来测试一段代码。在此代码中,用户原始输入在 for 循环中被接受,如下所示。我已经编写了test_apple_record
可以为托盘编号提供单个用户输入值的测试用例。
但是,对于 for 循环中的每次迭代,它只采用与预期相同的值 (5)。
问题是:如何为每次迭代提供不同的值?例如,对于 i=0、1 和 2,托盘编号的具体值分别为 5、6 和 7。
class SomeClass(unittest.TestCase):
def apple_counter(self):
apple_record = {}
for i in range(3):
apple_tray = input("enter tray number:")
apple_record[apple_tray] = (i+1)*10
print("i=%d, apple_record=%s"%(i, apple_record))
def test_apple_record(self):
with mock.patch('builtins.input', return_value='5'):
self.apple_counter()
Run Code Online (Sandbox Code Playgroud) 一个初学者级的问题..试图了解我如何才能最好地使用内置的unittest
。在下面的琐碎示例中,方法define_food选择一个食物,然后调用food.cut()
方法。将来,此方法可能返回Drink
object的实例。该#commented code
指示一个可能的未来的实施。在这种情况下,self.milk
将没有定义cut方法。
我想为consume_food
和pick_food
方法添加单元测试。我想先对原始实现进行此操作,然后在添加self.milk
功能后对其进行更改。
编辑:目的是为现有的api编写单元测试,以便我可以捕获任何此类更改(即,没有Drink.cut
方法),迫使我更新方法和单元测试。
有人可以帮我演示如何为该示例编写单元测试吗?
class Fruit:
def cut(self):
print("cut the fruit")
class Drink:
def pour(self):
print("pour the drink")
class A:
def __init__(self):
self.apple = Fruit()
self.banana=Fruit()
#self.milk = Drink()
#self.liquid_diet = True
def consume_food(self):
food = pick_food()
food.cut()
print("consuming the food")
def pick_food(self):
return self.apple
#if self.liquid_diet: return self.milk
#return self.apple
Run Code Online (Sandbox Code Playgroud) 这个问题的后续。
我正在 for 循环中接受用户输入,并编写了一个测试用例test_apple_record
。在此 for 循环中,它查询一个self.dispatch_requested()
可以随机返回 True 或 False 的方法(未显示)。基于这个答案,代码要求用户进行另一个输入——托盘应该发送到哪里。
我正在使用side_effect
的参数mock.patch
。如何使用模拟自动传递酒店号码作为用户输入?我仍然想继续将数字传递[5, 6, 7]
给 for 循环,但现在还想根据响应传递酒店号码self.dispatch_requested()
谢谢
class SomeClass(unittest.TestCase):
def apple_counter(self):
apple_record = {}
for i in range(3):
apple_tray = input("enter tray number:")
apple_record[apple_tray] = (i+1)*10
print("i=%d, apple_record=%s"%(i, apple_record))
if self.dispath_requested():
number = input("Enter Hotel number to dispatch this tray:")
update_hotel_record(number, apple_tray)
def update_hotel_record(self, number, tray):
self.hotel_record[number] = tray
def test_apple_record(self):
with mock.patch('builtins.input', side_effect=[5, 6, 7]):
self.apple_counter()
Run Code Online (Sandbox Code Playgroud)