banks.py :  » Development » Pyro » Pyro-3.10 » examples » Bank2 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Pyro 
Pyro » Pyro 3.10 » examples » Bank2 » banks.py
# Bank and accounts code.
# NOTE that the Account and Bank classes are directly derived
# from the Pyro.core.ObjBase base class. This is required to
# support the 1.2+ attribute access feature.
# Note that an account object is not returned as-is, 
# rather, a PROXY for it is returned.
# Also note that the proxy must support attribute access, so
# we use getAttrProxy().

import Pyro.core

# the bank uses this exception to say there's something wrong:
class BankError(Exception): pass

# Unrestricted account.
class Account(Pyro.core.ObjBase):
  def __init__(self,name,owner):
    Pyro.core.ObjBase.__init__(self)
    self.balance=0.0
    self.name=name
    self.bank=owner
  def _gotReaped(self):
    print 'Account reaped, sorry your cash is lost:',self.name,self.balance 
    self.bank._gotReaped(self)
  def withdraw(self, amount):
    self.balance-=amount
  def deposit(self,amount):
    self.balance+=amount

# Restricted withdrawal account.
class RestrictedAccount(Account):
  def withdraw(s, amount):
    if amount<=s.balance:
      s.balance=s.balance-amount
    else:
      raise BankError('insufficent balance')

# Abstract bank.
class Bank(Pyro.core.ObjBase):
  def __init__(s):
    Pyro.core.ObjBase.__init__(s)
    s.accounts={}
  def createAccount(s, name):
    pass # must override this!
  def _gotReaped(self, account):
    del self.accounts[account.name]
    print 'Bank removed reaped account',account.name
  def deleteAccount(s, name):
    try:
      # find account, disconnect from daemon, delete it
      acc = s.accounts[name]
      s.getDaemon().disconnect(acc)
      del s.accounts[name]
    except KeyError:
      raise BankError('unknown account')
  def findAccount(s, name):
    try:
      # find account, return proxy for it
      return s.accounts[name].getAttrProxy()
    except KeyError:
      raise BankError('unknown account')

  def allAccounts(s):
    # list all accounts, return list of proxies for them
    accs = s.accounts.values()
    proxies = []
    for a in accs:
      proxies.append(a.getAttrProxy())
    return proxies


# Special bank: Rabobank. It has unrestricted accounts.
class Rabobank(Bank):
  def __init__(s):
    Bank.__init__(s)
    s.name = 'Rabobank'
  def createAccount(s,name):
    if s.accounts.has_key(name):
      raise BankError('Account already exists')
    # create account object, connect to daemon, return proxy for it
    acc = Account(name,s)
    acc_URI = s.getDaemon().connect(acc)
    s.accounts[name]=acc
    print 'created account',name
    return acc.getAttrProxy()


# Special bank: VSB. It has restricted accounts.
class VSB(Bank):
  def __init__(s):
    Bank.__init__(s)
    s.name = 'VSB bank'
  def createAccount(s,name):
    if s.accounts.has_key(name):
      raise BankError('Account already exists')
    # create account object, connect to daemon, return proxy for it
    acc = RestrictedAccount(name,s)
    acc_URI = s.getDaemon().connect(acc)
    s.accounts[name]=acc
    print 'created account',name
    return acc.getAttrProxy()

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.