我正在尝试在基于Alpine 3.1的docker容器中安装numpy.我正在使用以下Dockerfile:
FROM alpine:3.1
RUN apk add --update make cmake gcc g++ gfortran
RUN apk add --update python py-pip python-dev
RUN pip install cython
RUN pip install numpy
Run Code Online (Sandbox Code Playgroud)
这运行正常,直到pip install numpy我收到以下错误:
error: Command "gcc -fno-strict-aliasing -Os -fomit-frame-pointer -DNDEBUG -Os -fomit-frame-pointer -fPIC -Inumpy/core/include -Ibuild/src.linux-x86_64-2.7/numpy/core/include/numpy -Inumpy/core/src/private -Inumpy/core/src -Inumpy/core -Inumpy/core/src/npymath -Inumpy/core/src/multiarray -Inumpy/core/src/umath -Inumpy/core/src/npysort -I/usr/include/python2.7 -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -Ibuild/src.linux-x86_64-2.7/numpy/core/src/private -c build/src.linux-x86_64-2.7/numpy/core/src/npymath/ieee754.c -o build/temp.linux-x86_64-2.7/build/src.linux-x86_64-2.7/numpy/core/src/npymath/ieee754.o" failed with exit status 1
Run Code Online (Sandbox Code Playgroud)
easy_install-2.7 numpy 给出了同样的错误.
我缺少任何配置/安装步骤吗?
tl; dr:使用带有ExpressJS的node-mysql处理MySQL数据库的两个或多个异步查询的正确方法是什么?
我使用带有node-mysql的ExpressJS在MySQL数据库上执行两个独立的,不相关的数据库查询.由于响应是异步的,我正在嵌套查询,这意味着它们最终会一个接一个地发生.
这似乎是一种丑陋,缓慢且通常不好的方法,特别是如果我要添加第三或第四个查询.
var mysql = require('mysql');
var credentials = {...}
router.get('/api/url/', function (req, res) {
return_data = {}
var connection = mysql.createConnection(credentials);
query1 = "SELECT column1 FROM table1 WHERE column2 = 'foo'";
query2 = "SELECT column1 FROM table2 WHERE column2 = 'bar'";
connection.query(query1, {}, function(err, results) {
return_data.table1 = results;
connection.query(query2, {}, function(err, results) {
return_data.table2 = results;
connection.end();
res.send(return_data);
});
});
});
Run Code Online (Sandbox Code Playgroud) 我有两个坐标列表:
l1 = [[x,y,z],[x,y,z],[x,y,z],[x,y,z],[x,y,z]]
l2 = [[x,y,z],[x,y,z],[x,y,z]]
Run Code Online (Sandbox Code Playgroud)
我想找到 l1 和 l2 之间的最短成对距离。两个坐标之间的距离很简单:
numpy.linalg.norm(l1_element - l2_element)
Run Code Online (Sandbox Code Playgroud)
那么如何使用 numpy 将这个操作有效地应用于每对元素呢?
我试图通过SSH在运行Ubuntu的远程服务器上调用node.js的安装.节点已通过nvm安装.
SSH和调用节点工作正常:
user@localmachine:~$ ssh user@remoteserver
(Server welcome text)
user@remoteserver:~$ which node
/home/user/.nvm/v0.10.00/bin/node
Run Code Online (Sandbox Code Playgroud)
但是,如果我将它组合成一行:
user@localmachine:~$ ssh user@remoteserver "which ls"
/bin/ls
user@localmachine:~$ ssh user@remoteserver "which node"
Run Code Online (Sandbox Code Playgroud)
没有节点的迹象,所以我尝试采购.bashrc并等待10秒:
user@localmachine:~$ ssh user@remoteserver "source ~/.bashrc; sleep 10; which node"
Run Code Online (Sandbox Code Playgroud)
只有节点似乎受此影响.我注意到的一件事是,如果我进去,然后检查我在里面的那个外壳-bash,如果我直接ssh它给了我/bin/bash.我尝试在bash登录shell中运行命令:
user@localmachine:~$ ssh user@remoteserver 'bash --login -c "which node"'
Run Code Online (Sandbox Code Playgroud)
依然没有.
基本上我的问题是:当我从SSH以非交互方式调用它时,为什么bash不能找到我的node.js安装?
使用python我可以轻松地增加当前进程的好处:
>>> import os
>>> import psutil
>>> # Use os to increase by 3
>>> os.nice(3)
3
>>> # Use psutil to set to 10
>>> psutil.Process(os.getpid()).nice(10)
>>> psutil.Process(os.getpid()).nice()
10
Run Code Online (Sandbox Code Playgroud)
但是,似乎不允许降低进程的好处:
>>> os.nice(-1)
OSError: [Errno 1] Operation not permitted
>>> psutil.Process(os.getpid()).nice(5)
psutil.AccessDenied: psutil.AccessDenied (pid=14955)
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?棘轮机制是错误还是功能?
假设我有大约100组100分,并想知道哪些点在彼此的给定距离内.我有两个这样的实现,一个使用kd树,另一个只是成对距离:
from scipy.spatial.distance import cdist
from scipy.spatial import KDTree
from itertools import combinations
import numpy
import time
pts = [numpy.random.randn(100,2) for x in range(100)]
start = time.time()
for p1, p2 in combinations(pts,2):
numpy.argwhere(cdist(p1, p2) < 0.5)
print(time.time() - start)
start = time.time()
trees = [KDTree(x) for x in pts]
for p1, p2 in combinations(trees,2):
p1.query_ball_tree(p2,0.5,eps=1)
print(time.time() - start)
Run Code Online (Sandbox Code Playgroud)
我的机器cdist需要0.5秒,而KDTree实施需要一分钟.构建树需要0.03秒.我希望该KDTree方法更快,因为它不需要考虑每一对可能的点.
那么,我误解了什么,这可以更快地完成吗?
我经常发现自己通过逐行读取文件来填充列表和字符串.
假设我正在读一份人和他们喜欢的食物清单:
ANNE CHEESE
ANNE POTATO
JOE PEAS
JOE CHIPS
JOE FISH
BERNARD LENTILS
Run Code Online (Sandbox Code Playgroud)
到Python字典:
{
"ANNE" : ["CHEESE", "POTATO"],
"JOE" : ["PEAS", "CHIPS", "FISH"],
"BERNARD": ["LENTILS"]
}
Run Code Online (Sandbox Code Playgroud)
我使用的一般模式是逐行读取文件,在每种情况下,在尝试追加之前检查密钥是否已经存在.今天我决定概括一下并编写一个safe_append函数,在附加到列表或设置字典键之前创建相关对象:
def safe_append(list_object, list_key, list_value, value_dict_key= None):
# Add empty dict if it does not already exist
if list_key not in list_object:
if value_dict_key is not None:
list_object[list_key] = {}
else:
list_object[list_key] = []
# Append/set value
if value_dict_key is not None:
list_object[list_key][value_dict_key] = list_value
else:
list_object[list_key].append(list_value)
# Return object …Run Code Online (Sandbox Code Playgroud)