-
Notifications
You must be signed in to change notification settings - Fork 0
/
word.py
41 lines (33 loc) · 1.46 KB
/
word.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import shutil
dataFoldername = 'dictionary_data'
class Word:
def __init__(self, word: str = None, imagePath: str = None, definitions: list = None, exampleSentences: list = None):
if not word:
return
self.word = word
self.imagePath = imagePath
self.imageExists = True
self.fileExtension = ''
if not os.path.exists(self.imagePath):
self.imageExists = False
self.copyImageToDataFolder()
self.definitions = definitions
self.exampleSentences = exampleSentences
def copyImageToDataFolder(self):
if not self.imageExists:
return
self.fileExtension = self.imagePath.split('.')[-1]
self.newImagePath = f'{dataFoldername}/{self.word}.{self.fileExtension}'
if self.imagePath != self.newImagePath:
shutil.copy2(self.imagePath, self.newImagePath)
def getAsDictionary(self):
return {'imageExists': self.imageExists,'definitions': self.definitions, 'exampleSentences': self.exampleSentences, 'fileExtension': self.fileExtension}
def loadFromDict(self, wordName: str, dct: dict):
self.word = wordName
self.imageExists = dct['imageExists']
self.definitions = dct['definitions']
self.exampleSentences = dct['exampleSentences']
self.fileExtension = dct['fileExtension']
if self.imageExists:
self.imagePath = f'{dataFoldername}/{self.word}.{self.fileExtension}'