kfsone

A brief dive into TD/Python

Feb 6th, 2015
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. # TradeDangerous: https://bitbucket.org/kfsone/tradedangerous/
  2. # Open up a Python 3 interactive prompt either by starting an
  3. # instance of the 'python' interpreter (typing 'python' or
  4. # 'python3' at a prompt), loading the 'IDLE' Python gui
  5. # (windows users: hit start and type idle), or by running the
  6. # 'ipython' interactive python interpreter.
  7. #
  8. # Possibly the best way is:
  9. #  ipython qtconsole
  10. # If you don't have ipython, try:
  11. #  pip install ipython
  12. # or
  13. #  pip3 install ipython
  14. #
  15. # Once you have a prompt, type or copy and paste from the following
  16. # code.
  17.  
  18. # "import" command loads a module, either from the standard
  19. # library or the current directory (or the 'python path').
  20. # The main module for TradeDangerous is 'tradedb'.
  21. import tradedb
  22.  
  23. # You can now reference things in the module by modulename.thingname
  24. # The cornerstone of TD is the TradeDB object, this is one way to
  25. # create one, and call it 'tdb'.
  26. tdb = tradedb.TradeDB()
  27.  
  28. # Look up a particular system
  29. help(tdb.lookupSystem)
  30. sol = tdb.lookupSystem("sol")
  31.  
  32. # Print some of its information
  33. print("Sol is at {}, {}, {}.".format(sol.posX, sol.posY, sol.posZ))
  34.  
  35. # Query some of it's information in interactive mode:
  36. sol.stations
  37.  
  38. # How did I know it has posX, posY and posZ, or stations?
  39. help(sol)
  40. # (this actually gives me help from the type-of-thing that sol is)
  41.  
  42. # Look up a station
  43. abe = tdb.lookupStation("Abraham Lincoln")
  44. # go on ... find out more... use help
  45.  
  46. # Look up a station in a particular system
  47. abe = tdb.lookupStation("Abraham Lincoln", sol)
  48.  
  49. # Look up a system or station using the flexible naming mechanism
  50. phoenix = tdb.lookupPlace("ascendingp")
  51. type(phoenix)   # is of type tradedb.Station
  52.  
  53. sol = tdb.lookupPlace("@sol")
  54. type(sol)
  55.  
  56. abe = tdb.lookupPlace("sol/hamlinc")
  57.  
  58. # Pick 5 systems at random:
  59. # tdb has a lookup table for <systemID> to <System>,
  60. # implemented using a Python dictionary. So
  61. #  tdb.systemByID.keys() => list of the IDs,
  62. #  tdb.systemByID.values() => list of the Systems,
  63. #  tdb.systemByID.items() => list of (ID, System)
  64. import random
  65. help(random.sample)
  66. systemTable = list(tdb.systemByID.values())
  67. visitMe = random.sample(systemTable, 5)
  68.  
  69. # Find the closest:
  70. # Call distanceTo(origin) on every member of visitMe and
  71. # then retrieve the one with the lowest distance.
  72. origin = tdb.lookupPlace("Eranin")
  73. closest = min(visitMe, key=lambda candidate: candidate.distanceTo(origin))
  74. print("{start} -> {dest}: {dist:.2f} ly".format(
  75.     start=origin.name(), dest=closest.name(),
  76.     dist=origin.distanceTo(closest),
  77. ))
  78.  
  79. # That's great, and all, but how would we get there?
  80. help(tdb.getRoute)
  81. route = tdb.getRoute(origin, closest, 15)
  82. if not route:
  83.     print("Shame, couldn't find a route.")
  84. else:
  85.     # Route is a list of Systems. Turn it into a list of
  86.     # System names...
  87.     routeNames = [ system.name() for system, distance in route ]
  88.     print("Route:", routeNames)
  89.  
  90. # Other interesting parts of tradedb:
  91. # tradeenv.TradeEnv for putting together command parameters,
  92. # tradecalc.TradeCalc for trading calculations,
  93. # formatting,
  94. # transfers, for upload/download
  95.  
  96. # See also: help(tradedb.TradeDB)
Advertisement
Add Comment
Please, Sign In to add comment