>>> +--+_+-+_++_+--_+_-_+-+-+-___++++_+-_-+++_+-+_--++--_ ', !'
+
, -
and ~
(bitwise negation). (There is still not
, but this is a separate story.) It is interesting that they can be combined in unlimited quantities: >>> ++-++-+---+--++-++-+1 -1 >>> -~-~-~-~-~-~-~-~-~-~1 11
+--+_+-+_++_+--_+_-_+-+-+-___++++_+-_-+++_+-+_--++--_
as (+--+_) + (-+_) + (+_) + (--_) + _ - _ + (-+-+-___) + (+++_) + (-_) - (+++_) + (-+_) - (-++--_)
_
(end of sequence) and ___
(end of sequence and space).__pos__
and __neg__
, and for binary ones, these are four methods at once: __add__
, __radd__
, __sub__
and __rsub__
._
. First of all, it needs to support unary operators and accumulate the facts of their application: class Morse(object): def __init__(self, buffer=""): self.buffer = buffer def __neg__(self): return Morse("-" + self.buffer) def __pos__(self): return Morse("." + self.buffer)
__str__
method. morse_alphabet = { "" : ".-", "" : "-...", "" : ".--", "" : "--.", "" : "-..", "" : ".", "" : "...-", "" : "--..", "" : "..", "" : ".---", "" : "-.-", "" : ".-..", "" : "--", "" : "-.", "" : "---", "" : ".--.", "" : ".-.", "" : "...", "" : "-", "" : "..-", "" : "..-.", "" : "....", "" : "-.-.", "" : "---.", "" : "----", "" : "--.-", "" : "--.--", "" : "-.--", "" : "-..-", "" : "..-..", "" : "..--", "" : ".-.-", "1" : ".----", "2" : "..---", "3" : "...--", "4" : "....-", "5" : ".....", "6" : "-....", "7" : "--...", "8" : "---..", "9" : "----.", "0" : "-----", "." : "......", "," : ".-.-.-", ":" : "---...", ";" : "-.-.-.", "(" : "-.--.-", ")" : "-.--.-", "'" : ".----.", "\"": ".-..-.", "-" : "-....-", "/" : "-..-.", "?" : "..--..", "!" : "--..--", "@" : ".--.-.", "=" : "-...-", } inverse_morse_alphabet = {v: k for k, v in morse_alphabet.items()}
def __str__(self): return inverse_morse_alphabet[self.buffer] # , # KeyError. .
def __add__(self, other): return str(self) + str(+other) # + other.
Morse
next to her? No, addition with this type in str.__add__
not provided. Therefore, Python will attempt to call the __radd__
method on the right object. We implement it: def __radd__(self, s): return s + str(+self)
def __sub__(self, other): return str(self) + str(-other) def __rsub__(self, s): return s + str(-self)
class Morse(object): def __init__(self, buffer=""): self.buffer = buffer def __neg__(self): return Morse("-" + self.buffer) def __pos__(self): return Morse("." + self.buffer) def __str__(self): return inverse_morse_alphabet[self.buffer] def __add__(self, other): return str(self) + str(+other) def __radd__(self, s): return s + str(+self) def __sub__(self, other): return str(self) + str(-other) def __rsub__(self, s): return s + str(-self)
def morsify(s): s = "_".join(map(morse_alphabet.get, s.upper())) s = s.replace(".", "+") + ("_" if s else "") return s
>>> morsify(",!") '+--+_+-+_++_+--_+_-_+-+-+-_++++_+-_-+++_+-+_--++--_' >>> _ = Morse() >>> +--+_+-+_++_+--_+_-_+-+-+-_++++_+-_-+++_+-+_--++--_ ',!'
Morse
, just add a space at the end. class MorseWithSpace(Morse): def __str__(self): return super().__str__() + " " ___ = MorseWithSpace()
MorseWithSpace
not replaced by objects of type Morse
during work, you must also change __pos__
and __neg__
: def __neg__(self): return MorseWithSpace(super().__neg__().buffer) def __pos__(self): return MorseWithSpace(super().__pos__().buffer)
" " : " "
to the Morse code dictionary and change a bit the morsify function: def morsify(s): s = "_".join(map(morse_alphabet.get, s.upper())) s = s.replace(".", "+") + ("_" if s else "") s = s.replace("_ ", "__").replace(" _", "__") return s
>>> morsify(", !") '+--+_+-+_++_+--_+_-_+-+-+-___++++_+-_-+++_+-+_--++--_' >>> ___ = MorseWithSpace() >>> +--+_+-+_++_+--_+_-_+-+-+-___++++_+-_-+++_+-+_--++--_ ', !'
Source: https://habr.com/ru/post/349776/
All Articles