我正在尝试将我的代码编译成Python 3模块.我在IDLE中选择"运行模块"时运行正常,但在尝试创建分发时收到以下语法错误:
File "/usr/local/lib/python3.2/dist-packages/simpletriple.py", line 9
def add(self, (sub, pred, obj)):
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助指出语法有什么问题吗?这是完整的代码:
import csv
class SimpleGraph:
def __init__(self):
self._spo = {}
self._pos = {}
self._osp = {}
def add(self, (sub, pred, obj)):
"""
Adds a triple to the graph.
"""
self._addToIndex(self._spo, sub, pred, obj)
self._addToIndex(self._pos, pred, obj, sub)
self._addToIndex(self._osp, obj, sub, pred)
def _addToIndex(self, index, a, b, c):
"""
Adds a triple to a specified index.
"""
if a not in index: index[a] = {b:set([c])}
else: …
Run Code Online (Sandbox Code Playgroud) 我有一个Connection
用于包含读取和写入asyncio
连接流的对象:
class Connection(object):
def __init__(self, stream_in, stream_out):
object.__init__(self)
self.__in = stream_in
self.__out = stream_out
def read(self, n_bytes : int = -1):
return self.__in.read(n_bytes)
def write(self, bytes_ : bytes):
self.__out.write(bytes_)
yield from self.__out.drain()
Run Code Online (Sandbox Code Playgroud)
在服务器端,每次客户端连接时connected
创建一个Connection
对象,然后读取4个字节.
@asyncio.coroutine
def new_conection(stream_in, stream_out):
conn = Connection(stream_in, stream_out)
data = yield from conn.read(4)
print(data)
Run Code Online (Sandbox Code Playgroud)
在客户端,写出4个字节.
@asyncio.coroutine
def client(loop):
...
conn = Connection(stream_in, stream_out)
yield from conn.write(b'test')
Run Code Online (Sandbox Code Playgroud)
这个作品几乎为预期的,但我必须yield from
每天read
和write
电话.我yield from
从里面尝试过Connection
: …
我知道创建自定义__repr__
或__add__
方法(等等),以修改运算符和函数的行为.是否有方法覆盖len
?
例如:
class Foo:
def __repr__(self):
return "A wild Foo Class in its natural habitat."
foo = Foo()
print(foo) # A wild Foo Class in its natural habitat.
print(repr(foo)) # A wild Foo Class in its natural habitat.
Run Code Online (Sandbox Code Playgroud)
这可以len
用列表来完成吗?通常,它看起来像这样:
foo = []
print(len(foo)) # 0
foo = [1, 2, 3]
print(len(foo)) # 3
Run Code Online (Sandbox Code Playgroud)
如果我想让搜索类型超出计数范围怎么办?像这样:
class Bar(list):
pass
foo = [Bar(), 1, '']
print(len(foo)) # 3
count = 0
for item in foo: …
Run Code Online (Sandbox Code Playgroud) 尝试selenium
使用Python 3进行Web抓取:
from selenium import webdriver
chrome_path = r"/Library/Frameworks/Python.framework/Versions/3.6/bin/chromedriver"
driver = webdriver.Chrome(chrome_path)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
selenium.common.exceptions.WebDriverException:消息:未知错误:找不到Chrome二进制文件
这里也解决了类似的问题,但令我困惑的是Chrome已经安装在我的系统上.另一个提问者显然没有在他们的电脑上.我正在运行最新版本的Mac OS.
我有这行代码:
datetime.datetime.fromtimestamp(0, "<DstTzInfo 'US/Pacific' PST-1 day, 16:00:00 STD>")
Run Code Online (Sandbox Code Playgroud)
它一直给我这个错误:
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'str'
Run Code Online (Sandbox Code Playgroud)
它到底是什么tzinfo
?
我有这个清单:
varlist = ['1', 'King', 't', '1']
Run Code Online (Sandbox Code Playgroud)
我试图在for
循环中解压缩这四个变量;
for var1, var2, var3, var4 in varlist:
post += len(var1)-len(var3)
post *= len(var2)/len(var4)
Run Code Online (Sandbox Code Playgroud)
当我运行此循环时,我得到以下内容:
ValueError: need more than 1 value to unpack
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我有一个字节数组:
[101, 97, 115, 121] # ['e', 'a', 's', 'y']
Run Code Online (Sandbox Code Playgroud)
我如何将其解释为打包的二进制文件?就像struct.Struct(format).unpack
在 Python 中一样:
>>> import struct
>>> s = struct.Struct('>1I') # a big-endian, two-byte, unsigned int
>>> s.unpack('easy')
(1700885369,)
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有导入的情况下在 JavaScript 中实现它?
python ×5
python-3.x ×5
python-2.7 ×2
bytearray ×1
endianness ×1
generator ×1
javascript ×1
python-2.x ×1
selenium ×1
yield-from ×1