我正在研究几个在不同版本的Python上运行的Python项目.我希望设置我的vim环境以使用ropevim,pyflakes和pylint,但是我遇到了一些问题,这些问题是由于使用单个vim(为特定版本的Python编译而与项目的Python版本不匹配) .
我希望将vim构建到我的每个virtualenv目录中,但我遇到了一个问题,我无法让它工作.当我尝试从源代码构建vim时,尽管在virtualenv中指定了Python配置文件夹,但总是使用系统范围的Python解释器.
目前,我安装了Python 2.6.2和Python 2.7.1,并且每个版本都创建了几个virtualenv.我正在使用Ubuntu 10.04,其中系统默认的Python是2.6.5.每次我编译vim并调用:python import sys; print(sys.version)它返回Python 2.6.5.
configure --prefix=/virtualenv/project --enable-pythoninterp=yes --with-python-config-dir=/virtualenv/project/lib/python2.6/config
config.log中的结果如下:
...
configure:5151: checking --enable-pythoninterp argument
configure:5160: result: yes
configure:5165: checking for python
configure:5195: result: /usr/bin/python
...
Run Code Online (Sandbox Code Playgroud)
它应该是/virtualenv/project/bin/python.有没有办法为vim指定Python解释器?
注意:我可以确认/ virtualenv/project/bin出现在PATH环境变量的前面.
Using str.format()是在Python 2.6和Python 3中格式化字符串的新标准.我在使用str.format()正则表达式时遇到了一个问题.
我写了一个正则表达式来返回指定域下的单个级别的所有域或者指定域下2级的任何域,如果下面的第二级是www ...
假设指定的域名是delivery.com,我的正则表达式应该返回a.delivery.com,b.delivery.com,www.c.delivery.com ......但它不应该返回xadelivery.com.
import re
str1 = "www.pizza.delivery.com"
str2 = "w.pizza.delivery.com"
str3 = "pizza.delivery.com"
if (re.match('^(w{3}\.)?([0-9A-Za-z-]+\.){1}delivery.com$', str1): print 'String 1 matches!'
if (re.match('^(w{3}\.)?([0-9A-Za-z-]+\.){1}delivery.com$', str2): print 'String 2 matches!'
if (re.match('^(w{3}\.)?([0-9A-Za-z-]+\.){1}delivery.com$', str3): print 'String 3 matches!'
Run Code Online (Sandbox Code Playgroud)
运行它应该给出结果:
String 1 matches!
String 3 matches!
Run Code Online (Sandbox Code Playgroud)
现在,问题是当我尝试使用str.format动态替换delivery.com时...
if (re.match('^(w{3}\.)?([0-9A-Za-z-]+\.){1}{domainName}$'.format(domainName = 'delivery.com'), str1): print 'String 1 matches!'
Run Code Online (Sandbox Code Playgroud)
这似乎失败了,因为str.format()期望{3}和{1}成为函数的参数.(我假设)
我可以使用+运算符连接字符串
'^(w{3}\.)?([0-9A-Za-z-]+\.){1}' + domainName + '$'
Run Code Online (Sandbox Code Playgroud)
问题归结为,str.format()当字符串(通常是正则表达式)中有" {n} " 时,是否可以使用?
在Python中,假设定义了以下函数:
def function(a, b, c):
... do stuff with a, b, c ...
Run Code Online (Sandbox Code Playgroud)
我可以使用Python的序列解包来使用该函数:
arguments = (1, 2, 3)
function(*arguments)
Run Code Online (Sandbox Code Playgroud)
Common Lisp中是否存在类似的功能?如果我有一个功能:
(defun function (a b c)
... do stuff with a, b, c ...
Run Code Online (Sandbox Code Playgroud)
如果我有3个元素的列表,我可以轻松地使用这3个元素作为函数的参数?
我目前实现它的方式如下:
(destructuring-bind (a b c) (1 2 3)
(function a b c))
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
我已经使用emacs 23(python.el)一个多月了,我对默认的自动缩进设置不满意.
目前,我的Python文件是自动缩进的,如下所示:
x = a_function_with_dict_parameter({
'test' : 'Here is a value',
'second' : 'Another value',
})
a_function_with_multiline_parameters(on='First', line='Line',
now_on='Second', next_line='Line',
next='Third', finally='Line')
Run Code Online (Sandbox Code Playgroud)
我更喜欢如果我可以设置自动缩进设置,以便可以轻松格式化相同的代码:
x = a_function_with_dict_parameter({
'test' : 'Here is a value',
'second' : 'Another value',
})
a_function_with_multiline_parameters(on='First', line='Line',
now_on='Second', next_line='Line', next='Third', finally='Line')
Run Code Online (Sandbox Code Playgroud)
似乎我喜欢自动缩进的逻辑是:
如果前一行的最后一个字符(非注释/空白)是:,则将缩进级别增加1.否则,使用相同的缩进级别.
但是使用该逻辑,TAB需要实际增加当前行的缩进级别.(目前,TAB只将行移动到自动缩进级别)
有谁知道如何修改emacs auto-indentation来实现我想要的风格?
Django的ForeignRelatedObjectsDescriptor.create_manager(...)函数动态创建RelatedManager类,然后初始化动态创建的类的实例.
如果我想覆盖该RelatedManager.add(...)方法,我该怎么做?
这些RelatedManager类是在文件中创建的:django/db/models/fields/related.py.
我想如何使用自定义的示例RelatedManager是......
class Record(Model):
string = CharField()
class Managed(Model):
record = ForeignKey('Record')
boolean = BooleanField()
def view_function(...):
record = Record(string='Example')
record.save()
record.managed_set.add(Managed(boolean=True)) # How to override add()?
Run Code Online (Sandbox Code Playgroud)
任何建议,将不胜感激.
我正在创建一个Django库,它使用乐观并发控制来防止并发写入导致不一致的数据.我希望能够为这个功能编写单元测试,但我不知道如何实现这一点.
我知道Django的单元测试是单线程的,所以我能想象测试工作的唯一方法是同时打开两个独立的数据库连接(到同一个数据库)并切换Django ORM在执行查询时使用的连接,尽管我我不确定Django中是否可以进行连接切换.
使用Django测试并发数据库操作有哪些技巧?
使用Python 2.6,我在Windows XP中编写了一个脚本.
该脚本执行以下操作:
输入:域名(即:amazon.com)
该脚本通过dnspython模块查询DNS并返回任何A记录IP地址.输出采用特殊应用程序所需的特殊格式,该应用程序利用此数据.
这在Windows中运行良好,但是当我把它放在我的Linux服务器上时,我得到了一些不寻常和不一致的结果.
第一次运行时,它按预期完成.如果我立即再次运行它,脚本将挂起并且什么也不做,没有输出,脚本也不会结束.如果我使用CTRL-C退出进程,它会打印出来!(几乎就像它已被缓冲,但没有写入终端)
我已经尝试了各种技术来解决这个问题,比如在打印后强制sys.stdout.flush()(尽管如此,print应该会自动刷新)并且没有运气.
如果我等待一段时间(几分钟),然后再次运行脚本,它将再次工作(一次),然后后续尝试将继续失败.我不确定发生了什么......还有其他人经历过这样的事吗?
Windows和Linux(Ubuntu)上的Python 2.6.
这是我的脚本:
from dns.resolver import Resolver
from dns.exception import DNSException
from cStringIO import StringIO
import sys
def maltego_transform(entities, messages = ''):
print '''<MaltegoMessage>
<MaltegoTransformResponseMessage>
<Entities>
{0}
</Entities>
<UIMessages>
{1}
</UIMessages>
</MaltegoTransformResponseMessage>
</MaltegoMessage>'''.format(entities, messages)
def domain_to_ip(domain):
resolver = Resolver()
results = []
for type in ['A', 'AAAA']:
try:
query = resolver.query(domain, type)
except DNSException:
query = []
results += query
entities = StringIO()
for answer in results:
entities.write('''<Entity Type="IPAddress"><Value>{0}</Value></Entity>'''.format(answer))
maltego_transform(entities.getvalue()) …Run Code Online (Sandbox Code Playgroud) python ×7
django ×2
coding-style ×1
common-lisp ×1
concurrency ×1
emacs ×1
format ×1
indentation ×1
lisp ×1
list ×1
printing ×1
regex ×1
unit-testing ×1
vim ×1