1from __future__ import print_function
2import random
3
4import Pyro4
5
6
7shop = Pyro4.Proxy("PYRONAME:example.shop")
8
9print("Simulating some customers.")
10harrysCart = shop.enter("Harry")
11sallysCart = shop.enter("Sally")
12shoplifterCart = shop.enter("shoplifter")
13# harry buys 4 things and sally 5, shoplifter takes 3 items
14# note that we put the item directly in the shopping cart.
15goods = list(shop.goods().keys())
16for i in range(4):
17    item = random.choice(goods)
18    print("Harry buys %s" % item)
19    harrysCart.purchase(item)
20for i in range(5):
21    item = random.choice(goods)
22    print("Sally buys %s" % item)
23    sallysCart.purchase(item)
24for i in range(3):
25    item = random.choice(goods)
26    print("Shoplifter takes %s" % item)
27    shoplifterCart.purchase(item)
28
29print("Customers currently in the shop: %s" % shop.customers())
30
31# Go to the counter to pay and get a receipt.
32# The shopping cart is still 'inside the shop' (=on the server)
33# so it knows what is in there for every customer in the store.
34# Harry pays by just telling his name (and the shop  looks up
35# harry's shoppingcart).
36# Sally just hands in her shopping cart directly.
37# The shoplifter tries to leave without paying.
38
39try:
40    receipt = shop.payByName("Harry")
41except:
42    print("ERROR: %s" % ("".join(Pyro4.util.getPyroTraceback())))
43print("Harry payed. The cart now contains: %s (should be empty)" % harrysCart.getContents())
44print("Harry got this receipt:")
45print(receipt)
46receipt = shop.payCart(sallysCart)
47print("Sally payed. The cart now contains: %s (should be empty)" % sallysCart.getContents())
48print("Sally got this receipt:")
49print(receipt)
50print("Harry is leaving.")
51shop.leave("Harry")
52print("Sally is leaving.")
53shop.leave("Sally")
54print("Shoplifter is leaving. (should be impossible i.e. give an error)")
55try:
56    shop.leave("shoplifter")
57except:
58    print("".join(Pyro4.util.getPyroTraceback()))
59
60print("Harry is attempting to put stuff back in his cart again,")
61print("which should fail because the cart does no longer exist.")
62harrysCart.purchase("crap")
63