Skip to main content.
February 22nd, 2007

Python - Temporarily Disable Printing to Console

I had a function I needed to call that insisted on being very verbose and printing a lot of stuff to the console that I didn’t want to see. But the rest of the script printed beautiful things. So the problem was, how can I disable printing ONLY while I call this function?

Here’s what I came up with. It’s primitive but it seems to work.

#Quick, turn off printing!
class dummyStream:
	''' dummyStream behaves like a stream but does nothing. '''
	def __init__(self): pass
	def write(self,data): pass
	def read(self,data): pass
	def flush(self): pass
	def close(self): pass
# Copy old print deals
old_printerators=[sys.stdout,sys.stderr,sys.stdin,sys.__stdout__,sys.__stderr__,sys.__stdin__][:]
# redirect all print deals
sys.stdout,sys.stderr,sys.stdin,sys.__stdout__,sys.__stderr__,sys.__stdin__=dummyStream(),dummyStream(),dummyStream(),dummyStream(),dummyStream(),dummyStream()

#call your verbose function here
verbose_function()

#Turn printing back on!
sys.stdout,sys.stderr,sys.stdin,sys.__stdout__,sys.__stderr__,sys.__stdin__=old_printerators

I really just repurposed this recipe: “No print error in console-less environments”. It would be smart to put this whole thing into another function such as “call_without_printing”, and just give that any function you want to run silently.

Posted by Greg Pinero (Primary Searcher) as Python at 5:23 PM MST

Comments Off

February 17th, 2007

Quick Searches and Answers

These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but you may still find them useful.

The GNOME Panel Libraries - How to Make a Panel Applet
This looks like the best instruction guide on how to make a panel applet in Gnome. I really want something that will hold text snippets and insert them into whatever program has the focus. Alas this looks like too much work.

pastebin.parentNode.org
A place to share your code with folks on IRC and more.

The easiest way to PNG support in IE6
Easily make PNG images’ transparency work properly in IE 6. How about IE 7 I wonders?

How to regularly backup Windows XP to Ubuntu, using rsync
I may end up trying this.

Google Help Center: How are Google Calendar events included in the public event search?
It looks in public Google calendars and private calendars with public events. It will be neat when it searches the internet for events.

Recover Master Boot Record [win32] [mbr]
Can it be this easy? I’ve always fooled with the Windows XP CD to this and half the time it doesn’t work.

MediaWiki Syntax Help - How to Link to an Image Page without Embedding the Image
To make a link to the image description page without including the actual image, use a link like: [[:Image:an_image.jpg]]. The leading colon prevents the image being embedded in the article, and instead makes an ordinary link.

Go2Web20.net - The complete Web 2.0 directory
All of the web 2.0 websites, there are 100s, only 10 can survive!

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Posted by Greg Pinero (Primary Searcher) as Uncategorized at 4:30 AM MST

Comments Off

February 11th, 2007

Mechanize - Useful Tips

Mechanize is a handy, high level, programmatic web browser type deal for Python. Use it for all of your screen scraping needs. Here are a few tips I had to figure out on my own:

How to set the user agent:

from mechanize import Browser
browser = Browser()
browser.addheaders = [("User-agent", "Mozilla/5.0 (compatible;
MyProgram/0.1)")]
browser.open('http://www.python.org')

How to see the data you’ve fetched:

from mechanize import Browser
browser = Browser()
browser.open('http://www.python.org')
print browser.response().read()

Posted by Greg Pinero (Primary Searcher) as Python at 1:56 AM MST

2 Comments »

February 8th, 2007

Python - Transform None to Empty String (a 1 Liner)

Here you go:
lambda x: ['',x][int(bool(x))]

I got tired of constantly pasting this function in all my code:

def none2blank(val):
    if val:return val
    else:return ''

So I really wanted an expression that would do the same thing. Hopefully I’ve discovered it now. So I can use it like this:
print some_function_that_MUST_have_a_string(['',x][int(bool(x))])

Go enjoy it, or alternatively tell me why it won’t work, or why I shouldn’t need this. All feedback is welcome.

Posted by Greg Pinero (Primary Searcher) as Python at 4:53 PM MST

8 Comments »

Quick Searches and Answers

These are links associated with recent searches I’ve done. They’re not difficult enough to warrant to their own posts but you may still find them useful.

openWYSIWYG - Open Source Cross-Browser WYSIWYG Editor
Will this work better than that stupid one in WordPress now that puts like 100 div tags when I try to center some text! I may end up trying this.

VMware Converter for workstation to virtual pc
This looks promising if you’re in a situation where an application you need is installed on an old computer and you’ve lost the installation media. Just turn the entire OS into a virtual machien and run it on your new computer.

SQL Server : Can the use of NULLS in a database affect performance?
Sounds like it just might affect performance. It also has some interesting advice for dealing with nulls. I wonder is My-SQL also is affected by nulls?

Meta Tracker - tracker-project.org
New desktop search program for Linux, compare to Beagle. I guess “Tracker” is a bad name for a program since I had to spend 5 minutes searching for its homepage.

All About Python and Unicode
Perhaps reading this will finally explain Unicode to me.

Getting IIS to use WSGI
It looks like the guide might work but the amount of work required is INSANE! I’ll have to find a better way.

Tags: , , , , , , , , , , , , , , , , , , , ,

Posted by Greg Pinero (Primary Searcher) as Uncategorized at 4:30 AM MST

Comments Off

« Previous Entries