如何检查模块/类/方法是否已更改并记录更改?
我试图比较两个模块/类/方法并找出类/方法是否已更改。我们允许用户更改类/方法,并且在处理后,我们使这些更改持久化,而不覆盖旧的类/方法。然而,在提交新类之前,我们需要确定代码是否已更改,以及方法的功能是否已更改,例如输出不同,并且相同输入数据的性能也有所不同。我对性能变化感到满意,但我的问题是代码的变化以及如何记录 - 发生了什么变化。我写了类似下面的内容
class TestIfClassHasChanged(unittest.TestCase):
def setUp(self):
self.old = old_class()
self.new = new_class()
def test_if_code_has_changed(self):
# simple case for one method
old_codeobject = self.old.area.func_code.co_code
new_codeobject = self.new.area.func_code.co_code
self.assertEqual(old_codeobject, new_codeobject)
,其中 area()
是两个类中的方法。但是,如果我有很多方法,我在这里看到的是循环所有方法。可以在类或模块级别执行此操作吗?
其次,如果我发现代码对象不相等,我想记录更改。我用inspect.getsource(self.old.area)
和inspect.getsource(self.new.area)
比较了两者的区别,是否有更好的这样做的方法?
I am trying to compare two modules/classes/method and to find out if the class/method has have changed. We allow users to change classes/methods, and after processing, we make those changes persistent, without overwriting the older classes/methods. However, before we commit the new classes, we need to establish if the code has changed and also if the functionally of the methods has changed e.g output differ and performance also defer on the same input data. I am ok with performance change, but my problem is changes in code and how to log - what has changed. i wrote something like below
class TestIfClassHasChanged(unittest.TestCase):
def setUp(self):
self.old = old_class()
self.new = new_class()
def test_if_code_has_changed(self):
# simple case for one method
old_codeobject = self.old.area.func_code.co_code
new_codeobject = self.new.area.func_code.co_code
self.assertEqual(old_codeobject, new_codeobject)
where area()
is a method in both classes.. However, if I have many methods, what i see here is looping over all methods. Possible to do this at class or module level?
Secondly if I find that the code objects are not equal, I would like to log the changes. I used inspect.getsource(self.old.area)
and inspect.getsource(self.new.area)
compared the two to get the difference, could there be a better way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用版本控制程序来帮助管理开发。这是您从 vc 程序中获得的特定 d= 功能之一,即跟踪更改的能力。您可以在当前源代码和之前的签入之间进行比较,以测试是否有任何更改。
You should be using a version control program to help manage development. This is one of the specific d=features you get from vc program is the ability to track changes. You can do diffs between current source code and previous check-ins to test if there were any changes.
我不会问你为什么要做这样的事?但是是的,你可以这是一个例子
i will not ask why you want to do such thing ? but yes you can here is an example