在代码中解释最简单:
require 'timeout'
puts "this block will properly kill the sleep after a second"
IO.popen("sleep 60") do |io|
begin
Timeout.timeout(1) do
while (line=io.gets) do
output += line
end
end
rescue Timeout::Error => ex
Process.kill 9, io.pid
puts "timed out: this block worked correctly"
end
end
puts "but this one blocks for >1 minute"
begin
pid = 0
Timeout.timeout(1) do
IO.popen("sleep 60") do |io|
pid = io.pid
while (line=io.gets) do
output += line
end
end
end
rescue Timeout::Error => ex
puts …Run Code Online (Sandbox Code Playgroud) 以下是Richard Jones博客的一些代码:
with gui.vertical:
text = gui.label('hello!')
items = gui.selection(['one', 'two', 'three'])
with gui.button('click me!'):
def on_click():
text.value = items.value
text.foreground = red
Run Code Online (Sandbox Code Playgroud)
我的问题是:他是怎么做到的?上下文管理器如何访问with块内的范围?这是一个试图解决这个问题的基本模板:
from __future__ import with_statement
class button(object):
def __enter__(self):
#do some setup
pass
def __exit__(self, exc_type, exc_value, traceback):
#XXX: how can we find the testing() function?
pass
with button():
def testing():
pass
Run Code Online (Sandbox Code Playgroud) 鉴于这两个测试用例:
func TestEqualWhat(t *testing.T) {
testMarshalUnmarshal(t, map[string]interface{}{"a":"b"})
testMarshalUnmarshal(t, map[string]interface{}{"a":5})
}
Run Code Online (Sandbox Code Playgroud)
testMarshalUnmarshal帮助程序只是对json进行编组并返回:
func testMarshalUnmarshal(t *testing.T, in map[string]interface{}) {
//marshal the object to a string
jsb, err := json.Marshal(in);
if err != nil {
log.Printf("Unable to marshal msg")
t.FailNow()
}
//unmarshal to a map
res := make(map[string]interface{})
if err := json.Unmarshal(jsb, &res); err != nil { t.FailNow() }
if !reflect.DeepEqual(in, res) {
log.Printf("\nExpected %#v\nbut got %#v", in, res)
t.FailNow()
}
}
Run Code Online (Sandbox Code Playgroud)
为什么第一个测试用例通过而第二个测试用例失败?测试的输出是这样的:
Expected map[string]interface {}{"a":5}
but got map[string]interface {}{"a":5}
--- FAIL: TestEqualWhat …Run Code Online (Sandbox Code Playgroud) 过去,版本3.0的rails指南上的ruby位于http://edgeguides.rubyonrails.org/,版本2.3的指南位于http://guides.rubyonrails.org.现在版本3已经发布,其指南已移至主URL.
有没有办法访问rails 2.3的指南?
我正在使用vgo和一个Dockerfile开展一个go项目,我希望docker能够缓存项目的依赖项,这样它就有两个属性:
go.mod文件更改,将重新下载依赖项go.mod,则不会重新下载依赖项.现在,我这样做:
...
RUN go get -u golang.org/x/vgo
COPY . /go/src/whatever
RUN vgo install
...
Run Code Online (Sandbox Code Playgroud)
但是,如果您更改了go文件,则必须从该COPY层重建dockerfile .
简而言之,我想做的是:
...
RUN go get -u golang.org/x/vgo
COPY go.mod /go/src/whatever
RUN vgo install_dependencies
COPY . /go/src/whatever
RUN vgo install
...
Run Code Online (Sandbox Code Playgroud)
这样,如果我更改go.mod,将下载并重建所有依赖项,但除此之外,我们可以继续构建二进制文件.
我可以看到几种方法来获得这样的行为,但所有这些方法都有缺点:
$GOPATH/src/mod到docker容器中,但是这将有很多我不需要的文件vgo mod -vendor在构建docker容器之前复制供应商目录,但这依赖于开发人员记住vgo mod -vendor每次go.mod更改时都要运行,否则应用程序将无法构建,他们必须vgo mod -vendor在重试docker构建之前运行.你能想到一种方式让我获得像我想象中的行为vgo install_dependencies吗?我错过了一个vgo技巧吗?
端口8888已通过在docker容器中运行的进程绑定到我的(OS X 10.13.5)系统上:
$ netstat -an | grep 8888
tcp6 0 0 ::1.8888 *.* LISTEN
tcp4 0 0 *.8888 *.* LISTEN
Run Code Online (Sandbox Code Playgroud)
尝试绑定到该端口的python程序(使用我可以管理的与golang尽可能接近的socket选项),以我期望的方式失败:
$ netstat -an | grep 8888
tcp6 0 0 ::1.8888 *.* LISTEN
tcp4 0 0 *.8888 *.* LISTEN
Run Code Online (Sandbox Code Playgroud)
失败:
$ python test.py
Traceback (most recent call last):
File "test.py", line 15, in <module>
main()
File "test.py", line 11, in main
sock.bind(("0.0.0.0", 8888))
OSError: [Errno 48] Address already in use
Run Code Online (Sandbox Code Playgroud)
但是go程序通过创建连接net.Listen不会失败,正如我期望的那样:
package main
import (
"fmt"
"net" …Run Code Online (Sandbox Code Playgroud) 解决了最近出现的代码问题,我发现我的默认Python比PyPy慢约40倍.通过在函数中运行限制调用和限制全局查找,我能够通过此代码将其降低到大约17倍len.
现在,e.py在python 3.6.3上运行5.162秒,在我的机器上在PyPy上运行.297秒.
我的问题是:这是JIT的不可缩短的加速,还是有某种方法可以加速CPython的回答?(没有极端的意思:我可以去Cython/Numba或其他什么?)我如何说服自己,我无能为力?
请参阅gist以获取数字输入文件列表.
如问题陈述中所述,它们代表跳跃偏移.position += offsets[current],并将当前偏移量增加1.当跳转将您带到列表外时,您就完成了.
这是给出的示例(需要5秒的完整输入更长,并且具有更大的数字):
(0) 3 0 1 -3 - before we have taken any steps.
(1) 3 0 1 -3 - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.
2 (3) 0 1 -3 - step forward because of the instruction we just modified. The first instruction is incremented again, …Run Code Online (Sandbox Code Playgroud) 我想将 postgres 设置为en-US-x-icu默认按排序规则排序,因此我将LC_CTYPE和LC_COLLATE环境变量设置为该值。Postgres 似乎看到了它们,并说它正在使用该排序规则:
:mctapi=# select datname, datcollate, datctype from pg_database where datname='mctapi';
datname | datcollate | datctype
---------+-------------+-------------
mctapi | en-US-x-icu | en-US-x-icu
:mctapi=# show lc_collate;
lc_collate
-------------
en-US-x-icu
(1 row)
:mctapi=# show lc_ctype;
lc_ctype
-------------
en-US-x-icu
(1 row)
Run Code Online (Sandbox Code Playgroud)
但是,除非我明确设置en-US-x-icu为排序规则,否则我会得到C排序规则:
:mctapi=# select distinct organization_name from plans order by organization_name limit 5;
organization_name
------------------------------------------
AMH Health
ATRIO Health Plans
Aetna Medicare
AgeRight Advantage
AgeRight Advantage Health Plan (HMO SNP)
(5 …Run Code Online (Sandbox Code Playgroud) go ×3
python ×3
benchmarking ×1
database ×1
docker ×1
io ×1
json ×1
macos ×1
networking ×1
performance ×1
postgresql ×1
pypy ×1
ruby ×1
scope ×1
sockets ×1
sql ×1
subprocess ×1
timeout ×1
vgo ×1