如何转换的svg到png,在Python?我正在存储svg一个实例StringIO.我应该使用pyCairo库吗?我该如何编写代码?
对于一些简单的线程相关代码,即:
\nimport threading\n\n\na = 0\nthreads = []\n\n\ndef x():\n global a\n for i in range(1_000_000):\n a += 1\n\n\nfor _ in range(10):\n thread = threading.Thread(target=x)\n threads.append(thread)\n thread.start()\n\n\nfor thread in threads:\n thread.join()\n\n\nprint(a)\nassert a == 10_000_000\nRun Code Online (Sandbox Code Playgroud)\n根据 Python 版本,我们得到了不同的行为。
\n对于 3.10,输出为:
\n\xe2\x9d\xaf python3.10 b.py\n10000000\nRun Code Online (Sandbox Code Playgroud)\n对于 3.9,输出为:
\n\xe2\x9d\xaf python3.9 b.py\n2440951\nTraceback (most recent call last):\n File "/Users/romka/t/threads-test/b.py", line 24, in <module>\n assert a == 10_000_000\nAssertionError\nRun Code Online (Sandbox Code Playgroud)\n由于我们没有获取任何锁,对我来说,3.9 的结果是显而易见的并且是预期的。问题是为什么 3.10 得到了“正确”的结果,而不应该得到“正确”的结果?
\n我正在查看 Python 3.10 的变更日志,没有任何与线程或 GIL 相关的内容可以带来这样的结果。
\npython multithreading python-multithreading python-internals
在Python 3.1中,我在builtins模块中有一个新的内置函数:
__build_class__(...)
__build_class__(func, name, *bases, metaclass=None, **kwds) -> class
Internal helper function used by the class statement.
Run Code Online (Sandbox Code Playgroud)
这个功能有什么作用?如果它是内部的,为什么必须在内置?这个type(name, bases, dict)功能有什么区别?
我刚刚注意到相对较新的开源noSQL数据库" HyperDex "在SO中没有提及问题 - 是否有人使用它?它与其他noSQL引擎相比如何?
反转元组并反转列表会返回不同类型的对象:
>>> reversed((1,2))
<reversed at 0x7fffe802f748>
>>> reversed([1,2])
<list_reverseiterator at 0x7fffebdd4400>
Run Code Online (Sandbox Code Playgroud)
他们也一样dir.这两种类型都不是另一种类型的子类.
这是为什么?一个人可以做什么,另一个不能?
我想知道Python 3中的新超级是如何实现的.
在我做了一个小例子之后,这个问题就出现在了我脑海中,我得到了一个奇怪的错误.我正在使用Pyutilib组件架构(PCA),我已经制作了自定义元类来驱动另一个类的创建:
from pyutilib.component.core import implements, SingletonPlugin, PluginMeta, Interface
class IPass(Interface):
pass
class __MetaPlugin(PluginMeta):
def __new__(cls, name, baseClasses, classdict):
print(cls, name, baseClasses, classdict)
if baseClasses:
baseClasses += (SingletonPlugin,)
return PluginMeta.__new__(cls, name, baseClasses, classdict)
class Pass(metaclass=__MetaPlugin):
implements(IPass)
def __init__(self, inputs=[], outputs=[]):
self.inputs = []
self.outputs = []
class A(Pass):
def __init__(self):
print(self.__class__) # <class '__main__.A'>
print(self.__class__.__class__) # <class '__main__.__MetaPlugin'>
print(PluginMeta.__class__) # <class 'type'>
super().__init__() # SystemError: super(): empty __class__ cell
#Pass.__init__(self) - this works
a = A()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误: …
根据object.__eq__()文档,默认(即在object类中)实现==如下:
True if x is y else NotImplemented
Run Code Online (Sandbox Code Playgroud)
仍然遵循的文档NotImplemented,我推断这NotImplemented意味着 Python 运行时将尝试以相反的方式进行比较。y.__eq__(x)即如果x.__eq__(y)返回则尝试NotImplemented(在运算符的情况下==)。
现在,在 python 3.9 中False打印以下代码:True
class A:
pass
print(A() == A())
print(bool(NotImplemented))
Run Code Online (Sandbox Code Playgroud)
所以我的问题如下:文档在哪里提到了NotImplemented在上下文中的特殊行为__eq__?
PS:我在CPython 源代码中找到了答案,但我想这必须/应该在文档中的某个位置。
当使用Python CTypes时,有结构,允许您在Python端克隆c结构,POINTERS对象从内存地址值创建一个软化的Python对象,并可用于通过引用来回传递对象C码.
我在文档或其他地方找不到的是当包含一个从C代码的返回指针中取消引用的Structure类的Python对象(即 - 结构的C函数分配的内存)本身被删除时会发生什么.是否释放了原始C结构的内存?如果没有怎么办?
此外 - 如果结构包含指针本身,还有由C函数分配的其他数据,该怎么办?删除Structure对象是否释放了Pointers onits成员?(我对此表示怀疑)否则 - 如何做到这一点?尝试从Python中为结构中的指针"免费"调用系统,这对我来说是崩溃的Python.
换句话说,我通过ac函数调用填充了这个结构:
class PIX(ctypes.Structure):
"""Comments not generated
"""
_fields_ = [
("w", ctypes.c_uint32),
("h", ctypes.c_uint32),
("d", ctypes.c_uint32),
("wpl", ctypes.c_uint32),
("refcount", ctypes.c_uint32),
("xres", ctypes.c_uint32),
("yres", ctypes.c_uint32),
("informat", ctypes.c_int32),
("text", ctypes.POINTER(ctypes.c_char)),
("colormap", ctypes.POINTER(PIXCOLORMAP)),
("data", ctypes.POINTER(ctypes.c_uint32))
]
Run Code Online (Sandbox Code Playgroud)
我想释放它从Python代码中消耗的内存.
__call__每当您尝试调用对象时,都会调用Python的魔术方法.Cls()()因此等于Cls.__call__(Cls()).
函数是Python中的第一类对象,这意味着它们只是可调用对象(使用__call__).然而,__call__本身是一个函数,因此它也有__call__,这又都有自己的__call__,而这又都有自己的__call__.
因此Cls.__call__(Cls()),等于Cls.__call__.__call__(Cls())和再等同于Cls.__call__.__call__.__call__(Cls())等等等等.
这个无限循环如何结束?如何__call__实际执行代码?
下面是python中使用PIL突出显示两个图像之间差异的当前工作代码.但其余的图像都是黑色的.
目前我想要显示背景以及突出显示的图像.
无论如何,我可以保持节目的背景更轻,只是突出差异.
from PIL import Image, ImageChops
point_table = ([0] + ([255] * 255))
def black_or_b(a, b):
diff = ImageChops.difference(a, b)
diff = diff.convert('L')
# diff = diff.point(point_table)
h,w=diff.size
new = diff.convert('RGB')
new.paste(b, mask=diff)
return new
a = Image.open('i1.png')
b = Image.open('i2.png')
c = black_or_b(a, b)
c.save('diff.png')
Run Code Online (Sandbox Code Playgroud)
!https://drive.google.com/file/d/0BylgVQ7RN4ZhTUtUU1hmc1FUVlE/view?usp=sharing