我在 Vim 中打开了一个文件:
py24-sqlalchemy
py25-beautifulsoup
py25-beautifulsoup4
py25-bpython
py25-epydoc
py25-icalendar
py25-ipython
py25-libgmail
py25-mechanize
py25-numpy
py25-pil
py25-simplejson
py25-sphinx
py25-sqlalchemy
py25-sqlite
py25-tkinter
py25-vobject
py26-appscript
py26-beautifulsoup
py26-bpython
py26-epydoc
py26-game
py26-icalendar
py26-ipython
py26-mechanize
py26-numpy
py26-pdfminer
py26-simplejson
py26-sphinx
py26-sqlalchemy
py26-sqlite
py26-tkinter
py26-vobject
py27-appscript
py27-asciitable
py27-asciitable
py27-beautifulsoup
py27-beautifulsoup4
py27-blist
py27-bpython
py27-chm
py27-configobj
py27-dateutil
py27-epydoc
py27-game
py27-gdal
py27-gtk
py27-ipython
py27-lxml
py27-matplotlib
py27-mechanize
py27-mysql
py27-numpy
py27-pdfminer
py27-pil
py27-prettytable
py27-progressbar
py27-psutil
py27-py2app
py27-pylint
py27-pyobjc
py27-pyobjc-cocoa
py27-pypdf
py27-pyqt4
py27-simplejson
py27-sphinx
py27-sqlalchemy
py27-sqlite
py27-termcolor
py27-tkinter
py27-tz
py27-wxpython
py31-appscript
py31-asciitable
py31-beautifulsoup4
py31-blist
py31-bpython
py31-game
py31-lxml
py31-psutil
py32-asciitable
py32-beautifulsoup4
py32-blist
py32-bpython
py32-game
py32-ipython
py32-lxml
py32-psutil
# ... etc ... (many lines)
我想过滤行,以便对于 2.x 范围内的 Python 版本,我只保留最新的包。
示例:如果文件同时包含 py25-ipython 和 py26-ipython,我希望 py25-ipython 消失而 py26-ipython 保留。始终只保留最新版本,并且每个包始终只保留一个版本。
这是我想出的,但它的编码很糟糕。在我看来,几乎是 C64-BASIC 代码。
#!/usr/bin/env python2.7
import sys
import re
PATTERN_PYTHON_MACPORT = '^py(2[4567])-(\w[-\w]*)$'
REGEX_PYTHON_MACPORT = re.compile(PATTERN_PYTHON_MACPORT)
def main():
packages = {}
filtered_lines = []
for line in sys.stdin:
match = REGEX_PYTHON_MACPORT.match(line)
if match:
python_version = int(match.group(1))
package_name = match.group(2)
if package_name in packages:
packages[package_name].append(python_version)
else:
packages[package_name] = [python_version]
else:
filtered_lines.append(line)
for package_name in packages:
versions = packages[package_name]
if len(versions) == 1:
version_to_keep = versions[0]
else:
version_to_keep = sorted(versions, reverse=True)[0]
filtered_lines.append('py{}-{}\n'.format(version_to_keep,
package_name))
for line in sorted(filtered_lines):
sys.stdout.write(line)
if __name__ == '__main__':
main()
我怎样才能使它更 Pythonic?这在 Vimscript 中会更容易吗?我可能更喜欢 Vimscript 中的解决方案..
顺便说一句,我是 Python 和 Vimscript 的初学者。来这里学习。代码示例会很棒。
PS我从Vim内部运行过滤器:'<,'>! /Users/tinosino/Desktop/pyfilter.py