-
Notifications
You must be signed in to change notification settings - Fork 46
Python Quick Peek
- Getting Started
- Importing Trade Dangerous
- Creating a TradeORM
- Looking Things Up
- Disambiguation
- Exploring Objects
- Running a command from Python
- Other Useful Parts of TD
- Want To Go Further
Trade Dangerous is primarily an application, but it can also be imported from Python.
This page is a quick peek for people who want to poke around, write small helpers, or understand how the command layer talks to the database. It is not a frozen public API reference; for serious development, read the source and the developer docs in the repository.
Use the package import:
import tradedangerous as tdCommon imports:
from tradedangerous import TradeEnv, TradeORM
from tradedangerous.version import __version__Check the version:
print(__version__)The current database-facing interface is TradeORM.
from tradedangerous import TradeEnv, TradeORM
tdenv = TradeEnv()
tdb = TradeORM(tdenv=tdenv)
try:
print(tdb.tradingStationCount)
finally:
tdb.close(final=True)TradeORM opens the configured Trade Dangerous database and provides the resolver/query interface used by the command layer. It does not preload the galaxy into an old-style in-memory database object.
A lot of useful TD work starts with lookups.
sol = tdb.lookup_system("sol")
print(sol.name)
print(sol.pos_x, sol.pos_y, sol.pos_z)abe = tdb.lookup_station("sol/abraham lincoln")
print(abe.dbname())
print(abe.system.name)lookup_place() can return either a System or a Station.
place = tdb.lookup_place("sol")
print(type(place), place.name)
place = tdb.lookup_place("@sol")
print(type(place), place.name)
place = tdb.lookup_place("sol/abraham lincoln")
print(type(place), place.dbname())gold = tdb.lookup_item("gold")
metals = tdb.lookup_category("metals")
asp = tdb.lookup_ship("asp")The resolver uses the same naming syntax as the CLI.
sol = tdb.lookup_place("@sol")abe = tdb.lookup_place("/abraham lincoln")abe = tdb.lookup_place("sol/abraham lincoln")When more than one real system shares a name, TD reports candidates with indexed names such as:
Some System@1
Some System@2
Use the indexed form to choose the one you want.
The important thing to remember is that @ is doing two related but different jobs:
-
@namemeans "this is definitely a system name" -
name@2means "pick the second matching system with this name"
Once you have looked something up, Python gives you a couple of good ways to inspect it.
type(sol)
help(sol)
dir(sol)You can also inspect useful attributes directly:
print(sol.name, sol.pos_x, sol.pos_y, sol.pos_z)
print(abe.name, abe.ls_from_star, abe.max_pad_size)
print(abe.system.name)The objects you get back are SQLAlchemy ORM objects from tradedangerous.db.orm_models.
The command layer parses argv through CommandIndex, then runs the command against a backend selected from the command's declared needs.
Minimal example:
from tradedangerous.commands import CommandIndex
from tradedangerous import TradeORM
argv = ["trade", "market", "sol/abraham lincoln"]
cmdenv = CommandIndex().parse(argv)
tdb = TradeORM(tdenv=cmdenv, require_db=True)
try:
results = cmdenv.run(tdb)
if results:
results.render()
finally:
tdb.close(final=True)For normal user workflows, prefer running trade ... directly. Programmatic command execution is mainly useful for tools that intentionally embed TD.
TradeEnv carries environment/config/debug/detail settings.
TradeORM is the current database and resolver interface.
The route planner lives under tradedangerous.planner. The command layer converts CLI input into a RunRequest; the planner returns a RunResult; renderers turn that result into CLI/GUI output.
Most TD-specific exceptions live under tradedangerous.tradeexcept or the command-layer exception modules.
Read the developer docs in the repository:
docs/Developers/RESOLVER_CONTRACT.mddocs/Developers/ORM_Schema_reference.mddocs/Developers/db_engine_reference.mddocs/Developers/planner-internals.md
And when in doubt, check the current source. It is the real reference for internals.
- Windows Python Install
- Install Trade Dangerous
- Running Trade Dangerous
- Get Market Data
- Upgrading
- Python 3.14 Warning
- Solo Stuff
- Mac/Linux
- Virtual Environment Stuff
- Troubleshooting
- Getting Started
- Conventions
- Using Trade Dangerous
- Obtaining Data
- Shortcuts
- Local Price Data
- Programming
- Starting The GUI
- How The GUI Is Laid Out
- Commander Baseline And Ship Profiles
- Workspaces
- Import
- Results And Diagnostics
- Saved State
- Troubleshooting
- Read This First
- Refresh Your Data The Normal Way
- List Stations In A System
- Find Systems Near A System
- Find Trading Stations Near You
- Plan An In-System Trading Tour
- Plan A Route That Comes Back Home
- Compare Two Stations Directly
- Find Somewhere To Buy An Item
- Find Somewhere To Sell An Item
- Inspect One Station Market
- Find Old Data To Refresh
- Find Rare Goods Near A System