Popular recipes by Antonio Cuni http://code.activestate.com/recipes/users/4169762/2010-05-14T18:35:27-07:00ActiveState Code Recipesstring Formatter that renders None values as empty strings (Python) 2010-05-14T18:35:27-07:00Antonio Cunihttp://code.activestate.com/recipes/users/4169762/http://code.activestate.com/recipes/577227-string-formatter-that-renders-none-values-as-empty/ <p style="color: grey"> Python recipe 577227 by <a href="/recipes/users/4169762/">Antonio Cuni</a> (<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/none/">none</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>This string Formatter works exactly as the default string.Formatter one (i.e., as str.format), with the exception that None values are rendered as empty strings instead of "None". Moreover, any attempt to access attributes or items of None values is rendered as an empty string as well, instead of raising an exception. <br /> E.g. fmt.format('{a}{a.foo}{a[0]}', a=None) == ''</p> <p>This is useful e.g. when filling a template with values that are fetched from a DB, where usually None means empty.</p> Yet another enum for Python (Python) 2009-04-03T12:02:33-07:00Antonio Cunihttp://code.activestate.com/recipes/users/4169762/http://code.activestate.com/recipes/576711-yet-another-enum-for-python/ <p style="color: grey"> Python recipe 576711 by <a href="/recipes/users/4169762/">Antonio Cuni</a> (<a href="/recipes/tags/enum/">enum</a>). </p> <p>There are plenty of ways to declare C-like enums in Python. This one tries to be minimalistic and with a nice syntax, (ab)using decorators.</p> <p>The code is also available here: <a href="http://codespeak.net/svn/user/antocuni/hack/enum.py" rel="nofollow">http://codespeak.net/svn/user/antocuni/hack/enum.py</a></p>