首页 \ 问答 \ python 3.6中的内联注释行为与2.7不同(Inline comment behavior in python 3.6 different from 2.7)

python 3.6中的内联注释行为与2.7不同(Inline comment behavior in python 3.6 different from 2.7)

所以我将我的代码从python 2.7移动到3.6(yay!)。 但是,我意识到我的所有超长配置文件都需要修改,因为虽然这样的行在2.7中的配置文件中有效,但它不在3.6中

SCALE_PRECIPITATION = 1000.0 ; Convert from m to mm

有没有办法在python 3.6的配置文件中进行内联注释?

import sys
if sys.version_info.major == 3:
    from configparser import ConfigParser as SafeConfigParser
else:
    from ConfigParser import SafeConfigParser

parser = SafeConfigParser(inline_comment_prefixes=True)
parser.read('config_file.txt')

So I am moving my code from python 2.7 to 3.6 (yay!). However, I realized that all my super-long config files will need to be modified because while a line like this was valid in a config file in 2.7, it is not in 3.6

SCALE_PRECIPITATION = 1000.0 ; Convert from m to mm

Is there a way to have inline comment in a config file in python 3.6?

import sys
if sys.version_info.major == 3:
    from configparser import ConfigParser as SafeConfigParser
else:
    from ConfigParser import SafeConfigParser

parser = SafeConfigParser(inline_comment_prefixes=True)
parser.read('config_file.txt')

原文:https://stackoverflow.com/questions/42013110
更新时间:2022-06-21 15:06

最满意答案

你只能模拟虚拟/抽象方法,除非你使用像TypeMock这样的重型东西。

当您必须测试无法控制的代码时,您将不得不打破对该代码的依赖。 创建一个包含untestable类的方法,属性和事件的Facade。 坚持你实际使用的那些; 它会记住你必须编写的代码的大小。 针对您控制的外观的代码而不是您没有的代码。

最后一件事是使用几种技术中的一种来访问外观,这将允许您在测试期间替换模拟。 您可以使用配置文件,依赖注入框架,延迟实例化等。这样您就可以模拟您的外观并将其用于单元测试。 当然,您仍然需要进行一些集成测试,以确保您的外观与实际的COM互操作类正常工作。

要获得一些灵感,请查看System.Web.Abstractions。 它包含许多类,它们包装ASP.NET核心类以使它们可模拟。


You can only mock virtual/abstract methods, unless you use something heavy-duty like TypeMock.

When you have to test code that you don't have control over, you're going to have to break your dependency on that code. Create a facade that has the methods, properties and events of the untestable class. Stick with the ones that you actually use; it'll keep down on the size of the code you have to write. Code against the facade you control rather than the code you don't.

The last thing is to use one of the several techniques to access the the facade that will allow you to substitute a mock during tests. You can use config files, dependency injection frameworks, lazy instantiation, etc. This way you can mock out your facade and use it for unit tests. Of course, you'll still have to do some integration tests to make sure your facade works correctly with the actual COM interop class.

For some inspiration, take a look at System.Web.Abstractions. It contains many classes which wrap ASP.NET core classes to make them mockable.

相关问答

