-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5a094e
commit 754113d
Showing
4 changed files
with
625 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# PaaS I/O | ||
|
||
Report network IO statistics. | ||
|
||
You are writing a [PaaS][], and you need a way to bill customers based | ||
on network and filesystem usage. | ||
|
||
Create a wrapper for network connections and files that can report IO | ||
statistics. The wrapper must report: | ||
|
||
- The total number of bytes read/written. | ||
- The total number of read/write operations. | ||
|
||
[PaaS]: http://en.wikipedia.org/wiki/Platform_as_a_service | ||
|
||
## 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 should write: | ||
|
||
```python | ||
raise Exception("Meaningful message indicating the source of the error") | ||
``` | ||
|
||
## Running the tests | ||
|
||
To run the tests, run `pytest paasio_test.py` | ||
|
||
Alternatively, you can tell Python to run the pytest module: | ||
`python -m pytest paasio_test.py` | ||
|
||
### Common `pytest` options | ||
|
||
- `-v` : enable verbose output | ||
- `-x` : stop running tests on first failure | ||
- `--ff` : run failures from previous test before running other test cases | ||
|
||
For other options, see `python -m pytest -h` | ||
|
||
## Submitting Exercises | ||
|
||
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/paasio` directory. | ||
|
||
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 [Running the Tests](http://exercism.io/tracks/python/tests). | ||
|
||
## Source | ||
|
||
Brian Matsuo [https://github.com/bmatsuo](https://github.com/bmatsuo) | ||
|
||
## Submitting Incomplete Solutions | ||
|
||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
class MeteredFile: | ||
def __init__(self, file): | ||
self._file = file | ||
self._read_bytes = 0 | ||
self._read_count = 0 | ||
self._write_bytes = 0 | ||
self._write_count = 0 | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
return self._file.__exit__(exc_type, exc_val, exc_tb) | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
self._read_count += 1 | ||
data = self._file.readline() | ||
self._read_bytes += len(data) | ||
if len(data) > 0: | ||
return data | ||
raise StopIteration | ||
|
||
def read(self, size=None): | ||
self._read_count += 1 | ||
if size is None: | ||
data = self._file.read() | ||
else: | ||
data = self._file.read(size) | ||
self._read_bytes += len(data) | ||
return data | ||
|
||
@property | ||
def read_stats(self): | ||
return (self._read_count, self._read_bytes) | ||
|
||
def write(self, data): | ||
self._write_count += 1 | ||
length = self._file.write(data) | ||
self._write_bytes += length | ||
return length | ||
|
||
@property | ||
def write_stats(self): | ||
return (self._write_count, self._write_bytes) | ||
|
||
|
||
class MeteredSocket: | ||
def __init__(self, socket): | ||
self._socket = socket | ||
self._recv_bytes = 0 | ||
self._recv_count = 0 | ||
self._send_bytes = 0 | ||
self._send_count = 0 | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
return self._socket.__exit__(exc_type, exc_val, exc_tb) | ||
|
||
def recv(self, size, flags=None): | ||
self._recv_count += 1 | ||
if flags is None: | ||
data = self._socket.recv(size) | ||
else: | ||
data = self._socket.recv(size, flags) | ||
self._recv_bytes += len(data) | ||
return data | ||
|
||
@property | ||
def recv_stats(self): | ||
return (self._recv_count, self._recv_bytes) | ||
|
||
def send(self, data, flags=None): | ||
self._send_count += 1 | ||
if flags is None: | ||
length = self._socket.send(data) | ||
else: | ||
length = self._socket.send(data, flags) | ||
self._send_bytes += length | ||
return length | ||
|
||
@property | ||
def send_stats(self): | ||
return (self._send_count, self._send_bytes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class MeteredFile: | ||
def __init__(self, file): | ||
pass | ||
|
||
def __enter__(self): | ||
pass | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
pass | ||
|
||
def __iter__(self): | ||
pass | ||
|
||
def __next__(self): | ||
pass | ||
|
||
def read(self, size=-1): | ||
pass | ||
|
||
@property | ||
def read_stats(self): | ||
pass | ||
|
||
def write(self, data): | ||
pass | ||
|
||
@property | ||
def write_stats(self): | ||
pass | ||
|
||
|
||
class MeteredSocket: | ||
def __init__(self, socket): | ||
pass | ||
|
||
def __enter__(self): | ||
pass | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
pass | ||
|
||
def recv(self, size, flags=0): | ||
pass | ||
|
||
@property | ||
def recv_stats(self): | ||
pass | ||
|
||
def send(self, data, flags=0): | ||
pass | ||
|
||
@property | ||
def send_stats(self): | ||
pass |
Oops, something went wrong.