Skip to content

Commit ea5b46e

Browse files
authored
Merge pull request #15 from BD2KGenomics/issues/14python3
Futurize (resolves #14)
2 parents a662f3c + 9b30604 commit ea5b46e

21 files changed

Lines changed: 83 additions & 40 deletions

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
package_dir={ '': 'src' },
1717
packages=find_packages( 'src' ),
18-
install_requires=[ ],
18+
install_requires=[ 'future' ],
1919
tests_require=[
2020
'pytest==2.7.2',
2121
'mock==1.0.1',
2222
'lockfile==0.11.0',
23-
'boto==2.38.0' ],
23+
'boto==2.38.0'],
2424
namespace_packages=[ 'bd2k' ] )
2525

2626
from setuptools.command.test import test as TestCommand

src/bd2k/util/collections.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import
22

3+
from builtins import next
34
import collections
45
from itertools import dropwhile
56

@@ -155,7 +156,7 @@ def rindex( l, v ):
155156
2
156157
"""
157158
try:
158-
n = next( dropwhile( lambda (i, x): v != x, enumerate( reversed( l ), 1 ) ) )[ 0 ]
159+
n = next( dropwhile( lambda i_x: v != i_x[1], enumerate( reversed( l ), 1 ) ) )[ 0 ]
159160
except StopIteration:
160161
raise ValueError( v )
161162
else:

src/bd2k/util/d32.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import division
12
# Copyright (c) 2015 Hannes Schmidt
23
#
34
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
@@ -17,6 +18,10 @@
1718

1819
# Inspired by Dominic Tarr's JavaScript at https://github.com/dominictarr/d64
1920

21+
from builtins import str
22+
from builtins import range
23+
from builtins import object
24+
from past.utils import old_div
2025
class D32( object ):
2126
"""
2227
Base32 encoding and decoding without padding, and using an arbitrary alphabet.
@@ -26,7 +31,7 @@ def __init__( self, alphabet ):
2631
super( D32, self ).__init__( )
2732
self.alphabet = bytearray( alphabet )
2833
self.lookup = bytearray( 255 )
29-
for i in xrange( 32 ):
34+
for i in range( 32 ):
3035
self.lookup[ self.alphabet[ i ] ] = i
3136

3237
def encode( self, d ):
@@ -44,7 +49,7 @@ def encode( self, d ):
4449
'222k62s62o'
4550
"""
4651
m = len( d )
47-
n = (m * 8 + 4) / 5
52+
n = old_div((m * 8 + 4), 5)
4853
padding = 8 - n % 8
4954
e = bytearray( n + padding )
5055
i, j = 0, 0
@@ -83,7 +88,7 @@ def decode( self, e ):
8388
'\\xff'
8489
"""
8590
n = len( e )
86-
m = n * 5 / 8
91+
m = old_div(n * 5, 8)
8792
padding = 5 - m % 5
8893
d = bytearray( m + padding )
8994
i, j = 0, 0
@@ -104,7 +109,7 @@ def decode( self, e ):
104109
d[ i + 4 ] = g[ 6 ] << 5 & 255 | g[ 7 ]
105110
j += 8
106111
i += 5
107-
return str( d[ :-padding ] )
112+
return bytes( d[ :-padding ] )
108113

109114

110115
# A variant of Base64 that maintains the lexicographical ordering such that for any given list of

src/bd2k/util/d64.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import division
12
# Copyright (c) 2014 Dominic Tarr
23
# Copyright (c) 2015 Hannes Schmidt
34
#
@@ -20,13 +21,17 @@
2021

2122

2223

24+
from builtins import str
25+
from builtins import range
26+
from builtins import object
27+
from past.utils import old_div
2328
class D64( object ):
2429
def __init__( self, special_chars ):
2530
super( D64, self ).__init__( )
2631
alphabet = 'PYFGCRLAOEUIDHTNSQJKXBMWVZpyfgcrlaoeuidhtnsqjkxbmwvz1234567890'
2732
self.alphabet = bytearray( sorted( alphabet + special_chars ) )
2833
self.lookup = bytearray( 255 )
29-
for i in xrange( 64 ):
34+
for i in range( 64 ):
3035
code = self.alphabet[ i ]
3136
self.lookup[ code ] = i
3237

@@ -45,11 +50,11 @@ def encode( self, data ):
4550
'..31.kF40VR'
4651
"""
4752
l = len( data )
48-
s = bytearray( (l * 4 + 2) / 3 )
53+
s = bytearray( old_div((l * 4 + 2), 3) )
4954
hang = 0
5055
j = 0
5156
a = self.alphabet
52-
for i in xrange( l ):
57+
for i in range( l ):
5358
v = ord( data[ i ] )
5459
r = i % 3
5560
if r == 0:
@@ -89,11 +94,11 @@ def decode( self, e ):
8994
"""
9095
n = len( e )
9196
j = 0
92-
b = bytearray( n * 3 / 4 )
97+
b = bytearray( old_div(n * 3, 4) )
9398
hang = 0
9499
l = self.lookup
95100

