我知道这不是标准用例,但我需要IntEnum在Python中动态地向派生类添加元素.请注意,使用功能API动态创建Enum是不够的.我需要在现有的枚举中添加元素.我怎样才能做到这一点?
背景:对于那些想知道为什么有人想要这样做的人.我正在包装一个库,并且在库中定义了枚举的值.我可以使用库API查询名称和值.但是我不能在初始化时这样做,因为它依赖于库在用户请求时动态加载的组件.我可以在启动时加载所有组件并使用功能API在导入时创建枚举,但这很耗时并且有副作用.
我有以下代码scan_value在更新ui(progress)中的进度条时执行后台操作().scan_value迭代某个值obj,value_changed每次更改值时发出signal().由于这里不相关的原因,我必须将它包装Scanner在另一个线程的object()中.当一个按钮扫描仪被称为scan是clicked.这是我的问题...以下代码工作正常(即进度条按时更新).
# I am copying only the relevant code here.
def update_progress_bar(new, old):
fraction = (new - start) / (stop - start)
progress.setValue(fraction * 100)
obj.value_changed.connect(update_progress_bar)
class Scanner(QObject):
def scan(self):
scan_value(start, stop, step)
progress.setValue(100)
thread = QThread()
scanner = Scanner()
scanner.moveToThread(thread)
thread.start()
scan.clicked.connect(scanner.scan)
Run Code Online (Sandbox Code Playgroud)
但如果我将最后一部分更改为:
thread = QThread()
scanner = Scanner()
scan.clicked.connect(scanner.scan) # This was at the end!
scanner.moveToThread(thread)
thread.start()
Run Code Online (Sandbox Code Playgroud)
进度条仅在最后更新(我的猜测是所有内容都在同一个线程上运行).如果在将对象接收对象移动到线程之前将信号连接到插槽,是否应该无关紧要.
以下代码有效,但不确定它是否正确.几个问题:
Path或PathBuf?AsRef吗?PathBuf::from(path)为了拥有结构所拥有的路径吗?use std::fmt;
use std::path::PathBuf;
struct Example {
path: PathBuf,
}
impl fmt::Display for Example {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.path.to_str().unwrap())
}
}
impl Example {
fn new(path: &PathBuf) -> Example {
// Do something here with path.
Example {
path: PathBuf::from(path),
}
}
}
fn main() {
let x = Example::new(&PathBuf::from("test.png"));
println!("{}", x);
}
Run Code Online (Sandbox Code Playgroud)
一些上下文:我试图对应该知道自己路径的文件进行高级抽象.也许设计是完全错误的.
在这篇2011年的帖子中,有人认为dev版本不应该上传到PyPI.现在(2013年5月)仍然如此吗?如果没有,分发Python包的开发版本的建议方法是什么.
使用元类,我试图通过简化现有的实例方法来创建实例方法.问题是partial不适用于实例方法.这是我尝试实现的一个简单示例:
from functools import partial
class Aclass(object):
def __init__(self, value):
self._value = value
def complex(self, a, b):
return a + b + self._value
class Atype(type):
def __new__(cls, name, bases, attrs):
return super(Atype, cls).__new__(cls, name, (Aclass, ) + bases, attrs)
def __init__(cls, name, bases, attrs):
setattr(cls, 'simple', partial(cls.complex, b=1))
class B(metaclass=Atype):
pass
b = B(10)
print(b.complex(1, 2))
print(b.simple(1))
Run Code Online (Sandbox Code Playgroud)
输出是:
13
Traceback (most recent call last):
File "metatest.py", line 22, in <module>
print(b.simple(1))
TypeError: complex() takes exactly 3 non-keyword positional arguments (1 …Run Code Online (Sandbox Code Playgroud) 我有工作的gitosis repo.如果我做
git clone git@server:repo.git
Run Code Online (Sandbox Code Playgroud)
它正确地克隆了回购.但是,如果我这样做,它就不起作用:
git clone ssh://git@server:repo.git
Cloning into repo...
ssh: connect to host port 22: Connection refused
fatal: The remote end hung up unexpectedly
Run Code Online (Sandbox Code Playgroud)
问题是,当我使用pipII时:
pip install git+ssh://git@server:repo.git
Run Code Online (Sandbox Code Playgroud)
当然还有:
Cloning into repo...
ssh: connect to host port 22: Connection refused
fatal: The remote end hung up unexpectedly
Run Code Online (Sandbox Code Playgroud)
如果我想这样做:
pip install git+git@server:repo.git
Run Code Online (Sandbox Code Playgroud)
我明白了
ValueError: ('Expected version spec in', 'git+git@server:repo.git', 'at', '+git@server:repo.git')
Run Code Online (Sandbox Code Playgroud)
有没有办法配置pip或gitosis所以我可以让它工作?
谢谢,
(请避免'使用gitolite',如果可以,我会的话)
我有几个不同种类的对象(不同的函数名称,不同的签名),我对它们进行修补,以便有一种从不同函数访问它们的通用方法.简而言之,有一个调度程序可以获取我想要修补的对象,并根据它调用不同修补程序的对象类型.修补程序将向对象添加方法:
def patcher_of_some_type(object):
def target(self, value):
# do something and call self methods
object.target = types.MethodType(target, object)
# many more of these
Run Code Online (Sandbox Code Playgroud)
随着程序变得越来越复杂,围绕对象(或对象类)的包装似乎更好.一些修补程序共享公共代码或相互关联.但我不控制对象创建,也不控制类创建.我只得到了这些物品.即使我能做到这一点,我只想包装(或修补)某些对象,而不是所有对象.
一种解决方案可能是为现有对象添加基类,但我不确定这是多么可维护和安全.还有其他解决方案吗?
我试图将numpy ndarray子类化,但是我无法使用其他numpy类型(如masked array或matrix)进行操作.在我看来,__ array_priority__没有被尊重.作为一个例子,我创建了一个模拟重要方面的虚拟类:
import numpy as np
class C(np.ndarray):
__array_priority__ = 15.0
def __mul__(self, other):
print("__mul__")
return 42
def __rmul__(self, other):
print("__rmul__")
return 42
Run Code Online (Sandbox Code Playgroud)
我的类和普通 ndarray 之间的操作按预期工作:
>>> c1 = C((3, 3))
>>> o1 = np.ones((3, 3))
>>> print(o1 * c1)
__mul__
42
>>> print(c1 * o1)
__rmul__
42
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用矩阵(或掩码数组)时,不尊重数组优先级.
>>> m = np.matrix((3, 3))
>>> print(c1 * m)
__mul__
42
>>> print(m * c1)
Traceback (most recent call last):
...
File "/usr/lib64/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 330, in …Run Code Online (Sandbox Code Playgroud) 我们的 Intranet 中有一个自定义 Web 应用程序,允许用户以更适合我们组织的方式浏览和搜索我们的共享文件系统。与 windows 资源管理器/mac finder 相比。但是,当用户单击例如指向 word 文档的链接时,该文档会被浏览器下载然后打开。我试图提供一种更好的方法,即直接从每个用户在他自己的计算机中映射的共享文件夹中打开文件。这将使事情变得更快,并且不会污染浏览器下载文件夹。
我计划创建一个 chrome 或 firefox 扩展,它识别附加到链接的某些 css 类,将链接重新映射到共享文件系统并启动一个外部进程。知道如何实现这一目标吗?有更好的解决方案吗?
我正在制作一个程序来读取多个文件,并将每个文件的摘要写入输出文件.输出文件的大小相当大,因此将其保存在内存中并不是一个好主意.我正在尝试开发一种多处理方式.到目前为止,我能够提供的最简单的方法是:
pool = Pool(processes=4)
it = pool.imap_unordered(do, glob.iglob(aglob))
for summary in it:
writer.writerows(summary)
Run Code Online (Sandbox Code Playgroud)
do是汇总文件的函数.writer是一个csv.writer对象
但事实是,我仍然完全不了解multiprocessing.imap.这是否意味着4个摘要是并行计算的,当我读取其中一个时,第5个开始计算?
有没有更好的方法呢?
谢谢.