Skip to content

Commit b187ca5

Browse files
authored
Merge pull request #12453 from velconia/port_python3_syntax
Port python3 syntax
2 parents 059b278 + d8ddd3b commit b187ca5

File tree

168 files changed

+1036
-940
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+1036
-940
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ option(PY_VERSION "Compile PaddlePaddle with python3 support" ${PY_VER
7272
if(NOT PY_VERSION)
7373
set(PY_VERSION 2.7)
7474
endif()
75+
set(PYBIND11_PYTHON_VERSION ${PY_VERSION})
7576

7677
# CMAKE_BUILD_TYPE
7778
if(NOT CMAKE_BUILD_TYPE)

paddle/fluid/pybind/pybind.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,10 @@ All parameter, weight, gradient are variables in Paddle.
394394
InferenceOptimize(*(origin.Proto()), &pruned_desc);
395395
return new ProgramDesc(pruned_desc);
396396
});
397-
m.def("empty_var_name", []() { return framework::kEmptyVarName; });
398-
m.def("grad_var_suffix", []() { return framework::kGradVarSuffix; });
397+
m.def("empty_var_name",
398+
[]() { return std::string(framework::kEmptyVarName); });
399+
m.def("grad_var_suffix",
400+
[]() { return std::string(framework::kGradVarSuffix); });
399401
m.def_submodule(
400402
"var_names",
401403
"The module will return special predefined variable name in Paddle")

