刚开始操场.我正在尝试创建一个简单的应用程序.
我已经创建了一个这样的日期对象:
var date = NSDate()
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得当前时间?在其他语言中,我可以这样做:
var hour = date.hour
Run Code Online (Sandbox Code Playgroud)
但我找不到任何类似的属性/方法.我找到了一种方法dateWithCalendarFormat.我应该用吗?如果是这样,怎么样?
我怎么能每分钟运行一个功能?在JavaScript中我可以做类似的事情setInterval,在Swift中存在类似的东西吗?
通缉输出:
你好世界每分钟一次......
这真让我抓狂.我做了一些奇怪的事情,似乎我的TEMPLATE_DIRS条目被忽略了.我只有一个settings.py文件,位于项目目录中,它包含:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'web_app/views/'),
)
Run Code Online (Sandbox Code Playgroud)
我将项目级模板放在/ templates文件夹中,然后在我的应用程序文件夹中包含不同视图类别的文件夹(例如,身份验证视图,帐户视图等).
例如,我的主索引页面视图位于web_app/views/main/views_main.py中,看起来像
from web_app.views.view_classes import AuthenticatedView, AppView
class Index(AppView):
template_name = "main/templates/index.html"
Run Code Online (Sandbox Code Playgroud)
其中AppView只是TemplateView的扩展.这是我的问题:当我尝试访问该页面时,我得到一个TemplateDoesNotExist异常,而让我感到困惑的部分是Template-Loader Postmortem:
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django\contrib\admin\templates\main\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\main\templates\index.html (File does not exist)
Run Code Online (Sandbox Code Playgroud)
为什么世界上没有搜索"模板"和"web_app/views"目录?我通过调试器和views_main.py中的断点检查了设置,看起来它们就在那里.有人有过类似的问题吗?谢谢.
我刚安装了ASP.NET 5并在Visual Studio中创建了一个控制台应用程序.我已经将一个文件config.json添加到项目的根文件夹中.
它看起来像这样:
{
"Data": {
"TargetFolderLocations": {
"TestFolder1": "Some path",
"TestFolder2": "Another path"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的Program.cs看起来像这样
public void Main(string[] args)
{
var configurationBuilder = new ConfigurationBuilder(Environment.CurrentDirectory)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = configurationBuilder.Build();
//Doesn't work...null all the time
var test = Configuration.Get("Data:TargetFolderLocations");
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
如何使用代码访问TargetFolderLocations键?
我正在将我的C#代码移植到F#库中.我的C#库中有以下接口/类:
public interface IMatch<out T> where T : IGame
{
IEnumerable<T> Games { get; }
}
public interface IGame
{
string Name { get; }
}
public class SoccerMatch : IMatch<SoccerGame>
{
public SoccerMatch(IEnumerable<SoccerGame> games)
{
Games = games;
}
public IEnumerable<SoccerGame> Games { get; }
}
public class SoccerGame : IGame
{
public SoccerGame(string name)
{
Name = name;
}
public string Name { get; }
}
Run Code Online (Sandbox Code Playgroud)
我试图将其移植到F#,这就是我想出来的:
type IGame =
abstract member Name: string with get
type IMatch<'T …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习NodeJS和Express.我使用node-localstorage包来访问localstorage.这在直接在这样的函数中使用代码时有效
路线/ social.js
exports.index = function(req, res)
{
if (typeof localStorage === "undefined" || localStorage === null)
{
var LocalStorage = require('node-localstorage').LocalStorage;
localStorage = new LocalStorage('./scratch');
}
localStorage.setItem('myFirstKey', 'myFirstValue');
console.log(localStorage.getItem('myFirstKey'));
res.render('social/index', {title: "Start"});
}
Run Code Online (Sandbox Code Playgroud)
但是,在访问localstorage时,我不想在所有其他函数中反复编写此代码.我希望能够注册一个我可以访问的辅助函数
var localStorage = helpers.getLocalStorage
Run Code Online (Sandbox Code Playgroud)
或类似的东西.
我怎样才能在NodeJS中做到这一点?我见过关于app.locals的一些事情?但是如何在路线中访问app对象?
我正在使用django嵌入式视频,因此当用户放置youtube链接视频时,我可以使用视频的缩略图
<img src="{{ my_video.thumbnail }}" class="img-rounded" alt="?" height="75" width="75"/>
Run Code Online (Sandbox Code Playgroud)
但是当用户插入不是youtube视频的链接时,我想插入默认图像.
目前这就是我所拥有的
{% if post.main_image %} //if post has main_image
<img src="{{post.get_image_url}}" class="img-rounded" alt="?" height="75" width="75"/>
{% elif post.url %} //if post has url
{% video post.video as my_video %}
{% if my_video %}//if that url is an link to video
<img src="{{ my_video.thumbnail }}" class="img-rounded" alt="?" height="75" width="75"/>
{% elif %} //if that url isn't a video
<img src="{{post.image}}" class="img-rounded" alt="? EBAGU" height="75" width="75"/>
{% endif %}
{% endvideo %} …Run Code Online (Sandbox Code Playgroud) 给出以下重定向列表
[
{
"old": "a",
"target": "b"
},
{
"old": "b",
"target": "c"
},
{
"old": "c",
"target": "d"
},
{
"old": "d",
"target": "a"
},
{
"old": "o",
"target": "n"
},
{
"old": "n",
"target": "b"
},
{
"old": "j",
"target": "x"
},
{
"old": "whatever",
"target": "something"
}
]
Run Code Online (Sandbox Code Playgroud)
在这里我们可以看到第一个项目“a”应该重定向到“b”。如果我们遵循该列表,我们可以看到以下模式:
a -> b
b -> c
c -> d
d -> a
Run Code Online (Sandbox Code Playgroud)
因此,我们最终会得到一个循环引用,因为“a”最终会指向“d”,而“d”会指向“a”。
查找循环引用的最有效方法是什么?
我用 C# 提出了以下算法
var items = JsonConvert.DeserializeObject<IEnumerable<Item>>(json)
.GroupBy(x => x.Old)
.Select(x => x.First())
.ToDictionary(x …Run Code Online (Sandbox Code Playgroud) 我试图将MongoDB与Django一起使用.我已按照本指南进行设置,以便安装所有必要的东西.
MongoDB + Django教程
我的问题如下:尝试cities = City.objects.get()在我运行时views.py遇到以下错误:
DoesNotExist at /GetAllCities/
City matching query does not exist.
Run Code Online (Sandbox Code Playgroud)
我的MongoDB看起来像这样
Databasename = "exjobb"
Collectioname = "cities"`
Run Code Online (Sandbox Code Playgroud)
它包含30,000行数据,它适用于我的Rails和PHP应用程序.
我的模型类看起来像这样
from django.db import models
from django.core.urlresolvers import reverse
from djangotoolbox.fields import ListField, EmbeddedModelField
# Create your models here.
class City(models.Model):
city = models.TextField()
loc = models.TextField()
population = models.IntegerField()
state = models.TextField()
_id = models.IntegerField()
def __unicode__(self):
return self.city
Run Code Online (Sandbox Code Playgroud)
数据库中的一行看起来像这样
{
"city" : "ACMAR",
"loc" : [
-86.51557,
33.584132
],
"population" …Run Code Online (Sandbox Code Playgroud) 我有一个查询,IGetHamburgers它调用外部API.我IGetHamburgers在我的DI容器中注册了Singleton的实现.我使用Polly作为断路器,如果两个请求失败,电路将打开.
我的目标是所有对Hamburger api的调用应该通过相同的断路器,如果GetHamburgers失败,那么所有其他调用也应该失败.
我该如何使用我的保单?我应该将我的政策注册为如下字段:
private Policy _policy;
private Policy Policy
{
get
{
if(this_policy != null)
{
return this_policy;
}
this._policy = Policy
.Handle<Exception>()
.CircuitBreaker(2, TimeSpan.FromMinutes(1));
return this._policy;
}
}
public object Execute(.......)
{
return Policy.Execute(() => this.hamburgerQuery.GetHamburgers());
}
Run Code Online (Sandbox Code Playgroud)
要么
public object Execute(.......)
{
var breaker = Policy
.Handle<Exception>()
.CircuitBreaker(2, TimeSpan.FromMinutes(1));
return breaker.Execute(() => this.hamburgerQuery.GetHamburgers());
}
Run Code Online (Sandbox Code Playgroud)
我想第一个选项是正确的方法,因为那时Policy对象将始终是相同的,并可以跟踪异常计数和类似的东西.我的问题是,第二种选择也会起作用吗?我在Pollys Github上发现了很多样本/例子,但是我找不到任何"现实世界"的例子,其中Polly与DI一起使用以及类似的东西?
将函数的签名更改ConfigureServices为异步后(最初它只是一个无效的同步函数并且应用程序运行良好),我收到以下错误:
无法找到所需的服务。请通过在应用程序启动代码中的调用
IServiceCollection.AddAuthorization内部调用来添加所有必需的服务ConfigureServices(...)。
下面是我的ConfigureServices函数的代码。
// This method gets called by the runtime. Use this method to add services to the container.
public async Task ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
// Create the necessary Cosmos DB infrastructure
await CreateDatabaseAsync();
await CreateContainerAsync();
}
Run Code Online (Sandbox Code Playgroud)
ConfigureServices 在运行时自动调用。
我有以下型号:
class InfoBox(models.Model):
type = models.ForeignKey(InfoBoxType)
content = models.TextField()
product = models.ForeignKey(Product)
Run Code Online (Sandbox Code Playgroud)
我想在我的外键中添加一个css类.我尝试过以下方法:
forms.py
class InfoBoxForm(ModelForm):
class Meta:
model = InfoBox
widgets = {
'type': ChoiceField(attrs={'class': 'hej'}),
}
Run Code Online (Sandbox Code Playgroud)
我也试过了,但结果相同......
class InfoBoxForm(forms.Form):
type = forms.ChoiceField(
widget=forms.ChoiceField(attrs={'class':'special'}))
Run Code Online (Sandbox Code Playgroud)
admin.py
class InfoBoxInline(admin.StackedInline):
model = InfoBox
extra = 0
form = InfoBoxForm
Run Code Online (Sandbox Code Playgroud)
但我只是得到这个:
__init__() got an unexpected keyword argument 'attrs'
Run Code Online (Sandbox Code Playgroud)
我觉得这样做很容易,所以我做错了什么......?