~ubuntu-branches/debian/sid/pyro/sid

« back to all changes in this revision

Viewing changes to examples/Bank2/client.py

  • Committer: Bazaar Package Importer
  • Author(s): Carl Chenet, Carl Chenet, Jakub Wilk
  • Date: 2010-09-14 01:04:28 UTC
  • Revision ID: james.westby@ubuntu.com-20100914010428-02r7p1rzr7jvw94z
Tags: 1:3.9.1-2
[Carl Chenet]
* revert to 3.9.1-1 package because of the development status 
  of the 4.1 package is unsuitable for stable use
  DPMT svn #8557 revision (Closes: #589172) 
* added debian/source
* added debian/source/format
* package is now 3.0 (quilt) source format
* debian/control
  - Bump Standards-Version to 3.9.1

[Jakub Wilk]
* Add ‘XS-Python-Version: >= 2.5’ to prevent bytecompilation with python2.4
  (closes: #589053).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
#
 
4
#    Bank client.
 
5
#
 
6
#    The client searches the two banks and performs a set of operations.
 
7
#    (the banks are searched simply by listing the :banks namespace!)
 
8
#
 
9
 
 
10
import sys
 
11
import Pyro.naming, Pyro.core
 
12
 
 
13
from banks import BankError
 
14
 
 
15
group = ':banks'  # the default namespace group
 
16
 
 
17
# initialize the client and set the default namespace group
 
18
Pyro.core.initClient()
 
19
Pyro.config.PYRO_NS_DEFAULTGROUP=group
 
20
 
 
21
# locate the NS
 
22
locator = Pyro.naming.NameServerLocator()
 
23
print 'Searching Naming Service...',
 
24
ns = locator.getNS()
 
25
 
 
26
print 'Naming Service found at',ns.URI.address,'('+(Pyro.protocol.getHostname(ns.URI.address) or '??')+') port',ns.URI.port
 
27
 
 
28
# List the banks.
 
29
# This is done by simply looking in the :banks namespace, to see what
 
30
# banks have registered. The filter is for removing any groups that could
 
31
# be in the namespace (the type of real names is 1).
 
32
banknames = filter(lambda x: x[1]==1, ns.list(group))
 
33
banknames = map(lambda (x,y): x, banknames) # keep only the object name
 
34
if not banknames:
 
35
    raise RuntimeError('There are no banks to do business with!')
 
36
 
 
37
banks={}    # banks (proxies)
 
38
 
 
39
 
 
40
print
 
41
for name in banknames:
 
42
    print 'Found a bank: ',name
 
43
    try:
 
44
        URI=ns.resolve(name)
 
45
    except Pyro.core.PyroError,x:
 
46
        print 'Bank can\'t be found:',x
 
47
        raise SystemExit
 
48
 
 
49
    # create a proxy for the bank object
 
50
    banks[name] = Pyro.core.getAttrProxyForURI(URI)
 
51
 
 
52
def selectBank():
 
53
    i = 1
 
54
    banknames=banks.keys()
 
55
    for b in banknames:
 
56
        print i," ",b
 
57
        i=i+1
 
58
    b = input("Select a bank: ")
 
59
    return banks[banknames[b-1]]
 
60
    
 
61
def createAccount():
 
62
    print "\nCreate Account."
 
63
    bank = selectBank()
 
64
    name = raw_input("Enter name: ")
 
65
    a = bank.createAccount(name)
 
66
    amount = input("Initial deposit: ")
 
67
    a.deposit(amount)
 
68
    print "Balance:", a.balance
 
69
    
 
70
 
 
71
def removeAccount():
 
72
    print "\nRemove Account."
 
73
    bank = selectBank()
 
74
    name = raw_input("Enter name: ")
 
75
    bank.deleteAccount(name)
 
76
 
 
77
def viewBalance():
 
78
    print "\nView Balance."
 
79
    bank = selectBank()
 
80
    name = raw_input("Enter name: ")
 
81
    ac = bank.findAccount(name)
 
82
    print ac.balance
 
83
 
 
84
def deposit():
 
85
    print "\nDeposit."
 
86
    bank = selectBank()
 
87
    name = raw_input("Enter name: ")
 
88
    ac = bank.findAccount(name)
 
89
    amount = input("Amount: ")
 
90
    ac.deposit(amount)
 
91
    print "New balance:", ac.balance
 
92
 
 
93
def withdraw():
 
94
    print "\nWithdraw."
 
95
    bank = selectBank()
 
96
    name = raw_input("Enter name: ")
 
97
    ac = bank.findAccount(name)
 
98
    amount = input("Amount: ")
 
99
    ac.withdraw(amount)
 
100
    print "New balance:", ac.balance
 
101
 
 
102
def listAll():
 
103
    print "\nList all accounts."
 
104
    for (bankname,bank) in banks.items():
 
105
        print bank.name
 
106
        accs = bank.allAccounts()
 
107
        if not accs:
 
108
            print "   No accounts."
 
109
        for a in accs:
 
110
            print "  ",a.name, a.balance
 
111
 
 
112
going = 1
 
113
 
 
114
while going:
 
115
    print "\n---- menu ----"
 
116
    print "1: create account"
 
117
    print "2: remove account"
 
118
    print "3: view balance"
 
119
    print "4: list all accounts"
 
120
    print "5: deposit money"
 
121
    print "6: withdraw money"
 
122
    print "0: exit"
 
123
    print
 
124
    try:
 
125
        choice = input("Choice: ")
 
126
        if choice==0:    going=0
 
127
        elif choice==1:    createAccount()
 
128
        elif choice==2:    removeAccount()
 
129
        elif choice==3:    viewBalance()
 
130
        elif choice==4:    listAll()
 
131
        elif choice==5:    deposit()
 
132
        elif choice==6:    withdraw()
 
133
    except SyntaxError,x:
 
134
        print "Input problem:",x
 
135
    except BankError,x:
 
136
        print 'Problem:',x
 
137
    except StandardError,x:
 
138
        print 'Try again (input incorrect?)'
 
139
        raise
 
140
 
 
141