tag:blogger.com,1999:blog-18278646996246139352024-09-09T16:24:32.671-04:00Change for the BetterPython, Frugal Living, and Wee Bit of Team Fortress 2Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-1827864699624613935.post-55296019687917381132014-04-11T18:03:00.000-04:002014-04-12T12:00:50.340-04:00Vagrant can't mount shared folder in VirtualBox 4.3.10 FixSetting up Virtualbox/Vagrant today on a new Windows 7 PC and ran into this problem while that my host directory wouldn't mount /vagrant during vagrant up. <br /> <br /> <div class="highlight"><pre><span class="gp">jbisbee@jbisbeewin7:~/vagrant/test$</span> vagrant up <span class="go">Bringing machine &#39;default&#39; up with &#39;virtualbox&#39; provider...</span> <span class="go">==&gt; default: Checking if box &#39;hashicorp/precise64&#39; is up to date...</span> <span class="go">==&gt; default: Clearing any previously set forwarded ports...</span> <span class="go">==&gt; default: Clearing any previously set network interfaces...</span> <span class="go">==&gt; default: Preparing network interfaces based on configuration...</span> <span class="go"> default: Adapter 1: nat</span> <span class="go"> default: Adapter 2: hostonly</span> <span class="go">==&gt; default: Forwarding ports...</span> <span class="go"> default: 22 =&gt; 2222 (adapter 1)</span> <span class="go">==&gt; default: Booting VM...</span> <span class="go">==&gt; default: Waiting for machine to boot. This may take a few minutes...</span> <span class="go"> default: SSH address: 127.0.0.1:2222</span> <span class="go"> default: SSH username: vagrant</span> <span class="go"> default: SSH auth method: private key</span> <span class="go"> default: Warning: Connection timeout. Retrying...</span> <span class="go">==&gt; default: Machine booted and ready!</span> <span class="go">GuestAdditions seems to be installed (4.3.10) correctly, but not running.</span> <span class="go">stdin: is not a tty</span> <span class="go">Starting the VirtualBox Guest Additions ...done.</span> <span class="go">==&gt; default: Checking for guest additions in VM...</span> <span class="go">==&gt; default: Configuring and enabling network interfaces...</span> <span class="go">==&gt; default: Mounting shared folders...</span> <span class="go"> default: /vagrant =&gt; C:/cygwin64/home/jbisbee/vagrant/test</span> <span class="go">Failed to mount folders in Linux guest. This is usually because</span> <span class="go">the &quot;vboxsf&quot; file system is not available. Please verify that</span> <span class="go">the guest additions are properly installed in the guest and</span> <span class="go">can work properly. The command attempted was:</span> <span class="go">mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` /vagrant /vagrant</span> <span class="go">mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` /vagrant /vagrant</span> </pre></div> <br /> First thing I did was ssh to the vm and run the commands: <br /> <br /> <div class="highlight"><pre><span class="gp">vagrant@precise64:~$</span> sudo mount -t vboxsf -o <span class="nv">uid</span><span class="o">=</span><span class="sb">`</span>id -u vagrant<span class="sb">`</span>,gid<span class="o">=</span><span class="sb">`</span>getent group vagrant | cut -d: -f3<span class="sb">`</span> /vagrant /vagrant <span class="go">mount: Protocol error</span> <span class="gp">vagrant@precise64:~$</span> sudo mount -t vboxsf -o <span class="nv">uid</span><span class="o">=</span><span class="sb">`</span>id -u vagrant<span class="sb">`</span>,gid<span class="o">=</span><span class="sb">`</span>id -g vagrant<span class="sb">`</span> /vagrant /vagrant <span class="go">mount: Protocol error</span> </pre></div> <br /> After just getting "Protocol error", I looked at the syslog and seeing "sf_read_super_aux err=-71". <br /> <br /> <div class="highlight"><pre><span class="go">Apr 11 17:56:40 precise64 kernel: [ 18.253255] sf_read_super_aux err=-71</span> <span class="go">Apr 11 17:56:40 precise64 kernel: [ 18.344137] sf_read_super_aux err=-71</span> </pre></div> <br /> Now googling this error, I found <a href="https://github.com/mitchellh/vagrant/issues/3341">Vagrant Bug #3341</a> from 15 days ago and the missing symlink that caused it all <br /> <br /> <div class="highlight"><pre><span class="gp">vagrant@precise64:~$</span> sudo ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions </pre></div> <br /> <b>Success! :)</b> <br /> <br /> <div class="highlight"><pre><span class="gp">jbisbee@jbisbeewin7:~/vagrant/test$</span> vagrant up <span class="go">Bringing machine &#39;default&#39; up with &#39;virtualbox&#39; provider...</span> <span class="go">==&gt; default: Checking if box &#39;hashicorp/precise64&#39; is up to date...</span> <span class="go">==&gt; default: Clearing any previously set forwarded ports...</span> <span class="go">==&gt; default: Clearing any previously set network interfaces...</span> <span class="go">==&gt; default: Preparing network interfaces based on configuration...</span> <span class="go"> default: Adapter 1: nat</span> <span class="go"> default: Adapter 2: hostonly</span> <span class="go">==&gt; default: Forwarding ports...</span> <span class="go"> default: 22 =&gt; 2222 (adapter 1)</span> <span class="go">==&gt; default: Booting VM...</span> <span class="go">==&gt; default: Waiting for machine to boot. This may take a few minutes...</span> <span class="go"> default: SSH address: 127.0.0.1:2222</span> <span class="go"> default: SSH username: vagrant</span> <span class="go"> default: SSH auth method: private key</span> <span class="go"> default: Warning: Connection timeout. Retrying...</span> <span class="go"> default: Warning: Connection timeout. Retrying...</span> <span class="go"> default: Warning: Connection timeout. Retrying...</span> <span class="go">==&gt; default: Machine booted and ready!</span> <span class="go">GuestAdditions 4.3.10 running --- OK.</span> <span class="go">==&gt; default: Checking for guest additions in VM...</span> <span class="go">==&gt; default: Configuring and enabling network interfaces...</span> <span class="go">==&gt; default: Mounting shared folders...</span> <span class="go"> default: /vagrant =&gt; C:/cygwin64/home/jbisbee/vagrant/test</span> <span class="go">==&gt; default: VM already provisioned. Run `vagrant provision` or use `--provision` to force it</span> </pre></div> <br /> <br /> <br />Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com9tag:blogger.com,1999:blog-1827864699624613935.post-18494420895330361342014-04-08T23:46:00.001-04:002014-04-09T00:02:05.544-04:00Getting Started with pylint<div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-6XiNGCUoBMI/U0S2J7WR68I/AAAAAAAAD8Y/dVco-_2RNzs/s1600/Screen+Shot+2014-04-08+at+10.52.32+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-6XiNGCUoBMI/U0S2J7WR68I/AAAAAAAAD8Y/dVco-_2RNzs/s1600/Screen+Shot+2014-04-08+at+10.52.32+PM.png" /></a></div><br /> Being somewhat new to Python, I figured I'd give <a href="http://www.pylint.org/">pylint</a> a try. In Perl we have <a href="http://search.cpan.org/~thaljef/Perl-Critic/">perlcritic</a> which serves a very similar function (although in Perl files are parsed in a DOM-like way because as some of us painfully know, the only thing that can truly parse Perl is Perl). <br /><br /> <h1>Setting Up</h1> First things first, let's setup a test virtualenv so we don't clobber anything. If you want to install globally or already have a project you'd like to lint, skip this step. <br /> <br /> <div class="highlight"> <pre><span class="gp">jbisbee@beni:~$</span> mkvirtualenv <span class="nb">test</span> <span class="go">New python executable in test/bin/python</span> <span class="go">Installing</span> <span class="go">Setuptools.............................................................................................done.</span> <span class="go">Installing</span> <span class="go">Pip....................................................................................................................................done.</span> </pre></div></br /> Now pip install pylint <br /> <br /> <div class="highlight"> <pre><span class="gp">(test)jbisbee@beni:~$</span> pip install pylint <span class="go">Downloading/unpacking pylint</span> <span class="go">...</span> <span class="go">Successfully installed pylint logilab-common astroid</span> <span class="go">Cleaning up...</span> </pre> </div> <br /> So out of the box I get a warning message saying I don't have a default configuration. Before I start looking at the output, I figure I should probably figure out where the defualt rcfile is stored (~/.pylintrc) and what format its in. <br /> <br /> <div class="highlight"> <pre><span class="gp">jbisbee@beni$</span> pylint sample.py <span class="go">No config file found, using default configuration</span> <span class="go">...</span> </pre> </div> <br /> <h1>Generate Your ~/.pylintrc File</h1> Lucky for us, the authors have included the handy --generate-rcfile switch that will generate one for you. The rcfile generation option also takes into account all switches you pass into pylint and will include those in your resulting file. The rcfile is printed to standard out which you can just redirect to your .pylintrc file in your home directory. <br /> <br /> <div class="highlight"> <pre><span class="gp">(test)jbisbee@beni:~/src/test$</span> pylint --generate-rcfile &gt; ~/.pylintrc </pre> </div><br /> <h1>Explore and Tweak To Your Liking</h1> The resulting file is pretty lengthy, but I'd like to highlight some of the various options and sections that I took interest in. I took some of the descriptions directly from the documentation. <b>disable=</b><br /> Disable the message, report, category or checker with the given id(s). If you or your team ends up choosing not to enforce certain errors or warnings that pylint produces, just add the error number (or in later versions, the human readable string) to disable the messages from showing up. <br /><br /> <b>output-format=colorize</b><br /> Set the output format. Available formats are text, parseable, colorized, msvs (visual studio) and html. You can also give a reporter class, mypackage.mymodule.MyReporterClass. Going to default to colorize. <br /><br /> <b>reports=no</b><br /> Tells whether to display a full report or only the messages. No reports by default for me. <br /><br /> <b>max-line-length=120</b><br /> Maximum number of characters on a single line. I'm going to set this from 80 to 120. Purists out there, all I have to say is, "Haters gonna hate." :) <br /><br /> <b>generated-members=objects</b><br /> List of members which are set dynamically and missed by pylint inference system. Django ORM's 'objects' is only value I have presently. Feel free to suggest other common generated members. <br /><br /> <b>Similarities Section</b><br /> Similar lines in %s files Indicates that a set of similar lines has been detected among multiple file. This usually means that the code should be refactored to avoid this duplication. I love the idea that pylint does this and I'd like to get some feedback for folks who encountered this error and how accurate it was. <br /><br /> <b>Design Section</b><br /> This sections is my favorite. It points out possible design flaws in your code based on a set of rules you can tweak. I consider my OO design to be pretty darn good, but as I was quickly porting a simple all in one command line program into classes, I got flagged with 'max-attributes' error. The error states if that you have more than 7 attributes you're most likely doing something wrong. (I had 8) It pointed out 5 of those attributes belonged in a separate class of their own and made my design cleaner (which never really hurts) <br /><br /> <h1>IDE/Editor Support</h1> One of the coolest things about pylint is the IDE/editor integration that can give you real time feedback as you're coding. Visit the <a href="http://docs.pylint.org/ide-integration">IDE integration page</a> on the pylint website for more information. Going attempt to setup <a href="http://www.vim.org/scripts/script.php?script_id=891">pylint.vim</a> soon. :) <br /> <br /> <h1>Would Love Your Feedback</h1> Hey guys, I'm writing these blog posts as an exercise to teach myself and hopefully have others who are in my position, stumble upon my blog posts and actually do them some good. If you have some valuable input, point out pitfalls, or just to say thank you, I would greatly appreciate it. <br /> <br /> Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com8tag:blogger.com,1999:blog-1827864699624613935.post-60367095234775661152014-04-06T09:11:00.000-04:002014-04-06T18:26:12.262-04:00Fix for iTerm2 Numeric Keypad Not Working<a href="http://www.iterm2.com/"><div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-_mAUle1VAqM/U0FLcNz9LgI/AAAAAAAAD4k/eKPMwJ9mqDM/s1600/Screen+Shot+2014-04-06+at+8.40.16+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-_mAUle1VAqM/U0FLcNz9LgI/AAAAAAAAD4k/eKPMwJ9mqDM/s1600/Screen+Shot+2014-04-06+at+8.40.16+AM.png" /></a></div></a><br /> I'd had to fix this a couple of times now on various computers and figured I'd just post the answer in case I ever have to do it again. <blockquote><i>By default the numeric keypad sends function key codes. You can turn that off by selecting the "xterm with numeric keypad" preset in Preferences > Profiles > Keys. <br /> <a href="https://code.google.com/p/iterm2/issues/detail?id=2000">https://code.google.com/p/iterm2/issues/detail?id=2000</a></i></blockquote> For those of you who don't use <a href="http://www.iterm2.com/">iTerm2</a>, I highly recommend it. One of my favorite features is enabling semi-opaque transparency and blur so I can still code and not be distracted the background artifacts. The settings can be found in Preferences > Profiles > Window<br /><br /> <div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/-HBPLeqgUdw4/U0FRF7rUs3I/AAAAAAAAD5o/WLAW77JxHfY/s1600/Screen+Shot+2014-04-06+at+9.05.06+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://1.bp.blogspot.com/-HBPLeqgUdw4/U0FRF7rUs3I/AAAAAAAAD5o/WLAW77JxHfY/s1600/Screen+Shot+2014-04-06+at+9.05.06+AM.png" /></a></div><br /> And these settings end up looking like this<br /><br /> <div class="separator" style="clear: both; text-align: center;"><a href="http://2.bp.blogspot.com/-ahRzh9rJmjo/U0FQ5xd7_sI/AAAAAAAAD5g/yqVC_FDlF0M/s1600/Screen+Shot+2014-04-06+at+9.04.06+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-ahRzh9rJmjo/U0FQ5xd7_sI/AAAAAAAAD5g/yqVC_FDlF0M/s1600/Screen+Shot+2014-04-06+at+9.04.06+AM.png" /></a></div>Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com1tag:blogger.com,1999:blog-1827864699624613935.post-62767615463799781712014-04-05T00:31:00.002-04:002014-04-05T00:31:42.935-04:00Speed Up Key Repeat Input on Your MacSo I'm happy to be blogging again and I though I'd start by sharing a hack I ran across a couple of months ago. &nbsp;It involves adjusting the OSX Key Repeat setting to a lower value that what the GUI allows. &nbsp;This setting is great for developers like me who sometimes navigate through code using arrow keys or like to quickly delete something by holding down the delete key.<br /> <br /> <div class="separator" style="clear: both; text-align: center;"> <i><span style="color: #999999;">System Preferences &gt; Keyboard</span></i></div> <br /> <a href="http://3.bp.blogspot.com/-deJbRjdP5k8/Uz-C4HW1qkI/AAAAAAAAD30/ycqlJs1yc_c/s1600/Screen+Shot+2014-04-05+at+12.11.02+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://3.bp.blogspot.com/-deJbRjdP5k8/Uz-C4HW1qkI/AAAAAAAAD30/ycqlJs1yc_c/s1600/Screen+Shot+2014-04-05+at+12.11.02+AM.png" /></a> <br /> <br /> Now the GUI presently allows you to set Key Repeat to a minimum value of 2, which for many folks, doesn't seem to be fast enough. We can change the value to 1, but before we do, take note of your current setting with the following command.<br /> <br /> <br /> <div class="highlight"> <pre><span class="gp">jbisbee@beni$</span> defaults <span class="nb">read </span>NSGlobalDomain KeyRepeat <span class="go">30</span> </pre> </div> <br /> Now to set the value to 1, we can use the following command. Unfortunately, <span style="background-color: #ffe599;">you will need to reboot</span> to have the change take affect. <br /> <br /> <div class="highlight"> <pre><span class="gp">jbisbee@beni$</span> defaults write NSGlobalDomain KeyRepeat -int 1 </pre> </div> <br /> In the event you don't like the result, simply run the command again with your initial value. <br /> <br /> <div class="highlight"> <pre><span class="gp">jbisbee@beni$</span> defaults write NSGlobalDomain KeyRepeat -int 30 </pre> </div> <br /> <i>If someone knows the mac command to reinitialize, please share. :)</i> <br /> <br /> <br />Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com0tag:blogger.com,1999:blog-1827864699624613935.post-26172541542728039492013-07-27T08:45:00.000-04:002013-07-27T11:28:13.568-04:00python-shell-enhancement - Add History and Tab Completion to the Default Python ShellI decided to take the blog post I wrote earlier this month, <a href="http://jbisbee.blogspot.com/2013/07/add-history-and-tab-completion-to.html">Add History and Tab Completion to the Default Python Shell</a>, and commit code to github under the name <br /> <br /> <div style="text-align: center;"> <a href="https://github.com/jbisbee/python-shell-enhancement"><span style="font-family: inherit; font-size: large;">python-shell-ehnancement</span></a></div> <br /> I updated the script with feedback I got from the blog and reddit and fixed tab completion to work correctly on a mac by default. Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com0tag:blogger.com,1999:blog-1827864699624613935.post-21379723224535646772013-07-26T14:36:00.002-04:002013-07-26T14:41:31.040-04:00Double Window Sniper BonkThis is a blatant geek post because playing <a href="http://www.teamfortress.com/">Team Fortress 2</a> is one of favorite hobbies. Anyone who knows me (gaming-wise), knows I like playing the <a href="http://wiki.teamfortress.com/wiki/Double_Cross">Double Cross</a> map. On this map there is a underhanded tactic called window sniping where a sniper stands sideways in he window and is able to shoot into the enemy courtyard. The other day, two snipers were hanging out in the window and I managed to get this screen cap of me <a href="http://wiki.teamfortress.com/wiki/Home_Run">taunt killing</a> them both. Double Bonk! <br /> <br /> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMiFzXsANz8MBEyHdvD0G3DBENQ-x1Eag5DVsDaRXNka_GFItsd583_AZVTtHOcKshwfCFHYyFGhlHUi3wGi3s20RHbbENXZEusP_qv6JlF9radr4J0uZ6hDldekFsks6aksopJdYZOx8/s1600/doublebonk.jpg" imageanchor="1" ><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjMiFzXsANz8MBEyHdvD0G3DBENQ-x1Eag5DVsDaRXNka_GFItsd583_AZVTtHOcKshwfCFHYyFGhlHUi3wGi3s20RHbbENXZEusP_qv6JlF9radr4J0uZ6hDldekFsks6aksopJdYZOx8/s640/doublebonk.jpg" /></a>Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com0tag:blogger.com,1999:blog-1827864699624613935.post-1386877175698362572013-07-23T22:35:00.000-04:002013-07-24T11:51:22.883-04:00Introducing virtualenvwrapper-djangoI recently took one of my previous posts entitled <a href="http://jbisbee.blogspot.com/2013/07/never-have-to-type-python-managepy-with.html" target="_blank">"Never Have to Type "python manage.py" with Django Again"</a>, and combined it with another bash function to auto set DJANGO_MODULE_SETTINGS by interrogating a Django project's directory. I then rolled everything together into github as<br /> <br /> <div style="text-align: center;"> <a href="https://github.com/jbisbee/virtualenvwrapper-django"><span style="font-family: inherit; font-size: large;">virtualenvwrapper-django</span></a> </div> <br /> Its a little rough I admit, and I welcome any feedback for improvements. &nbsp;As a result, I'm now able to run commands like the following anywhere once I have my virtual environment loaded.<br /> <br /> <div class="highlight"> <pre><span class="gp">(spock)jbisbee@tacquito:~/src/spock$</span> manage dbshell <span class="go">Welcome to the MySQL monitor. Commands end with ; or \g.</span> <span class="go">Your MySQL connection id is 95</span> <span class="go">Server version: 5.5.31-0ubuntu0.12.04.2 (Ubuntu)</span> <span class="go">Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.</span> <span class="go">Oracle is a registered trademark of Oracle Corporation and/or its</span> <span class="go">affiliates. Other names may be trademarks of their respective</span> <span class="go">owners.</span> <span class="go">Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.</span> <span class="go">mysql&gt;</span> </pre> </div> <br /> <b>Edit:</b> I've gotten some feedback and I realize I didn't sell the real reason behind dynamically setting the DJANGO_SETTINGS_MODULE variable. I use Django at work and we have moved from the generic one file settings.py to a settings directory which looks something like this <br /> <br /> <div class="highlight"><pre><span class="go"> settings/ base.py dev.py # imports from base.py test.py # imports from base.py production.py # imports from base.py jbisbee.py # imports from dev.py </span></pre></div> <br /> This way you can have cascading Django settings with overrides at multiple levels. This bash function I wrote finds the settings directory and will set DJANGO_SETTINGS_MODULE to the user's specific dev settings (or dev if the user can't be found) <br /> <br /> <div class="highlight"><pre><span class="nv">DJANGO_SETTINGS_MODULE</span><span class="o">=</span>projectname.settings.jbisbee </pre></div> </br> This way in jbisbee.py I can override my database, turn on crazy debugging, or do anything else I want and be able to commit those settings without affecting others. <br /> <br /> <i><b>Disclaimer:</b> If you manage your Django project's settings differently let me know. I'd love to make the determine_django_module_settings more flexible and patches are more than welcome!</i>Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com1tag:blogger.com,1999:blog-1827864699624613935.post-34038159174458005552013-07-13T21:06:00.001-04:002013-07-14T17:29:33.372-04:00Django's DateTimeField auto_now* Option CaveatsI'm new to Python and Django and had a run in with DateTimeField's auto_now and auto_now_new. I'm importing data from an existing database and there currently is a column called upd_stamp which I've renamed to modified. Looking through the documentation, I was happy that I could just add auto_now to the options to get the behavior I wanted without any additional code. Well it didn't and I'll explain why as well as the solution I came up with (thanks to a bit of Goggling and Stack Overflow). <br /> <br /> <div class="highlight"> <pre><span class="n">created</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">auto_now_add</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="n">modified</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">auto_now</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> </pre> </div> <br /> The problem I ran into was that I wanted to keep the legacy timestamp from the old database when I loaded the record for the first time then have the auto_now kick in. The problem is that auto_now is very stupid (as it should be) and doesn't understand the concept of insert or update and whether your new record is coming in with pre-populated DateTimeField attribute. It gladly throws away any value away and always replaces the value with the current time. I didn't notice it on first import but soon realized auto_now wouldn't meet my needs. <br /> <br /> After a quick search, I found <a href="http://stackoverflow.com/questions/1737017/django-auto-now-and-auto-now-add">this Stack Overflow</a> question that had the perfect fix for my problem. It suggested implementing my own save that would allow me to pass in values for created or modified but would fall back to the current time. I changed the code a bit because I didn't need a created timestamp for my model, but this should give you an idea. <br /> <br /> <i>Note: you have to remove any additional values from kwargs before passing to the parent class or super.save will complain that created and modified aren't valid arguments.</i> <br /> <br /> <div class="highlight"><pre><span class="kn">import</span> <span class="nn">datetime</span> <span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span> <span class="n">created</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">editable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span> <span class="n">modified</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">()</span> <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="c"># original Code: called datetime.datetime.today()</span> <span class="c"># multiple places. Modified kwargs</span> <span class="c">#</span> <span class="c"># created = kwargs.pop(&#39;created&#39;, None)</span> <span class="c"># if created:</span> <span class="c"># self.created = created</span> <span class="c"># elif not self.id:</span> <span class="c"># self.created = datetime.datetime.today()</span> <span class="c"># </span> <span class="c"># modified = kwargs.pop(&#39;modified&#39;, None)</span> <span class="c"># if modified:</span> <span class="c"># self.modified = modified</span> <span class="c"># else:</span> <span class="c"># self.modified = datetime.datetime.today()</span> <span class="c"># Edit #1: Changed example to more efficient </span> <span class="c"># version from blog comments.</span> <span class="c"># single today()</span> <span class="c"># </span> <span class="c"># today = datetime.datetime.today()</span> <span class="c">#</span> <span class="c"># if not self.id</span> <span class="c"># self.created = kwargs.pop(&#39;created&#39;, today)</span> <span class="c"># elif kwargs.get(&#39;created&#39;, None)</span> <span class="c"># self.created = kwargs.pop(&#39;created&#39;)</span> <span class="c">#</span> <span class="c"># self.modified = kwargs.pop(&#39;modified&#39;, today)</span> <span class="c"># Edit #2: no longer pass arguments via save</span> <span class="c"># was orignially how I wanted to solve</span> <span class="c"># the problem and how I felt auto_now</span> <span class="c"># should have worked. Thanks to</span> <span class="c"># indosauros from reddit. :)</span> <span class="c">#</span> <span class="c"># On inital creation of the object</span> <span class="c"># only set today if values are not</span> <span class="c"># already populated. auto_now*</span> <span class="c"># wipes them out regardless</span> <span class="n">today</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">today</span><span class="p">()</span> <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">:</span> <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">created</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">created</span> <span class="o">=</span> <span class="n">today</span> <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">modified</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">modified</span> <span class="o">=</span> <span class="n">today</span> <span class="k">else</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">modified</span> <span class="o">=</span> <span class="n">today</span> <span class="nb">super</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> </pre></div> <br /> If you see any glaring python or style related issues, please let me know. I'm still finding my way pythonwise and would love any best practices feedback.Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com4tag:blogger.com,1999:blog-1827864699624613935.post-91382612851397523862013-07-08T18:03:00.000-04:002013-07-14T09:00:19.445-04:00Set UTF-8 as the Default Mysql EncodingRan into a small issue today with Mysql and encodings. &nbsp;I'm running Ubuntu 12.04 and the default mysql database encoding is lantin1_sweedish_ci. To change the default to utf8, add the following to your my.cnf file<br /> <br /> <i><b>Edit:</b> Turns out utf8 and utf8_general_ci is not the full utf8 implmentation in mysql. See <a href="http://mathiasbynens.be/notes/mysql-utf8mb4">this blog</a> post for an in-depth explaination on how they're different (has to do with 4 byte utf8 characters being thrown out by mysql)</i> <br /> <br /> <i><b>Edit #2:</b> From reddit, <a href="http://www.reddit.com/user/credomane">credomane</a> stated that character-set-handshake caused him troubles. With existing applications. I'm going to do a bit more research, but here is his response to sharing this blog entry on r/mysql</i> <blockquote> You might want to consider removing <b>character-set-client-handshake</b> or at least mention it could cause troubles with some programs that use mysql. Changing it to false or skipping the handshake makes MySQL behave like MySQL 4.0 according to the mysql docs. When I was looking over your blog post yesterday and checking the mysql docs that seemed like a bad idea to deny the client the ability to change to a character set it prefers/designed for. <br /> <br /> <b>Story time:</b><br /> I went through the whole process of taking mysql offline, backing up my databases, altering/converting the tables, updating the mysql config and bringing mysql back online. At first everything seemed fine then I started to notice a lot of 500 errors appearing in my apache2 access logs and some things not working. Mainly applications sitting behind apache2's mod_wsgi. After spending several hours restoring databases and redoing everything database by database I eventually found I could convert all my databases just fine. Know I know the problem isn't in the database conversions but with my config changes. <br /> <pre> character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci</pre> Caused no problems what so ever but as soon as I added <pre> character-set-client-handshake = FALSE</pre> I started having problems again. Investigating the problem applications show that they desire utf8_unicode_ci but since the mysql server says utf8mb4_unicode_ci only, they abort the mysql connection when they can't get the charset they wanted to prevent any possible corruption. </blockquote> <div class="highlight"> <pre><span class="k">[mysqld]</span> <s><span class="na">collation-server</span> <span class="o">=</span> <span class="s">utf8_general_ci</span></s> <s><span class="na">init-connect</span><span class="o">=</span><span class="s">'SET NAMES utf8'</span></s> <s><span class="na">character-set-server</span> <span class="o">=</span> <span class="s">utf8</span></s> <span class="na">character-set-client-handshake</span> <span class="o">=</span> <span class="s">FALSE</span> <span class="na">character-set-server</span> <span class="o">=</span> <span class="s">utf8mb4</span> <span class="na">collation-server</span> <span class="o">=</span> <span class="s">utf8mb4_unicode_ci</span> </pre> </div> <br /> <s>Great <a href="http://stackoverflow.com/questions/3513773/change-mysql-default-character-set-to-utf8-in-my-cnf">Stack Overflow post</a> on the topic. Be sure to scroll down for Mysql 5.5 and above</s> <br /> <br /> Mysql reference: <a href="http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_character_set_server">character_set_server</a>, <a href="http://dev.mysql.com/doc/refman/5.5/en/server-options.html#option_mysqld_character-set-client-handshake">character-set-client-handshake</a>, <a href="http://dev.mysql.com/doc/refman/5.5/en/server-options.html#option_mysqld_collation-server">collation-server</a>, <s><a href="http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_init_connect">init-connect</a></s> <br /> <br /> <i>Special thanks to <a href="http://www.reddit.com/user/johns-appendix">johns-appendix</a> from my <a href="http://www.blogger.com/set_utf8_as_the_default_mysql_encoding_not_as">reddit post to r/programming</a> for pointing out that I had the wrong encoding. I welcome the down votes as I learned something. :)</i>Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com0tag:blogger.com,1999:blog-1827864699624613935.post-16592294991027304332013-07-04T11:08:00.003-04:002013-07-05T09:15:45.843-04:00The Nest Thermostat Helped Me Save $360 So Far and Paid for Itself After 5 Months<div class="separator" style="clear: both; text-align: center;"> <a href="http://www.amazon.com/gp/product/B009GDHYPQ/ref=as_li_ss_tl%20ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B009GDHYPQ&amp;linkCode=as2&amp;tag=jbisbee-20"><img border="0" src="http://2.bp.blogspot.com/-k1Tbr9GnGK0/UdVi6U7DOCI/AAAAAAAACqo/HJhybUL4qtA/s550/nest-thermostat1.jpg" /></a></div> <img alt="" border="0" height="1" src="http://ir-na.amazon-adsystem.com/e/ir?t=jbisbee-20&amp;l=as2&amp;o=1&amp;a=B009GDHYPQ" style="border: none !important; margin: 0px !important;" width="1" /><br /> About 8 months ago I went to <a href="http://www.amazon.com/?_encoding=UTF8&amp;camp=1789&amp;creative=390957&amp;linkCode=ur2&amp;tag=jbisbee-20" target="_blank">Amazon.com</a><img alt="" border="0" height="1" src="https://ir-na.amazon-adsystem.com/e/ir?t=jbisbee-20&amp;l=ur2&amp;o=1" style="border: none !important; margin: 0px !important;" width="1" /> to rent a streaming movie and saw an ad for the <a href="http://www.amazon.com/gp/product/B009GDHYPQ/ref=as_li_ss_tl%20ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B009GDHYPQ&amp;linkCode=as2&amp;tag=jbisbee-20">The Nest Learning Thermostat</a>, saying it was one of the hottest selling items on Amazon. I was a bit skeptical because I figured a thermostat is a thermostat right, and asked myself, "why is this thing so popular?" <br /> <br /> Well it turns out that the magic is in the merging of technologies. The <a href="http://www.amazon.com/gp/product/B009GDHYPQ/ref=as_li_ss_tl%20ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B009GDHYPQ&amp;linkCode=as2&amp;tag=jbisbee-20">Nest</a> combines <br /> <ul> <li>A Thermostat</li> <li>A motion sensor</li> <li>Wi-Fi</li> <li>Data from the internet </li> <li>Great interface (like the interface on an iPod) </li> </ul> <br /> The real magic is in the motion sensor. The biggest problem with optimizing a thermostat is you have to sit down and program the schedule you want it to follow. The <a href="http://www.amazon.com/gp/product/B009GDHYPQ/ref=as_li_ss_tl%20ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B009GDHYPQ&amp;linkCode=as2&amp;tag=jbisbee-20">Nest</a> takes care of that for you. About a week after I installed it, I was walking down my hallway and the <a href="http://www.amazon.com/gp/product/B009GDHYPQ/ref=as_li_ss_tl%20ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B009GDHYPQ&amp;linkCode=as2&amp;tag=jbisbee-20">Nest</a> lit up and stated it had, "learned my schedule" and would optimize my cooling. I was a bit taken aback but it makes perfect sense. The best part is that the default behavior is after it goes into "away" mode it doesn't turn back on until it senses motion again. <br /> <br /> The <a href="http://www.amazon.com/gp/product/B009GDHYPQ/ref=as_li_ss_tl%20ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B009GDHYPQ&amp;linkCode=as2&amp;tag=jbisbee-20">Nest</a> also does something else very subtly, it motivates you slowly turn up your target temperature to earn a "green leaf". I live in Florida and used to run my thermostat at a steady 77 degrees. Now with the <a href="http://www.amazon.com/gp/product/B009GDHYPQ/ref=as_li_ss_tl%20ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B009GDHYPQ&amp;linkCode=as2&amp;tag=jbisbee-20">Nest</a>, I've bumped that up to 80 and supplemented with fans and surprisingly haven't noticed much of a difference in temperature. What I have noticed is a drastic change in my power bill. <br /> <div class="separator" style="clear: both; text-align: center;"> <script src="//ajax.googleapis.com/ajax/static/modules/gviz/1.0/chart.js" type="text/javascript"> {"dataSourceUrl":"//docs.google.com/spreadsheet/tq?key=0AgK5K_8u4M0RdEhSa3ljREFxVlRxSnJ5bnpodmhWR3c&transpose=1&headers=1&range=A1%3AI3&gid=0&pub=1","options":{"vAxes":[{"useFormatFromData":true,"minValue":null,"logScale":false,"viewWindow":{"min":null,"max":null},"maxValue":null},{"useFormatFromData":true,"minValue":null,"logScale":false,"viewWindow":{"min":null,"max":null},"maxValue":null}],"titleTextStyle":{"bold":true,"color":"#000","fontSize":16},"series":{"0":{"color":"#3366cc"},"1":{"color":"#dc3912"}},"booleanRole":"certainty","title":"Monthly Power Bill Savings","animation":{"duration":500},"backgroundColor":{"fill":"#ffffff"},"legend":"in","hAxis":{"useFormatFromData":true,"minValue":null,"viewWindowMode":null,"viewWindow":null,"maxValue":null},"tooltip":{},"isStacked":false,"width":680,"height":500},"state":{},"view":{},"isDefaultVisualization":true,"chartType":"ColumnChart","chartName":"Chart 2"} </script> </div> I took my power bills from FP&amp;L (power company in Florida) from the last 8 months and plotted the kilowatts per day usage and amazingly it has dropped <b>over 30%</b>. To put it in perspective, <a href="http://www.amazon.com/gp/product/B009GDHYPQ/ref=as_li_ss_tl%20ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B009GDHYPQ&amp;linkCode=as2&amp;tag=jbisbee-20"><b>I've saved over $360 so far</b></a> from December 2012 through July 2013 and <a href="http://www.amazon.com/gp/product/B009GDHYPQ/ref=as_li_ss_tl%20ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B009GDHYPQ&amp;linkCode=as2&amp;tag=jbisbee-20">Nest</a> paid for itself after just 5 months. Best of all it was extremely simple to setup and install. It comes down to just plugging in just few wires and they have excellent phone support that can help walk you through the process step by step. <br /> <br /> <i><b>Disclaimer</b>: I work during the day and have no one at home that would set off the motion sensor to turn the air back on. If you do have someone at home all day or you're one of the awesome people who actually program their thermostat, then most likely you won't see savings as large as mine. Also I live in south Florida were air conditioning is king and we only run the heat one or two times a year :)</i> <br /> <br /> <br />Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com6tag:blogger.com,1999:blog-1827864699624613935.post-50808520095534045062013-07-03T01:55:00.000-04:002013-07-23T23:10:39.865-04:00Never Have to Type "python manage.py" with Django AgainI got very quickly got tired making sure I was in the right directory and typing python manage.py. I found <a href="https://github.com/jgorset/django-shortcuts">django-shortcuts</a> but didn't like the fact that it only mapped a handful of the commands and I wanted access to everything manage.py could do. <p> I then briefly considered creating a bash function that would determine manage.py's location on every invocation but figured that was silly and decided I might as leverage virtualenv's virtual environment and as Ron Popiel says, "<a href="http://www.youtube.com/watch?v=tLq27iOW0R0">You set it and forget it!</a>" <p> It only took a minute of poking to find the postactivate hook file within my ~/.virtualenvs directory. Below is the fruit of my labor. <p> <div class="highlight"> <pre><span class="c">#!/bin/bash</span> <span class="c"># ~/.virtualenvs/postactive</span> <span class="c"># This hook is run after every virtualenv is activated.</span> <span class="nv">VIRTUAL_ENV_NAME</span><span class="o">=</span><span class="k">$(</span>basename <span class="nv">$VIRTUAL_ENV</span><span class="k">)</span> <span class="nv">SRC_DIR</span><span class="o">=</span><span class="s2">"$HOME/src"</span> <span class="k">if</span> <span class="o">[</span> -d <span class="s2">"$SRC_DIR/$VIRTUAL_ENV_NAME"</span> <span class="o">]</span> <span class="k">then</span> <span class="k"> </span><span class="nv">MANAGE_PY</span><span class="o">=</span><span class="k">$(</span>find <span class="s2">"$SRC_DIR/$VIRTUAL_ENV_NAME"</span> -name <span class="s2">"manage.py"</span> -type f<span class="k">)</span> <span class="k">if</span> <span class="o">[</span> -e <span class="s2">"$MANAGE_PY"</span> <span class="o">]</span> <span class="k">then</span> <span class="k"> </span><span class="nb">alias </span><span class="nv">django</span><span class="o">=</span><span class="s2">"python $MANAGE_PY"</span> <span class="k">else</span> <span class="k"> </span><span class="nb">unalias </span>django <span class="k">fi</span> <span class="k">else</span> <span class="k"> </span><span class="nb">unalias </span>django <span class="k">fi</span> </pre> </div> <p> The end result is that I have a new django alias that will work anywhere and act as though I typed python manage.py <div class="highlight"><pre>jbisbee@beni:~$ workon django-tutorial (django-tutorial)jbisbee@beni:~$ alias | grep django alias django='python /Users/jbisbee/src/django-tutorial/mysite/manage.py' (django-tutorial)jbisbee@beni:~$</pre></div> <p> <i><b>Disclaimer</b>: I'm making a big assumption here that the virtualenv name you're using is identical to the name of the project you're working on. It's a pretty big assumption so I apologize if it does't work right out of the box for you.</i>Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com1tag:blogger.com,1999:blog-1827864699624613935.post-52723749064730092112013-07-02T23:33:00.001-04:002013-07-04T17:50:48.975-04:00Team Fortress 2 High Five TauntSo the latest thing in the office is to do the TF2 high five taunt while saying, "Aye!" They key is to do the cheesy demoman smile while you do it. You have to sell it by remaining motionless for a couple of seconds. If you get a high five, do the random taunt of your choice. <br /> <br /> <a class="g-profile" href="http://plus.google.com/106015545959432183027" target="_blank">+Marlon Bailey</a>&nbsp;, looking at you buddy! <br /> <br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-D8zH4qWnJJNwAEAkUSM6T9Y4X0m9dvcbtJu99S_kskK8Kd0t_qxstGdDlHBlX11hDF6Srm8tQcWUV5bRNLVL4k6T0B7UYhduSgpaoy5qEgML-FWZiYSxBw9rI4FAtCRpudFR9rMywfQ/s1600/440_screenshots_2013-06-29_00001.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-D8zH4qWnJJNwAEAkUSM6T9Y4X0m9dvcbtJu99S_kskK8Kd0t_qxstGdDlHBlX11hDF6Srm8tQcWUV5bRNLVL4k6T0B7UYhduSgpaoy5qEgML-FWZiYSxBw9rI4FAtCRpudFR9rMywfQ/s640/440_screenshots_2013-06-29_00001.jpg" /></a></div> <br />Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com1tag:blogger.com,1999:blog-1827864699624613935.post-5764642909881830352013-07-02T10:32:00.001-04:002013-07-26T09:29:45.836-04:00Proper Way to Start Programming Python So after quite a few missteps I've fallen into the correct pattern for programming python. There are two main tools for installing python modules: <a href="https://pypi.python.org/pypi/pip">pip</a> and <a href="https://pypi.python.org/pypi/setuptools">setuptools</a>' easy_install script. On a virgin system you'll want to the following packages from the OS distribution. I've repeated this process on my macbook air, windows machine under <a href="http://www.cygwin.com/">cgywin</a>, and my Ubuntu virtual machine. <p> <h3>1. Install python and python-setuptools</h3> <p> You'll need to install python and python-setuptools before you begin. Macs come with these packages pre-installed so you can skip this step if you'd like. <div class="highlight"><pre> python python-setuptools </pre></div> <p> <h3>2. easy_install virtualenvwrapper</h3> <p> Then you'll want to install virtualenvwrapper via setuptool's easy_install. It should be the <b>only</b> package you should ever install with easy_install. <div class="highlight"><pre> jbisbee@benny:~$ easy_install virtualenvwrapper Searching for virtualenvwrapper Reading http://pypi.python.org/simple/virtualenvwrapper/ Best match: virtualenvwrapper 4.0 ... Finished processing dependencies for virtualenvwrapper </pre></div> <p> <h3>3. Find locaiton of virtualenvwrapper.sh</h3> <p> Once you have virtualenvwrapper installed you'll want to see where it was installed in your path. Please note that it is a shell script and does end with the .sh file extension. <div class="highlight"><pre> jbisbee@benny:~$ which virtualenvwrapper.sh /usr/bin/virtualenvwrapper.sh </pre></div> <p> <h3>4. Add virtualenvwrapper.sh to Your Shell Startup</h3> <p> <div class="highlight"><pre> <span class="c"># your ~/.bashrc or ~/.profile file</span> export WORKON_HOME=~/.virtualenvs <span class="c"># virtualenvwrapper.sh may be installed in </span> <span class="c"># /usr/local/bin on your system</span> source /usr/bin/virtualenvwrapper.sh </pre></div> <p> <h3>5. Reload Your Shell</h3> <p> <div class="highlight"><pre> jbisbee@benny:~$ source ~/.bashrc <span class="c"># source .profile</span> </pre></div> <p> <h3>6. Create a New virtualenv Environment</h3> <p> Now you're in business to start creating virtual workspaces and installing libraries within them with pip. Using django as an example :) <div class="highlight"><pre> jbisbee@benny:~$ mkvirtualenv tutorial New python executable in tutorial/bin/python2.7 Also creating executable in tutorial/bin/python Installing setuptools............done. Installing pip...............done. (tutorial)jbisbee@benny:~$ </pre></div> <p> <h3>7. Install Python Modules with pip</h3> <p> Now install python modules with pip within your virtualenv sandbox: <div class="highlight"><pre> (tutorial)jbisbee@benny:~$ pip install django Downloading/unpacking django Downloading Django-1.5.1.tar.gz (8.0MB): 8.0MB downloaded Running setup.py egg_info for package django ... Successfully installed django Cleaning up... (tutorial)jbisbee@benny:~$ </pre></div> <p> Have fun and let me know if you run into any issues as I wrote this post pretty quickly and may have left something out. <p> <i><b>Edit:</b> This blog post has been <a href="http://apokalyptica79.wordpress.com/2013/07/24/la-forma-correcta-de-iniciar-la-programacion-en-python/">translated to Spanish</a> by <a href="https://plus.google.com/u/0/101971011247553678786/">+Apokalyptica Painkiller</a>. Thank you!</i>Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com1tag:blogger.com,1999:blog-1827864699624613935.post-44526941628111609792013-07-02T04:15:00.000-04:002013-07-03T00:11:50.723-04:00Add History and Tab Completion to the Default Python ShellSo I've in the python shell quite a bit lately and figured it would be nice to be able to have readline history between sessions as well as tab completion. Yes I know things like <a href="http://ipython.org/">iPython</a> exist as well as others but wanted to keep it simple. It turns out its not that hard to enable and here is how you do it. <p> First you'll need to add a line to your .bashrc or .profile setting your PYTHONSTARTUP environment variable <p> <div class="highlight"><pre><span class="c"># ~/.bashrc</span> <span class="nb">export </span><span class="nv">PYTHONSTARTUP</span><span class="o">=</span><span class="nv">$HOME</span>/.pythonstartup.py </pre></div> <p> Then copy this scripts contents to file your new environment variable points to <p> <div class="highlight"><pre><span class="c"># ~/.pythonstartup.py</span> <span class="k">try</span><span class="p">:</span> <span class="kn">import</span> <span class="nn">readline</span> <span class="kn">import</span> <span class="nn">rlcompleter</span> <span class="kn">import</span> <span class="nn">atexit</span> <span class="kn">import</span> <span class="nn">os</span> <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span> <span class="k">print</span> <span class="s">&quot;Python shell enhancement modules not available.&quot;</span> <span class="k">else</span><span class="p">:</span> <span class="n">histfile</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">&quot;HOME&quot;</span><span class="p">],</span> <span class="s">&quot;.pythonhistory&quot;</span><span class="p">)</span> <span class="kn">import</span> <span class="nn">rlcompleter</span> <span class="n">readline</span><span class="o">.</span><span class="n">parse_and_bind</span><span class="p">(</span><span class="s">&quot;tab: complete&quot;</span><span class="p">)</span> <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">histfile</span><span class="p">):</span> <span class="n">readline</span><span class="o">.</span><span class="n">read_history_file</span><span class="p">(</span><span class="n">histfile</span><span class="p">)</span> <span class="n">atexit</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">readline</span><span class="o">.</span><span class="n">write_history_file</span><span class="p">,</span> <span class="n">histfile</span><span class="p">)</span> <span class="k">del</span> <span class="n">os</span><span class="p">,</span> <span class="n">histfile</span><span class="p">,</span> <span class="n">readline</span><span class="p">,</span> <span class="n">rlcompleter</span><span class="p">,</span> <span class="n">atexit</span> <span class="k">print</span> <span class="s">&quot;Python shell history and tab completion are enabled.&quot;</span> </pre></div> <p> That's it. Now anytime you pop into the python shell you'll have history from previous sessions as well as tab completion. Enjoy! <p> <b>Edit:</b> If you're not running the latest version of OSX you may need to change the readline.pasre_and_bind line to the following <p> <div class="highlight"><pre><span class="c">#readline.parse_and_bind(&quot;tab: complete&quot;)</span> <span class="n">readline</span><span class="o">.</span><span class="n">parse_and_bind</span><span class="p">(</span><span class="s">&quot;bind ^I rl_complete&quot;</span><span class="p">)</span> </pre></div>Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com9tag:blogger.com,1999:blog-1827864699624613935.post-60773304622118338072013-07-02T03:37:00.001-04:002013-07-05T08:16:43.740-04:00Cache Pip DownloadsWorking with virtualenvwrapper makes python development so clean. And I found you can speed up installing common libraries in your different projects by telling pip to cache the downloads. Easy peasy lemon sqeezey! <br /> <br /> <div class="highlight"> <pre><span class="nb">export </span><span class="nv">PIP_DOWNLOAD_CACHE</span><span class="o">=</span><span class="nv">$HOME</span>/.pip-download-cache </pre> </div> <br /> Now anytime I pip install anything the package gets cached locally so I won't have to fetch it again.<br /> Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com0tag:blogger.com,1999:blog-1827864699624613935.post-2745635757829634292013-07-02T02:43:00.000-04:002013-07-03T16:16:37.066-04:00Stupid MysqlI really really don't like Mysql but it seems like a&nbsp;necessary&nbsp;evil sometimes. &nbsp;There are so many bugs and edge cases but so many people us it that its hard to get away from it. &nbsp;I can never remember the syntax for creating users and giving them access to db's so I'm going to just leave this here for myself... <p> <div class="highlight"><pre><span class="k">CREATE</span> <span class="k">DATABASE</span> <span class="s1">database_name</span><span class="p">;</span> <span class="k">CREATE</span> <span class="k">USER</span> <span class="s1">&#39;who&#39;</span><span class="o">@</span><span class="s1">&#39;localhost&#39;</span> <span class="n">IDENTIFIED</span> <span class="k">BY</span> <span class="s1">&#39;password&#39;</span><span class="p">;</span> <span class="k">GRANT</span> <span class="k">ALL</span> <span class="k">ON</span> <span class="n">database_name</span><span class="p">.</span><span class="o">*</span> <span class="k">TO</span> <span class="s1">&#39;who&#39;</span><span class="o">@</span><span class="s1">&#39;localhost&#39;</span><span class="p">;</span> </pre></div> <p> Now this is really basic stuff and I realize it, but the point is that I hate looking it up. The sad thing is that after writing this blog post I'll probably never forget it. Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com0tag:blogger.com,1999:blog-1827864699624613935.post-11458591803512716402013-07-01T00:11:00.000-04:002013-07-03T12:18:44.229-04:00Here We Go...<div class="separator" style="clear: both; text-align: center;"> <a href="http://3.bp.blogspot.com/-TRvFaw_4G24/UdJnBcDGENI/AAAAAAAACms/Jjs6DWJgQRk/s400/perl_and_python-t.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="224" src="http://3.bp.blogspot.com/-TRvFaw_4G24/UdJnBcDGENI/AAAAAAAACms/Jjs6DWJgQRk/s320/perl_and_python-t.jpg" width="320" /></a></div> <br /> I recently took the job as AlertSite's Architect and I'll be switching gears and beginning to code python. I plan on keeping this blog pretty tech related, I plan on slipping in few life hacks as well has some light <a href="http://www.teamfortress.com/">Team Fortress 2</a> banter as its one of my guilty pleasures. I've tried blogging in the past with minimal success, but for some reason I think this time it's going to stick... We'll see!Anonymoushttp://www.blogger.com/profile/11913488355114009595noreply@blogger.com1