我正在使用OS X El Capitan(10.11.4).
我刚刚下载TensorFlow使用PIP安装说明这里.
一切都很顺利,虽然我收到了一些警告信息,如:
The directory '/Users/myusername/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag.
和
You are using pip version 6.0.8, however version 8.1.2 is available. 即使我刚刚安装了pip.
然后,当我在Python中测试TensorFlow时,我得到了错误:
>>> import tensorflow as tf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/__init__.py", …Run Code Online (Sandbox Code Playgroud) 我有一个类对象,cls.我想知道它的元类.我该怎么做呢?
(如果我想知道它的父类,我会这样做cls.__mro__.是否有这样的东西来获取元类?)
Google Colab 输出被截断。我查看了设置,没有发现任何限制。解决问题的最佳选择是什么?
我有以下代码:
from typing import Callable
MyCallable = Callable[[object], int]
MyCallableSubclass = Callable[['MyObject'], int]
def get_id(obj: object) -> int:
return id(obj)
def get_id_subclass(obj: 'MyObject') -> int:
return id(obj)
def run_mycallable_function_on_object(obj: object, func: MyCallable) -> int:
return func(obj)
class MyObject(object):
'''Object that is a direct subclass of `object`'''
pass
my_object = MyObject()
# works just fine
run_mycallable_function_on_object(my_object, get_id)
# Does not work (it runs, but Mypy raises the following error:)
# Argument 2 to "run_mycallable_function_on_object" has incompatible type "Callable[[MyObject], int]"; expected "Callable[[object], …Run Code Online (Sandbox Code Playgroud) 在 Python 中处理可变默认参数的方法是将它们设置为 None。
例如:
def foo(bar=None):
bar = [] if bar is None else bar
return sorted(bar)
Run Code Online (Sandbox Code Playgroud)
如果我输入函数定义,那么唯一的类型 forbar说的bar是Optional,很明显,它不是Optional我期望sorted在其上运行该函数的时间:
def foo(bar: Optional[List[int]]=None):
bar = [] if bar is None else bar
return sorted(bar) # bar cannot be `None` here
Run Code Online (Sandbox Code Playgroud)
那么我应该投吗?
def foo(bar: Optional[List[int]]=None):
bar = [] if bar is None else bar
bar = cast(List[int], bar) # make it explicit that `bar` cannot be `None`
return sorted(bar) …Run Code Online (Sandbox Code Playgroud) 我尝试使用torch.utils.data.random_split如下:
import torch
from torch.utils.data import DataLoader, random_split
list_dataset = [1,2,3,4,5,6,7,8,9,10]
dataset = DataLoader(list_dataset, batch_size=1, shuffle=False)
random_split(dataset, [0.8, 0.1, 0.1], generator=torch.Generator().manual_seed(123))
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试这个时,我收到了错误raise ValueError("Sum of input lengths does not equal the length of the input dataset!")
我查看了文档,似乎我应该能够传递总和为 1 的小数,但显然它不起作用。
我也在谷歌上搜索了这个错误,最接近的事情就是这个问题。
我究竟做错了什么?
通常,为了子类化一个类,我会做类似的事情
class Subby(Parenty):
def __init__(self):
Parenty.__init__(self)
Run Code Online (Sandbox Code Playgroud)
但现在我正在尝试子类化元类,特别是 ABCMeta,所以我正在做
import ABCMeta
class Subby(ABCMeta):
def __init__(self):
ABCMeta.__init__(self) #this never seems to run
def __new__(mcls, name, bases, namespace):
cls = ABCMeta.__new__(mcls, name, bases, namespace)
return cls
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试将其子类化为Subby元类时,例如
class newClass(metaclass=Subby):
pass
Run Code Online (Sandbox Code Playgroud)
我得到了错误TypeError: __init__() takes 1 positional argument but 4 were given。
这是为什么?如何正确子类化 ABCMeta?
阅读本教程后,我想我对如何使用 call 和 apply 有了一些了解。但是,我仍然有点困惑。
是否存在 usingfunction.call(this, arguments)与 using 不同的情况this.function(arguments)?
如果没有,那我们为什么需要这个call函数?
我有一个数据库,我正在尝试向其中添加一列。此列应保存 type 的信息timestamp,并且我希望完成后每一行都具有相同的时间戳(当前时间)。
我目前已经尝试过:
cursor.execute('''ALTER TABLE my_table ADD COLUMN time timestamp DEFAULT ?''', (datetime.datetime.utcnow(),))
Run Code Online (Sandbox Code Playgroud)
结果是sqlite3.OperationalError: near "?": syntax error.
然后我尝试了:
cursor.execute(f'''ALTER TABLE my_table ADD COLUMN time timestamp DEFAULT {datetime.datetime.utcnow()}''')
Run Code Online (Sandbox Code Playgroud)
结果是sqlite3.OperationalError: near "-": syntax error.
另外,做
cursor.execute(f'''ALTER TABLE my_table ADD COLUMN time timestamp DEFAULT CURRENT_TIMESTAMP''')
Run Code Online (Sandbox Code Playgroud)
结果是sqlite3.OperationalError: Cannot add a column with non-constant default。
如何添加新列并设置该列中的值?(通过DEFAULT或其他一些机制。)
python ×6
python-3.x ×3
metaclass ×2
class ×1
create-table ×1
dataset ×1
date ×1
javascript ×1
macos ×1
mutable ×1
mypy ×1
numpy ×1
pandas ×1
pip ×1
pytorch ×1
sql ×1
sqlite ×1
subclass ×1
tensorflow ×1
type-hinting ×1
typing ×1