try:
for key in copy['KnownNetworks'].keys():
if copy['KnownNetworks'][key]['SSID_STR'] == networkDict['name']:
networkDict['uuid'] = copy['KnownNetworks'][key]['Unique Password ID']
print 'Found existing reference to wireless password uuid: %s' % networkDict['uuid']
found = True
except:
print 'Key KnownNetworks not found'
# Clean up top level key
keychain = '/Library/Keychains/System.keychain'
arguments = [security,"add-generic-password",'-a',networkDict['name'],'-l',networkDict['name'],'-D','Airport network password','-s',networkDict['uuid'],'-w',networkDict['pass'],'-T','group://Aiport','-T','/System/Library/CoreServices/SystemUIServer.app','-T','/Applications/System Preferences.app','-T','/usr/libexec/airportd',keychain]
addKeychainPassword(arguments)
users = '/var/db/dslocal/nodes/Default/users'
listing = os.listdir(users)
for plist in listing:
我有一个格式化问题,就是上面的例子,是否有更好的方法来格式化参数列表声明?它是一个子进程调用,所以我需要它作为一个列表,但我想我可以将它转换为字典,然后返回到列表。我的主要目标是,我希望它适合大多数unix脚本的80 col标准。您想要在清洁代码方面分享哪些技术?
答案 0 :(得分:8)
对于超过80列的行,您可以将它们分成多行。这里很容易,因为你有一个清单。
arguments = [security,
'add-generic-password',
'-a', networkDict['name'],
'-l', networkDict['name'],
'-D', 'Airport network password',
'-s', networkDict['uuid'],
'-w', networkDict['pass'],
'-T', 'group://Aiport',
'-T', '/System/Library/CoreServices/SystemUIServer.app',
'-T', '/Applications/System Preferences.app',
'-T', '/usr/libexec/airportd',
keychain]
在这里,您不仅可以获得符合Python样式指南的温暖,而且很多更容易看到代码以这种方式格式化时传递的参数。
Python布局的官方指南是PEP-8。它有其他分裂长线的例子。