看看下面的代码。
我想使用 的值a
作为c
classmethod 声明中参数的默认值my_method()
。
我该怎么做?下面的例子失败了。
>>> class X:
... a = 'hello'
... def __init__(self, b):
... self.b = b
... @classmethod
... def my_method(cls, c=X.a):
... print 'cls.a = {}'.format(cls.a)
... print 'c = {}'.format(c)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in X
NameError: name 'X' is not defined
Run Code Online (Sandbox Code Playgroud) 我有一个 go test 文件,我在其中编写了一个基准函数,如下所示:
func BenchmarkStuff(b *testing.B) {
for i := 0; i < b.N; i++ {
stuff()
}
}
Run Code Online (Sandbox Code Playgroud)
然而,该stuff()
函数每次运行前都需要进行一些设置,每次运行后都需要进行清理。我有功能setup()
和cleanup()
分别执行此操作。但我不想对设置和清理功能进行基准测试。
那么应该在哪里调用它们呢?如果我在 BenchmarkStuff 中调用它们,它们将被添加到结果测量中。但没有它们,stuff()
就会失败。
这是我非常简单的C++函数:
#include <string.h>
void myFunc(void * param)
{
string command;
}
Run Code Online (Sandbox Code Playgroud)
为什么不编译?
% CC -c -o testFunc.o testFunc.C
"testFunc.C", line 6: Error: string is not defined.
1 Error(s) detected.
Run Code Online (Sandbox Code Playgroud) 我正在做一个Django查询.我想知道有多少MyModel有myAttribute值为"X".这就是我这样做的方式:
len(MyModel.objects.filter(myAttribute="X"))
Run Code Online (Sandbox Code Playgroud)
这是处理它的最有效方法吗?我担心这会不必要地从数据库中获取比我需要的更多的数据,而是我应该使用Count()函数.但是,从我看过的例子中我发现我不确定是否可以将Count()与filter()结合起来.有人可以建议吗?
我有一个看起来像这样的 Django 对象:
from datetime import timedelta
class MyObject:
startTime = models.DateTimeField(default=datetime.datetime.utcnow)
Run Code Online (Sandbox Code Playgroud)
现在我想向这个模型添加一个名为 expiredTime 的字段,它会在 startTime 之后自动设置为 1 天。显然这行不通:
expiredTS = models.DateTimeField(default=datetime.datetime.utcnow + timedelta(days=1))
Run Code Online (Sandbox Code Playgroud)
那么我该怎么做呢?
我有一个我正在开发的webapp,它使用HTML + Javascript(Angular 1.5.0)+ CSS的前端.当我在本地主机上运行时,网站运行良好.我有一个/index.html
文件.这个文件导入我的javascript代码,如下所示:<script src="/app/app.js"></script>
但现在我刚刚使用Grunt实现了自动linting,uglifying等.Grunt非常好地将我的单个Javascript文件(/app/app.cs
)转换为minnified one(/dist/js/app.min.js
).我改变了我/index.html
对包括minnified的Javascript来代替:<script src="/dist/js/app.min.js"></script>
.但现在应用程序停止工作.在浏览器的开发控制台中,我看到以下错误:
angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:unpr] Unknown provider: a
http://errors.angularjs.org/1.5.0/$injector/unpr?p0=a
at http://127.0.0.1:8080/bower_components/angular/angular.js:68:12
at http://127.0.0.1:8080/bower_components/angular/angular.js:4397:19
at getService (http://127.0.0.1:8080/bower_components/angular/angular.js:4550:39)
at injectionArgs (http://127.0.0.1:8080/bower_components/angular/angular.js:4574:58)
at Object.invoke (http://127.0.0.1:8080/bower_components/angular/angular.js:4596:18)
at runInvokeQueue (http://127.0.0.1:8080/bower_components/angular/angular.js:4497:35)
at http://127.0.0.1:8080/bower_components/angular/angular.js:4506:11
at forEach (http://127.0.0.1:8080/bower_components/angular/angular.js:321:20)
at loadModules (http://127.0.0.1:8080/bower_components/angular/angular.js:4487:5)
at createInjector (http://127.0.0.1:8080/bower_components/angular/angular.js:4409:19)
http://errors.angularjs.org/1.5.0/$injector/modulerr?p0=raptorApp&p1=Error%…F%2F127.0.0.1%3A8080%2Fbower_components%2Fangular%2Fangular.js%3A4409%3A19)
Run Code Online (Sandbox Code Playgroud)
为什么正常的Javascript文件可以正常工作,但Grunt缩小的文件不会?我该如何解决?
我有一个 Python/ Tornado应用程序,它使用以下 3 个类响应 HTTP 请求:
import tornado.web
class MyClass1(tornado.web.RequestHandler):
x = 1
y = 2
def my_method1(self):
print "Hello World"
class MyClass2(MyClass1):
@tornado.web.authenticated
def get(self):
#Do Something 1
pass
@tornado.web.authenticated
def post(self):
#Do Something 2
pass
class MyClass3(MyClass2):
pass
Run Code Online (Sandbox Code Playgroud)
我希望所有实例MyClass2
的实例变量都m
设置为整数 3。但是任何实例都MyClass3
应该覆盖它并m
设置为整数 4。我该怎么做?
我尝试将以下构造函数分别添加到MyClass2
和MyClass3
,但是当我尝试创建 的实例时MyClass3
,出现以下错误:TypeError: __init__() takes exactly 1 argument (3 given)
我的课堂2。初始化():
def __init__(self):
self.m = 3 # This value …
Run Code Online (Sandbox Code Playgroud) 我有两个python对象a
和b
.
什么是最好/最有效/最pythonic的方法来检查这些对象中究竟有一个是None
什么?
我有一个angularJS指令,通过以下方式调用:
<rpt-closing closing-begin-ts="null" closing-begin-ts="'2014-11-25 23:59:59'"></rpt-closing>
Run Code Online (Sandbox Code Playgroud)
指令代码如下:
.directive('rptClosing',function(){
return {
restrict:'E',
scope: {
closingBeginTs: '=',
closingEndTs: '='
},
link: function(scope, element, attrs) {
console.log('*******************************************');
console.log('scope = ', scope);
console.log('scope.closingBeginTs = ', scope.closingBeginTs);
console.log('scope.closingEndTs = ', scope.closingEndTs);
console.log('*******************************************');
},
template: '<div>BLAH BLAH BLAH</div>'
};
}
)
Run Code Online (Sandbox Code Playgroud)
这段代码在jsFiddle中运行得非常好.我能看到的价值scope.closingBeginTs
,并scope.closingEndTs
在控制台输出.
我有以下字符串表示 UTC 时间戳: 2017-12-03T20:38:00.971261Z
我想将其转换为 Posix 时间戳(即:自纪元以来的秒数)使用此在线转换器(https://www.epochconverter.com/)我知道答案是1512333480
但是,当我执行以下代码时,结果将关闭 1800 秒 - 30 分钟:
>>> temp_time1 = datetime.datetime.strptime('2017-12-03T20:38:00.971261Z', '%Y-%m-%dT%H:%M:%S.%fZ')
>>> ctime = int(datetime.datetime(temp_time1.year,
temp_time1.month,
temp_time1.day,
temp_time1.hour,
temp_time1.minute,
temp_time1.second,
temp_time1.microsecond,
pytz.timezone('Europe/London')).strftime('%s'))
>>> print ctime
1512351480
Run Code Online (Sandbox Code Playgroud)
有人知道我在这里想念什么吗?
python ×5
angularjs ×2
django ×2
javascript ×2
c++ ×1
class-method ×1
datetime ×1
epoch ×1
go ×1
gruntjs ×1
inheritance ×1
tornado ×1