def move_to_avg(objs, orientation): """ Move `objs` vertically/horizontally to their average x/y position. """ if orientation not in ("h", "v"): raise ValueError("Orientation must be 'v' or 'h'") if len(objs) == 0: return [] move_axis = "v" if orientation == "h" else "h" attr = "top" if orientation == "h" else "x0" values = list(map(itemgetter(attr), objs)) q = pow(10, utils.decimalize(values[0]).as_tuple().exponent) avg = utils.decimalize(float(sum(values) / len(values)), q) new_objs = [ utils.move_object(obj, move_axis, avg - obj[attr]) for obj in objs ] return new_objs
def test_move_object(self): a = { "x0": 5, "x1": 10, "top": 20, "bottom": 30, "width": 5, "height": 10, "doctop": 120, "y0": 40, "y1": 50, } b = dict(a) b["x0"] = 15 b["x1"] = 20 a_new = utils.move_object(a, "h", 10) assert a_new == b