Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # classes.py - AuroraTag
  4. # Author: mark grandi
  5. # 6/6/2011
  6. #
  7.  
  8. from datetime import datetime
  9. from database import Database
  10.  
  11.  
  12. class TagCollection:
  13.     ''' class that contains a collection of tags'''
  14.    
  15.     # set up the table stuff
  16.    
  17.     def __init__(self):
  18.        
  19.         # instance variables
  20.         self.tags = []
  21.         self.id = None
  22.        
  23.     def __iter__(self):
  24.    
  25.         return self.tags.__iter__()
  26.        
  27.     def __next__(self):
  28.    
  29.         return self.tags.__next__()
  30.        
  31.     def __str__(self):
  32.         ''' toString override'''
  33.        
  34.         result = ""
  35.        
  36.         for tag in self.tags:
  37.        
  38.             result = result + tag + "\n"
  39.            
  40.         return result
  41.        
  42.        
  43.     def numberOfTags(self):
  44.         ''' function that returns the number of tags'''
  45.    
  46.         return len(self.tags)
  47.        
  48.    
  49.     def addTag(self, tagToAdd):
  50.         ''' adds the tag to the collection. Throws a NotTagException if
  51.         you try and add any object except a Tag object to the collection'''
  52.    
  53.         if not isinstance(tagToAdd, Tag):
  54.    
  55.             raise NotTagException("You can only add tags to a TagCollection")
  56.            
  57.         #  add it to our internal list
  58.         self.tags.append(tagToAdd)
  59.        
  60.     def containsTag(self, tagToCheck):
  61.         ''' returns whether this collection contains the specified tag or not'''
  62.         return  tagToCheck in self.tags
  63.        
  64.    
  65.    
  66.        
  67.  
  68. class Tag:
  69.     ''' class that represents a tag'''
  70.  
  71.     def __init__(self, tagName = ""):
  72.         '''
  73.         init function
  74.        
  75.         @param tagName - the name of the tag.      
  76.         '''
  77.        
  78.         # instance variables
  79.         self.id = None
  80.         #self.parentId = None
  81.         self.name = tagName
  82.         #self.nestedTags = None
  83.         self.dateCreated = datetime.now()
  84.         self.state = State.NEW
  85.        
  86.     def __setattr__(self, name, value):
  87.         ''' so i'm defining this method so that it gets called every time
  88.         i set an attribute on this object. All this does is sets the attribute
  89.         normally, and then set the object's state to be dirty if its not already new. '''
  90.        
  91.         self.__dict__[name] = value
  92.        
  93.         # set the object state, we do it through __dict__ to not cause an infinite loop
  94.         # self.state = State.DIRTY
  95.         self.__dict__["state"] = State.DIRTY
  96.        
  97.     def __str__(self):
  98.         ''' toString override'''
  99.        
  100.         return "<Tag: {}>".format(self.name)
  101.        
  102.     def __repr__(self):
  103.         return "<Tag: {}>".format(self.name)
  104.  
  105.        
  106. class State:
  107.     ''' enum class that represents the state of a tag. '''
  108.     CLEAN = 0
  109.     DIRTY = 1
  110.     NEW = 2
  111.  
  112.            
  113.            
  114. class SingletonException:
  115.     ''' exception that gets called when you try to create another instance of a singleton'''
  116.    
  117.     pass
  118.        
  119.        
  120. class NotTagException(Exception):
  121.     '''exception class that gets raised when you try to add
  122.     a non Tag object to the TagCollection'''
  123.  
  124.     pass
  125.    
  126. class NotTagCollectionException(Exception):
  127.     '''exception class that gets raised when you try to save
  128.     a non TagCollection object to the database'''
  129.  
  130.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement