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
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 AppletThis 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.
Tags: Ajax, Applet, Applications, Backup, Becky, Boot, Business, Code, Collaberation, Css, Design, Directory, Gnome, Google, Googlecalendar, Idea, Ideas, Ie6, Mbr, Mediawiki, Paste, Pastebin, Png, Rsync, Share, Snippets, Startup, Syntax, Ubuntu, Web2.0, Webdesign, Wiki, Win32, Windows
Posted by Greg Pinero (Primary Searcher) as Uncategorized at 4:30 AM MST
Comments Off
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 »
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 »
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.
VMware Converter for workstation to virtual pcThis 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.
Meta Tracker - tracker-project.orgNew 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.
Getting IIS to use WSGIIt looks like the guide might work but the amount of work required is INSANE! I’ll have to find a better way.
Tags: Desktop, Editor, Html, Iis, Javascript, Mssql, Null, Python, Sas, Search, Sql, Sqlserver, Toread, Ubuntu, Unicode, Virtualization, Vmware, Win32, Wordpress, Wsgi, Wysiwyg
Posted by Greg Pinero (Primary Searcher) as Uncategorized at 4:30 AM MST
Comments Off