python/paddle/dataset/cifar.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
2929
"""
3030

31-
import cPickle
3231
import itertools
3332
import numpy
3433
import paddle.dataset.common
3534
import tarfile
35+
from six.moves import zip
36+
from six.moves import cPickle as pickle
3637

3738
__all__ = ['train100', 'test100', 'train10', 'test10', 'convert']
3839

@@ -48,7 +49,7 @@ def read_batch(batch):
4849
data = batch['data']
4950
labels = batch.get('labels', batch.get('fine_labels', None))
5051
assert labels is not None
51-
for sample, label in itertools.izip(data, labels):
52+
for sample, label in zip(data, labels):
5253
yield (sample / 255.0).astype(numpy.float32), int(label)
5354

5455
def reader():
@@ -58,7 +59,7 @@ def reader():
5859

5960
while True:
6061
for name in names:
61-
batch = cPickle.load(f.extractfile(name))
62+
batch = pickle.load(f.extractfile(name))
6263
for item in read_batch(batch):
6364
yield item
6465
if not cycle:

python/paddle/dataset/common.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import sys
2121
import importlib
2222
import paddle.dataset
23-
import cPickle
23+
import six.moves.cPickle as pickle
2424
import glob
25-
import cPickle as pickle
2625

2726
__all__ = [
2827
'DATA_HOME',
@@ -75,13 +74,13 @@ def download(url, module_name, md5sum, save_name=None):
7574
retry_limit = 3
7675
while not (os.path.exists(filename) and md5file(filename) == md5sum):
7776
if os.path.exists(filename):
78-
print "file md5", md5file(filename), md5sum
77+
print("file md5", md5file(filename), md5sum)
7978
if retry < retry_limit:
8079
retry += 1
8180
else:
8281
raise RuntimeError("Cannot download {0} within retry limit {1}".
8382
format(url, retry_limit))
84-
print "Cache file %s not found, downloading %s" % (filename, url)
83+
print("Cache file %s not found, downloading %s" % (filename, url))
8584
r = requests.get(url, stream=True)
8685
total_length = r.headers.get('content-length')
8786

@@ -104,8 +103,9 @@ def download(url, module_name, md5sum, save_name=None):
104103

105104

106105
def fetch_all():
107-
for module_name in filter(lambda x: not x.startswith("__"),
108-
dir(paddle.dataset)):
106+
for module_name in [
107+
x for x in dir(paddle.dataset) if not x.startswith("__")
108+
]:
109109
if "fetch" in dir(
110110
importlib.import_module("paddle.dataset.%s" % module_name)):
111111
getattr(
@@ -114,8 +114,9 @@ def fetch_all():
114114

115115

116116
def fetch_all_recordio(path):
117-
for module_name in filter(lambda x: not x.startswith("__"),
118-
dir(paddle.dataset)):
117+
for module_name in [
118+
x for x in dir(paddle.dataset) if not x.startswith("__")
119+
]:
119120
if "convert" in dir(
120121
importlib.import_module("paddle.dataset.%s" % module_name)) and \
121122
not module_name == "common":
@@ -126,7 +127,7 @@ def fetch_all_recordio(path):
126127
"convert")(ds_path)
127128

128129

129-
def split(reader, line_count, suffix="%05d.pickle", dumper=cPickle.dump):
130+
def split(reader, line_count, suffix="%05d.pickle", dumper=pickle.dump):
130131
"""
131132
you can call the function as:
132133
@@ -167,7 +168,7 @@ def split(reader, line_count, suffix="%05d.pickle", dumper=cPickle.dump):
167168
def cluster_files_reader(files_pattern,
168169
trainer_count,
169170
trainer_id,
170-
loader=cPickle.load):
171+
loader=pickle.load):
171172
"""
172173
Create a reader that yield element from the given files, select
173174
a file set according trainer count and trainer_id
@@ -188,7 +189,7 @@ def reader():
188189
my_file_list = []
189190
for idx, fn in enumerate(file_list):
190191
if idx % trainer_count == trainer_id:
191-
print "append file: %s" % fn
192+
print("append file: %s" % fn)
192193
my_file_list.append(fn)
193194
for fn in my_file_list:
194195
with open(fn, "r") as f:
@@ -221,7 +222,7 @@ def write_data(indx_f, lines):
221222
for l in lines:
222223
# FIXME(Yancey1989):
223224
# dumps with protocol: pickle.HIGHEST_PROTOCOL
224-
writer.write(cPickle.dumps(l))
225+
writer.write(pickle.dumps(l))
225226
writer.close()
226227

227228
lines = []

python/paddle/dataset/conll05.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import gzip
2525
import itertools
2626
import paddle.dataset.common
27+
from six.moves import zip
2728

2829
__all__ = ['test, get_dict', 'get_embedding', 'convert']
2930

@@ -87,12 +88,12 @@ def reader():
8788
sentences = []
8889
labels = []
8990
one_seg = []
90-
for word, label in itertools.izip(words_file, props_file):
91+
for word, label in zip(words_file, props_file):
9192
word = word.strip()
9293
label = label.strip().split()
9394

9495
if len(label) == 0: # end of sentence
95-
for i in xrange(len(one_seg[0])):
96+
for i in range(len(one_seg[0])):
9697
a_kind_lable = [x[i] for x in one_seg]
9798
labels.append(a_kind_lable)
9899

python/paddle/dataset/flowers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@
2828
http://www.robots.ox.ac.uk/~vgg/publications/papers/nilsback08.{pdf,ps.gz}.
2929
3030
"""
31-
import cPickle
3231
import itertools
3332
import functools
34-
from common import download
33+
from .common import download
3534
import tarfile
3635
import scipy.io as scio
3736
from paddle.dataset.image import *
3837
from paddle.reader import *
3938
import os
4039
import numpy as np
4140
from multiprocessing import cpu_count
41+
from six.moves import cPickle as pickle
42+
from six.moves import zip
4243
__all__ = ['train', 'test', 'valid']
4344

4445
DATA_URL = 'http://www.robots.ox.ac.uk/~vgg/data/flowers/102/102flowers.tgz'
@@ -116,10 +117,10 @@ def reader():
116117
file = file.strip()
117118
batch = None
118119
with open(file, 'r') as f:
119-
batch = cPickle.load(f)
120+
batch = pickle.load(f)
120121
data = batch['data']
121122
labels = batch['label']
122-
for sample, label in itertools.izip(data, batch['label']):
123+
for sample, label in zip(data, batch['label']):
123124
yield sample, int(label) - 1
124125
if not cycle:
125126
break

