class tennis:
    def __init__ (self, name, rank, tournamentsPlayed, country, racquetBrand):
        self.name = name
        self.rank = rank
        self.tournamentsPlayed = tournamentsPlayed
        self.country = country
        self.racquetBrand = racquetBrand
        #defines each thing^

    def __str__(self):#creates a string
        return self.name + ":" + self.rank + "\n" + self.tournamentsPlayed + "\n" + self.country + "\n" + self.racquetBrand
    #prints information from above things

class professionals:
    def __init__(self):
        self.tennisPro = []
        self.tennisPro.append(tennisPro("Roger Federer", "1", "18", "SUI", "Wilson"))
        self.tennisPro.append(tennisPro("Novak Djokovic", "2", "21", "SRB", "Head"))
        self.tennisPro.append(tennisPro("Rafael Nadal", "3", "17", "ESP", "Babolat"))
        self.tennisPro.append(tennisPro("Juan Martin Del Potro", "4", "18", "ARG", "Wilson"))
        self.tennisPro.append(tennisPro("Andy Murray", "5", "17", "GRB", "Head"))
        self.tennisPro.append(tennisPro("Nikolay Davydenko", "6", "25", "RUS", "Prince"))
        self.tennisPro.append(tennisPro("Robin Soderling", "7", "25", "SWE", "Head"))
        self.tennisPro.append(tennisPro("Andy Roddick", "8", "20", "USA", "Babolat"))
        self.tennisPro.append(tennisPro("Fernando Verdasco", "9", "26", "ESP", "Tecnifibre"))
        self.tennisPro.append(tennisPro("Jo-Wilfried Tsonga", "10", "25", "FRA", "Wilson"))

    def sort(self, x):
        n = len(self.tennisPro)
        if n <= 1:
            return self.tennisPro
        else:
            return tennisPro.mergeSort(self.tennisPro, x)
        #finds the length of the length of self.tennisPro and returns it if its 1 or less, if not it runs the mergeSort method

    @staticmethod #makes it a class method  
    def mergeSort(l, x):
##        print("Running MergeSort on {0}".format(l))
        n = len(l)
        if n <= 1:
            return l
        else:
            i = n//2
            a = tennisPro.mergeSort(l[0:i], x)
            b = tennisPro.mergeSort(l[i:], x)
            return tennisPro.merge(a, b, x)
        #finds length of self.tennisPro ands returns its if its 1 or less, if not it runs it through merge and returns it

    @staticmethod #makes it a class method
    def merge(a , b, x):
##        print("Running Merge on {0} and {1}".format(a, b))
        index1 = 0
        index2 = 0
        temp = []
        if  x == "name":
            while (index1<len(a)) and (index2<len(b)):
                    if a[index1].name <= b[index2].name:
                        temp.append(a[index1])
                        index1+=1
                    else:
                        temp.append(b[index2])
                        index2+=1
            if index1 < len(a):
                for i in a[index1:]:
                    temp.append(i)
            elif index2 < len(b):
                for i in b[index2:]:
                    temp.append(i)      
        if  x == "rank":
            while (index1<len(a)) and (index2<len(b)):
                    if a[index1].rank <= b[index2].rank:
                        temp.append(a[index1])
                        index1+=1
                    else:
                        temp.append(b[index2])
                        index2+=1
            if index1 < len(a):
                for i in a[index1:]:
                    temp.append(i)
            elif index2 < len(b):
                for i in b[index2:]:
                    temp.append(i)
        if  x == "tournamentsPlayed":
            while (index1<len(a)) and (index2<len(b)):
                    if a[index1].tournamentsPlayed <= b[index2].tournamentsPlayed:
                        temp.append(a[index1])
                        index1+=1
                    else:
                        temp.append(b[index2])
                        index2+=1
            if index1 < len(a):
                for i in a[index1:]:
                    temp.append(i)
            elif index2 < len(b):
                for i in b[index2:]:
                    temp.append(i)
        if  x == "country":
            while (index1<len(a)) and (index2<len(b)):
                    if a[index1].country <= b[index2].country:
                        temp.append(a[index1])
                        index1+=1
                    else:
                        temp.append(b[index2])
                        index2+=1
            if index1 < len(a):
                for i in a[index1:]:
                    temp.append(i)
            elif index2 < len(b):
                for i in b[index2:]:
                    temp.append(i)
        if  x == "racquetBrand":
            while (index1<len(a)) and (index2<len(b)):
                    if a[index1].racquetBrand <= b[index2].racquetBrand:
                        temp.append(a[index1])
                        index1+=1
                    else:
                        temp.append(b[index2])
                        index2+=1
            if index1 < len(a):
                for i in a[index1:]:
                    temp.append(i)
            elif index2 < len(b):
                for i in b[index2:]:
                    temp.append(i)
