![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() |
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 TIt 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? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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 )
__________________
Waterloo's Canadian Computing Competition (CCC) - Stage 2 Problems, Solutions, and Test Data Last edited by Sane; Jul 3rd, 2005 at 12:05 PM. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4
![]() |
Yey, go me!!
![]()
__________________
while me is alive: make(life,simple) |
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
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 productprint 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. |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
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, ) |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
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 )
__________________
Waterloo's Canadian Computing Competition (CCC) - Stage 2 Problems, Solutions, and Test Data Last edited by Sane; Jul 3rd, 2005 at 5:25 PM. |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Feb 2005
Posts: 54
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
I already gave the EXACT example why I can't use lists. The pygame function accepts the layers as a tuple.
|
|
|
|
|
|
#9 |
|
Programmer
Join Date: Feb 2005
Posts: 54
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#10 |
|
Programming Guru
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|