from-style imports (was Re: [PATCH] Also find correct column width of wide characters)

Matt Mackall mpm at selenic.com
Tue Jan 20 12:13:21 CST 2009


On Tue, 2009-01-20 at 19:47 +0900, Shun-ichi Goto wrote:
> +def colwidth(s):
> +    """Find the column width of string to display."""
> +    global _colwidth
> +    if _colwidth is None:
> +        try:
> +            from unicodedata import east_asian_width as eaw
> +            _colwidth = lambda s: sum([eaw(c) in 'WF' and 2 or 1 for
> c in s])
> +        except:
> +            _colwidth = len
> +    return _colwidth(s.decode(_encoding, "replace"))

A couple notes about imports:

First, remember that we've got a sophisticated demand-loading system for
imports. In general, you should just add the import to the import
section at the top. The import won't happen until some piece of code
actually accesses code or data inside the module.

Second, don't use imports of the style:

from x import y

These defeat the demand-loading system.

-- 
http://selenic.com : development and support for Mercurial and Linux




More information about the Mercurial-devel mailing list