小编ast*_*nlu的帖子

在Python 3中将int转换为字节

我试图在Python 3中构建这个字节对象:

b'3\r\n'

所以我尝试了显而易见的(对我来说),并发现了一个奇怪的行为:

>>> bytes(3) + b'\r\n'
b'\x00\x00\x00\r\n'
Run Code Online (Sandbox Code Playgroud)

显然:

>>> bytes(10)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Run Code Online (Sandbox Code Playgroud)

我一直无法看到任何关于为什么字节转换以这种方式阅读文档的指针.但是,我确实在Python问题中发现了一些关于添加format字节的惊喜消息(另请参阅Python 3字节格式化):

http://bugs.python.org/issue3982

这与奇怪的事情(如字节(int)现在返回零)的交互更加糟糕

和:

如果bytes(int)返回该int的ASCIIfication,对我来说会更方便; 但老实说,即使错误也会比这种行为更好.(如果我想要这种行为 - 我从来没有 - 我宁愿它是一个类方法,调用类似"bytes.zeroes(n)".)

有人可以解释一下这种行为来自哪里?

python python-3.x

147
推荐指数
9
解决办法
28万
查看次数

来自concurrent.futures的ProcessPoolExecutor比multiprocessing.Pool慢

我是用新的闪亮试验concurrent.futures在Python 3.2中引入模块,和我注意到,几乎相同的代码,使用泳池从concurrent.futures的方式比使用慢multiprocessing.Pool.

这是使用多处理的版本:

def hard_work(n):
    # Real hard work here
    pass

if __name__ == '__main__':
    from multiprocessing import Pool, cpu_count

    try:
        workers = cpu_count()
    except NotImplementedError:
        workers = 1
    pool = Pool(processes=workers)
    result = pool.map(hard_work, range(100, 1000000))
Run Code Online (Sandbox Code Playgroud)

这是使用concurrent.futures:

def hard_work(n):
    # Real hard work here
    pass

