Parsing and utilities API¶
Used throughout the parsed-data showcase.
Column definitions are in the parser data dictionary.
get_vic3_directory
¶
Search common Steam library paths and return the Victoria 3 game directory.
Returns:
-
Path–The absolute path to the
Victoria 3/gamedirectory.
Raises:
-
FileNotFoundError–If the Victoria 3 game directory cannot be found in any of the known Steam library locations.
Source code in src/vic3_analysis/utils.py
localization
¶
Parse the effective Victoria 3 localization catalog.
Values are raw quoted contents: game references, markup, and escape
sequences are not expanded. language is Victoria 3's internal name,
such as "english" or "simp_chinese".
Source code in src/vic3_analysis/parse/localization.py
parse_merge
¶
parse_merge(
path: str | Path, merge_levels: int = 0
) -> Tree
Parse all .txt files in path and merge them into a single Tree.
Parameters:
-
(path¶str | Path) –Directory containing the Paradox script (
.txt) files to parse. -
(merge_levels¶int, default:0) –Number of levels deep to merge nested Trees. Passed directly to
pyradox.Tree.merge. Defaults to0.
Returns:
-
Tree–A
pyradox.Treerepresenting the merged contents of all.txt -
Tree–files found in path (sorted alphabetically).
Source code in src/vic3_analysis/utils.py
buy_packages
¶
buy_packages(
game_dir: str | Path | None = None,
) -> DataFrame
Parse Victoria 3 buy-packages files into a pandas.DataFrame.
Parameters:
-
(game_dir¶str | Path | None, default:None) –Path to the Victoria 3
gamedirectory. IfNonethe directory is located automatically viaget_vic3_directory.
Returns:
-
DataFrame–A
DataFramewith one row per wealth level and columns for -
DataFrame–"wealth","political_strength","total_popneeds", and one -
DataFrame–column per
popneed_*good. Missing consumption values are filled -
DataFrame–with
0.
Source code in src/vic3_analysis/parse/buy_packages.py
BuildingGroupParser
¶
BuildingGroupParser(game_dir: str | Path | None = None)
Bases: Tree
A pyradox.Tree populated with Victoria 3 building-group definitions.
On construction the parser reads all building-group .txt files from the
game's common/building_groups directory. Raw entries can be iterated
via items (inherited from Tree); a flat per-group table is
built by to_dataframe; and a per-group attribute dict with
land_usage and cash_reserves_max inheritance resolved is returned
by resolved_attributes.
Parameters:
-
(game_dir¶str | Path | None, default:None) –Path to the Victoria 3
gamedirectory. IfNonethe directory is located automatically viaget_vic3_directory.
Methods:
-
to_dataframe–Convert the building-groups tree to a flat
pandas.DataFrame. -
resolved_attributes–Return per-group attribute dicts with inherited attrs resolved.
Source code in src/vic3_analysis/parse/building_groups.py
to_dataframe
¶
to_dataframe(*, language: str | None = None) -> DataFrame
Convert the building-groups tree to a flat pandas.DataFrame.
Each row is one building group; scalar attributes are preserved as
columns, list values are concatenated into a +-joined string,
and nested Tree/dict values (such as should_auto_expand
trigger blocks) are omitted. No parent-chain inheritance is applied
here — use resolved_attributes for inherited values.
Parameters:
-
(language¶str | None, default:None) –Optional Victoria 3 internal language name. When provided, adds a nullable
key_localizationcolumn.
Returns:
-
DataFrame–A
DataFramewith one row per building group ("key"column) -
DataFrame–and one column per scalar attribute declared on that group.
Source code in src/vic3_analysis/parse/building_groups.py
resolved_attributes
¶
Return per-group attribute dicts with inherited attrs resolved.
For each building group, a copy of its raw scalar attributes is
returned with the following inherited attributes filled by walking the
parent_group chain when they are unset on the group itself:
land_usage— inherited when missing.cash_reserves_max— inherited when missing or explicitly0(per the vanilla file comment: "If unspecified or set to 0, it will use the value from the parent group").economy_of_scale— inherited when missing because the vanilla rule applies to buildings in a flagged group or any child group.
Non-inherited attributes keep their raw, group-local values (including
None/missing, which are simply absent from the dict). Cycle guards
prevent infinite loops on malformed parent_group loops.
Returns:
-
dict[str, dict[str, Any]]–A dict mapping each building-group key to a dict of its resolved
-
dict[str, dict[str, Any]]–scalar attributes.
Source code in src/vic3_analysis/parse/building_groups.py
BuildingsParser
¶
BuildingsParser(game_dir: str | Path | None = None)
Bases: Tree
A pyradox.Tree populated with Victoria 3 building definitions.
On construction the parser reads all building .txt files from the game
directory, resolves required_construction keys to their numeric point
values using the game's script_values, and stores the resolved value
under the required_construction_points key for each building entry.
Attributes:
-
cost_modifiers(dict[str, int]) –Mapping of construction-cost script-value names (e.g.
"construction_cost_urban") to their integer values, extracted fromcommon/script_values.
Parameters:
-
(game_dir¶str | Path | None, default:None) –Path to the Victoria 3
gamedirectory. IfNonethe directory is located automatically viaget_vic3_directory.
Methods:
-
to_dataframe–Convert the buildings tree to a flat
pandas.DataFrame. -
production_method_groups–Return a mapping of building keys to their production-method-group lists.
-
building_groups–Return a mapping of building group keys to their member building keys.
Source code in src/vic3_analysis/parse/buildings.py
to_dataframe
¶
to_dataframe(*, language: str | None = None) -> DataFrame
Convert the buildings tree to a flat pandas.DataFrame.
Scalar attributes of each building are preserved as columns; nested
Tree and dict values are omitted, and list values are
concatenated into a +-joined string.
In addition, the resolved attributes of each building's
building_group (parsed from common/building_groups) are joined
onto the row. land_usage and cash_reserves_max are resolved
along the parent_group chain; other group attributes are taken
verbatim. Group-attribute columns share the group attribute's name
unless that name collides with an existing building-level column, in
which case the building_group_ prefix is applied.
Parameters:
-
(language¶str | None, default:None) –Optional Victoria 3 internal language name. When provided, adds nullable localization columns for the building and its building group.
Returns:
-
DataFrame–A
DataFramewith one row per building and one column per scalar -
DataFrame–attribute, augmented with the building's resolved group attributes.
Source code in src/vic3_analysis/parse/buildings.py
production_method_groups
¶
Return a mapping of building keys to their production-method-group lists.
Returns:
-
dict[str, list[str]]–A dict where each key is a building identifier and each value is a
-
dict[str, list[str]]–list of production-method-group keys associated with that building.
-
dict[str, list[str]]–Buildings without
production_method_groupsare omitted.
Source code in src/vic3_analysis/parse/buildings.py
building_groups
¶
Return a mapping of building group keys to their member building keys.
Returns:
-
dict[str, list[str]]–A dict where each key is a building group identifier and each value
-
dict[str, list[str]]–is a list of building identifiers that belong to that group.
Source code in src/vic3_analysis/parse/buildings.py
goods
¶
Parse Victoria 3 goods definitions and return them as a DataFrame.
Parameters:
-
(game_dir¶str | Path | None, default:None) –Path to the Victoria 3
gamedirectory. IfNonethe directory is located automatically viaget_vic3_directory. -
(language¶str | None, default:None) –Optional Victoria 3 internal language name. When provided, adds a nullable
key_localizationcolumn.
Returns:
-
DataFrame–A
DataFramewith one row per tradeable good, where the"key" -
DataFrame–column holds the good's identifier and remaining columns represent its
-
DataFrame–attributes (e.g.
"cost").
Raises:
-
ValueError–If any entry in the goods tree is not a
dict.
Source code in src/vic3_analysis/parse/goods.py
PopNeedsParser
¶
PopNeedsParser(game_dir: str | Path | None = None)
Bases: Tree
Parser for Victoria 3 pop need definitions.
Parameters:
-
(game_dir¶str | Path | None, default:None) –Path to the Victoria 3
gamedirectory. IfNone, locate the directory automatically.
Methods:
-
to_dataframe–Convert the pop-needs tree to a flat
pandas.DataFrame.
Source code in src/vic3_analysis/parse/pop_needs.py
to_dataframe
¶
to_dataframe(*, language: str | None = None) -> DataFrame
Convert the pop-needs tree to a flat pandas.DataFrame.
Parameters:
-
(language¶str | None, default:None) –Optional Victoria 3 internal language name. When provided, adds a nullable
key_localizationcolumn.
Returns:
-
DataFrame–A
DataFramewith one row per pop need entry. Columns include -
DataFrame–"key"(the pop need type),"goods","weight", -
DataFrame–"max_supply_share","min_supply_share", and"is_default".
Source code in src/vic3_analysis/parse/pop_needs.py
PopTypesParser
¶
PopTypesParser(game_dir: str | Path | None = None)
Bases: Tree
A pyradox.Tree populated with Victoria 3 pop-type definitions.
On construction the parser reads all pop-type .txt files from the
game's common/pop_types directory and stores the parsed entries.
Parameters:
-
(game_dir¶str | Path | None, default:None) –Path to the Victoria 3
gamedirectory. IfNonethe directory is located automatically viaget_vic3_directory.
Methods:
-
to_dataframe–Convert the pop-types tree to a flat
pandas.DataFrame. -
flags–Return a mapping of boolean-flag names to the pop types that set them.
Source code in src/vic3_analysis/parse/pop_types.py
to_dataframe
¶
to_dataframe(*, language: str | None = None) -> DataFrame
Convert the pop-types tree to a flat pandas.DataFrame.
Scalar attributes of each pop type are preserved as columns; nested
Tree and dict values are omitted, and list values are
concatenated into a +-joined string.
Parameters:
-
(language¶str | None, default:None) –Optional Victoria 3 internal language name. When provided, adds a nullable
key_localizationcolumn.
Returns:
-
DataFrame–A
DataFramewith one row per pop type and one column per scalar -
DataFrame–attribute.
Source code in src/vic3_analysis/parse/pop_types.py
flags
¶
Return a mapping of boolean-flag names to the pop types that set them.
Scans each pop type for boolean attributes whose value is True and
groups pop-type keys by flag name (e.g. "is_slave",
"military", "unemployment").
Returns:
-
dict[str, list[str]]–A dict where each key is a boolean attribute name and each value is
-
dict[str, list[str]]–a list of pop-type identifiers for which that flag is set to
-
dict[str, list[str]]–True.
Source code in src/vic3_analysis/parse/pop_types.py
production_method_groups
¶
production_method_groups(
game_dir: str | Path | None = None,
) -> dict[str, list[str]]
Parse Victoria 3 production-method-group data into a dict.
Parameters:
-
(game_dir¶str | Path | None, default:None) –Path to the Victoria 3
gamedirectory. IfNonethe directory is located automatically viaget_vic3_directory.
Returns:
-
dict[str, list[str]]–A dict mapping each production-method-group key to its ordered list of
-
dict[str, list[str]]–production-method keys.
Source code in src/vic3_analysis/parse/production_method_groups.py
ProductionMethodParser
¶
ProductionMethodParser(game_dir: str | Path | None = None)
Bases: Tree
A pyradox.Tree populated with Victoria 3 production-method definitions.
On construction the parser reads all production-method .txt files from
the game's common/production_methods directory. Raw entries can be
iterated via items (inherited from Tree); per-method employment
(total and broken down by profession) is available via employment;
per-method state modifiers are available via state_modifiers; and
a flat per-configuration table of production-method attributes plus
appended employment, goods-flow and state-modifier columns is built by
to_dataframe.
Parameters:
-
(game_dir¶str | Path | None, default:None) –Path to the Victoria 3
gamedirectory. IfNonethe directory is located automatically viaget_vic3_directory.
Methods:
-
employment–Return per-method employment broken down by profession.
-
state_modifiers–Return per-method state modifiers flattened across scaling blocks.
-
to_dataframe–Build a flat DataFrame of per-configuration production-method stats.
Source code in src/vic3_analysis/parse/production_methods.py
employment
¶
Return per-method employment broken down by profession.
Iterates every production method in the tree and, for each one that
defines building_modifiers, reads its level_scaled modifiers to
compute the total employment and one employment_<profession>
value per profession (e.g. employment_laborers). Methods without
level_scaled employment modifiers still appear with a zero total.
Returns:
-
dict[str, dict[str, Any]]–A dict mapping each production-method key to a dict containing an
-
dict[str, dict[str, Any]]–"employment"value (total across all professions) and one -
dict[str, dict[str, Any]]–"employment_<profession>"value per profession that the method -
dict[str, dict[str, Any]]–employs.
Source code in src/vic3_analysis/parse/production_methods.py
state_modifiers
¶
Return per-method state modifiers flattened across scaling blocks.
Iterates every production method in the tree and, for each one that
defines state_modifiers, flattens its unscaled,
level_scaled and workforce_scaled blocks into a single dict
mapping modifier keys (e.g. state_infrastructure_add) to their
values. The scaling distinction is dropped; vanilla never defines
the same modifier key under two scaling blocks of one method.
Returns:
-
dict[str, dict[str, Any]]–A dict mapping each production-method key that defines
-
dict[str, dict[str, Any]]–state_modifiersto a dict of its modifier keys and values.
Raises:
-
ValueError–If the same modifier key is defined under more than one scaling block of a single production method.
Source code in src/vic3_analysis/parse/production_methods.py
to_dataframe
¶
to_dataframe(
df_goods: DataFrame | None = None,
df_buildings: DataFrame | None = None,
pmg_dict: dict[str, list[str]] | None = None,
*,
language: str | None = None,
) -> DataFrame
Build a flat DataFrame of per-configuration production-method stats.
Combines the parsed production methods with building and
production-method-group data into one row per
(building, production-method-group, production-method) combination.
The production method's scalar attributes are preserved as columns,
list values are concatenated into +-joined strings, and nested
dict/Tree values (such as building_modifiers) are omitted.
Total and per-profession employment and per-good net flows (positive =
output, negative = input) are appended as columns at the end. Goods
columns follow the goods table's row order and are prefixed with
goods_ to distinguish them from attribute columns. State modifiers
(flattened across the state_modifiers scaling blocks, e.g.
state_infrastructure_add) are appended after the goods columns.
Parameters:
-
(df_goods¶DataFrame | None, default:None) –Optional pre-parsed goods table.
-
(df_buildings¶DataFrame | None, default:None) –Optional pre-parsed buildings table.
-
(pmg_dict¶dict[str, list[str]] | None, default:None) –Optional production-method-group mapping.
-
(language¶str | None, default:None) –Optional Victoria 3 internal language name. When provided, adds nullable localization columns for the building, production-method group, and production method.
Returns:
-
DataFrame–A
DataFramewith"building", -
DataFrame–"production_method_group"and"production_method"key -
DataFrame–columns, the production method's scalar/list attributes, and
-
DataFrame–appended
"employment"(total),"employment_<profession>", -
DataFrame–"goods_<good>"and state-modifier (e.g. -
DataFrame–"state_infrastructure_add") columns. Employment, goods and -
DataFrame–state-modifier columns are zero-filled; scalar-attribute columns
-
DataFrame–are left missing (
NaN) when a method does not define them, -
DataFrame–matching
BuildingsParser.to_dataframe.
Raises:
-
ValueError–If a goods modifier string cannot be classified as either an input or an output, if the associated good key cannot be identified, if a state modifier is defined under multiple scaling blocks of one production method, or if a production-method-group referenced by a building is not found.
Source code in src/vic3_analysis/parse/production_methods.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
StateRegionsParser
¶
StateRegionsParser(game_dir: str | Path | None = None)
Bases: Tree
A pyradox.Tree subclass for parsing Victoria 3 state region definitions.
Reads all .txt files from the game's map_data/state_regions directory
and stores the parsed data in a tree structure that mirrors the original
file hierarchy. Provides helper methods for converting to a flat
pandas.DataFrame and for looking up state region attributes & resources.
Parameters:
-
(game_dir¶str | Path | None, default:None) –Path to the Victoria 3
gamedirectory. IfNonethe directory is located automatically viaget_vic3_directory.
Methods:
-
to_dataframe–Convert the state regions tree to a flat
pandas.DataFrame.
Source code in src/vic3_analysis/parse/state_regions.py
to_dataframe
¶
to_dataframe(*, language: str | None = None) -> DataFrame
Convert the state regions tree to a flat pandas.DataFrame.
Scalar attributes of each state region are preserved as columns; nested
Tree, list, and dict values are omitted.
Parameters:
-
(language¶str | None, default:None) –Optional Victoria 3 internal language name. When provided, adds a nullable
key_localizationcolumn.
Returns:
-
DataFrame–A
DataFramewith one row per state region and one column per scalar -
DataFrame–attribute.
Source code in src/vic3_analysis/parse/state_regions.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
state_region_arable_land_limit
¶
state_region_arable_land_limit(
state_regions_df: DataFrame,
state_region_keys: Iterable[str],
) -> float
Return total arable land across selected state regions.
Parameters:
-
(state_regions_df¶DataFrame) –Parsed state-region DataFrame.
-
(state_region_keys¶Iterable[str]) –State-region keys to aggregate. Repeated keys are counted once. Unknown keys are ignored with a warning.
Returns:
-
float–The sum of the selected regions'
arable_landvalues.
Raises:
-
ValueError–If required columns are missing.
Warns:
-
UserWarning–If a requested state-region key is unknown.
Source code in src/vic3_analysis/parse/state_regions.py
state_region_resource_limits
¶
state_region_resource_limits(
state_regions_df: DataFrame,
state_region_keys: Iterable[str],
) -> dict[str, float]
Aggregate state-region resource capacity into building-level limits.
The input is the flattened output of StateRegionsParser.to_dataframe.
Only total-potential resource_* columns are used; discovered and
undiscovered component columns and arable-land data are intentionally
excluded. Every resource building represented by the DataFrame is returned,
including a zero limit when it is absent from the selected regions.
Gold fields are a discoverable precursor to gold mines. For long-run
nominal optimisation their potential is added to building_gold_mine and
building_gold_field is assigned a zero limit.
Parameters:
-
(state_regions_df¶DataFrame) –Parsed state-region DataFrame.
-
(state_region_keys¶Iterable[str]) –State-region keys to aggregate. Repeated keys are counted once. Unknown keys are ignored with a warning.
Returns:
-
dict[str, float]–An insertion-ordered mapping suitable for
-
dict[str, float]–Scenario(building_level_caps=tuple(limits.items())).
Raises:
-
ValueError–If the DataFrame has no
keycolumn.
Warns:
-
UserWarning–If a requested state-region key is unknown.
Source code in src/vic3_analysis/parse/state_regions.py
technology
¶
Parse Victoria 3 technology definitions into a DataFrame.
Reads all .txt files from common/technology/technologies, skipping
keys that are not useful for analysis (modifier, ai_weight,
unlocking_technologies, on_researched), and converts era_N
strings to their integer era numbers.
Parameters:
-
(game_dir¶str | Path | None, default:None) –Path to the Victoria 3
gamedirectory. IfNonethe directory is located automatically viaget_vic3_directory. -
(language¶str | None, default:None) –Optional Victoria 3 internal language name. When provided, adds a nullable
key_localizationcolumn.
Returns:
-
DataFrame–A
DataFramewith one row per technology. Always contains a -
DataFrame–"key"column and an"era"column (integer), plus any -
DataFrame–additional scalar attributes defined in the game files.
Raises:
-
ValueError–If a technology entry contains a nested
Treevalue for an unexpected key, or if the"era"value cannot be parsed.