Monday, September 09, 2013

Alternate Fortune Cookie Sayings

I had 'Panda Express' Chinese take-out for lunch, and found the fortune cookes to be too boring. Thus, I've created some of my own, in the hope they liven things up a bit. Feel free to reply with some, if you're inspired to do so.
  • Avoid doing new things, you might get hurt.
  • Your opinions are frequently wrong.
  • Now is not a good time to invest.
  • Your face betrays you.
  • Other people work harder than you do.
  • Your life has no meaning.
  • Your lucky number is Zero.
  • Avoid having opinions, you might be wrong.
  • Do not finish any projects tomorrow.
  • Avoid doing things that require too much thought.
  • Your efforts are doomed to failure.
  • Ask people for help, pleading stupidity.
  • Insult the nationality of all new people.
  • Your smile looks pathetic, only use it when begging.
  • Your kids will always ignore you.
  • Things are always as they seem.
  • Bad news is coming, in large amounts.
  • Fear your loved ones.
  • Sunrises bode not well for your financial future.
  • Steal everything before your friends stop liking you.
  • Wear red on Tuesdays to avoid a dishonorable death.
  • Cancel all your credit cards before it's too late.
  • Your wishes of last Wednesday will never come true.
  • Buy new underwear before they find out.
  • Several former friends are conspiring against you.
  • Never order fries with that.
  • People's clothes indicate if they like you.
  • Someone knows what you did that time.
  • Invest only in companies starting with the letter F.
  • Learn how to cook roadkill safely. Soon.
  • To see far, sleep on your roof.
  • Learn how to chop the head off an attacker.
  • As of yesterday, praying became useless.
  • The Antichrist was born last week.
  • To kill the bugs, wash all your clothes on hot.
  • Your spouse and your best friend have a secret.
  • When you see the flash, duck, it might help.
  • Horseback riding will be useless in 3 years.
  • Farm animals like you until they smell you.
  • Unplug your lamps and toaster before it's too late.
  • A phone call will soon make you cry loudly.
  • Fear everything, it's safer that way.
  • Put your affairs in order before tomorrow night.
  • Give away your posessions, but don't leave a note.
  • Contemplate everything, but don't commit, ever.
  • Physical pain can help you make hard decisions.
  • To change the inside, change the inside.
  • Lock up your women. 
Have any others?

Tuesday, August 13, 2013

Example of how to set JournalCommitInterval in MongoDB

I have found no way yet to find the current value of journalCommitInterval.

But, I have found how to set the journalCommitInterval.

We've got our mongodb set up to use a local disk for our journal, and data resides on a SAN-mounted mount point.

We'd like to set our journalCommitInterval up to a larger value to cut down on the IOPS to our local disk. This might have the added benefit of helping us run faster.

I've run the following command against a mongos, and it complains:

mongos> db.adminCommand({ "setParameter" : 1, "journalCommitInterval": 499});
{ "ok" : 0, "errmsg" : "journaling is off" }
When I run this against a specific daemon, it works up to a value of 499 (setting it to 500 generates a complaint/error):
shard-000:PRIMARY> db.adminCommand({ "setParameter" : 1, "journalCommitInterval": 499});
{ "ok" : 1 }
I have posed the follwoing questions to 10Gen:
  • Question 1: do I need to run this against each mongod individually?
  • Answer 1: Yes.
  • Question 2: does this persist across restarts of the daemon?
  • Answer 2: No. That daemon's journalCommitInterval returns to its default when a daemon are restarted. Thus, it's better to set it in the config file you have defined for the shard. Or, if you define it via command line, do something like:
    /path/to/mongod --journalCommitInterval=499 ...
    
I have just created a minor request in core-server to expose the current value so we can verify it has been set correctly on all shards. If you agree this would be a handy feature to have, please watch and VOTE on the case, https://jira.mongodb.org/browse/SERVER-10508

Thursday, August 08, 2013

Solved: Installing Linux on Acer Aspire V5-122P-0643 Quadcore AMD laptop

So, my wife wanted an Acer Aspire laptop because of its size, primarily. The form factor was just right for her. But, it came with Windows 8, which she really hates (she loves Ubuntu Linux and Libre Office Writer). So, I downloaded Ubuntu Linux 13.04 Raring Ringtail and installed it on this Aspire V5 122P 0643 box. Complications?

1. windows 8 doesn't like to give up control of the BIOS. Going to system settings and boot from USB key (which I'd already put Ubuntu's install image on via another box).

2. Once installed, it wanted to reboot. I did so.

3. VAST multi-day hassles due to black screen after boot, then prompting for user login: instead of going to the graphical user login page familiar to all Ubuntu users. I messed around for a long time trying various things. Running startx failed with a message about no screens. Trying to reboot with some setting for recovery mode got me success once, but I couldn't reproduce it.

The command to show all hw installed revealed the video chipset is AMD Radeon 8280. This appears to be the only laptop, or in fact any device whatsoever, that uses this chipset, though Ubuntu seems to think it's on some desktops somewhere (in their compatibility pages).

4. FINALLY, solved the problem. On my other laptop, navigated to find the download for AMD's proprietary driver, and found the download destination.

Logged into Ubuntu using the user created during installation. Did 'sudo ls' and reentered my password so I had sudo privs without prompting. Then, downloaded the AMD driver from the link on this page: http://support.amd.com/us/gpudownload/linux/Pages/radeon_linux.aspx

the download link was http://www2.ati.com/drivers/linux/amd-driver-installer-catalyst-13-4-linux-x86.x86_64.zip, so I retyped it at the command prompt as:

$ wget http://www2.ati.com/drivers/linux/amd-driver-installer-catalyst-13-4-linux-x86.x86_64.zip

and it downloaded. I

then did: unzip amd* and then did: chmod 755 amd* then agreed to everything and installed it, rebooted per instructions, and voila! IT WORKED!

So, the answer is that the fglrx drivers (automatically installed as part of the amd drivers download, methinks) are the correct solution here.

The touch screen does work , but is of limited usefulness since the icons are so small in a normal linux desktop screen, as they should be. After all , windows 8 sucks at trying to unite the phone with the desktop environment, because they're disparate platforms and everyone except Microsoft seems to know this.

Enjoy!

Friday, July 19, 2013

How to Disable password prompt during ssh login with key failure

How to Disable password prompt during ssh login with key failure

I wanted to login to a bunch of servers and test whether I could get in, or if I got error messages.

After messing around a while, I worked it into:

for n in `cat allhosts.txt`; do echo "host: ${n}"; ssh -oConnectTimeout=2 -oKbdInteractiveAuthentication=no -oPasswordAuthentication=no -oStrictHostKeyChecking=no -oChallengeResponseAuthentication=no myuser@${n} echo '----------'; done

Thursday, June 20, 2013

Question on StackOverflow: Best Unittest for Python programs using OptionParser() ?

Just posted this on stackoverflow. http://stackoverflow.com/questions/17223530/python-how-do-i-usefully-unittest-a-method-invoking-optionparser

MagicMock Returns different values each call

Recent discoveries about mock objects!

I'm trying to create a Python unittest that exercises a class method with a time() call in it.

That is I have this:

class DumObj(object):
def methodOne(self, intime):
while time.time() < intime:
... do stuff ...
so, I refactored this to:

class DumObj(object):
def nowTime(self):
return time.time()
def methodOne(self, intime):
while self.nowTime() < intime:
... do stuff ...
and have to test methodOne. How to get it to go through the loop once (or twice)?

I tried setting the return value to the output of a function:

import unittest
from mock import Mock, MagicMock
class TestDumObj(unittest.TestCase)
def test_methodOne(self):
d = DumObj()
retvals=[1,2,3,4,5]
def mf():
ret = retvals.pop()
return ret
d.nowTime = MagicMock(return_value=mf()) d.methodOne()

Doesn't work. Here's the interactive version:

