如何检查模块/类/方法是否已更改并记录更改?

发布于 2024-09-30 19:14:32 字数 823 浏览 1 评论 0原文

我试图比较两个模块/类/方法并找出类/方法是否已更改。我们允许用户更改类/方法,并且在处理后,我们使这些更改持久化,而不覆盖旧的类/方法。然而,在提交新类之前,我们需要确定代码是否已更改,以及方法的功能是否已更改,例如输出不同,并且相同输入数据的性能也有所不同。我对性能变化感到满意,但我的问题是代码的变化以及如何记录 - 发生了什么变化。我写了类似下面的内容

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-10-07 19:14:32

您应该使用版本控制程序来帮助管理开发。这是您从 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.

神回复 2024-10-07 19:14:32

如果我有很多方法,我会看到什么
这是循环所有方法。
可以在类或模块中执行此操作
等级?

我不会问你为什么要做这样的事?但是是的,你可以这是一个例子

import inspect
import collections

# Here i will loop over all the function in a module

module = __import__('inspect')   # this is fun !!!

# Get all function in the module.
list_functions = inspect.getmembers(module, inspect.isfunction)

# Get classes and methods correspond .
list_class = inspect.getmembers(module, inspect.isclass)

class_method = collections.defaultdict(list)

for class_name, class_obj in list_class:
    for method in inspect.getmembers(class_obj, inspect.ismethod):
        class_method[class_name].append(method)

if i have many methods, what i see
here is looping over all methods.
Possible to do this at class or module
level?

i will not ask why you want to do such thing ? but yes you can here is an example

import inspect
import collections

# Here i will loop over all the function in a module

module = __import__('inspect')   # this is fun !!!

# Get all function in the module.
list_functions = inspect.getmembers(module, inspect.isfunction)

# Get classes and methods correspond .
list_class = inspect.getmembers(module, inspect.isclass)

class_method = collections.defaultdict(list)

for class_name, class_obj in list_class:
    for method in inspect.getmembers(class_obj, inspect.ismethod):
        class_method[class_name].append(method)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文