## each one takes each variable and seperates it into 2 indexes, puts the greater into temp list, and merges with other list till temp is ordered
##        for i in temp:
##            print(i.name)
        return temp
    #returns list temp(ordered)


t = tennisPro()
s = t.sort("name")#sorts self.players depending on varialbe put in quotes
for i in t:#for i in sorted list
    print(i)#prints answer

this is my code for a project i'm working on. however, when i run it, it says that t = tennisPro() is not defined. is there any way for me to fix this?

thanks in advance.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

That is because there is no class or function called tennisPro..

Also, you need to use code tags

[code]

#code here

[/code]

cheers

ok. well how exactly should i go about defining the list at the bottom? because that's what tennisPro is...

I have not also played much with objects in Python so I gave it a go, but using built in sort, as I could not understand the organisation of poster's code.

class TennisPro:
    fields=('name','rank','tournamentsPlayed', 'country', 'racquetBrand')
    def __init__ (self, name, rank, tournamentsPlayed, country, racquetBrand):
        self.values=[]
        self.name = name
        self.values.append(self.name)
        self.rank = int(rank)
        self.values.append(self.rank)        
        self.tournamentsPlayed = int(tournamentsPlayed)
        self.values.append(self.tournamentsPlayed)        
        self.country = country
        self.values.append(self.country)
        self.racquetBrand = racquetBrand
        self.values.append(self.racquetBrand)        

    def __str__(self):
        return ("TennisPro( Name: " + self.name +
                ", Rank: " + str(self.rank) +
                ", Tournaments: " + str(self.tournamentsPlayed) +
                ", Country: " + self.country +
                ", Racquet: " + self.racquetBrand +
                ' )'
                )

    def __iter__(self):
        for i in self.values:
            yield i

    def __getitem__(self,n):
        return self.values[n]

     def get(self,what=None):
        if what==None: return self.values
        elif what in self.fields: return eval('self.'+what)

class TennisProfessionals(TennisPro):
    
    def __init__(self,pro):
        self.professional = []
        self.sorting ='name'
        self.add(pro)

    def __str__(self):
        r='TennisProfessionals('
        for r in self.professional:
            r+=repr(r)+','
        return r[:-1]+')'

    def __iter__(self):
        for i in self.professional:
            yield i
            
    def __getitem__(self,n):
        return self.professional[n]

    ## takes list/tuple of professionals to add
    def add(self,pros):
        if not isinstance(pros,(list,tuple)):
            pros=list(pros) ## try to fix for single player added without list/tuple
        for i in pros:
            self.professional.append(i)
        self.sort(self.sorting)
    ## keeps the list sorted
        
    def sort(self,bywhat):
        self.sorting=bywhat
        self.professional.sort(key=lambda x:TennisPro.get(x,bywhat))

t = TennisProfessionals((
    TennisPro("Roger Federer", "1", "18", "SUI", "Wilson"),
    TennisPro("Novak Djokovic", "2", "21", "SRB", "Head"),
    TennisPro("Rafael Nadal", "3", "17", "ESP", "Babolat"),
    TennisPro("Juan Martin Del Potro", "4", "18", "ARG", "Wilson"),
    TennisPro("Andy Murray", "5", "17", "GRB", "Head"),
    TennisPro("Nikolay Davydenko", "6", "25", "RUS", "Prince"),
    TennisPro("Robin Soderling", "7", "25", "SWE", "Head"),
    TennisPro("Andy Roddick", "8", "20", "USA", "Babolat"),
    TennisPro("Fernando Verdasco", "9", "26", "ESP", "Tecnifibre"),
    TennisPro("Jo-Wilfried Tsonga", "10", "25", "FRA", "Wilson")
    )
                        )
