The blergh

The personal dumping grounds of an engineer, nerdist and human

about me

You have landed in the personal “blergh” of Gary Wright. I use this space to ramble about things such as network engineering, software development and life stuff…

Its a Major Award Slash

It has been a wild few months since moving back here to the great PNW. Something that recently happened is obtaining an award for a recent join patent that I filed with a couple of close friends / engineers at Microsoft back in October. The patent is based on a concept of flexible vm disk images, which would incorporate a layer of metadata to describe the different configuration standards in which to be used, however do away with the need to store those individual images directly on disk. This would also in turn mean that you can speed up your deployments easily, without the extra overhead.

http://i60.tinypic.com/rjp44i.jpg

written

Is This Thing On?

Still here, alive and kicking. Getting settled back into the PNW and now living in Redmond. Will get a new blog post up soon, I promise! :)

written

Is This Blog Abandoned?

Sorry for the lack of posts lately, but I am in the process currently of moving from SVC back to good ol’ Seattle Washington. Still working on Microsoft, except now I will be back to an area that makes much more sense for an aging techy like me. Cost of living will certainly feel a lot better too. I will be back soon though, hopefully with better consistancy in updates as well.

written

Python: Protip - Enable Tab Completion in Python Shell

I was recently chatting with someone that made the statement; “The only reason why I use my text editor is because of tab completion, I am a minimalist pythonista.”. I responded with, “If your a minimalist, why don’t you have tab completion enabled in the interpreter?” They were unaware of the ability to enable tab completion within python. Here is a my PSA on how to enable this behavior. :)



By following these instructions you will be able to enable tab-completion within your python shell.



Preperation steps:


Before enabling tab-completion, you may need to install 2 python modules (rlcompleter, readline). While these libraries are mostly included with python2.6+, some versions (OS X, for example), require the updated version to allow readline to function correctly.



shell
1
2
pip install readline
pip install rlcompleter



Step 1:


If you don’t already have a ~/.pyrc file, this command will create one for you, which is required for this trick to work.



shell
1
#> touch ~/.pyrc



Step 2:


Now we will create a file within your homedir which will instruct python to bind tab completion at python launch.



~/.pyrc
1
2
3
import rlcompleter
import readline
readline.parse_and_bind("tab: complete")



Step 3:


Now to ensure that your newly created ~/.pyrc file is executed each time python starts, add the following to your ~/.bashrc (or equiv. shell rc).



shell
1
2
3
4
5
#> export PYTHONSTARTUP="[PATH TO PYRC FILE]/.pyrc"

*OR TO MAKE THE CHANGE PERSIST TERMINAL CLOSURE

#> echo export PYTHONSTARTUP="[PATH TO PYRC FILE].pyrc" >> ~/.bashrc



Now to test, execute the following:



shell
1
#> source ~/.bashrc   #reloads your ~/.bashrc file (if you added the entry to your ~/.bashrc, else ignore)



Now we will open the actual python shell, and view the tab completion goodness.



shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.
Display all 234 possibilities? (y or n)
os.EX_CANTCREAT             os.class(               os.fchdir(                  os.putenv(
os.EX_CONFIG                os.delattr(             os.fchmod(                  os.read(
os.EX_DATAERR               os.dict                 os.fchown(                  os.readlink(
os.EX_IOERR                 os.doc                  os.fdopen(                  os.remove(
os.EX_NOHOST                os.file                 os.fork(                    os.removedirs(
os.EX_NOINPUT               os.format(              os.forkpty(                 os.rename(
os.EX_NOPERM                os.getattribute(        os.fpathconf(               os.renames(
os.EX_NOUSER                os.hash(                os.fstat(                   os.rmdir(
os.EX_OK                    os.init(                os.fstatvfs(                os.sep
os.EX_OSERR                 os.name                 os.fsync(                   os.setegid(
os.EX_OSFILE                os.new(                 os.ftruncate(               os.seteuid(
os.EX_PROTOCOL              os.package              os.getcwd(                  os.setgid(
os.EX_SOFTWARE              os.reduce(              os.getcwdu(                 os.setgroups(
os.EX_TEMPFAIL              os.reduce_ex(           os.getegid(                 os.setpgid(
os.EX_UNAVAILABLE           os.repr(                os.getenv(                  os.setpgrp(
os.EX_USAGE                 os.setattr(             os.geteuid(                 os.setregid(
os.F_OK                     os.sizeof(              os.getgid(                  os.setreuid(
os.NGROUPS_MAX              os.str(                 os.getgroups(               os.setsid(
os.O_APPEND                 os.subclasshook(        os.getloadavg(              os.setuid(
os.O_ASYNC                  os.copy_reg                os.getlogin(                os.spawnl(
os.O_CREAT                  os.execvpe(                os.getpgid(                 os.spawnle(
os.O_DIRECTORY              os.exists(                 os.getpgrp(                 os.spawnlp(
os.O_DSYNC                  os.exit(                   os.getpid(                  os.spawnlpe(
os.O_EXCL                   os.get_exports_list(       os.getppid(                 os.spawnv(
os.O_EXLOCK                 os.make_stat_result(       os.getsid(                  os.spawnve(
os.O_NDELAY                 os.make_statvfs_result(    os.getuid(                  os.spawnvp(
os.O_NOCTTY                 os.pickle_stat_result(     os.initgroups(              os.spawnvpe(
os.O_NOFOLLOW               os.pickle_statvfs_result(  os.isatty(                  os.stat(
os.O_NONBLOCK               os.spawnvef(               os.kill(                    os.stat_float_times(
os.O_RDONLY                 os.abort(                   os.killpg(                  os.stat_result(
os.O_RDWR                   os.access(                  os.lchflags(                os.statvfs(
os.O_SHLOCK                 os.altsep                   os.lchmod(                  os.statvfs_result(
os.O_SYNC                   os.chdir(                   os.lchown(                  os.strerror(
os.O_TRUNC                  os.chflags(                 os.linesep                  os.symlink(
os.O_WRONLY                 os.chmod(                   os.link(                    os.sys
os.P_NOWAIT                 os.chown(                   os.listdir(                 os.sysconf(
os.P_NOWAITO                os.chroot(                  os.lseek(                   os.sysconf_names
os.P_WAIT                   os.close(                   os.lstat(                   os.system(
os.R_OK                     os.closerange(              os.major(                   os.tcgetpgrp(
os.SEEK_CUR                 os.confstr(                 os.makedev(                 os.tcsetpgrp(
os.SEEK_END                 os.confstr_names            os.makedirs(                os.tempnam(
os.SEEK_SET                 os.ctermid(                 os.minor(                   os.times(
os.TMP_MAX                  os.curdir                   os.mkdir(                   os.tmpfile(
os.UserDict                 os.defpath                  os.mkfifo(                  os.tmpnam(
os.WCONTINUED               os.devnull                  os.mknod(                   os.ttyname(
os.WCOREDUMP(               os.dup(                     os.name                     os.umask(
os.WEXITSTATUS(             os.dup2(                    os.nice(                    os.uname(
os.WIFCONTINUED(            os.environ                  os.open(                    os.unlink(
os.WIFEXITED(               os.errno                    os.openpty(                 os.unsetenv(
os.WIFSIGNALED(             os.error(                   os.pardir                   os.urandom(
os.WIFSTOPPED(              os.execl(                   os.path                     os.utime(
os.WNOHANG                  os.execle(                  os.pathconf(                os.wait(
os.WSTOPSIG(                os.execlp(                  os.pathconf_names           os.wait3(
os.WTERMSIG(                os.execlpe(                 os.pathsep                  os.wait4(
os.WUNTRACED                os.execv(                   os.pipe(                    os.waitpid(
os.W_OK                     os.execve(                  os.popen(                   os.walk(
os.X_OK                     os.execvp(                  os.popen2(                  os.write(
os._Environ                 os.execvpe(                 os.popen3(
os.all                  os.extsep                   os.popen4(
>>> os.



In the above example, I have imported the “os” module, typed “os.” and have pressed . Now all of the possible matched object names available within the module are shown. viola, python tab completion enabled.

written in python, shell, tab complete

Python: Intro to Decorators

Do you even decorate, bro? – Intro to decorators

When looking at the Python programming language, some of the most powerful and, unfortunately very under utilized “macro” design pattern within the language. One of the reasons I believe this to be the case is that most explanations of decorators suck. When you read the word “decorator” in regards to application development, most people generally think of http://www.amazon.com/gp/product/0201633612/ref=ase_bruceeckelA/ . While the “decorator” in Python can indeed be implemented in this fashion of design pattern, it is a very limited version of what decorators can actually accomplish.

I believe that decorators can actually be thought of as more “macros”, than the classical design pattern aforementioned above. http://en.wikipedia.org/wiki/Macro_(computer_science) , as defined by Wikipedia is “a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to a replacement output sequence (also often a sequence of characters) according to a defined procedure.” In short, if you have some metadata that you want to apply to any class, function or object, dress it up with a decorator.

Enough already, get to the example!

Decorators allow you to inject or modify code in functions or classes. Sounds a bit like Aspect-Oriented Programming (AOP) in Java, doesn’t it? So lets say you have an action that you would like to perform at the entry point (execution) or exit point (return) of a class, function. This is a prime example as when to use a decorator.

decorator example.py
1
2
3
@myDecorator
def myFunction1():
     print (hello from inside myFunction1())

note the

myscript
1
@myDecorator # The @myDecorator denotes the the application of a decorator

Function decorators

So by affixing a @decoratorname on a line directly above a function (or object), denotes the application of the results of a decorator function. In the previous example, when the python parser passes over the “myFunction()”, “myFunction()” is compiled and is, in turn, passed to the “myDecorator code block. This code block creates a function-like object that is ultimately what is returned when calling the “myFunction()” function. Confusing? Maybe this example will help.

decorator_example2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class myDecorator(object):
    def init(self, func_object):
        print ("Hello from inside myDecorator.init()")
        func_object() # Execute func_object() to prove it has been executed.
    def call(self):
        print ("Hello from inside myDecorator.call()")


@myDecorator
def myFunction():
    print ("Hello from inside myFunction()")


print ("Finished decorating myFunction()")
myFunction()

When you execute the above code, your results will look something like this:

output
1
2
3
4
Hello from inside myDecorator.init()
Hello from inside myFunction()
Finished decorating myFunction()
Hello from inside myDecorator.call()

Note that the initialization of myDecorator(object) is executed when myFunction() is called. Due to the fact that we call “func_object()”, which is just myFunction() passed into the decorator class.init as a function object labeled “func_object”. Generally, you’ll pass the function object in the constructor and later use it in the call() method.

When myFunction() is called after it has been decorated, we get completely different behavior; the myDecorator.call() method is called instead of the original code. This is due to the fact that decoration replaces the original function object with the result of the decoration. In our case, the myDecorator object replaces myFunction.

This is it for the introduction to Python decorators, however look out for part II, coming soon… ☺

written in Python, decorators, design patterns