Unable to encode/decode pprint output

pprint appears to use repr by default, you can work around this by overriding PrettyPrinter.format: # coding=utf8 import pprint class MyPrettyPrinter(pprint.PrettyPrinter): def format(self, object, context, maxlevels, level): if isinstance(object, unicode): return (object.encode(‘utf8’), True, False) return pprint.PrettyPrinter.format(self, object, context, maxlevels, level) d = {‘foo’: u’işüğçö’} pprint.pprint(d) # {‘foo’: u’i\u015f\xfc\u011f\xe7\xf6′} MyPrettyPrinter().pprint(d) # {‘foo’: işüğçö}

Measuring text in WPF

The most low-level technique (and therefore giving the most scope for creative optimisations) is to use GlyphRuns. It’s not very well documented but I wrote up a little example here: http://smellegantcode.wordpress.com/2008/07/03/glyphrun-and-so-forth/ The example works out the length of the string as a necessary step before rendering it.

Format Strings in Console.WriteLine method

It adds padding to the left. Very useful for remembering the various string formatting patterns is the following cheat sheet: .NET String.Format Cheat Sheet Positive values add padding to the left, negative add padding to the right Sample Generates String.Format(“[{0, 10}]”, “Foo”); [∙∙∙∙∙∙∙Foo] String.Format(“[{0, 5}]”, “Foo”); [∙∙Foo] String.Format(“[{0, -5}]”, “Foo”); [Foo∙∙] String.Format(“[{0, -10}]”, “Foo”); [Foo∙∙∙∙∙∙∙]

How to replace numbers in body to Persian numbers?

You can use this method: (http://jsfiddle.net/A4NfG/1/) persian={0:’۰’,1:’۱’,2:’۲’,3:’۳’,4:’۴’,5:’۵’,6:’۶’,7:’۷’,8:’۸’,9:’۹’}; function traverse(el){ if(el.nodeType==3){ var list=el.data.match(/[0-9]/g); if(list!=null && list.length!=0){ for(var i=0;i<list.length;i++) el.data=el.data.replace(list[i],persian[list[i]]); } } for(var i=0;i<el.childNodes.length;i++){ traverse(el.childNodes[i]); } }

Can you format pandas integers for display, like `pd.options.display.float_format` for floats?

You could monkey-patch pandas.io.formats.format.IntArrayFormatter: import contextlib import numpy as np import pandas as pd import pandas.io.formats.format as pf np.random.seed(2015) @contextlib.contextmanager def custom_formatting(): orig_float_format = pd.options.display.float_format orig_int_format = pf.IntArrayFormatter pd.options.display.float_format=”{:0,.2f}”.format class IntArrayFormatter(pf.GenericArrayFormatter): def _format_strings(self): formatter = self.formatter or ‘{:,d}’.format fmt_values = [formatter(x) for x in self.values] return fmt_values pf.IntArrayFormatter = IntArrayFormatter yield pd.options.display.float_format = orig_float_format pf.IntArrayFormatter … Read more

Rails (or Ruby): Yes/No instead of True/False

There isn’t something in Rails. A better way than adding to the true/false classes to achieve something similar would be to make a method in ApplicationHelper: def human_boolean(boolean) boolean ? ‘Yes’ : ‘No’ end Then, in your view <%= human_boolean(boolean_youre_checking) %> Adding methods to built-in classes is generally frowned upon. Plus, this approach fits in … Read more