我试图 classmethod用functools.lru_cache. 我的尝试失败了:
import functools
class K:
@functools.lru_cache(maxsize=32)
@classmethod
def mthd(i, stryng: str): \
return stryng
obj = K()
Run Code Online (Sandbox Code Playgroud)
错误消息来自functools.lru_cache:
TypeError: the first argument must be callable
Run Code Online (Sandbox Code Playgroud) python static-methods decorator python-3.x python-decorators
我希望能够解压我自己的类似字典的类。
class FauxDict:
def __getitem__(self, key):
return 99
def __iter__(self):
return range(0, 1)
def to_map(self):
return map(lambda x: True, range(0, 2))
def bar(**kwargs):
pass
dct = {"x":1, "y":2}
bar(**dct) # no error
dct = FauxDict()
bar(**dct) # error
dct = FauxDict()
bar(**dct.to_map()) # error
Run Code Online (Sandbox Code Playgroud)
错误是:
bar(**dct) # error
TypeError: bar() argument after ** must be a mapping, not FauxDict
bar(**dct.to_map()) # error
TypeError: bar() argument after ** must be a mapping, not map
Run Code Online (Sandbox Code Playgroud)
另外,哪个python类在技术上有资格作为映射?
我想要一个try-block,以便-block 内部引发的任何异常都try无法处理。这样我就可以编写一个try块来为将来做准备。有一天,我将写一些有意义的错误处理。但是,我还没有真正的except陈述。以下是作品,但很难看
_ = type("", (Exception,), dict())
try:
lizard = [1, 2, 3]
y = z + w
print(lizard[983])
except _:
print("I hope this string never prints")
Run Code Online (Sandbox Code Playgroud) 我想在类似于我在下面发布的图像中找到空白区域(黑色区域),其中散布着大小随机的块。
通过空白空间,我指的是这种可能的开放领域(我对该区域没有特别的下限,但我想提取图像中出现的前 3-4 个最大的区域。)对几何形状也没有限制他们可以拿走,但这些空位不能包含任何蓝色方块。
解决这个问题的最佳方法是什么?
到目前为止我所做的:
我的原始图像实际上是这样的。我确定了所有的点,根据一定的距离阈值对它们进行分组,并在它们周围应用凸包。我不确定如何进一步进行。任何帮助将不胜感激。谢谢!
在这里提出了这个问题,但是我不想要python特定的解决方案。它应该可以在JavaScript和任何其他主流语言中工作。它必须是一个正则表达式。
假设分隔符为“ !”
下表显示了一些示例输入的期望行为
+------------------------+--------+
| INPUT | OUTPUT |
+------------------------+--------+
| "aaaa!bbbb" | "aaaa" |
| "aaaa" | "aaaa" |
| "aaaa!bbbb!ccccc!dddd" | "aaaa" |
| "aaaa!" | "aaaa" |
| "!aaaaa" | "" |
+------------------------+--------+
Run Code Online (Sandbox Code Playgroud)
我尝试以下无济于事:
“ ^[^!]*$(!)”
python ×5
python-3.x ×3
algorithm ×1
decorator ×1
exception ×1
opencv ×1
regex ×1
regex-group ×1
try-catch ×1