HOME | Blog | YouTube | LinkedIn | About Me         || Calculators    | SoftEng/Tech Posts    | Code/Scripts

Python - Collection of Objects

As I am learning, I notice that classes/objects are not used as much as they are used in C#/Java and I do see that dictionaries are used a lot instead. However, I still wanted to program a little example with collection/list of objects.


object-collection.py
__________________________________________________________________
#!/usr/bin/python

###############################################################################

def main():
    # Create the customer manager instance
    CustMgr = CustomerMgr('fjksljfk')

    # Get a single customer
    customerObj1 = CustMgr.GetSingleCustomer()
    print str(customerObj1.id) + ' ^^ ' + str(customerObj1.name) + ' ^^ ' + str(customerObj1.address)

    print '...'
    # Get the list of customers and print out the values
    myListOfCustomers = CustMgr.GetTheListOfCustomers()
    for i in myListOfCustomers:
        print str(i.id) + ' ** ' + str(i.name) + ' ** ' + str(i.address)

    print '...'
    # Get a specific customer and print its values
    customerObj2 = CustMgr.GetSpecificCustomerFromTheList(4)
    print str(customerObj2.id) + ' ^^ ' + str(customerObj2.name) + ' ^^ ' + str(customerObj2.address)


###############################################################################

###############################################################################

class CustomerMgr:
    def __init__(self, className):  # Constructor of the class
        self.className = className

    def GetSingleCustomer(self):
        print 'getting single customer...'
        myCustomer = MyCustomerValueObject(1, 'almir', '23 Bloor St, Toronto')
        return myCustomer

    def GetSpecificCustomerFromTheList(self, id):
        print 'Get SPECIFIC customer based on id'
        for j in self.Generate_GetTheListOfCustomers():
            if j.id == id:
                return j

    def GetTheListOfCustomers(self):
        print 'Get the list of customer ...'        
        return self.Generate_GetTheListOfCustomers()

    def Generate_GetTheListOfCustomers(self):
        for n in range(1, 5):
            testCustomerValueObject = MyCustomerValueObject(n, 'james' + str(n), '535 Sienna Blvd' + '...' + str(n))
            yield testCustomerValueObject


###############################################################################
###############################################################################

class MyCustomerValueObject:
    def __init__(self, id, name, address):  # Constructor of the class
        self.id = id
        self.name = name
        self.address = address


###############################################################################

if __name__ == "__main__": main()

__________________________________________________________________



OUTPUT:

getting single customer...
1 ^^ almir ^^ 23 Bloor St, Toronto
...
Get the list of customer ...
1 ** james1 ** 535 Sienna Blvd...1
2 ** james2 ** 535 Sienna Blvd...2
3 ** james3 ** 535 Sienna Blvd...3
4 ** james4 ** 535 Sienna Blvd...4
...
Get SPECIFIC customer based on id
4 ^^ james4 ^^ 535 Sienna Blvd...4




almirsCorner.com

#python #programming #code #coding #software #softwaredeveloper #softwareengineering #programmer


No comments:

Post a Comment