我需要一种快速的方法来计算python中整数的位数.我目前的解决方案是
bin(n).count("1")
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更快的方法这样做?
PS :(我代表一个大的2D二进制数组作为数字和按位操作的单一列表,并且将时间从几小时缩短到几分钟.现在我想摆脱那些额外的分钟.
编辑:1.它必须在python 2.7或2.6中
并且对小数量进行优化并不重要,因为那不是一个明确的瓶颈,但我确实在某些地方有10 000 +位的数字
例如,这是一个2000位的情况:
12448057941136394342297748548545082997815840357634948550739612798732309975923280685245876950055614362283769710705811182976142803324242407017104841062064840113262840137625582646683068904149296501029754654149991842951570880471230098259905004533869130509989042199261339990315125973721454059973605358766253998615919997174542922163484086066438120268185904663422979603026066685824578356173882166747093246377302371176167843247359636030248569148734824287739046916641832890744168385253915508446422276378715722482359321205673933317512861336054835392844676749610712462818600179225635467147870208L
Run Code Online (Sandbox Code Playgroud) 我见过一些关于.ssh/config和的博客 proxycommand
现在下一个命令有什么区别
ProxyCommand ssh proxyserver -W [%h]:%p
ProxyCommand ssh proxyserver nc -q0 %h %p 2> /dev/null
ProxyCommand ssh proxyserver exec nc -q0 %h %p 2> /dev/null
其中一些命令适用于某些机器,而不适用于其他机器.
我想知道我是否可以冻结当前的Activity,而我等待另一个活动或对话(任何可以)完成.
我知道我可以为结果启动一个活动,并在那里处理它们,但是startActivityForResult()之后的代码仍然会被执行
这是我想做的事情:
PopupDialog dialog = new PopupDialog(this,android.R.style.Theme_Black_NoTitleBar);
dialog.show();
// wait here, and continue the code after the dialog has finishes
int result = getResultFromDialogSomehow();
if (result == 1){
//do something
}else{
//do something else
}
Run Code Online (Sandbox Code Playgroud)
我知道这听起来很奇怪,但是如果有人能告诉我如何实现这样的功能,我真的很感激.
我有一个简单的活动,只显示两个按钮,我想加载另一个活动.
@Override
public void onCreate(Bundle savedInstanceState) {
dbg("starting on create");
super.onCreate(savedInstanceState);
dbg("stting content view");
setContentView(R.layout.main);
createDrPopup();
}
private void createDrPopup(){
dbg( "created new activity");
startActivityForResult(new Intent(this, DrPopup.class),
DR_POPUP_ACTIVITY_ID);
}
Run Code Online (Sandbox Code Playgroud)
我没有使用此代码的任何错误,但新活动无法正确加载.这样我只能获得一个空白屏幕.
但如果我延迟调用新活动,那么一切正常.
@Override
public void onCreate(Bundle savedInstanceState) {
dbg("starting on create");
super.onCreate(savedInstanceState);
dbg("stting content view");
setContentView(R.layout.main);
Thread splashTread = new Thread() {
@Override
public void run() {
try {
sleep(500);
} catch(InterruptedException e) {
} finally {
createDrPopup();
}
}
};
splashTread.start();
}
private void createDrPopup(){
dbg( "created new activity");
startActivityForResult(new …Run Code Online (Sandbox Code Playgroud) 如何在没有外键的情况下建立关系?
@declared_attr
def custom_stuff(cls):
joinstr = 'foreign(Custom.name) == "{name}"'.format(name=cls.__name__)
return db.relationship('Custom', primaryjoin=joinstr)
Run Code Online (Sandbox Code Playgroud)
这引发了一个错误:
ArgumentError: Could not locate any simple equality expressions involving locally mapped foreign key columns for primary join condition
这有效,但我认为这是一个非常丑陋的黑客.
@declared_attr
def custom_stuff(cls):
joinstr = 'or_(
and_(foreign(Custom.name) == MyTable.title,
foreign(Custom.name) != MyTable.title),
foreign(Custom.name) == "{name}")'.format(name=cls.__name__)
return db.relationship('Custom', primaryjoin=joinstr)
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
编辑:额外的属性需要添加为@declared_attr必须使用关系,因为我们的序列化程序是编写的,因此它适用于声明的attrs.
用@hybrid_property其他东西做这件事会有效,但是我们的json序列化器会破坏.让它发挥作用似乎比定义关系更难.
我们使用flake8和pylint对项目进行代码样式检查。
但是问题是,这些都没有检查线的分割方式。由于一致性在一个项目中很好,所以我们发现它看起来很奇怪
foo = long_function_name(var_one, var_two,
var_three, var_four)
foo = long_function_name(
var_one,
var_two,
var_three,
var_four)
foo = long_function_name(
var_one,
var_two,
var_three,
var_four
)
foo = {
a,
b}
foo = {
a,
b,
}
Run Code Online (Sandbox Code Playgroud)
在我们的代码库中。有时,如上例所示,关闭事物的不同方法彼此相邻。
现在有一个用于检查pylint或薄片或特殊检查器的检查器或规则,它可以确保:
一致性是干净代码库中的关键,如果不自动检查规则,我们就不能依靠开发人员遵守规则。因此,对于上述情况,我需要检查人员。
我有点迷失在这里:
我不能itertools.product在我的代码中使用.这是unittest setUp方法的一个突破点:
ipdb> import itertools
ipdb> itertools
<module 'itertools' (built-in)>
ipdb> itertools.product
<class 'itertools.product'>
ipdb> list(itertools.product([2,7], [1,4]))
*** Error in argument: '(itertools.product([2,7], [1,4]))'
Run Code Online (Sandbox Code Playgroud)
我很确定我没有对模块本身做任何奇怪的事情,因为这是在我的代码库中(没有非公开的更改):
$ git grep itertools
simple_wbd/climate.py:import itertools
Run Code Online (Sandbox Code Playgroud)
如果我在Ipython解释器中尝试这个,它工作正常.
In [1]: import itertools
In [2]: list(itertools.product([2,7], [1,4]))
Out[2]: [(2, 1), (2, 4), (7, 1), (7, 4)]
Run Code Online (Sandbox Code Playgroud)
我甚至不知道如何调试这个.你能帮忙的话,我会很高兴.
谢谢.
python ×4
android ×2
binary ×1
counting ×1
dialog ×1
indentation ×1
ipdb ×1
ipython ×1
java ×1
lint ×1
pylint ×1
python-3.x ×1
relationship ×1
sqlalchemy ×1
ssh ×1