-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
79 lines (68 loc) · 2.89 KB
/
Copy pathbasic.py
File metadata and controls
79 lines (68 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Pack through the adapter, whichever backend happens to be installed.
Run it:
python3 examples/basic.py
This package is a thin selector, not an engine. It tries the compiled Rust wheel
(`packvium-native-rust`) first and uses the pure-Python `packvium` package when that
wheel is not installed for your platform. Both answer the same shared JSON contract, so
your code does not branch on which one is present -- that is the whole point of the
adapter.
`backend()` tells you which one answered, which is worth logging once at startup: it is
the difference between "we are running the compiled engine" and "we quietly fell back",
and you want to find that out from a log line rather than from a latency graph.
"""
from packvium_native import __version__, backend, pack
print(f"adapter {__version__} using the {backend()} backend\n")
request = {
"items": [
# Lengths and weights are strings on purpose. They are parsed into exact
# integers, so "0.1" means a tenth of a millimetre and never
# 0.09999999999999999. Plain integers and fractions like "3/16" work too.
{
"id": "mug",
"quantity": 6,
"dimensions": {"length": "120", "width": "120", "height": "100"},
"weight": "400 g",
},
{
"id": "plate",
"quantity": 8,
"dimensions": {"length": "260", "width": "260", "height": "20"},
"weight": "600 g",
},
# Too long for the box in every orientation, so it cannot be placed.
{
"id": "ladder",
"quantity": 1,
"dimensions": {"length": "1800", "width": "300", "height": "100"},
"weight": "6 kg",
},
],
"containers": [
{
"id": "box",
"inner_dimensions": {"length": "400", "width": "400", "height": "400"},
"max_payload": "15 kg",
"cost_minor": 180,
}
],
}
result = pack(request)
print(f"status: {result['status']}")
print(f"containers opened: {len(result['containers'])}")
for index, container in enumerate(result["containers"], start=1):
placements = container["placements"]
print(f"\nbox #{index}: {len(placements)} placement(s)")
for placement in placements:
# Every measurement arrives as {"ticks", "value", "unit"}: `ticks` is the exact
# integer the engine reasoned about, `value` is that number written for a human.
position = placement["position"]
print(
f" {placement['item_type']:8s} at "
f"({position['x']['value']}, {position['y']['value']}, {position['z']['value']}) "
f"{position['x']['unit']} orientation {placement['orientation']}"
)
# A refusal is an answer, not an error.
if result["unpacked_items"]:
print("\nnot packed:")
for unpacked in result["unpacked_items"]:
print(f" {unpacked['item_id']:10s} {unpacked['reason']}")