class Document: """Represents document""" def __init__(self): """Initializes variables""" self.characters = [] self.cursor = Cursor(self) self.filename = '' def insert(self, character): """Insert character on cursor's position (limitations described in document_test.py)""" if not hasattr(character, 'character'): character = Character(character) self.characters.insert(self.cursor.position, character) self.cursor.forward() def delete(self): """Delete character on cursor's position (limitations described in document_test.py)""" del self.characters[self.cursor.position] def save(self): """Save file but we should input filename in __init__()""" f = open(self.filename, 'w') f.write(''.join(self.characters)) f.close() @property def string(self): "Returns string of characters" return ''.join((str(c) for c in self.characters))
class Document: def __init__(self): """ inits a new instance of Document class with attributes characters, cursor and filename """ self.characters = [] self.cursor = Cursor(self) self.filename = '' @property def string(self): """ returns a string of styled chars """ return "".join((str(c) for c in self.characters)) """ c. збереження файлу без імені d. введення декількох символів """ def insert(self, character): """ insters the character to the position, where the cursor is located now """ if not hasattr(character, 'character'): character = Character(character) if len(character.character) > 1: raise NotChar self.characters.insert(self.cursor.position, character) self.cursor.forward() def delete(self): """ deletes the character from the position, where the cursor is located now """ try: del self.characters[self.cursor.position] except: raise CharacterDontExist def save(self): """ saves the document to the file with its filename """ if not self.filename: raise NoFilename f = open(self.filename, 'w') f.write(''.join(self.characters)) f.close()
class Document: """ Represents a generic document that can be parsed by text editors or document processors """ def __init__(self): self.characters = [] self.cursor = Cursor(self) self._filename = '' def insert(self, character): """ Insert a new character in the document at the current cursor position """ self.characters.insert(self.cursor.position, character) self.cursor.forward() def delete(self): """Deletes the character at current cursor position""" del self.characters[self.cursor.position] def save(self): """Saves the document to persistent memory storage on the computer""" with open(self._filename, 'w') as f: f.write(''.join(self.characters)) @property def string(self): return ''.join(self.characters) @property def filename(self): print('getting filename...') return self._filename @filename.setter def filename(self, name): print('setting filename...') self._filename = name def __repr__(self): return self._filename
class Document: def __init__(self): self.characters = [] self.cursor = Cursor(self) self.filename = '' @property def string(self): return "".join((str(c) for c in self.characters)) def insert(self, character): if not hasattr(character, 'character'): character = Character(character) self.characters.insert(self.cursor.position, character) self.cursor.forward() def delete(self): del self.characters[self.cursor.position] def save(self): f = open(self.filename, 'w') f.write(''.join(self.characters)) f.close()