Skip to content

Instantly share code, notes, and snippets.

@farcaller
Created September 30, 2009 09:35
Show Gist options
  • Select an option

  • Save farcaller/197944 to your computer and use it in GitHub Desktop.

Select an option

Save farcaller/197944 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import zipfile
from lxml import etree
def fb_info(fb):
xml = etree.XML(fb)
NODE = lambda n:xml.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:'+n, namespaces={'fb':'http://www.gribuser.ru/xml/fictionbook/2.0'})
T = lambda n:n[0].text if len(n)>0 else ''
return {
'title': T(NODE('book-title')),
'genres': [g.text for g in NODE('genre') if g.text != None],
'author': (T(NODE('author/fb:first-name')), T(NODE('author/fb:last-name'))),
}
def read_zip(fn):
zip = zipfile.ZipFile(fn)
l = []
for n in [i.filename for i in zip.infolist()]:
fb = zip.read(n)
print "%s..." % (n,)
l.append(fb_info(fb))
return l
def calculate(func, fn, z):
print "%s..." % (fn,)
d = z.read(fn)
return func(d)
def read_zip_mp(fn):
import multiprocessing
zip = zipfile.ZipFile(fn)
l = []
wk = [(fb_info, i.filename, zip) for i in zip.infolist()]
pool = multiprocessing.Pool(4)
results = [pool.apply(calculate, t) for t in wk]
print results
return l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment