我有这个
bc = 'off'
if c.page == 'blog':
bc = 'on'
print bc
Run Code Online (Sandbox Code Playgroud)
在python中有更多的pythonic(和/或更短)的写法吗?
我试图理解为什么在javascript中,您可能想要更改函数的上下文.我正在寻找一个真实世界的例子或某些东西,它将帮助我理解这项技术的使用方式/原因以及它的意义.
使用此示例说明了该技术(来自http://ejohn.org/apps/learn/#25)
var object = {};
function fn(){
return this;
}
assert( fn() == this, "The context is the global object." );
assert( fn.call(object) == object, "The context is changed to a specific object." );
Run Code Online (Sandbox Code Playgroud) 我们已经为移动设备准备了一个替代版本的网站.我们服务的内容不同,效果也很好.
检测要投放的版本的最佳方法是什么.我们没有所有移动设备的列表,因此使用用户代理标头很棘手,因为我们可能会遗漏一些东西.
我们考虑过使用设备屏幕宽度 - 但如果移动设备不支持javascript会发生什么.我们怎么闻这个?
这很简单,不应该是一个问题,但我不明白这里发生了什么.
我有以下代码
class DashboardController < ApplicationController
def bookings
@bookings = Booking.all
end
end
Run Code Online (Sandbox Code Playgroud)
/views/dashboard/bookings.html.erb
<%= render 'booking', :collection => @bookings %>
Run Code Online (Sandbox Code Playgroud)
/views/dashboard/_booking.html.erb
<%= booking.booking_time %>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
undefined method `booking_time' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
但是,如果我在/views/dashboard/_bookings.html.erb中执行此操作
<% @bookings.each do |booking| %>
<%= render 'booking', :booking => booking %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我得到(正确的)
2012-12-19 09:00:00 UTC
2012-12-28 03:00:00 UTC
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?我真的想使用:这里定义的集合 http://guides.rubyonrails.org/layouts_and_rendering.html
我想用Ruby或Python编写一个脚本来查看目录和监视器的内容
1. Any new files
2. Any saves to existing files.
Run Code Online (Sandbox Code Playgroud)
我并不担心文件中的不同之处 - 只是它们已被保存或是新的.
然后脚本返回文件的名称(带有完整路径),可能每2秒钟返回一次.
我想扫描文件夹中的文件
mainfile.txt
images/image1.jpg
images/icons/icon1.png
Run Code Online (Sandbox Code Playgroud)
需要监视所有这些文件/文件夹.
我最终想要FTP已经更改/已添加的文件,但我很乐意解决问题的第一部分的解决方案,因为我知道如何ftp(但如果你想的话,无论如何包括这个!)
我使用的是OSX 10.5.8
我在这里使用这个例子
https://github.com/zeit/next.js#custom-document
Run Code Online (Sandbox Code Playgroud)
我想更改 body 标签上的 custom_class
我试过在调用组件时传递道具,但没有任何效果。我想根据某些条件添加一个“暗”类,但我不知道如何更改。
编辑:
我不确定这是可能的。从 nextjs slack 频道获得帮助后,我被告知
"Pages get rendered client side with next/link
And body is not touched after server side rendering"
Run Code Online (Sandbox Code Playgroud)
我将尝试将内容包装在我生成的另一个标签中并尝试更改它。
这应该很简单.我只是想不通.
如何使用javascript从这段JSON中获取最大值.
{"data":{"one":21,"two":35,"three":24,"four":2,"five":18},"meta":{"title":"Happy with the service"}}
Run Code Online (Sandbox Code Playgroud)
我需要的关键和价值是:
"two":35
Run Code Online (Sandbox Code Playgroud)
因为它是最高的
谢谢
我只是在学习骨干.我有以下内容
window.ServerList = Backbone.Collection.extend({
model: Server,
cpuTotal: function(){
if (!this.length) return 0;
/*
* NOT SURE HOW TO SUM THEM
* this.get('cpu') is an integer for each of the collections
*/
return this.get('cpu');
}
});
Run Code Online (Sandbox Code Playgroud)
我是从像这样的视图的render方法调用它
window.AppView = Backbone.View.extend({
// ....
render: function(){
var total_cpu = ServerList.cpuTotal();
var items = ServerList.length;
}
});
Run Code Online (Sandbox Code Playgroud)
变量total_cpu始终为空,但项始终正确.有任何想法吗 ?
我知道我的收藏正在工作,因为我有很多项目,但我需要从集合中的每个项目中添加所有CPU的页面摘要.
对于那些知道todos示例http://documentcloud.github.com/backbone/docs/todos.html的人我有一个非常相似的设置.
我有这个优秀的宝石https://github.com/alexreisner/geocoder在我的应用程序中正常工作,但我需要略微不同地订购活动记录查询的结果.
例如,使用rails控制台
$> Person.near('London',10)
Run Code Online (Sandbox Code Playgroud)
生成一个长SQL查询(不相关)后跟
ORDER BY distance ASC
Run Code Online (Sandbox Code Playgroud)
这给出了一个很好的有序人员列表,从最近到最远(最远10英里).现在,如果我这样做
$> Person.near('London',10).order('price_per_hour')
Run Code Online (Sandbox Code Playgroud)
查询附加
ORDER BY distance ASC, price_per_hour
Run Code Online (Sandbox Code Playgroud)
我仍然想要伦敦附近的所有人,但我不希望它按距离排序.似乎无论如何都没有把它拿出来.
我无法在活动记录查询之外进行排序,因为我正在使用分页,因此需要在主查询中进行排序,然后对其进行分页.
我在C#中有以下函数,一个哈希函数,我需要将其转换为PHP.我在PHP中尝试过一些东西,但是我没有得到相同的结果(我对.NET不是很好)
private static string GetSignature(string args, string privatekey)
{
var encoding = new System.Text.ASCIIEncoding();
byte[] key = encoding.GetBytes(privatekey);
var myhmacsha256 = new HMACSHA256(key);
byte[] hashValue = myhmacsha256.ComputeHash(encoding.GetBytes(args));
string hmac64 = Convert.ToBase64String(hashValue);
myhmacsha256.Clear();
return hmac64;
}
Run Code Online (Sandbox Code Playgroud)
PHP中的一次(错误)尝试是这样的:
function encode($data,$key)
{
return base64_encode( hash_hmac('sha256', $data, $key ) );
}
Run Code Online (Sandbox Code Playgroud)
答案
DampeS8N下面建议的略有不同,对我有用.
function encode($data,$key)
{
iconv_set_encoding("input_encoding", "ASCII");
iconv_set_encoding("internal_encoding", "ASCII");
iconv_set_encoding("output_encoding", "ASCII");
return base64_encode( hash_hmac('sha256', $data, $key, true ) );
}
Run Code Online (Sandbox Code Playgroud)
请不要使用hash_hmac的第四个参数 - 现在将原始输出设置为true作为二进制数据
javascript ×4
python ×2
activerecord ×1
backbone.js ×1
c# ×1
filesystems ×1
function ×1
hmac ×1
html ×1
if-statement ×1
json ×1
macos ×1
next.js ×1
php ×1
reactjs ×1
redirect ×1
ruby ×1
scope ×1
sorting ×1
user-agent ×1