Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # classes.py - AuroraTag
- # Author: mark grandi
- # 6/6/2011
- #
- from datetime import datetime
- from database import Database
- class TagCollection:
- ''' class that contains a collection of tags'''
- # set up the table stuff
- def __init__(self):
- # instance variables
- self.tags = []
- self.id = None
- def __iter__(self):
- return self.tags.__iter__()
- def __next__(self):
- return self.tags.__next__()
- def __str__(self):
- ''' toString override'''
- result = ""
- for tag in self.tags:
- result = result + tag + "\n"
- return result
- def numberOfTags(self):
- ''' function that returns the number of tags'''
- return len(self.tags)
- def addTag(self, tagToAdd):
- ''' adds the tag to the collection. Throws a NotTagException if
- you try and add any object except a Tag object to the collection'''
- if not isinstance(tagToAdd, Tag):
- raise NotTagException("You can only add tags to a TagCollection")
- # add it to our internal list
- self.tags.append(tagToAdd)
- def containsTag(self, tagToCheck):
- ''' returns whether this collection contains the specified tag or not'''
- return tagToCheck in self.tags
- class Tag:
- ''' class that represents a tag'''
- def __init__(self, tagName = ""):
- '''
- init function
- @param tagName - the name of the tag.
- '''
- # instance variables
- self.id = None
- #self.parentId = None
- self.name = tagName
- #self.nestedTags = None
- self.dateCreated = datetime.now()
- self.state = State.NEW
- def __setattr__(self, name, value):
- ''' so i'm defining this method so that it gets called every time
- i set an attribute on this object. All this does is sets the attribute
- normally, and then set the object's state to be dirty if its not already new. '''
- self.__dict__[name] = value
- # set the object state, we do it through __dict__ to not cause an infinite loop
- # self.state = State.DIRTY
- self.__dict__["state"] = State.DIRTY
- def __str__(self):
- ''' toString override'''
- return "<Tag: {}>".format(self.name)
- def __repr__(self):
- return "<Tag: {}>".format(self.name)
- class State:
- ''' enum class that represents the state of a tag. '''
- CLEAN = 0
- DIRTY = 1
- NEW = 2
- class SingletonException:
- ''' exception that gets called when you try to create another instance of a singleton'''
- pass
- class NotTagException(Exception):
- '''exception class that gets raised when you try to add
- a non Tag object to the TagCollection'''
- pass
- class NotTagCollectionException(Exception):
- '''exception class that gets raised when you try to save
- a non TagCollection object to the database'''
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement