<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Ryan Robitaille</title>
	
	<link>http://ryrobes.com</link>
	<description>Business Technologist, Data Artist, Problem Solver</description>
	<lastBuildDate>Mon, 22 Apr 2013 19:47:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Ryrobes" /><feedburner:info uri="ryrobes" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Facts, Stories, Conveyance: Postmortem “Data Viz Advice” from a famous American street photographer</title>
		<link>http://feedproxy.google.com/~r/Ryrobes/~3/xhz7HF0-Tiw/</link>
		<comments>http://ryrobes.com/data-storytelling/facts-stories-conveyance-postmortem-data-viz-advice-from-a-famous-american-street-photographer/#comments</comments>
		<pubDate>Mon, 22 Apr 2013 19:47:28 +0000</pubDate>
		<dc:creator>Ry</dc:creator>
				<category><![CDATA[Data Storytelling]]></category>

		<guid isPermaLink="false">http://ryrobes.com/?p=43064</guid>
		<description><![CDATA[<p>Just got back from SF - one of the things we did was visit the Museum of Modern Art - the building is a wonder in itself, but obviously the museum hosts countless photographs and art installations from many famous (and not so famous) artists. I had no idea I'd find inspiration that can be DIRECTLY applied to modern data visualization practices. Advice that doesn't get followed as much as it should...
</p><p>The post <a href="http://ryrobes.com/data-storytelling/facts-stories-conveyance-postmortem-data-viz-advice-from-a-famous-american-street-photographer/">Facts, Stories, Conveyance: Postmortem &#8220;Data Viz Advice&#8221; from a famous American street photographer</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p>]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://fixxer.ryrobes.com/wp-content/uploads/2013/04/winogr-150x150.jpg" width="240" />
		</p><p><a href="http://fixxer.ryrobes.com/wp-content/uploads/2013/04/902958_10151427976077763_1704206675_o.jpg"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2013/04/902958_10151427976077763_1704206675_o-150x150.jpg" alt="902958_10151427976077763_1704206675_o" width="150" height="150" class="alignleft size-thumbnail wp-image-43113" /></a><span class="dropcap">I</span><!--/.dropcap--> just got back from spending the last week in the beautiful <a href="http://instagram.com/p/YLeUSkSjFo/" target="_blank">San Francisco</a> Bay area. If you've never been, seriously, go there. As an obnoxious East Coast / New England guy, I thought I had seen it all. Nope.</p>
<p>Anyways, one of the things we did was visit the <a href="http://www.sfmoma.org/" target="_blank">SF Museum of Modern Art</a> - the building is a wonder in itself, but obviously the museum hosts countless photographs and art installations from many famous <em>(and not so famous)</em> artists. Honestly, I had no expectations. I was on vacation after all - just doing a fun "touristy" activity between <a href="http://instagram.com/p/YRDPzpSjJW/" target="_blank">rounds of Grey Goose Oysters shooters</a>, <a href="http://instagram.com/p/YaxbGqyjBl/" target="_blank">eating sea creatures</a> and pints of AnchorSteam.</p>
<h4>I had no idea I'd find inspiration that can be DIRECTLY applied to modern data visualization practices. Advice that doesn't get followed as much as it should...</h4>
<p>The exhibit - a <a href="http://www.sfmoma.org/about/press/press_exhibitions/releases/920" target="_blank">complete retrospective of the working life and career of Garry Winogrand</a> (1920s - 1984), including many photos that were never published <em>(he left behind 6.5k rolls of undeveloped film when he passed at the age of 56)</em>. </p>
<p>Garry was a <a href="http://en.wikipedia.org/wiki/Garry_Winogrand" target="_blank">famous street-documentary photographer</a> mostly known for his NYC shots in the 50s-early 80s. His many striking photos told a story through the faces and bodies of it's living subjects. Short on hard "data" - but miles long on human interest, wonder, and conveyance. Stories of frozen emotions and reactions, but without a full sense of context (aka interpretative art; mystery).</p>
<p>I came upon a particular photo in the "Down in the Bronx" section of the exhibit that caught my eye. It was an assault scene from the 50s. Murder possibly. There was a well dressed man on the ground, you can see only his well-made shoes and an upturned hat on the ground - near a large pool of blood. You can see people rushing around him, a nun leaning in and praying, and a little boy stading in the background looking on with eyes agape. Clearly "something" had happened just moments earlier.</p>
<p>On the display card that accompanied the photo is where I found my connection. It described how Garry always gravitated toward this type of photo journalism instead of blatantly trying to capture the <i>What</i> or <i>Why</i> for the sake of written narration. This quote from him was below it.</p>
<div align="center">
<h2>"There is nothing more mysterious<br/>as a fact clearly described"</h2>
<h4> - Garry Winogrand</h4>
</div>
<p><a href="http://fixxer.ryrobes.com/wp-content/uploads/2013/04/winogr.jpg"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2013/04/winogr-150x150.jpg" alt="winogr" width="150" height="150" class="alignright size-thumbnail wp-image-43160" /></a><b>Boom</b>. I immediately tapped this quote out on my phone. </p>
<p>It seems that in all the academic and ideological viz wars we've seen lately - people sometimes forget the importance of storytelling and conveyance. Showing the results without the story is next to useless. </p>
<p>Our industry is about action and communication, not mystery. </p>
<h4>The facts mean nothing if the reader can't understand it, conceptualize it, or (even worse) be intrigued enough to <strong>even give a shit</strong> about it. </h4>
<p>The reader might be a literal "reader" of a magazine, newspaper, or online periodical - but really in our case the "reader" is YOUR FUCKING BOSS and YOUR CUSTOMERS. It's essential that they not only know the <strong>facts</strong>, but the <strong>breadcrumbs</strong> that led to such facts. <strong>Seeing is understanding</strong>. You can't make things <b>actionable</b> if they can't be understood (even more important is self-explanatory interactive visualizations).</p>
<h4>In any useful data viz - calculations & insightful analysis is good, facts are good, in-depth data is good, but...</h4>
<div align="center">
<h2 style="font-size:64px;">CONTEXT IS GOD.</h2>
</div>
<p>It's not only about WHAT you are showing, but about the why, how, and the elusive 'what do we do next'. Data is good, geeky math is good, but if it can't be framed in a way that biz or end-user peeps (or the public) can <b>understand</b> it's just shred fodder that you can use in little Suzi's hamster cage.</p>
<p>Getting and interpreting the data is one part - CONVEYING it is another altogether.<br />
(again - not to get into a <strong>Pure Math / Science vs Functional "Art"</strong> war - but it bodes discussion)</p>
<p>What do you guys think?</p>
<p>The post <a href="http://ryrobes.com/data-storytelling/facts-stories-conveyance-postmortem-data-viz-advice-from-a-famous-american-street-photographer/">Facts, Stories, Conveyance: Postmortem &#8220;Data Viz Advice&#8221; from a famous American street photographer</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Ryrobes?a=xhz7HF0-Tiw:WbeEeAjYY2U:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=xhz7HF0-Tiw:WbeEeAjYY2U:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=xhz7HF0-Tiw:WbeEeAjYY2U:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=xhz7HF0-Tiw:WbeEeAjYY2U:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=xhz7HF0-Tiw:WbeEeAjYY2U:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=xhz7HF0-Tiw:WbeEeAjYY2U:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Ryrobes/~4/xhz7HF0-Tiw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ryrobes.com/data-storytelling/facts-stories-conveyance-postmortem-data-viz-advice-from-a-famous-american-street-photographer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://ryrobes.com/data-storytelling/facts-stories-conveyance-postmortem-data-viz-advice-from-a-famous-american-street-photographer/</feedburner:origLink></item>
		<item>
		<title>Going Yard: Using MLB data and Tableau 8 to “re-imagine” Homeruns in PetCo Park – “The Business Case”</title>
		<link>http://feedproxy.google.com/~r/Ryrobes/~3/sYm5aZFLR-4/</link>
		<comments>http://ryrobes.com/tableau/going-yard-using-mlb-data-and-tableau-8-to-re-imagine-homeruns-in-petco-park-the-business-case/#comments</comments>
		<pubDate>Tue, 02 Apr 2013 01:10:00 +0000</pubDate>
		<dc:creator>Ry</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[Tableau]]></category>
		<category><![CDATA[Digging for Data]]></category>
		<category><![CDATA[gameday]]></category>
		<category><![CDATA[MLB]]></category>
		<category><![CDATA[PetCo Park]]></category>
		<category><![CDATA[San Diego Padres]]></category>
		<category><![CDATA[tableau 8]]></category>

		<guid isPermaLink="false">http://ryrobes.com/?p=42935</guid>
		<description><![CDATA[<p>Answering life's Big Questions with Data Viz: The San Diego Padres are changing the wall dimensions for their home stadium, PetCo Park, for the 2013 MLB season. In a notoriously large "pitchers park", how might this change affect the amount of Homeruns, the Padres record, and (possibly) ticket sales?</p><p>The post <a href="http://ryrobes.com/tableau/going-yard-using-mlb-data-and-tableau-8-to-re-imagine-homeruns-in-petco-park-the-business-case/">Going Yard: Using MLB data and Tableau 8 to &#8220;re-imagine&#8221; Homeruns in PetCo Park &#8211; &#8220;The Business Case&#8221;</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p>]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://fixxer.ryrobes.com/wp-content/uploads/2013/04/padres-hat-tableau2.jpg" width="240" />
		</p><p><img src="http://fixxer.ryrobes.com/wp-content/uploads/2013/04/padres-hat-tableau2-150x150.jpg" alt="padres-hat-tableau2" width="150" height="150" class="alignleft size-thumbnail wp-image-43039" />Greetings, it's been awhile since I published anything - my apologies! I've been hella busy this year, but <i>finally</i> have dedicated some time to a mini-project I've been working on for some time. This time, it's a baseball viz / park issue that I can hopefully shed some (theoretical) light on. IN an effort to stop writing massive blog posts - consider this post a intro - more like the "Business Case", the next one will be "Technical HOW, WHY, WHAT".</p>
<p>Anyways, on with the premise...</p>
<div class="woo-sc-box normal  rounded full">
<h2>THE QUESTION:</h2>
<p>The <a href="http://sandiego.padres.mlb.com/index.jsp?c_id=sd" target="_blank">San Diego Padres</a> are changing the wall dimensions for their home stadium, <a href="http://www.petcoparkevents.com/" target="_blank">PetCo Park</a>, for the 2013 MLB season. In a notoriously large "pitchers park", how might this change affect the amount of Homeruns, the Padres record, and (possibly) ticket sales?<br />
</div>
<div class="woo-sc-box normal  rounded full">
<h2>MY APPROACH:</h2>
<p>Using freely available data from the MLB Gameday app (via <a href="http://gd2.mlb.com/components/game/mlb/year_2013/month_04/day_01/gid_2013_04_01_bosmlb_nyamlb_1/" target="_blank">their awesome XML datastore</a>) can we map out the hitcharts for the past 3-5 years (by mapping Gameday x,y coordinates to geocoded coordinates) and see what effect a closer (and lower) wall would have had on THOSE seasons. Changing Outs and Base Hits into HRs (with associated RBIs, if men are on base) If we can try to understand with MIGHT have happened, we can shed some light on what this season might bring.</div>
<h4>Tools Used:</h4>
<div class="shortcode-unorderedlist bullet">
<ul>
<li><b>Python</b> = Data Harvesting (MLB, Weather, Salaries)</li>
<li><b>SQL</b> = Data Storage (SQL Server 2012 Azure cloud DB)</li>
<li><b>Tableau</b> = Data Presentation / Visualization / Analysis</li>
<li><b>Photoshop</b> = Designing Layout (mock-up wireframes) + 'Design' Sections of the Viz</li>
<li><b>Total Time Taken</b> = <i>Lots</i>. :)</li>
</ul>
<p></div>

<div class="woo-sc-box normal  rounded full">
<h2>THE RESULTS:</h2>
<p><b>More Homeruns?</b> Yes, as many as 50-100 MORE per year.<br />
<b>More Padres Wins?</b> No (the visiting team will probably gain MORE HRs).<br />
<b>More Ticket Sales?</b> Possibly (HRs make for a more 'exciting' game to watch, and may be a bigger draw for fans than wins - <a href="http://umresearchboard.org/resources/davis/Baseball_Attendance_Winning.pdf" target="_blank">but the jury</a> <a href="http://www.hardballtimes.com/main/article/how-are-wins-attendance-and-payroll-all-related/" target="_blank">is out on that</a>).</p>
<p><b>Also, notice that attendance seems to be declining regardless of wins AND plummeting average ticket prices...</b></p>
<p><i>Take this with a grain of salt, Baseball is a game that can change on a dime. I'm only making estimations based on that data available.</i></p>
<p>But don't take my word for it - <a href="http://ryrobes.com/visual-analytics-and-data-porn/mapping-san-diego-padres-hits-petco-park-changes-tableau-8/" title="Mapping San Diego Padres Hits &amp; PetCo Park Changes | Tableau 8">check out the viz in all it's glory by clicking on the image below</a>.</div>
<div id="attachment_42966" class="wp-caption aligncenter" style="width: 510px"><a href="http://ryrobes.com/visual-analytics-and-data-porn/mapping-san-diego-padres-hits-petco-park-changes-tableau-8/"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2013/04/padres-long-image.jpg" alt="Taming PetCo Park" width="500" height="1142" class="size-full wp-image-42966" /></a><p class="wp-caption-text"><b>"Taming PetCo Park"</b>. Click for full interactive version.</p></div>
<div class="woo-sc-divider"></div>
<p>Gird your loins. Next time, I'll have all the gory details - Python, SQL scripts and all....</p>
<p>The post <a href="http://ryrobes.com/tableau/going-yard-using-mlb-data-and-tableau-8-to-re-imagine-homeruns-in-petco-park-the-business-case/">Going Yard: Using MLB data and Tableau 8 to &#8220;re-imagine&#8221; Homeruns in PetCo Park &#8211; &#8220;The Business Case&#8221;</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Ryrobes?a=sYm5aZFLR-4:PPrLwB6Rh40:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=sYm5aZFLR-4:PPrLwB6Rh40:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=sYm5aZFLR-4:PPrLwB6Rh40:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=sYm5aZFLR-4:PPrLwB6Rh40:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=sYm5aZFLR-4:PPrLwB6Rh40:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=sYm5aZFLR-4:PPrLwB6Rh40:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Ryrobes/~4/sYm5aZFLR-4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ryrobes.com/tableau/going-yard-using-mlb-data-and-tableau-8-to-re-imagine-homeruns-in-petco-park-the-business-case/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://ryrobes.com/tableau/going-yard-using-mlb-data-and-tableau-8-to-re-imagine-homeruns-in-petco-park-the-business-case/</feedburner:origLink></item>
		<item>
		<title>The Anatomy of a Tableau 8 Dashboard (or a “floating tile grid autopsy”)</title>
		<link>http://feedproxy.google.com/~r/Ryrobes/~3/ORldj6CgjrA/</link>
		<comments>http://ryrobes.com/tableau/the-anatomy-of-a-tableau-8-dashboard-or-a-floating-tile-grid-autopsy/#comments</comments>
		<pubDate>Fri, 28 Dec 2012 00:58:21 +0000</pubDate>
		<dc:creator>Ry</dc:creator>
				<category><![CDATA[Tableau]]></category>
		<category><![CDATA[dashboard]]></category>
		<category><![CDATA[information design]]></category>
		<category><![CDATA[kraken]]></category>
		<category><![CDATA[tableau public]]></category>

		<guid isPermaLink="false">http://ryrobes.com/?p=42796</guid>
		<description><![CDATA[<p>With the release of Tableau 8 just around the corner, and the Tableau 8 Public servers in a (very) limited release beta – I figured that it was good a time as any to see what I could come up with using (a couple) of the new features. Specifically the “floating tile” option for dashboards...</p><p>The post <a href="http://ryrobes.com/tableau/the-anatomy-of-a-tableau-8-dashboard-or-a-floating-tile-grid-autopsy/">The Anatomy of a Tableau 8 Dashboard (or a &#8220;floating tile grid autopsy&#8221;)</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p>]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/yeaser1.jpg" width="240" />
		</p><p>With the release of Tableau 8 just around the corner, and the Tableau 8 Public servers in a (very) limited release beta - I figured that it was good a time as any to see what I could come up with using (a couple) of the new features. Specifically the "floating tile" option for dashboard creation (one that I'm particularly jazzed about, as you'll see)...</p>
<h3>"Floating Tiles" = A Game Changer</h3>
<p>Previously in 7 (and earlier) you were restricted to putting dashboard "parts" (for lack of a better term - think worksheets, text, and images) into a editable "grid" pattern. While this allowed some cool designs if you wanted to get creative - you could really only go so far. Now since that has been lifted (with the added inclusion of allowing overlapping dashboard parts) we are going to see some really cool designs.</p>
<div align="center"><a href="http://publicbeta.tableausoftware.com/views/ryrobesgoogleanalyticsdash/googleanalyticsdash?:embed=y&amp;:display_count=y" target="_blank"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/yeaser.jpg" alt="yeaser" width="450" height="381" class="aligncenter size-full wp-image-42806" style="border:0px;" alt="Click here to open the viz in a new window"  title="Click here to open the viz in a new window" /></a>
</div>
<p><br/>I wanted to visually illustrate how the new dashboard layout rules radically change the dash construction and design in Tableau 8 - so I started up Photoshop to give this particular viz a "grid autopsy". Anyone who is familiar with Tableau in any way should be able to see quickly what we've got going on here.</p>
<p>This dashboard viz is literally made up of 21 separate Tableau worksheets, plus several static images and static text boxes. </p>
<div align="center">
<a href="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/viz-anatomy.jpg"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/viz-anatomy.jpg" alt="viz-anatomy" width="500" height="490" class="aligncenter size-medium wp-image-42804"  style="border:0px;" /></a>
</div>
<p></br></p>
<ul>
<li><span class="shortcode-typography" style="font-family: Arial, Helvetica, sans-serif; font-size: 18px; color: blue;">Blue - Tableau Worksheet</span></li>
<li><span class="shortcode-typography" style="font-family: Arial, Helvetica, sans-serif; font-size: 18px; color: red;">Red - Static Image</span></li>
<li><span class="shortcode-typography" style="font-family: Arial, Helvetica, sans-serif; font-size: 18px; color: green;">Green - Static Text</span></li>
</ul>
<p>Also, the ability to move tiles pixel by pixel and size them pixel makes it possible without contemplating murder.</p>
<p><em>For the record, SOME of this would have been somewhat possible with 7, but it would have been a total trauma to keep things aligned properly - and generally not recommended for anyone who wants continued grip on their sanity.</em></p>
<div class="woo-sc-box info  rounded full">NOTE: Yes, I know that the "trending" green up triangle should be a worksheet with a shape calculation - but I got really lazy</div>
<div class="woo-sc-box tick  rounded full">UPDATE: I'm also quite fond of the "date range" text worksheet. It's not groundbreaking or anything, but having a calculated field dynamically show some absolute filter ranges means that when filtering you can have text updated to "re-represent" what the user is looking at WITHOUT counting on the filter dialogs themselves...</p>
<p><strong>Ex.</strong></p>
<p><a href="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/date-range-wksht-string.jpg"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/date-range-wksht-string.jpg" alt="date-range-wksht-string" width="440" height="305" class="aligncenter size-medium wp-image-42854" /></a></p>
</div>
<p>To be continued... :) Really looking forward to what everyone else comes up with too!</p>
<p>The post <a href="http://ryrobes.com/tableau/the-anatomy-of-a-tableau-8-dashboard-or-a-floating-tile-grid-autopsy/">The Anatomy of a Tableau 8 Dashboard (or a &#8220;floating tile grid autopsy&#8221;)</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Ryrobes?a=ORldj6CgjrA:3J36mkdKdjY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=ORldj6CgjrA:3J36mkdKdjY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=ORldj6CgjrA:3J36mkdKdjY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=ORldj6CgjrA:3J36mkdKdjY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=ORldj6CgjrA:3J36mkdKdjY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=ORldj6CgjrA:3J36mkdKdjY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Ryrobes/~4/ORldj6CgjrA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ryrobes.com/tableau/the-anatomy-of-a-tableau-8-dashboard-or-a-floating-tile-grid-autopsy/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://ryrobes.com/tableau/the-anatomy-of-a-tableau-8-dashboard-or-a-floating-tile-grid-autopsy/</feedburner:origLink></item>
		<item>
		<title>Quick &amp; Dirty Address Geocoding and Formatting with Google Maps API</title>
		<link>http://feedproxy.google.com/~r/Ryrobes/~3/J78rYclsN_0/</link>
		<comments>http://ryrobes.com/python/quick-dirty-address-geocoding-and-formatting-with-google-maps-api/#comments</comments>
		<pubDate>Tue, 11 Dec 2012 00:19:32 +0000</pubDate>
		<dc:creator>Ry</dc:creator>
				<category><![CDATA[Cut-n-Paste Code]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[geocode]]></category>
		<category><![CDATA[geocoding]]></category>
		<category><![CDATA[google maps]]></category>

		<guid isPermaLink="false">http://ryrobes.com/?p=42739</guid>
		<description><![CDATA[<p>Howdy. Cheers to all ya'll down there in internet land. I got in a conversation earlier today regarding geocoding addresses in data-sets - it's a pretty common thing, and I've done it numerous times for a WIDE variety of data-points (Bigfoot, Sex Offenders, Concert Venues, etc.), so I figured hell, I'll clean it up and offer it to the Google gods. Maybe someone will find it useful...</p><p>The post <a href="http://ryrobes.com/python/quick-dirty-address-geocoding-and-formatting-with-google-maps-api/">Quick &#038; Dirty Address Geocoding and Formatting with Google Maps API</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p>]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/Full4.jpg" width="240" />
		</p><p><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/Vacation.jpg" alt="Why aren't we flying? Because getting there is half the fun. You know that." title="Why aren't we flying? Because getting there is half the fun. You know that." width="500" align="center" /></p>
<p>Howdy. Cheers to all ya'll down there in internet land. I got in a conversation earlier today regarding geocoding addresses in data-sets - it's a pretty common thing, and I've done it numerous times for a WIDE variety of data-points (Bigfoot, Sex Offenders, Concert Venues, etc.), so I figured hell, I'll clean it up and offer it to the Google gods. Maybe someone will find it useful.</p>
<h3>"Why aren't we flying? Because getting there is half the fun. You know that."</h3>
<p>The script is pretty simple, but it usually gets the job done with minimal modification. I tend to use a small dimension table for geo lat / long data - because generally the coordinates of an address won't change, so there is no reason to RE-Geocode an address (in most cases).</p>
<p>In this example, I'm using a SQL Server connection - but the query usage are so basic that any other could be used just by loading a different module and changing the connection syntax a bit (SQLite, Oracle, MYSQL, etc).</p>
<p>You'll need to download and <a href="https://bitbucket.org/shelldweller/python-geocoder" target="_blank">install this simple Google Maps API wrapper first</a>.</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> geocode.<span style="color: black;">google</span> <span style="color: #ff7700;font-weight:bold;">import</span> GoogleGeocoderClient<br />
<span style="color: #ff7700;font-weight:bold;">import</span> pymssql <span style="color: #808080; font-style: italic;"># or MySQLdb or etc.</span><br />
<br />
<span style="color: #808080; font-style: italic;"># sql server example</span><br />
db <span style="color: #66cc66;">=</span> pymssql.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>host<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'localhost'</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">user</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">'sa'</span><span style="color: #66cc66;">,</span> password<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'trolololo'</span><span style="color: black;">&#41;</span> <br />
cursor <span style="color: #66cc66;">=</span> db.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
geocoder <span style="color: #66cc66;">=</span> GoogleGeocoderClient<span style="color: black;">&#40;</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span><br />
<br />
<br />
<span style="color: #808080; font-style: italic;"># your source SQL in this example I'm expect at least 2 fields, the first a unique id for that address (for updating), the next a string of the address</span><br />
cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;select 12341 as myid, '101 s presidents st baltimore MD USA' as address &quot;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># the hotel I wrote this in</span><br />
addy_queue <span style="color: #66cc66;">=</span> cursor.<span style="color: black;">fetchall</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">for</span> curraddy <span style="color: #ff7700;font-weight:bold;">in</span> addy_queue:<br />
<br />
&nbsp; &nbsp; addy_id <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>curraddy<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># your unique id</span><br />
&nbsp; &nbsp; addy <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>curraddy<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># your location</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'****************************ADDY IN*********************************'</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' ('</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>addy_id<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">') '</span> + addy<br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #66cc66;">=</span> geocoder.<span style="color: black;">geocode</span><span style="color: black;">&#40;</span>addy<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># result.is_success() # nice little boolean if you need it</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; googaddy <span style="color: #66cc66;">=</span> result.<span style="color: black;">get_formatted_address</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;'&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; coord <span style="color: #66cc66;">=</span> result.<span style="color: black;">get_location</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; latitude <span style="color: #66cc66;">=</span> coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; longitude <span style="color: #66cc66;">=</span> coord<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; location_type <span style="color: #66cc66;">=</span> result.<span style="color: black;">get_location_type</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># just for fun</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'****************************ADDY OUT*********************************'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>latitude<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">', '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>longitude<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' ('</span> + location_type + <span style="color: #483d8b;">')'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> googaddy <span style="color: #808080; font-style: italic;"># Google Maps formatted address</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' '</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># this is where you're updating the geo coords BACK to your data, or inserting them in a sep table</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;delete from homes.dbo.my_geo where id = '&quot;</span> +<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>addy_id<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;' &quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;insert into homes.dbo.my_geo (lat, long, id, clean_address) values ('&quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>latitude<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;', '&quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>longitude<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;', '&quot;</span> +<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>addy_id<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;', '&quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>googaddy<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;' ) &quot;</span><span style="color: black;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; db.<span style="color: black;">commit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">pass</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># in case of error: do something (optional) I'm sure you're going to have some bad addys, just address them later, or flag them here</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#time.sleep(.2) # just in case you dont want to hammer Google's servers, but I say - fuck 'em</span></div></div>
<div class="woo-sc-box info   ">FYI - Last I heard, the Google API limits geocoding to 2,500 requests within 24 hours, after that you'll just get error responses.</div>
<p>Feel free to give me a shout!</p>
<p>The post <a href="http://ryrobes.com/python/quick-dirty-address-geocoding-and-formatting-with-google-maps-api/">Quick &#038; Dirty Address Geocoding and Formatting with Google Maps API</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Ryrobes?a=J78rYclsN_0:oDW5ASuek1c:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=J78rYclsN_0:oDW5ASuek1c:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=J78rYclsN_0:oDW5ASuek1c:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=J78rYclsN_0:oDW5ASuek1c:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=J78rYclsN_0:oDW5ASuek1c:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=J78rYclsN_0:oDW5ASuek1c:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Ryrobes/~4/J78rYclsN_0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ryrobes.com/python/quick-dirty-address-geocoding-and-formatting-with-google-maps-api/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://ryrobes.com/python/quick-dirty-address-geocoding-and-formatting-with-google-maps-api/</feedburner:origLink></item>
		<item>
		<title>Build Tableau Data Extracts out of CSV Files? More Python TDE API madness!</title>
		<link>http://feedproxy.google.com/~r/Ryrobes/~3/H_f8Y03NKj4/</link>
		<comments>http://ryrobes.com/python/build-tableau-data-extracts-out-of-csv-files-more-python-tde-api-madness/#comments</comments>
		<pubDate>Fri, 07 Dec 2012 19:10:17 +0000</pubDate>
		<dc:creator>Ry</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Tableau]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[tableau 8]]></category>
		<category><![CDATA[tableau data extracts]]></category>

		<guid isPermaLink="false">http://ryrobes.com/?p=42693</guid>
		<description><![CDATA[<p>So here we have the 3rd in a series about using Tableau 8′s ‘Data Extract’ API to automatically create TDE files from various data sources without using the desktop client. This time we’re focusing on good ole’ Comma Separated files....

Get yo CSV on!</p><p>The post <a href="http://ryrobes.com/python/build-tableau-data-extracts-out-of-csv-files-more-python-tde-api-madness/">Build Tableau Data Extracts out of CSV Files? More Python TDE API madness!</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p>]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/LOG-ren-and-stimpy-1552749-1280-1024.jpg" width="240" />
		</p><p>So here we have the 3rd in a <a href="http://ryrobes.com/category/tableau/">series about using Tableau 8's 'Data Extract' API</a> to automatically create TDE files from various data sources without using the desktop client. This time we're focusing on good ole' Comma Separated files.</p>
<p><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/powdered-toast-man.jpg" alt="" title="powdered-toast-man" width="200" height="171" class="alignleft size-full wp-image-42718" /> Much like <a href="http://ryrobes.com/python/sql-server-query-to-tableau-data-extract-more-tde-api-fun-with-python-tableau-8/" title="SQL Server Query to Tableau Data Extract LIKE A BOSS – Some more TDE API fun with Python & Tableau 8">my SQL Server script</a>, this one tries to guess field names and field data types in order to produce as good of an extract as possible - I've also added a little command line progress bar for your <strong>FILE WATCHING ENJOYMENT</strong>. </p>
<p>You know, in case you're into shit like that. </p>
<p>Plus, I report on file sizes and row counts! OMG! You ALSO have the ability to run MULTIPLE files in a directory - in case you have a bunch that need to be crunched into their own TDE files - that's right, Christmas come early! (You're welcome)</p>
<p>I used some census data files for testing <em>(as well as Metallica, Bigfoot, and UFO data - but that's a given)</em> as you can see below. I gotta, say processing CSV files is decent speedy - even though I haven't tried to process any HUGE files yet (2GB+).</p>
<p>Bit of explanation about the script parameters:</p>
<ul>
<li><strong>cvsfilenamemask = '.csv'</strong> It's basically an "ends with" string - so you can use something like '.csv' to process ALL CSV files in a directory, or simply change it to something like 'input.csv' to ONLY process that file from that dir.</li>
<li><strong>sourcedir = 'C:\\Python27\\'</strong> Fairly obvious - this is the folder that the script will look in for any files matching 'csvfilenamemask' above. You can also use UNC file paths like ''\\\\ryrobesxps\d$\\'. Remember to double up your slashes accordingly!</li>
<li><strong>targetdir = 'C:\\Python27\\'</strong> Same as above, except this it the target dir that the resulting TDE file will be written to. </li>
<li><strong>csvdelimiter = ','</strong> You should know this one.</li>
<li><strong>csvquotechar = '"'</strong> AND this one, but who knows.</li>
<li><strong>rowoutput = False</strong> Set to 'True' to have all the row and column info echoed into the command line window - it's good for debugging data and script errors but it slows things down like 10X (literally). Who knew that printing out thousands of things would be so expensive! ;)</li>
</ul>
<h4>Here's a sample output from some tests I did earlier</h4>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">C:\Python27&gt;python CSV_to_TDE.py<br />
<br />
###########################################################################<br />
&nbsp; Now working on cbp10co.csv (2,155,390) -&gt; cbp10co.tde (59,871 rows per =)<br />
###########################################################################<br />
[ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ]<br />
(2,155,390 rows)<br />
<br />
###########################################################################<br />
&nbsp; Now working on cbp10st.csv (456,410) -&gt; cbp10st.tde (12,678 rows per =)<br />
###########################################################################<br />
[ = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ]<br />
(456,410 rows)<br />
<br />
###########################################################################<br />
&nbsp; Now working on PlayerStats.csv (23) -&gt; PlayerStats.tde (10 rows per =)<br />
###########################################################################<br />
[ = = ]<br />
(23 rows)<br />
<br />
###########################################################################<br />
&nbsp; Now working on simple.csv (32) -&gt; simple.tde (10 rows per =)<br />
###########################################################################<br />
[ = = = ]<br />
(31 rows)<br />
<br />
cbp10co.csv &nbsp; 383 seconds &nbsp; 2,155,390 rows. TDE file is 172.9MB (source was 42.2MB)<br />
cbp10st.csv &nbsp; 431 seconds &nbsp; 456,410 rows. TDE file is 114.6MB (source was 43.5MB)<br />
PlayerStats.csv 4 seconds &nbsp; 23 rows. TDE file is 3.5KB (source was 57.4KB)<br />
simple.csv &nbsp; &nbsp; &nbsp;1 seconds &nbsp; 31 rows. TDE file is 659.0bytes (source was 34.4KB)<br />
<br />
TOTAL RUN &nbsp; &nbsp; 820 seconds &nbsp; 2,611,854 rows - 287.5MB of text into 85.8MB of data sex!<br />
<br />
C:\Python27&gt;</div></div>
<p>Sexy, right? Indeed. Ok, here it is, it's a whopper, so get that Cntrl-C hand ready...</p>
<h3>Wh3r3's th3 b33f? Oh, nevermind.</h3>
<div class="woo-sc-box normal   "><strong>Note:</strong> if you have less than 20 rows or so, my "fancy" header checking could get confused. Why you would have a file like that, I have no idea - just sayin'.</div>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># ryan robitialle (12/6/2012)</span><br />
<span style="color: #808080; font-style: italic;"># creating Tableau Data Extracts via CSV files</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">csv</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">os</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">time</span><br />
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">datetime</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> dataextract <span style="color: #ff7700;font-weight:bold;">as</span> tde <span style="color: #808080; font-style: italic;">#saves some typing, cause i'm a lazy fucker</span><br />
<br />
<span style="color: #808080; font-style: italic;">################ PARAMETERS FOR YOU, CODE MONKEY! ##########################</span><br />
cvsfilenamemask <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'s.csv'</span> <span style="color: #808080; font-style: italic;"># can be explicit 'thisfile.csv' for one file - or open '.csv' for all that match</span><br />
sourcedir <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'C:<span style="color: #000099; font-weight: bold;">\\</span>Python27<span style="color: #000099; font-weight: bold;">\\</span>'</span> <span style="color: #808080; font-style: italic;"># need to double up the \\s | windows shares use like this '\\\\ryrobesxps\d$\' etc</span><br />
targetdir <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'C:<span style="color: #000099; font-weight: bold;">\\</span>Python27<span style="color: #000099; font-weight: bold;">\\</span>'</span> <span style="color: #808080; font-style: italic;"># can't be a share or UNC path</span><br />
csvdelimiter <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">','</span> <span style="color: #808080; font-style: italic;"># obvious!</span><br />
csvquotechar <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'&quot;'</span> <span style="color: #808080; font-style: italic;"># obvious!</span><br />
rowoutput <span style="color: #66cc66;">=</span> <span style="color: #008000;">False</span> <span style="color: #808080; font-style: italic;"># useful for debugging data errors / slows shit down a lot however</span><br />
<span style="color: #808080; font-style: italic;">################ PARAMETERS FOR YOU, CODE MONKEY! ##########################</span><br />
<br />
<span style="color: #808080; font-style: italic;"># Note: if you have less than a few thousand rows, the progress bar will be a bit fucked looking.</span><br />
<br />
fileperf <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># for saving each files execution times</span><br />
<br />
<span style="color: #808080; font-style: italic;"># since the CSV module imports all fields as strings regardless of what they are..</span><br />
<span style="color: #ff7700;font-weight:bold;">def</span> datatyper<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>: &nbsp; &nbsp;<span style="color: #808080; font-style: italic;"># force some data types to figure shit out</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>: &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># kind of lame.... BUT IT WORKS</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x <span style="color: #66cc66;">=</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x <span style="color: #66cc66;">=</span> <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date_object <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">datetime</span>.<span style="color: black;">strptime</span><span style="color: black;">&#40;</span>n<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'%m/%d/%Y'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> date_object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date_object <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">datetime</span>.<span style="color: black;">strptime</span><span style="color: black;">&#40;</span>n<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'%Y-%m-%d'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> date_object<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> n <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'NULL'</span>: <span style="color: #808080; font-style: italic;"># just in case, don't want any literal NULLs in there</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>: <span style="color: #808080; font-style: italic;"># no need to return an empty string, let's NULL that shit out</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span><br />
<span style="color: #808080; font-style: italic;"># end ugly data types function</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> showhumanfilesize<span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">'bytes'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'KB'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'MB'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'GB'</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> num <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">1024.0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;%3.1f%s&quot;</span> % <span style="color: black;">&#40;</span>num<span style="color: #66cc66;">,</span> x<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; num /<span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1024.0</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;%3.1f%s&quot;</span> % <span style="color: black;">&#40;</span>num<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'TB'</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> intWithCommas<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">type</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #008000;">type</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">type</span><span style="color: black;">&#40;</span>0L<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">TypeError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Parameter must be an integer.&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> x <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'-'</span> + intWithCommas<span style="color: black;">&#40;</span>-x<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; result <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">''</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">while</span> x <span style="color: #66cc66;">&gt;=</span> <span style="color: #ff4500;">1000</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; x<span style="color: #66cc66;">,</span> r <span style="color: #66cc66;">=</span> <span style="color: #008000;">divmod</span><span style="color: black;">&#40;</span>x<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1000</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;,%03d%s&quot;</span> % <span style="color: black;">&#40;</span>r<span style="color: #66cc66;">,</span> result<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;%d%s&quot;</span> % <span style="color: black;">&#40;</span>x<span style="color: #66cc66;">,</span> result<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> file_lines<span style="color: black;">&#40;</span>fname<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>fname<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> f:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> i<span style="color: #66cc66;">,</span> l <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">pass</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> i + <span style="color: #ff4500;">1</span><br />
<br />
<span style="color: #808080; font-style: italic;">#print ' '</span><br />
<span style="color: #808080; font-style: italic;">#print '[ Note: Each . = ' +str(dotsevery)+ ' rows processed ]'</span><br />
<br />
<span style="color: #dc143c;">os</span>.<span style="color: black;">chdir</span><span style="color: black;">&#40;</span>sourcedir<span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> csvfilename <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">listdir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;.&quot;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> csvfilename.<span style="color: black;">endswith</span><span style="color: black;">&#40;</span>cvsfilenamemask<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; tdefilename <span style="color: #66cc66;">=</span> csvfilename.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">'.tde'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; linez <span style="color: #66cc66;">=</span> file_lines<span style="color: black;">&#40;</span>sourcedir + csvfilename<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> linez <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">36</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dotsevery <span style="color: #66cc66;">=</span> linez/<span style="color: #ff4500;">36</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dotsevery <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' '</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'###########################################################################'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' &nbsp;Now working on '</span> + csvfilename + <span style="color: #483d8b;">' ('</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>intWithCommas<span style="color: black;">&#40;</span>linez<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>+<span style="color: #483d8b;">') -&gt; '</span> + tdefilename + <span style="color: #483d8b;">' ('</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>intWithCommas<span style="color: black;">&#40;</span>dotsevery<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' rows per =)'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'###########################################################################'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#print dotsevery</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># so you can read it.</span><br />
<br />
<span style="color: #808080; font-style: italic;"># BEGIN MULTI FILE LOOP</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; start_time <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># simple timing for test purposes</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># taking a sample of the file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvfile <span style="color: #66cc66;">=</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>csvfilename<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'rb'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toplines <span style="color: #66cc66;">=</span> csvfile.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filebuffer <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">''</span> <span style="color: #808080; font-style: italic;"># empty string</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>dotsevery<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filebuffer <span style="color: #66cc66;">=</span> filebuffer + toplines<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasheader <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">csv</span>.<span style="color: black;">Sniffer</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">has_header</span><span style="color: black;">&#40;</span>filebuffer<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># csvfile.read() &nbsp;/ &nbsp;filebuffer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasheader <span style="color: #66cc66;">=</span> <span style="color: #008000;">False</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># ok lets go</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; csvfile.<span style="color: black;">seek</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># YOU WILL DO, WHAT I SAY, WHEN I SAY! BACK TO THE FRONT!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; csvreader <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">csv</span>.<span style="color: black;">DictReader</span><span style="color: black;">&#40;</span>csvfile<span style="color: #66cc66;">,</span> delimiter<span style="color: #66cc66;">=</span>csvdelimiter<span style="color: #66cc66;">,</span> quotechar<span style="color: #66cc66;">=</span>csvquotechar<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; dfields <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dtypes <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> hasheader <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> csvreader.<span style="color: black;">fieldnames</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dfields.<span style="color: black;">append</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>: <span style="color: #808080; font-style: italic;"># WTF? No header? JERK.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldnum <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#print 'If you don\'t have a header, how the fuck will you recognize the fields in Tableau?'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> csvreader.<span style="color: black;">fieldnames</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dfields.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'field'</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>fieldnum<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldnum <span style="color: #66cc66;">=</span> fieldnum + <span style="color: #ff4500;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvreader <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">csv</span>.<span style="color: black;">DictReader</span><span style="color: black;">&#40;</span>csvfile<span style="color: #66cc66;">,</span> delimiter<span style="color: #66cc66;">=</span>csvdelimiter<span style="color: #66cc66;">,</span> quotechar<span style="color: #66cc66;">=</span>csvquotechar<span style="color: #66cc66;">,</span> fieldnames<span style="color: #66cc66;">=</span>dfields<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># we have to make our own field names</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> csvreader:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> dfields:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dtypes.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">type</span><span style="color: black;">&#40;</span>datatyper<span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">break</span> <span style="color: #808080; font-style: italic;"># got shit, we're out</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; csvfile.<span style="color: black;">seek</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># BACK TO THE FRONT! (AGAIN!)</span><br />
<br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">chdir</span><span style="color: black;">&#40;</span>targetdir<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>: &nbsp;<span style="color: #808080; font-style: italic;"># Just in case the file exists already, we don't want to bomb out</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tdefile <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Extract</span><span style="color: black;">&#40;</span>tdefilename<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># in CWD</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>: <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'del '</span> + targetdir + tdefilename<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'del DataExtract.log'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#might as well erase this bitch too</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tdefile <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Extract</span><span style="color: black;">&#40;</span>targetdir + tdefilename<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># ok lets build the table definition in TDE with our list of names and types first</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># replacing literals with TDE datatype integers, etc</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tableDef <span style="color: #66cc66;">=</span> tde.<span style="color: black;">TableDefinition</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#create a new table def</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; numfields <span style="color: #66cc66;">=</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>dfields<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#print numfields</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'*** field names list ***'</span> <span style="color: #808080; font-style: italic;"># debug </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> t <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>numfields<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldtypeo <span style="color: #66cc66;">=</span> dtypes<span style="color: black;">&#91;</span>t<span style="color: black;">&#93;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;type '&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;'&gt;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;class '&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'NoneType'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'str'</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'uuid.UUID'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'str'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldname <span style="color: #66cc66;">=</span> dfields<span style="color: black;">&#91;</span>t<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldtype <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>fieldtypeo<span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;str&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;15&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;datetime.datetime&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;13&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;int&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;7&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;decimal.Decimal&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;10&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;float&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;10&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;uuid.UUID&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;15&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bool&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;11&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> fieldname + <span style="color: #483d8b;">' &nbsp;(looks like '</span> + fieldtypeo +<span style="color: #483d8b;">', TDE datatype '</span> + fieldtype + <span style="color: #483d8b;">')'</span> &nbsp;<span style="color: #808080; font-style: italic;"># debug </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span>fieldname<span style="color: #66cc66;">,</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>fieldtype<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># if we pass a non-int to fieldtype, it'll fail</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span>fieldname<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># if we get a weird type we don't recognize, just make it a string</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'***'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># wait 5 seconds so you can actually read shit!</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># ok, lets print out the table def we just made, for shits and giggles</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'################## TDE table definition created ######################'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span>tableDef.<span style="color: black;">getColumnCount</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Column: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>tableDef.<span style="color: black;">getColumnName</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' Type: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>tableDef.<span style="color: black;">getColumnType</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># wait 5 seconds so you can actually read shit!</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># ok lets add the new def as a table in the extract</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tabletran <span style="color: #66cc66;">=</span> tdefile.<span style="color: black;">addTable</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Extract&quot;</span><span style="color: #66cc66;">,</span>tableDef<span style="color: black;">&#41;</span> <br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># time to start pumping rows!</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; rowsinserted <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># if we have a header, we don't want to try and process it</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> hasheader <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csvreader.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'['</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> csvreader:<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>: <span style="color: #808080; font-style: italic;"># row deets, else just '.'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'************** INSERTING ROW NUMBER: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>rowsinserted<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'**************'</span> <span style="color: #808080; font-style: italic;"># debug output</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>: <span style="color: #808080; font-style: italic;"># only print dot every 50 records</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>rowsinserted%dotsevery<span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'='</span><span style="color: #66cc66;">,</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columnposition <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Row</span><span style="color: black;">&#40;</span>tableDef<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> t <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>numfields<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldtype <span style="color: #66cc66;">=</span> dtypes<span style="color: black;">&#91;</span>t<span style="color: black;">&#93;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;type '&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;'&gt;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;class '&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'NoneType'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'str'</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'uuid.UUID'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'str'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldname <span style="color: #66cc66;">=</span> dfields<span style="color: black;">&#91;</span>t<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>: <span style="color: #808080; font-style: italic;"># column deets</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + fieldname + <span style="color: #483d8b;">': &nbsp; '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' ('</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>fieldtype<span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">')'</span> <span style="color: #808080; font-style: italic;"># debug output</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> fieldtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'str'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #008000;">None</span>: <span style="color: #808080; font-style: italic;"># we don't want no None!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setCharString</span><span style="color: black;">&#40;</span>columnposition<span style="color: #66cc66;">,</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setNull</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># ok, put that None here</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> fieldtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'int'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #008000;">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setInteger</span><span style="color: black;">&#40;</span>columnposition<span style="color: #66cc66;">,</span> datatyper<span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setNull</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> fieldtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'bool'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #008000;">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setBoolean</span><span style="color: black;">&#40;</span>columnposition<span style="color: #66cc66;">,</span> datatyper<span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setNull</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> fieldtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'decimal.Decimal'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #008000;">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setDouble</span><span style="color: black;">&#40;</span>columnposition<span style="color: #66cc66;">,</span> datatyper<span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setNull</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> fieldtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'datetime.datetime'</span>: <span style="color: #808080; font-style: italic;"># sexy datetime splitting</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #008000;">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strippeddate <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>datatyper<span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># just in case we get microseconds (not all datetime uses them)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timechunks <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">time</span>.<span style="color: black;">strptime</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>strippeddate<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;%Y-%m-%d %H:%M:%S&quot;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># chunky style!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setDateTime</span><span style="color: black;">&#40;</span>columnposition<span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0000</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setNull</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columnposition <span style="color: #66cc66;">=</span> columnposition + <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;"># we gots to know what column number we're working on!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tabletran.<span style="color: black;">insert</span><span style="color: black;">&#40;</span>newrow<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># finally insert buffered row into TDE 'table'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowsinserted <span style="color: #66cc66;">=</span> rowsinserted + <span style="color: #ff4500;">1</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># ok let's write out that file and get back to making dinner</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; tdefile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; csvfile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'] '</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'('</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>intWithCommas<span style="color: black;">&#40;</span>rowsinserted<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>+<span style="color: #483d8b;">' rows)'</span> <span style="color: #808080; font-style: italic;"># to clear out the row on command line</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; plist <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># timing purposes for debugging / optimizing / FUN! This is FUN, Lars.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; timetaken <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - start_time<br />
&nbsp; &nbsp; &nbsp; &nbsp; plist.<span style="color: black;">append</span><span style="color: black;">&#40;</span>timetaken<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; plist.<span style="color: black;">append</span><span style="color: black;">&#40;</span>rowsinserted<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; plist.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">getsize</span><span style="color: black;">&#40;</span>sourcedir + csvfilename<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; plist.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">getsize</span><span style="color: black;">&#40;</span>sourcedir + tdefilename<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fileperf<span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>csvfilename<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> plist<br />
<br />
<span style="color: #808080; font-style: italic;"># just for our &quot;result time&quot;</span><br />
totaltime <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
totalrecords <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
totalpresize <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
totalpostsize <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' '</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> fileperf:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> p + <span style="color: #483d8b;">' &nbsp; &nbsp; '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>intWithCommas<span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>fileperf<span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' seconds &nbsp; &nbsp; processed '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>intWithCommas<span style="color: black;">&#40;</span>fileperf<span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' records. Resulting TDE file is '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>showhumanfilesize<span style="color: black;">&#40;</span>fileperf<span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' (source was '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>showhumanfilesize<span style="color: black;">&#40;</span>fileperf<span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">')'</span><br />
&nbsp; &nbsp; totaltime <span style="color: #66cc66;">=</span> fileperf<span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + totaltime<br />
&nbsp; &nbsp; totalrecords <span style="color: #66cc66;">=</span> fileperf<span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + totalrecords<br />
&nbsp; &nbsp; totalpresize <span style="color: #66cc66;">=</span> fileperf<span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span> + totalpresize<br />
&nbsp; &nbsp; totalpostsize <span style="color: #66cc66;">=</span> fileperf<span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span> + totalpostsize<br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>fileperf<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' '</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'TOTAL RUN&nbsp; &nbsp; &nbsp; &nbsp; '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>intWithCommas<span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>totaltime<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' seconds&nbsp; &nbsp; &nbsp; processed '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>intWithCommas<span style="color: black;">&#40;</span>totalrecords<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' records - crunched '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>showhumanfilesize<span style="color: black;">&#40;</span>totalpresize<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' of text into '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>showhumanfilesize<span style="color: black;">&#40;</span>totalpostsize<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' of binary sexiness'</span></div></div>
<p>Issues? Let me know below and I can fix them! Maybe.</p>
<p>The post <a href="http://ryrobes.com/python/build-tableau-data-extracts-out-of-csv-files-more-python-tde-api-madness/">Build Tableau Data Extracts out of CSV Files? More Python TDE API madness!</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Ryrobes?a=H_f8Y03NKj4:FP_yGVJ-DJI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=H_f8Y03NKj4:FP_yGVJ-DJI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=H_f8Y03NKj4:FP_yGVJ-DJI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=H_f8Y03NKj4:FP_yGVJ-DJI:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=H_f8Y03NKj4:FP_yGVJ-DJI:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=H_f8Y03NKj4:FP_yGVJ-DJI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Ryrobes/~4/H_f8Y03NKj4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ryrobes.com/python/build-tableau-data-extracts-out-of-csv-files-more-python-tde-api-madness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://ryrobes.com/python/build-tableau-data-extracts-out-of-csv-files-more-python-tde-api-madness/</feedburner:origLink></item>
		<item>
		<title>SQL Server Query to Tableau Data Extract LIKE A BOSS – Some more TDE API fun with Python &amp; Tableau 8</title>
		<link>http://feedproxy.google.com/~r/Ryrobes/~3/wbQTsBBgLCo/</link>
		<comments>http://ryrobes.com/python/sql-server-query-to-tableau-data-extract-more-tde-api-fun-with-python-tableau-8/#comments</comments>
		<pubDate>Wed, 05 Dec 2012 01:20:30 +0000</pubDate>
		<dc:creator>Ry</dc:creator>
				<category><![CDATA[Cut-n-Paste Code]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tableau]]></category>
		<category><![CDATA[microsoft sql server. pymssql]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[tableau data extracts]]></category>

		<guid isPermaLink="false">http://ryrobes.com/?p=42576</guid>
		<description><![CDATA[<p>Coming off the excitement of my last post about writing a simple bare-bones python usage of Tableau’s brand-new Data Extracts API from version 8.0 – I figured that it was time to build on that. Let’s take a step forward and get a little more complicated – AND a little more useful.... Let's do this.</p><p>The post <a href="http://ryrobes.com/python/sql-server-query-to-tableau-data-extract-more-tde-api-fun-with-python-tableau-8/">SQL Server Query to Tableau Data Extract LIKE A BOSS &#8211; Some more TDE API fun with Python &#038; Tableau 8</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p>]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/he_man_picture.jpg" width="240" />
		</p><p>Coming off the <em>excitement</em> of <a href="http://ryrobes.com/python/building-tableau-data-extract-files-with-python-in-tableau-8-sample-usage/" title="Building Tableau Data Extract files with Python in Tableau 8 – Sample Usage">my last post about writing a simple bare-bones python usage of Tableau's brand-new Data Extracts API</a> from version 8.0 - I figured that it was time to build on that. Let's take a step forward and get a little more complicated - AND a little more useful.<img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/heman-case.jpg" alt="I'm encapsulated in a proprietary binary data file OF EMOTION!" title="I'm encapsulated in a proprietary binary data file OF EMOTION!" width="207" height="158" class="alignleft size-full wp-image-42626" /></p>
<h2>Oh, noes!</h2>
<h4>I'm encapsulated in a proprietary binary data file OF EMOTION!</h4>
<h2>Save me, Cringer!</h2>
<p>Wait. This could be MORE useful then my last post?<br />
<h4>You mean your business doesn't run on a billion rows of dummy shit data? Weird.</h4>
<p>Anyways, this time we're going to extract data from SQL Server with a single SQL query and populate a new TDE file. </p>
<p><strong>This script has 3 main features:</strong></p>
<div class="shortcode-unorderedlist tick"></p>
<ul>
<li>Can run any query you want (even 'select * from' queries)</li>
<li>You don't have to pre-define the field names</li>
<li>You don't have to pre-define the data types</li>
</ul>
<p></div>

<h2>I got you, shawty. I GOT YOU.</h2>
<p>All of which we had to explicitly state in the last script (even as a 'proof of concept'), and as you can imagine - with a wide result set, defining every little thing and then inserting every little thing can be a huge pain in the ass (not to mention ugly). With my approach, it's literally plug and play - err, paste and play. You can even run queries like "select * from " it's not even necessary to use the field names (assuming they are all unique, that is - more on that later) - and yes, I mentioned that twice <strong>for <em>EMPHASIS</em></strong>!</p>
<h4>Ye gods! No scripting out my lame_ass_field_names? Huzzah!</h4>
<p>Basically, the script works like this:</p>
<ul>
<li>Connect to SQL Server</li>
<li>Execute your SQL query</li>
<li>Look at the field names and first row data</li>
<li>Have Python try to guess what data types they are (based on that first row)</li>
<li>Map those data types and field names to a TDE schema definition (TableDefinition())</li>
<li>Loop through the entire result set, calling each field by name and type so the correct insert method is used (setDouble, setInteger, setNull, setDateTime, setBoolean, etc)</li>
<li>Do a little headbanging as you watch the text <em>fly</em> by!</li>
<li>Close the file (write the TDE to disk)</li>
<li>Close the SQL connection</li>
<li>Wipe hands on pants</li>
</ul>
<h4>Booya. Done.</h4>
<p> And you didn't have to do a damn thing except give it a server connection and a SQL query. Now you can open that fresh TDE file and have yourself a little data party. { Pants optional }</p>
<div class="woo-sc-box tick  rounded full"><strong>Requires:</strong> <a href="http://python.org/download/" target="_blank">Python 2.7.X</a>+, the DataExtracts module, and <a href="http://code.google.com/p/pymssql/" target="_blank">pymssql module</a> (<a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql" target="_blank">pre-compiled binaries here</a>). Oh, and it would help to have a SQL Server and Tableau Desktop as well. ;)</div>
<p>Here it is! Cut, Paste, and give it a try, gosh darnint!</p>
<div class="woo-sc-box info  rounded full"><strong>UPDATE:</strong> Added var <strong>(rowoutput = True / False)</strong> for turning off the debug row / column output - good for looking into errors but slowed the script down like 10x</div>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># ryan robitaille 12/6/2012</span><br />
<span style="color: #808080; font-style: italic;"># simple Tableau Data Extract creation from a single microsoft sql server sql statement</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">import</span> dataextract <span style="color: #ff7700;font-weight:bold;">as</span> tde <span style="color: #808080; font-style: italic;"># saves some typing, cause i'm a lazy fucker</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">time</span><span style="color: #66cc66;">,</span> pymssql <span style="color: #808080; font-style: italic;"># for file manipulation, script timing (not necc), database access!</span><br />
<br />
<span style="color: #808080; font-style: italic;">###################### FOR YOUR PARAMETERS, SON! ######################</span><br />
tdefilename <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'ufo_datas.tde'</span><br />
sql <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;select * from UFOdata.dbo.Sightings&quot;</span> <span style="color: #808080; font-style: italic;"># whatever</span><br />
sqlserverhost <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'localhost'</span><br />
sqlusername <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'sa'</span><br />
sqlpassword <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'passy'</span><br />
sqldatabase <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'UFOdata'</span><br />
rowoutput <span style="color: #66cc66;">=</span> <span style="color: #008000;">False</span> <span style="color: #808080; font-style: italic;"># for DEBUGGING data errors / slows shit down 10X however</span><br />
<span style="color: #808080; font-style: italic;">###################### FOR YOUR PARAMETERS, SON! ######################</span><br />
<br />
dotsevery <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">75</span><br />
<br />
start_time <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># simple timing for test purposes</span><br />
<br />
mssql_db <span style="color: #66cc66;">=</span> pymssql.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>host<span style="color: #66cc66;">=</span>sqlserverhost<span style="color: #66cc66;">,</span> <span style="color: #dc143c;">user</span><span style="color: #66cc66;">=</span>sqlusername<span style="color: #66cc66;">,</span> password<span style="color: #66cc66;">=</span>sqlpassword<span style="color: #66cc66;">,</span> database<span style="color: #66cc66;">=</span>sqldatabase<span style="color: #66cc66;">,</span> as_dict<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># as_dict very important</span><br />
mssql_cursor <span style="color: #66cc66;">=</span> mssql_db.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
mssql_cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span>sql<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' '</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'[ Note: Each . = '</span> +<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>dotsevery<span style="color: black;">&#41;</span>+ <span style="color: #483d8b;">' rows processed ]'</span><br />
<br />
fieldnameslist <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># define our empty list</span><br />
<br />
<span style="color: #808080; font-style: italic;">#go through the first row to TRY to set fieldnames and datatypes</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> mssql_cursor:<br />
&nbsp; &nbsp; &nbsp; &nbsp; itemz <span style="color: #66cc66;">=</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>row.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span> <span style="color: #808080; font-style: italic;"># because the dict rowset includes BOTH number keys and fieldname keys</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> row.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldnameslist.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>k<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'|'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">type</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;type '&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;'&gt;&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;class '&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'NoneType'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'str'</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'uuid.UUID'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">'str'</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">break</span> <span style="color: #808080; font-style: italic;"># after the first row, we SHOULD have a decent idea of the datatypes</span><br />
<span style="color: #808080; font-style: italic;"># ^ a bit inelegant, but it gets the job done</span><br />
<br />
fieldnameslist.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># sort them out so the integer keys are first (we're gonna whack em)</span><br />
<span style="color: #ff7700;font-weight:bold;">del</span> fieldnameslist<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>:itemz<span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># remove first x amount of keys (should be all integers instead of dict literals)</span><br />
<br />
<br />
<span style="color: #ff7700;font-weight:bold;">try</span>: &nbsp;<span style="color: #808080; font-style: italic;"># Just in case the file exists already, we don't want to bomb out</span><br />
&nbsp; &nbsp; tdefile <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Extract</span><span style="color: black;">&#40;</span>tdefilename<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># in CWD</span><br />
<span style="color: #ff7700;font-weight:bold;">except</span>: <br />
&nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'del '</span>+tdefilename<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'del DataExtract.log'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#might as well erase this bitch too</span><br />
&nbsp; &nbsp; tdefile <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Extract</span><span style="color: black;">&#40;</span>tdefilename<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;"># ok lets build the table definition in TDE with our list of names and types first</span><br />
<span style="color: #808080; font-style: italic;"># replacing literals with TDE datatype integers, etc</span><br />
tableDef <span style="color: #66cc66;">=</span> tde.<span style="color: black;">TableDefinition</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#create a new table def</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'*** field names list ***'</span> <span style="color: #808080; font-style: italic;"># debug </span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> t <span style="color: #ff7700;font-weight:bold;">in</span> fieldnameslist:<br />
&nbsp; &nbsp; fieldtype <span style="color: #66cc66;">=</span> t.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'|'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; fieldname <span style="color: #66cc66;">=</span> t.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'|'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; fieldtype <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>fieldtype<span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;str&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;15&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;datetime.datetime&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;13&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;int&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;7&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;decimal.Decimal&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;10&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;float&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;10&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;uuid.UUID&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;15&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bool&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;11&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> fieldname + <span style="color: #483d8b;">' &nbsp;(looks like '</span> + t.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'|'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> +<span style="color: #483d8b;">', TDE datatype '</span> + fieldtype + <span style="color: #483d8b;">')'</span> &nbsp;<span style="color: #808080; font-style: italic;"># debug </span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span>fieldname<span style="color: #66cc66;">,</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>fieldtype<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># if we pass a non-int to fieldtype, it'll fail</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span>fieldname<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># if we get a weird type we don't recognize, just make it a string</span><br />
<span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'***'</span><br />
&nbsp; &nbsp; <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># wait 5 seconds so you can actually read shit!</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># ok, lets print out the table def we just made, for shits and giggles</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'################## TDE table definition created ######################'</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span>tableDef.<span style="color: black;">getColumnCount</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Column: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>tableDef.<span style="color: black;">getColumnName</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' Type: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>tableDef.<span style="color: black;">getColumnType</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># wait 5 seconds so you can actually read shit!</span><br />
<br />
<span style="color: #808080; font-style: italic;"># ok lets add the new def as a table in the extract</span><br />
tabletran <span style="color: #66cc66;">=</span> tdefile.<span style="color: black;">addTable</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Extract&quot;</span><span style="color: #66cc66;">,</span>tableDef<span style="color: black;">&#41;</span> <br />
<span style="color: #808080; font-style: italic;"># why table NEEDS to be called 'Extract' is beyond me</span><br />
<br />
rowsinserted <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;"># we need to count stuff, dude! Robots start at 0, I START AT 1!</span><br />
<br />
<span style="color: #808080; font-style: italic;"># ok, for each row in the result set, we iterate through all the fields and insert based on datatype</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> mssql_cursor:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'************** INSERTING ROW NUMBER: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>rowsinserted<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'**************'</span> <span style="color: #808080; font-style: italic;"># debug output</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>: <span style="color: #808080; font-style: italic;"># only print dot every 50 records</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>rowsinserted%dotsevery<span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff4500;">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'.'</span><span style="color: #66cc66;">,</span><br />
<br />
&nbsp; &nbsp; columnposition <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
&nbsp; &nbsp; newrow <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Row</span><span style="color: black;">&#40;</span>tableDef<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> t <span style="color: #ff7700;font-weight:bold;">in</span> fieldnameslist:<br />
&nbsp; &nbsp; &nbsp; &nbsp; fieldtype <span style="color: #66cc66;">=</span> t.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'|'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fieldname <span style="color: #66cc66;">=</span> t.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'|'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowoutput <span style="color: #66cc66;">==</span> <span style="color: #008000;">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + fieldname + <span style="color: #483d8b;">': &nbsp; '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' ('</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>fieldtype<span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">')'</span> <span style="color: #808080; font-style: italic;"># debug output</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> fieldtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'str'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #008000;">None</span>: <span style="color: #808080; font-style: italic;"># we don't want no None!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setCharString</span><span style="color: black;">&#40;</span>columnposition<span style="color: #66cc66;">,</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setNull</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># ok, put that None here</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> fieldtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'int'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #008000;">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setInteger</span><span style="color: black;">&#40;</span>columnposition<span style="color: #66cc66;">,</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setNull</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> fieldtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'bool'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #008000;">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setBoolean</span><span style="color: black;">&#40;</span>columnposition<span style="color: #66cc66;">,</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setNull</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> fieldtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'decimal.Decimal'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #008000;">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setDouble</span><span style="color: black;">&#40;</span>columnposition<span style="color: #66cc66;">,</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setNull</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> fieldtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'datetime.datetime'</span>: <span style="color: #808080; font-style: italic;"># sexy datetime splitting</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!=</span> <span style="color: #008000;">None</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strippeddate <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span>fieldname<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># just in case we get microseconds (not all datetime uses them)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timechunks <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">time</span>.<span style="color: black;">strptime</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>strippeddate<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;%Y-%m-%d %H:%M:%S&quot;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># chunky style!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setDateTime</span><span style="color: black;">&#40;</span>columnposition<span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> timechunks<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0000</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newrow.<span style="color: black;">setNull</span><span style="color: black;">&#40;</span>columnposition<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; columnposition <span style="color: #66cc66;">=</span> columnposition + <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;"># we gots to know what column number we're working on!</span><br />
&nbsp; &nbsp; tabletran.<span style="color: black;">insert</span><span style="color: black;">&#40;</span>newrow<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># finally insert buffered row into TDE 'table'</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; rowsinserted <span style="color: #66cc66;">=</span> rowsinserted + <span style="color: #ff4500;">1</span><br />
<br />
<span style="color: #808080; font-style: italic;"># ok let's write out that file and get back to making dinner</span><br />
tdefile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
mssql_db.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;"># timing purposes for debugging / optimizing / FUN! This is FUN, Lars.</span><br />
timetaken <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - start_time<br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>rowsinserted<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' rows inserted in '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>timetaken<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' seconds'</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' &nbsp; &nbsp;('</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>rowsinserted/timetaken<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' rows per second)'</span><br />
<span style="color: #808080; font-style: italic;"># woo, let's have a drink!</span></div></div>
<p>I threw this together in a few hours, and will probably be adding to it, fixing it down the line. It has very little exception handling and is only around 130 lines; including my asinine comments and whitespace lines. Parts of it are a bit 'inelegant', lots of list building and string mangling - but it works. Quite well actually.</p>
<p>Love to get some feedback so I can make it better. I tested it on many different tables I had laying around, including that crazy MS AdventureWorks2008R2 Data Warehouse DB which uses a lot of strange data types and calculated fields. All worked splendidly.</p>
<div class="woo-sc-box normal  rounded full">
<strong>Current Known Issues: </strong></p>
<p>1) I'm only looking at the first row for data type guessing. I really should be taking a much larger sample of the data. </p>
<p>2) If you do a bunch of joins, don't explicitly define the field names in the query, and have duplicate field names in the result set - the file will miss some fields, or even bomb out completely.</p>
<p>i.e. - Don't do a: </p>
<pre>select a.*, b.*, c.* from table1 a, table2 b, table3 c 
            where a.id = b.id and a.id = c.id


</pre>
<p>But you wouldn't do that fucking nonsense anyways - right? ;)<br />
</div>
<p>I'm also thinking of making a version of this with command line parameters instead of file editing.</p>
<pre>i.e. Python TDE_from_mssql.py 
--file=belly.tde 
--server=servername 
--username=sa 
--database=db45 
--sql="select * from bellybutton"

</pre>
<p>What's next for TDE file population posts? Web scraping, MySQL, Oracle, flat files? Might as well hit 'em all.</p>
<p>The post <a href="http://ryrobes.com/python/sql-server-query-to-tableau-data-extract-more-tde-api-fun-with-python-tableau-8/">SQL Server Query to Tableau Data Extract LIKE A BOSS &#8211; Some more TDE API fun with Python &#038; Tableau 8</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Ryrobes?a=wbQTsBBgLCo:IqS-mjeptqo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=wbQTsBBgLCo:IqS-mjeptqo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=wbQTsBBgLCo:IqS-mjeptqo:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=wbQTsBBgLCo:IqS-mjeptqo:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=wbQTsBBgLCo:IqS-mjeptqo:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=wbQTsBBgLCo:IqS-mjeptqo:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Ryrobes/~4/wbQTsBBgLCo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ryrobes.com/python/sql-server-query-to-tableau-data-extract-more-tde-api-fun-with-python-tableau-8/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://ryrobes.com/python/sql-server-query-to-tableau-data-extract-more-tde-api-fun-with-python-tableau-8/</feedburner:origLink></item>
		<item>
		<title>Building Tableau Data Extract files with Python in Tableau 8 – Sample Usage</title>
		<link>http://feedproxy.google.com/~r/Ryrobes/~3/2HxfaWaAqqA/</link>
		<comments>http://ryrobes.com/python/building-tableau-data-extract-files-with-python-in-tableau-8-sample-usage/#comments</comments>
		<pubDate>Mon, 03 Dec 2012 02:11:16 +0000</pubDate>
		<dc:creator>Ry</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tableau]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[data extracts]]></category>
		<category><![CDATA[tableau 8]]></category>
		<category><![CDATA[tcc12]]></category>
		<category><![CDATA[tde]]></category>

		<guid isPermaLink="false">http://ryrobes.com/?p=42485</guid>
		<description><![CDATA[<p>If you were learning (and / or partying) with us at the Tableau Customer Conference 2012 in Beautiful San Diego – then you’ve probably been playing around with the Tableau 8 Beta over the past few weeks. Tableau 8 bring a whole new slew of features - INCLUDING 2 new APIs... I'm going to go over some sample Python usage for the new 'Tableau Data Extract' API...</p><p>The post <a href="http://ryrobes.com/python/building-tableau-data-extract-files-with-python-in-tableau-8-sample-usage/">Building Tableau Data Extract files with Python in Tableau 8 &#8211; Sample Usage</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p>]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/bubo.jpg" width="240" />
		</p><p><strong>See also</strong>:  <a href="http://ryrobes.com/python/sql-server-query-to-tableau-data-extract-more-tde-api-fun-with-python-tableau-8/" class="woo-sc-button  custom" style="background:;border-color:"><span class="woo-">Tableau Data Extracts from SQL Server</span></a> <a href="http://ryrobes.com/python/build-tableau-data-extracts-out-of-csv-files-more-python-tde-api-madness/" class="woo-sc-button  custom" style="background:;border-color:"><span class="woo-">or from CSV files</span></a><br />
If you were learning (and / or partying) with us at the Tableau Customer Conference 2012 in Beautiful San Diego (where me and 6 other individuals were also <a href="http://www.tableausoftware.com/about/blog/2012/11/masters-teachers-innovators-meet-tableau-zen-masters-19799" title="Tableau Zen Masters 2012" target="_blank">crowned 'Tableau Zen Masters'</a>, ahem) - then you've probably been playing around with the Tableau 8 Beta over the past few weeks. Tableau 8 brings <a href="http://www.tableausoftware.com/about/blog/2012/11/keynote-christian-chabot-chris-stolte-19767" target="_blank">a whole load of new features and performance improvements</a>, but it also includes 2 new APIs for us tinkering / hack-y peeps.</p>
<p><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/kraken5.jpg" alt="UNLEASH THE KRAKEN!" title="UNLEASH THE KRAKEN!" width="510"  class="size-full wp-image-42550"></p>
<p>One of these is the '<strong>Tableau Data Extract API</strong>' <em>(also called the 'TDE API' in acronym land, where I have a beach house)</em>. TDE will allow us to programatically create Data Extracts for Tableau Desktop and Server using our favorite data-munching language. That is, assuming that your favorite language is Python, C, C++, or Java (barf). This is going to be huge, and while the libraries seem to be in their infancy - I fully expect for them to evolve and add features as we move along.</p>
<p>Python is wicked flexible and ideal for data-centric workflow / runstreams (in my humble opinion, at least). It's my weapon of choice. In essence, these libraries give us one more <strong>output format</strong> for our scripts / programs...</p>
<h3>A "NATIVE Tableau Data Extract".</h3>
<h4>What <strong>*IS*</strong> a <em>Tableau Data Extract</em> you ask?</h4>
<h4>You know, those super fast binary .tde files that Tableau creates behind the scenes to mitigate your shitty datasource performance? Yes, THOSE.</h4>
<p>If you're a fan of this blog you know that I revel in <a href="http://ryrobes.com/visual-analytics/between-crazy-genius-bigfoot-aliens-together-at-last/" target="_blank">pulling</a> <a href="http://ryrobes.com/metallicanalysis/visual-setlist-analysis/" target="_blank">down</a> <a href="http://ryrobes.com/visual-analytics-and-data-porn/more-data-porn-garyvee-twitter-stats/" target="_blank">bizarre</a> datasources from various places and throwing them in Tableau for making my own brand of 'data visualization moonshine'. Usually, I need to write them out to CSV files, Excel files, or a local database first. But with TDE I can write them directly to a usable Tableau file in the same script, saving a step and enabling a whole bunch of other integrations - most of which I haven't even thought of yet.</p>
<blockquote><p>First things first. Since we're talking about <em>beta stuff</em>, I can't actually tell you where to GET IT. But I can do everything else from there. Besides if you attended TCC12, you probably already have access to it - otherwise, hold out until Q1 2013 - it'll be SO worth it.</p></blockquote>
<p>It comes in 2 Python versions - 32-bit and 64-bit (both v8-beta2 as of this writing) - for these examples I'm using the 32-bit versions, since most of my Python modules are 32-bit for now.</p>
<p>Installation is straightforward, the old 'python setup.py install' in the module directory. You'll also see that it also comes with a number of Tableau Server DLLs and a tdeserver.exe in the BIN directory <em>(so, sorry folks - Windows only)</em> - but don't worry about the PATH of those files, they'll all get installed in Python's site-packages directory upon installation.</p>
<p>For the most basic and simple usage - there are no other required modules, just the standard ones. I'm using <a href="http://python.org/download/" target="_blank">Python 2.7.3</a> currently.</p>
<p>I'm going to step through creating a sample file (as of this writing, sample usage files and documentation are not included in the packages) but, I'll show more advanced uses in the future, as I expand on the possibilities <em>(custom functions, integration with other data-handy python modules, revamping old scripts, etc)</em>.</p>
<h3>Simple Usage of TDE Python Module</h3>
<p><em>(a decent starting point, but with no exception handling except built-in with the module)</em></p>
<p>Annotated anywhere possible, including code comments - full script below.</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> dataextract <span style="color: #ff7700;font-weight:bold;">as</span> tde <span style="color: #808080; font-style: italic;">#saves some typing</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">time</span> <span style="color: #808080; font-style: italic;"># for file manipulation / script timing (not necc)</span><br />
<br />
start_time <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># simple timing for test purposes</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">try</span>: &nbsp;<span style="color: #808080; font-style: italic;"># Just for testing purposes and re-running</span><br />
&nbsp; &nbsp; tdefile <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Extract</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'test.tde'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#in CWD</span><br />
<span style="color: #ff7700;font-weight:bold;">except</span>: <br />
&nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'del test.tde'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'del DataExtract.log'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; tdefile <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Extract</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'test.tde'</span><span style="color: black;">&#41;</span><br />
<br />
tableDef <span style="color: #66cc66;">=</span> tde.<span style="color: black;">TableDefinition</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#create a new table def</span></div></div>
<p>As you can see I'm simply importing the '<strong>dataextract</strong>' module plus a few others. The only reason I included the try/except block is because during testing I was running this bitch over and over again - and it got really annoying to delete the last created file.</p>
<ul>
<li><strong>tdefile = tde.Extract('test.tde')</strong> names my TDE file and creates 'tdefile' for later usage</li>
<li>Then <strong>tableDef = tde.TableDefinition()</strong> gets me ready to define my file 'schema' (for lack of a better word)</li>
</ul>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># using integers for now because the literal defs are not in the python module code</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type7&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span> &nbsp; <span style="color: #808080; font-style: italic;">#INTEGER</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type10&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#DOUBLE</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type11&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">11</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#BOOLEAN</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type12&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">12</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#DATE</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type13&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">13</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#DATETIME</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type14&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">14</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#DURATION</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type15&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#CHAR_STRING</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type16&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#UNICODE_STRING</span></div></div>
<p>As you can see, I'm defining my field name and field data type here.</p>
<p>I couldn't find any place to define the data-types with literal words (like the C and C++ examples show), for now I'm going to stick with the numbering. Besides, <strong>'addColumn'</strong> requires an integer for the datatype anyways. Thankfully there are only 8 data types.</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># ok, lets print out the table def, just for shits and giggles</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span>tableDef.<span style="color: black;">getColumnCount</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Column: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>tableDef.<span style="color: black;">getColumnName</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' Type: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>tableDef.<span style="color: black;">getColumnType</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div></div>
<p>This is unnecessary, but you may find it useful. Basically, after we define our table layout (in <strong>tableDef</strong>) we echo it out on the screen. I like to add stuff like this so I have something to look at while it crunches away. So shoot me.</p>
<p>In my case, the output looks like this:</p>
<pre>
Column: field_type7 Type: 7
Column: field_type10 Type: 10
Column: field_type11 Type: 11
Column: field_type12 Type: 12
Column: field_type13 Type: 13
Column: field_type14 Type: 14
Column: field_type15 Type: 15
Column: field_type16 Type: 16

</pre>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># lets add the new def as a table in the extract</span><br />
tabletran <span style="color: #66cc66;">=</span> tdefile.<span style="color: black;">addTable</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Extract&quot;</span><span style="color: #66cc66;">,</span>tableDef<span style="color: black;">&#41;</span> <br />
<span style="color: #808080; font-style: italic;"># why table NEEDS to be called 'Extract' is beyond me</span><br />
<br />
rowsinserted <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span></div></div>
<p>Ok, now that we've defined the table schema (<strong>tableDef</strong>) - we need to create that 'table' in our TDE file we defined. As of now, you are (seemingly) required to name the table "<strong>Extract</strong>", not that it really matters at this time.</p>
<p>"rowsinserted" is just so I can count all the rows and echo the number out at the end.</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># let's create a bunch of rows! wheeeeee!</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1000000</span><span style="color: black;">&#41;</span>:<br />
<br />
&nbsp; &nbsp; newrow <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Row</span><span style="color: black;">&#40;</span>tableDef<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#newrow.setNull(0) #column</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setInteger</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span>i<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, value</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setDouble</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span>i*<span style="color: #ff4500;">1.4</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, value</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setBoolean</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, value (1/0)</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setDate</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2012</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">12</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, y,m,d</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setDateTime</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2012</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">12</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">18</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0000</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, y,m,d,h,m,s,frac</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setDuration</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">18</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0000</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, d,h,m,s,frac</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setCharString</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'Character String'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, 'value'</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setString</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">7</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'Unicode String!'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, 'value'</span><br />
&nbsp; &nbsp; rowsinserted <span style="color: #66cc66;">=</span> rowsinserted + <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;">#count this row!</span><br />
<br />
&nbsp; &nbsp; tabletran.<span style="color: black;">insert</span><span style="color: black;">&#40;</span>newrow<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#insert row into TDE table</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
tdefile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>Here is my main "insert" loops. Let's go step by step <em>(the '<strong>for i in range(x,x)</strong>' is just to run this block multiple times and generate some row depth for sample purposes)</em>.</p>
<li>Define our "<strong>newrow</strong>" as (our previously defined schema) tableDef</li>
<li>Insert each field in our new row - based on the different datatype we defined it as <em>(for validation purposes)</em> <strong>Note for each particular datatype we have to use a different format after defining the column number <em>(based on the order they exist in the schema)</em></strong></li>
<li>Once done, we insert that defined row (schema and data) into our file object</li>
<li>Close that row object since it will be re-opened next row <em>(not sure if this is required, just keeping it clean)</em></li>
<li>Finally, when all rows are inserted - <strong>tdefile.close()</strong> writes it all to disk</li>
</ul>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">timetaken <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - start_time <span style="color: #808080; font-style: italic;">#just for timing and fun</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>rowsinserted<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' rows inserted in '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>timetaken<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' seconds'</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' &nbsp; &nbsp;('</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>rowsinserted/timetaken<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' rows per second)'</span><br />
<span style="color: #808080; font-style: italic;">#booya!</span></div></div>
<p>Just printing some info about the execution - not needed - <strong>BUT IT'S FUN!</strong></p>
<pre>
1000000 rows inserted in 34.0269999504 seconds
    (29388.4268803 rows per second)

</pre>
<p><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/12/tde-example.jpg" alt="" title="tde-example" width="510" height="400" class="aligncenter size-full wp-image-42563" /></p>
<h4>Here's the whole shebang, Cut-n-Paste Away!</h4>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;"># ryan robitaille 12/2/2012</span><br />
<span style="color: #808080; font-style: italic;"># really simple TDE creation test</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">import</span> dataextract <span style="color: #ff7700;font-weight:bold;">as</span> tde <span style="color: #808080; font-style: italic;">#saves some typing</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">time</span> <span style="color: #808080; font-style: italic;"># for file manipulation / script timing (not necc)</span><br />
<br />
start_time <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># simple timing for test purposes</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">try</span>: &nbsp;<span style="color: #808080; font-style: italic;"># Just for testing purposes and re-running</span><br />
&nbsp; &nbsp; tdefile <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Extract</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'test.tde'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#in CWD</span><br />
<span style="color: #ff7700;font-weight:bold;">except</span>: <br />
&nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'del test.tde'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'del DataExtract.log'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; tdefile <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Extract</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'test.tde'</span><span style="color: black;">&#41;</span><br />
<br />
tableDef <span style="color: #66cc66;">=</span> tde.<span style="color: black;">TableDefinition</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#create a new table def</span><br />
<br />
<span style="color: #808080; font-style: italic;"># using integers for now because the literal defs are not in the python module code</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type7&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span> &nbsp; <span style="color: #808080; font-style: italic;">#INTEGER</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type10&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#DOUBLE</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type11&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">11</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#BOOLEAN</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type12&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">12</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#DATE</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type13&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">13</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#DATETIME</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type14&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">14</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#DURATION</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type15&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#CHAR_STRING</span><br />
tableDef.<span style="color: black;">addColumn</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field_type16&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#UNICODE_STRING</span><br />
<br />
<span style="color: #808080; font-style: italic;"># ok, lets print out the table def, just for shits and giggles</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span>tableDef.<span style="color: black;">getColumnCount</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Column: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>tableDef.<span style="color: black;">getColumnName</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' Type: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>tableDef.<span style="color: black;">getColumnType</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;"># lets add the new def as a table in the extract</span><br />
tabletran <span style="color: #66cc66;">=</span> tdefile.<span style="color: black;">addTable</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Extract&quot;</span><span style="color: #66cc66;">,</span>tableDef<span style="color: black;">&#41;</span> <br />
<span style="color: #808080; font-style: italic;"># why table NEEDS to be called 'Extract' is beyond me</span><br />
<br />
rowsinserted <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span><br />
<br />
<span style="color: #808080; font-style: italic;"># let's create a bunch of rows! wheeeeee!</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1000000</span><span style="color: black;">&#41;</span>:<br />
<br />
&nbsp; &nbsp; newrow <span style="color: #66cc66;">=</span> tde.<span style="color: black;">Row</span><span style="color: black;">&#40;</span>tableDef<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#newrow.setNull(0) #column</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setInteger</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span>i<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, value</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setDouble</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span>i*<span style="color: #ff4500;">1.4</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, value</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setBoolean</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, value (1/0)</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setDate</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2012</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">12</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, y,m,d</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setDateTime</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2012</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">12</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">18</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0000</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, y,m,d,h,m,s,frac</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setDuration</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">18</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0000</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, d,h,m,s,frac</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setCharString</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'Character String'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, 'value'</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">setString</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">7</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'Unicode String!'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#column, 'value'</span><br />
&nbsp; &nbsp; rowsinserted <span style="color: #66cc66;">=</span> rowsinserted + <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;">#count this row!</span><br />
<br />
&nbsp; &nbsp; tabletran.<span style="color: black;">insert</span><span style="color: black;">&#40;</span>newrow<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#insert row into TDE table</span><br />
&nbsp; &nbsp; newrow.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
tdefile.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
timetaken <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - start_time <span style="color: #808080; font-style: italic;">#just for timing and fun</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>rowsinserted<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' rows inserted in '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>timetaken<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' seconds'</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' &nbsp; &nbsp;('</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>rowsinserted/timetaken<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' rows per second)'</span></div></div>
<p>What's next? Building TDEs from web screen-scraping as well as <a href="http://messytables.readthedocs.org/en/latest/index.html" target="_blank">using the MessyTables python module</a> for turning ugly CSV and XLS into sexified TDEs (hopefully). Stay tuned - this topic is somewhat of a moving target...</p>
<div class="woo-sc-box tick  rounded full"><strong>UPDATE: </strong>Changed the code loop to a million rows.. <strong>instead of a BILLION</strong>. Oops. For the record, on my desktop, generating a billion rows took 9.5 hours - approx 33,924 rows per second <em>(using 64-bit Python and Tableau libs)</em></div>
<p><strong>Want more? <a href="http://ryrobes.com/python/sql-server-query-to-tableau-data-extract-more-tde-api-fun-with-python-tableau-8/" title="SQL Server Query to Tableau Data Extract LIKE A BOSS – Some more TDE API fun with Python & Tableau 8">Here is a follow-up post creating TDE files from SQL Server queries</a>!</strong></p>
<p>The post <a href="http://ryrobes.com/python/building-tableau-data-extract-files-with-python-in-tableau-8-sample-usage/">Building Tableau Data Extract files with Python in Tableau 8 &#8211; Sample Usage</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Ryrobes?a=2HxfaWaAqqA:3A3QWONdLoE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=2HxfaWaAqqA:3A3QWONdLoE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=2HxfaWaAqqA:3A3QWONdLoE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=2HxfaWaAqqA:3A3QWONdLoE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=2HxfaWaAqqA:3A3QWONdLoE:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=2HxfaWaAqqA:3A3QWONdLoE:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Ryrobes/~4/2HxfaWaAqqA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ryrobes.com/python/building-tableau-data-extract-files-with-python-in-tableau-8-sample-usage/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://ryrobes.com/python/building-tableau-data-extract-files-with-python-in-tableau-8-sample-usage/</feedburner:origLink></item>
		<item>
		<title>Face Harvesting: Download your Facebook data for External Reporting and Visualization</title>
		<link>http://feedproxy.google.com/~r/Ryrobes/~3/z1wn69biw0I/</link>
		<comments>http://ryrobes.com/data/face-harvesting-download-your-facebook-data-for-external-reporting-and-visualization/#comments</comments>
		<pubDate>Wed, 17 Oct 2012 00:11:34 +0000</pubDate>
		<dc:creator>Ry</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[FQL]]></category>
		<category><![CDATA[harvest]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[open graph]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tableau]]></category>

		<guid isPermaLink="false">http://ryrobes.com/?p=42342</guid>
		<description><![CDATA[<p>I'm always on the lookout for cool new data feeds that I can mess with - as you can clearly see from some of the other posts on this site. Usually it involves some scripting, screen-scraping, system fiddling, and general madness - basically meaning that some people can get it to work, while others cannot. [...]</p><p>The post <a href="http://ryrobes.com/data/face-harvesting-download-your-facebook-data-for-external-reporting-and-visualization/">Face Harvesting: Download your Facebook data for External Reporting and Visualization</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p>]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/07/fbdata-thumb.jpg" width="240" />
		</p><p><a href="http://fixxer.ryrobes.com/wp-content/uploads/2012/07/fbdata-thumb.jpg"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/07/fbdata-thumb.jpg" alt="" title="Yeaaaah! Dataaaaa!" width="110" height="110" class="alignleft size-full wp-image-41715" /></a>I'm always on the lookout for cool new data feeds that I can mess with - as you can clearly see from some of the other posts on this site. </p>
<p>Usually it involves some scripting, screen-scraping, system fiddling, and general madness - basically meaning that some people can get it to work, while others cannot. This bothers me, so THIS little "data harvester"... well, this time it's all automatic, baby!</p>
<p>Anyways. where was I? Oh yeah, that's right - we're talking about the good ole' <i>Book of Faces</i> again...</p>
<h2>Facebook.</h2>
<h4>One of the great white whales of the data world.</h4>
<p>Thankfully instead of harpoon, <a href="http://developers.facebook.com/docs/reference/api/" target="_blank">we have their nifty Open Graph API</a>.</p>
<p><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/10/fql-function.jpg" style="border:0px;"></p>
<p>I previously <a href="http://ryrobes.com/python/how-to-harvest-facebook-updates-with-python-mysql-or-sql-server/" target="_blank" >posted about pulling data from the Facebook Open Graph</a> - but that was just an unauthenticated search on posts in the "public stream", this is completely different...</p>
<p>No need to cut and paste scripts this time. Basically, I made a small web app that (once properly authenticated and given permission) will download a decent portion of your Facebook records in MySQL tables and/or CSV files for you to do whatever you want with. Not only your personal data (friends, comments, posts, etc), but also "Page" data (insights, posts, likes, etc) for any Pages that you might be a administrator of. For a random user with no "Fan Pages" (like me), it's just a cool way to bring up your data and check it out (maybe in <a href="http://tableausoftware.com" target="_blank">Tableau</a>, hint, hint), but for someone who runs an actual Fan Page, it can be a great deal more interesting.</p>
<p>Hell, you could even massage this data, crank it into something like SPSS or R, and generate something <i>really</i> interesting.</p>
<p><strong>Here's whats going to happen.</strong></p>
<div class="shortcode-unorderedlist bullet"></p>
<ul>
<li>You will click the button below</li>
<li>You will be re-directed to log into Facebook</li>
<li>You will have to give the 'Data Harvester' app permission to see your FB data</li>
<li>You will be re-directed back to this site</li>
<li>A live counter will begin to show records pulled from key FB "tables"</li>
<li>A big-ass button will appear with a link to the "data download" page when it's done</li>
</ul>
<p></div>

<div id="tabs-92" class="shortcode-tabs boxed"><ul class="tab_titles">
<li class="nav-tab"><a href="#tab-1">Pulling Data!</a></li>
<li class="nav-tab"><a href="#tab-2">It&#039;s DONE!</a></li>
<li class="nav-tab"><a href="#tab-3">OMG MySQL!</a></li>
<li class="nav-tab"><a href="#tab-4">OMG CSV!</a></li>
</ul>
<div class="tab tab-pulling-data">We're off to the races, pullin' data!<br/><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/10/harvie1.jpg" style="border:0px;" alt="Pullin' Data" title="Pullin' Data"></div><!--/.tab--><div class="tab tab-its-done">Finally!<br/><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/10/harvie2.jpg" style="border:0px;" alt="Pullin' Data" title="Pullin' Data"></div><!--/.tab--><div class="tab tab-omg-mysql"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/10/harvie3.jpg" style="border:0px;" alt="Pullin' Data" title="Pullin' Data"></div><!--/.tab--><div class="tab tab-omg-csv"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/10/harvie4.jpg" style="border:0px;" alt="Pullin' Data" title="Pullin' Data"></div><!--/.tab-->
<div class="fix"></div><!--/.fix-->
</div><!--/.tabs-->
<p>Here's a shitty (and incomplete) diagram to give you an idea of what kind of data we will have access to.</p>
<p><a href="http://fixxer.ryrobes.com/wp-content/uploads/2012/10/facebook-dia2.jpg" target="_blank"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/10/facebook-dia2.jpg" style="border:0px;" width="500"></a></p>
<div class="woo-sc-box info   ">The actual data pull could take a <i>long ass</i> time - especially if you have several 'Fan Pages' with associated Insight Data. I'm looking into notification methods that don't require staying on the page for the download link.</div>
<div align="center"><a href="http://ryrobes.com/getfacedata/" class="woo-sc-button  custom xl" style="background:;border-color:"><span class="woo-download">Get Face Data NOW!</span></a></div>
<p><div class="woo-sc-box note   ">FYI, your data automagically gets deleted after 24 hours.</div><br />
I'm sure that there are tons of problems and bugs, so please don't be shy to let me know how it works for you! Cheers!</p>
<p>The post <a href="http://ryrobes.com/data/face-harvesting-download-your-facebook-data-for-external-reporting-and-visualization/">Face Harvesting: Download your Facebook data for External Reporting and Visualization</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Ryrobes?a=z1wn69biw0I:3EJKsS-PgR8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=z1wn69biw0I:3EJKsS-PgR8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=z1wn69biw0I:3EJKsS-PgR8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=z1wn69biw0I:3EJKsS-PgR8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=z1wn69biw0I:3EJKsS-PgR8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=z1wn69biw0I:3EJKsS-PgR8:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Ryrobes/~4/z1wn69biw0I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ryrobes.com/data/face-harvesting-download-your-facebook-data-for-external-reporting-and-visualization/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://ryrobes.com/data/face-harvesting-download-your-facebook-data-for-external-reporting-and-visualization/</feedburner:origLink></item>
		<item>
		<title>Turning Guitar Tabs into structured Data. Silicon crunching Metal.</title>
		<link>http://feedproxy.google.com/~r/Ryrobes/~3/X-q3GDBN1p4/</link>
		<comments>http://ryrobes.com/python/turning-guitar-tabs-into-structured-data-silicon-crunching-metal/#comments</comments>
		<pubDate>Tue, 07 Aug 2012 23:46:43 +0000</pubDate>
		<dc:creator>Ry</dc:creator>
				<category><![CDATA[Metallica]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tableau]]></category>
		<category><![CDATA[dataviz]]></category>
		<category><![CDATA[guitar tab]]></category>
		<category><![CDATA[metallica]]></category>
		<category><![CDATA[process tab files]]></category>
		<category><![CDATA[read tab files]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[tab file program]]></category>
		<category><![CDATA[tablature]]></category>

		<guid isPermaLink="false">http://ryrobes.com/?p=41937</guid>
		<description><![CDATA[<p>I’m always looking for data from unlikely sources, especially sources that are close to my own bizarre interests. Anyone who played guitar at one point or another has probably used tablature.  
What if we scripted out a way to crunch through an old school tab text file, split it up in a logical way, and insert it all into a database for display / analysis...</p><p>The post <a href="http://ryrobes.com/python/turning-guitar-tabs-into-structured-data-silicon-crunching-metal/">Turning Guitar Tabs into structured Data. Silicon crunching Metal.</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p>]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/08/sandman-viz.jpg" width="240" />
		</p><p>I'm always looking for data from unlikely sources, especially sources that are close to my own bizarre <em>(and predictable)</em> interests. Anyone who grew up learning <em>(or still plays)</em> guitar at one point or another has probably used <a href="http://en.wikipedia.org/wiki/Tablature" target="_blank">tablature</a> at one time or another.</p>
<p><a href="http://ryrobes.com/visual-analytics-and-data-porn/enter-tableauman-creating-useful-datavisualizations-with-old-school-guitar-tab-and-metallica/"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/08/sandman-viz.jpg" alt="Visualizing Enter Sandman Tab with Tableau" title="Visualizing Enter Sandman Tab with Tableau" width="240" height="303" class="alignleft size-full wp-image-41948" /></a></p>
<p>From the old Cherry Hill tab books of our favorite albums and artists, to the internet age where tens of thousands of plain-text format tabs because available all over the place <em>(of varying levels of correctness at times)</em>.</p>
<p>I've been playing off and on for the past 21 years - more "on" lately than any time in recent memory.</p>
<p>So, I had an idea. What if I were to script out a way to crunch through an old school tab text file, split it up in a usable / logical way, and insert it all into a database so I can analyze and display it later via different methods. What would the data tell me? </p>
<div class="shortcode-unorderedlist bullet"></p>
<ul>
<li>What if I could compile 100s of these files in this way?</li>
<li>What could I do with the data once it was transformed?</li>
<li>Would anyone even give a shit?</li>
</ul>
<p></div>

<h4>Well, I've taken the first step - both in the CRUNCHING side and on the VISUALIZING side.</h4>
<p>First, <a href="http://ryrobes.com/visual-analytics-and-data-porn/enter-tableauman-creating-useful-datavisualizations-with-old-school-guitar-tab-and-metallica/" target="_blank">the end product</a> - we can save the technical shit for AFTER. As you can see from the shot above. I took a (mostly correct) guitar tab for Metallica's Enter Sandman from ultimate-guitar.com - crunched it, note-for-note, into a database table and built a "analytic report" (using the term very loosely) out of it <a href="http://www.tableausoftware.com/public/community" target="_blank">using Tableau Public</a>.</p>
<p><a href="http://ryrobes.com/visual-analytics-and-data-porn/enter-tableauman-creating-useful-datavisualizations-with-old-school-guitar-tab-and-metallica/" target="_blank"><br />
<h4>Link: Learning Enter Sandman with Visual Analytics</h4>
<p></a></p>
<p>Now, on to the "how"...</p>
<p>If you look at my source tab, it's not that straightforward for computer reading with all the literal "3x", "12x" etc. I had to streamline it for line-by-line (and col-by-col) machine reading.  You can see the <a href="http://tabs.ultimate-guitar.com/m/metallica/enter_sandman_tab.htm" target="_blank">original tab I started with here</a>.</p>
<h4>My "machine-friendly streamlined" version</h4>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">?Metallica|Enter Sandman|The Black Album|http://tabs.ultimate-guitar.com/m/metallica/enter_sandman_tab.htm<br />
<br />
+01 Intro:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|------------5---------------------------5---------------------------5---------------------------5---------------------------5---------------------------5---------------<br />
5|--------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7---<br />
6|----0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|------------5---------------------------5---------------------------5---------------------------5---------------------------5---------------------------5---------------<br />
5|--------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7---<br />
6|----0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-------<br />
<br />
+02 Rhythm Fill 1:<br />
#| &nbsp; &nbsp; &nbsp;PM------------------- &nbsp; sl &nbsp; PM----------------------- &nbsp;sl &nbsp; &nbsp; PM----------------------- &nbsp;sl &nbsp; &nbsp; &nbsp;PM----------------------- &nbsp;sl &nbsp; PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------- &nbsp; &nbsp;P PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------- &nbsp; &nbsp;P PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------- &nbsp; &nbsp;P PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------- &nbsp; &nbsp;P <br />
1|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
5|--2---------------------------/7------------------------------/7--------------------------------/7---------------------------------/7-----------7------------------3\2--------7------------------3\2--------7------------------3\2--------7------------------3\2<br />
6|--0---0---0---0---0---0---0--------0---0---0---0---0---0---0---------0---0---0---0---0---0---0----------0---0---0---0---0---0---0-------0---0------6---0---0---0---1\00---0------6---0---0---0---1\00---0------6---0---0---0---1\00---0------6---0---0---0---1\0<br />
<br />
#|PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--- &nbsp; &nbsp; P &nbsp; PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--- &nbsp; &nbsp; P &nbsp; PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--- &nbsp; &nbsp; P &nbsp; PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--- &nbsp; &nbsp; P &nbsp; PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--- &nbsp; &nbsp; P &nbsp; PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--- &nbsp; &nbsp; P &nbsp; PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--- &nbsp; &nbsp; P &nbsp; PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--- &nbsp; &nbsp; P &nbsp; <br />
1|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
5|--------7-------------------3\2-----------7-------------------3\2-----------7-------------------3\2-----------7-------------------3\2-----------7-------------------3\2-----------7-------------------3\2-----------7-------------------3\2-----------7-------------------3\2---<br />
6|0---0-------6---5---0---0---1\0---0---0-------6---5---0---0---1\0---0---0-------6---5---0---0---1\0---0---0-------6---5---0---0---1\0---0---0-------6---5---0---0---1\0---0---0-------6---5---0---0---1\0---0---0-------6---5---0---0---1\0---0---0-------6---5---0---0---1\0---<br />
<br />
+03 Rhythm Fill 2:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------------------- &nbsp; &nbsp; &nbsp; <br />
1|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
5|--2---2----/7-------------2---2----/7-------------2---2----/7--------------5-------5-------4---5---4--------2---2----/7-------------2---2----/7-------------2---2----/7--------------5-------5-------4---5---4------<br />
6|--0---0---------6---5-----0---0---------6---5-----0---0---------6---5------3---0---3---0---2---3---2--------0---0---------6---5-----0---0---------6---5-----0---0---------6---5------3---0---3---0---2---3---2------<br />
<br />
+04 Rhythm Fill 3:<br />
#| &nbsp; &nbsp; &nbsp;PM------------------- &nbsp; &nbsp; P &nbsp; &nbsp;PM------------------------ &nbsp; P &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM----------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------------ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
5|--2----------------------------3\2-------------------------------3\2-----------------------------------------5-------5-------4---5---4------------------<br />
6|--0---0---0---0---0---0---0----1\0---0---0---0---0---0---0---0---1\0----------0---0---0---0---0---0---0------3---0---3---0---2---3---2------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp;PM------------------- &nbsp; &nbsp; P &nbsp; &nbsp;PM------------------------ &nbsp; P &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM----------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------------ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
5|--2----------------------------3\2-------------------------------3\2-----------------------------------------5-------5-------4---5---4------------------<br />
6|--0---0---0---0---0---0---0----1\0---0---0---0---0---0---0---0---1\0----------0---0---0---0---0---0---0------3---0---3---0---2---3---2------------------<br />
<br />
+05 Bridge:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--------------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|------------------------------------------------------------------------------------------------------------------------<br />
2|------------------------------------------------------------------------------------------------------------------------<br />
3|---------2---------------------2---------------------2------------------------------------------------------------------<br />
4|-----4---------------------4---------------------4----------------------------------------------------------------------<br />
5|-------------3---2-----------------3---2-----------------3---2-------4---4---4---4---4---2---3---4----------------------<br />
6|--2---------------------2---------------------2----------------------2---2---2---2---2---0---1---2----------------------<br />
<br />
+06 Chorus Fill:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM------------ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|----------------------------------------------------------------------------<br />
2|----------------------------------------------------------------------------<br />
3|----------------------------------------------------------------2-----------<br />
4|------5---4---------5---4---------5---4---------5---2---5---2-------2-------<br />
5|--4---3---2-----4---3---2-----4---3---2----2--------------------------------<br />
6|--2-------------2-------------2------------0--------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM----------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM----------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|----------------------------------------------------------------------------<br />
2|----------------------------------------------------------------------------<br />
3|------------------------------------2---------------------------------------<br />
4|------5---4---------5---2---5---2-------2-----------------------------------<br />
5|--4---3---2-----2----------------------------5-------5-------4---5---5------<br />
6|--2-------------0----------------------------3---0---3---0---2---3---3------<br />
<br />
+07 Rhythm Fill 2:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------------------- &nbsp; &nbsp; &nbsp; <br />
1|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
5|--2---2----/7-------------2---2----/7-------------2---2----/7--------------5-------5-------4---5---4--------2---2----/7-------------2---2----/7-------------2---2----/7--------------5-------5-------4---5---4------<br />
6|--0---0---------6---5-----0---0---------6---5-----0---0---------6---5------3---0---3---0---2---3---2--------0---0---------6---5-----0---0---------6---5-----0---0---------6---5------3---0---3---0---2---3---2------<br />
<br />
+08 Rhythm Fill 3:<br />
#| &nbsp; &nbsp; &nbsp;PM------------------- &nbsp; &nbsp; P &nbsp; &nbsp;PM------------------------ &nbsp; P &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM----------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------------ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
5|--2----------------------------3\2-------------------------------3\2-----------------------------------------5-------5-------4---5---4------------------<br />
6|--0---0---0---0---0---0---0----1\0---0---0---0---0---0---0---0---1\0----------0---0---0---0---0---0---0------3---0---3---0---2---3---2------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp;PM------------------- &nbsp; &nbsp; P &nbsp; &nbsp;PM------------------------ &nbsp; P &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM----------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------------ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|--------------------------------------------------------------------------------------------------------------------------------------------------------<br />
5|--2----------------------------3\2-------------------------------3\2-----------------------------------------5-------5-------4---5---4------------------<br />
6|--0---0---0---0---0---0---0----1\0---0---0---0---0---0---0---0---1\0----------0---0---0---0---0---0---0------3---0---3---0---2---3---2------------------<br />
<br />
+09 Bridge:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--------------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|------------------------------------------------------------------------------------------------------------------------<br />
2|------------------------------------------------------------------------------------------------------------------------<br />
3|---------2---------------------2---------------------2------------------------------------------------------------------<br />
4|-----4---------------------4---------------------4----------------------------------------------------------------------<br />
5|-------------3---2-----------------3---2-----------------3---2-------4---4---4---4---4---2---3---4----------------------<br />
6|--2---------------------2---------------------2----------------------2---2---2---2---2---0---1---2----------------------<br />
<br />
+10 Chorus Fill:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM------------ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|----------------------------------------------------------------------------<br />
2|----------------------------------------------------------------------------<br />
3|----------------------------------------------------------------2-----------<br />
4|------5---4---------5---4---------5---4---------5---2---5---2-------2-------<br />
5|--4---3---2-----4---3---2-----4---3---2----2--------------------------------<br />
6|--2-------------2-------------2------------0--------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM----------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM----------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|----------------------------------------------------------------------------<br />
2|----------------------------------------------------------------------------<br />
3|------------------------------------2---------------------------------------<br />
4|------5---4---------5---2---5---2-------2-----------------------------------<br />
5|--4---3---2-----2----------------------------5-------5-------4---5---5------<br />
6|--2-------------0----------------------------3---0---3---0---2---3---3------<br />
<br />
+11 Solo<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|-------------------------------------------------------------<br />
2|-15-15-15-12----12---------------12-12----15b17-15b17-(15)\--<br />
3|-------------14-----14b16---(14)-------14--------------------<br />
4|-------------------------------------------------------------<br />
5|-------------------------------------------------------------<br />
6|-------------------------------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|--------------------------------------------------<br />
2|--------------------------------------------------<br />
3|-12/14-14-14-14\12-12-12-12/14-14-14-14\12-12-12--<br />
4|--------------------------------------------------<br />
5|--------------------------------------------------<br />
6|--------------------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|------------------------------------------<br />
2|------------------------------------------<br />
3|-14p12------------------------------------<br />
4|-------14--14p12--------------------------<br />
5|-----------------14--14p12\10-------------<br />
6|------------------------------12-~-(12)\--<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|-------12-12-12-------12-12-12-------12-12-12-------12-12-12--<br />
2|-15b17----------15b17----------15b17----------15b17-----------<br />
3|--------------------------------------------------------------<br />
4|--------------------------------------------------------------<br />
5|--------------------------------------------------------------<br />
6|--------------------------------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|-------12-12-12h15p12-12h15p12-12h15p12-12-15-12-12-14-14--<br />
2|-15b17-----------------------------------------------------<br />
3|-----------------------------------------------------------<br />
4|-----------------------------------------------------------<br />
5|-----------------------------------------------------------<br />
6|-----------------------------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|-17p14----17p14----17p14-19p15----19p15----19p15--<br />
2|-------15-------15-------------17-------17--------<br />
3|--------------------------------------------------<br />
4|--------------------------------------------------<br />
5|--------------------------------------------------<br />
6|--------------------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|-21p17----21p17----21p17----22b24-(22)\-------------------------<br />
2|-------17-------17-------17-------------9b10---(9)---9b10---9---<br />
3|----------------------------------------9b11---(9)---9b11---9---<br />
4|----------------------------------------------------------------<br />
5|----------------------------------------------------------------<br />
6|----------------------------------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|-------------------------------------------------------------------<br />
2|-9b10--9b10-----------------10---(10)----10-8-7---8\7---7----------<br />
3|-9b11--9b11--7--9--------7----------------------9-----9---9-7---7--<br />
4|--------------------7h9---------------------------------------9----<br />
5|-------------------------------------------------------------------<br />
6|-------------------------------------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|------------------------------------------------------------------<br />
2|------------------------------------------------------------------<br />
3|-9p7---------------------------16b18-14-----14-16b18--14------14--<br />
4|-----9p7---7-9-7------------------------16----------------16------<br />
5|---------9-------9-8-7--------------------------------------------<br />
6|-----------------------10-7\--------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|------------------------------------<br />
2|------------------------------------<br />
3|-16b18-14----14-16b18-14--------14--<br />
4|----------16-------------16--16-----<br />
5|------------------------------------<br />
6|------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|-----------------------------------------------------------------------<br />
2|-------17-------17------------17---------------------------------------<br />
3|-16b18----16b18----16b18---------16b18---(16)-\1/-\1/-\1/-\1/-\1/-\1/--<br />
4|-----------------------------------------------------------------------<br />
5|----------------------------------------------accentuate-w/bar---------<br />
6|-----------------------------------------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|-21p17----21p17----21p17----21p17----21p17----21p17-----<br />
2|-------17-------17-------17-------17-------17-------17--<br />
3|--------------------------------------------------------<br />
4|--------------------------------------------------------<br />
5|--------------------------------------------------------<br />
6|--------------------------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|-22b24--22-22-22b24--22-22-22b24-~-~-(22)\--<br />
2|--------------------------------------------<br />
3|--------------------------------------------<br />
4|--------------------------------------------<br />
5|--------------------------------------------<br />
6|--------------------------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|--------------------------------------------------------<br />
2|-10p8p7-------------------------------------------------<br />
3|--------9--9p7------------------------------------------<br />
4|---------------9--9p7-5--7b9---5------------------------<br />
5|----------------------------------7---(7)-~-~-~----5h7--<br />
6|------------------------------------------w/bar---------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|------------------------------------------------------<br />
2|------------------------------------------------------<br />
3|------------------------------------------------------<br />
4|------------------------------------------------------<br />
5|-4---(4)-~-~-~-~-----0----2--\1/-\1/-\1/-\1/-\1/-\1/--<br />
6|------------------------------------------------------<br />
<br />
<br />
+12 Prayer Intro:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|------------5---------------------------5---------------------------5---------------------------5---------------------------5---------------------------5---------------------------5---------------------------5---------------<br />
5|--------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7-----------7---------------7---<br />
6|----0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-----------0-----------6---5-------<br />
<br />
<br />
+13 Bridge:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--------------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM--------------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|---------2---------------------2---------------------2----------------------2----------------------2---------------------2----------------------------------------------------------------<br />
4|-----4---------------------4---------------------4----------------------4----------------------4---------------------4--------------------------------------------------------------------<br />
5|-------------3---2-----------------3---2-----------------3---2------------------3---2------------------3---2-----------------3---2-----4---4---4---4---4---2---3---4----------------------<br />
6|--2---------------------2---------------------2----------------------2----------------------2---------------------2--------------------2---2---2---2---2---0---1---2----------------------<br />
<br />
+14 Chorus Fill:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM------------ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PM------------ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|----------------------------------------------------------------2--------------------------------------------------------------------------------------------------------------2------<br />
4|------5---4---------5---4---------5---4---------5---2---5---2-------2--------5---4-----------------------------------5---4---------5---4---------5---4---------5---2---5---2-------2--<br />
5|--4---3---2-----4---3---2-----4---3---2----2-----------------------------4---3---2-----2-------------------------4---3---2-----4---3---2-----4---3---2----2---------------------------<br />
6|--2-------------2-------------2------------0-----------------------------2-------------0-------------------------2-------------2-------------2------------0---------------------------<br />
<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM----------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM----------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
1|----------------------------------------------------------------------------<br />
2|----------------------------------------------------------------------------<br />
3|------------------------------------2---------------------------------------<br />
4|------5---4---------5---2---5---2-------2-----------------------------------<br />
5|--4---3---2-----2----------------------------5-------5-------4---5---5--2---<br />
6|--2-------------0----------------------------3---0---3---0---2---3---3--0---<br />
<br />
<br />
<br />
+15 Rhythm Fill 2:<br />
#| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp;BOOOM! &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sl &nbsp; &nbsp; &nbsp; V &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------------------- &nbsp; &nbsp; &nbsp; <br />
1|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
5|--2---2----/7-------------2---2----/7-------------2---2----/7-------------2---2----/7-------------2---2----/7-------------2---2----/7-------------2---2----/7--------------5-------5-------4---5---4------<br />
6|--0---0---------6---5-----0---0---------6---5-----0---0---------6---5-----0---0---------6---5-----0---0---------6---5-----0---0---------6---5-----0---0---------6---5------3---0---3---0---2---3---2------<br />
<br />
+16 Outro:<br />
#| &nbsp; &nbsp; &nbsp;PM &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM--- &nbsp; &nbsp;P &nbsp; &nbsp;PM---------------------- &nbsp; &nbsp;P &nbsp; &nbsp;PM---------------------- &nbsp; &nbsp;P &nbsp; &nbsp;PM---------------------- &nbsp; &nbsp;P &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|--------------------------------------------------------------------------------------------------------------------------------------------<br />
2|--------------------------------------------------------------------------------------------------------------------------------------------<br />
3|--------------------------------------------------------------------------------------------------------------------------------------------<br />
4|--------------------------------------------------------------------------------------------------------------------------------------------<br />
5|--2-------7-------------------3\2-----------7------------------3\2-----------7------------------3\2-----------7------------------3\2--------<br />
6|--0---0-------6---5---0---0---1\0---0---0------6---5---0---0---1\0---0---0------6---5---0---0---1\0---0---0------6---5---0---0---1\0--------<br />
<br />
#| &nbsp;PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------- &nbsp; &nbsp;P &nbsp; &nbsp; &nbsp;PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------- &nbsp; &nbsp;P &nbsp; &nbsp; &nbsp;PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------- &nbsp; &nbsp;P &nbsp; &nbsp; &nbsp;PM--- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PM------- &nbsp; &nbsp;P &nbsp; &nbsp; &nbsp;PM--------------------------- &nbsp; PM--------------------------- &nbsp; PM--------------------------- &nbsp; PM--------------------------- &nbsp; &nbsp; &nbsp; <br />
1|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
2|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
3|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
4|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------<br />
5|----------7------------------3\2-------------7------------------3\2-------------7------------------3\2-------------7------------------3\2-------------7-------------------------------7-------------------------------7-------------------------------7---------------------------<br />
6|--0---0------6---0---0---0---1\0-----0---0------6---0---0---0---1\0-----0---0------6---0---0---0---1\0-----0---0------6---0---0---0---1\0-----0---0-------0---0---0---0---0---0---0-------0---0---0---0---0---0---0-------0---0---0---0---0---0---0-------0---0---0---0---0-------<br />
<br />
#| &nbsp;PM------------------------------- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
1|----------------------------------------------------------------------------<br />
2|----------------------------------------------------------------------------<br />
3|----------------------------------------------------------------------------<br />
4|----------------------------------------------------------------------------<br />
5|----------------------------------------------------------------------------<br />
6|--0---0---0---0---0---0---0---0---0-----------------------------------------</div></div>
<p>Note that I repeated sections where it was needed and evened out all the whitespace in regards to PalmMuting, slide, etc notations. </p>
<p>I also created a tagging system for both what "part" we were on as well as the sequence of that part <em>(really important for re-ordering data in a viz, especially when you have repeating "parts" that may or may not be EXACTLY the same)</em>. Which made things slightly more complicated than I anticipated, but not much. </p>
<p>The cool thing about sequencing LINEAR rhythm parts <em>(no multi-part overlapping yet, including solos)</em> is that you can build Gantt charts out of it - and they actually make sense from a arrangement perspective.</p>
<p><a href="http://ryrobes.com/visual-analytics-and-data-porn/enter-tableauman-creating-useful-datavisualizations-with-old-school-guitar-tab-and-metallica/"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/08/gantt-sample.jpg" alt="" title="gantt-sample" width="510" height="285" class="aligncenter size-full wp-image-42007" /></a></p>
<p>My Python way of converting this into a table was to use multiple dictionaries and be very conscience of column number and "line type" so they can iterated back through at the end and have the same orientation, string-wise. There are probably better ways to do this <em>(ones that have more fault-tolerance as far as whitespace goes, etc)</em>, but this was the fastest way I could think of - and it only came out to around ~130 lines <em>(and is already fast as hell)</em>.</p>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> requests<span style="color: #66cc66;">,</span> <span style="color: #dc143c;">pprint</span><span style="color: #66cc66;">,</span> json<span style="color: #66cc66;">,</span> pymssql<span style="color: #66cc66;">,</span> psutil<span style="color: #66cc66;">,</span> <span style="color: #dc143c;">urllib</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">os</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">string</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">sys</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">time</span><br />
<span style="color: #ff7700;font-weight:bold;">import</span> pymssql<br />
<br />
mssql_db <span style="color: #66cc66;">=</span> pymssql.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>host<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'localhost:1450'</span><span style="color: #66cc66;">,</span> &nbsp;<span style="color: #dc143c;">user</span><span style="color: #66cc66;">=</span><span style="color: #483d8b;">'sa'</span><span style="color: #66cc66;">,</span> password<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'****'</span><span style="color: #66cc66;">,</span> database<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'FretLogic'</span><span style="color: black;">&#41;</span><br />
mssql_cursor <span style="color: #66cc66;">=</span> mssql_db.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
pp <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">pprint</span>.<span style="color: black;">PrettyPrinter</span><span style="color: black;">&#40;</span>indent<span style="color: #66cc66;">=</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;"># For converting WELL FORMATTED (FULL) tablature to MSSQL Server Tables for later analysis...</span><br />
f <span style="color: #66cc66;">=</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'sandmantab-modded.txt'</span><span style="color: black;">&#41;</span><br />
<br />
line <span style="color: #66cc66;">=</span> f.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
PMfretlogic <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
E1fretlogic <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
B2fretlogic <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
G3fretlogic <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
D4fretlogic <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
A5fretlogic <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
E6fretlogic <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
PRTfretlogic <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
POfretlogic <span style="color: #66cc66;">=</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
part_type <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">' '</span><br />
<br />
nummPM <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
nummE1 <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
nummB2 <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
nummG3 <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
nummD4 <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
nummA5 <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
nummE6 <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
<br />
<br />
<span style="color: #ff7700;font-weight:bold;">for</span> lineno<span style="color: #66cc66;">,</span> linestr <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;line number: &quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>lineno<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;: &quot;</span> + linestr.<span style="color: black;">rstrip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; rowtype <span style="color: #66cc66;">=</span> linestr<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;">#what string, what type of &nbsp;data</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> rowtype<br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'+'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; part_type <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>linestr.<span style="color: black;">rstrip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'+'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">':'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; part_order <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_type<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_type<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> part_order<br />
&nbsp; &nbsp; &nbsp; &nbsp; part_type <span style="color: #66cc66;">=</span> part_type<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> part_type<br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'?'</span>: <span style="color: #808080; font-style: italic;">#grab song data from ?string|shit</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">##Artist|Song|Album|link</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; artist_data <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>linestr.<span style="color: black;">rstrip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'?'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; artist <span style="color: #66cc66;">=</span> artist_data.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; song <span style="color: #66cc66;">=</span> artist_data.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; album <span style="color: #66cc66;">=</span> artist_data.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; links <span style="color: #66cc66;">=</span> artist_data.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'|'</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#print part_type</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> colno<span style="color: #66cc66;">,</span> linecol <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>linestr<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#print &quot;line number: &quot; + str(lineno) + &quot; col number: &quot; + str(colno) + &quot;: &quot; + linecol.rstrip()</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> rowtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'#'</span>: &nbsp; <span style="color: #808080; font-style: italic;">#'PALM MUTER line'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> linecol <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #483d8b;">'|'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PMfretlogic<span style="color: black;">&#91;</span>nummPM<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>linecol<span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PRTfretlogic<span style="color: black;">&#91;</span>nummPM<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_type<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; POfretlogic<span style="color: black;">&#91;</span>nummPM<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_order<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nummPM <span style="color: #66cc66;">=</span> nummPM + <span style="color: #ff4500;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> rowtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'1'</span>: <span style="color: #808080; font-style: italic;">#'1E'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> linecol <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #483d8b;">'|'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; E1fretlogic<span style="color: black;">&#91;</span>nummE1<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>linecol<span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PRTfretlogic<span style="color: black;">&#91;</span>nummE1<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_type<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; POfretlogic<span style="color: black;">&#91;</span>nummE1<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_order<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nummE1 <span style="color: #66cc66;">=</span> nummE1 + <span style="color: #ff4500;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> rowtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'2'</span>: <span style="color: #808080; font-style: italic;">#'2B'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> linecol <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #483d8b;">'|'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B2fretlogic<span style="color: black;">&#91;</span>nummB2<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>linecol<span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PRTfretlogic<span style="color: black;">&#91;</span>nummB2<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_type<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; POfretlogic<span style="color: black;">&#91;</span>nummB2<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_order<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nummB2 <span style="color: #66cc66;">=</span> nummB2 + <span style="color: #ff4500;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> rowtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'3'</span>: <span style="color: #808080; font-style: italic;">#'3G'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> linecol <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #483d8b;">'|'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; G3fretlogic<span style="color: black;">&#91;</span>nummG3<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>linecol<span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PRTfretlogic<span style="color: black;">&#91;</span>nummG3<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_type<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; POfretlogic<span style="color: black;">&#91;</span>nummG3<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_order<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nummG3 <span style="color: #66cc66;">=</span> nummG3 + <span style="color: #ff4500;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> rowtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'4'</span>: <span style="color: #808080; font-style: italic;">#'4D'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> linecol <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #483d8b;">'|'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D4fretlogic<span style="color: black;">&#91;</span>nummD4<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>linecol<span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PRTfretlogic<span style="color: black;">&#91;</span>nummD4<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_type<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; POfretlogic<span style="color: black;">&#91;</span>nummD4<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_order<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nummD4 <span style="color: #66cc66;">=</span> nummD4 + <span style="color: #ff4500;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> rowtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'5'</span>: <span style="color: #808080; font-style: italic;">#'5A'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> linecol <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #483d8b;">'|'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; A5fretlogic<span style="color: black;">&#91;</span>nummA5<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>linecol<span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PRTfretlogic<span style="color: black;">&#91;</span>nummA5<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_type<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; POfretlogic<span style="color: black;">&#91;</span>nummA5<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_order<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nummA5 <span style="color: #66cc66;">=</span> nummA5 + <span style="color: #ff4500;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> rowtype <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'6'</span>: <span style="color: #808080; font-style: italic;">#'6E'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> linecol <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #483d8b;">'|'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; E6fretlogic<span style="color: black;">&#91;</span>nummE6<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>linecol<span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PRTfretlogic<span style="color: black;">&#91;</span>nummE6<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_type<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; POfretlogic<span style="color: black;">&#91;</span>nummE6<span style="color: black;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>part_order<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nummE6 <span style="color: #66cc66;">=</span> nummE6 + <span style="color: #ff4500;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<br />
<span style="color: #808080; font-style: italic;">#pp.pprint(B2fretlogic)</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'cols '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummPM<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummE1<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummB2<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummG3<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummD4<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummA5<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummE6<span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'lines '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>lineno<span style="color: black;">&#41;</span><br />
<br />
<br />
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>nummE1<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">try</span>:<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>E1fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'1'</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>B2fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'2'</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>G3fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'3'</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>D4fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'4'</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>A5fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'5'</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>E6fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'6'</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">pass</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>E1fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">''</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">pass</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'-'</span> +<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>artist<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>song<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>album<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>links<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> +<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>PRTfretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>PMfretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>E1fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>B2fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + &nbsp;<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>G3fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + &nbsp;<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>D4fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + &nbsp;<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>A5fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + &nbsp;<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>E6fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mssql_cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;&quot;insert into FretLogic.dbo.TabData (PickId, Test, Artist, Song, Album, TabLink, Part, PartOrder, PM, E1, B2, G3, D4, A5, E6) values ('&quot;&quot;&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', GETDATE(), '&quot;&quot;&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>artist<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>song<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>album<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>links<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> +<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>PRTfretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> +<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>POfretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>PMfretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>E1fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>B2fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> + &nbsp;<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>G3fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> + &nbsp;<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>D4fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> + &nbsp;<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>A5fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;', '&quot;&quot;&quot;</span> + &nbsp;<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>E6fretlogic<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&quot;&quot;' )&quot;&quot;&quot;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'cols '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummPM<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummE1<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummB2<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummG3<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummD4<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummA5<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">' &nbsp;'</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>nummE6<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'lines '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>lineno<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">exc_info</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;">#debug for fuckups</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">exc_info</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;">#debug for fuckups</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span> artist<br />
<span style="color: #ff7700;font-weight:bold;">print</span> song<br />
<span style="color: #ff7700;font-weight:bold;">print</span> album<br />
<br />
mssql_cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;update TabData set Vert = (E1 + CHAR(13) + B2 + CHAR(13) + G3 + CHAR(13) + D4 + CHAR(13) + A5 + CHAR(13) + E6)&quot;</span><span style="color: black;">&#41;</span><br />
<br />
mssql_db.<span style="color: black;">commit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">#Let's drink!</span></div></div>
<h4>On my TO DO List:</h4>
<div class="shortcode-unorderedlist bullet"></p>
<ul>
<li>Actual note lookup (i.e. being able to name chords, E5, and single notes etc)</li>
<li>much less human intervention required on the source files</li>
<li>Lyrics as new line in tab reading?</li>
<li>Some type of 1:1 time references, some permitting</li>
</ul>
<p></div>

<p>Regardless, I'm def going to "viz out" a few more song's off 'The Black Album' just for the hell of it. I think I can make some usability improvements over this one.</p>
<p>The post <a href="http://ryrobes.com/python/turning-guitar-tabs-into-structured-data-silicon-crunching-metal/">Turning Guitar Tabs into structured Data. Silicon crunching Metal.</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Ryrobes?a=X-q3GDBN1p4:_bjf2NkW2CE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=X-q3GDBN1p4:_bjf2NkW2CE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=X-q3GDBN1p4:_bjf2NkW2CE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=X-q3GDBN1p4:_bjf2NkW2CE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=X-q3GDBN1p4:_bjf2NkW2CE:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=X-q3GDBN1p4:_bjf2NkW2CE:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Ryrobes/~4/X-q3GDBN1p4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ryrobes.com/python/turning-guitar-tabs-into-structured-data-silicon-crunching-metal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://ryrobes.com/python/turning-guitar-tabs-into-structured-data-silicon-crunching-metal/</feedburner:origLink></item>
		<item>
		<title>Python Snippet: Sending HTML email with an attachment via Google Apps SMTP (or Gmail)</title>
		<link>http://feedproxy.google.com/~r/Ryrobes/~3/CsIS6rxnR3E/</link>
		<comments>http://ryrobes.com/python/python-snippet-sending-html-email-with-an-attachment-via-google-apps-smtp-or-gmail/#comments</comments>
		<pubDate>Thu, 26 Jul 2012 22:14:42 +0000</pubDate>
		<dc:creator>Ry</dc:creator>
				<category><![CDATA[Cut-n-Paste Code]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[attachment]]></category>
		<category><![CDATA[cheers]]></category>
		<category><![CDATA[cliff claven]]></category>
		<category><![CDATA[google apps]]></category>
		<category><![CDATA[mime encoding]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[tls]]></category>

		<guid isPermaLink="false">http://ryrobes.com/?p=41829</guid>
		<description><![CDATA[<p>This is just one of those things that I had to do in a project (as part of a much bigger system), and wish I had it all laid out for me like this when I was writing it.

Here it is for your cut-n-paste hacking pleasure...</p><p>The post <a href="http://ryrobes.com/python/python-snippet-sending-html-email-with-an-attachment-via-google-apps-smtp-or-gmail/">Python Snippet: Sending HTML email with an attachment via Google Apps SMTP (or Gmail)</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p>]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/07/mailman-300x256.jpg" width="240" />
		</p><p><a href="http://fixxer.ryrobes.com/wp-content/uploads/2012/07/mailman.jpg"><img src="http://fixxer.ryrobes.com/wp-content/uploads/2012/07/mailman-150x150.jpg" alt="" title="mailman" width="150" height="150" class="alignleft size-thumbnail wp-image-41846" /></a>This is just one of those things that I had to do in a project (as part of a much bigger system), and wish I had it all laid out for me like this when I was writing it.</p>
<p>I broke it down into simple input variables that need to be filled out, and then let 'er rip.</p>
<p>You supply the account credentials, to address, from address, subject, HTML body (will be converted to plain as well, in case the email reader cannot support HTML for whatever reason), etc and everything else gets taken care of.</p>
<p>Should be easy to take the whole thing and throw it into a loop from a DB resultset, or something of that nature.</p>
<blockquote><p>I initially wrote it to be used as part of a system that sent out different versions of "welcome" emails, as well as automated follow-up emails based on customer data and bunch of other shit. Fun times!</p></blockquote>
<h4>You're WELCOME!</h4>
<div class="codecolorer-container python blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">smtplib</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">os</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">re</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">sys</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">glob</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">string</span><span style="color: #66cc66;">,</span> <span style="color: #dc143c;">datetime</span><br />
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">MIMEMultipart</span> <span style="color: #ff7700;font-weight:bold;">import</span> MIMEMultipart<br />
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">MIMEBase</span> <span style="color: #ff7700;font-weight:bold;">import</span> MIMEBase<br />
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">MIMEText</span> <span style="color: #ff7700;font-weight:bold;">import</span> MIMEText<br />
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span> <span style="color: #ff7700;font-weight:bold;">import</span> Encoders<br />
<br />
<br />
<br />
<br />
<span style="color: #808080; font-style: italic;">#attachmentname = 'C:\\python27\\email.log' #path to an attachment, if you wish</span><br />
<br />
username <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'someguy@mygoogleapps-or-gmail.com'</span><br />
password <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'superSECRETdinoCLUBpasswords!!'</span><br />
<br />
fromaddr <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'Some Guy &lt;someguy@mygoogleapps-or-gmail.com&gt;'</span> <span style="color: #808080; font-style: italic;">#must be a vaild 'from' addy in your GApps account</span><br />
toaddr &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'someoneelse@eelsandheels.com'</span><br />
replyto <span style="color: #66cc66;">=</span> fromaddr <span style="color: #808080; font-style: italic;">#unless you want a different reply-to</span><br />
<br />
msgsubject <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'This is for sexy email subjektz!'</span><br />
<br />
htmlmsgtext <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">&quot;&quot;&quot;This is my message body in HTML&lt;br/&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Yup. Yup. Yup.&lt;/br&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;b&gt;Done!&lt;/a&gt;&quot;&quot;&quot;</span><br />
<br />
<br />
<br />
<br />
<br />
<span style="color: #808080; font-style: italic;">#ok, here goes nothing</span><br />
<span style="color: #ff7700;font-weight:bold;">try</span>:<br />
<br />
&nbsp; &nbsp; msgtext <span style="color: #66cc66;">=</span> htmlmsgtext.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;b&gt;'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;/b&gt;'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;br&gt;'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;/br&gt;'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;br/&gt;'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;/a&gt;'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; msgtext <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&lt;.*?&gt;'</span><span style="color: #66cc66;">,</span><span style="color: #483d8b;">''</span><span style="color: #66cc66;">,</span>msgtext<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#pain the ass mimey stuff</span><br />
&nbsp; &nbsp; msg <span style="color: #66cc66;">=</span> MIMEMultipart<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; msg.<span style="color: black;">preamble</span> <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'This is a multi-part message in MIME format.<span style="color: #000099; font-weight: bold;">\n</span>'</span><br />
&nbsp; &nbsp; msg.<span style="color: black;">epilogue</span> <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">''</span><br />
<br />
&nbsp; &nbsp; body <span style="color: #66cc66;">=</span> MIMEMultipart<span style="color: black;">&#40;</span><span style="color: #483d8b;">'alternative'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; body.<span style="color: black;">attach</span><span style="color: black;">&#40;</span>MIMEText<span style="color: black;">&#40;</span>msgtext<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; body.<span style="color: black;">attach</span><span style="color: black;">&#40;</span>MIMEText<span style="color: black;">&#40;</span>htmlmsgtext<span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'html'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; msg.<span style="color: black;">attach</span><span style="color: black;">&#40;</span>body<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'attachmentname'</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">globals</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;">#DO WE HAZ ATTACHMENT?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; f <span style="color: #66cc66;">=</span> attachmentname<br />
&nbsp; &nbsp; &nbsp; &nbsp; part <span style="color: #66cc66;">=</span> MIMEBase<span style="color: black;">&#40;</span><span style="color: #483d8b;">'application'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">&quot;octet-stream&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; part.<span style="color: black;">set_payload</span><span style="color: black;">&#40;</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>f<span style="color: #66cc66;">,</span><span style="color: #483d8b;">&quot;rb&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Encoders.<span style="color: black;">encode_base64</span><span style="color: black;">&#40;</span>part<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; part.<span style="color: black;">add_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Content-Disposition'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'attachment; filename=&quot;%s&quot;'</span> % <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">basename</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; msg.<span style="color: black;">attach</span><span style="color: black;">&#40;</span>part<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; msg.<span style="color: black;">add_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'From'</span><span style="color: #66cc66;">,</span> fromaddr<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; msg.<span style="color: black;">add_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'To'</span><span style="color: #66cc66;">,</span> toaddr<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">##msg.add_header('Cc', ccaddy) &nbsp; &nbsp;#doesn't work apparently</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">##msg.add_header('Bcc', bccaddy) &nbsp;#doesn't work apparently</span><br />
&nbsp; &nbsp; msg.<span style="color: black;">add_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Subject'</span><span style="color: #66cc66;">,</span> msgsubject<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; msg.<span style="color: black;">add_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Reply-To'</span><span style="color: #66cc66;">,</span> replyto<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;"># The actual email sendy bits</span><br />
&nbsp; &nbsp; server <span style="color: #66cc66;">=</span> <span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTP</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'smtp.gmail.com:587'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; server.<span style="color: black;">set_debuglevel</span><span style="color: black;">&#40;</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#commenting this out, changing to False will make the script give NO output at all upon successful completion</span><br />
&nbsp; &nbsp; server.<span style="color: black;">starttls</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; server.<span style="color: black;">login</span><span style="color: black;">&#40;</span>username<span style="color: #66cc66;">,</span>password<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; server.<span style="color: black;">sendmail</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'From'</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: black;">&#91;</span>msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'To'</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> msg.<span style="color: black;">as_string</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; server.<span style="color: black;">quit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#bye bye</span><br />
<br />
<br />
<br />
<br />
<span style="color: #ff7700;font-weight:bold;">except</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Email NOT sent to %s successfully. %s ERR: %s %s %s '</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>toaddr<span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'tete'</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">exc_info</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">exc_info</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">exc_info</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#just in case</span></div></div>
<h4>Sample Output</h4>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">send: 'ehlo [0.0.0.0]\r\n'<br />
reply: '250-mx.google.com at your service, [67.248.53.114]\r\n'<br />
reply: '250-SIZE 35882577\r\n'<br />
reply: '250-8BITMIME\r\n'<br />
reply: '250-STARTTLS\r\n'<br />
reply: '250 ENHANCEDSTATUSCODES\r\n'<br />
reply: retcode (250); Msg: mx.google.com at your service, [67.248.53.114]<br />
SIZE 35882577<br />
8BITMIME<br />
STARTTLS<br />
ENHANCEDSTATUSCODES<br />
send: 'STARTTLS\r\n'<br />
reply: '220 2.0.0 Ready to start TLS\r\n'<br />
reply: retcode (220); Msg: 2.0.0 Ready to start TLS<br />
send: 'ehlo [0.0.0.0]\r\n'<br />
reply: '250-mx.google.com at your service, [67.248.53.114]\r\n'<br />
reply: '250-SIZE 35882577\r\n'<br />
reply: '250-8BITMIME\r\n'<br />
reply: '250-AUTH LOGIN PLAIN XOAUTH\r\n'<br />
reply: '250 ENHANCEDSTATUSCODES\r\n'<br />
reply: retcode (250); Msg: mx.google.com at your service, [67.248.53.114]<br />
SIZE 35882577<br />
8BITMIME<br />
AUTH LOGIN PLAIN XOAUTH<br />
ENHANCEDSTATUSCODES<br />
<br />
reply: '235 2.7.0 Accepted\r\n'<br />
reply: retcode (235); Msg: 2.7.0 Accepted<br />
send: 'mail FROM:&lt;emailpeep@whatever.com&gt; size=878\r\n'<br />
reply: '250 2.1.0 OK bo5sm543779qab.1\r\n'<br />
reply: retcode (250); Msg: 2.1.0 OK bo5sm543779qab.1<br />
send: 'rcpt TO:&lt;emailpeep@whatever.com&gt;\r\n'<br />
reply: '250 2.1.5 OK bo5sm543779qab.1\r\n'<br />
reply: retcode (250); Msg: 2.1.5 OK bo5sm543779qab.1<br />
send: 'data\r\n'<br />
reply: '354 &nbsp;Go ahead bo5sm543779qab.1\r\n'<br />
reply: retcode (354); Msg: Go ahead bo5sm543779qab.1<br />
data: (354, 'Go ahead bo5sm543779qab.1')<br />
send: 'Content-Type: multipart/mixed; boundary=&quot;===============0988883067==&quot;\r\nMIME-Version: 1.0\r\nFrom: Some Guy &lt;emailpeep@whatever.com&gt;\r\nTo: emailpeep@whatever.com\r\nSubject: This is for sexy email su<br />
bjektz!\r\nReply-To: Some Guy &lt;emailpeep@whatever.com&gt;\r\n\r\nThis is a multi-part message in MIME format.\r\n\r\n--===============0988883067==\r\nContent-Type: multipart/alternative; boundary=&quot;===============13<br />
70445963==&quot;\r\nMIME-Version: 1.0\r\n\r\n--===============1370445963==\r\nContent-Type: text/plain; charset=&quot;us-ascii&quot;\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\n\r\nThis is my message body in HTML\r<br />
\n\t\t\t\tYup. Yup. Yup.\r\n\t\t\t\tDone!\r\n--===============1370445963==\r\nContent-Type: text/html; charset=&quot;us-ascii&quot;\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\n\r\nThis is my message body in HT<br />
ML&lt;br/&gt;\r\n\t\t\t\tYup. Yup. Yup.&lt;/br&gt;\r\n\t\t\t\t&lt;b&gt;Done!&lt;/a&gt;\r\n--===============1370445963==--\r\n--===============0988883067==--\r\n.\r\n'<br />
reply: '250 2.0.0 OK 1343338799 bo5sm543779qab.1\r\n'<br />
reply: retcode (250); Msg: 2.0.0 OK 1343338799 bo5sm543779qab.1<br />
data: (250, '2.0.0 OK 1343338799 bo5sm543779qab.1')<br />
send: 'quit\r\n'<br />
reply: '221 2.0.0 closing connection bo5sm543779qab.1\r\n'<br />
reply: retcode (221); Msg: 2.0.0 closing connection bo5sm543779qab.1</div></div>
<p>The post <a href="http://ryrobes.com/python/python-snippet-sending-html-email-with-an-attachment-via-google-apps-smtp-or-gmail/">Python Snippet: Sending HTML email with an attachment via Google Apps SMTP (or Gmail)</a> appeared first on <a href="http://ryrobes.com">Ryan Robitaille</a>.</p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Ryrobes?a=CsIS6rxnR3E:YEPeM34Wdq0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=CsIS6rxnR3E:YEPeM34Wdq0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=CsIS6rxnR3E:YEPeM34Wdq0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=CsIS6rxnR3E:YEPeM34Wdq0:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Ryrobes?i=CsIS6rxnR3E:YEPeM34Wdq0:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Ryrobes?a=CsIS6rxnR3E:YEPeM34Wdq0:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Ryrobes?d=qj6IDK7rITs" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Ryrobes/~4/CsIS6rxnR3E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://ryrobes.com/python/python-snippet-sending-html-email-with-an-attachment-via-google-apps-smtp-or-gmail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://ryrobes.com/python/python-snippet-sending-html-email-with-an-attachment-via-google-apps-smtp-or-gmail/</feedburner:origLink></item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc
Database Caching using apc
Object Caching 1915/2043 objects using apc
Content Delivery Network via fixxer.ryrobes.com

 Served from: www.ryrobes.com @ 2013-05-23 09:22:59 by W3 Total Cache -->
