我想要cp一个目录,但我不想覆盖任何现有的文件,即使它们比复制的文件旧.而且我想完全不受欢迎,因为这将是Crontab Bash脚本的一部分.有任何想法吗?
假设connectionDetails是一个Python字典,那么重构这样的代码的最好,最优雅,最"pythonic"的方法是什么?
if "host" in connectionDetails:
host = connectionDetails["host"]
else:
host = someDefaultValue
Run Code Online (Sandbox Code Playgroud) 我有以下javaScript"class":
A = (function() {
a = function() { eval(...) };
A.prototype.b = function(arg1, arg2) { /* do something... */};
})();
Run Code Online (Sandbox Code Playgroud)
现在让我们假设在eval()中我传递的字符串包含调用带有一些参数的表达式:
b("foo", "bar")
Run Code Online (Sandbox Code Playgroud)
但后来我得到b没有定义的错误.所以我的问题是:如何在A类语境中调用eval?
我是virtualenv的新手,但我正在编写django应用程序,最后我将不得不以某种方式部署它.
所以我们假设我的应用程序在我的本地virtualenv上工作,我安装了所有必需的库.我现在要做的是运行某种脚本,这将采用我的virtualenv,检查内部安装的内容并生成一个脚本,将所有这些库安装在其他机器上的新virtualenv上.怎么做到这一点?请帮忙.
让我这样说:
model.py:
class Task(models.Model):
...
seq_file = models.FileField(upload_to='files/', blank=True, null=True)
...
Run Code Online (Sandbox Code Playgroud)
ajax.py(我使用的是dajaxice,但没关系):
...
def startTask(request, name):
task = Task.objects.get(task_name=name)
data = task.seq_file.open()
filename = os.path.join(settings.MEDIA_ROOT ,task.seq_file.name)
if not os.path.isfile(filename):
raise Exception, "file " + filename + " not found."
sequences = parser.parse(data.read())
...
Run Code Online (Sandbox Code Playgroud)
这会返回:
File "/home/mnowotka/Dokumenty/MgrFuncAdnot/app/django-gui/src/gui/ajax.py", line 43, in startTask
sequences = parser.parse(data.read())
AttributeError: 'NoneType' object has no attribute 'read'
Run Code Online (Sandbox Code Playgroud)
但:
...
def startTask(request, name):
task = Task.objects.get(task_name=name)
filename = os.path.join(settings.MEDIA_ROOT ,task.seq_file.name)
if not os.path.isfile(filename):
raise Exception, "file " + …Run Code Online (Sandbox Code Playgroud) 我有很长的文件需要解析.因为它很长,我需要通过块来做大块.我试过这个:
function parseFile(file){
var chunkSize = 2000;
var fileSize = (file.size - 1);
var foo = function(e){
console.log(e.target.result);
};
for(var i =0; i < fileSize; i += chunkSize)
{
(function( fil, start ) {
var reader = new FileReader();
var blob = fil.slice(start, chunkSize + 1);
reader.onload = foo;
reader.readAsText(blob);
})( file, i );
}
}
Run Code Online (Sandbox Code Playgroud)
运行后我只看到控制台中的第一个块.如果我将'console.log'更改为jquery附加到某个div,我只看到该div中的第一个块.其他块怎么样?如何使它工作?
这是使用deffered/promise实现某些异步函数超时的常见模式:
// Create a Deferred and return its Promise
function timeout(funct, args, time) {
var dfd = new jQuery.Deferred();
// execute asynchronous code
funct.apply(null, args);
// When the asynchronous code is completed, resolve the Deferred:
dfd.resolve('success');
setTimeout(function() {
dfd.reject('sorry');
}, time);
return dfd.promise();
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以执行一些调用的异步函数myFunc并处理超时:
// Attach a done and fail handler for the asyncEvent
$.when( timeout(myFunc, [some_args], 1000) ).then(
function(status) {
alert( status + ', things are going well' );
},
function(status) {
alert( status + ', …Run Code Online (Sandbox Code Playgroud) 我正在使用带有会话的python请求库:
def _get_session(self):
if not self.session:
self.session = requests.Session()
return self.session
Run Code Online (Sandbox Code Playgroud)
有时我会在日志中收到此警告:
[2014/May/12 14:40:04 WARNING ] HttpConnectionPool is full, discarding connection: www.ebi.ac.uk
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么这是警告而不是例外?
这是负责此的代码(来自http://pydoc.net/Python/requests/0.8.5/requests.packages.urllib3.connectionpool/):
def _put_conn(self, conn):
try:
self.pool.put(conn, block=False)
except Full:
# This should never happen if self.block == True
log.warning("HttpConnectionPool is full, discarding connection: %s"
% self.host)
Run Code Online (Sandbox Code Playgroud)
为什么会遇到这种异常?如果它被重新启动,我可以在我的代码中处理此异常,方法是创建新会话并删除旧会话.
如果它只是一个警告,是否意味着它不会以任何方式影响我的结果?我可以忽略它吗?如果没有,我该如何处理这种情况?
我有模块foo,在这个模块里面我动态创建了类:
def superClassCreator():
return type("Bar", (object,), {})
Run Code Online (Sandbox Code Playgroud)
现在,我想要实现的是使这个新的动态类作为这个模块的一个类可见:
import foo
dir(foo)
>>> [... 'Bar' ...]
Run Code Online (Sandbox Code Playgroud)
你知道怎么做吗?
让我说我想以同步方式处理一些任务,所以我有这个功能:
function executePromiseQueueSync(queue){
var seed = $.Deferred(),
finalPromise;
finalPromise = _.reduce(queue, function(memo, promise){
return memo.then(function(){
return promise.funct.apply(null, promise.argmnt);
});
}, seed.promise());
seed.resolve();
return finalPromise;
}
Run Code Online (Sandbox Code Playgroud)
现在我可以用它来处理一些文件:
_.each(fileList, function(element, index, list){
_.each(element, function(el, idx, lst){
promisesQueue.push({funct: processFile, argmnt:[el, index + (len - fileList.length) ,len]});
});
});
Run Code Online (Sandbox Code Playgroud)
执行它并指出进度:
executePromiseQueueSync(promisesQueue).then(function(){
....
}, function(){
....
}).progress(function(msg, progress, name, index, status, desc){
console.log('progress');
});
Run Code Online (Sandbox Code Playgroud)
流程函数本身如下所示:
function processFile(file, index, size)
{
var dfd = new jQuery.Deferred();
if (file.name.match('(.*)\\.jpg'))
...
else if
...
else
$.when(processWrongFileType(file)).then(function(){
dfd.notify(...);
dfd.resolve(); …Run Code Online (Sandbox Code Playgroud) python ×5
javascript ×4
deferred ×2
django ×2
jquery ×2
bash ×1
class ×1
coding-style ×1
cp ×1
dictionary ×1
filereader ×1
grequests ×1
html5 ×1
httplib ×1
linux ×1
module ×1
parsing ×1
promise ×1
virtualenv ×1