我曾尝试使用Python的ConfigParser模块来保存设置.对于我的应用程序,重要的是我在我的部分中保留每个名称的大小写.文档提到将str()传递给ConfigParser.optionxform()会实现这一点,但它对我不起作用.名称都是小写的.我错过了什么吗?
<~/.myrc contents>
[rules]
Monkey = foo
Ferret = baz
Run Code Online (Sandbox Code Playgroud)
我获得的Python伪代码:
import ConfigParser,os
def get_config():
config = ConfigParser.ConfigParser()
config.optionxform(str())
try:
config.read(os.path.expanduser('~/.myrc'))
return config
except Exception, e:
log.error(e)
c = get_config()
print c.options('rules')
[('monkey', 'foo'), ('ferret', 'baz')]
Run Code Online (Sandbox Code Playgroud) 所以,你们都准备好做一个大的SVN提交并且因为你的某些文件中的行结尾不一致而爆炸.有趣的是,您正在查看跨越数十个不同深度文件夹的1,000个文件.
你是做什么?
有时候你知道你的循环永远不会超过x次,其中x可以用byte或short来表示,基本上是一个小于int的数据类型.
为什么我们使用占用32位(大多数语言)的int,就像一个字节就足够只有8位.
我知道我们有32位和64位进程,所以我们可以在一次旅行中轻松获取值,但它仍然消耗更多内存.或者我在这里缺少什么?
更新:只是为了澄清.我知道速度明智没有区别.我问的是对内存消耗的影响.
在Windows上使用python 2.7.4(注意:WinXP - 下面的评论者建议在Win7上正常工作),我有一个脚本创建几个线程,每个线程通过Popen运行子进程,stdout/stderr重定向到文件和调用等待().每个Popen都有自己的stdout/stderr文件.每个进程返回后,我有时必须删除文件(实际上将它们移动到其他地方).
我发现在所有wait()调用返回之前我都无法删除stdout/stderr日志.在此之前,我得到"WindowsError:[错误32]该进程无法访问该文件,因为它正被另一个进程使用".看起来,只要至少有一个子进程打开,Popen就会以某种方式保留stderr文件,即使这些文件没有共享.
测试代码重现如下.
C:\ test1.py
import subprocess
import threading
import os
def retryDelete(p, idx):
while True:
try:
os.unlink(p)
except Exception, e:
if "The process cannot access the file because it is being used by another process" not in e:
raise e
else:
print "Deleted logs", idx
return
class Test(threading.Thread):
def __init__(self, idx):
threading.Thread.__init__(self)
self.idx = idx
def run(self):
print "Creating %d" % self.idx
stdof = open("stdout%d.log" % self.idx, "w")
stdef = open("stderr%d.log" % self.idx, "w")
p = …Run Code Online (Sandbox Code Playgroud) python windows-xp subprocess python-multithreading python-2.7
我正在解析 android 应用程序中提到的 xml 文件(androidmanifest.xml)中的 uses-permission 标记
我曾尝试实现一个 for 循环以使其迭代,但我失败了,所以我在这里
Python:
from xml.dom.minidom import parseString
file = open('/root/Desktop/AndroidManifest.xml','r')
data = file.read()
file.close()
dom = parseString(data)
xmlTag = dom.getElementsByTagName('uses-permission')[0].toxml()
print xmlTag
Run Code Online (Sandbox Code Playgroud)
输出:
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
Run Code Online (Sandbox Code Playgroud)
for循环错误:
for uses-permission in xmlTag:
#print child.tag, child.attrib
print xmlTag.tag
xmlTag = dom.getElementsByTagName('uses-permission')[1].toxml()
xmlTag= dom._get_childNodes
#print xmlTag
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个生成器函数,该函数从数据库中获取行并一次返回一行。但是,我不确定下面标记为 ** 的清理代码是否按照我的想法执行。如果没有,将清理代码放入生成器本身并在最后一个yield语句之后执行的最佳方法是什么?我查看了捕获 StopIteration 但这似乎是由调用者完成的,而不是在生成器内完成的。
def MYSQLSelectGenerator(stmt):
...
try:
myDB = MySQLdb.connect(host=..., port=..., user=..., passwd=..., db=...)
dbc=myDB.cursor()
dbc.execute(stmt)
d = "asdf"
while d is not None:
d = dbc.fetchone() #can also use fetchmany() to be more efficient
yield d
dbc.close() #** DOES THIS WORK AS I INTEND, MEANING AS SOON AS d = "None"
except MySQLdb.Error, msg:
print("MYSQL ERROR!")
print msg
Run Code Online (Sandbox Code Playgroud) python ×4
android ×1
coding-style ×1
configparser ×1
dom ×1
eol ×1
for-loop ×1
generator ×1
mysql-python ×1
python-2.7 ×1
python-2.x ×1
subprocess ×1
svn ×1
windows-xp ×1
xml ×1
xml-parsing ×1