96-
for i in xrange( n ):
101+
for i in range( n ):
97102
v = l[ ord( e[ i ] ) ]
98103
r = i % 4
99104
if r == 0:
@@ -111,7 +116,7 @@ def decode( self, e ):
111116
j += 1
112117
else:
113118
assert False
114-
return str( b )
119+
return bytes( b )
115120

116121

117122
standard = D64( '._' )

src/bd2k/util/ec2/credentials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _populate_keys_from_metadata_server( self ):
109109
log.debug( 'Racing to create %s.', tmp_path )
110110
# Only one process, the winner, will succeed
111111
try:
112-
fd = os.open( tmp_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0600 )
112+
fd = os.open( tmp_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600 )
113113
except OSError as e:
114114
if e.errno == errno.EEXIST:
115115
log.debug( 'Lost the race to create %s. Waiting on winner to remove it.', tmp_path )

src/bd2k/util/ec2/test/test_credentials.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from builtins import range
12
import logging
23

34
import errno

src/bd2k/util/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from builtins import object
12
from contextlib import contextmanager
23
import sys
34

src/bd2k/util/hashes.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from builtins import str
2+
from builtins import next
3+
from past.builtins import basestring
14
def hash_json( hash_obj, value ):
25
"""
36
Compute the hash of a parsed JSON value using the given hash object. This function does not
@@ -58,7 +61,7 @@ def hash_json( hash_obj, value ):
5861
ValueError: Type <type 'object'> is not supported
5962
"""
6063
try:
61-
items = value.iteritems( )
64+
items = iter(value.items( ))
6265
except AttributeError:
6366
# Must check for string before testing iterability since strings are iterable
6467
if isinstance( value, basestring ):
@@ -123,7 +126,8 @@ def _hash_hashable( hash_obj, items ):
123126
hash_obj.update( '}' )
124127

125128

126-
def _hash_hashable_item( hash_obj, (k, v) ):
129+
def _hash_hashable_item( hash_obj, k_v ):
130+
(k, v) = k_v
127131
if isinstance( k, basestring ):
128132
hash_obj.update( k )
129133
hash_obj.update( ':' )

src/bd2k/util/humanize.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
Author: Giampaolo Rodola' <g.rodola [AT] gmail [DOT] com>
99
License: MIT
1010
"""
11+
from __future__ import division
1112

1213
# see: http://goo.gl/kTQMs
14+
from past.utils import old_div
1315
SYMBOLS = {
1416
'customary' : ('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'),
1517
'customary_ext' : ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa',
@@ -65,7 +67,7 @@ def bytes2human(n, fmt='%(value).1f %(symbol)s', symbols='customary'):
6567
prefix[s] = 1 << (i+1)*10
6668
for symbol in reversed(symbols[1:]):
6769
if n >= prefix[symbol]:
68-
value = float(n) / prefix[symbol]
70+
value = old_div(float(n), prefix[symbol])
6971
return fmt % locals()
7072
return fmt % dict(symbol=symbols[0], value=n)
7173

@@ -110,7 +112,7 @@ def human2bytes(s):
110112
s = s[1:]
111113
num = float(num)
112114
letter = s.strip()
113-
for name, sset in SYMBOLS.items():
115+
for name, sset in list(SYMBOLS.items()):
114116
if letter in sset:
115117
break
116118
else:

src/bd2k/util/iterables.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
from itertools import takewhile, izip, izip_longest, dropwhile, imap, chain
1+
from builtins import map
2+
from builtins import zip
3+
from builtins import object
4+
from itertools import takewhile, dropwhile, chain
5+
try:
6+
from itertools import zip_longest as zip_longest
7+
except:
8+
from itertools import izip_longest as zip_longest
29

310

411
def common_prefix( xs, ys ):
@@ -18,7 +25,7 @@ def common_prefix( xs, ys ):
1825
>>> list( common_prefix('A','B') )
1926
[]
2027
"""
21-
return imap( lambda (x, y): x, takewhile( lambda (a, b): a == b, izip( xs, ys ) ) )
28+
return map( lambda x_y: x_y[0], takewhile( lambda a_b: a_b[0] == a_b[1], zip( xs, ys ) ) )
2229

2330

2431
def disparate_suffix( xs, ys ):
@@ -38,7 +45,7 @@ def disparate_suffix( xs, ys ):
3845
>>> list( disparate_suffix('A','B') )
3946
[('A', 'B')]
4047
"""
41-
return dropwhile( lambda (a, b): a == b, izip_longest( xs, ys ) )
48+
return dropwhile( lambda a_b1: a_b1[0] == a_b1[1], zip_longest( xs, ys ) )
4249

4350

4451
def flatten( iterables ):
@@ -121,7 +128,7 @@ def expand( x ):
121128
i = x,
122129
return i
123130

124-
return flatten( imap( expand, self.args ) )
131+
return flatten( map( expand, self.args ) )
125132

126133

127134
# noinspection PyPep8Naming
@@ -166,4 +173,4 @@ def expand( x ):
166173
except AttributeError:
167174
return x,
168175

169-
return flatten( imap( expand, self.iterables ) )
176+
return flatten( map( expand, self.iterables ) )

0 commit comments

Comments
 (0)