更多
  • C ++中通常不使用多重继承。 在大多数情况下,它是接口/实现的混合。 这也不是禁止的。 对(虚拟)多重继承的需求来自于从一个通用基类派生的设计决策: class Window { /* draw, show, hide, background ... */ }; class WindowWithBorder : public virtual Window { /* methods to manipulate / draw border ... */ }; class WindowWithMenu ...
  • 简短的回答是(如你所说), Bottom没有方法f() ,因此没有必要尝试调用它。 Bottom包含Left两个子对象。 它们中的每一个都继承自Base ,因此Bottom包含成员函数Left::f()和Right::f() ,但没有Bottom::f() 。 由于Bottom不会覆盖Left::f() (例如使用Right::f() ),因此Base::f()是Left::g()唯一的最终覆盖。 比照 来自C ++ 03标准的10.3.9中的示例。 The short answer is that (as ...
  • sealed可以防止任何进一步覆盖链上的虚拟方法。 你只能定义sealed的方法是过度的。 看看sealed的文档: http : //msdn.microsoft.com/en-us/library/aa645769(v=vs.71).aspx 他们举了一个密封用法的例子: using System; class A { public virtual void F() { Console.WriteLine("A.F"); } public virtual void G() ...
  • 用g++ -Wall编译g++ -Wall会给出一个警告。 或者-Wnon-virtual-dtor如果你只是想要这个警告,那么就是-Wnon-virtual-dtor 。 Compiling with g++ -Wall will give a warning about that. Or -Wnon-virtual-dtor if you just want that warning.
  • 您正在尝试模拟NameValueCollection索引器而不是属性getter。 你需要做类似的事情(注意SetupGet而不是Setup ): .SetupGet(cm => cm.AppSettings).Returns(....) 如果你想要“mocked”集合返回“任何东西”,你可能需要覆盖实际的类(检查是否有可能)。 如果您知道要查找的设置 - 从Mock返回填充的集合可能是一个选项: configurationManagerMock .SetupGet(cm => cm.Ap ...
  • 我没有使用过模拟框架,但这应该是更容易做的事情之一...... 使用Mock类扩展B类,其中只覆盖纯虚方法并在构造函数上转发。 然后实例化派生类并将其作为引用/指针传递给模拟类,这是应用程序的其余部分可能已准备好处理的事情:如果类具有纯虚函数,则应用程序通过指针以多态方式使用它和/或参考。 update/answer: I found a way around it by deriving a new class from B and adding a helper to forward the calls ...
  • 你只能模拟虚拟/抽象方法,除非你使用像TypeMock这样的重型东西。 当您必须测试无法控制的代码时,您将不得不打破对该代码的依赖。 创建一个包含untestable类的方法,属性和事件的Facade。 坚持你实际使用的那些; 它会记住你必须编写的代码的大小。 针对您控制的外观的代码而不是您没有的代码。 最后一件事是使用几种技术中的一种来访问外观,这将允许您在测试期间替换模拟。 您可以使用配置文件,依赖注入框架,延迟实例化等。这样您就可以模拟您的外观并将其用于单元测试。 当然,您仍然需要进行一些集成测试,以 ...
  • 解决问题的第一步是找到定义IHawk::EncryptedHandle位置。 这可以使用目标文件上的nm来完成,例如: nm -po *.o | c++filt | grep IHawk::EncryptedHandle | grep -v ' U ' | less 如果定义来自库,则可以添加相应的库或在相应的目录中使用*.a和*.so 。 一旦符号被定位并且在库中(因为未定义的引用来自库,很可能缺少的符号也在库中),您需要确保具有缺失符号的库在之后指定参考它的那个。 我看到它发生了一段时间,但如果符号来 ...
  • 您正在寻找一种覆盖虚拟功能的方法,以便它不再是虚拟的。 使用继承的可能方法 不幸的是,一旦声明为virtual,就无法摆脱成员函数的虚拟性。 这是C ++标准的直接结果: 10.3 / 2:如果虚拟成员函数vf在类Base和Derived类中声明, 直接或间接从Base派生,则成员函数vf具有相同的名称,参数类型列表,cv限定和refqualifier (或者没有相同的)声明Base :: vf,然后Derived :: vf 也是虚拟的 。 使用final不会解决您的问题:它只会禁止您在更多派生类中覆盖该 ...
  • 子类中的覆盖也是隐式虚拟的。 您可以再次指定virtual关键字,但它没有任何区别,尽管它通常被认为是一种很好的做法。 它在标准中绝对“定义明确”。 从C ++ 11开始,你有一个更好的选择:你可以用子类标记虚拟子类中的方法,以确保它们实际上覆盖了某些东西。 这样可以防止由于原型差异而导致实际无法覆盖父方法,并且如果更改基本虚拟原型但会忘记调整覆盖,将触发错误。 Overrides in subclasses are implicitly virtual too. You can specify the v ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。