Nobody Reads This

Arthas: This kingdom shall fall, and from the ashes shall arise a new order that will shake the very foundations of the world!

And I have no Illusions Otherwise.
  • Home
  • Images
  • Videos
  • About JamesR

My Python needs a Shaft

James Reynolds | August 28, 2008

You know you’re massively out of practice when it takes 63 lines of Python just to filter an XML file and convert what you get into a CSV file.

I think if I really tried I could get it down to 15-20 well formed lines … I start with something like standard XML trees and finish with:

1,Back_Only,Use_Job_Setting,Tabloid,
2,Different,Use_Job_Setting,Tabloid,
3,Back_Only,Use_Job_Setting,Statement,
4,Different,Use_Job_Setting,Statement,
5,Back_Only,Use_Job_Setting,Executive,
6,Different,Use_Job_Setting,Executive,
7,Back_Only,Use_Job_Setting,8_5_x_13,
8,Different,Use_Job_Setting,8_5_x_13,

The code is thus:

## Fireeye XML Test Plan to CSV converter.
## Scripted by James Reynolds

def main():
print "Filename (*.xml): "
fileName = raw_input()
fileHold = open(fileName, "rb")
fileContent = fileHold.readlines()
fileContent = relist(fileContent)
print "Read and Relisted"
writeTo = open(fileName.replace(".xml",".csv"), 'w')
print "Writing to " + fileName.replace(".xml",".csv")
for i in fileContent:
string = listToCSV(i)
string = remXML(string)
writeTo.write(string)
print "Done."

## Takes XML doc and filters out what we need returning list of tests
def relist(fileContent):
master = []
sub = []
adder = False
for element in fileContent:
if (element.find("Testcase TCNo") >= 0):
adder = True
elif (element.find("") >= 0):
adder = False
sub.append(element)
master.append(sub)
sub = []
if (adder == True):
sub.append(element)
return master

## Takes list and reformats to CSV
def listToCSV(varList):
csvString = ""
for element in varList:
if (csvString == ""):
element = element.replace("\r\n", "")
element = element.strip()
csvString = csvString + element
else:
element = element.replace("\r\n", ",")
element = element.strip()
csvString = csvString + element
csvString = csvString + "\r\n"
return csvString

## Removes XML tags
def remXML(XMLstring):
delete = False
newString = ""
for i in XMLstring:
if (i == "<"):
delete = True
if (i == ">"):
delete = False
if (delete == False and i != ">"):
newString = newString + i
return newString.replace(",,",",")
main()

You can probably see the though process … “I need it to do this. Oh, it won’t? Ok … This. That’s not quite right … What if I had this? Nearly there … And done. Next …”

Clearly not the best way to program … Knowing me I’ll sit down and rewrite it to be a lot better some time soon but … Meh.

Check out todaysbigfail.com, it’s run by another IP here.

Regards,
JamesR.

Wonders if his title is an obvious enough reference.

Comments
No Comments »
Categories
General Junk
Tags
Badly, Converter, Python, Terrible Coding, XML to CSV
Comments rss Comments rss
Trackback Trackback

Ups, You did it again

James Reynolds | August 28, 2008
UPS, guranteed to piss off the working class

UPS, guranteed to piss off the working class

I find it quite funny that UPS tries to deliver a package three times in one working week – I ask you, do they expect us to randomly be in one midday but not the next? I’m beginning to wonder exactly where in the scheme of good business sense delivery companies sit …

How many millions (tens or hundreds?) of people around the world work 9-5~ and can’t be in for the mail? Where is the option to have it delivered on a Saturday or a Sunday? It’s that bloody simple! Or perhaps have it delivered earlier in the morning like 5-7 but that doesn’t help some people…

Consider that if we opt to pick it up, they don’t even man the depots on weekends! Jesus tap dancing Mother Theresa’s Ghost Puppy! Who came up with that fucking business plan?

It’s a good thing the company I’m working for are decent human beings and let you deliver to the office …

… Le sigh. Anyway, you might have noticed I have been posting less and this is primarily to do with being in training or not at a PC for the last week and a bit but if I’m honest I generally don’t want to write in the precious little time I have to myself between chores, work and sleep. I am sure this will return to normal once we have a net connection and I pick up my new PC at the end of this week …

In the mean time however you can all do me a favour and head over to Resuri’s (Leslie’s) blog and comment, we were all new to blogging one time or another and it’s always good to have some encouragement at the start. I’d recommend Mark’s blog, but I think she’s already beating him on Posts per week :P

Regards,
James
R.
Systems Software, Hardware Integration.

Quietly proud of the article title.

Comments
No Comments »
Categories
General Junk
Tags
Art Manga Links, Crabcakes, Cunt Features, Jewfro Extraganza, Leslie, Mark is a Jew, Resuri, UPS
Comments rss Comments rss
Trackback Trackback

Tweet

  • Note to world: Mc Lars is ftw 8 hours ago

Blogroll

  • Beagle2’s Brain
  • Laputan Machine
  • Lost Caykes
  • Rasberry lOlly’s Blog
  • Resuri’s Blog
  • Twitter (JamesR87)
  • WS@Vlogging
  • Yet Another Blog

Misc

  • Bunny
  • Ctrl+Alt+Delete
  • Cyanide & Happiness
  • Dinosaur Comics
  • Penny Arcade
  • Questionable Content
  • VGCats
  • XboxElite.co.uk
  • XKCD

WoW Blogs

  • Gnomeaggedon
  • MMO Champ
  • WoW.com

Gamercard

Statistics

  • Total Stats
    • 353 Posts
    • 466 Comments

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Cats

Arcs

Calendar

August 2008
M T W T F S S
« Jul   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

Akismet

9,600 spam comments
blocked by
Akismet
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox