Archive for January, 2006

Excel – Providing a Default Value for the Lookup Function

Wednesday, January 25th, 2006

The Lookup, Hlookup, and Vlookup functions in Excel are pretty bad. I found that they die with #VALUE, or #N/A if they don’t find the value you’re looking up, and today I found that my lookup function was only looking for the first word in my lookup value!

So here is a better lookup function to use in Excel when you want it to return a specified default value if your value is not found:
(more…)

Python CGI helper function – Convert POST to GET

Sunday, January 22nd, 2006

Why do this?
I made a small Python-based website that outputs results based on user supplied data from a form. The form is submitted using POST but I wanted to let people bookmark or link to their results which requires creating a link which includes all of the data that generated their page.

Here’s how I did it:
Instead of converting the whole site to use GET, I wrote this conversion function that works with Python’s cgi module:

This is the function:

#do these imports somewhere in your module of course.
import cgi
import urllib

def convertpost_to_get(baseurl,postdata):
    """Convert POST data into get data and return the Get url.
    This is useful for creating a "link to this page" or "bookmark this page"
    type of link for your post results.
    """
    if not postdata.keys():
        return baseurl
    query_items=[(key,postdata.getfirst(key)) for key in postdata.keys()]
    baseurl+='?'+urllib.urlencode(query_items,0)
    return baseurl

This is how you would use it:

import cgi
form = cgi.FieldStorage()
url_linkback=convertpost_to_get(
    baseurl=r"http://www.yourURL.com/index.py",postdata=form)
print '<a href="%s">Link to this page!</a>' % url_linkback

This all works by taking advantage of the fact that Python’s cgi module doesn’t make a distinction between data passed via GET or via POST (At least for this purpose). So that means when you script gets this link you made, it will behave the same way it does for post data. Well at least it does for me. If you have more complex POST data then your results may vary and you could have to do some tweaking. Definitely let me know what you did and I’ll update this post.

Disclaimer:
I’m fairly new to Python CGI programming, hence I have no idea what I’m doing, but this does work for me. Caveat emptor ;-)

Update:
I modified this to encode the url parameters with urllib.urlencode. It will now work even if you have amperstands and such in your data.

[tags]Python CGI, GET, POST, HTTP GET,HTTP POST[/tags]

Python – How to Get the Sign of a Number

Sunday, January 22nd, 2006

Update:
Here’s a better way provided by Florent Guillaume:

def sign(number):return cmp(number,0)

Here is my original method:

def sign(number):
    """Will return 1 for positive,
    -1 for negative, and 0 for 0"""
    try:return number/abs(number)
    except ZeroDivisionError:return 0

It’s just a function I’ve been thinking would be included somewhere in Python but haven’t been able to find. It’s simple but _I_ searched for it so enjoy!

Terms I searched for:

  1. python get sign of a number
  2. python “get sign”

What Do Geese Eat?

Thursday, January 19th, 2006

Wikipedia tells us:

These birds feed mainly on plant material. When feeding in water, they submerge their heads and necks to reach aquatic plants, sometimes tipping forward like a dabbling duck. Flocks of these birds often feed on leftover cultivated grains in fields, especially during migration or in winter. They also eat some insects, molluscs and crustaceans.

Why I was curious:
I always see 30-50 of these birds in a field on my way to work every day and they seem to just be pecking in the grass. I can’t fathom what there is for them to eat there. I’d say insects but they appear to be continually eating and there can’t be that many insects. So my best guess so far is that they’re just eating grass. Perhaps I’ll never know.

Do you want to learn more about geese? I highly recommend the wonderful book “Domestic Geese”.

What readers are saying:

This is by far the best and most complete book on the goose. It covers all breeds in depth and the deailed info on hatching geese it the best info to be found anywhere. This book is so complete that you do not even need any others.
K. Wells (cathedral city, CA USA)

I found this book incredibly helpful for background and as a reference as I started raising geese this year. The author gives detailed information on the history of the goose, the different breeds and how to raise them, and most importantly to me, how to incubate and hatch goose eggs. If you are interested in keeping geese on a large or small scale, this book will be helpful.
Padraic MacLeish (NY, USA)

For people that want to know about geese, this is the book to get. It has excellent pictures and the terminolgy used is easy to understand .
Heidi Allison (Australia)

Click Here to buy the wonderful book “Domestic Geese” now

[tags]canada goose,geese,eating grass[/tags]

Word – Page Setup Option is Grayed Out – How to fix

Tuesday, January 17th, 2006

I had this problem today and I fixed it my simply changing the position of that blinking cursor to be over some text.

It looks like if your insert point is over a graphic or other non-text element then page setup becomes grayed out for who knows why.

I found this answer here.

If that doesn’t solve your problem I also read about people having corrupted normal.dot files so you could try these instructions.

My search terms were:

  1. word page setup grayed out
  2. word + “page setup” + (“grayed out” OR “greyed out”) #google and google groups