在我的Android应用程序中,我想列出可以单击的选项.在您单击的任何项目上,它总是会将您带到相同的下一个屏幕,但使用不同的.putextra().我可以使用简单的ListAdapter 来完成此操作.
这很好,但我确实希望在应用程序中为不同语言翻译这些值.我想这意味着我需要在strings.xml中定义listview中的所有值.我当然可以在strings.xml中定义一些值,但我不知道从哪里开始从strings.xml获取值到listview中.
有谁知道我怎么能将可翻译的值变成Android ListView?欢迎任何提示!
我的问题可能有点奇怪,但我不知道怎么说.情况是我有一个以元组为键的字典:
{(1, 2): 'a', (3, 4): 'b', (5, 6): 'c'}
Run Code Online (Sandbox Code Playgroud)
我现在想要得到dict中元组中所有值的列表(或元组).所以结果应该是这样的:
[1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
有人知道如何以高效和pythonic的方式做到这一点吗?
欢迎所有提示!
[编辑]看到我得到了很多回答,还有一些额外的问题:
我不关心订单,但应该删除可能的重复.
我在html中有一个简单的表单:
<form action="" method="post">
<input id="title" name="title" size="30" type="text" value="">
<input type="submit" value="Save this stuff">
</form>
Run Code Online (Sandbox Code Playgroud)
我还在页面上有一个文件上传文件,它使用ajax处理上传并将文件添加到mongoDB.文件上传返回文件的mongoDB id(例如12345
),我想将该id作为隐藏字段添加到表单中,以便在提交表单时将id发布到服务器.由于用户可以添加多个文件,我想在表单中添加一个id列表.
我发现如何从javascript向表单添加一个隐藏字段,但这总是处理单个字段,而不是具有多个值的字段.所以我想在表单中添加一个复选框字段,以便我可以在一个元素中提交多个值,但它有点像黑客.
任何人都可以向我提示如何使用Javascript向表单添加隐藏的值列表?欢迎所有提示!
[编辑]最后我希望表单看起来像这样:
<form action="" method="post">
<input type="hidden" name="ids" value="[123, 534, 634, 938, 283, 293]">
<input id="title" name="title" size="30" type="text" value="">
<input type="submit" value="Save this stuff">
</form>
Run Code Online (Sandbox Code Playgroud) 在我使用Flask构建的网站中,人们可以互相发送PM.当用户收到pm时,我现在想要实现类似于StackOverflow的通知.由于SO使用websockets实现了这一点,我使用本教程开始使用websockets来实现Flask-socketIO(适用于Socket.io).
我下载了教程制作的示例,我理解其中的代码.但我不明白的是:
所以,假设我有一个简单的路由,人们可以将PM发布给另一个用户:
@app.route('/admin/pm', methods=['GET', 'POST'])
@login_required
def pms():
if request.method == 'POST':
savePM(g.user.id, request.form['toUserId'], request.form['text'])
# How do I emit a message here to the user to whom this message is sent?
return render_template('sendPM.html')
Run Code Online (Sandbox Code Playgroud)
我的评论已经说明了:如何向该用户发送此消息的用户发送消息?欢迎所有提示!
[编辑]按照Miguel的提示,我想到创建一个名为的房间user.id
,所以我现在创建了以下连接和断开事件:
@socketio.on('connect', namespace='/test')
@login_required
def websocketConnect():
join_room(g.user.id)
emit('my response', {'data': 'Connected'}, room=g.user.id)
@socketio.on('disconnect', namespace='/test')
@login_required
def websocketDisconnect():
leave_room(g.user.id)
print('Client disconnected')
Run Code Online (Sandbox Code Playgroud)
但在连接时,我得到下面的堆栈跟踪.是不是g
用socketio路由创建了对象?
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/gevent/greenlet.py", …
Run Code Online (Sandbox Code Playgroud) 我对我的js感到困惑.通常我定义这样的函数:
function f(){
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
但我也可以定义这样的函数:
f = function(){
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
我一直认为他们之间没有区别,但我现在发现这是有效的:
f = function(){
alert('IT WORKS!!');
}
function createCallback(request){
request.done(function(data){
var html = '';
data['result'].forEach(function(bill){
html += "<tr onclick=\"f();\"><td>" + bill.title + "</td></tr>";
});
$("#someId").html(html);
});
}
Run Code Online (Sandbox Code Playgroud)
但是当我定义f
如下:
function f(){
alert('IT WORKS!!');
}
Run Code Online (Sandbox Code Playgroud)
然后我点击该行,它给出了一个ReferenceError: f is not defined
.
所以我想知道:function f(){}
和之间的区别是f = function(){}
什么?
我正在用Python编写一些单元测试,现在我有一个相当奇怪的错误.我比较两个字符串,看起来完全相同,但我得到一个断言错误.下面的代码打印出我后面评论的内容:
print type(a), len(a), a # <type 'unicode'> 12 € 290.000,00
print type(b), len(b), b # <type 'unicode'> 12 € 290.000,00
print a == b # False
Run Code Online (Sandbox Code Playgroud)
因此,为了找出哪些字符不同,我将它们打印出来并按字符比较字符串:
for enum, i in enumerate(a):
print a[enum], b[enum], a[enum] == b[enum]
Run Code Online (Sandbox Code Playgroud)
打印出来:
€ € True
False # <== THE SPACE IS NOT EQUAL?!!?
2 2 True
9 9 True
0 0 True
. . True
0 0 True
0 0 True
0 0 True
, , True
0 0 True
0 0 …
Run Code Online (Sandbox Code Playgroud) 正如标题所说:
>>> from subprocess import check_output
>>> check_output(['ln', '~/other_folder/src/models/sc_models.py', './src/models/sc_models.py'])
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['ln', '~/other_folder/src/models/sc_models.py', './src/models/sc_models.py']' returned non-zero exit status 1
>>> exit()
$ ln ~/other_folder/src/models/sc_models.py ./src/models/sc_models.py
$
Run Code Online (Sandbox Code Playgroud)
怎么会这样?如何从命令行取得成功,但是从Python子进程调用失败了?
欢迎所有提示!
我正在使用(很棒的)Flask 框架来构建网站,但我现在遇到了 html 无法正确呈现的问题。我的模板中有一行带有 if-else 取决于public
变量是否为 True:
{{ theInfo if public else '<span style="background-color: green;">this info is hidden</span>' }}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这只是在浏览器中显示 html,而不是渲染它。我是否需要以某种方式让 Jinja 知道它的 html 应该被渲染?
欢迎所有提示!
我有一些Python代码如下:
for emailCredentials in emailCredentialsList:
try:
if not emailCredentials.valid:
emailCredentials.refresh()
except EmailCredentialRefreshError as e:
emailCredentials.active = False
emailCredentials.save()
# HERE I WANT TO STOP THIS ITERATION OF THE FOR LOOP
# SO THAT THE CODE BELOW THIS DOESN'T RUN ANYMORE. BUT HOW?
# a lot more code here that scrapes the email box for interesting information
Run Code Online (Sandbox Code Playgroud)
正如我已经在代码中注释的那样,如果EmailCredentialRefreshError
抛出,我希望for循环的这个迭代停止并移动到下一个项目emailCredentialsList
.我不能使用a,break
因为这将停止整个循环,它不会覆盖循环中的其他项目.我当然可以将所有代码包装在try/except中,但我希望将它们保持在一起以便代码保持可读性.
解决这个问题的最恐怖的方法是什么?
我有一个有.count()
方法的对象列表.我现在想要所有对象的总数.以下作品:
count = 0
for the_obj in obj_list:
count += the_obj.count()
print count
Run Code Online (Sandbox Code Playgroud)
但我觉得有更多的Pythonic方法可以做到这一点.有人有小费吗?
python ×7
flask ×2
html ×2
javascript ×2
android ×1
bash ×1
break ×1
count ×1
dictionary ×1
exit-code ×1
forms ×1
function ×1
hidden-field ×1
jinja2 ×1
jquery ×1
list ×1
listview ×1
ln ×1
loops ×1
python-2.7 ×1
socket.io ×1
string ×1
structure ×1
subprocess ×1
syntax ×1
try-except ×1
tuples ×1
unicode ×1
unit-testing ×1
websocket ×1
xml ×1