objectSet.py :  » Web-Server » Porcupine-Web-Application-Server » porcupine-0.6-src » porcupine » core » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Server » Porcupine Web Application Server 
Porcupine Web Application Server » porcupine 0.6 src » porcupine » core » objectSet.py
#===============================================================================
#    Copyright 2005-2009, Tassos Koutsovassilis
#
#    This file is part of Porcupine.
#    Porcupine is free software; you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation; either version 2.1 of the License, or
#    (at your option) any later version.
#    Porcupine is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#    You should have received a copy of the GNU Lesser General Public License
#    along with Porcupine; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#===============================================================================
"""
Porcupine Object Set
"""

class ObjectSet(object):
    """
    Porcupine Object Set
    ====================
    The Porcupine object set is a versatile type for keeping a large collection
    of objects or rows with a specified schema.
    """
    __slots__ = ('_list', 'schema')
    
    def __init__(self, data, schema=None):
        self._list = data
        self.schema = schema

    def __iter__(self):
        if len(self._list) > 0:
            if self.schema == None:
                for item in self._list:
                    yield item
            else:
                for x in self._list:
                    yield dict(zip(self.schema, x))
                
    def __nonzero__(self):
        return len(self._list)
    
    def __len__(self):
        """Returns the size of the objects set.
        Valid only for resolved object sets.
        
        @raise TypeError: if the object set is unresolved
        """
        return len(self._list)
        
    def __add__(self, objectset):
        """Implements the '+' operator.
        In order to add two object sets successfully one of the following
        conditions must be met:
            1. Both of the object sets must contain objects
            2. Object sets must have identical schema
        """
        if self.schema == objectset.schema:
            return ObjectSet(self._list + objectset._list,
                             schema = self.schema)
        else:
            raise TypeError, 'Unsupported operand (+). Object sets do not ' + \
                             'have the same schema'
        
    def __contains__(self, value):
        """Implements membership tests.
        If the object set contains objects then legal tests are:
            1. C{object_id in objectset}
            2. C{object in objectset}
        If the object set contains rows then legal tests are:
            1. C{row_tuple in objectset}
            2. C{value in objectset} if the object set contains one field
        """
        if self.schema:
            if len(self.schema) != 1:
                return value in self._list
            else:
                return value in [z[0] for z in self._list]
        else:
            if not isinstance(value, str):
                try:
                    value = value._id
                except AttributeError:
                    raise TypeError, 'Invalid argument type'
            return value in [z._id for z in self._list]

    def __getitem__(self, key):
        "Implements slicing. Useful for paging."
        if self.schema == None:
            return self._list[key] 
        else:
            if type(key) == int:
                return dict(zip(self.schema, self._list[key]))
            else:
                return [dict(zip(self.schema, x)) for x in self._list[key]]
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.