Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # TradeDangerous: https://bitbucket.org/kfsone/tradedangerous/
- # Open up a Python 3 interactive prompt either by starting an
- # instance of the 'python' interpreter (typing 'python' or
- # 'python3' at a prompt), loading the 'IDLE' Python gui
- # (windows users: hit start and type idle), or by running the
- # 'ipython' interactive python interpreter.
- #
- # Possibly the best way is:
- # ipython qtconsole
- # If you don't have ipython, try:
- # pip install ipython
- # or
- # pip3 install ipython
- #
- # Once you have a prompt, type or copy and paste from the following
- # code.
- # "import" command loads a module, either from the standard
- # library or the current directory (or the 'python path').
- # The main module for TradeDangerous is 'tradedb'.
- import tradedb
- # You can now reference things in the module by modulename.thingname
- # The cornerstone of TD is the TradeDB object, this is one way to
- # create one, and call it 'tdb'.
- tdb = tradedb.TradeDB()
- # Look up a particular system
- help(tdb.lookupSystem)
- sol = tdb.lookupSystem("sol")
- # Print some of its information
- print("Sol is at {}, {}, {}.".format(sol.posX, sol.posY, sol.posZ))
- # Query some of it's information in interactive mode:
- sol.stations
- # How did I know it has posX, posY and posZ, or stations?
- help(sol)
- # (this actually gives me help from the type-of-thing that sol is)
- # Look up a station
- abe = tdb.lookupStation("Abraham Lincoln")
- # go on ... find out more... use help
- # Look up a station in a particular system
- abe = tdb.lookupStation("Abraham Lincoln", sol)
- # Look up a system or station using the flexible naming mechanism
- phoenix = tdb.lookupPlace("ascendingp")
- type(phoenix) # is of type tradedb.Station
- sol = tdb.lookupPlace("@sol")
- type(sol)
- abe = tdb.lookupPlace("sol/hamlinc")
- # Pick 5 systems at random:
- # tdb has a lookup table for <systemID> to <System>,
- # implemented using a Python dictionary. So
- # tdb.systemByID.keys() => list of the IDs,
- # tdb.systemByID.values() => list of the Systems,
- # tdb.systemByID.items() => list of (ID, System)
- import random
- help(random.sample)
- systemTable = list(tdb.systemByID.values())
- visitMe = random.sample(systemTable, 5)
- # Find the closest:
- # Call distanceTo(origin) on every member of visitMe and
- # then retrieve the one with the lowest distance.
- origin = tdb.lookupPlace("Eranin")
- closest = min(visitMe, key=lambda candidate: candidate.distanceTo(origin))
- print("{start} -> {dest}: {dist:.2f} ly".format(
- start=origin.name(), dest=closest.name(),
- dist=origin.distanceTo(closest),
- ))
- # That's great, and all, but how would we get there?
- help(tdb.getRoute)
- route = tdb.getRoute(origin, closest, 15)
- if not route:
- print("Shame, couldn't find a route.")
- else:
- # Route is a list of Systems. Turn it into a list of
- # System names...
- routeNames = [ system.name() for system, distance in route ]
- print("Route:", routeNames)
- # Other interesting parts of tradedb:
- # tradeenv.TradeEnv for putting together command parameters,
- # tradecalc.TradeCalc for trading calculations,
- # formatting,
- # transfers, for upload/download
- # See also: help(tradedb.TradeDB)
Advertisement
Add Comment
Please, Sign In to add comment