小编fla*_*ini的帖子

在bash中用(下划线)_替换空格的最简单方法

最近我不得不编写一个在XenServer中解析虚拟机的小脚本,因为虚拟机的名称主要是在Windows XP或Windows Server 2008中使用空格,我不得不修剪这些空格并用下划线替换它们_.我找到了一个简单的解决方案,使用sed这是一个很好的工具,当涉及到字符串操作.

echo "This is just a test" | sed -e 's/ /_/g'
Run Code Online (Sandbox Code Playgroud)

回报

This_is_just_a_test
Run Code Online (Sandbox Code Playgroud)

bash sed

85
推荐指数
2
解决办法
10万
查看次数

模拟整个python类

我正在尝试在python中进行一个简单的测试,但我无法弄清楚如何完成模拟过程.

这是类和def代码:

class FileRemoveOp(...)
    @apply_defaults
    def __init__(
            self,
            source_conn_keys,
            source_conn_id='conn_default',
            *args, **kwargs):
        super(v4FileRemoveOperator, self).__init__(*args, **kwargs)
        self.source_conn_keys = source_conn_keys
        self.source_conn_id = source_conn_id


    def execute (self, context)
          source_conn = Connection(conn_id)
          try:
              for source_conn_key in self.source_keys:
                  if not source_conn.check_for_key(source_conn_key):    
                      logging.info("The source key does not exist")  
                  source_conn.remove_file(source_conn_key,'')
          finally:
              logging.info("Remove operation successful.")
Run Code Online (Sandbox Code Playgroud)

这是我对执行功能的测试:

@mock.patch('main.Connection')
def test_remove_execute(self,MockConn):
    mock_coon = MockConn.return_value
    mock_coon.value = #I'm not sure what to put here#
    remove_operator = FileRemoveOp(...)
    remove_operator.execute(self)
Run Code Online (Sandbox Code Playgroud)

由于execute方法尝试建立连接,我需要模拟,我不想做一个真正的连接,只是返回一些mock.我该怎么做?我习惯用Java做测试,但我从来没有在python上做过..

python unit-testing magicmock

13
推荐指数
1
解决办法
8996
查看次数

pkg_resources.resource_stream在python3上失败

我正在尝试加载我的项目中使用的资源,pkg_resources但它只是抛出一个异常,说它引用"无法为没有'get_data()'的加载器执行此操作".我不确定我在这里做错了什么,或者是否pkg_resources在python 3.3上以某种方式被破坏了.我准确地使用python 3.3.3.这是我试图执行的代码

>>> import pkg_resources
>>> data = pkg_resources.resource_stream('configgenerator', 'schema_rules.yml')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/users/frank/workspace/configgenerator/env/lib/python3.3/site-packages/distribute-0.6.31-py3.3.egg/pkg_resources.py", line 931, in resource_stream
    self, resource_name
  File "/home/users/frank/workspace/configgenerator/env/lib/python3.3/site-packages/distribute-0.6.31-py3.3.egg/pkg_resources.py", line 1207, in get_resource_stream
    return StringIO(self.get_resource_string(manager, resource_name))
  File "/home/users/frank/workspace/configgenerator/env/lib/python3.3/site-packages/distribute-0.6.31-py3.3.egg/pkg_resources.py", line 1210, in get_resource_string
    return self._get(self._fn(self.module_path, resource_name))
  File "/home/users/frank/workspace/configgenerator/env/lib/python3.3/site-packages/distribute-0.6.31-py3.3.egg/pkg_resources.py", line 1289, in _get
    "Can't perform this operation for loaders without 'get_data()'"
NotImplementedError: Can't perform this operation for loaders without 'get_data()'
>>> 
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何解决这个问题?

python pkg-resources python-3.x

9
推荐指数
1
解决办法
2107
查看次数

标签 统计

python ×2

bash ×1

magicmock ×1

pkg-resources ×1

python-3.x ×1

sed ×1

unit-testing ×1