我正在寻找一种方法从Python列表中删除重复的条目,但有一个扭曲; 最终列表必须区分大小写,并且首选大写单词.
例如,我cup和之间Cup只需要保持Cup而不是cup.与其他建议lower()首先使用的常见解决方案不同,我更喜欢在这里维护字符串的情况,特别是我更喜欢将大写字母保留在小写的字母上.
我再次尝试将此列表转为:
[Hello, hello, world, world, poland, Poland]
进入这个:
[Hello, world, Poland]
我该怎么办?
提前致谢.
我正在尝试配置我的django settings.py以正确使用python日志工具,但我偶然发现了一个相当奇怪的问题:
即使在阅读了文档之后,我也无法找到如何将控制台打印的调试请求行从Django重定向到我指定的文件; 以下是我的日志记录配置的一部分.
LOGGING = {
'version': 1,
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s'
},
}
'handlers': {
'file_http': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': r'C:\mysystem-http.log',
'formatter': 'verbose'
}
},
'loggers': {
'django.request': {
'handlers': ['file_http'],
'level': 'DEBUG',
'propagate': False
}
}
}
Run Code Online (Sandbox Code Playgroud)
我一直看到以下格式的控制台打印行:
[19/Dec/2014 11:48:03] "POST /api/v1/ HTTP/1.1" 200 10
如何使用日志工具将这些重定向到文件?
提前致谢
我已经建立了一个 Django 项目,它利用django-rest-framework来提供一些 ReST 功能。网站和其他功能都运行良好。
然而有一个小问题:我需要我的 API 端点指向不同的子域。
例如,当用户访问网站时,他/她可以根据我的正常导航urls.py:
http://example.com/control_panel
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很好。然而,当使用 API 时,我想将其更改为更合适的东西。因此,http://example.com/api/tasks我需要将其变为:
http://api.example.com/tasks
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
提前致谢。
PS 该网站将在 Gunicorn 上运行,并使用 nginx 作为反向代理。
我在Celery中有很多任务都使用了canvas chain.
@shared_task(bind=True)
def my_task_A(self):
try:
logger.debug('running task A')
do something
except Exception:
run common cleanup function
@shared_task(bind=True)
def my_task_B(self):
try:
logger.debug('running task B')
do something else
except Exception:
run common cleanup function
...
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.问题是我正在寻找使用这样的通用实用程序函数的最佳实践:
def cleanup_and_notify_user(task_data):
logger.debug('task failed')
send email
delete folders
...
Run Code Online (Sandbox Code Playgroud)
没有任务阻止,最好的办法是什么?例如,我可以run common cleanup function用一个电话替换cleanup_and_notify_user(task_data)吗?如果来自多个工作人员的多个任务试图同时调用该函数会发生什么?
每个工人都有自己的副本吗?我显然对这里的一些概念感到困惑.任何帮助深表感谢.
谢谢大家.
我最近遇到了Axel Rauschmayer博士的这篇精彩文章:
http://www.2ality.com/2015/02/es6-classes-final.html
下面的代码片段大致描述了ECMAScript 6原型链如何从ECMAScript 5的角度来看(原帖的第4.2节):
// ECMAScript 6
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
···
}
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y);
this.color = color;
}
···
}
let cp = new ColorPoint(25, 8, 'green');
Run Code Online (Sandbox Code Playgroud)
ECMAScript 5中的"引擎盖下"视图:
// ECMAScript 5
// Instance is allocated here
function Point(x, y) {
// Performed before entering this constructor:
this = Object.create(new.target.prototype);
this.x …Run Code Online (Sandbox Code Playgroud) 这是昨天在TC39上提出的.你可以在这里找到要点:
var p = () => console.log(f);
{
p(); // undefined
console.log(f); // function f(){}
f = 1;
p(); // undefined
console.log(f); // 1
function f(){}
p(); // 1
console.log(f); // 1
f = 2;
p(); // 1
console.log(f); // 2
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释这件事是如何运作的吗?对于记录,它仅在非严格模式下工作.
谢谢.
在David Black的The Well-Grounded Rubyist中,我遇到了以下关于枚举器的Ruby代码:
e = Enumerator.new do |y|
puts "Starting up the block!"
(1..3).each {|i| y << i }
puts "Exiting the block!"
end
p e.to_a
Run Code Online (Sandbox Code Playgroud)
返回以下输出:
Starting up the block!
Exiting the block!
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
让我困扰的是,我无法围绕执行的顺序.我相信输出应该更直接:
Starting up the block!
[1, 2, 3]
Exiting the block!
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.
在第2章" 改进 "部分的元编程Ruby 2中,我发现了以下Ruby代码:
class MyClass
def my_method
"original my_method()"
end
def another_method
my_method
end
end
module MyClassRefinement
refine MyClass do
def my_method
"refined my_method()"
end
end
end
using MyClassRefinement
MyClass.new.my_method # => "refined my_method()"
MyClass.new.another_method # => "original my_method()" - How is this possible?
Run Code Online (Sandbox Code Playgroud)
据作者说:
但是,调用
another_method可能抓住你措手不及:即使你打电话another_method后using,调用my_method本身发生之前using-所以它调用该方法的原始的,未精制的版本.
这完全让我失望.
为什么MyClass.new.another_method打印"原始my_method()",因为它使用后using MyClassRefinement,作者试图在这里说什么?
任何人都可以提供更直观/更好的解释吗?
谢谢.
这主要是一个"学术"的,但在这里:
根据这个Ruby eigenclass图(略加编辑):
BasicObject.singleton_class.singleton_class.superclass是Class.
但是,在Ruby解释器(Ruby v2.5.1)上运行它,事实证明它BasicObject.singleton_class.singleton_class.superclass是#<Class:Class>和不是Class.因此,图表是说谎还是我遗漏了什么?
该图来自我在Freenode的Ruby IRC聊天的用户.但是,它被多次引用给许多其他用户,它被视为Ruby对象模型圣经.
我有以下字符串:
'USD 100'
Run Code Online (Sandbox Code Playgroud)
基于这篇文章,我试图捕获100是否USD包含在字符串中或单个(货币)字符如果USD不包含在字符串中。
例如:
'USD 100'
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经解决了这个问题,但它不起作用:
https://rubular.com/r/cK8Hn2mzrheHXZ
有趣的是,如果我把USD它放在似乎有效的数量之后。理想情况下,无论货币字符的位置如何,我都希望拥有相同的行为。
python ×4
ruby ×4
django ×3
ecmascript-6 ×2
javascript ×2
celery ×1
celery-task ×1
closures ×1
duplicates ×1
eigenclass ×1
function ×1
hoisting ×1
list ×1
logging ×1
nginx ×1
refinements ×1
regex ×1
subclass ×1
subdomain ×1
url ×1