<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4185664664749718629</id><updated>2024-10-25T00:08:30.693-05:00</updated><category term="python"/><category term="technology"/><title type='text'>Electric Sheep</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://keywordarguments.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://keywordarguments.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>hderms</name><uri>http://www.blogger.com/profile/09071245396027726049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrP5L5EQIwN2SiS94AciL0Aj010JE6NBWRxN1xoWMwlr7kLlzLgQr9Ne48PEF0O8DLpDnPwMzrhtO8bhQ8RYeWjXW4xFezIhYiU_6w4pnVmCb6F90FC3vgsogbBdCFkA/s220/cropped.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4185664664749718629.post-9118282387646849544</id><published>2011-05-20T22:13:00.003-05:00</published><updated>2011-05-20T23:27:13.082-05:00</updated><title type='text'>Genetic algz</title><content type='html'>[edit: guess I should make it more clear that I don&#39;t intend this to be an example of good practice, It is just the result of a game.&lt;br /&gt;
&lt;br /&gt;
When I start a new project in Python, I often ask myself what features of Python could make this project better. A natural enough question if you are learning a language, and I think it is vital to try new things as often as possible in programming (unless you&#39;re programming in a production environment, then by all means steer clear).&lt;br /&gt;
&lt;br /&gt;
&quot;So&quot;, I asked myself, &quot;what could Python possibly assist with in the implementation of a genetic algorithm?&quot;. Now using lists were an obvious choice: slices could take a bit of the load off of programming a cross-over function. Boring.&lt;br /&gt;
&lt;br /&gt;
For the uniniated, &quot;cross-over&quot; is performed in a genetic algorithm when two or more organisms trade contiguous segments of their gene data. In simple enough terms, this is thought to preserve favorable segments of genes (those that contribute to fitness) instead of relying on the same favorable mutations to occur in multiple organisms.&lt;br /&gt;
&lt;br /&gt;
One could easily use a list, or code their own structure that would accomodate this behavior. After all, it would be simple enough to iterate through a list and just exchange elements.&amp;nbsp; But I wanted to be more general, and to do something that would be a bit more difficult in most languages. So I used vars(). Leveraging the ability of dynamic typing and local variable lookup, we have the ability to perform cross-over on relatively arbitrary objects (no guarantee what is produced will work). Because python allows us to change function binding dynamically, we can also do this with functions (though I suspect this isn&#39;t as easy to make use of, perhaps with currying and large set of lambdas. one could find a way.)&lt;br /&gt;
&lt;pre style=&quot;font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%&quot;&gt;&lt;code&gt;        def meta_mate(self, matee,mater, rand_func = None):
                #uses pythons ability to get a dict of local variables in order to allow us to switch them without knowing anything about them
                child = organism(self)
                if not rand_func:
                        return
                for x in vars(matee).keys():
                        if x in vars(matee).keys() and x in vars(mater).keys():
                                if rand_func():
                                        vars(child)[x]=vars(matee)[x]
                                else:
                                        vars(child)[x] = vars(mater)[x]
                        elif x in vars(mater).keys():
                                vars(child)[x] = vars(mater)[x]
                        elif x in vars(matee).keys():
                                vars(child)[x] = vars(matee)[x]

                return child

&lt;/code&gt;&lt;/pre&gt;(I realize I can do this with zip, if expressions and a lambda, but for blogging purposes I decided to do this in more than one line)&lt;br /&gt;
Iterating through the local variable lookup dict and switching items seems to be a nice way to achieve cross-over. Unfortunately I can&#39;t think of a very good way to implement a generic mutate() function like this, so this isn&#39;t as useful as I&#39;d like. If anyone has any ideas, feel free to comment!</content><link rel='replies' type='application/atom+xml' href='http://keywordarguments.blogspot.com/feeds/9118282387646849544/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://keywordarguments.blogspot.com/2011/05/when-i-start-new-project-in-python-i.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default/9118282387646849544'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default/9118282387646849544'/><link rel='alternate' type='text/html' href='http://keywordarguments.blogspot.com/2011/05/when-i-start-new-project-in-python-i.html' title='Genetic algz'/><author><name>hderms</name><uri>http://www.blogger.com/profile/09071245396027726049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrP5L5EQIwN2SiS94AciL0Aj010JE6NBWRxN1xoWMwlr7kLlzLgQr9Ne48PEF0O8DLpDnPwMzrhtO8bhQ8RYeWjXW4xFezIhYiU_6w4pnVmCb6F90FC3vgsogbBdCFkA/s220/cropped.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4185664664749718629.post-302167659844755641</id><published>2011-05-10T17:33:00.001-05:00</published><updated>2011-05-10T17:33:03.503-05:00</updated><title type='text'>Short challenge</title><content type='html'>What does the following statement do:&lt;br /&gt;
reduce(lambda x, y:x if x[0] &amp;gt; y[0] else y,filter(lambda x: reduce(lambda x, y: x and y, map(lambda b: b[0] == b[1],zip(list(str(x[1])),list(str(x[1])[::-1])))) else False, zip(range(0,999),map(lambda x:x**2,range(0,999)))))&lt;br /&gt;
&lt;br /&gt;
if you can figure this out, beat me and make a shorter one</content><link rel='replies' type='application/atom+xml' href='http://keywordarguments.blogspot.com/feeds/302167659844755641/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://keywordarguments.blogspot.com/2011/05/short-challenge.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default/302167659844755641'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default/302167659844755641'/><link rel='alternate' type='text/html' href='http://keywordarguments.blogspot.com/2011/05/short-challenge.html' title='Short challenge'/><author><name>hderms</name><uri>http://www.blogger.com/profile/09071245396027726049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrP5L5EQIwN2SiS94AciL0Aj010JE6NBWRxN1xoWMwlr7kLlzLgQr9Ne48PEF0O8DLpDnPwMzrhtO8bhQ8RYeWjXW4xFezIhYiU_6w4pnVmCb6F90FC3vgsogbBdCFkA/s220/cropped.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4185664664749718629.post-9209040586694590781</id><published>2011-04-12T12:38:00.007-05:00</published><updated>2011-04-13T00:17:13.858-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python"/><category scheme="http://www.blogger.com/atom/ns#" term="technology"/><title type='text'>Duck-typing in practice</title><content type='html'>As a recent Python devotee, I have found that duck-typing can sometimes be harder in practice than in theory. In theory, if it looks like a duck, swims like a duck, and quacks like a duck, then it&#39;s a duck. In practice, or at least in my practice, there are ducks incapable of flight. Or wolves in ducks&#39; clothing. All attempted humor aside, there is difficulty in reconciling a type-based view of object oriented programming and the much more laissez-faire approach that Python adopts. &lt;br /&gt;
&lt;br /&gt;
It should be obvious that duck-typing provides a more variable approach to implementation, something which was included in the driving factors behind the invention of OOP. Nonetheless, #python on freenode is inundated with questions from inexperienced programmers as to the proper way to distinguish a list from a string, or other similar situations. I will side with the operators of #python and assert that the correct way doesn&#39;t happen to be isinstance(). This function&#39;s usage is contrary to the idea of duck-typing and, in its positive form, should be avoided at all costs. However, how &lt;i&gt;does &lt;/i&gt; one distinguish a list from a string?&lt;br /&gt;
&lt;br /&gt;
On the surface, there are many similarities between these two datatypes. Both possess a length, have __add__ ,__mul__ , and &amp;nbsp;__contains__ methods.&amp;nbsp;One example of a method they don&#39;t share is the append method.&lt;br /&gt;
&lt;br /&gt;
What I am getting to is that, while duck-typing ensures that a function, or bit of code will work regardless of type, so long as it has the proper interface, what happens when two things share the relevant methods and the only thing distinguishing them is irrelevant to the problem at hand? It is easy to dismiss these situations as inconsequential given the nature of duck-typing (if they share the relevant interface, then it SHOULD work). But is it possible to make concessions? Is duck-typing compatible with negative definition by type (a set of &quot;is not isinstance()&quot; checks)? After all, if something looks like a duck, swims like a duck, and quacks like a duck, can we at least check to make sure it isn&#39;t a wolf in duck&#39;s clothing?&lt;br /&gt;
&lt;br /&gt;
If the number of question marks in this post are any indication, I invite discussion on this topic, perhaps someone more&amp;nbsp;knowledgeable&amp;nbsp;than me will have more declarative sentences.</content><link rel='replies' type='application/atom+xml' href='http://keywordarguments.blogspot.com/feeds/9209040586694590781/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://keywordarguments.blogspot.com/2011/04/duck-typing-in-practice.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default/9209040586694590781'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default/9209040586694590781'/><link rel='alternate' type='text/html' href='http://keywordarguments.blogspot.com/2011/04/duck-typing-in-practice.html' title='Duck-typing in practice'/><author><name>hderms</name><uri>http://www.blogger.com/profile/09071245396027726049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrP5L5EQIwN2SiS94AciL0Aj010JE6NBWRxN1xoWMwlr7kLlzLgQr9Ne48PEF0O8DLpDnPwMzrhtO8bhQ8RYeWjXW4xFezIhYiU_6w4pnVmCb6F90FC3vgsogbBdCFkA/s220/cropped.JPG'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4185664664749718629.post-7452973612370949669</id><published>2011-04-08T12:53:00.004-05:00</published><updated>2011-04-08T15:40:18.617-05:00</updated><title type='text'>Keywordargs Followup</title><content type='html'>&lt;div&gt;This is a follow-up post to my very first blog post, in which I introduced a pattern that I like to use. In this post I will clarify a bit of the reasoning behind me using it, and perhaps why you should use it as well.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;So that&#39;s all well and good but what&#39;s the benefit of using this pattern? Having a variable number of arguments can be extremely useful, and often can allow one to achieve desired behavior with less effort. However, it can at times increase the ambiguity of a function, or decrease its security. Having arguments assigned to keywords definitely reduces ambiguity, although it can increase the number of keystrokes for doing even a simple task. With this in mind, some of the benefits of using this method are:&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;Variable composition of arguments without using something like overloading, which should reduce code duplication. An example of this is: say you wanted to create a function that plots some form of graph. You want to have one function to handle both 2d and 3d graphing, with the data to be graphed passed as arguments. One way of checking which case you&#39;re in is to do something like set([&#39;axis1&#39;,&#39;axis2&#39;,&#39;axis3&#39;]).issubset(kwargs.keys()). This is one of those moments where I really appreciate python&#39;s clarity and respect for mathematics.&lt;/li&gt;
&lt;li&gt;Regex-based searching of arguments can be extremely useful, especially when these keyword-argument dicts are being formed from user input.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Since the arguments are stored as a dict, one can have several dictionaries with sets of default values for this functions behavior. If, like in the previous example, you discovered the user was trying to plot 3d data, it would be easy to provide a set of default values for that particular circumstance&lt;/li&gt;
&lt;/ul&gt;&lt;div&gt;In order to really properly leverage the power of having dictionaries holding the default values, I think it requires an additional behavior to the dict type:&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;class updict(dict):
        def healthyMerge(self, target):
                if not target.keys():
                        return 
                for (key,value) in target.iteritems():
                    self.update(tup for tup in target.iteritems() if tup[0] not in self)


if __name__==&quot;__main__&quot;:
        testDict = updict()
        mergeDict = {}

        testDict[&#39;a&#39;]=1
        testDict[&#39;b&#39;]=2

        mergeDict[&#39;b&#39;]=None
        mergeDict[&#39;c&#39;] = 3

        testDict.healthyMerge(mergeDict)
        print testDict

        testDict.update(mergeDict)
        print testDict
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;(Thanks to the redditor wot-the-phuck for a very pythonic suggestion for my code&lt;br /&gt;
discussion located at:&lt;br /&gt;
http://www.reddit.com/r/Python/comments/gloje/usage_of_dictionary_arguments_in_python/c1oglxf)&lt;br /&gt;
&lt;br /&gt;
Here you&#39;ll see something I&#39;ve named healthyMerge because I&#39;m not exactly sure what the proper name of this kind of merge is. Since it seems that the basic dict class lacks a way of simply updating a dictionary with values that aren&#39;t already set, I&#39;ve created a very simple one. I make no guarantees about this code, it just serves as an example.</content><link rel='replies' type='application/atom+xml' href='http://keywordarguments.blogspot.com/feeds/7452973612370949669/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://keywordarguments.blogspot.com/2011/04/keywordargs-followup.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default/7452973612370949669'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default/7452973612370949669'/><link rel='alternate' type='text/html' href='http://keywordarguments.blogspot.com/2011/04/keywordargs-followup.html' title='Keywordargs Followup'/><author><name>hderms</name><uri>http://www.blogger.com/profile/09071245396027726049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrP5L5EQIwN2SiS94AciL0Aj010JE6NBWRxN1xoWMwlr7kLlzLgQr9Ne48PEF0O8DLpDnPwMzrhtO8bhQ8RYeWjXW4xFezIhYiU_6w4pnVmCb6F90FC3vgsogbBdCFkA/s220/cropped.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4185664664749718629.post-4427883722537048243</id><published>2011-04-07T17:26:00.006-05:00</published><updated>2011-04-08T15:05:25.132-05:00</updated><title type='text'>Post[0];</title><content type='html'>As a professionally-oriented blog, and moreover, a software oriented one, this will be terse and to the point. Personal details will be kept to a minimum, software details will be explored at a significant depth.&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;For my first post, I&#39;d like to describe a rather simple, and no-doubt popular technique that I have been using recently while coding in python. This technique is what I call a &#39;keyword argument&#39;. Inspired by the type of argument in python that can be passed to a function as follows:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
def foo(**kwargs):
    for (key,value) in kwargs.items():
        print &quot;%s for %s&quot; % (key,value)
if __name__ == &quot;__main__&quot;:
    foo(a = 1, b = 2, c = 3)
&lt;/code&gt;&lt;/pre&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;the output of this program is of course&amp;nbsp;&lt;/span&gt;(or some permutation of the following):&amp;nbsp;&lt;/div&gt;&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;a for 1
c for 3 
b for 2
&lt;/code&gt;&lt;/pre&gt;&lt;div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;now what is interesting about this idea? It is a fairly standard way of passing variable-length arguments where values are assigned to variables. The way it has interested me is to borrow this kind of pattern, except instead of using a dictionary built by the python interpreter out of arguments to a function, I use a &quot;command&quot; function which builds this dictionary from some input. A simplified version is as follows (please ignore the naive regex):&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;def command(self, command_string):
    command_root = re.findall(r&quot;^(\w+)&quot;,command_string)
    key_argument_tuples = re.findall(r&quot;--([\w]+)=([^ ^=]+)&quot;,command_string)
    for x in key_args:
        cmd_dict[x[0]] = x[1]
    self.function_dict[command_root](kwargs=cmd_dict)

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;this is an extremely simple way (through the use of first-class functions) to build arguments for functions through a generic interface. I will clarify the part about first-class functions (self.function_dict) in my next post.&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;blog-&amp;gt;previouspost = currentpost;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://keywordarguments.blogspot.com/feeds/4427883722537048243/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://keywordarguments.blogspot.com/2011/04/post0.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default/4427883722537048243'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4185664664749718629/posts/default/4427883722537048243'/><link rel='alternate' type='text/html' href='http://keywordarguments.blogspot.com/2011/04/post0.html' title='Post[0];'/><author><name>hderms</name><uri>http://www.blogger.com/profile/09071245396027726049</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrP5L5EQIwN2SiS94AciL0Aj010JE6NBWRxN1xoWMwlr7kLlzLgQr9Ne48PEF0O8DLpDnPwMzrhtO8bhQ8RYeWjXW4xFezIhYiU_6w4pnVmCb6F90FC3vgsogbBdCFkA/s220/cropped.JPG'/></author><thr:total>0</thr:total></entry></feed>