我试图在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)".)
有人可以解释一下这种行为来自哪里?
我是用新的闪亮试验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
我一直试图找到一种符合标准的方法来检查Fortran 90/95中的无限和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()?
阅读pip文档,我不清楚指定--find-linksURL或--index-url/--extra-index-url额外包之间的区别.
文件说明:
Run Code Online (Sandbox Code Playgroud)-i, --index-url <url>Python包索引的基本URL(默认为https://pypi.python.org/simple).这应该指向符合PEP 503(简单存储库API)的存储库或以相同格式布局的本地目录.
Run Code Online (Sandbox Code Playgroud)-f, --find-links <url>如果是html文件的URL或路径,则解析链接到档案.如果是本地路径或文件:// url这是一个目录,那么在目录列表中查找存档.
据我所知,除了索引URL必须遵循PEP 503之外,两者之间没有真正的区别.我想是遵循在所有可用的版本中选择最新版本的通常逻辑.
我错过了两者之间是否有任何其他概念上的差异?如果是这样,哪些?如果没有,为什么两者都有?
我试图将其他地方定义的函数分配给类变量,以便稍后我可以在实例的一个方法中调用它,如下所示:
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) 在一些地方,建议根据我们要对它们执行的查询来设计我们的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) 我习惯用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终端吗?
如何在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) 我试图在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) 我在统计模块中偶然发现了SciPy源代码中的这行代码:
return 1.0*(x==x)
Run Code Online (Sandbox Code Playgroud)
这是否会归还其他东西1.0?换句话说,是否有X使得任何价值x == x持有False?