Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,18 @@
"loops"
]
},
{
"slug": "paasio",
"uuid": "ec36f9ed-f376-43d9-90ee-be0bea0cfc98",
"core": false,
"unlocked_by": "book-store",
"difficulty": 5,
"topics": [
"classes",
"interfaces",
"networking"
]
},
{
"slug": "accumulate",
"uuid": "e7351e8e-d3ff-4621-b818-cd55cf05bffd",
Expand Down
60 changes: 60 additions & 0 deletions exercises/paasio/README.md
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.
102 changes: 102 additions & 0 deletions exercises/paasio/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import io


class MeteredFile(io.BufferedRandom):
"""Implement using a subclassing model."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._read_bytes = 0
self._read_ops = 0
self._write_bytes = 0
self._write_ops = 0

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
return super().__exit__(exc_type, exc_val, exc_tb)

def __iter__(self):
return self

def __next__(self):
self._read_ops += 1
data = super().readline()
self._read_bytes += len(data)
if data:
return data
raise StopIteration

def read(self, size=-1):
self._read_ops += 1
data = super().read(size)
self._read_bytes += len(data)
return data

@property
def read_bytes(self):
return self._read_bytes

@property
def read_ops(self):
return self._read_ops

def write(self, b):
self._write_ops += 1
length = super().write(b)
self._write_bytes += length
return length

@property
def write_bytes(self):
return self._write_bytes

@property
def write_ops(self):
return self._write_ops


class MeteredSocket:
"""Implement using a delegation model."""

def __init__(self, socket):
self._socket = socket
self._recv_bytes = 0
self._recv_ops = 0
self._send_bytes = 0
self._send_ops = 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, bufsize, flags=0):
self._recv_ops += 1
data = self._socket.recv(bufsize, flags)
self._recv_bytes += len(data)
return data

@property
def recv_bytes(self):
return self._recv_bytes

@property
def recv_ops(self):
return self._recv_ops

def send(self, data, flags=0):
self._send_ops += 1
length = self._socket.send(data, flags)
self._send_bytes += length
return length

@property
def send_bytes(self):
return self._send_bytes

@property
def send_ops(self):
return self._send_ops
77 changes: 77 additions & 0 deletions exercises/paasio/paasio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import io


class MeteredFile(io.BufferedRandom):
"""Implement using a subclassing model."""

def __init__(self, *args, **kwargs):
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_bytes(self):
pass

@property
def read_ops(self):
pass

def write(self, b):
pass

@property
def write_bytes(self):
pass

@property
def write_ops(self):
pass


class MeteredSocket:
"""Implement using a delegation model."""

def __init__(self, socket):
pass

def __enter__(self):
pass

def __exit__(self, exc_type, exc_val, exc_tb):
pass

def recv(self, bufsize, flags=0):
pass

@property
def recv_bytes(self):
pass

@property
def recv_ops(self):
pass

def send(self, data, flags=0):
pass

@property
def send_bytes(self):
pass

@property
def send_ops(self):
pass
Loading