Files
jogobot-red/red_fam.py
GOLDERWEB – Jonathan Golder 28d9a26cf1 Always handle beginning and ending definitions via setter functions
Setter functions for beginning and ending decide wether it is a datetime object or a parseable timestamp string
2015-09-06 15:04:49 +02:00

104 lines
2.6 KiB
Python

import hashlib
from datetime import datetime
class RED_FAM:
# Define the timestamp format
__timestamp_format = "%H:%M, %d. %b. %Y (%Z)"
def __init__( self, articlesList, beginning=None, ending=None, status=0 ):
"""
Generates a new RED_FAM object
@param articlesList list List of articles of redundance family
@param beginning datetime Beginning date of redundance diskussion
@param ending datetime Ending date of redundance diskussion
"""
self.__articlesList = articlesList
self.__hash = self.__get_fam_hash( )
if( beginning ):
self.add_beginning( beginning )
else:
self.__beginning = None
if( ending ):
self.add_ending( ending )
else:
self.__ending = None
self.__status = status # __TODO__ STATUS CODE
def __get_fam_hash( self ):
"""
Calculates the SHA-1 hash for the articlesList of redundance family.
Since we don't need security SHA-1 is just fine.
@returns str String with the hexadecimal hash digest
"""
h = hashlib.sha1()
h.update( str( self.__articlesList ).encode('utf-8') )
return h.hexdigest()
def add_beginning( self, beginning ):
"""
Adds the beginning date of a redundance diskussion to the object and sets changed to True
@param datetime datetime Beginning date of redundance diskussion
"""
self.__beginning = self.__datetime( beginning )
self.__changed = True
def add_ending( self, ending ):
"""
Adds the ending date of a redundance diskussion to the object. Also sets the status to __TODO__ STATUS NUMBER and changed to True
@param datetime datetime Ending date of redundance diskussion
"""
self.__ending = self.__datetime( ending )
self.__status = 2 #__TODO__ STATUS NUMBER
self.__changed = True
def __datetime( self, timestamp ):
"""
Decides wether given timestamp is a parseable string or a datetime object and returns a datetime object in both cases
@param timestamp datetime Datetime object
str Parseable string with timestamp in format __timestamp_format
@returns datetime Datetime object
"""
if( isinstance( timestamp, datetime ) ):
return timestamp
else:
return datetime.strptime( timestamp, type( self ).__timestamp_format )
def __repr__( self ):
if( self.__beginning ):
beginning = ", beginning=" + repr( self.__beginning )
else:
beginning = ""
if( self.__ending ):
ending = ", ending=" + repr( self.__ending )
else:
ending = ""
__repr = "RED_FAM( " + repr( self.__articlesList ) + beginning + ending + ", status=" + repr( self.__status ) + " )"
return __repr
x = RED_FAM( [ "Test", "Foo", "Bar" ] )
print( repr( x ) )