我今天已经介绍过xpath,它似乎非常强大,但经过相当多的搜索后,我还没有找到如何在使用contains时检索兄弟姐妹(通过follow-sibling和previous-sibling):
text = """
<html>
<head>
<title>This tag includes 'some_text'</title>
<h2>A h2 tag</h2>
</head>
</html>
"""
import lxml.html
doc = lxml.html.fromstring(text)
a = doc.xpath("//*[contains(text(),'some_text')]/following-sibling::*")
Run Code Online (Sandbox Code Playgroud)
哪个产生[].当然,我期望的结果是得到h2标签.
但是,使用*[contains(text(),'name')]按预期方式检索title元素.以同样的方式,如果不使用跟随兄弟轴(我认为它是如何被称为),我使用//parent::*,也有效.
那么,我怎样才能让兄弟姐妹处于这种状态?
提前致谢.
我django.contrib.auth.views.logout用另一个函数包装,以添加其他行为.这是我的新logout_page:
views.py
from django.contrib.auth import logout
def logout_page(request, *args, **kwargs):
from django.utils import timezone
user = request.user
profile = user.get_profile()
profile.last_logout = timezone.now()
profile.save()
logout(request, *args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
和
urls.py
(r'^logout/$', 'main.views.logout_page', {'next_page': '/'})
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
logout() got an unexpected keyword argument 'next_page'
Run Code Online (Sandbox Code Playgroud)
当logout包含**kwargs时,我不确定为什么'next_page'是一个意外的参数,据我所知,从django.contrib.auth注销应该包含这样的参数.从来源:
https://github.com/django/django/blob/master/django/contrib/auth/views.py
def logout(request, next_page=None,
template_name='registration/logged_out.html',
redirect_field_name=REDIRECT_FIELD_NAME,
current_app=None, extra_context=None):
...
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
假设你有这个数组:
In [29]: a = array([[10, 20, 30, 40, 50], [14, 28, 42, 56, 70], [18, 36, 54, 72, 90]])
Out[30]: a
array([[ 0, 0, 0, 0, 0],
[14, 28, 42, 56, 70],
[18, 36, 54, 72, 90]])
Run Code Online (Sandbox Code Playgroud)
现在将第三行除以第一行(使用来自未来的导入部门)
In [32]: a[0]/a[2]
Out[32]: array([ 0.55555556, 0.55555556, 0.55555556, 0.55555556, 0.55555556])
Run Code Online (Sandbox Code Playgroud)
现在对循环中的每一行执行相同的操作:
In [33]: for i in range(3):
print a[i]/a[2]
[ 0.55555556 0.55555556 0.55555556 0.55555556 0.55555556]
[ 0.77777778 0.77777778 0.77777778 0.77777778 0.77777778]
[ 1. 1. 1. 1. 1.]
Run Code Online (Sandbox Code Playgroud)
一切看起来都合适.但现在,将第一个数组a [i]/a …
我已经看到很多问题要求以更快的方式迭代2d数组的每个元素,但是我没有找到一个很好的方法迭代3d数组以便在每个2d数组上应用函数.例如:
from scipy.fftpack import dct
X = np.arange(10000, dtype=np.float32).reshape(-1,4,4)
np.array(map(dct, X))
Run Code Online (Sandbox Code Playgroud)
在这里,我将浏览包含在3d维数组中的每个2d数组,(625,4,4)并对每个4X4数组应用DCT(离散余弦变换).我想知道是否有更合适的方法来实现这一目标.
谢谢
作为这个问题的后续,我想知道为什么这不起作用
$("#textarea").keydown(function(){
oldValue = $("#textarea").val();
});
$("#textarea").keyup(function(){
var startTimer = null;
if(startTimer) clearTimeout(startTimer);
startTimer = setTimeout(function(){
var newValue = $("#textarea").val();
// get something out of the comparison
alert(something) // alert the comparison
oldValue = newValue;
},2000);
Run Code Online (Sandbox Code Playgroud)
所需的行为是仅在用户未输入任何内容2秒时才收到警报消息.它适用于比较部分,但是,当我按原样键入时,它不会停止警报消息.相反,我得到的警报数量与按下的按键数量相同.我试过这个创建和删除cookie的版本,它工作得很好.这个特殊情况有什么问题?
我有这样的事情:
forms.py:
AVATAR_CHOICES = (('1','option1'), ('2','option2'), ('3','option3'),
('4','option4'))
class RegistrationForm(forms.Form):
avatar = ChoiceField(widget=RadioSelect, choices=AVATAR_CHOICES)
Run Code Online (Sandbox Code Playgroud)
我form从我的观点中走过:
views.py
form = RegistrationForm()
return render_to_response('registration/register.html', {'form': form},
context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
进入这样的模板:
registration.html:
{% for radio in form.avatar %}
<label class="radio">{{ radio.tag }}</label>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
但是我想在这些选项字段前包含图像,以便让用户选择一个头像.但是,我不知道如何从元组访问键或值AVATAR_CHOICES.主要的想法是能够做这样的事情:
{% for radio in form.avatar %}
<label class="radio">{{ radio.tag }}</label>
<img src="{{ STATIC_URL }}img/avatars/{{ radio.tag.value }}.jpg"/>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
提前致谢.
我刚注意到一些不寻常的事 这就是我想要完成的事情:
我想在点击链接时显示div
当我点击文档中的其他位置时,我希望div消失
当我点击div本身时,我不希望它消失
像这样的东西:
JS:
(function() {
var box = $('#box');
$(document).on('click', function() {
if (box.css('display') == 'block') {
box.css('display', 'none');
}
});
$('#start').on('click', function(e) {
box.css({
'text': 'Box',
'position': 'absolute',
'top': '50px',
'left': '0',
'background': '#EEE',
'border': '1px solid #555',
'width': '200px',
'height': '50px',
'display': 'block'
});
e.stopPropagation();
});
box.on('click', function(e) {
e.stopPropagation();
});
})();?
Run Code Online (Sandbox Code Playgroud)
这个小提琴工作得很好但是当我在Firefox(15.0.1)中测试它时,如果你右键点击div,它会消失,这不是我正在寻找的行为.似乎stopPropagation()适用于点击,但不适用于Firefox中的右键单击.Chrome会将右键单击传播到文档.
我该如何解决?
谢谢
我一直在使用statsmodels.distributions的ECDF(经验累积分布函数)绘制一些数据的CDF.然而,ECDF使用阶梯函数,因此我得到锯齿状的图.

所以我的问题是:scipy或statsmodels是否有没有步进功能的ECDF烘焙?
顺便说一句,我知道我可以这样做:
hist, bin_edges = histogram(b_oz, normed=True)
plot(np.cumsum(hist))
Run Code Online (Sandbox Code Playgroud)
但我没有得到正确的尺度.
谢谢!
我想知道为什么 Typescript 会抱怨以下错误:
(22,28): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number[]'.
Run Code Online (Sandbox Code Playgroud)
当我使用带有以下参数的 zip 函数时:
let ranges = ["0-100", "100-200", "200-300", "300-400", "400-500"];
let arrays = [[19.99, 49.99, 49.99, 49.99, 29.99, 29.99, 9.99, 29.99, 34.99, 34.99, 59.99], [149.99, 179.99, 129.99, 149.99, 129.99, 199.99, 129.99], [209.99, 249.99, 292.99, 279.99, …Run Code Online (Sandbox Code Playgroud) 我想在MySQL中创建一个事件,以便更改表中的值.该事件检查日期(特别是TIMESTAMP字段的TIME()部分)是否大于当前日期.显然,我在一个名为"active"的表中有一个time_stamp和一个active列.这就是我所拥有的
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
SELECT @time:= time_stamp FROM active WHERE user_id = 1;
SELECT @time2:= TIMESTAMP(@time);
SELECT @active:=TIMEDIFF(@time2,DATE_ADD(NOW(),INTERVAL -15 SECOND));
UPDATE active SET active=if(TIME_TO_SEC(TIME(@active))>=0,1,0) WHERE user_id=1;
Run Code Online (Sandbox Code Playgroud)
据我所知,带有变量的SELECT部分工作正常,即TIME_TO_SEC(TIME(@active))看起来像是从15开始的回归计数(假设我正确地更新了'time_stamp'字段).
预期的行为是,当TIME_TO_SEC(TIME(@active))达到0时,UPDATE查询会将"active"字段的值从1更改为0(默认值为1).但是,它没有做任何事情.
更新:确切地说,有时它会将值更改为0,但是当TIME_TO_SEC(TIME(@active))仍为正值时.
第二更新:我最近尝试过这个:
CREATE EVENT update_status
ON SCHEDULE EVERY 2 SECOND
DO
SELECT @time:= time_stamp FROM active WHERE user_id = 1;
SELECT @time2:= TIMESTAMP(@time);
SELECT @active:=TIMEDIFF(@time2,DATE_ADD(NOW(),INTERVAL -15 SECOND));
UPDATE active SET active=0 WHERE user_id=1 AND TIME_TO_SEC(TIME(@active))>=0;
Run Code Online (Sandbox Code Playgroud)
这也没用.
第三次更新:我使用了Adam和我的建议,即使它没有按预期工作,它肯定会在我开始事件后立即将值"active"更改为0.想法?
解决方案:感谢Adam,我将他的代码用于IF语句: …
这可能是一个很容易回答的问题,但我还没有弄清楚正确的方法是什么。
我需要通过正则表达式匹配文本,使用^ and $仅匹配以该字符串开头和结尾的元素。但是,我需要能够使用变量来做到这一点:
var name // some variable
var re = new RegExp(name,"g");
Run Code Online (Sandbox Code Playgroud)
所以我想匹配包含完全(从头到尾)我的变量的每个字符串name,但我不想匹配包含在某个地方我的变量的字符串name。
我该怎么做?
谢谢
python ×4
javascript ×3
django ×2
numpy ×2
arrays ×1
choicefield ×1
datetime ×1
division ×1
events ×1
firefox ×1
iteration ×1
lodash ×1
login ×1
mysql ×1
regex ×1
right-click ×1
scipy ×1
settimeout ×1
statsmodels ×1
templates ×1
typescript ×1
xpath ×1