old_contents: #!/usr/bin/python3
  # requires system Python and the python3-apt package
  from collections import OrderedDict # Starting with Python 3.7, we could just use vanilla dicts
  import apt # ImportError? apt install python3-apt
  
  def describe(pkg):
  	# Python 3.7 equivalent:
  	# return {"Name": pkg.name, "Installed": pkg.installed.version, "Candidate": pkg.candidate.version}
  	return OrderedDict((("Name", pkg.name), ("Current", pkg.installed.version), ("Target", pkg.candidate.version)))
  
  def show_packages(scr, upgrades, auto):
  	def print(s="", *args):
  		scr.addstr(str(s) + "\n", *args)
  	desc = [describe(pkg) for pkg in upgrades]
  	widths = OrderedDict((x, len(x)) for x in desc[0]) # Start with header widths
  	for d in desc:
  		for col in d:
  			widths[col] = max(widths[col], len(d[col]))
  	fmt = "[ ] " + "  ".join("%%-%ds" % col for col in widths.values())
  	print(fmt % tuple(widths), curses.A_BOLD)
  	print("--- " + "  ".join("-" * col for col in widths.values()))
  	# TODO: Cope with more packages than lines on the screen (scroll? paginate?)
  	for d in desc:
  		print(fmt % tuple(d.values()))
  
  	print()
  	if auto: print("Plus %d auto-installed packages." % auto)
  	while True:
  		key = scr.getkey()
  		if key == "Q" or key == "q": break
  
  def main():
  	cache = apt.Cache()
  	cache.open()
  	upgrades = []
  	auto = 0
  	for pkg in cache:
  		if not pkg.is_installed: continue # This is checking upgrades only
  		if pkg.candidate == pkg.installed: continue # Already up-to-date
  		if pkg.is_auto_installed:
  			# Ignore (but summarize) autoinstalled packages
  			auto += 1
  			continue
  		upgrades.append(pkg)
  	if not upgrades:
  		print("Everything up-to-date.")
  		return
  
  	global curses; import curses
  	curses.wrapper(show_packages, upgrades, auto)
  
  if __name__ == "__main__":
  	main()
  
new_contents: #!/usr/bin/python3
  # requires system Python and the python3-apt package
  from collections import OrderedDict # Starting with Python 3.7, we could just use vanilla dicts
  import apt # ImportError? apt install python3-apt
  
  def describe(pkg):
  	# Python 3.7 equivalent:
  	# return {"Name": pkg.name, "Installed": pkg.installed.version, "Candidate": pkg.candidate.version}
  	return OrderedDict((("Name", pkg.name), ("Current", pkg.installed.version), ("Target", pkg.candidate.version)))
  
  def show_packages(scr, upgrades, auto):
  	def print(s="", *args):
  		scr.addstr(str(s) + "\n", *args)
  	desc = [describe(pkg) for pkg in upgrades]
  	widths = OrderedDict((x, len(x)) for x in desc[0]) # Start with header widths
  	for d in desc:
  		for col in d:
  			widths[col] = max(widths[col], len(d[col]))
  	fmt = "[ ] " + "  ".join("%%-%ds" % col for col in widths.values())
  	print(fmt % tuple(widths), curses.A_BOLD)
  	print("--- " + "  ".join("-" * col for col in widths.values()))
  	# TODO: Cope with more packages than lines on the screen (scroll? paginate?)
  	for d in desc:
  		print(fmt % tuple(d.values()))
  
  	print()
  	if auto: print("Plus %d auto-installed packages." % auto)
  	print("Select packages to upgrade, then Enter to apply.")
  	print("Press I for more info on a package [TODO]")
  	pkg = 0
  	install = [False] * len(upgrades)
  	while True:
  		scr.move(pkg + 2, 1)
  		key = scr.getkey()
  		if key == "Q" or key == "q": return []
  		if key == "\n": break
  		if key == "KEY_UP":   pkg = (pkg - 1) % len(upgrades)
  		if key == "KEY_DOWN": pkg = (pkg + 1) % len(upgrades)
  		if key == " ":
  			install[pkg] = not install[pkg]
  			scr.addstr(pkg + 2, 1, "X" if install[pkg] else " ")
  		# scr.addstr(len(upgrades) + 7, 0, repr(key))
  	return [pkg for pkg, keep in zip(upgrades, install) if keep]
  
  def main():
  	cache = apt.Cache()
  	cache.open()
  	upgrades = []
  	auto = 0
  	for pkg in cache:
  		if not pkg.is_installed: continue # This is checking upgrades only
  		if pkg.candidate == pkg.installed: continue # Already up-to-date
  		if pkg.is_auto_installed:
  			# Ignore (but summarize) autoinstalled packages
  			auto += 1
  			continue
  		upgrades.append(pkg)
  	if not upgrades:
  		print("Everything up-to-date.")
  		return
  
  	global curses; import curses
  	upgrades = curses.wrapper(show_packages, upgrades, auto)
  	if not upgrades: return
  	print(upgrades)
  
  if __name__ == "__main__":
  	main()
  
subject: Allow user to select packages to upgrade