Last active
November 10, 2018 07:16
-
-
Save weallwegot/c4fb3bfd5d9ad842678ab2256a14a80c to your computer and use it in GitHub Desktop.
Represents a text message object, or a series of texts sent in quick succession.
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
class TextEquivalent(object): | |
def __init__(self,name,timestamp_string,text): | |
self.sender = name | |
self.time = None | |
self.all_text = text | |
self.timestamp = timestamp_string | |
self.date_day_of_week = self.timestamp.isoweekday() | |
self.date_year = self.timestamp.year | |
self.date_month = self.timestamp.month | |
self.date_day = self.timestamp.day | |
@property | |
def timestamp(self): | |
return self._timestamp | |
@timestamp.setter | |
def timestamp(self,tstamp): | |
if isinstance(tstamp,str): | |
self._timestamp = parser.parse(tstamp) | |
elif isinstance(tstamp,datetime.datetime): | |
self._timestamp = tstamp | |
else: | |
logger.warning('attempted to set timestamp w/ an incorrect type: {}' | |
.format(type(tstamp))) | |
def merge_sequential_text_equiv(self,text_equivalent_to_merge): | |
""" | |
merges an additional text object into the current one. | |
appends to text the all_text attribute with a new line character | |
updates the timestamp attribute by averaging the two timestamps | |
:param text_equivalent_to_merge: the TextEquivalent to merge with self | |
:text_equivalent_to_merge type: TextEquivalent | |
""" | |
text_to_append = text_equivalent_to_merge.all_text | |
self.all_text = self.all_text + "\n" + text_to_append | |
timestamp_to_merge = text_equivalent_to_merge.timestamp | |
self.timestamp = self.average_timestamps(self.timestamp,timestamp_to_merge) | |
if not text_equivalent_to_merge.sender == self.sender: | |
error_msg = "Senders of merged texts must be identical\n{} does not equal {}".\ | |
format(self.sender,text_equivalent_to_merge.sender) | |
logger.error(error_msg) | |
raise Exception(error_msg) | |
def average_timestamps(self,earlier,later): | |
""" | |
function to average two datetime objects | |
:param earlier: the earlier of the two datatime objects | |
:earlier type: datetime.datetime | |
:param later: the later of the two datatime objects | |
:later type: datetime.datetime | |
""" | |
#http://stackoverflow.com/questions/25473394/finding-mid-point-date-between-two-dates-in-python | |
return(earlier + (later-earlier)/2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment