<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<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/"
	>

<channel>
	<title>Tech Ravings</title>
	<link>http://www.techraving.com</link>
	<description>An Opinion is a Terrible Thing To Waste</description>
	<pubDate>Tue, 29 Jul 2008 19:18:01 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Twitter Fails Again</title>
		<link>http://www.techraving.com/2008/07/28/twitter-fails-again/</link>
		<comments>http://www.techraving.com/2008/07/28/twitter-fails-again/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 00:23:35 +0000</pubDate>
		<dc:creator>ScottK</dc:creator>
		
		<category><![CDATA[Stuff]]></category>

		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.techraving.com/2008/07/28/twitter-fails-again/</guid>
		<description><![CDATA[Seriously I am not addicted to Twitter. I use it for light communication between friends or to help out those with questions when I can. Otherwise I am happy to just stay heads down in code.
Twitters downtime is infamous and I am fine with that. However when my browser starts crashing because of mistakes I [...]]]></description>
			<content:encoded><![CDATA[<p>Seriously I am not addicted to Twitter. I use it for light communication between friends or to help out those with questions when I can. Otherwise I am happy to just stay heads down in code.</p>
<p>Twitters downtime is infamous and I am fine with that. However when my browser starts crashing because of mistakes I take a huge offense to that. So now Twitter is throwing an SSL certificate error because they haven&#8217;t configured it correctly.</p>
<p><img src="http://www.techraving.com/images/twitter_ssl.png" alt="Twitter Fails" height="125" width="500" /></p>
<p>Do you see the localhost.localdomain on twitter.com?!?</p>
<p>What&#8217;s the point of this site, really.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.techraving.com%2F2008%2F07%2F28%2Ftwitter-fails-again%2F';
  addthis_title  = 'Twitter+Fails+Again';
  addthis_pub    = 'DarkRyder';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.techraving.com/2008/07/28/twitter-fails-again/feed/</wfw:commentRss>
		</item>
		<item>
		<title>CherryPy Using Routes Library</title>
		<link>http://www.techraving.com/2008/06/23/cherrypy-using-routes-library/</link>
		<comments>http://www.techraving.com/2008/06/23/cherrypy-using-routes-library/#comments</comments>
		<pubDate>Tue, 24 Jun 2008 01:17:18 +0000</pubDate>
		<dc:creator>ScottK</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<category><![CDATA[cherrypy]]></category>

		<category><![CDATA[pylons]]></category>

		<category><![CDATA[routes]]></category>

		<guid isPermaLink="false">http://www.techraving.com/2008/06/23/cherrypy-using-routes-library/</guid>
		<description><![CDATA[I&#8217;m still plucking away at this project as time allows using CherryPy. This weekend I just started to use it and finally got a working environmental configuration system and then next step was to figure out if RESTful controllers could be used. As it turns out you can not only use RESTful controllers but have [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m still plucking away at this project as time allows using CherryPy. This weekend I just started to use it and finally got a working environmental configuration system and then next step was to figure out if RESTful controllers could be used. As it turns out you can not only use RESTful controllers but have the additional benefit of format types as well (html, xml, json, etc).</p>
<p>This is due to the CherryPy RoutesDispatcher using the <a href="http://routes.groovie.org/" title="Routes" target="_blank">routes</a> library.  I&#8217;ve successfully setup dispatcher with RESTful routes and have tests to show that they work as planned.</p>
<p>I&#8217;ll do a follow up post with working examples this weekend.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.techraving.com%2F2008%2F06%2F23%2Fcherrypy-using-routes-library%2F';
  addthis_title  = 'CherryPy+Using+Routes+Library';
  addthis_pub    = 'DarkRyder';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.techraving.com/2008/06/23/cherrypy-using-routes-library/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Setting Up Environments in CherryPy</title>
		<link>http://www.techraving.com/2008/06/20/setting-up-environments-in-cherrypy/</link>
		<comments>http://www.techraving.com/2008/06/20/setting-up-environments-in-cherrypy/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 22:02:26 +0000</pubDate>
		<dc:creator>ScottK</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.techraving.com/2008/06/20/setting-up-environments-in-cherrypy/</guid>
		<description><![CDATA[Here&#8217;s my dilemma, I&#8217;m a developer working on code on my machine. Once I&#8217;m done I send the code to QA who then says yay or nay. Once I get a yay then the code goes into production on a live server. CherryPy has a site config for all apps as well as an app [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s my dilemma, I&#8217;m a developer working on code on my machine. Once I&#8217;m done I send the code to QA who then says yay or nay. Once I get a yay then the code goes into production on a live server. <a href="http://www.cherrypy.org/" title="Python CherryPy" target="_blank">CherryPy</a> has a site config for all apps as well as an app specific config, which pertains to the code I writing. Under the CherryPy documentation I would have to modify 3 different files in order to add 1 new model! That&#8217;s insane.</p>
<p>Additionally, I&#8217;m working on my computer (localhost) so I need the SQLObject connections to point to my machine. When I send my work to QA the db connections are completely different. Production environment, again different. Traces, ip address, etc, all different from the various environments. CherryPy 3 does have a &#8220;global&#8221; config for all apps as well as a &#8220;site&#8221; config pertaining to your app, but no where does it suggest setting up these environments. Outside of the &#8220;environment = whatever&#8221; in the config.</p>
<p>Do I really have to write my code on my machine, send it to QA, log into shell and make the modifications, get a yay and send to production, log into shell there and make modifications? Do this across twenty developers as well?</p>
<p>No&#8230;</p>
<p>I hope you see the catastrophe that could ensue because an out of the box framework doesn&#8217;t allow environments to be set up within an app. Likewise I do judge a framework by the lack of code duplication (DRY).  Searching the documentation for CherryPy as well as many other sites I found no information concerning my requirements. So it was time to start &#8220;playing&#8221;</p>
<p>CherryPy does allow you to remain DRY and set up environments!<br />
Takes a little work around though, but with a deploy script it&#8217;s easy.</p>
<p>First let&#8217;s create the scripts:</p>
<p># Site Config (site_config.py)</p>
<pre>
[global]
server.protocol_version = "HTTP/1.1"
tools.proxy.on = True
tools.proxy.local = "X-Forwarded-Host"
tools.proxy.remote =  "X-Forwarded-For"
log.error_file = "logs/error.log"
log.access_file = "logs/access.log"
</pre>
<p>#Development (development.py)</p>
<pre>
[global]
server.socket_host = "127.0.0.1"
server.socket_port = 8082
log.screen = True
tools.log_tracebacks.on = True
</pre>
<p>#QA (qa.py)</p>
<pre>
[global]
server.socket_host = "127.0.0.1"
server.socket_port = 8083
log.screen = True
tools.log_tracebacks.on = False
</pre>
<p>#routes.py (routes.py)</p>
<pre>
[/]
</pre>
<p>#whatever</p>
<p>#deploy script (deploy.py)</p>
<pre>
import sys
import cherrypy

import models
from models.base import Base
#add your additional models as well from models folder

if __name__ == '__main__':
    cherrypy.config.update(sys.argv[1] + ".py")
    cherrypy.config.update(sys.argv[2] + ".py")
    cherrypy.tree.mount(Base(), script_name="/", config="routes.py")
    cherrypy.engine.start()
</pre>
<p>So in the command line when you call:<br />
&gt;&gt;python deploy.py site_config.py development<br />
what happens is:</p>
<p>1. Loads the models as classes.<br />
2. cherrypy loads the site wide config variables (site_config.py). These pertain to all the environments.<br />
3. chrrypy loads, adds or overwrites, all the environment settings. Specific to development (locally), qa (hope it works), production (umm better work) <img src='http://www.techraving.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
4. Mounts each model to the correct path as found in the routes.py file which contains settings for each route.</p>
<p>Of course it&#8217;s not completely DRY. As each new model is made you do have to update the deploy script as well as the routes.py, but it&#8217;s a lot better than doing all your environment files.</p>
<p>I do feel CherryPy would be much more powerful if out of the box we could set up situations like this as well as a plain routes file (Dispatcher) so hacks like this aren&#8217;t needed. However this hack at least kept me considering using CherryPy.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.techraving.com%2F2008%2F06%2F20%2Fsetting-up-environments-in-cherrypy%2F';
  addthis_title  = 'Setting+Up+Environments+in+CherryPy';
  addthis_pub    = 'DarkRyder';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.techraving.com/2008/06/20/setting-up-environments-in-cherrypy/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Restful Pylons with Extension Types</title>
		<link>http://www.techraving.com/2008/06/13/restful-pylons-with-extension-types/</link>
		<comments>http://www.techraving.com/2008/06/13/restful-pylons-with-extension-types/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 01:01:21 +0000</pubDate>
		<dc:creator>ScottK</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<category><![CDATA[framework]]></category>

		<category><![CDATA[model-view-controller]]></category>

		<category><![CDATA[mvc]]></category>

		<category><![CDATA[pyhton]]></category>

		<category><![CDATA[pylons]]></category>

		<category><![CDATA[restful]]></category>

		<guid isPermaLink="false">http://www.techraving.com/2008/06/13/restful-pylons-with-extension-types/</guid>
		<description><![CDATA[The time has come for me to finally use a python web framework, Pylons is the choice around here. I&#8217;ve set up a few toy apps using it but nothing so involved that took real software planning. Since I was making a real application that required real business planning I found the Pylons documentation to [...]]]></description>
			<content:encoded><![CDATA[<p>The time has come for me to finally use a python web framework, <a href="http://pylonshq.com/" title="Pylons" target="_blank">Pylons</a> is the choice around here. I&#8217;ve set up a few toy apps using it but nothing so involved that took real software planning. Since I was making a real application that required real business planning I found the Pylons documentation to be really screwed up. The instructions in the tutorials conflicted with each other in how to set up the websetup.py, development.py, etc. Months ago I had a toy program setup in half an hour using one tutorial; today it took me over two hours because the Pylons docs had changed. But not across the boards.</p>
<p>While displeased with this, I&#8217;m used to it. It also means that serious research is needed in order to work with the software.  Being fully involved with Pylons, albeit new. I didn&#8217;t expect it to be <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" title="REST" target="_blank">RESTful</a>; nor did I expect it to allow different extension type within an action. I am actually impressed with the Ruby on Rails RESTful implementation and was very surprised to find that Pylons itself has RESTful implementations itself.</p>
<p>However, my project requires me to call one action in the extension with &#8220;.js&#8221; for <a href="http://en.wikipedia.org/wiki/JSON" title="JavaScript Object Notation" target="_blank">JSON</a> and another extension of &#8220;.rss&#8221; for that version. The Pylons RESTful documentation lacks for this. I couldn&#8217;t find any other reference for this on the web either. While I don&#8217;t know for sure when Pylons added RESTful resources the documentation lacks any examples that I found for using different formats, so here it goes.</p>
<p>The Pylons wiki for RESTful controllers is found here: <span class="pagetitle" style="margin: 0px; line-height: 1; text-decoration: none">                                         <a href="http://wiki.pylonshq.com/display/pylonscookbook/How+map.resource+enables+controllers+as+services" target="_blank">How map.resource enables controllers as services</a>. It explains everything you want to know about how to use REST within Pylons. It does not explain my predicament on how to make the index action display JSON format or RSS format.</span></p>
<p>After a little testing and discovery I found out that you can, within an action, produce different views specific to the format of the action.  Given my requirements here is how I was able to use one RESTful action for many formats, i.e (.js, .gif, .rss, .rtf, etc).</p>
<p>1. Create your RESTful controller using the command: paster restcontroller &lt;singular name&gt; &lt;plural name&gt;<br />
This is no different than the instructions in the map.resource link above.</p>
<p>2. In the action create an if&#8230;elsif&#8230;else to actually control the format:<br />
def index(self, format=&#8221;html&#8221;):<br />
if format == &#8220;js&#8221;:<br />
response.headers[&#8217;Content-Type&#8217;] = &#8216;application/javascript&#8217;<br />
return render(&#8217;/pubscript/indexjs.mako&#8217;)<br />
elif format == &#8220;xml&#8221;:</p>
<p>response.headers[&#8217;Content-Type&#8217;] = &#8216;application/xml&#8217;<br />
return render(&#8217;/pubscript/indexxml.mako&#8217;)<br />
else:<br />
abort(404, &#8216;Page Not Found&#8217;)</p>
<p>So as the index definition provides that any index call to the controller does perform the action, only the .js returns an js (JSON) file or an .xml renders an xml file format. Any other format or none provided sends a 404 File Not Found error. Now I had complete control of what the format is and control of any unexpected calls.</p>
<p>Pylons has evoled to the presedence set by Ruby on Rails in that RESTful controllers can be used. Even though the default format for Pylons is HTML you can detect such as present different formats based upon such.  Being new to the Pylon framework itself I wasn&#8217;t expecting this functionality. Discovering that it has it, and is acting on the different formats, I do believe that it has emerged as a mature framework that embraces best practices of web application programming.</p>
<p>Still highly complicated in setting up and a huge learning curve with little correct documentation though, don&#8217;t get me wrong there.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.techraving.com%2F2008%2F06%2F13%2Frestful-pylons-with-extension-types%2F';
  addthis_title  = 'Restful+Pylons+with+Extension+Types';
  addthis_pub    = 'DarkRyder';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.techraving.com/2008/06/13/restful-pylons-with-extension-types/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Passing Arguments between Python Decorators</title>
		<link>http://www.techraving.com/2008/06/08/passing-arguments-between-python-decorators/</link>
		<comments>http://www.techraving.com/2008/06/08/passing-arguments-between-python-decorators/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 11:17:28 +0000</pubDate>
		<dc:creator>ScottK</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.techraving.com/2008/06/08/passing-arguments-between-python-decorators/</guid>
		<description><![CDATA[One of the problems of learning Python decorators was figuring out how to not only pass arguments and use them, but also how to pass class objects to decorators and make them functional. I my post about Python Decorators, Wrapped with Goodness I introduced Python decorators. I  left off with that article on actually [...]]]></description>
			<content:encoded><![CDATA[<p>One of the problems of learning Python decorators was figuring out how to not only pass arguments and use them, but also how to pass class objects to decorators and make them functional. I my post about <a href="http://www.techraving.com/2008/06/07/python-decorators-wrapped-with-goodness/" target="_blank">Python Decorators, Wrapped with Goodness</a> I introduced Python decorators. I  left off with that article on actually how to pass through arguments for use in the decorator. This was intentional because, here comes another long post. <img src='http://www.techraving.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>The ability in Python to explicitly and easily tell the interpreter to perform other actions on a called block of executable code in one line without changing any underlying logic is powerful. However the actually documentation or tutorials are vague on the actual inner workings of what goes on. How do you use the decorated function? You passed in arguments, are they available? I passed in a class, do I have access to it?</p>
<p>They answer is yes!  Let&#8217;s set up a test case to walk through all of this and explain along the way.</p>
<p>1. I need a program that reads from a database x amount of records, and then converts them to the appropriate class object. Each object is then feed to a Queue for processing. Well that&#8217;s easy enough isn&#8217;t it.</p>
<p>2. Now as the Queue is iterated the object is removed from the Queue and an object method is called. Based upon the result of the method: place in the completed Queue; place in the failed Queue; place back into the processing Queue.</p>
<p>3. Run this until the processing Queue is empty.</p>
<p>A fairly simple program indeed. The complexity we need without Python decorators and maintaining OOP are two classes: A jobs class to load the special Queues, hold methods for switching Queues; the item class to hold the database information. Maintaining OOP means each class should not know about what happens to the other and each should perform only on itself.</p>
<p>Problem, the jobs class would need to know the result status of the item instance to correctly place it in the correct Queue. Or we have to pass in the different Queue to the item object during construction to be placed. Thereby locking in the item to the program and not very re-usable. Python decorators short cut this, we get to drop the jobs class and our code is more re-usable.</p>
<pre>
from Queue import Queue
import random

print "loading"
job_que = Queue()
failed_que = Queue()
completed_que = Queue()
print "queue complete"

def global_job(fn):
    def wrappedFN(arg1, arg2):
        eval(arg2 + "_que").put(arg1, block = True)
        result = fn(arg1, arg2)
        print "put into job complete"
        return result

    return wrappedFN

class SomeItem(object):

    _name = ""

    def __init__(self):
        self._name = "Me"

    def run_job(self):
        print ""
        num = random.sample([0, 1, 2],  1)
        if num == [0]:
            self._generic_job("failed")

        if num == [1]:
            self._generic_job("job")

        if num == [2]:
            self._generic_job("completed")

    @global_job
    def _generic_job(self, type):
        print "job " + type

    def load_queue():
        print "loading queue"
        for i in range(10):
            job_que.put(SomeItem(), block = True)

print "Starting Main"

load_queue()
print "finished loading jobs"

while not job_que.empty():
    print "processing size: " + str(job_que.qsize())
    for i in range(job_que.qsize()):
        job = job_que.get(i)      #job (class object) pulled the instance out of the Queue,
        job.run_job()
        job_que.task_done()

        print "processing size: " + str(job_que.qsize())
        print "completed size: " + str(completed_que.qsize())
        print "failed size: " + str(failed_que.qsize())
        job_que.join()
</pre>
<p>So there is the code to make this work and here&#8217;s how it does.</p>
<p>1. Load the processing Queue with the instances of &#8220;SomeItem&#8221;<br />
2. Iterate each item in the Queue and remove the object from said Queue<br />
3. Call object method &#8220;run_job&#8221;<br />
4. The result within &#8220;run_job&#8221; can be one of three result: &#8220;failed&#8221;, &#8220;completed&#8221;, &#8220;job&#8221;.<br />
5. run_job then calls self class method _generic_queue<br />
6. Python decorator generic_job intercepts this call<br />
7. _generic_queue gets executed<br />
8. Python decorator global_job get executed and places in the correct Queue<br />
9. Rinse and repeat job Queue until empty</p>
<p>All very cut and dry huh? Yep this is where my research into Python decorators ended. I mean I sent arguments to the class method that got intercepted and I need to perform an action in that method yet how does the decorator REALLY work?</p>
<p>Let&#8217;s start from the self._generic_job call. To keep the program <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" title="Don't Repeat Yourself" target="_blank">DRY</a> a singular function is called with arguments to process the results. In order to maintain the DRY&#8217;ness an argument of the result needs to sent. Being a class method we also need to def _generic_job with the first argument as &#8220;self&#8221;. The self reference is very import to the decorator. The second argument as the type of Queue we will be placing in.</p>
<p>We&#8217;ve called the self._generic_job, yet the global global_job has been wrapped around the _generic_job. So following the order of operation when we call self._generic_job(&#8221;completed&#8221;) what the interpreter did was global_job(&lt;pointer&gt;_generic_job). Awesomeness!</p>
<p>Taking a look at the actual decorator we can find out how to actually use it to process not only the class/function but also the arguments passed! Yes I made this difficult by passing a class instance over sending in a functiton!  So let&#8217;s move on to the decorator itself.</p>
<pre>
def global_job(fn):
    def wrappedFN(arg1, arg2):
        eval(arg2 + "_que").put(arg1, block = True)
        result = fn(arg1, arg2)
        print "put into job complete"
        return result

    return wrappedFN
</pre>
<p>So the global global_job has intercepted the class method and wrapped itself around such. In the definition of global_job the argument of fn actually refers to the callback of the called object. global_job in and of itself returns a pointer to another nested function within itself which process the arguments. This nested function then takes the callback and can act on such along with the arguments to perform such.</p>
<p>I know, I know that was a very high level description. Just for brevity I swear. I got confused just writing it so here&#8217;s the actual processing.</p>
<p>self._generic_job was called with one argument. The actual definition takes two arguments: self; type. Both of these were passed to the global_job method.</p>
<p>So the global_job actually received the &lt;object&gt;.generic_job pointer reference. That would be the &#8220;fn&#8221;. The last line in global_job returns the reference to wrappedFN. Which basically means execute wrappedFN and here&#8217;s the arguments as well. So wrappedfn executes before the return.</p>
<p>You&#8217;ll notice that the wrappedFN is defined with two arguments: arg1, arg2. This is only for this example and need not be so for yours(*args, **kwargs) works also. But what are these arguments really? Really they are how the base function gets executed.</p>
<p>Keeping in mind that &#8220;fn&#8221; declared in generic_job is accessible to wrappedFN and that &#8220;fn&#8221; is the callback, then arg1 and arg2 are the method arguments. So in this case:</p>
<p>fn = SomeItem (instance)._generic_job<br />
arg1 = self._generic_job(self&lt;pointer&gt;&#8230;&#8230;<br />
arg2 = self._generic_job(&#8230;, type)</p>
<p>So low and behold that in the def _generic_job method takes self as the first argument (naturally) and it is really the arg1 of the wrappedFN. Easy enough to use it as the reference pointer to the instance and of which can use as a full blown instance. Then arg2 must be the passed string argument of type. In this case we pass through the eval statement to place into the correct Queue as we need.</p>
<p>So we have the three key pieces: callback function; class instance; and the additional arguments it easy to place into the correct Queue. Yet how does the _generic_gob actaully get executed? That happens in line &#8220;result = fn(arg1, arg2)&#8221;. Keeping in mind that translates to SomeItem._generic_job(self, type). SomeItem isn&#8217;t a singelton object, it&#8217;s the actual job we pulled from the Queue and the self argument is actually the pointer referencing it.</p>
<p>Using Python decorators is a powerful option for extending programs but there aren&#8217;t any clear tutorials or articles explaining the way arguments and callback functions get executed. This article does just that as the decorator function does have access to the callback function and arguments passed. With just one line of code the call back function can be executed as well.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.techraving.com%2F2008%2F06%2F08%2Fpassing-arguments-between-python-decorators%2F';
  addthis_title  = 'Passing+Arguments+between+Python+Decorators';
  addthis_pub    = 'DarkRyder';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.techraving.com/2008/06/08/passing-arguments-between-python-decorators/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Python Decorators, Wrapped With Goodness</title>
		<link>http://www.techraving.com/2008/06/07/python-decorators-wrapped-with-goodness/</link>
		<comments>http://www.techraving.com/2008/06/07/python-decorators-wrapped-with-goodness/#comments</comments>
		<pubDate>Sat, 07 Jun 2008 16:33:29 +0000</pubDate>
		<dc:creator>ScottK</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.techraving.com/2008/06/07/python-decorators-wrapped-with-goodness/</guid>
		<description><![CDATA[This post is really the first part of two. Here I&#8217;ll talk about what is this Python decorator that everyone is raving about. How does it work. What is it really. The second part I&#8217;ll show how to pass arguments and classes between Python decorators so that not only are the decorators executed but the [...]]]></description>
			<content:encoded><![CDATA[<p>This post is really the first part of two. Here I&#8217;ll talk about what is this Python decorator that everyone is raving about. How does it work. What is it really. The second part I&#8217;ll show how to pass arguments and classes between Python decorators so that not only are the decorators executed but the original code block is executed.</p>
<p>Two weeks ago I was put on a Twitter project written in Python and had a requirement of specifically using decorators for logging. I didn&#8217;t know anything about this strange functionality and was given a very brief overview of what they were. After the project meeting I hit the interwebs and played with some toy code to figure out just how they work I finally got it figured out to the point that &#8220;it worked&#8221;.</p>
<p>I was still left feeling like I didn&#8217;t understand what Python decorators were and really what the need for them are. Even worse I questioned the need for them anyhow. A short case</p>
<p>def global_some_function(fn):<br />
def wrappedfn(*args, **kwargs):<br />
#do stuff here<br />
return wrappedfn</p>
<p>class base(object):<br />
@global_function<br />
def base_method(self):<br />
pass</p>
<p>class ChildClass(base):<br />
def call_base(self):<br />
base.base_method()</p>
<p>So this is were I really felt like I was violating a bunch of programming rules. I mean I call ChildClass.call_base(), which then calls it&#8217;s parent class base_method. Yet that is intercepted by the Python decorator and global global_some_function. So effectively using this decorator the child class is not totally object oriented, and I would feel a lot more happier just having the Base.base_method class do the work instead of the decorator.</p>
<p>Feeling un-satisfied, confused, and like I had dirty code I pressed on and completed the project. The next project consisted of taking a Queue of classes, performing an action on them in a iteration, and then sorting based upon the result of the actions. Almost immediately I realized that this is were the power and flexibility of Python decorators comes in.</p>
<p>I mean based up the result of the action the object may be put back in the Queue, or it may go to another Queue for further processing. The object shouldn&#8217;t care about what Queue it&#8217;s in, so I needed a way to wrap in this functionality shared by by all Queues. Hitting the interwebs to truly understand what and how Python decorators work yeilded many examples but not a clear cut understanding of how decorators work.</p>
<p>So here my friends is how I figured out what, and how Python decorators work.</p>
<p>First and foremost when I say Python decorators I am not referring to the Decorator Design Pattern. Although I have found to be effectively the same. The Python decorator being a symbolic tag signifying to the Python interpreter that the following method is to be wrapped within an function or class for further processing. So while the Python decorator creates that link to another function, the other function then becomes the Decorated Design Pattern following that pattern.</p>
<p>def foo(fn):<br />
print &#8220;Hello&#8221;</p>
<p>@foo<br />
def bar(some_arg):<br />
print some_arg</p>
<p>bar(&#8221;world&#8221;)</p>
<p>What would you expect with the above example? I initially expected the result to print foo(bar(&#8221;world&#8221;)), however that was not the case as only &#8220;Hello&#8221; was the result. This tripped me up for sometime and it&#8217;s were the online examples and documentation lacked. The reason is due to the decorator function &#8220;foo&#8221;. There are no functionalities to actually execute the original function &#8220;bar&#8221;. This pretty much where all the tutorials left off; if you&#8217;ve read this far you probably want me to get to the point now huh?</p>
<p>As I stated earlier Python decorators are not design patterns. They are merely a signal to the Python interpreter to wrap the call in other signified functions.  There are at least two ingrained decorators that Python ships with: @staticmethod; @classmethod, but as programmers we&#8217;ll be dealing with our own custom decorators.</p>
<p>Using the Python decorator is relatively simple. Just declare the decorator with the @ symbol preceding the method to be wrapped. So:</p>
<p>def foo():<br />
print &#8220;Hello World&#8221;</p>
<p>@foo<br />
def bar():<br />
pass</p>
<p>bar()</p>
<p>To the Python interpreter bar get wrapped with the function foo whenever the method bar is executed. So calling bar and following the order of exectution look s like this foo(bar). There is certainly no reason not to use only one to make if even more flexible:</p>
<p>def foo(fn):<br />
print &#8220;world&#8221;</p>
<p>def bar(fn):<br />
print &#8220;Hello&#8221;</p>
<p>@foo<br />
@bar<br />
def something():<br />
print &#8220;I&#8217;m alive&#8221;</p>
<p>So executing something() would follow the order of execution of foo(bar(something)) would print:</p>
<p>Hello<br />
World</p>
<p>but strangely &#8220;I&#8217;m alive&#8221; did not print! Why is that? It&#8217;s because wrapping the something function only sends the pointer to the function to the decorators, they are in charge of actually executing the received function. The other huge problem is how to deal with function arguements passed, and even bigger than that how to pass classes to global decorators while retaining the object.</p>
<p>Even though the name implies, Python decorators are not the design pattern. However usage is the same. Signify to the interpreter tho wrap this executable code within this executable code by using the @ character preceding the wrapping code block and preceding the code block to be wrapped.</p>
<p>This post is merely an introduction to what the Python decorators are and how they can be used. I&#8217;m following up this post with how to pass arguments through these decorators to really gain the power afforded by the decorator and to make your libraries more extend able. This post is <a href="http://www.techraving.com/2008/06/08/passing-arguments-between-python-decorators/" title="Passing arguments in python decorators">here</a>.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.techraving.com%2F2008%2F06%2F07%2Fpython-decorators-wrapped-with-goodness%2F';
  addthis_title  = 'Python+Decorators%2C+Wrapped+With+Goodness';
  addthis_pub    = 'DarkRyder';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.techraving.com/2008/06/07/python-decorators-wrapped-with-goodness/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Twitter, A Submission Easter Egg</title>
		<link>http://www.techraving.com/2008/05/06/twitter-a-submission-easter-egg/</link>
		<comments>http://www.techraving.com/2008/05/06/twitter-a-submission-easter-egg/#comments</comments>
		<pubDate>Wed, 07 May 2008 02:03:13 +0000</pubDate>
		<dc:creator>ScottK</dc:creator>
		
		<category><![CDATA[SEO/SEM]]></category>

		<guid isPermaLink="false">http://www.techraving.com/2008/05/06/twitter-a-submission-easter-egg/</guid>
		<description><![CDATA[In a post on Come Adventuring I made an announcement that I was web casting again. I also Twittered this information. Since I used UrlBrief I was also able to see click thru.  As a  whim I checked them an hour later and was suprised to see that traffic had been generated. Especially [...]]]></description>
			<content:encoded><![CDATA[<p>In a post on Come Adventuring I made an announcement that I was web casting again. I also Twittered this information. Since I used UrlBrief I was also able to see click thru.  As a  whim I checked them an hour later and was suprised to see that traffic had been generated. Especially since Googlebot had come through twice. Because I had used the same link in both places and the surprising traffic which one drew the traffic?</p>
<p>So started the experiment.</p>
<p>Using the traditional blogging methods: make a post; have it ping news aggregates, I created a link for than. I let it run for an hour to see if anything click the UrlBrief link and thus being recorded. The only two hits were from my Wordpress blog, none others.</p>
<p>After an hour and to the minute I made a Tweet with a fresh link for Come Adventuring. Absolutely no inclusion in any post. Even though I&#8217;m making this post twenty minutes early I have recorded 5 click thru&#8217;s to the post. One being googlebot.</p>
<p>After spending years in SEO/SEM I have become familiar with the traditional methods of code cleanliness, link campaigns, directory inclusions, ads, etc. I have never seen a faster generation of traffic than making a tweet on Twitter. This is truly amazing as Google must be sitting on Twitter and scraping.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.techraving.com%2F2008%2F05%2F06%2Ftwitter-a-submission-easter-egg%2F';
  addthis_title  = 'Twitter%2C+A+Submission+Easter+Egg';
  addthis_pub    = 'DarkRyder';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.techraving.com/2008/05/06/twitter-a-submission-easter-egg/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Twitter the New Search Engine Submission Tool</title>
		<link>http://www.techraving.com/2008/05/06/twitter-the-new-search-engine-submission-tool/</link>
		<comments>http://www.techraving.com/2008/05/06/twitter-the-new-search-engine-submission-tool/#comments</comments>
		<pubDate>Wed, 07 May 2008 00:11:08 +0000</pubDate>
		<dc:creator>ScottK</dc:creator>
		
		<category><![CDATA[SEO/SEM]]></category>

		<guid isPermaLink="false">http://www.techraving.com/2008/05/06/twitter-the-new-search-engine-submission-tool/</guid>
		<description><![CDATA[In my excitement about setting up streaming as posted on Come Adventuring I looked to UrlBrief to shorten the url to the radio broadcast. I also made a tweet about how excited I was with the urlbrief link. Not thinking anything of it I just went about normal business.
Now keep in mind that UrlBrief also [...]]]></description>
			<content:encoded><![CDATA[<p>In my excitement about setting up streaming as posted on <a target="_blank" href="http://urlbrief.com/619fbd" title="Come Adventuring">Come Adventuring</a> I looked to <a href="http://www.urlbrief.com" title="UrlBrief">UrlBrief</a> to shorten the url to the radio broadcast. I also made a tweet about how excited I was with the urlbrief link. Not thinking anything of it I just went about normal business.</p>
<p>Now keep in mind that UrlBrief also records for you to see where clicks have come from which make it very cool. So just as a grins and giggles idea I went back an hour later. Just to see. I about dropped out of my chair to see that google bot had already visited the links via the urlbrief url, and twice.</p>
<p>Not there are two ways that google bot could have done that: one is through the natural indexing of Come Adventuring, or; through Twitter. I&#8217;m guessing Twitter because Come Adventuring is way too new. Although it has a PR of 1, seriously it just doesn&#8217;t happen like that. Even by coincidence.</p>
<p>So I have another test running just in case. The Come Adventuring link above is url brief encoded. I&#8217;m going to let it run for an hour on this blog which Google has ranked spanked. I&#8217;ll post the details.</p>
<p> Then I&#8217;m going to make a tweet about it. Then post after an hour.</p>
<p>I&#8217;ve spent years doing SEO/SEM and have never seen a quicker response from a SE. I can certainly attribute it to Twitter because, frankly, I don&#8217;t market this blog.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.techraving.com%2F2008%2F05%2F06%2Ftwitter-the-new-search-engine-submission-tool%2F';
  addthis_title  = 'Twitter+the+New+Search+Engine+Submission+Tool';
  addthis_pub    = 'DarkRyder';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.techraving.com/2008/05/06/twitter-the-new-search-engine-submission-tool/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP Rocks and Now with Firewalls</title>
		<link>http://www.techraving.com/2008/04/25/php-rocks-and-now-with-firewalls/</link>
		<comments>http://www.techraving.com/2008/04/25/php-rocks-and-now-with-firewalls/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 20:43:06 +0000</pubDate>
		<dc:creator>ScottK</dc:creator>
		
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.techraving.com/2008/04/25/php-rocks-and-now-with-firewalls/</guid>
		<description><![CDATA[So far in the past month I have had two database injections. I don&#8217;t know how they are getting in and since I am on a shared server I don&#8217;t have access to the raw apache logs to figure this out. Security being very important to me I had to do something on the server [...]]]></description>
			<content:encoded><![CDATA[<p>So far in the past month I have had two database injections. I don&#8217;t know how they are getting in and since I am on a shared server I don&#8217;t have access to the raw apache logs to figure this out. Security being very important to me I had to do something on the server that would allow tracking or even setting up a complete firewall custom taliored for me.</p>
<p>I found <a target="_blank" href="http://www.idevaffiliate.com/31216/idevaffiliate.php?id=108" title="Firewall Script">Firewall Script</a> that can be used specifically to safe gaurd my personal site with dis-regard to all the others. I have not downloaded it yet but from the <a target="_blank" href="http://firewallscript.com/features.htm" title="Firewall Script Features">features</a> it looks to be exactly what I was looking for. Protection from sql injection, XSS attacks, even DDOS protection. Not that this blog would really ever need that I do run other sites that would really require it if the server can&#8217;t handle it.</p>
<p>I wanted to get this news out as soon as possible because several of us Wordpress and site owners are experiencing a rise in intrusion attempts. I&#8217;m gonna give it a try as soon as possible and I hope that this may help you as well.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.techraving.com%2F2008%2F04%2F25%2Fphp-rocks-and-now-with-firewalls%2F';
  addthis_title  = 'PHP+Rocks+and+Now+with+Firewalls';
  addthis_pub    = 'DarkRyder';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.techraving.com/2008/04/25/php-rocks-and-now-with-firewalls/feed/</wfw:commentRss>
		</item>
		<item>
		<title>This hijacking is getting stupid!</title>
		<link>http://www.techraving.com/2008/04/19/this-hijacking-is-getting-stupid/</link>
		<comments>http://www.techraving.com/2008/04/19/this-hijacking-is-getting-stupid/#comments</comments>
		<pubDate>Sat, 19 Apr 2008 14:57:12 +0000</pubDate>
		<dc:creator>ScottK</dc:creator>
		
		<category><![CDATA[My Life]]></category>

		<category><![CDATA[hijack]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[SocialSpark]]></category>

		<guid isPermaLink="false">http://www.techraving.com/2008/04/19/this-hijacking-is-getting-stupid/</guid>
		<description><![CDATA[I want to shout out Mouseclone for letting me know there was a problem with this site. I was contacted on Friday about an alert that was raised to him that I had a possible iframe hijack on my site. Not really knowing what he was talking about I quickly scanned my source code for [...]]]></description>
			<content:encoded><![CDATA[<p>I want to shout out <a target="_blank" href="http://socialspark.com/bloggers/mouseclone" title="Mouseclone">Mouseclone</a> for letting me know there was a problem with this site. I was contacted on Friday about an alert that was raised to him that I had a possible iframe hijack on my site. Not really knowing what he was talking about I quickly scanned my source code for the exact iframe source that he provided. Not finding anything I dismissed it as many users of SocialSpark visit me and he possibly confused me with someone else.</p>
<p> I did find today that in fact a database injection had been done and the iframe was there. My mistake was that I only hit the home page and the iframe was on the previous page. The iframe source refers to 61.155.8.157 and after some examination of the obfuscated JavaScript, redirections were discovered. I&#8217;ve contacted the server abuse department and will follow up if these people are not shut-down.</p>
<p>Welcome to the interwebs. This is the second time I have been hijacked in the same way. This is a new theme that I have gone through and can not find malicious php code, and since it&#8217;s in the admin section I can only assume that it&#8217;s a Wordpress security problem. This however is the second time I&#8217;ve found hijacked stuff in my database!</p>
<p>While I have gone through every line of code in both this theme and the access logs for this site I can not find how this is happening. Which make me very cranky as it&#8217;s happening.</p>
<p>My apologies to anyone who has gotten alerts. Systems are in place to track and prevent these from happening anymore. I hate Wordpress 2.5 (I just set up a site using it) but if it&#8217;s a 2.3 problem I&#8217;ll have to upgrade.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.techraving.com%2F2008%2F04%2F19%2Fthis-hijacking-is-getting-stupid%2F';
  addthis_title  = 'This+hijacking+is+getting+stupid%21';
  addthis_pub    = 'DarkRyder';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.techraving.com/2008/04/19/this-hijacking-is-getting-stupid/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