if __name__ == '__main__':
    from concurrent.futures import ProcessPoolExecutor, wait
    from multiprocessing import cpu_count
    try:
        workers = cpu_count()
    except NotImplementedError:
        workers = 1
    pool = ProcessPoolExecutor(max_workers=workers)
    result = pool.map(hard_work, …
Run Code Online (Sandbox Code Playgroud)

python concurrency future multiprocessing concurrent.futures

34
推荐指数
1
解决办法
1万
查看次数

有没有一种标准方法可以检查Fortran 90/95中的Infinite和NaN?

我一直试图找到一种符合标准的方法来检查Fortran 90/95中的无限和NaN值,但事实证明它比我想象的要难.

  • 我尝试使用IEEE 754中描述的二进制表示手动创建Inf和NaN变量,但我没有发现这样的功能.
  • 我知道内在的ieee_arithmeticFortran中2003年与模块ieee_is_nan()ieee_is_finite()内部函数.然而,所有编译器都不支持它(特别是版本4.9的gfortran).

在像一开始定义无限大和NaN pinf = 1. / 0,并nan = 0. / 0似乎hackish的我,恕我直言,可以提出一些建筑问题-例如,如果某些编译器检查这在编译的时候人们就必须提供一个特殊的标志.

有没有办法在标准的Fortran 90/95中实现?

function isinf(x)
! Returns .true. if x is infinity, .false. otherwise
...
end function isinf
Run Code Online (Sandbox Code Playgroud)

isnan()

fortran fortran95

20
推荐指数
1
解决办法
3万
查看次数

--find-links和--index-url pip标志有什么区别?

阅读pip文档,我不清楚指定--find-linksURL或--index-url/--extra-index-url额外包之间的区别.

文件说明:

-i, --index-url <url>
Run Code Online (Sandbox Code Playgroud)

Python包索引的基本URL(默认为https://pypi.python.org/simple).这应该指向符合PEP 503(简单存储库API)的存储库或以相同格式布局的本地目录.

-f, --find-links <url>
Run Code Online (Sandbox Code Playgroud)

如果是html文件的URL或路径,则解析链接到档案.如果是本地路径或文件:// url这是一个目录,那么在目录列表中查找存档.

据我所知,除了索引URL必须遵循PEP 503之外,两者之间没有真正的区别.我想是遵循在所有可用的版本中选择最新版本的通常逻辑.

我错过了两者之间是否有任何其他概念上的差异?如果是这样,哪些?如果没有,为什么两者都有?

python pip

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

在Python中将外部函数分配给类变量

我试图将其他地方定义的函数分配给类变量,以便稍后我可以在实例的一个方法中调用它,如下所示:

from module import my_func

class Bar(object):
    func = my_func
    def run(self):
        self.func()  # Runs my function
Run Code Online (Sandbox Code Playgroud)

问题是这失败了,因为在执行时self.func(),实例作为第一个参数传递.

我想出了一个黑客,但对我来说似乎很难看,有没有人可以选择?

In [1]: class Foo(object):
   ...:     func = lambda *args: args
   ...:     def __init__(self):
   ...:         print(self.func())
   ...:

In [2]: class Foo2(object):
   ...:     funcs = [lambda *args: args]
   ...:     def __init__(self):
   ...:         print(self.funcs[0]())
   ...:

In [3]: f = Foo()
(<__main__.Foo object at 0x00000000044BFB70>,)

In [4]: f2 = Foo2()
()
Run Code Online (Sandbox Code Playgroud)

编辑:内置函数的行为不同!

In [13]: from math import pow

In [14]: def pow_(a, …
Run Code Online (Sandbox Code Playgroud)

python oop python-2.x

14
推荐指数
1
解决办法
2701
查看次数

如何将数据从Cassandra表复制到另一个结构以获得更好的性能

在一些地方,建议根据我们要对它们执行的查询来设计我们的Cassandra表.在DataScale的这篇文章中,他们说明了这一点:

事实是,在Cassandra中拥有许多具有类似数据的类似表格是一件好事.将主键限制为您将要搜索的内容.如果您计划使用类似但不同的标准搜索数据,请将其设为单独的表.以不同方式存储相同数据没有缺点.重复数据是您在Cassandra的朋友.

[...]

如果您需要在14个不同的表中存储相同的数据,则将其写出14次.多次写入没有障碍.

我已经理解了这一点,现在我的问题是:只要我有一张现有的桌子,比如说

CREATE TABLE invoices (
    id_invoice int PRIMARY KEY,
    year int,
    id_client int,
    type_invoice text
)
Run Code Online (Sandbox Code Playgroud)

但我希望按年份和类型进行查询,所以我希望有类似的东西

CREATE TABLE invoices_yr (
    id_invoice int,
    year int,
    id_client int,
    type_invoice text,
    PRIMARY KEY (type_invoice, year)
)
Run Code Online (Sandbox Code Playgroud)

使用id_invoice分区键和year聚类键,将数据从一个表复制到另一个表以便稍后执行优化查询的首选方法是什么?

我的Cassandra版本:

user@cqlsh> show version;
[cqlsh 5.0.1 | Cassandra 3.5.0 | CQL spec 3.4.0 | Native protocol v4]
Run Code Online (Sandbox Code Playgroud)

cql cassandra cql3 cqlsh

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

matplotlib中的gnuplot epslatex功能

我习惯用gnuplot绘制数据,所以我可以使用epslatex终端轻松地将数字放入LaTeX文档中.例如:

file = "data.dat"

set terminal epslatex
set output "figure1.tex"

plot file
Run Code Online (Sandbox Code Playgroud)

这样,生成了两个文件:一个.eps文件(包含图形)和一个.tex文件(包含文本).这样做的最大好处是文本由LaTeX呈现,因此抽搐,标签等与文档的其余部分具有相同的字体.

现在我开始使用matplotlib,它有一个更好的API,更易编写脚本,而且是Python.但是,即使我可以让matplotlib使用LaTeX渲染文本,也会嵌入到图像中,我无法获得与gnuplot相同的优势.

有什么方法可以在matplotlib中模拟epslatex终端吗?

latex gnuplot matplotlib eps

11
推荐指数
1
解决办法
1667
查看次数

Enthought Canopy中的Python 3

如何在Canopy Enthought中使用Python 3?它有底部的选项来选择Python 3,但是当我们使用时,这不会改变任何东西:

print(sys.version)
2.7.3 | 32-bit | (default, Mar 25 2013, 15:38:39) [MSC v.1500 32 bit (Intel)]
Run Code Online (Sandbox Code Playgroud)

enthought python-3.x canopy

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

等待使用pySerial进行Arduino自动重置

我试图在Linux上用一个非常简单的代码(为了展示问题)从Arduino板读取行.

Python代码:

# arduino.py
import serial
arduino = serial.Serial('/dev/ttyACM0')

with arduino:
    while True:
        print(arduino.readline())
Run Code Online (Sandbox Code Playgroud)

Arduino代码:

// simpleWrite.ino
long ii = 0;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  Serial.println(ii);
  ii++;
}
Run Code Online (Sandbox Code Playgroud)

由于电路板在打开串行连接时自动复位,因此第一个字节可能是垃圾.一两秒后一切正常.

这是典型的输出:

$ python arduino.py 
b'09\r\n'
b'540\r\n'
b'541\r\n'
b'542\r\n'
b'543\r\n'
b'544\r\n'
b'545\r\n'
b'546\r\n'
b'547\r\n'
b'548\r\n'
b'549\r\n'
b'550\r\n'
b'551\r\n'
b'552\r\n'
b'553\r\n'
b'554\r\n'
b'555\r\n'
b'556\r\n'
b'557\r\n'
b'55\xfe0\r\n'  # <---- Here the board restarted
b'1\r\n'
b'2\r\n'
b'3\r\n'
b'4\r\n'
b'5\r\n'
b'6\r\n'
b'7\r\n'
b'8\r\n'
b'9\r\n'
b'10\r\n' …
Run Code Online (Sandbox Code Playgroud)

python arduino pyserial

9
推荐指数
1
解决办法
8449
查看次数

Python中x == x是否为假?

我在统计模块中偶然发现了SciPy源代码中的这行代码:

return 1.0*(x==x)
Run Code Online (Sandbox Code Playgroud)

这是否会归还其他东西1.0?换句话说,是否有X使得任何价值x == x持有False

python floating-point equality

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