Use print() function in both Python 2 and Python 3

Legacy __print__ statements are syntax errors in Python 3 but __print()__ function works as expected in both Python 2 and Python 3.
This commit is contained in:
cclauss
2019-09-22 13:54:24 -04:00
committed by Daniel Agar
parent cdbe4a3ee8
commit c18104d48b
5 changed files with 53 additions and 48 deletions
+8 -7
View File
@@ -41,6 +41,7 @@ Usage: python fetch_file.py [-l] [-f] [-d device] [-s speed] [-o out_path] path
\t-s\tSerial baudrate
\t-o\tOutput path
\tpath\tPath to list/fetch, if ends with "/" then directory will be fetched recursively"""
from __future__ import print_function
__author__ = "Anton Babushkin"
__version__ = "1.1"
@@ -82,7 +83,7 @@ def _ls_dir(ser, dir, timeout):
return res
def _get_file(ser, fn, fn_out, force, timeout):
print "Get %s:" % fn,
print("Get %s:" % fn, end=' ')
if not force:
# Check if file already exists with the same size
try:
@@ -90,7 +91,7 @@ def _get_file(ser, fn, fn_out, force, timeout):
except:
pass
else:
print "already fetched, skip"
print("already fetched, skip")
return
cmd = "dumpfile " + fn
@@ -112,7 +113,7 @@ def _get_file(ser, fn, fn_out, force, timeout):
sys.stdout.write(".")
sys.stdout.flush()
fout.write(buf)
print " done"
print(" done")
fout.close()
os.rename(fn_out_part, fn_out)
else:
@@ -133,13 +134,13 @@ def _get_files_in_dir(ser, path, path_out, force, timeout):
_get_file(ser, path_fn, path_fn_out, force, timeout)
def _usage():
print """Usage: python fetch_file.py [-l] [-f] [-d device] [-s speed] [-o out_path] path
print("""Usage: python fetch_file.py [-l] [-f] [-d device] [-s speed] [-o out_path] path
\t-l\tList files
\t-f\tOverwrite existing files
\t-d\tSerial device
\t-s\tSerial baudrate
\t-o\tOutput path
\tpath\tPath to list/fetch, if ends with "/" then directory will be fetched recursively"""
\tpath\tPath to list/fetch, if ends with "/" then directory will be fetched recursively""")
def _main():
dev = "/dev/tty.usbmodem1"
@@ -185,7 +186,7 @@ def _main():
try:
if cmd == "ls":
# List directory
print _ls_dir_raw(ser, path, timeout)
print(_ls_dir_raw(ser, path, timeout))
elif cmd == "get":
# Get file(s)
if path.endswith("/"):
@@ -199,7 +200,7 @@ def _main():
path_out = os.path.split(path)[1]
_get_file(ser, path, os.path.split(path)[1], force, timeout)
except Exception as e:
print e
print(e)
ser.close()
+14 -13
View File
@@ -30,6 +30,7 @@
# Parts included from https://github.com/PX4/flight_review/
from __future__ import print_function
import os, sys, time, datetime, piexif
from pyulog import *
from pyulog.px4 import *
@@ -38,9 +39,9 @@ from fractions import Fraction
if(len(sys.argv)) < 3:
print "Usage: python geotag_images_ulog.py [logfile] [image dir]"
print "Example: python geotag_images_ulog.py mylog.ulg ./images"
print len(sys.argv)
print("Usage: python geotag_images_ulog.py [logfile] [image dir]")
print("Example: python geotag_images_ulog.py mylog.ulg ./images")
print(len(sys.argv))
sys.exit()
logfile = sys.argv[1]
@@ -189,13 +190,13 @@ for f in reversed(files):
timestamp = time.mktime(datetime.datetime.strptime(timestring, "%Y:%m:%d %H:%M:%S").timetuple())
if first == 0:
first = timestamp
print "Calibrating on",f,"as last image on",timestring
print ""
print "[filename] [offset] [trigger seq] [lat] [lng] [alt]"
print("Calibrating on",f,"as last image on",timestring)
print("")
print("[filename] [offset] [trigger seq] [lat] [lng] [alt]")
offset = first - timestamp
print(f),
print(offset),
print((f), end=' ')
print((offset), end=' ')
if not offset in offsets:
offset += 1
@@ -203,9 +204,9 @@ for f in reversed(files):
offset += 1
if offset in offsets:
print(offsets[offset]),
print(camera_capture.data['lat'][offsets[offset]]),
print(camera_capture.data['lon'][offsets[offset]]),
print((offsets[offset]), end=' ')
print((camera_capture.data['lat'][offsets[offset]]), end=' ')
print((camera_capture.data['lon'][offsets[offset]]), end=' ')
print(camera_capture.data['alt'][offsets[offset]])
lat = camera_capture.data['lat'][offsets[offset]]
@@ -232,7 +233,7 @@ for f in reversed(files):
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, f)
else:
print "Could not georeference"
print("Could not georeference")
print "Done"
print("Done")
+7 -6
View File
@@ -33,6 +33,7 @@ Example invocations.
Compilation database setup:
http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
"""
from __future__ import print_function
import argparse
import json
@@ -54,7 +55,7 @@ def find_compilation_database(path):
result = './'
while not os.path.isfile(os.path.join(result, path)):
if os.path.realpath(result) == '/':
print 'Error: could not find compilation database.'
print('Error: could not find compilation database.')
sys.exit(1)
result += '../'
return os.path.realpath(result)
@@ -168,9 +169,9 @@ def main():
if args.checks:
invocation.append('-checks=' + args.checks)
invocation.append('-')
print subprocess.check_output(invocation)
print(subprocess.check_output(invocation))
except:
print >>sys.stderr, "Unable to run clang-tidy."
print("Unable to run clang-tidy.", file=sys.stderr)
sys.exit(1)
# Load the database and extract all files.
@@ -208,18 +209,18 @@ def main():
except KeyboardInterrupt:
# This is a sad hack. Unfortunately subprocess goes
# bonkers with ctrl-c and we start forking merrily.
print '\nCtrl-C detected, goodbye.'
print('\nCtrl-C detected, goodbye.')
if args.fix:
shutil.rmtree(tmpdir)
os.kill(0, 9)
if args.fix:
print 'Applying fixes ...'
print('Applying fixes ...')
apply_fixes(args, tmpdir)
global tidy_failures
if tidy_failures > 0:
print >>sys.stderr, "clang-tidy errors: ", tidy_failures
print("clang-tidy errors: ", tidy_failures, file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':