我有这门课:
from threading import Thread
import time
class Timer(Thread):
def __init__(self, interval, function, *args, **kwargs):
Thread.__init__()
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.start()
def run(self):
time.sleep(self.interval)
return self.function(*self.args, **self.kwargs)
Run Code Online (Sandbox Code Playgroud)
我用这个脚本调用它:
import timer
def hello():
print \"hello, world
t = timer.Timer(1.0, hello)
t.run()
Run Code Online (Sandbox Code Playgroud)
并得到此错误,我无法弄清楚原因: unbound method __init__() must be called with instance as first argument
我试图在InterviewStreet上解决问题.一段时间后,我确定我实际上花了大量时间阅读输入.这个特殊的问题有很多输入,所以这有点意义.没有意义的是为什么不同的输入方法具有如此不同的表现:
最初我有:
std::string command;
std::cin >> command;
Run Code Online (Sandbox Code Playgroud)
更换它使它明显更快:
char command[5];
cin.ignore();
cin.read(command, 5);
Run Code Online (Sandbox Code Playgroud)
重写使用scanf的所有内容使其更快
char command;
scanf("get_%c", &command);
Run Code Online (Sandbox Code Playgroud)
总而言之,我把读取输入的时间减少了大约1/3.
我想知道这些不同方法之间的性能存在这样的差异.另外,我想知道为什么使用gprof没有强调我在I/O上花费的时间,而是似乎指责我的算法.
Java具有基本类型的object,Integer和原始版本int.
原始版本更快/更轻/等.所以一般来说你应该使用它们.
我想知道为什么Java的设计者不仅拥有对象类型,而且使用原始版本作为幕后优化.
所以:
Integer foo(Integer alpha)
{
Integer total = 0;
for(Integer counter = 0; counter < alpha; counter++)
{
total += counter;
}
return total;
}
Run Code Online (Sandbox Code Playgroud)
将被编译成代码类似于:
int foo(int alpha)
{
int total = 0;
for(int counter = 0; counter < alpha; counter++)
{
total += counter;
}
return total;
}
Run Code Online (Sandbox Code Playgroud)
本质上,这个假设的java编译器会将Integer,Double,Float等实例转换为等效的原始类型.只有在真正需要对象的情况下(比如将元素放在容器中)才会涉及实际的Integer对象.
注意:上面的代码在Integer对象上使用了运算符,我知道实际上并不允许这样做.由于我正在发明假设的Java编译器,我假装这个版本具有Integer/Float/Double的特殊外壳,就像它对String一样.
我正在尝试理解aho-corasick字符串匹配算法.假设我们的模式是abcd和bc.我们最终得到了这样一棵树
[]
/\
[a]..[b]
/ : |
[b].: [c]
| :
[c].....
|
[d]
Run Code Online (Sandbox Code Playgroud)
虚线表示故障功能.
现在假设我们输入字符串abcd.这将跟随树并检测匹配"abcd",但是,据我所知,匹配bc将不会被报告.我误解了算法吗?
我正在尝试编写一个访问我的 bigquery 数据的谷歌云函数。但是,当我运行它时出现错误。如果我在本地模拟器下运行,它可以正常工作,但在部署时则不能。
我的 package.json:
{
"dependencies": {
"@google-cloud/bigquery": "^0.9.6"
}
}
Run Code Online (Sandbox Code Playgroud)
索引.js
var BigQuery = require('@google-cloud/bigquery');
exports.hello = function hello(req, res) {
var bigQuery = BigQuery({ projectId: 'influence-ranking' });
bigQuery.query({
query: 'SELECT label from `influence-ranking.wikidata.labels` LIMIT 1',
useLegacySql: false
}).then(function () {
return res.status(200).send('hello');
}).catch(function (error) {
return res.status(500).json(error);
});
};
Run Code Online (Sandbox Code Playgroud)
我部署功能:
$ gcloud beta functions deploy hello --stage-bucket influence-ranking-web --trigger-http --project influence-ranking
Copying file:///tmp/tmpwR3Sza/fun.zip [Content-Type=application/zip]...
/ [1 files][ 7.3 KiB/ 7.3 KiB]
Operation completed over 1 objects/7.3 KiB. …Run Code Online (Sandbox Code Playgroud) 有没有人有一个想法如何在方法/功能Int()或floor()实施?我正在寻找各自的实现,因为以下是abs()功能.
Int Abs (float x){
if x > 0
return x;
else
return -x
}
Run Code Online (Sandbox Code Playgroud)
我正在努力为它找到一个解决方案而不使用模数运算符.
我一直在寻找将Flow添加到我的javascript项目中.
在某些情况下,我做这样的事情,我有一个对象.
const myObject = {
x: 12,
y: "Hello World"
}
Run Code Online (Sandbox Code Playgroud)
我有一个通用函数,它对对象进行一些映射,保留键但替换值.
function enfunctionate(someObject) {
return _.mapValues(myObject, (value) => () => value)
}
Run Code Online (Sandbox Code Playgroud)
我希望函数返回类型{x: () => number, y: () => string}有没有办法让这种情况发生?
当然,我可以更一般地输入它,{[key: string]: any}但是我会失去很多我希望通过使用flow获得的静态类型.
如果我可以用代码生成或可能有效的宏替换我执行此操作的少数情况,但我看不到用流程做到这一点的好方法.
有没有办法解决这个问题?
import math
print "python calculator"
print "calc or eval"
while 0 == 0:
check = raw_input() #(experimental evaluation or traditional calculator)
if check == "eval":
a = raw_input("operator\n") #operator
if a == "+":
b = input("arg1\n") #inarg1
c = input("arg2\n") #inarg2
z = b + c
print z
elif a == "-":
b = input("arg1\n") #inarg1
c = input("arg2") #inarg2
z = b - c
print z
elif a == "/":
b = input("arg1\n") #inarg1
c = input("arg2\n") #inarg2
z …Run Code Online (Sandbox Code Playgroud) 我想检查单词列表中是否有单词.
word = "with"
word_list = ["without", "bla", "foo", "bar"]
Run Code Online (Sandbox Code Playgroud)
我试过了if word in set(list),但由于事实in是匹配字符串而不是项目,所以不会产生想要的结果.也就是说,"with"在任何一个词中都是匹配word_list但仍然if "with" in set(list)会说True.
执行此检查的简单方法是什么,而不是手动迭代list?
我有以下反应成分:
var App = React.createClass({
getInitialState: function() {
return {value: 4.5}
},
change: function(event) {
this.setState({value: parseFloat(event.target.value)});
},
render: function() {
return <input type="number" value={this.state.value} onChange={this.change}/>;
}
});
React.render(<App/>, document.body);
Run Code Online (Sandbox Code Playgroud)
您可以在这里看到它:http : //jsfiddle.net/2hauj2qg/
问题是,如果我想输入数字,例如:“ 4.7”。当用户输入“ 4.”时,由于转换为向后浮动,因此它变为“ 4”。但这会打断用户输入的内容。解决此问题的推荐方法是什么?
我年轻的堂兄不知怎的拿到了一本蟒蛇书,一直在努力学习Python.他向我求助了.但我在做这件事时遇到了麻烦.
他正在使用IDLE,他报告说他收到了这个错误:
Error: Inconsistent indentation detected!
1) Your indentation is outright incorrect (easy to fix), OR
2) Your indentation mixes tabs and spaces.
To fix case 2, change all tabs to spaces by using Edit->Select All
Followed by Format->Untabify Region and specify the number of
Columns used by each tab.
Run Code Online (Sandbox Code Playgroud)
他给我发了一份他的代码副本,缩进是正确的.由于许多语法错误,它无法运行,但这并不重要.他报告说他使用了Format-> Untabify Region,但问题并没有解决.
我不能为我的生活弄清楚为什么我可以运行他的python文件,但他不能.有没有人知道这是怎么回事?我很遗憾地目前正坐飞机飞行五个小时,否则我会看到发生了什么.
他的代码在这里,当我尝试运行它时没有任何缩进错误,所以我怀疑它告诉你任何有用的东西.如果有问题,那么在我得到它之前它已被某种方式删除了.
import pygame, sys, random
skier_images = ["skier_down.png", "skier_right1.png",
"skier_right2.png", "skier_left2.png",
"skier_left1.png"]
class SkierClass(pygame.sprite.Sprite):
def __init__(self)
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("skier_down.png")
self.rect = self.image.get_rect()
self.rect.center = [320, …Run Code Online (Sandbox Code Playgroud) 我的代码:
html = "<tag> </tag>"
from bs4 import BeautifulSoup
print BeautifulSoup(html).renderContents()
Run Code Online (Sandbox Code Playgroud)
输出:
<tag>?á</tag>
Run Code Online (Sandbox Code Playgroud)
期望的输出:
<tag> </tag>
Run Code Online (Sandbox Code Playgroud)
BeautifulSoup似乎被替换为我的破解空间html转义与unicode字符意味着同样的事情.但这并没有完全通过我的系统,最终成为一个不间断的空间,从而没有做我想要的.有没有办法告诉BeautifulSoup不这样做?
这是我的代码:
from PySide import QtCore, QtGui, QtTest
import sys
application = QtGui.QApplication(sys.argv)
checkbox = QtGui.QCheckBox()
assert not checkbox.isChecked()
QtTest.QTest.mouseClick(checkbox, QtCore.Qt.LeftButton)
assert checkbox.isChecked()
Run Code Online (Sandbox Code Playgroud)
我希望在模拟鼠标单击后该复选框会被选中。但事实并非如此。我使用的是 Mac OS X、Python 2.7,并且新安装了 PySide。
python ×6
javascript ×2
substring ×2
aho-corasick ×1
algorithm ×1
autoboxing ×1
c++ ×1
flowtype ×1
function ×1
html ×1
indentation ×1
init ×1
input ×1
iostream ×1
java ×1
macos ×1
math ×1
performance ×1
profiling ×1
pyside ×1
python-idle ×1
qt ×1
reactjs ×1
stdio ×1
trigonometry ×1
validation ×1