Skip to content

Commit

Permalink
print() is a function in Python 3 (again) (#1450)
Browse files Browse the repository at this point in the history
switching print to be python3 print()
  • Loading branch information
cclauss authored and mekarpeles committed Oct 31, 2018
1 parent 17ce387 commit 2b2895f
Show file tree
Hide file tree
Showing 212 changed files with 1,621 additions and 1,409 deletions.
3 changes: 2 additions & 1 deletion openlibrary/accounts/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
"""
from __future__ import print_function
import time
import datetime
import hmac
Expand Down Expand Up @@ -39,7 +40,7 @@ def sendmail(to, msg, cc=None):
'\n' +
web.safestr(msg))

print >> web.debug, "sending email", message
print("sending email", message, file=web.debug)
else:
web.sendmail(config.from_address, to, subject=msg.subject.strip(),
message=web.safestr(msg), cc=cc)
Expand Down
3 changes: 2 additions & 1 deletion openlibrary/actions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Custom OL Actions.
"""
from __future__ import print_function
import infogami
import sys

@infogami.action
def runmain(modulename, *args):
print "run_main", modulename, sys.argv
print("run_main", modulename, sys.argv)
mod = __import__(modulename, globals(), locals(), modulename.split("."))
mod.main(*args)
7 changes: 4 additions & 3 deletions openlibrary/catalog/add_book/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
response = load(record)
"""
from __future__ import print_function

import re
import json
Expand Down Expand Up @@ -266,7 +267,7 @@ def find_match(e1, edition_pool):
found = False
break
if is_redirect(thing):
print 'following redirect %s => %s' % (edition_key, thing['location'])
print('following redirect %s => %s' % (edition_key, thing['location']))
edition_key = thing['location']
if not found:
continue
Expand Down Expand Up @@ -452,15 +453,15 @@ def add_cover(cover_url, ekey):
try:
res = urllib.urlopen(upload_url, urllib.urlencode(params))
except IOError:
print 'retry, attempt', attempt
print('retry, attempt', attempt)
sleep(2)
continue
body = res.read()
if body not in ['', 'None']:
reply = json.loads(body)
if res.getcode() == 200 and 'id' in reply:
break
print 'retry, attempt', attempt
print('retry, attempt', attempt)
sleep(2)
if not reply or reply.get('message') == 'Invalid URL':
return
Expand Down
1 change: 1 addition & 0 deletions openlibrary/catalog/add_book/test_add_book.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from load_book import build_query, InvalidLanguage
from . import load, RequiredField, build_pool, add_db_name
from .. import add_book
Expand Down
7 changes: 4 additions & 3 deletions openlibrary/catalog/amazon/add_covers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from urllib2 import urlopen
import simplejson

Expand All @@ -9,7 +10,7 @@
url = base + offset
ret = simplejson.load(urlopen(url))
offset, data = ret['offset'], ret['data']
print offset, len(data)
print(offset, len(data))
for i in data:
action = i.pop('action')
key = i['data'].pop('key', None)
Expand All @@ -24,11 +25,11 @@
if e:
isbn = e.get('isbn_10', None)
if isbn:
print >> out, (key, isbn)
print((key, isbn), file=out)
elif action == 'save_many':
for e in i['data']['query']:
if e['type'] == '/type/edition' and e['key'].startswith('/b/'):
isbn = e.get('isbn_10', None)
if isbn:
print >> out, (e['key'], isbn)
print((e['key'], isbn), file=out)
out.close()
3 changes: 2 additions & 1 deletion openlibrary/catalog/amazon/amazon_to_arc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import socket

#url = "http://www.amazon.com/dp/1847195881"
Expand All @@ -20,7 +21,7 @@ def get(sock, host, url):

while True:
chunk_size = int(fp.readline(),16)
print chunk_size
print(chunk_size)
if chunk_size == 0:
break
print(len(fp.read(chunk_size)))
Expand Down
5 changes: 3 additions & 2 deletions openlibrary/catalog/amazon/arc_index.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import os

arc_dir = '/2/edward/amazon/arc'
Expand All @@ -10,13 +11,13 @@ def read_arc(filename):
line = f.readline()
if line == '':
break
print >> idx, pos
print(pos, file=idx)
size = int(line[:-1].split(' ')[4])
f.read(size)
line = f.readline()
f.close()
idx.close()

for filename in (i for i in os.listdir(arc_dir) if i.endswith('.arc')):
print filename
print(filename)
read_arc(filename)
41 changes: 21 additions & 20 deletions openlibrary/catalog/amazon/crawl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from lxml.html import parse, tostring, fromstring
import re, sys, os, socket
from urllib import unquote
Expand Down Expand Up @@ -141,7 +142,7 @@ def read_books(params, root):
if all(a is not None for a in book_links):
break
sleep(2)
print 'retry:', params
print('retry:', params)
root = get_url(params)
if re_child_book_param.search(params) and all(re_personalized.search(span.text) for span in root.find_class('srTitle')):
raise PersonalizedBooks
Expand Down Expand Up @@ -187,16 +188,16 @@ def read_page(params):
root = get_url(params)
total = get_total(root)
if total == 0:
print 'no results found'
print('no results found')
return total, set(), []
grand_total = total
pages = (total / page_size) + 1
print 'total:', total, 'pages:', pages
print('total:', total, 'pages:', pages)

cats = get_cats(root)
print 'cats 1'
print('cats 1')
for a, b, c in cats:
print "%8d %-30s %8d" % (a, b, c)
print("%8d %-30s %8d" % (a, b, c))
#return grand_total, [], cats

books = set()
Expand All @@ -205,37 +206,37 @@ def read_page(params):
for page in range(2, min((pages, 100))+1):
params_with_page = params + "&page=%d" % page
books.update(read_books(params_with_page, get_url(params_with_page)))
print page, len(books)
print(page, len(books))

print len(books)
print(len(books))

cats = get_cats(root)
print 'cats 2'
print('cats 2')
for a, b, c in cats:
print "%8d %30s %8d" % (a, b, c)
print 'cat total:', sum(i[2] for i in cats)
print("%8d %30s %8d" % (a, b, c))
print('cat total:', sum(i[2] for i in cats))
if total > max_results:
for n, title, count in cats:
print(repr(n, title, count))
params_with_cat = params + ",n:" + str(n)
root = get_url(params_with_cat)
cat_total = get_total(root)
pages = (cat_total / page_size) + 1
print 'cat_total:', total, 'pages:', total / page_size
print('cat_total:', total, 'pages:', total / page_size)
if cat_total > max_results:
print 'cat_total (%d) > max results (%d)' % (total, max_results)
print('cat_total (%d) > max results (%d)' % (total, max_results))
# assert cat_total <= max_results
try:
books.update(read_books(params_with_cat, root))
except PersonalizedBooks:
print 'WARNING: Personalized Books'
print('WARNING: Personalized Books')
continue
for page in range(2, min((pages, 100)) + 1):
params_with_page = params_with_cat + "&page=%d" % page
try:
books.update(read_books(params_with_page, get_url(params_with_page)))
except PersonalizedBooks:
print 'WARNING: Personalized Books'
print('WARNING: Personalized Books')
break
print(repr(n, title, page, cat_total / page_size, len(books), "%.1f%%" % percent(len(books), grand_total)))

Expand All @@ -257,12 +258,12 @@ def write_books(books):
print(repr(page[-60:]))
error_count += 1
if error_count == 50:
print 'too many bad endings'
print 'http://amazon.com/dp/' + asin
print('too many bad endings')
print('http://amazon.com/dp/' + asin)
sys.exit(0)
except:
pass
print 'retry'
print('retry')
sleep(5)

if __name__ == '__main__':
Expand All @@ -272,14 +273,14 @@ def write_books(books):
cur = date(2009, 11, 11) # start date
#cur = date(2009, 12, 25)
while True:
print cur
print(cur)
total, books, cats = read_page(rh + cur.strftime("%Y%m%d"))
open(out_dir + '/total.' + str(cur), 'w').write(str(total) + "\n")

out = open(out_dir + "/cats." + str(cur), 'w')
for i in cats:
print >> out, i
print(i, file=out)
out.close()
print len(books)
print(len(books))
write_books(books)
cur += one_day
5 changes: 3 additions & 2 deletions openlibrary/catalog/amazon/crawl_top_books.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from openlibrary.catalog.amazon.crawl import read_page, write_books, get_url, get_cats

def get_serp():
Expand All @@ -6,7 +7,7 @@ def get_serp():
#crawled = set(i[:-1] for i in open('/2/edward/amazon/crawled'))

total, books, cats = read_page(params)
print 'total:', total, 'number of books:', len(books), 'number of cats:', len(cats)
print('total:', total, 'number of books:', len(books), 'number of cats:', len(cats))

#get_serp()

Expand All @@ -15,7 +16,7 @@ def get_serp():
cats = get_cats(root)

for a, b, c in cats:
print "%8d %-30s %8d" % (a, b, c)
print("%8d %-30s %8d" % (a, b, c))

#books = [i[:-1] for i in open('/2/edward/amazon/best_sellers2')]
#write_books(books)
3 changes: 2 additions & 1 deletion openlibrary/catalog/amazon/get_other_editions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from catalog.read_rc import read_rc
import web, urllib2, sys, os.path
from time import time
Expand Down Expand Up @@ -29,4 +30,4 @@
if i % chunk == 0:
t1 = time() - t0
rec_per_sec = float(i) / float(t1)
print "%s %s %.2f rec/sec" % (url, isbn, rec_per_sec)
print("%s %s %.2f rec/sec" % (url, isbn, rec_per_sec))
31 changes: 16 additions & 15 deletions openlibrary/catalog/amazon/import.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import sys,re, os
from parse import read_edition
from lxml.html import fromstring
Expand Down Expand Up @@ -56,7 +57,7 @@ def read_amazon_file(f):
try:
edition = read_edition(fromstring(page))
except:
print 'bad record:', asin
print('bad record:', asin)
raise
if not edition:
continue
Expand All @@ -70,7 +71,7 @@ def follow_redirects(key):
thing = withKey(key)
assert thing
if thing['type']['key'] == '/type/redirect':
print 'following redirect %s => %s' % (key, thing['location'])
print('following redirect %s => %s' % (key, thing['location']))
key = thing['location']
return (keys, thing)

Expand All @@ -84,7 +85,7 @@ def ia_match(a, ia):
try:
e1 = build_marc(rec)
except TypeError:
print rec
print(rec)
raise
return amazon_merge.attempt_merge(a, e1, threshold, debug=False)

Expand All @@ -111,7 +112,7 @@ def source_records_match(a, thing):
break
elif src.startswith(ia):
if src == 'ia:ic':
print thing['source_records']
print(thing['source_records'])
if ia_match(a, src[len(ia):]):
match = True
break
Expand All @@ -124,7 +125,7 @@ def source_records_match(a, thing):
def try_merge(edition, ekey, thing):
thing_type = thing['type']['key']
if 'isbn_10' not in edition:
print edition
print(edition)
asin = edition.get('isbn_10', None) or edition['asin']
if 'authors' in edition:
authors = [i['name'] for i in edition['authors']]
Expand Down Expand Up @@ -155,11 +156,11 @@ def import_file(filename):
index_fields = build_index_fields(asin, edition)
found = pool.build(index_fields)
if 'title' not in found:
print found
print asin
print edition
print index_fields
print
print(found)
print(asin)
print(edition)
print(index_fields)
print()

if not found['title'] and not found['isbn']:
#print 'no pool load book:', asin
Expand All @@ -183,10 +184,10 @@ def import_file(filename):
try:
m = try_merge(edition, ekey, thing)
except:
print asin
print edition
print ekey
print found
print(asin)
print(edition)
print(ekey)
print(found)
raise

# import_file(sys.argv[1])
Expand All @@ -195,7 +196,7 @@ def import_file(filename):
for f in os.listdir(d):
if not f.startswith('amazon.'):
continue
print f
print(f)
if '2009-02' in f:
continue
import_file(d + "/" + f)
Loading

0 comments on commit 2b2895f

Please sign in to comment.