>>> from mock import MagicMock
>>> retvals = [ 1, 2, 3, 4, 5 ]
>>> def mse(*args, **kwargs):
... ret = retvals.pop()
... return ret
...
>>> mm = MagicMock(return_value=mse())
>>> mm()
5
>>> mm()
5

Damn! I want MagicMock to return different values each time it's called. I want to have an array of return values for MagicMock, and each call it returns the next one. How to do this? Turns out it's using the side_effect param. Continuing from above:

>>> mm = MagicMock(side_effect=mse)
>>> mm()
4
>>> mm()
3
Yay! Solved: MagicMock returns different values each call. Now, tie it into the overall test:

import unittest
from mock import Mock, MagicMock
class TestDumObj(unittest.TestCase)
def test_methodOne(self):
d = DumObj()
retvals=[1,2,3,4,5]
def mse():
ret = retvals.pop()
return ret
d.nowTime = MagicMock(side_effect=mse) d.methodOne()

Works!

Tuesday, May 14, 2013

Drop-Thru Code Considered Harmful

Recently, I was describing my frustrations with some Python code that had been written by a now-departed co-worker.  I describe this code as "Drop-Thru Code".  The main characteristic is that it uses module scope for a non-trivial number of variables.

Module scope is functionally almost-global.  That is, it's so close to global you'll want to use it, but it's far enough away that you'll end up shooting yourself in the foot.

Consider this code snippet:

#!/bin/env python2.6
import os
import sys
varname = 33
vardict = { 'a' : 44 }
# other imports here
print "Starting!"
def something():
print "thing", varname
print "next"
something()
print "end"

This is what I call drop-thru code.  All the variables are global, and the call to something() will fail because varname is out of scope.

Contrast this with what I prefer, well-encapsulated code:

#!/bin/env python2.6
import os
import sys

class SomeThing(object):
    def __init__(self):
        self.varname = 33 
    def something(self):
        print "thing", self.varname
    def main(self):
        self.vardict = { 'a' : 44 }
        # other imports here
        print "Starting!"
        print "next"
        self.something()
        print "end" 

st = SomeThing()
st.main()

Nothing is global, it's obvious what the scope for things is.  Clean, beautiful, easy to understand.

There are lots of examples of scope problems.  I created this one for my team so they understood my frustration:

outside = 1
def function1():
    try:
        print "f1 outside: %s" % (outside)
        outside += 1
    except:
        print "no outside in function1."
print "a outside: %s" % (outside)
function1()                                     
print "b outside: %s" % (outside)
def function2():
    global outside
    try:
        print "f2 outside: %s" % (outside)
        outside += 1
    except:
        print "no outside in function2"
function2()
function2()
class Dum(object):
    def __init__(self):
        print "dum instantiated."
    def main(self):
        print "Dum main outside: %s" % (outside)
    def changeOutside(self, inval):
        outside = inval
        print "Dum changed: outside: %s" % (outside)
    def changeGlobalOutside(self, inval):
        global outside
        outside = inval
        print "Dum changed: outside: %s" % (outside)
d = Dum()
print "c outside: %s" % (outside)
d.main()
print "d outside: %s" % (outside)
d.changeOutside(33)
print "e outside: %s" % (outside)
d.main()
d.changeGlobalOutside(44)
d.main()
print "f outside: %s" % (outside)
______________________________________________________
output:
krice4@zaphod:~/checkouts/userSandboxes/krice4$ python scopeTest.py 
a outside: 1
no outside in function1.
b outside: 1
f2 outside: 1
f2 outside: 2
dum instantiated.
c outside: 3
Dum main outside: 3
d outside: 3
Dum changed: outside: 33
e outside: 3
Dum main outside: 3
Dum changed: outside: 44
Dum main outside: 44
f outside: 44

In short, Drop-Thru Code is considered harmful.  It allows for lots of scope problems that show up as bugs and frustrations later.  So, avoid them.  Put all the vars you can in a class and invoke that class, you'll be glad you did.  IMHO.