t | class morse: | t | class morse: |
| | | |
| def __init__(self, s=''): | | def __init__(self, s=''): |
| if not s or ' ' in s: | | if not s or ' ' in s: |
| self.sep = ',' | | self.sep = ',' |
| self.end = '.' | | self.end = '.' |
| s = s.split(' ') | | s = s.split(' ') |
| else: | | else: |
| self.sep = ' ' | | self.sep = ' ' |
| self.end = '' | | self.end = '' |
| n = len(s) | | n = len(s) |
| if n == 2: | | if n == 2: |
| self.dot = s[0] | | self.dot = s[0] |
| self.end_dot = s[0] | | self.end_dot = s[0] |
| self.dash = s[1] | | self.dash = s[1] |
| elif n == 3: | | elif n == 3: |
| (self.dot, self.end_dot, self.dash) = s | | (self.dot, self.end_dot, self.dash) = s |
| elif n == 4: | | elif n == 4: |
| (self.dot, self.end_dot, self.dash, self.end) = s | | (self.dot, self.end_dot, self.dash, self.end) = s |
| else: | | else: |
| self.dot = 'di' | | self.dot = 'di' |
| self.end_dot = 'dit' | | self.end_dot = 'dit' |
| self.dash = 'dah' | | self.dash = 'dah' |
| self.prev = None | | self.prev = None |
| self.buff = self.end | | self.buff = self.end |
| | | |
| def __neg__(self): | | def __neg__(self): |
| if self.sep != ' ': | | if self.sep != ' ': |
| self.buff = ' ' + self.dash + self.buff | | self.buff = ' ' + self.dash + self.buff |
| else: | | else: |
| self.buff = self.dash + self.buff | | self.buff = self.dash + self.buff |
| self.prev = '-' | | self.prev = '-' |
| return self | | return self |
| | | |
| def __pos__(self): | | def __pos__(self): |
| tmp = self.dot | | tmp = self.dot |
| if self.prev == '~' or not self.prev: | | if self.prev == '~' or not self.prev: |
| tmp = self.end_dot | | tmp = self.end_dot |
| if self.sep != ' ': | | if self.sep != ' ': |
| tmp = ' ' + tmp | | tmp = ' ' + tmp |
| self.buff = tmp + self.buff | | self.buff = tmp + self.buff |
| self.prev = '+' | | self.prev = '+' |
| return self | | return self |
| | | |
| def __invert__(self): | | def __invert__(self): |
| self.buff = self.sep + self.buff | | self.buff = self.sep + self.buff |
| self.prev = '~' | | self.prev = '~' |
| return self | | return self |
| | | |
| def __str__(self): | | def __str__(self): |
| return self.buff.strip() | | return self.buff.strip() |