我想尝试做类似的事情:
from collections import defaultdict
import hashlib
def factory():
key = 'aaa'
return { 'key-md5' : hashlib.md5('%s' % (key)).hexdigest() }
a = defaultdict(factory)
print a['aaa']
Run Code Online (Sandbox Code Playgroud)
(实际上,我之所以需要访问工厂中的密钥,不是为了计算md5,而是出于其他原因;这只是一个例子)
正如你所看到的,在工厂里我无法访问密钥:我只是强迫它,这没有任何意义.
是否可以以defaultdict我可以在工厂中访问密钥的方式使用?
我正在经营这样一个芹菜工人:
celery worker --app=portalmq --logfile=/tmp/portalmq.log --loglevel=INFO -E --pidfile=/tmp/portalmq.pid
Run Code Online (Sandbox Code Playgroud)
现在我想在后台运行这个worker.我尝试过几件事,包括:
nohup celery worker --app=portalmq --logfile=/tmp/portal_mq.log --loglevel=INFO -E --pidfile=/tmp/portal_mq.pid >> /tmp/portal_mq.log 2>&1 </dev/null &
Run Code Online (Sandbox Code Playgroud)
但它没有用.我检查了芹菜文档,我发现了这个:
特别是这个评论是相关的:
In production you will want to run the worker in the background as a daemon.
To do this you need to use the tools provided by your platform, or something
like supervisord (see Running the worker as a daemon for more information).
Run Code Online (Sandbox Code Playgroud)
这只是为了在后台运行进程而产生的开销太大.我需要在我的服务器中安装supervisord,并熟悉它.现在不行.有没有一种简单的方法在后台运行芹菜工人?
假设我在python中有这个字典,在模块级别定义(mysettings.py):
settings = {
'expensive1' : expensive_to_compute(1),
'expensive2' : expensive_to_compute(2),
...
}
Run Code Online (Sandbox Code Playgroud)
我希望在访问密钥时计算这些值:
from mysettings import settings # settings is only "prepared"
print settings['expensive1'] # Now the value is really computed.
Run Code Online (Sandbox Code Playgroud)
这可能吗?怎么样?
在我的localhost开发期间,我试图自己托管libphonenumber库.我正在尝试以下方面:
<script src="//closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script>
<script>goog.require('goog.proto2.Message');</script>
<script src="scripts/vendor/pn/phonemetadata.pb.js"></script>
<script src="scripts/vendor/pn/phonenumber.pb.js"></script>
<script src="scripts/vendor/pn/metadata.js"></script>
<script src="scripts/vendor/pn/phonenumberutil.js"></script>
<script src="scripts/vendor/pn/asyoutypeformatter.js"></script>
Run Code Online (Sandbox Code Playgroud)
这是有效的,但我仍然依赖于外部托管的组件:闭包库.我尝试过使用closure-lite,它(显然,我是新来的)是一个非常完整的闭包库版本,可用于自托管.我尝试过以下操作:
<script src="scripts/vendor/closure-lite.js"></script>
<script>goog.require('goog.proto2.Message');</script>
<script src="scripts/vendor/pn/phonemetadata.pb.js"></script>
<script src="scripts/vendor/pn/phonenumber.pb.js"></script>
<script src="scripts/vendor/pn/metadata.js"></script>
<script src="scripts/vendor/pn/phonenumberutil.js"></script>
<script src="scripts/vendor/pn/asyoutypeformatter.js"></script>
Run Code Online (Sandbox Code Playgroud)
但是goog.proto2.Message没有.我收到以下错误:
Uncaught TypeError: Cannot read property 'Message' of undefined
Run Code Online (Sandbox Code Playgroud)
该错误来自phonemetadata.pb.js脚本:
goog.inherits(i18n.phonenumbers.NumberFormat, goog.proto2.Message);
Run Code Online (Sandbox Code Playgroud)
我能做些什么才能完全自我主持libphonenumber?
我创建了一个python3虚拟环境(明确地避免使用符号链接--copies):
» python3 -m venv --without-pip --copies venv
Run Code Online (Sandbox Code Playgroud)
这是我现在完整的虚拟环境:
» tree venv/
venv/
??? bin
? ??? activate
? ??? activate.csh
? ??? activate.fish
? ??? python
? ??? python3
??? include
??? lib
? ??? python3.4
? ??? site-packages
??? lib64 -> lib
??? pyvenv.cfg
Run Code Online (Sandbox Code Playgroud)
我禁用了PYTHONPATH,以确保没有任何东西从外面泄漏:
» PYTHONPATH=""
Run Code Online (Sandbox Code Playgroud)
激活venv:
» source venv/bin/activate
Run Code Online (Sandbox Code Playgroud)
确认activate没有污染我的PYTHONPATH:
» echo $PYTHONPATH
Run Code Online (Sandbox Code Playgroud)
(空白,如预期的那样)
我正在使用正确的python:
» which python
/foo/bar/venv/bin/python
Run Code Online (Sandbox Code Playgroud)
但仍在访问系统模块:
» python
Python 3.4.3 (default, Oct …Run Code Online (Sandbox Code Playgroud) 注:类似的问题,因为这一个,但也有一些重要的变化.
在给定提交ID的情况下,我有以下函数来重写提交的日期:
rewrite-commit-date () {
local commit="$1"
local newdate="$2"
newdate="$(date -R --date "$newdate")"
echo ">>>> Rewriting commit $commit date: $newdate"
git filter-branch --env-filter \
"if test \$GIT_COMMIT = '$commit'
then
export GIT_AUTHOR_DATE
export GIT_COMMITTER_DATE
GIT_AUTHOR_DATE='$newdate'
GIT_COMMITTER_DATE='$newdate'
fi" &&
rm -fr "$(git rev-parse --git-dir)/refs/original/"
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试实现类似的功能rewrite-commit-message来更改提交消息.我想要的是:
rewrite-commit-message接受两个参数:和commit_id,和new_commit_messagecommit_id知道哪个提交更改git commit --amend,因为这与旧提交有关(不一定与最近的提交有关)git push -ffilter-branch它,但我不知道如何:
test在所使用的rewrite-commit-date功能是用来在env-filter,但我不会做env-filter在这里,因为我不希望改变有关提交环境中任何事物,但提交信息.我在这里经常有一些Vaadin代码阻塞,我不知道问题是什么:
Thread 7892: (state = IN_JAVA)
- java.util.HashMap.getEntry(java.lang.Object) @bci=61, line=349 (Compiled frame; information may be imprecise)
- java.util.HashMap.containsKey(java.lang.Object) @bci=2, line=335 (Compiled frame)
- java.util.HashSet.contains(java.lang.Object) @bci=5, line=184 (Compiled frame)
- com.vaadin.ui.Table.unregisterPropertiesAndComponents(java.util.HashSet, java.util.HashSet) @bci=85, line=1693 (Compiled frame)
- com.vaadin.ui.Table.refreshRenderedCells() @bci=992, line=1641 (Compiled frame)
- com.vaadin.ui.Table.valueChange(com.vaadin.data.Property$ValueChangeEvent) @bci=23, line=2897 (Compiled frame)
- com.vaadin.data.util.IndexedContainer.firePropertyValueChange(com.vaadin.data.util.IndexedContainer$IndexedContainerProperty) @bci=140, line=553 (Compiled frame)
- com.vaadin.data.util.IndexedContainer.access$1000(com.vaadin.data.util.IndexedContainer, com.vaadin.data.util.IndexedContainer$IndexedContainerProperty) @bci=2, line=64 (Compiled frame)
- com.vaadin.data.util.IndexedContainer$IndexedContainerProperty.setValue(java.lang.Object) @bci=202, line=915 (Compiled frame)
- com.aimprosoft.wavilon.backgroundthreads.ChangeCdrThread.insertNewPersonIntoTable(com.aimprosoft.wavilon.model.Person, com.vaadin.ui.HorizontalLayout, com.aimprosoft.wavilon.ui.menuitems.CallContent, com.vaadin.ui.Table) @bci=924, line=208 (Interpreted frame)
- com.aimprosoft.wavilon.backgroundthreads.ChangeCdrThread$RepaintTableThread.run() @bci=622, line=446 (Compiled …Run Code Online (Sandbox Code Playgroud) 我已修改配置rsyslogd以禁用RSYSLOG_TraditionalFileFormat.但是,apache日志仍然/var/log/apache/error.log只显示第二个精确度.
还有其他需要配置的东西吗?
我有这个myfile(我已经粘贴了,我希望有问题的相关数据在复制/粘贴中幸存下来).我尝试用以下内容读取该文件:
import codecs
codecs.open('myfile', 'r', 'utf-8').read()
Run Code Online (Sandbox Code Playgroud)
但这给了:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe5 in position 7128: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)
如果我检查文件:
» file myfile
myfile: C source, ISO-8859 text
Run Code Online (Sandbox Code Playgroud)
很多时候我正在处理我没有生成的文件(系统文件,从互联网上下载的随机文件,供应商,客户提供的随机文件......):这些文件没有提供编码的线索他们正在使用.在多文化环境(欧洲)中,很难知道这些文件是如何编码的.大多数时候,即使是提供文件的人也没有关于编码的线索,这可以通过选择的编辑器/工具在幕后发生.如何确定所使用的编码,逐个文件?
在 python_中用作一次性变量。javascript 中是否有惯用的名称?
python ×5
apache ×1
background ×1
celery ×1
defaultdict ×1
dictionary ×1
git ×1
git-amend ×1
git-commit ×1
hashmap ×1
java ×1
javascript ×1
lazy-loading ×1
python-2.7 ×1
python-venv ×1
rsyslog ×1
syslog ×1
timestamp ×1
unicode ×1
vaadin ×1
virtualenv ×1