Skip to content

Commit

Permalink
Make code (test code at least) compatible to Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
pazeshun committed Aug 18, 2023
1 parent 7a9ea00 commit df165a8
Show file tree
Hide file tree
Showing 29 changed files with 171 additions and 27 deletions.
5 changes: 5 additions & 0 deletions archives/master.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion archives/open_color_histogram_data.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demos/grasp_fusion/ros/grasp_fusion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ find_package(catkin REQUIRED catkin_virtualenv)

catkin_python_setup()

# add_custom_target(${PROJECT_NAME}_install_data ALL COMMAND ${PROJECT_SOURCE_DIR}/scripts/install_data.py)
# add_custom_target(${PROJECT_NAME}_install_data ALL COMMAND python$ENV{ROS_PYTHON_VERSION} ${PROJECT_SOURCE_DIR}/scripts/install_data.py)
# Don't install data on build time to prevent build failure on travis due to googledrive access limit

################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ find_package(catkin REQUIRED catkin_virtualenv)

catkin_python_setup()

# add_custom_target(${PROJECT_NAME}_install_data ALL COMMAND ${PROJECT_SOURCE_DIR}/scripts/install_data.py)
# add_custom_target(${PROJECT_NAME}_install_data ALL COMMAND python$ENV{ROS_PYTHON_VERSION} ${PROJECT_SOURCE_DIR}/scripts/install_data.py)
# Don't install data on build time to prevent build failure on travis due to googledrive access limit

################################################
Expand Down
2 changes: 1 addition & 1 deletion demos/selective_dualarm_grasping/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ generate_messages(
jsk_recognition_msgs
)

# add_custom_target(${PROJECT_NAME}_install_models ALL COMMAND ${PROJECT_SOURCE_DIR}/scripts/install_models.py)
# add_custom_target(${PROJECT_NAME}_install_models ALL COMMAND python$ENV{ROS_PYTHON_VERSION} ${PROJECT_SOURCE_DIR}/scripts/install_models.py)

generate_dynamic_reconfigure_options(
cfg/DualarmGraspSegmentation.cfg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
from utils import get_APC_pt


# Python2's xrange equals Python3's range, and xrange is removed on Python3
if not hasattr(__builtins__, 'xrange'):
xrange = range


def compute_point_threshold(
gpu, data_type, log_dir, model_type):
answer_pt_list = []
Expand Down
5 changes: 5 additions & 0 deletions demos/selective_dualarm_stowing/experiments/show_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
from selective_arm_stowing.dataset.v4 import DualarmDatasetV4


# Python2's xrange equals Python3's range, and xrange is removed on Python3
if not hasattr(__builtins__, 'xrange'):
xrange = range


def show_acc_graph(log_dir, line_num):
for root, dirs, files in os.walk(log_dir):
plt.figure()
Expand Down
7 changes: 6 additions & 1 deletion jsk_2015_05_baxter_apc/json/2016_ar/generate_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import jsk_apc2015_common


# Python2's xrange equals Python3's range, and xrange is removed on Python3
if not hasattr(__builtins__, 'xrange'):
xrange = range


parser = argparse.ArgumentParser()
parser.add_argument('json_id')
args = parser.parse_args()
Expand Down Expand Up @@ -51,4 +56,4 @@
if osp.exists(json_file):
print('json file already exists: {0}'.format(json_file))
else:
json.dump(json_data, open(json_file, 'wb'), indent=2)
json.dump(json_data, open(json_file, 'wb'), indent=2)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import numpy as np
import os
import gzip
import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle

from sklearn.svm import SVC
from skimage.io import imread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
from __future__ import division
import rospy
import cv2
import cPickle
try:
# Python2
import cPickle
except ModuleNotFoundError:
# Python3
import _pickle as cPickle
import gzip
import numpy as np
import os
Expand Down
7 changes: 6 additions & 1 deletion jsk_2015_05_baxter_apc/node_scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import os
import yaml
import gzip
import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle

import cv2
from catkin import terminal_color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
"""
import os
import gzip
import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle

import cv2
import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion jsk_2016_01_baxter_apc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ if(CATKIN_ENABLE_TESTING)
add_rostest(test/work_order.test)
add_rostest(test/stow_work_order.test)
add_rostest(test/apply_bin_contents_hint.test)
add_custom_target(download_test_data COMMAND ${PROJECT_SOURCE_DIR}/scripts/download_test_data.py)
add_custom_target(download_test_data COMMAND python$ENV{ROS_PYTHON_VERSION} ${PROJECT_SOURCE_DIR}/scripts/download_test_data.py)
if(TARGET tests)
add_dependencies(tests download_test_data)
endif()
Expand Down
7 changes: 6 additions & 1 deletion jsk_apc2015_common/scripts/test_bof_object_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
# -*- coding: utf-8 -*-

import argparse
import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle
import gzip
import sys

Expand Down
5 changes: 5 additions & 0 deletions jsk_apc2015_common/src/jsk_apc2015_common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
PKG = 'jsk_apc2015_common'


# Python2's xrange equals Python3's range, and xrange is removed on Python3
if not hasattr(__builtins__, 'xrange'):
xrange = range


def get_object_list():
"""Returns the object name list for APC2015.
Expand Down
4 changes: 2 additions & 2 deletions jsk_apc2015_common/src/jsk_apc2015_common/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def test_load_json():
json_file = osp.join(_this_dir, 'data', 'f2.json')
bin_contents, work_order = jsk_apc2015_common.load_json(json_file)
assert_true(isinstance(bin_contents, dict))
assert_equal(bin_contents.keys(), ['f'])
assert_equal(list(bin_contents.keys()), ['f'])
assert_true(isinstance(work_order, dict))
assert_equal(work_order.keys(), ['f'])
assert_equal(list(work_order.keys()), ['f'])


def test__get_tile_shape():
Expand Down
4 changes: 2 additions & 2 deletions jsk_apc2016_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ target_link_libraries(sib_topic_synchronizer ${catkin_LIBRARIES})
target_link_libraries(cloud_to_spatial_features ${catkin_LIBRARIES} ${Eigen3_LIBRARIES})

# trained data
# add_custom_target(install_trained_data ALL COMMAND ${PROJECT_SOURCE_DIR}/scripts/install_trained_data.py)
# add_custom_target(install_trained_data ALL COMMAND python$ENV{ROS_PYTHON_VERSION} ${PROJECT_SOURCE_DIR}/scripts/install_trained_data.py)
# sample data
# add_custom_target(install_sample_data ALL COMMAND ${PROJECT_SOURCE_DIR}/scripts/install_sample_data.py)
# add_custom_target(install_sample_data ALL COMMAND python$ENV{ROS_PYTHON_VERSION} ${PROJECT_SOURCE_DIR}/scripts/install_sample_data.py)

#############
## Install ##
Expand Down
2 changes: 1 addition & 1 deletion jsk_apc2016_common/python/jsk_apc2016_common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def visualize_stow_contents(work_order):
tote_region = [[190, 230], [1080, 790]]
region_h = tote_region[1][1] - tote_region[0][1]
region_w = tote_region[1][0] - tote_region[0][0]
max_obj_h, max_obj_w = region_h / 3, region_w / 4
max_obj_h, max_obj_w = region_h // 3, region_w // 4
tote_x_min, tote_y_min = tote_region[0][0], tote_region[0][1]
x_min, y_min = tote_x_min, tote_y_min
for obj in work_order:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import cv2
import os.path

import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle
import matplotlib.pyplot as plt
import numpy as np

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import numpy as np
import cv2
import os, sys
import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle
import copy

import utils
Expand Down Expand Up @@ -228,7 +233,12 @@ def display_experiment_objects(filename):
"stick_notes", "safety_glasses", "duck_toy", "highlighters",
"frog_toy", "stanley_66", "dental_treats", "tennis_ball", "shelf"]

