小编DrF*_*k3n的帖子

如何在Python中清空列表?

以这种方式清空列表似乎很"脏":

while len(alist) > 0 : alist.pop()
Run Code Online (Sandbox Code Playgroud)

这样做有明确的方法吗?

python list

348
推荐指数
5
解决办法
45万
查看次数

python队列和多处理队列:它们的行为方式如何?

这个示例代码有效(我可以在文件中写一些内容):

from multiprocessing import Process, Queue

queue = Queue()
def _printer(self, queue):
    queue.put("hello world!!")

def _cmdDisp(self, queue):
    f = file("Cmd.log", "w")
    print >> f, queue.get()
    f.close()
Run Code Online (Sandbox Code Playgroud)

而不是这个其他样本:( errormsg:'module'对象不可调用)

import Queue

queue = Queue()
def _printer(self, queue):
    queue.put("hello world!!")

def _cmdDisp(self, queue):
    f = file("Cmd.log", "w")
    print >> f, queue.get()
    f.close()
Run Code Online (Sandbox Code Playgroud)

这个其他样本没有(我不能在文件中写一些东西):

import Queue

queue = Queue.Queue()
def _printer(self, queue):
    queue.put("hello world!!")

def _cmdDisp(self, queue):
    f = file("Cmd.log", "w")
    print >> f, queue.get()
    f.close()
Run Code Online (Sandbox Code Playgroud)

有人可以解释这些差异吗?和权利?

python queue

25
推荐指数
2
解决办法
1万
查看次数

如何使用包含空格的参数处理shell getopts

我正在寻找一种方法来处理包含空格的参数,这些空格必须由shell getopts命令解析.

while getopts ":a:i:o:e:v:u:" arg
  do
  echo "ARG is: $arg" >> /tmp/submit.log
  case "$arg" in
  a) arg1="$OPTARG" ;;
  i) arg2="$OPTARG" ;;
  o) arg3="$OPTARG" ;;
  ...
  u) argn="$OPTARG" ;;
  -) break ;;
  \?) ;;
  *) echo "unhandled option $arg" >> /tmp/submit.log ;;
  ?) echo $usage_string
     exit 1 ;;
  esac
done
Run Code Online (Sandbox Code Playgroud)

现在,如果-u具有像"STRING WITH WHITE SPACE"这样的参数,而不仅仅是触发了字符串的第一部分,而while循环不会结束.

非常感谢.

shell getopts

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

python singleton进入多处理

我如何编写代码以在进程中共享"单一"类的相同实例?

python multiprocessing

5
推荐指数
2
解决办法
4863
查看次数

Python正则表达式匹配#后跟0-7后跟##

我想截取字符串开头 \*#\*

后跟0到7之间的数字

并以:结尾 ##

所以像 \*#\*0##

但是我找不到这个的正则表达式

python regex

2
推荐指数
1
解决办法
420
查看次数

Python 复合模式异常处理和 pylint

我以这种方式实现复合模式:

1)“抽象”部分是:

class Component(object):
    """Basic Component Abstraction"""
    def __init__(self, *args, **kw):
        raise NotImplementedError("must be subclassed")

    def status(self):
        """Base Abstract method"""
        raise NotImplementedError("must be implemented")
Run Code Online (Sandbox Code Playgroud)

2)一片叶子:

class Leaf(Component):
    """Basic atomic component
    """
    def __init__(self, *args, **kw):
        self.dict = {}

    def status(self):
        """Retrieves properties
        """
        return self.dict
Run Code Online (Sandbox Code Playgroud)

问题是 pylint 当然会生成以下警告:

Leaf.__init__: __init__ method from base class 'Component' is not called
Run Code Online (Sandbox Code Playgroud)

但在我的叶子中我不能要求:

def __init__(self, *args, **kw):
    Component.__init__(self, *args, **kw)
    self.dict = {}
Run Code Online (Sandbox Code Playgroud)

没有引发异常。

我必须忽略 pylint 警告还是存在一些错误的编码?

python pylint composite

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

标签 统计

python ×5

composite ×1

getopts ×1

list ×1

multiprocessing ×1

pylint ×1

queue ×1

regex ×1

shell ×1