Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 633709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:12:56+00:00 2026-05-13T20:12:56+00:00

first, an example: given a bunch of Person objects with various attributes (name, ssn,

  • 0

first, an example:

given a bunch of Person objects with
various attributes (name, ssn, phone,
email address, credit card #, etc.)

now imagine the following simple
website:

  1. uses a person’s email address as unique login name
  2. lets users edit their attributes (including their email address)

if this website had tons of users,
then it make sense to store Person
objects in a dictionary indexed by
email address, for quick Person
retrieval upon login.

however when a Person’s email address
is edited, then the dictionary key for
that Person needs to be changed as
well. this is slightly yucky

im looking for suggestions on how to tackle the generic problem:

given a bunch of entities with a shared aspect. the aspect is used both for fast access to the entities and within each entity’s functionality. where should the aspect be placed:

  1. within each entity (not good for fast access)
  2. index only (not good for each entity’s functionality)
  3. both within each entity and as index (duplicate data/reference)
  4. somewhere else/somehow differently

the problem may be extended, say, if we want to use several indices to index the data (ssn, credit card number, etc.). eventually we may end up with a bunch of SQL tables.

im looking for something with the following properties (and more if you can think of them):

# create an index on the attribute of a class
magical_index = magical_index_factory(class, class.attribute)
# create an object
obj = class() 
# set the object's attribute
obj.attribute= value
# retrieve object from using attribute as index
magical_index[value] 
# change object attribute to new value
obj.attribute= new_value 
# automagically object can be retrieved using new value of attribute
magical_index[new_value]
# become less materialistic: get rid of the objects in your life
del obj
# object is really gone
magical_index[new_value]
KeyError: new_value

i want the object, indices, all to play nicely and seamlessly with each other.

please suggest appropriate design patterns

note:
the above example is just that, an example. an example used to portray the generic problem.
so please provide generic solutions (of course, you may choose to keep using the example when explaining your generic solution)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:12:57+00:00Added an answer on May 13, 2026 at 8:12 pm

    Consider this.

    class Person( object ):
        def __init__( self, name, addr, email, etc. ):
            self.observer= []
            ... etc. ...
        @property
        def name( self ): return self._name
        @name.setter
        def name( self, value ): 
            self._name= value
            for observer in self.observedBy: observer.update( self )
        ... etc. ...
    

    This observer attribute implements an Observable that notifies its Observers of updates. This is the list of observers that must be notified of changes.

    Each attribute is wrapped with properties. Using Descriptors us probably better because it can save repeating the observer notification.

    class PersonCollection( set ):
        def __init__( self, *args, **kw ):
            self.byName= collections.defaultdict(list)
            self.byEmail= collections.defaultdict(list)
            super( PersonCollection, self ).__init__( *args, **kw )
        def add( self, person ):
            super( PersonCollection, self ).append( person )
            person.observer.append( self )
            self.byName[person.name].append( person )
            self.byEmail[person.email].append( person )
        def update( self, person ):
            """This person changed.  Find them in old indexes and fix them."""
            changed = [(k,v) for k,v in self.byName.items() if id(person) == id(v) ]
            for k, v in changed:
                self.byName.pop( k )
            self.byName[person.name].append( person )
            changed = [(k,v) for k,v in self.byEmail.items() if id(person) == id(v) ]
            for k, v in changed:
                self.byEmail.pop( k )
            self.byEmail[person.email].append( person)
    
        ... etc. ... for all methods of a collections.Set.
    

    Use collections.ABC for more information on what must be implemented.

    http://docs.python.org/library/collections.html#abcs-abstract-base-classes

    If you want “generic” indexing, then your collection can be parameterized with the names of attributes, and you can use getattr to get those named attributes from the underlying objects.

    class GenericIndexedCollection( set ):
        attributes_to_index = [ ] # List of attribute names
        def __init__( self, *args, **kw ):
            self.indexes = dict( (n, {}) for n in self.attributes_to_index ]
            super( PersonCollection, self ).__init__( *args, **kw )
        def add( self, person ):
            super( PersonCollection, self ).append( person )
            for i in self.indexes:
                self.indexes[i].append( getattr( person, i )
    

    Note. To properly emulate a database, use a set not a list. Database tables are (theoretically) sets. As a practical matter they are unordered, and an index will allow the database to reject duplicates. Some RDBMS’s don’t reject duplicate rows because — without an index — it’s too expensive to check.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Beyond the obvious answer of multiplying by 1024 (or 1000… May 14, 2026 at 6:40 pm
  • Editorial Team
    Editorial Team added an answer If you want to do it on a scheduled basis,… May 14, 2026 at 6:40 pm
  • Editorial Team
    Editorial Team added an answer Use: if(@mfr_id = '5') Value comparisons have to be the… May 14, 2026 at 6:40 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.