python/paddle/dataset/image.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
cv2 = None
3737
import os
3838
import tarfile
39-
import cPickle
39+
import six.moves.cPickle as pickle
4040

4141
__all__ = [
4242
"load_image_bytes", "load_image", "resize_short", "to_chw", "center_crop",
@@ -86,21 +86,21 @@ def batch_images_from_tar(data_file,
8686
output = {}
8787
output['label'] = labels
8888
output['data'] = data
89-
cPickle.dump(
89+
pickle.dump(
9090
output,
9191
open('%s/batch_%d' % (out_path, file_id), 'w'),
92-
protocol=cPickle.HIGHEST_PROTOCOL)
92+
protocol=pickle.HIGHEST_PROTOCOL)
9393
file_id += 1
9494
data = []
9595
labels = []
9696
if len(data) > 0:
9797
output = {}
9898
output['label'] = labels
9999
output['data'] = data
100-
cPickle.dump(
100+
pickle.dump(
101101
output,
102102
open('%s/batch_%d' % (out_path, file_id), 'w'),
103-
protocol=cPickle.HIGHEST_PROTOCOL)
103+
protocol=pickle.HIGHEST_PROTOCOL)
104104

105105
with open(meta_file, 'a') as meta:
106106
for file in os.listdir(out_path):

python/paddle/dataset/imdb.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ def tokenize(pattern):
4242
# sequential access of member files, other than
4343
# tarfile.extractfile, which does random access and might
4444
# destroy hard disks.
45-
tf = tarf.next()
45+
tf = next(tarf)
4646
while tf != None:
4747
if bool(pattern.match(tf.name)):
4848
# newline and punctuations removal and ad-hoc tokenization.
4949
yield tarf.extractfile(tf).read().rstrip("\n\r").translate(
5050
None, string.punctuation).lower().split()
51-
tf = tarf.next()
51+
tf = next(tarf)
5252

5353

5454
def build_dict(pattern, cutoff):
@@ -62,11 +62,11 @@ def build_dict(pattern, cutoff):
6262
word_freq[word] += 1
6363

6464
# Not sure if we should prune less-frequent words here.
65-
word_freq = filter(lambda x: x[1] > cutoff, word_freq.items())
65+
word_freq = [x for x in list(word_freq.items()) if x[1] > cutoff]
6666

6767
dictionary = sorted(word_freq, key=lambda x: (-x[1], x[0]))
6868
words, _ = list(zip(*dictionary))
69-
word_idx = dict(zip(words, xrange(len(words))))
69+
word_idx = dict(list(zip(words, list(range(len(words))))))
7070
word_idx['<unk>'] = len(words)
7171
return word_idx
7272

python/paddle/dataset/imikolov.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ def build_dict(min_word_freq=50):
6464
# remove <unk> for now, since we will set it as last index
6565
del word_freq['<unk>']
6666

67-
word_freq = filter(lambda x: x[1] > min_word_freq, word_freq.items())
67+
word_freq = [x for x in list(word_freq.items()) if x[1] > min_word_freq]
6868

6969
word_freq_sorted = sorted(word_freq, key=lambda x: (-x[1], x[0]))
7070
words, _ = list(zip(*word_freq_sorted))
71-
word_idx = dict(zip(words, xrange(len(words))))
71+
word_idx = dict(list(zip(words, list(range(len(words))))))
7272
word_idx['<unk>'] = len(words)
7373

7474
return word_idx

python/paddle/dataset/mnist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def reader():
6565

6666
images = images / 255.0 * 2.0 - 1.0
6767

68-
for i in xrange(buffer_size):
68+
for i in range(buffer_size):
6969
yield images[i, :], int(labels[i])
7070
finally:
7171
try:

0 commit comments

Comments
 (0)