-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
12 changed files
with
197 additions
and
51 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
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,29 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
import ringer as rg | ||
|
||
|
||
def main(): | ||
logging.basicConfig(level='DEBUG') | ||
|
||
file_path = Path('test.rgr') | ||
ring = rg.RingerList(file_path=file_path, storage=rg.Storage.FILE) | ||
|
||
ring.append("A") | ||
ring.append("A") | ||
ring.append("A") | ||
ring.append("A") | ||
|
||
ring[1] = "B" | ||
ring[2] = "C" | ||
ring[3] = "D" | ||
|
||
print(ring[0]) | ||
print(ring[1]) | ||
print(ring[2]) | ||
print(ring[3]) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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 |
---|---|---|
@@ -1,16 +1,8 @@ | ||
from .version import \ | ||
VERSION, \ | ||
__version__ | ||
|
||
from .exceptions import \ | ||
RingException, \ | ||
EmptyRingException | ||
|
||
from .rings import \ | ||
Ring, \ | ||
RingDirectory, \ | ||
RingFile, \ | ||
RingMemory | ||
|
||
from .manager import \ | ||
Manager | ||
RingerException, \ | ||
EmptyRingerException | ||
from .collections import * | ||
from .storages import * |
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,3 @@ | ||
from .base import Ringer | ||
from .deques import RingerDeque | ||
from .lists import RingerList |
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,14 @@ | ||
from abc import ABC | ||
from typing import Any | ||
from uuid import uuid4 | ||
|
||
from ..storages import Storage | ||
|
||
|
||
class Ringer(ABC): | ||
|
||
def __init__(self, storage: Storage, uuid: Any = None, *args, **kwargs): | ||
if uuid is None: | ||
uuid = uuid4() | ||
self.uuid = uuid | ||
self.storage = storage |
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
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,71 @@ | ||
import logging | ||
from collections import MutableSequence | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
from ..storages import Storage | ||
from .base import Ringer | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RingerList(Ringer, MutableSequence): | ||
|
||
def __init__(self, file_path: Path, *args, **kwargs): | ||
if kwargs.get('storage') != Storage.FILE: | ||
raise NotImplementedError | ||
|
||
super().__init__(*args, **kwargs) | ||
|
||
self._file_path = file_path | ||
open(str(self._file_path), 'w').close() | ||
|
||
def insert(self, index: int, value: Any) -> None: | ||
logger.debug('Inserting "{}" on "{}" index.'.format(str(value)[:30], index)) | ||
|
||
if self._file_path.exists(): | ||
with open(str(self._file_path)) as file_reader: | ||
file_data = file_reader.readlines() | ||
else: | ||
file_data = list() | ||
raw_value = '{}\n'.format(value) | ||
file_data.insert(index, raw_value) | ||
|
||
with open(str(self._file_path), 'w') as file_writer: | ||
file_writer.writelines(file_data) | ||
|
||
def __setitem__(self, index, value): | ||
logger.debug('Setting "{}" on "{}" index.'.format(str(value)[:30], index)) | ||
|
||
if self._file_path.exists(): | ||
with open(str(self._file_path)) as file_reader: | ||
file_data = file_reader.readlines() | ||
else: | ||
file_data = list() | ||
raw_value = '{}\n'.format(value) | ||
file_data[index] = raw_value | ||
|
||
with open(str(self._file_path), 'w') as file_writer: | ||
file_writer.writelines(file_data) | ||
|
||
def __delitem__(self, index): | ||
logger.debug('Deleting on "{}" index.'.format(index)) | ||
|
||
if self._file_path.exists(): | ||
with open(str(self._file_path)) as file_reader: | ||
file_data = file_reader.readlines() | ||
else: | ||
file_data = list() | ||
del file_data[index] | ||
|
||
with open(str(self._file_path), 'w') as file_writer: | ||
file_writer.writelines(file_data) | ||
|
||
def __getitem__(self, index): | ||
logger.debug('Getting on "{}" index.'.format(index)) | ||
with open(str(self._file_path)) as file_reader: | ||
return file_reader.readlines()[index].strip() | ||
|
||
def __len__(self): | ||
with open(str(self._file_path)) as f: | ||
return sum(1 for _ in f) |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
class RingException(Exception): | ||
class RingerException(Exception): | ||
pass | ||
|
||
|
||
class EmptyRingException(RingException): | ||
class EmptyRingerException(RingerException): | ||
pass |
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
from enum import Enum | ||
|
||
|
||
class Storage(Enum): | ||
MEMORY = 'memory' | ||
FILE = 'file' | ||
DIRECTORY = 'directory' |
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,51 @@ | ||
import unittest | ||
from pathlib import Path | ||
|
||
import ringer as rg | ||
|
||
|
||
class TestRingMemory(unittest.TestCase): | ||
|
||
def setUp(self) -> None: | ||
self.file_path = Path('test.rgr') | ||
self.ring = rg.RingerList(file_path=self.file_path, storage=rg.Storage.FILE) | ||
|
||
def tearDown(self) -> None: | ||
self.file_path.unlink() | ||
|
||
def test_append(self): | ||
self.ring.append('A') | ||
self.assertEqual(len(self.ring), 1) | ||
|
||
self.ring.append('B') | ||
self.assertEqual(len(self.ring), 2) | ||
|
||
self.ring.append('C') | ||
self.assertEqual(len(self.ring), 3) | ||
|
||
self.ring.append('D') | ||
self.assertEqual(len(self.ring), 4) | ||
|
||
def test_pop(self): | ||
self.ring.append('A') | ||
self.ring.append('B') | ||
self.ring.append('C') | ||
self.ring.append('D') | ||
|
||
result = self.ring.pop() | ||
self.assertEqual(result, 'D') | ||
|
||
result = self.ring.pop() | ||
self.assertEqual(result, 'C') | ||
|
||
result = self.ring.pop() | ||
self.assertEqual(result, 'B') | ||
|
||
result = self.ring.pop() | ||
self.assertEqual(result, 'A') | ||
|
||
self.assertEqual(len(self.ring), 0) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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