小编Zac*_*tes的帖子

嵌套参数不编译

我正在尝试将我的代码编译成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)

python compatibility python-2.x python-3.x

23
推荐指数
1
解决办法
3554
查看次数

从流中产生的正确方法是什么?

我有一个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每天readwrite电话.我yield from从里面尝试过Connection: …

python generator python-3.x python-asyncio yield-from

23
推荐指数
1
解决办法
1322
查看次数

是否可以修改len()的行为?

我知道创建自定义__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)

python python-3.x

12
推荐指数
3
解决办法
2004
查看次数

Selenium在Mac上提供"selenium.common.exceptions.WebDriverException:消息:未知错误:无法找到Chrome二进制文件"

尝试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.

selenium python-3.x

9
推荐指数
4
解决办法
2万
查看次数

这个tzinfo变量有什么问题?

我有这行代码:

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

python python-2.7 python-3.x

2
推荐指数
2
解决办法
8142
查看次数

如何从列表中解压缩四个变量?

我有这个清单:

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)

我怎样才能解决这个问题?

python python-2.7

2
推荐指数
2
解决办法
119
查看次数

在 JavaScript 中将字节解释为打包的二进制数据

我有一个字节数组:

[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 中实现它?

javascript bytearray endianness

2
推荐指数
1
解决办法
2279
查看次数