我需要在Python程序中模拟do-while循环.不幸的是,以下简单的代码不起作用:
list_of_ints = [ 1, 2, 3 ]
iterator = list_of_ints.__iter__()
element = None
while True:
if element:
print element
try:
element = iterator.next()
except StopIteration:
break
print "done"
Run Code Online (Sandbox Code Playgroud)
而不是"1,2,3,完成",它打印以下输出:
[stdout:]1
[stdout:]2
[stdout:]3
None['Traceback (most recent call last):
', ' File "test_python.py", line 8, in <module>
s = i.next()
', 'StopIteration
']
Run Code Online (Sandbox Code Playgroud)
我能做些什么来捕获'stop iteration'异常并正确地打破while循环?
以下将伪代码示为可能需要这样的事物的示例.
状态机:
s = ""
while True :
if state is STATE_CODE :
if "//" in s :
tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
state = …Run Code Online (Sandbox Code Playgroud) 令我遗憾的是,我无法弄清楚如何处理python'with'语句的异常.如果我有一个代码:
with open("a.txt") as f:
print f.readlines()
Run Code Online (Sandbox Code Playgroud)
我真的想处理'文件未找到异常'以便进行处理.但我不能写
with open("a.txt") as f:
print f.readlines()
except:
print 'oops'
Run Code Online (Sandbox Code Playgroud)
并且不能写
with open("a.txt") as f:
print f.readlines()
else:
print 'oops'
Run Code Online (Sandbox Code Playgroud)
在try/except语句中包含'with'不起作用:不引发异常.为了以Pythonic方式处理'with'语句内部的失败,我该怎么办?
大多数linux应用程序都使用以
make
make install clean
Run Code Online (Sandbox Code Playgroud)
据我所知,make将构建目标的名称作为参数.所以install是一个目标是复制一些文件,之后clean是一个目标即删除临时文件.
但是make如果没有指定参数,会构建什么目标(例如我的例子中的第一个命令)?
我有一些简单的python代码搜索文件中的字符串,例如path=c:\path,c:\path可能会有所不同.目前的代码是:
def find_path(i_file):
lines = open(i_file).readlines()
for line in lines:
if line.startswith("Path="):
return # what to do here in order to get line content after "Path=" ?
Run Code Online (Sandbox Code Playgroud)
之后获取字符串文本的简单方法是什么Path=?有没有简单的方法,没有封闭,反射或其他深奥的东西?
给出以下代码:
def A() :
b = 1
def B() :
# I can access 'b' from here.
print( b )
# But can i modify 'b' here? 'global' and assignment will not work.
B()
A()
Run Code Online (Sandbox Code Playgroud)
对于B()函数变量中的代码,b在外部作用域中,但不在全局作用域中.是否可以b从B()函数内修改变量?当然我可以从这里读取它print(),但是如何修改呢?
我在Lua中有一个字符串,想要在其中迭代单个字符.但是没有我尝试过的代码和官方手册只显示如何查找和替换子串:(
str = "abcd"
for char in str do -- error
print( char )
end
for i = 1, str:len() do
print( str[ i ] ) -- nil
end
Run Code Online (Sandbox Code Playgroud) 我有一个大量使用regexp的python模板引擎.它使用如下的连接:
re.compile( regexp1 + "|" + regexp2 + "*|" + regexp3 + "+" )
Run Code Online (Sandbox Code Playgroud)
我可以修改单个子串(regexp1,regexp2等).
是否有任何小而轻的表达式,什么都不匹配,我可以在模板中使用,我不想要任何匹配?不幸的是,有时'+'或'*'被附加到正则表达式原子,因此我不能使用空字符串 - 这将引发"无需重复"错误.
我正在使用vim -d file1 file2它以查看它们之间的差异.这工作正常,但我想忽略空格更改 - 它们与源代码文件无关.
Vim帮助声明以下命令将起到魔力:
set diffopt+=iwhite
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,此命令仅添加-b到diff工具命令行,并且只忽略尾随空格.diff的正确命令行键应该是-w,忽略所有空格更改.但我找不到如何直接从Vim修改diff命令行.当然我可以编译自定义差异,或用diff.sh替换差异,但这看起来有点难看:(.
有没有更好的方法来修改Vim如何与diff工具交互以显示文件差异?
Qt文档指出信号和槽可以是direct,queued和auto.
它还声明,如果拥有插槽的对象'生命'在与拥有信号的对象不同的线程中,则发出此类信号就像发布消息一样 - 信号发出将立即返回,并且将在目标线程的事件循环中调用slot方法.
不幸的是,文档没有说明"生命"代表的是没有例子可用.我试过以下代码:
main.h:
class CThread1 : public QThread
{
Q_OBJECT
public:
void run( void )
{
msleep( 200 );
std::cout << "thread 1 started" << std::endl;
MySignal();
exec();
}
signals:
void MySignal( void );
};
class CThread2 : public QThread
{
Q_OBJECT
public:
void run( void )
{
std::cout << "thread 2 started" << std::endl;
exec();
}
public slots:
void MySlot( void )
{
std::cout << "slot called" << std::endl;
} …Run Code Online (Sandbox Code Playgroud) 我正在将一个存储库从sourcesafe转移到subversion,我需要确保关键点是平等的.是否有任何现有的Windows命令/工具允许我比较它们相同的两个文件夹树(具有相同的文件夹结构和相同内容的文件)?理想将是命令行像:
some_cool_compare c:\current_vss c:\current_svn -exclude .svn;*.vspscc;*.scc
Run Code Online (Sandbox Code Playgroud)
是否存在某些工具/命令或者我需要编写自己的脚本?
python ×5
c++ ×1
comparison ×1
diff ×1
do-while ×1
lua ×1
makefile ×1
python-2.7 ×1
qt ×1
qt-signals ×1
regex ×1
string ×1
vim ×1
vimdiff ×1
while-loop ×1