import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle

with open(filename,'rb') as f:
results = pickle.load(f)
Expand Down Expand Up @@ -440,7 +450,12 @@ def experiment_baseline(datasets, results_path):

def display_experiment_baseline(filename):

import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle
with open(filename,'rb') as f:
results = pickle.load(f)

Expand Down Expand Up @@ -549,7 +564,12 @@ def experiment_removed_features(datasets, results_path):

def display_experiment_features(filenames):

import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle

results = dict()
for filename in filenames:
Expand Down Expand Up @@ -894,7 +914,12 @@ def experiment_model(datasets, results_path):

def display_experiment_model(filename):

import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle
with open(filename,'rb') as f:
results = pickle.load(f)

Expand Down Expand Up @@ -1000,4 +1025,4 @@ def display_experiment_model(filename):
filename_results = experiment_model(datasets, results_path)
display_experiment_model(filename_results)

print('\nDone.')
print('\nDone.')
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
import cv2
import os.path

import cPickle as pickle
try:
# Python2
import cPickle as pickle
except ModuleNotFoundError:
# Python3
import _pickle as pickle
import matplotlib.pyplot as plt
import numpy as np


# Python2's xrange equals Python3's range, and xrange is removed on Python3
if not hasattr(__builtins__, 'xrange'):
xrange = range


def visualize_color_image(img, title=''):
plt.figure()
plt.title(title)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from geometry_msgs.msg import Point, PointStamped


# Python2's xrange equals Python3's range, and xrange is removed on Python3
if not hasattr(__builtins__, 'xrange'):
xrange = range


class BinData(object):
def __init__(self, *args, **kwargs):
if 'bin_info' in kwargs:
Expand Down
10 changes: 9 additions & 1 deletion jsk_apc2016_common/python/jsk_apc2016_common/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@
import jsk_apc2016_common


# basestring is deleted on Python3
# https://stackoverflow.com/questions/11301138/how-to-check-if-variable-is-string-with-python-2-and-3-compatibility/22679982#22679982
try:
basestring
except NameError:
basestring = str


_this_dir = osp.dirname(osp.realpath(osp.abspath(__file__)))


def test_get_object_data():
obj_data = jsk_apc2016_common.get_object_data()
obj_names = map(lambda d: d['name'], obj_data)
obj_names = list(map(lambda d: d['name'], obj_data))
assert_true(isinstance(obj_data, list))
assert_equal(39, len(obj_data))
assert_equal(sorted(obj_names), obj_names)
Expand Down
5 changes: 5 additions & 0 deletions jsk_arc2017_baxter/scripts/force_recognize_usb_devices
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import time
import termcolor


# Python2's xrange equals Python3's range, and xrange is removed on Python3
if not hasattr(__builtins__, 'xrange'):
xrange = range


def sudo_bash_cmd(cmd):
return "sudo bash -c '%s'" % cmd

Expand Down
2 changes: 1 addition & 1 deletion jsk_arc2017_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ catkin_package(
## Build ##
###########

# add_custom_target(install_data ALL COMMAND ${PROJECT_SOURCE_DIR}/scripts/install_data.py)
# add_custom_target(install_data ALL COMMAND python$ENV{ROS_PYTHON_VERSION} ${PROJECT_SOURCE_DIR}/scripts/install_data.py)

#############
## Install ##
Expand Down
Loading

0 comments on commit df165a8

Please sign in to comment.