class ThreadSafeSavingsAccount:
    """This class represents a thread-safe savings account
    with the owner's name, PIN, and balance."""
    def __init__(self, name, pin, balance=0.0):
        """Wrap a new account in a shared cell for thread-safety."""
        account = SavingsAccount(name, pin, balance)
        self.cell = SharedCell(account)

    def __str__(self):
        """Returns the string rep of the account."""
        return self.cell.read(lambda account: str(account))

    def getBalance(self):
        """Returns the current balance."""
        return self.cell.read(lambda account: account.getBalance())

    def getName(self):
        """Returns the current name."""
        return self.cell.read(lambda account: account.getName())

    def getPin(self):
        """Returns the current pin."""
        return self.cell.read(lambda account: account.getPin())

    def deposit(self, amount):
        """If the amount is valid, adds it
        to the balance and returns None;
        otherwise, returns an error message."""
        return self.cell.write(lambda account: account.deposit(amount))
class ThreadSafeTranscript(object):
    """This class represents a thread-safe transcript."""
    def __init__(self):
        """Wrap a new transcript in a shared cell for
        thread-safety."""
        transcript = Transcript()
        self.cell = SharedCell(transcript)

    def __str__(self):
        """Returns the string rep of the transcript."""
        return self.cell.read(lambda transcript: str(transcript))

    def add(self, message):
        """Adds message to transcript."""
        return self.cell.write(lambda transcript: transcript.add(message))
class ThreadTranscript:
    def __init__(self, address, chats):
        who = Transcript(address, chats)
        self.cell = SharedCell(who)

    def __str__(self):
        return self.cell.read(lambda who: str(who))

    def getChats(self):
        return self.cell.read(lambda who: who.getChats())

    def getAddress(self):
        return self.cell.read(lambda who: who.getAddress())

    def addChat(self, message):
        return self.cell.write(lambda who: who.addChat(message))
Пример #4
0
class ThreadSafeSavingsAccount:
    """This class represents a thread-safe savings account
    with the owner's name, PIN, and balance."""

    def __init__(self, name, pin, balance = 0.0):
        """Wrap a new account in a shared cell for thread-safety."""
        account = SavingsAccount(name, pin, balance)
        self.cell = SharedCell(account)

    def __str__(self):
        """Returns the string rep of the account."""
        return self.cell.read(lambda account: str(account))

    def getBalance(self):
        """Returns the current balance."""
        return self.cell.read(lambda account: account.getBalance())

    def getName(self):
        """Returns the current name."""
         return self.cell.read(lambda account: account.getName())
def main():
    """Creates a shared cell on a Counter object, and reader and
    writer threads to increment it and observe its value."""
    counter = Counter()
    cell = SharedCell(counter)
    threads = []
    print("Creating reader threads.")
    for i in range(1, 5):
        threads.append(Reader(cell, i))
    print("Creating writer threads.")
    for i in range(1, 3):
        threads.append(Writer(cell, i))
    print("Starting the threads.")
    for thread in threads:
        thread.start()
 def __init__(self, name, pin, balance=0.0):
     """Wrap a new account in a shared cell for
     thread-safety."""
     account = SavingsAccount(name, pin, balance)
     self.cell = SharedCell(account)
Пример #7
0
 def __init__(self):
     """Wrap a new addressbook in a shared cell for
     thread-safety."""
     self.addressbook = Addressbook()
     self.cell = SharedCell(self.addressbook)
     self.iter_current = 0
Пример #8
0
class ThreadSafeAddressbook(object):
    """This class represents a thread-safe addressbook."""
    def __init__(self):
        """Wrap a new addressbook in a shared cell for
        thread-safety."""
        self.addressbook = Addressbook()
        self.cell = SharedCell(self.addressbook)
        self.iter_current = 0

    def __iter__(self):
        """Declares that this class is iterable."""
        return self

    def __next__(self):
        """Facilitates iteration through the addressbook."""
        if self.iter_current >= len(
                self.addressbook.entries):  # Stop condition
            raise StopIteration
        else:
            result = self.addressbook.entries[self.iter_current]
            self.iter_current += 1
            return result

    def iter_reset(self):
        """Keeps track of the current index while iterating through the addressbook."""
        self.iter_current = 0

    def __str__(self):
        """Returns the string representation of the addressbook."""
        try:
            return self.cell.read(lambda addressbook: str(self.addressbook))
        except:
            self.cell.endRead()
            raise

    def set_filename(self, filename):
        """Set the filename of the addressbook for later retrieval if necessary."""
        try:
            return self.cell.write(
                lambda addressbook: self.addressbook.set_filename(filename))
        except:
            print("Error writing to addressbook. (set_filename function)")
            self.cell.endRead()
            raise

    def get_filename(self):
        """Returns the filename of the addressbook .csv that is loaded."""
        try:
            return self.cell.read(
                lambda addressbook: self.addressbook.get_filename())
        except:
            print("Error reading from addressbook. (get_filename function)")
            self.cell.endRead()
            raise

    def add(self, message):
        """Adds entry to addressbook."""
        try:
            return self.cell.write(
                lambda addressbook: self.addressbook.add(message))
        except:
            print("Error writing to addressbook. (add function)")
            self.cell.endWrite()
            raise

    def get_by_index(self, index):
        """Returns the Person at the index specified from a parameter."""
        try:
            return self.cell.read(
                lambda addressbook: self.addressbook.get_by_index(index))
        except:
            print("Error reading from addressbook. (get_by_index function)")
            self.cell.endRead()
            raise

    def get_by_name(self, namere):
        """Searches the addressbook with regexp with a pattern from a parameter."""
        try:
            return self.cell.read(
                lambda addressbook: self.addressbook.get_by_name(namere))
        except:
            print("Error reading from addressbook. (get_by_name function)")
            self.cell.endRead()
            raise

    def update(self, payload):
        """Update an entry in the addressbook."""
        try:
            return self.cell.write(
                lambda addressbook: self.addressbook.update(payload))
        except:
            print("Error writing to addressbook. (updte function)")
            self.cell.endWrite()
            raise

    def delete(self, payload):
        """Delete an entry in the addressbook."""
        try:
            return self.cell.write(
                lambda addressbook: self.addressbook.delete(payload))
        except:
            print("Error writing to addressbook. (delete function)")
            self.cell.endWrite()
            raise
Пример #9
0
 def __init__(self, address, chats):
     who = Transcript(address, chats)
     self.cell = SharedCell(who)
 def __init__(self):
     """Wrap a new transcript in a shared cell for
     thread-safety."""
     transcript = Transcript()
     self.cell = SharedCell(transcript)