Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize Python 2 code to get ready for Python 3 #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions nesmdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import apu
import convert
import cycle
from __future__ import absolute_import
from . import apu
from . import convert
from . import cycle
7 changes: 4 additions & 3 deletions nesmdb/convert.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import cPickle as pickle
import os

Expand Down Expand Up @@ -294,7 +295,7 @@ def main():
out_fps = [os.path.join(args.out_dir, fn) for fn in out_fns]

if os.path.exists(args.out_dir):
print 'WARNING: Output directory {} already exists'.format(args.out_dir)
print('WARNING: Output directory {} already exists'.format(args.out_dir))
else:
os.makedirs(args.out_dir)

Expand Down Expand Up @@ -324,8 +325,8 @@ def main():
try:
out_file = globals()[args.conversion](in_file, **kwargs)
except:
print '-' * 80
print in_fp
print('-' * 80)
print(in_fp)
traceback.print_exc()
continue

Expand Down
19 changes: 10 additions & 9 deletions nesmdb/cycle.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import numpy as np

import nesmdb.vgm
Expand Down Expand Up @@ -152,7 +153,7 @@ def vgm_dist(a_vgm, b_vgm):
args = parser.parse_args()

if args.representation not in ['ndr', 'ndf', 'nlm', 'rawsco'] and args.score_rate is None:
print 'Must specify --score_rate'
print('Must specify --score_rate')
sys.exit(1)

kwargs = {'score_rate': args.score_rate}
Expand All @@ -169,9 +170,9 @@ def vgm_dist(a_vgm, b_vgm):
try:
cycle_vgm = vgm_cycle(source_vgm, representation=args.representation, **kwargs)
except:
print '-' * 80
print vgm_fp
print traceback.print_exc()
print('-' * 80)
print(vgm_fp)
print(traceback.print_exc())
continue

dist, source_wav, cycle_wav = vgm_dist(source_vgm, cycle_vgm)
Expand All @@ -188,13 +189,13 @@ def vgm_dist(a_vgm, b_vgm):
nesmdb.vgm.save_vgmwav(vgm_fp.replace('.vgm', '.cycle.wav'), cycle_wav)

dists = vgm_fp_to_dist.values()
print 'Mean distance (n={}): {} (std={})'.format(len(dists), np.mean(dists), np.std(dists))
print('Mean distance (n={}): {} (std={})'.format(len(dists), np.mean(dists), np.std(dists)))

if not args.quiet:
print '-' * 40 + ' Worst ' + '-' * 40
print('-' * 40 + ' Worst ' + '-' * 40)
for vgm_fp, dist in sorted(vgm_fp_to_dist.items(), key=lambda x:-x[1])[:8]:
print '{:.8f}: {}'.format(dist, vgm_fp)
print('{:.8f}: {}'.format(dist, vgm_fp))

print '-' * 40 + ' Best ' + '-' * 40
print('-' * 40 + ' Best ' + '-' * 40)
for vgm_fp, dist in sorted(vgm_fp_to_dist.items(), key=lambda x:x[1])[:8]:
print '{:.8f}: {}'.format(dist, vgm_fp)
print('{:.8f}: {}'.format(dist, vgm_fp))
11 changes: 6 additions & 5 deletions nesmdb/score/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rawsco import ndf_to_rawsco, rawsco_to_ndf
from exprsco import rawsco_to_exprsco, exprsco_downsample, exprsco_to_rawsco
from seprsco import exprsco_to_seprsco, seprsco_to_exprsco
from blndsco import exprsco_to_blndsco, blndsco_to_exprsco
from midi import exprsco_to_midi, midi_to_exprsco
from __future__ import absolute_import
from .rawsco import ndf_to_rawsco, rawsco_to_ndf
from .exprsco import rawsco_to_exprsco, exprsco_downsample, exprsco_to_rawsco
from .seprsco import exprsco_to_seprsco, seprsco_to_exprsco
from .blndsco import exprsco_to_blndsco, blndsco_to_exprsco
from .midi import exprsco_to_midi, midi_to_exprsco
5 changes: 5 additions & 0 deletions nesmdb/score/blndsco.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import numpy as np

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


def exprsco_to_blndsco(exprsco):
rate, nsamps, score = exprsco
Expand Down
5 changes: 5 additions & 0 deletions nesmdb/score/exprsco.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import numpy as np
from scipy.stats import mode

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


def rawsco_to_exprsco(rawsco, midi_valid_range=(21, 108)):
clock, rate, nsamps, rawsco = rawsco
Expand Down
5 changes: 5 additions & 0 deletions nesmdb/score/midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import numpy as np
import tempfile

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


def exprsco_to_midi(exprsco):
import pretty_midi
Expand Down
5 changes: 5 additions & 0 deletions nesmdb/score/rawsco.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

import nesmdb.apu

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


fs = 44100.
dt = 1. / fs
Expand Down
13 changes: 7 additions & 6 deletions nesmdb/vgm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from vgm_ndr import vgm_to_ndr, ndr_to_vgm
from ndr_ndf import ndr_to_ndf, ndf_to_ndr
from ndf_nlm import ndf_to_nlm, nlm_to_ndf
from nd_txt import nd_to_txt, txt_to_nd
from vgm_simplify import vgm_simplify, vgm_shorten
from vgm_to_wav import vgm_to_wav, load_vgmwav, save_vgmwav
from __future__ import absolute_import
from .vgm_ndr import vgm_to_ndr, ndr_to_vgm
from .ndr_ndf import ndr_to_ndf, ndf_to_ndr
from .ndf_nlm import ndf_to_nlm, nlm_to_ndf
from .nd_txt import nd_to_txt, txt_to_nd
from .vgm_simplify import vgm_simplify, vgm_shorten
from .vgm_to_wav import vgm_to_wav, load_vgmwav, save_vgmwav
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
from setuptools import setup
from setuptools.command.install import install
from setuptools.command.develop import develop
Expand All @@ -17,14 +18,14 @@ def _build_vgm_play(build_temp, build_lib):
made_build_temp = True

# Download VGMPlay 0.40.8
print 'Downloading VGMPlay'
print('Downloading VGMPlay')
tgz_filepath = os.path.join(build_temp, '0.40.8.tar.gz')
urllib.urlretrieve(
'https://github.com/vgmrips/vgmplay/archive/0.40.8.tar.gz',
tgz_filepath)

# Extract
print 'Extracting VGMPlay'
print('Extracting VGMPlay')
with tarfile.open(tgz_filepath, 'r:gz') as f:
f.extractall(build_temp)
vgmplay_dir = os.path.join(build_temp, 'vgmplay-0.40.8', 'VGMPlay')
Expand All @@ -40,7 +41,7 @@ def _build_vgm_play(build_temp, build_lib):
f.write(makefile)

# Build
print 'Building VGMPlay'
print('Building VGMPlay')
command = 'make -C {} vgm2wav'.format(vgmplay_dir)
res = subprocess.call(command.split())
if res > 0:
Expand Down