草庐IT

type-specifier

全部标签

python - `__metaclass__ = type` 的目的是什么?

Python(仅限2?)查看变量__metaclass__的值以确定如何从类定义创建type对象。Itispossibletodefine__metaclass__atthemoduleorpackagelevel,在这种情况下,它适用于该模块中的所有后续类定义。然而,我在flufl.enum中遇到了以下情况包裹的__init__.py:__metaclass__=type如果未定义__metaclass__,默认的元类是type,这不会没有效果吗?(如果__metaclass__在更高的范围内被分配,这个分配将恢复为默认值,但我没有看到这样的分配。)它的目的是什么?

python - 错误 "TypeError: type numpy.ndarray doesn' t 定义 __round__ 方法”

importnumpy......#Predictionpredictions=model.predict(X_test)#roundpredictionsrounded=[round(x)forxinpredictions]print(rounded)"predictions"isalistofdecimalsbetween[0,1]withsigmoidoutput.为什么总是报这个错:File"/home/abigail/workspace/ml/src/network.py",line41,inrounded=[round(x)forxinpredictions]TypeErr

python - FutureWarning : specifying 'categories' or 'ordered' in . astype() 已弃用;改为传递 CategoricalDtype

标题中的警告是由pandas0.21.0在Python3.6.3上产生的,代码如pd.Series(["a","b","b"]).astype("category",类别=["a","b","c"])。现在应该怎么写这个? 最佳答案 警告中提到的CategoricalDtype可用pd.api.types.CategoricalDtype.所以,你可以这样写pd.Series(["a","b","b"]).astype(pd.api.types.CategoricalDtype(categories=["a","b","c"])).

python - "tkinter TclError: bad file type"使用 askopenfilename

这是我第一次使用Tkinter。我已经导入它并且它一直在工作直到这一点。文件类型似乎有问题?如果这有什么不同的话,我也在使用Mac。这是我的代码:defimportTracks(self):self.fname=askopenfilename(filetypes=(("Mp3Files","*.mp3")))这是我收到的错误,/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4/Users/accudeveloper/PycharmProjects/AccuAdmin2.0/AccuAdmin2.0.pyExce

python - 使用 cx_Freeze 和 tkinter 时,我得到 : "DLL load failed: The specified module could not be found." (Python 3. 5.3)

当使用cx_Freeze和Tkinter时,我收到消息:File"C:\Users\VergilTheHuragok\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py",line35,inimport_tkinter#IfthisfailsyourPythonmaynotbeconfiguredforTkImportError:DLLloadfailed:Thespecifiedmodulecouldnotbefound.一些注意事项:我想使用Python3+(目前使用3.5.3,32位)。并不真正关心特

python - Mac 操作系统, pip : specify compiler for packages containing C libraries

我在使用pip使用默认的clang编译器编译mapscript(是来自pypi的包含C代码的包)时遇到了一些问题。这是我的尝试:-$sudopipinstallmapscriptPassword:Downloading/unpackingmapscriptRunningsetup.pyegg_infoforpackagemapscriptRequirementalreadysatisfied(use--upgradetoupgrade):distributein/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib

python - Content-Type in 用于 python 请求中的单个文件

我想向运行在pythonflask中的服务器请求文件和一些元信息。因此,我的请求内容类型将是“multipart/form-data”。有没有一种方法可以设置文件的内容类型,如图像/jpg、图像/gif等...如何设置文件的内容类型。可否 最佳答案 如果您将每个文件规范设为一个元组,则可以将mime类型指定为第三个参数:files={'file1':('foo.gif',open('foo.gif','rb'),'image/gif'),'file2':('bar.png',open('bar.png','rb'),'image/p

python - 我可以使用 typing 模块在 python 中为类型构造函数创建类型别名吗?

自python版本3.5起,您可以使用类型提示来指示函数期望的参数类型。我发现这些类型提示对于文档而言非常有值(value),所以我尽可能多地使用它们。它们还帮助linter,因此经常使我免受代码更改引入的错误。例如,在我的代码中有几个函数将零参数函数作为参数。例如:defonReady(f:Callable[[],Any])->None:...或者defcheckIfReady(f:Callable[[],Bool])->None:...我想做的是像这样创建一个类型别名(下面的代码不是有效的python):Action[A]=Callable[[],A]然后我可以缩短上述参数的类型:

Python argparse : type inconsistencies when combining 'choices' , 'nargs' 和 'default'

我有以下python程序:#!/usr/bin/envpythonimportargparseparser=argparse.ArgumentParser()parser.add_argument('arg',choices=['foo','bar','baz'],default='foo',nargs='*')args=parser.parse_args()print(args)如果我这样调用程序:./prog.py输出是Namespace(arg='foo')但是如果我用foo作为参数调用程序:./prog.pyfoo输出是Namespace(arg=['foo'])问题如何让ar

python - 在运行时创建类时使用 `exec` 比 `type()` 有什么优势?

我想在运行时在python中动态创建类。例如,我想复制下面的代码:>>>classRefObj(object):...def__init__(self,ParentClassName):...print"CreatedRefObjwithtiesto%s"%ParentClassName...classFoo1(object):...ref_obj=RefObj("Foo1")...classFoo2(object):...ref_obj=RefObj("Foo2")...CreatedRefObjwithtiestoFoo1CreatedRefObjwithtiestoFoo2>>>