编译以下代码失败,但如果我删除specialized方法中的注释,它就会通过dot.
Scala代码运行器版本2.12.0-RC2 - 版权所有2002-2016,LAMP/EPFL和Lightbend,Inc.
abstract class Op[@specialized Left, @specialized Right] {
@specialized
type Result
def r: Numeric[Result]
def times(left: Left, right: Right): Result
}
object Op {
implicit object IntDoubleOp extends Op[Int, Double] {
type Result = Double
val r = implicitly[Numeric[Double]]
def times(left: Int, right: Double): Double = left * right
}
}
object calc {
def dot[@specialized Left, @specialized Right](xs: Array[Left], ys: Array[Right])
(implicit op: Op[Left, Right]): op.Result = {
var total = op.r.zero …Run Code Online (Sandbox Code Playgroud) 我想在pandas中做一些滚动窗口计算,需要同时处理两列.我将用一个简单的例子来清楚地表达问题:
import pandas as pd
df = pd.DataFrame({
'x': [1, 2, 3, 2, 1, 5, 4, 6, 7, 9],
'y': [4, 3, 4, 6, 5, 9, 1, 3, 1, 2]
})
windowSize = 4
result = []
for i in range(1, len(df)+1):
if i < windowSize:
result.append(None)
else:
x = df.x.iloc[i-windowSize:i]
y = df.y.iloc[i-windowSize:i]
m = y.mean()
r = sum(x[y > m]) / sum(x[y <= m])
result.append(r)
print(result)
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有for pringas循环来解决问题?任何帮助表示赞赏
假设我定义了四个类如下:
(该代码已经在 Python 3.6.5 上进行了测试。但是,我希望它也可以在 Python 2.7.x 上使用from __future__ import print_function)
In [1]: class A(object):
...: pass
...:
...: class B(object):
...: def __init__(self, value):
...: print('B(value=%s)' % value)
...:
...: class C(A):
...: def __init__(self, value):
...: print('C(value=%s)' % value)
...: super(C, self).__init__(value)
...:
...: class D(A, B):
...: def __init__(self, value):
...: print('D(value=%s)' % value)
...: super(D, self).__init__(value)
...:
In [2]: C.mro()
Out[2]: [__main__.C, __main__.A, object]
In [3]: D.mro()
Out[3]: …Run Code Online (Sandbox Code Playgroud) 我的真正任务是使用多线程的paramiko递归遍历远程目录.为简单起见,我只使用本地文件系统来演示它:
from pathlib import Path
from typing import List
from concurrent.futures import ThreadPoolExecutor, Executor
def listdir(root: Path, executor: Executor) -> List[Path]:
if root.is_dir():
xss = executor.map(lambda d: listdir(d, executor), root.glob('*'))
return sum(xss, [])
return [root]
with ThreadPoolExecutor(4) as e:
listdir(Path('.'), e)
Run Code Online (Sandbox Code Playgroud)
但是,上面的代码运行没有尽头.
我的代码出了什么问题?以及如何解决它(更好地使用Executor而不是原始Thread)?
编辑:我通过以下代码确认了@Sraw的答案:
In [4]: def listdir(root: Path, executor: Executor) -> List[Path]:
...: print(f'Enter {root}', flush=True)
...: if root.is_dir():
...: xss = executor.map(lambda d: listdir(d, executor), root.glob('*'))
...: return sum(xss, [])
...: return [root] …Run Code Online (Sandbox Code Playgroud) 我想使用 Paramiko 和多线程从远程服务器下载文件。
我想到了两个解决方案,但我不确定哪个是正确的(或更好)。
解决方案 1:
假设它SFTPClient.get是线程安全的(但我找不到任何提到的文档),一个简单的方法是:
from paramiko import SSHClient, AutoAddPolicy, SFTPClient
from concurrent.futures import ThreadPoolExecutor
from typing import List
client = SSHClient()
ciient.set_missing_host_key_policy(AutoAddPolicy())
client.connect( ... )
sftp = client.open_sftp()
files_to_download: List[str] = ...
with ThreadPoolExecutor(10) as pool:
pool.map(lambda fn: sftp.get(fn, fn), files_to_download)
Run Code Online (Sandbox Code Playgroud)
解决方案2:解决方案1中有两个问题
所以这是我的第二个解决方案:
from paramiko import SSHClient, AutoAddPolicy, SFTPClient
from concurrent.futures import ThreadPoolExecutor
from threading import Lock, local
from typing import List
client = SSHClient()
ciient.set_missing_host_key_policy(AutoAddPolicy())
client.connect( ... …Run Code Online (Sandbox Code Playgroud) 我想计算每个组的系列差异,如下例所示:
\n\nIn [24]: rnd_ser = pd.Series(np.random.randn(5000))\n ...: com_ser = pd.concat([rnd_ser] * 500, keys=np.arange(500), names=[\'Date\', \'ID\'])\n\nIn [25]: d1 = com_ser.groupby("Date").diff()\n\nIn [26]: d2 = com_ser - com_ser.groupby("Date").shift()\n\nIn [27]: np.allclose(d1.fillna(0), d2.fillna(0))\nOut[27]: True\nRun Code Online (Sandbox Code Playgroud)\n\n有两种方法可以解决这个问题,但是第一种方法性能较差:
\n\nIn [30]: %timeit d1 = com_ser.groupby("Date").diff()\n616 ms \xc2\xb1 5.62 ms per loop (mean \xc2\xb1 std. dev. of 7 runs, 1 loop each)\n\nIn [31]: %timeit d2 = com_ser - com_ser.groupby("Date").shift()\n95 ms \xc2\xb1 326 \xc2\xb5s per loop (mean \xc2\xb1 std. dev. of 7 runs, 10 loops each)\nRun Code Online (Sandbox Code Playgroud)\n\n这是预期的还是错误? …
使用有效数据的interploate方法pandas来插值nan。但是,它保持旧的有效数据不变,如以下代码所示。
有什么方法可以使用interploate更改了旧值以使序列变得平滑的方法?
In [1]: %matplotlib inline
In [2]: from scipy.interpolate import UnivariateSpline as spl
In [3]: import numpy as np
In [4]: import pandas as pd
In [5]: samples = { 0.0: 0.0, 0.4: 0.5, 0.5: 0.9, 0.6: 0.7, 0.8:0.3, 1.0: 1.0 }
In [6]: x, y = zip(*sorted(samples.items()))
In [7]: df1 = pd.DataFrame(index=np.linspace(0, 1, 31), columns=['raw', 'itp'], dtype=float)
In [8]: df1.loc[x] = np.array(y)[:, None]
In [9]: df1['itp'].interpolate('spline', order=3, inplace=True)
In [10]: …Run Code Online (Sandbox Code Playgroud) 我是emacs的新手,我想在Windows中使用ensime.我试过但它不起作用.它似乎不起作用,因为有一个名为"\ ensime\bin\server.sh"的*nix格式文件.非常感谢有人给我一些提示.
编辑:我遵循VonC的建议,但它不完美.我确定我错过了什么.
我在D:D:\ Dev\emacs-23.1中安装了emacs23.1,在D:\ Dev\emacs-23.1\scala-mode中安装了scala-mode,在D:\ Dev\emacs-23.1\ensime中安装了ensime.
;;?????
(tool-bar-mode nil)
;;????
(global-linum-mode t)
;;??scala mode
(add-to-list 'load-path "D:/Dev/emacs-23.1/scala-mode/")
(require 'scala-mode)
(add-to-list 'auto-mode-alist '("\\.scala$" . scala-mode))
(add-to-list 'load-path "D:/Dev/emacs-23.1/ensime/src/elisp/")
(require 'ensime)
(add-hook 'scala-mode-hook 'ensime-scala-mode-hook)
Run Code Online (Sandbox Code Playgroud)
(
:server-root "D:/Dev/emacs-23.1/ensime/"
:server-cmd "D:/Dev/emacs-23.1/ensime/bin/server.bat"
:server-host "localhost"
:server-env ()
:project-package "com.ensime"
:source ("src")
:exclude-source ()
:classpath ("lib/jnotify/jnotify-0.93.jar"
"lib/scala/scala-library.jar"
"lib/scala/scala-compiler.jar")
)
Run Code Online (Sandbox Code Playgroud)
这是我的D:\ Dev\emacs-23.1\ensime\bin\server.bat文件内容:
@echo off
set PORT_FILE=%1
set CLASSPATH=D:\Dev\emacs-23.1\ensime\lib\jnotify\jnotify-0.93.jar;D:\Dev\emacs-23.1\ensime\lib\scala\scala-library.jar;D:\Dev\emacs-23.1\ensime\lib\scala\scala-compiler.jar;D:\Dev\emacs-23.1\ensime\dist\ensime.jar
java -classpath %CLASSPATH% -Djava.library.path=D:\Dev\emacs-23.1\ensime\lib\jnotify com.ensime.server.Server %PORT_FILE%
Run Code Online (Sandbox Code Playgroud)
alt text http://www.turboimagehost.com/p/3350328/3769883.PNG.html
假设我有一个布尔值DataFrame df和一个Series x具有相同索引的布尔值,并且我想在每列之间df和之间进行逻辑运算x。有没有像DataFrame.sub使用比较短而快速的方法DataFrame.apply?
In [31]: df
Out[31]:
x y z u
A False False True True
B True True True True
C True False False False
In [32]: x
Out[32]:
A True
B False
C True
dtype: bool
In [33]: r = df.apply(lambda col: col & x) # Any other way ??
In [34]: r
Out[34]:
x y z u
A False False True True
B False False …Run Code Online (Sandbox Code Playgroud) 我制作了一个自定义部分函数,如下所示:
(Python 3.6.5 |Anaconda 自定义(64 位)| (默认,2018 年 3 月 29 日,13:32:41)[MSC v.1900 64 位 (AMD64)])
In [1]: class SecPartialF(object):
...: def __init__(self, func, arg1):
...: self.func, self.arg1 = func, arg1
...: def __call__(self, arg2):
...: return self.func(self.arg1, arg2)
...:
Run Code Online (Sandbox Code Playgroud)
它适用于构建map:
In [2]: def f(x, y):
...: print(x, y)
...: return x + y
In [3]: list(map(SecPartialF(f, 10), range(10)))
10 0
10 1
10 2
10 3
10 4
10 5
10 6
10 7
10 8
10 …Run Code Online (Sandbox Code Playgroud)