Page 1 of 1

Want to see something really ugly?

Posted: Sun Jan 27, 2013 11:27 am
by Cayle
Using VBA to generate python code from Excel sheets makes for some disgusting spaghetti...

Code: Select all

    spellBookTemplateLibraryHeaderList(1,1) = "def createSpellBookInInventory(templateID, ownerKey):" & vbCrLf & "    returnKey = None" & vbCrLf
	spellBookTemplateLibraryHeaderList(1,2) = "    try:"
	spellBookTemplateLibraryHeaderList(1,2) = spellBookTemplateLibraryHeaderList(1,2) & vbCrLf & "        owner = ownerKey.get()"
	spellBookTemplateLibraryHeaderList(1,2) = spellBookTemplateLibraryHeaderList(1,2) & vbCrLf & "        owner.inventory.contents.append(newEntity.key)"
	spellBookTemplateLibraryHeaderList(1,2) = spellBookTemplateLibraryHeaderList(1,2) & vbCrLf & "        newEntity.onAcquireItem()" 'The item fires its onAquire event
	spellBookTemplateLibraryHeaderList(1,2) = spellBookTemplateLibraryHeaderList(1,2) & vbCrLf & "        owner.onAcquireItem()"     'The new owner fires its onAquire event
	spellBookTemplateLibraryHeaderList(1,2) = spellBookTemplateLibraryHeaderList(1,2) & vbCrLf & "    except AttributeError:"    
	spellBookTemplateLibraryHeaderList(1,2) = spellBookTemplateLibraryHeaderList(1,2) & vbCrLf & "        raise Exceptions.NoSuchEntityError('There is no datastore entity associated with key' %ownerKey)" & vbCrLf 
    spellBookTemplateLibraryHeaderList(2,1) = "def createSpellBookAtLocation(templateID, locLatitude, locLongitude):" & vbCrLf & "    returnKey = None" & vbCrLf
    spellBookTemplateLibraryHeaderList(2,1) = spellBookTemplateLibraryHeaderList(2,1) & "    if locLongitude is None:" & vbCrLf
    spellBookTemplateLibraryHeaderList(2,1) = spellBookTemplateLibraryHeaderList(2,1) & "        raise Exceptions.LocationlessEntity('SpellBook %s requires a longitude when being created' %templateID)" & vbCrLf
    spellBookTemplateLibraryHeaderList(2,1) = spellBookTemplateLibraryHeaderList(2,1) & "    if locLatitude is None:" & vbCrLf
    spellBookTemplateLibraryHeaderList(2,1) = spellBookTemplateLibraryHeaderList(2,1) & "        raise Exceptions.LocationlessEntity('SpellBook %s requires a latitude when being created' %templateID)" & vbCrLf
	spellBookTemplateLibraryHeaderList(2,3) = ", locLatitude=locLatitude, locLongitude=locLongitude"
And yeah, I've got some long variable names. I like them to be self descriptive, so that I understand what they mean when I go back and look at them a year or two later.

Re: Want to see something really ugly?

Posted: Sun Jan 27, 2013 4:07 pm
by Jackolantern
Wow, VBA generating Python? haha :lol:

Re: Want to see something really ugly?

Posted: Sun Jan 27, 2013 6:27 pm
by Cayle
Yeah, I'll post a demo video of the content editor for Magicus Occultus in a couple of weeks. It uses something like 60 excel sheets and 3-4k lines of VBA to generate some of the python modules. :P I'm finishing up removing some nastiness and making sure everything works. When I've got valid exports again (they've been broken since I switched from DB to NDB), I'll wrap everything up into a nice little VSTO app.

Using VBA is like going on an archaeological dig. It really feels like an archaic language; especially when compared to a duck typed, multiple inheritance language like python. It even makes a distinction between subroutines and functions!!!

Re: Want to see something really ugly?

Posted: Mon Jan 28, 2013 2:38 pm
by hallsofvallhalla
wow now this is interesting. Python and VB sleeping together. Wonder what kind of baby this produces?