# show order
print 'By ',t.sorting
for i in t: print(i)
print '='*60

# sorts self.players depending on variable put in quotes INPLACE, RETURNS NOTHING unlike sorted
t.sort("rank")

# show order
print 'By ',t.sorting
for i in t: print(i)

print
print '='*60
print 'First rank players details:'
for i in TennisPro.fields:
    print "%s: %s" % (i, t[0].get(i))
""" Output: 
By  name
TennisPro( Name: Andy Murray, Rank: 5, Tournaments: 17, Country: GRB, Racquet: Head )
TennisPro( Name: Andy Roddick, Rank: 8, Tournaments: 20, Country: USA, Racquet: Babolat )
TennisPro( Name: Fernando Verdasco, Rank: 9, Tournaments: 26, Country: ESP, Racquet: Tecnifibre )
TennisPro( Name: Jo-Wilfried Tsonga, Rank: 10, Tournaments: 25, Country: FRA, Racquet: Wilson )
TennisPro( Name: Juan Martin Del Potro, Rank: 4, Tournaments: 18, Country: ARG, Racquet: Wilson )
TennisPro( Name: Nikolay Davydenko, Rank: 6, Tournaments: 25, Country: RUS, Racquet: Prince )
TennisPro( Name: Novak Djokovic, Rank: 2, Tournaments: 21, Country: SRB, Racquet: Head )
TennisPro( Name: Rafael Nadal, Rank: 3, Tournaments: 17, Country: ESP, Racquet: Babolat )
TennisPro( Name: Robin Soderling, Rank: 7, Tournaments: 25, Country: SWE, Racquet: Head )
TennisPro( Name: Roger Federer, Rank: 1, Tournaments: 18, Country: SUI, Racquet: Wilson )
============================================================
By  rank
TennisPro( Name: Roger Federer, Rank: 1, Tournaments: 18, Country: SUI, Racquet: Wilson )
TennisPro( Name: Novak Djokovic, Rank: 2, Tournaments: 21, Country: SRB, Racquet: Head )
TennisPro( Name: Rafael Nadal, Rank: 3, Tournaments: 17, Country: ESP, Racquet: Babolat )
TennisPro( Name: Juan Martin Del Potro, Rank: 4, Tournaments: 18, Country: ARG, Racquet: Wilson )
TennisPro( Name: Andy Murray, Rank: 5, Tournaments: 17, Country: GRB, Racquet: Head )
TennisPro( Name: Nikolay Davydenko, Rank: 6, Tournaments: 25, Country: RUS, Racquet: Prince )
TennisPro( Name: Robin Soderling, Rank: 7, Tournaments: 25, Country: SWE, Racquet: Head )
TennisPro( Name: Andy Roddick, Rank: 8, Tournaments: 20, Country: USA, Racquet: Babolat )
TennisPro( Name: Fernando Verdasco, Rank: 9, Tournaments: 26, Country: ESP, Racquet: Tecnifibre )
TennisPro( Name: Jo-Wilfried Tsonga, Rank: 10, Tournaments: 25, Country: FRA, Racquet: Wilson )

============================================================
First rank players details:
name: Roger Federer
rank: 1
tournamentsPlayed: 18
country: SUI
racquetBrand: Wilson
>>> """

How did I manage? I would, at least, separate Family and First names for players, if I would organize the info myself.

Edited 6 Years Ago by pyTony: typo first, not fist

I know my post does not reply to main problem the poster has with disorganization (at least in my limited understanding).

But maybe he can get to right track and attack the problem himself. For example could all those ifs and fors in the merge sort really be needed?

I want to use this as an opportunity to study the basics of the subject myself also, and get some guidance for more experienced coders. I would really appreciate any comments on this tangent post of doing similar program object oriented way, so that I could get to understand the object oriented support in Python, even I have some basic theoretical understanding of the subject. Maybe anybody could private message me about the missing points and misunderstandings in my code, especially the down voter of previous post of mine.

This article has been dead for over six months. Start a new discussion instead.