@@ -242,6 +242,7 @@ def get_tree_diff(
242242 other_tree : node .Node ,
243243 only_diff : bool = True ,
244244 attr_list : List [str ] = [],
245+ fallback_sep : str = "/" ,
245246) -> node .Node :
246247 """Get difference of `tree` to `other_tree`, changes are relative to `tree`.
247248
@@ -254,6 +255,12 @@ def get_tree_diff(
254255 - For example: (+) refers to nodes that are in `other_tree` but not `tree`.
255256 - For example: (-) refers to nodes that are in `tree` but not `other_tree`.
256257
258+ !!! note
259+
260+ - tree and other_tree must have the same `sep` symbol, otherwise this will raise ValueError
261+ - If the `sep` symbol contains one of `+` / `-` / `~` character, a fallback sep will be used
262+ - Node names in tree and other_tree must not contain the `sep` (or fallback sep) symbol
263+
257264 Examples:
258265 >>> # Create original tree
259266 >>> from bigtree import Node, get_tree_diff, list_to_tree
@@ -333,11 +340,25 @@ def get_tree_diff(
333340 other_tree (Node): tree to be compared with
334341 only_diff (bool): indicator to show all nodes or only nodes that are different (+/-), defaults to True
335342 attr_list (List[str]): tree attributes to check for difference, defaults to empty list
343+ fallback_sep (str): sep to fall back to if tree and other_tree has sep that clashes with symbols "+" / "-" / "~".
344+ All node names in tree and other_tree should not contain this fallback_sep, defaults to "/"
336345
337346 Returns:
338347 (Node)
339348 """
340- other_tree .sep = tree .sep
349+ if tree .sep != other_tree .sep :
350+ raise ValueError ("`sep` must be the same for tree and other_tree" )
351+
352+ forbidden_sep_symbols = ["+" , "-" , "~" ]
353+ if any (
354+ forbidden_sep_symbol in tree .sep
355+ for forbidden_sep_symbol in forbidden_sep_symbols
356+ ):
357+ tree = tree .copy ()
358+ other_tree = other_tree .copy ()
359+ tree .sep = fallback_sep
360+ other_tree .sep = fallback_sep
361+
341362 name_col = "name"
342363 path_col = "PATH"
343364 indicator_col = "Exists"
@@ -403,9 +424,11 @@ def get_tree_diff(
403424 (data_both [indicator_col ] != "both" )
404425 | (data_both [path_col ].isin (path_changes_deque ))
405426 ]
406- data_both = data_both [[path_col ]]
427+ data_both = data_both [[path_col ]]. sort_values ( path_col )
407428 if len (data_both ):
408- tree_diff = construct .dataframe_to_tree (data_both , node_type = tree .__class__ )
429+ tree_diff = construct .dataframe_to_tree (
430+ data_both , node_type = tree .__class__ , sep = tree .sep
431+ )
409432 # Handle tree attribute difference
410433 if len (path_changes_deque ):
411434 path_changes_list = sorted (path_changes_deque , reverse = True )
0 commit comments