Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 3rd, 2005, 10:47 AM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Tuple Appending

Instead of searching for a way to add items to a tuple, I innovated my own. Hehe. :p

def Tuple_Append(tuple, item):
    
    #Handle exceptions of 0 or 1 items in Tuple
    if tuple==():return'Error: Tuple is empty'
    if str(tuple)[0]!='(':return(tuple,item) 

    #Create Code For 'T'
    elem = 'T=(tuple[0]'
    for a in range(1, len(tuple)):
        elem+=',tuple[%d]'%(a)
    elem+=',item)'

    #Execute Code
    exec elem

    #Return Product
    return T

It works by first assigning all the variable names to the tuple, then executes that code to reference the values. Followed by returning the newly appended tuple.

So:

print Tuple_Append( (1, 2), 3 )

Would return the tuple:

(1, 2, 3)

It's useful for situations like so in my RPG:

        exec 'image = Graphics.%s_%s_%s'%(character, Pos[0], Pos[1])
        draw_all(screen,  Tuple_Append(layers, image))
        draw_windows(screen, '', 'area', Graphics)

        time.sleep(speed)

Which adds a character to the screen, without destroying the initial layers.

Was there already a command for appending to a tuple, or is that what lists are for?
Sane is offline   Reply With Quote
Old Jul 3rd, 2005, 11:45 AM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Since it won't let me edit:

Here it is using my newfound ability of 'try' and 'except' thanks to SaturN who taught it to me.

def Tuple_Append(tuple, item):

    #Create Code For 'T'
    elem = 'T = (tuple[0]'
    try:
        for a in range( 1, len( tuple ) ):
            elem += ',tuple[%d]'%( a )
        elem += ',item)'
     
    #Handle exception of only one item in Tuple
    except:
        return( tuple, item )
        
    #Execute Code
    try:
        exec elem

    #Handle exception of an empty Tuple
    except:
        return item

    #Return Product
    return T

print Tuple_Append( (), 1 )
print Tuple_Append( (1), 2 )
print Tuple_Append( (1, 2), 3 )

Last edited by Sane; Jul 3rd, 2005 at 12:05 PM.
Sane is offline   Reply With Quote
Old Jul 3rd, 2005, 12:03 PM   #3
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
Yey, go me!!
__________________
while me is alive:
	make(life,simple)
SaturN is offline   Reply With Quote
Old Jul 3rd, 2005, 12:32 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
I've gone except and try crazy!!!

def Tuple_Append(tuple, item):

    try:
        code = 'product = (tuple[0]'
        for items in range( 1, len( tuple ) ):
            code += ',tuple[%d]'%( items )
        try:
            for items in range( len( item ) ):
                code += ',item[%d]'%( items )
            code += ')'
        except: code += ',item)'    
    except:
        code = 'product = (tuple'
        try:
            for items in range( len( item ) ):
                code += ',item[%d]'%( items )
            code += ')'
        except: code += ',item)'
       
    try:exec code
    except:return item

    return product

print Tuple_Append( (), (1, 5, 10) )
print Tuple_Append( (1, 5, 10), () )
print Tuple_Append( (), () )
print Tuple_Append( (7), (7) )
print Tuple_Append( (42), (24) )
print Tuple_Append( (1, 2), (3, 2, 1) )

Thanks to except and try, you can append as many items as you want to as many items as you want, including none and one!! lol.
Sane is offline   Reply With Quote
Old Jul 3rd, 2005, 1:17 PM   #5
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Bear in mind that having a try block is a very cheap and efficient process, but catching an exception is not. If you're going to be working with data where you'll be getting many exceptions, then try and avoid them as much as you can to begin with (if..else). Utilize them for full-proofing your applications, but don't go crazy with them

Oh, and a simpler way to "append" to a tuple:
def addToTuple(tup, item):
    return tup + (item, )
Cerulean is offline   Reply With Quote
Old Jul 3rd, 2005, 5:02 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Well I went crazy with it. lol.

And here's some more stuff. I think it's fool-proof now. And I didn't know it could be done that easily. Wow. I thought the only way was to create the code for it in a string, then execute that code.

def Tuple_Append(tuple, item):

    try:
        #Handle Exception of Accidently Splitting A String From "tuple"
        if str(type(tuple)) == "<type 'str'>":
            code = 'product = (tuple'

        #Write Code Handling The Original Tuple
        else:
            code = 'product = (tuple[0]'
            for items in range( 1, len( tuple ) ):
                code += ',tuple[%d]'%( items )

        #Handle Exception of Accidently Splitting A String From "item"
        try:
            if str(type(item)) == "<type 'str'>":
                code += ',item)'

            #Write Code Handling The Items
            else:
                for items in range( len( item ) ):
                    code += ',item[%d]'%( items )
                code += ')'

        #Handle Exception of Only one Item From "item"
        except: code += ',item)'

    #Handle Exception of Only one Item From "tuple"
    except:
        code = 'product = (tuple'
        try:
            for items in range( len( item ) ):
                code += ',item[%d]'%( items )
            code += ')'

        #Still A Possibility of Only one Item From "item"
        except: code += ',item)'

    #Write "code" To "product"
    try:exec code

    #In Case There Was No Tuple
    except:return item

    return product

def Tuple_Join(tuple, link=''):

    #Convert to a List
    product = []
    try:
        for a in range(len(tuple)):
            product.append(str(tuple[a]))

    #Handle Exception of Only one Item From "tuple"
    except:
        return tuple

    #Join the List with the Given Link
    return str(link).join(product)

OldTuple = ('turkey', 'zebra')
NewTuple = Tuple_Append( OldTuple , 'pig' )
JoinedTuple = Tuple_Join( NewTuple, ', ' )
OtherTuple = Tuple_Join( NewTuple )

print OldTuple
print NewTuple
print JoinedTuple
print OtherTuple

print Tuple_Append( (2) , () )
print Tuple_Append( () , 4 )
print Tuple_Append( 5, 5 )

Last edited by Sane; Jul 3rd, 2005 at 5:25 PM.
Sane is offline   Reply With Quote
Old Jul 5th, 2005, 2:03 PM   #7
Moldz
Programmer
 
Moldz's Avatar
 
Join Date: Feb 2005
Posts: 54
Rep Power: 4 Moldz is on a distinguished road
Why not just use a list? Tuples don't have append() functions because you are not supposed to change a tuple once it is created. In other words, tuples are "immutable" and for that reason they are more efficient than lists.

If you are changing your tuples, that's a good indication that they should be lists, which already have append() and extend() functions.
Moldz is offline   Reply With Quote
Old Jul 5th, 2005, 2:18 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
I already gave the EXACT example why I can't use lists. The pygame function accepts the layers as a tuple.
Sane is offline   Reply With Quote
Old Jul 5th, 2005, 3:55 PM   #9
Moldz
Programmer
 
Moldz's Avatar
 
Join Date: Feb 2005
Posts: 54
Rep Power: 4 Moldz is on a distinguished road
Excuse me for missing the EXACT EXAMPLE from the hundreds of lines of code in this thread.

You could use a list to store all the layers then use the tuple() function to convert it to a tuple before you pass it to pygame. Or was this also covered by an EXACT example?
Moldz is offline   Reply With Quote
Old Jul 5th, 2005, 6:42 PM   #10
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Sorry for capitilizing. I feel that's a problem with Internet, you have to assume the tone of the person you are reading from. Anyways. Ya, I didn't know there was a tuple() function. Thanks I guess.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC