<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>blacknode &#187; Python</title>
	<atom:link href="http://blog.blacknode.net/category/programming/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.blacknode.net</link>
	<description>NO CARRIER</description>
	<lastBuildDate>Mon, 13 Jun 2011 07:12:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>1337</title>
		<link>http://blog.blacknode.net/2010/11/1337/</link>
		<comments>http://blog.blacknode.net/2010/11/1337/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 07:31:53 +0000</pubDate>
		<dc:creator>jrod</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Turbogears]]></category>

		<guid isPermaLink="false">http://blog.blacknode.net/?p=751</guid>
		<description><![CDATA[﻿﻿﻿Calculating how many lines my project is at currently (not including tester code or templates): (kserve)@haven:~/kserve/fresh/kserver$ find lib/ controllers/ model/ -type f -name &#8220;*.py&#8221;  &#124; grep -v template.py &#124; grep -v auth.py &#124; grep -v error.py &#124; grep -v __init__ &#124;grep -v app_globals.py&#124; grep -v base.py &#124;xargs cat  &#124; wc -l 1337 l33t.. no bs. ]]></description>
			<content:encoded><![CDATA[<p>﻿﻿﻿Calculating how many lines my project is at currently (not including  tester code or templates):</p>
<p>(kserve)@haven:~<em>/kserve/fresh/kserver$ find lib/</em> controllers/ model/ -type f -name &#8220;*.py&#8221;  | grep -v template.py | grep  -v auth.py | grep -v error.py | grep -v __init__ |grep -v  app_globals.py| grep -v base.py |xargs cat  | wc -l<br />
1337</p>
<p>l33t.. no bs.</p>
<p>Unfortunately, it is not done. I am going to to have to code 30,000 more  lines now.. damnit!</p>
<p>Jrod.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.blacknode.net/2010/11/1337/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Woe to the client&#8230;</title>
		<link>http://blog.blacknode.net/2010/11/woe-to-the-client/</link>
		<comments>http://blog.blacknode.net/2010/11/woe-to-the-client/#comments</comments>
		<pubDate>Thu, 18 Nov 2010 23:02:57 +0000</pubDate>
		<dc:creator>jrod</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.blacknode.net/?p=735</guid>
		<description><![CDATA[that triggers this exception! Woe to them indeed!]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.blacknode.net/wp-content/uploads/2010/11/Consequences.png"><img class="alignleft size-full wp-image-739" title="Consequences" src="http://blog.blacknode.net/wp-content/uploads/2010/11/Consequences.png" alt="" width="562" height="148" /></a></p>
<p>that triggers this exception! Woe to them indeed!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.blacknode.net/2010/11/woe-to-the-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQLAlchemy declarative_base: setting the storage engine and other table options</title>
		<link>http://blog.blacknode.net/2010/11/sqlalchemy-declarative_base-setting-the-storage-engine-and-other-table-options/</link>
		<comments>http://blog.blacknode.net/2010/11/sqlalchemy-declarative_base-setting-the-storage-engine-and-other-table-options/#comments</comments>
		<pubDate>Wed, 17 Nov 2010 23:25:05 +0000</pubDate>
		<dc:creator>jrod</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SQLAlchemy]]></category>
		<category><![CDATA[Turbogears]]></category>

		<guid isPermaLink="false">http://blog.blacknode.net/?p=728</guid>
		<description><![CDATA[Using mysql for your Engine. DeclarativeBase = declarative_base() class TableClass(DeclarativeBase): __tablename__ = 'table_name' __table_args__ = {'mysql_engine':'InnoDB', 'mysql_charset':'utf8'} col_id = Column(Integer, primary_key=True) ]]></description>
			<content:encoded><![CDATA[<p>Using mysql for your Engine.</p>
<pre>DeclarativeBase = declarative_base()
class TableClass(DeclarativeBase):
    __tablename__ = 'table_name'
    __table_args__ =  {'mysql_engine':'InnoDB', 'mysql_charset':'utf8'}
    col_id = Column(Integer, primary_key=True)
    ...</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.blacknode.net/2010/11/sqlalchemy-declarative_base-setting-the-storage-engine-and-other-table-options/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exposing sqlalchemy engine in turbogears 2.1</title>
		<link>http://blog.blacknode.net/2010/11/exposing-sqlalchemy-engine-in-turbogears-2-1/</link>
		<comments>http://blog.blacknode.net/2010/11/exposing-sqlalchemy-engine-in-turbogears-2-1/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 20:25:20 +0000</pubDate>
		<dc:creator>jrod</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SQLAlchemy]]></category>
		<category><![CDATA[Turbogears]]></category>

		<guid isPermaLink="false">http://blog.blacknode.net/?p=694</guid>
		<description><![CDATA[Getting access to the internals of the sqlalchemy stack in turbogears can be difficult if you do not know where to look. The use of declarative_base() to create model classes further obfuscates the location of the table and metadata objects. In order to create/drop a table using an SQLAlchemy object, you need access to the table ]]></description>
			<content:encoded><![CDATA[<p>Getting access to the internals of the sqlalchemy stack in turbogears can be difficult if you do not know where to look. The use of declarative_base() to create model classes further obfuscates the location of the table and metadata objects.</p>
<p>In order to create/drop a table using an SQLAlchemy object, you need access to the table object, the table metadata, and a database engine object. When extending DeclarativeBase, the table object is exposed by the __table__ attribute of every model class. For instance:</p>
<blockquote><p>class Distro(DeclarativeBase):<br />
__tablename__ = &#8216;distro&#8217;<br />
id = Column(Integer, primary_key=True, unique=True)<br />
label = Column(Unicode(35), nullable=False)</p>
<p>In [1]: from yourapp.model import *</p>
<p>In [2]: import transaction</p>
<p>In [3]: distro_obj = Distro()</p>
<p>In [4]: print distro_obj.__table__.__doc__<br />
&#8212;&#8212;&gt; print(distro_obj.__table__.__doc__)<br />
Represent a table in a database.</p>
<p>e.g.::</p>
<p>mytable = Table(&#8220;mytable&#8221;, metadata,<br />
Column(&#8216;mytable_id&#8217;, Integer, primary_key=True),<br />
Column(&#8216;value&#8217;, String(50))<br />
)</p></blockquote>
<p>As you can see, you may access the methods of Table() from the DeclarativeBase __table__ attribute. In order to use the model to create/drop the table it describes, you need to bind the table metadata to an instantiated SQLAlchemy engine object. Pylons, the framework which Turbogears 2+ is based, stores the engine (defined in the paster .ini configuration) in the config global configuration dictionary, specifically:</p>
<pre>config['pylons.app_globals'].sa_engine</pre>
<p>Going back to the model we created, we can create the table using the Table() method create.</p>
<pre>distro_obj.__table__.create(config['pylons.app_globals'].sa_engine)</pre>
<p>Now that the table is created you may use the session object to interact with the table.</p>
<pre>In [5]: distro_obj.label = 'rhel5_minimal'
In [6]: DBSession.add(distro_obj)
In [7]: transaction.commit()
In [8]: distro_label = DBSession.query(Distro.label).filter(Distro.label=='rhel5_minimal').one()[0]
In [9]: print distro_label
-------&gt; print(distro_label)
rhel5_minimal</pre>
<p>For more information, take a look at the documentation for <a href="http://www.turbogears.org/2.1/docs/">TG2.1</a> and <a href="http://www.sqlalchemy.org/docs/orm/extensions/declarative.html">SQLAlchemy declarative syntax</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.blacknode.net/2010/11/exposing-sqlalchemy-engine-in-turbogears-2-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>sqlalchemy, MySQLdb, debian</title>
		<link>http://blog.blacknode.net/2010/10/sqlalchemy-mysqldb-debian/</link>
		<comments>http://blog.blacknode.net/2010/10/sqlalchemy-mysqldb-debian/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 23:09:18 +0000</pubDate>
		<dc:creator>jrod</dc:creator>
				<category><![CDATA[Debian]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sqlalchemy]]></category>

		<guid isPermaLink="false">http://blog.blacknode.net/?p=553</guid>
		<description><![CDATA[(in virtualevn) $ sudo apt-get install libmysqlclient-dev python-dev $ easy_install SQLAlchemy mysql-python Not sure why the package is labeled mysql-python. Also, you need the development headers because the MySQLdb module is written in c]]></description>
			<content:encoded><![CDATA[<p>(in virtualevn)</p>
<blockquote><p>$ sudo apt-get install libmysqlclient-dev python-dev<br />
$ easy_install SQLAlchemy mysql-python</p></blockquote>
<p>Not sure why the package is labeled mysql-python. Also, you need the development headers because the MySQLdb module is written in c.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.blacknode.net/2010/10/sqlalchemy-mysqldb-debian/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning python</title>
		<link>http://blog.blacknode.net/2008/12/learning-python/</link>
		<comments>http://blog.blacknode.net/2008/12/learning-python/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 00:33:25 +0000</pubDate>
		<dc:creator>jrod</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.blacknode.net/?p=136</guid>
		<description><![CDATA[I have started to rewrite some of my existing code in python in an earnest effort to master the language (I have been putting it off for a long while now). Anyway, I was working on a parser for a # commented file, this is how I stripped the comments from each line in C: ]]></description>
			<content:encoded><![CDATA[<p>I have started to rewrite some of my existing code in python in an earnest effort to master the language (I have been putting it off for a long while now).  Anyway, I was working on a parser for a # commented file, this is how I stripped the comments from each line in C:</p>
<pre><code>
while(fgets(line, sizeof(line)-1, fp)) {
  char *hash; //ptr to hash mark denoting a comment
  if(line[0] == '#' || line[0] == '\n')
    continue;
  if((hash = strchr(line,'#')) != NULL)
  line[hash - line] = '\0'; //line + hash = memory location of hash
  printf("%s", line);
}
</code></pre>
<p>Now that I am beginning to get a handle on python, I am really starting to appreciate its elegance. This line of code accomplishes the same task:</p>
<p><code>line.strip()[:line.find("#")].strip()</code></p>
<p>I have not compared the performance of the above line to my c code , but I would assume using all the nested objects would incur a performance hit. I may test this later, but I am pretty sure my c code would be much faster.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.blacknode.net/2008/12/learning-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

