From 5aba8c97bde78a4b914c34dc3647b60999167b86 Mon Sep 17 00:00:00 2001 From: mRcfps <1402491442@qq.com> Date: Wed, 14 Feb 2018 17:13:37 +0800 Subject: [PATCH 1/3] Implement exercise bank-account --- config.json | 12 ++ exercises/bank-account/README.md | 21 ++++ exercises/bank-account/bank_account.py | 18 +++ exercises/bank-account/bank_account_test.py | 120 ++++++++++++++++++++ exercises/bank-account/example.py | 35 ++++++ 5 files changed, 206 insertions(+) create mode 100644 exercises/bank-account/README.md create mode 100644 exercises/bank-account/bank_account.py create mode 100644 exercises/bank-account/bank_account_test.py create mode 100644 exercises/bank-account/example.py diff --git a/config.json b/config.json index 12b936eed6..aa909e48cb 100644 --- a/config.json +++ b/config.json @@ -666,6 +666,18 @@ "transforming" ] }, + { + "uuid": "83a3ff95-c043-401c-bc2c-547d52344b02", + "slug": "bank-account", + "core": false, + "unlocked_by": null, + "difficulty": 4, + "topics": [ + "classes", + "concurrency", + "conditionals" + ] + }, { "uuid": "d41238ce-359c-4a9a-81ea-ca5d2c4bb50d", "slug": "twelve-days", diff --git a/exercises/bank-account/README.md b/exercises/bank-account/README.md new file mode 100644 index 0000000000..886035121a --- /dev/null +++ b/exercises/bank-account/README.md @@ -0,0 +1,21 @@ +# Bank Account + +Simulate a bank account supporting opening/closing, withdrawals, and deposits of money. Watch out for concurrent transactions! + +A bank account can be accessed in multiple ways. Clients can make deposits and withdrawals using the internet, mobile phones, etc. Shops can charge against the account. + +Create an account that can be accessed from multiple threads/processes (terminology depends on your programming language). + +It should be possible to close an account; operations against a closed account must fail. + +## Submitting Exercises + +Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. + +For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. + +For more detailed information about running tests, code style and linting, +please see the [help page](http://exercism.io/languages/python). + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. \ No newline at end of file diff --git a/exercises/bank-account/bank_account.py b/exercises/bank-account/bank_account.py new file mode 100644 index 0000000000..fab129e36f --- /dev/null +++ b/exercises/bank-account/bank_account.py @@ -0,0 +1,18 @@ +class BankAccount(object): + def __init__(self): + pass + + def get_balance(self): + pass + + def open(self): + pass + + def deposit(self, amount): + pass + + def withdraw(self, amount): + pass + + def close(self): + pass diff --git a/exercises/bank-account/bank_account_test.py b/exercises/bank-account/bank_account_test.py new file mode 100644 index 0000000000..a82ccbe1ec --- /dev/null +++ b/exercises/bank-account/bank_account_test.py @@ -0,0 +1,120 @@ +import sys +import threading +import time +import unittest + +from bank_account import BankAccount + + +class BankAccountTests(unittest.TestCase): + + def setUp(self): + self.account = BankAccount() + + def test_newly_opened_account_has_zero_balance(self): + self.account.open() + self.assertEqual(self.account.get_balance(), 0) + + def test_can_deposit_money(self): + self.account.open() + self.account.deposit(100) + self.assertEqual(self.account.get_balance(), 100) + + def test_can_deposit_money_sequentially(self): + self.account.open() + self.account.deposit(100) + self.account.deposit(50) + + self.assertEqual(self.account.get_balance(), 150) + + def test_can_withdraw_money(self): + self.account.open() + self.account.deposit(100) + self.account.withdraw(50) + + self.assertEqual(self.account.get_balance(), 50) + + def test_can_withdraw_money_sequentially(self): + self.account.open() + self.account.deposit(100) + self.account.withdraw(20) + self.account.withdraw(80) + + self.assertEqual(self.account.get_balance(), 0) + + def test_checking_balance_of_closed_account_throws_error(self): + self.account.open() + self.account.close() + + with self.assertRaises(ValueError): + self.account.get_balance() + + def test_deposit_into_closed_account(self): + self.account.open() + self.account.close() + + with self.assertRaises(ValueError): + self.account.deposit(50) + + def test_withdraw_from_closed_account(self): + self.account.open() + self.account.close() + + with self.assertRaises(ValueError): + self.account.withdraw(50) + + def test_cannot_withdraw_more_than_deposited(self): + self.account.open() + self.account.deposit(25) + + with self.assertRaises(ValueError): + self.account.withdraw(50) + + def test_cannot_withdraw_negative(self): + self.account.open() + self.account.deposit(100) + + with self.assertRaises(ValueError): + self.account.withdraw(-50) + + def test_cannot_deposit_negative(self): + self.account.open() + + with self.assertRaises(ValueError): + self.account.deposit(-50) + + def test_can_handle_concurrent_transactions(self): + self.account.open() + self.account.deposit(1000) + + for _ in range(10): + self.adjust_balance_concurrently() + + def adjust_balance_concurrently(self): + def transact(): + self.account.deposit(5) + time.sleep(0.001) + self.account.withdraw(5) + + # Greatly improve the chance of an operation being interuppted + # by thread switch, thus testing synchronization effectively + try: + sys.setswitchinterval(1e-12) + except AttributeError: + # Python 2 compatible + sys.setcheckinterval(1) + + threads = [] + for _ in range(1000): + t = threading.Thread(target=transact) + threads.append(t) + t.start() + + for thread in threads: + thread.join() + + self.assertEqual(self.account.get_balance(), 1000) + + +if __name__ == '__main__': + unittest.main() diff --git a/exercises/bank-account/example.py b/exercises/bank-account/example.py new file mode 100644 index 0000000000..55b6f9ff8d --- /dev/null +++ b/exercises/bank-account/example.py @@ -0,0 +1,35 @@ +import threading + + +class BankAccount(object): + def __init__(self): + self.is_open = False + self.balance = 0 + self.lock = threading.Lock() + + def get_balance(self): + with self.lock: + if self.is_open: + return self.balance + else: + raise ValueError + + def open(self): + self.is_open = True + + def deposit(self, amount): + with self.lock: + if self.is_open and amount > 0: + self.balance += amount + else: + raise ValueError + + def withdraw(self, amount): + with self.lock: + if self.is_open and 0 < amount <= self.balance: + self.balance -= amount + else: + raise ValueError + + def close(self): + self.is_open = False From 5aab3777d0fa24f258ff2dc0b0b381cfaa5b6228 Mon Sep 17 00:00:00 2001 From: mRcfps <1402491442@qq.com> Date: Thu, 15 Feb 2018 12:32:35 +0800 Subject: [PATCH 2/3] bank-account: generate README using configlet --- exercises/bank-account/README.md | 46 +++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/exercises/bank-account/README.md b/exercises/bank-account/README.md index 886035121a..552c2707e4 100644 --- a/exercises/bank-account/README.md +++ b/exercises/bank-account/README.md @@ -1,21 +1,53 @@ # Bank Account -Simulate a bank account supporting opening/closing, withdrawals, and deposits of money. Watch out for concurrent transactions! +Simulate a bank account supporting opening/closing, withdrawals, and deposits +of money. Watch out for concurrent transactions! -A bank account can be accessed in multiple ways. Clients can make deposits and withdrawals using the internet, mobile phones, etc. Shops can charge against the account. +A bank account can be accessed in multiple ways. Clients can make +deposits and withdrawals using the internet, mobile phones, etc. Shops +can charge against the account. -Create an account that can be accessed from multiple threads/processes (terminology depends on your programming language). +Create an account that can be accessed from multiple threads/processes +(terminology depends on your programming language). -It should be possible to close an account; operations against a closed account must fail. +It should be possible to close an account; operations against a closed +account must fail. + +## Instructions + +Run the test file, and fix each of the errors in turn. When you get the +first test to pass, go to the first pending or skipped test, and make +that pass as well. When all of the tests are passing, feel free to +submit. + +Remember that passing code is just the first step. The goal is to work +towards a solution that is as readable and expressive as you can make +it. + +Have fun! + +## Exception messages + +Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to +indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include +a message. + +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of +`raise Exception`, you shold write: + +```python +raise Exception("Meaningful message indicating the source of the error") +``` ## Submitting Exercises -Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. +Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/bank-account` directory. -For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. +You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`. For more detailed information about running tests, code style and linting, please see the [help page](http://exercism.io/languages/python). ## Submitting Incomplete Solutions -It's possible to submit an incomplete solution so you can see how others have completed the exercise. \ No newline at end of file +It's possible to submit an incomplete solution so you can see how others have completed the exercise. From fea46c277594276421f2f3f69eba141a101049a8 Mon Sep 17 00:00:00 2001 From: mRcfps <1402491442@qq.com> Date: Thu, 15 Feb 2018 17:44:55 +0800 Subject: [PATCH 3/3] bank-account: fix typo and comments --- exercises/bank-account/bank_account_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/bank-account/bank_account_test.py b/exercises/bank-account/bank_account_test.py index a82ccbe1ec..8879c76b18 100644 --- a/exercises/bank-account/bank_account_test.py +++ b/exercises/bank-account/bank_account_test.py @@ -96,12 +96,12 @@ def transact(): time.sleep(0.001) self.account.withdraw(5) - # Greatly improve the chance of an operation being interuppted + # Greatly improve the chance of an operation being interrupted # by thread switch, thus testing synchronization effectively try: sys.setswitchinterval(1e-12) except AttributeError: - # Python 2 compatible + # For Python 2 compatibility sys.setcheckinterval(1) threads = []