Skip to content

Instantly share code, notes, and snippets.

@datamafia
Last active August 29, 2015 14:01
Show Gist options
  • Save datamafia/d1a3d1577d570bd0abda to your computer and use it in GitHub Desktop.
Save datamafia/d1a3d1577d570bd0abda to your computer and use it in GitHub Desktop.
To create the perfect command line output tool for Python
#!/usr/bin/env python
# debugging needs
# datamafia.com | @datamafia % (twitter, github)
# remember to import sys
def term(self, s, ref='fallback_models'):
# intended to be used in class, remove self for procedural style
'''
safe print to terminal
@param s : string or whatever to print - required
@param ref : string, optional, used to reference where print occurs
@return void
'''
# obj check aka disqulaify by type. Different in 3.0
if not isinstance(s, basestring) and not isinstance(s, int) \
and not isinstance(s, dict) and not isinstance(s, tuple):
try:
s = s.__name__
except :
pass
try :
s = s.encode('utf-8')
except :
pass
# print
print '['+ref+'] '+str(s)
sys.stdout.flush()
return
'''
Object alias / lamba magic
self.dbg = dbg.dbg() #instantiate
self.d = lambda x,y='api': self.dbg.term(x,y) #make smaller
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment