当前位置: 首页>>代码示例>>Python>>正文


Python unicodedata.east_asian_width函数代码示例

本文整理汇总了Python中unicodedata.east_asian_width函数的典型用法代码示例。如果您正苦于以下问题:Python east_asian_width函数的具体用法?Python east_asian_width怎么用?Python east_asian_width使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了east_asian_width函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

 def __init__(self, text, aspect_ratio=1, textual=False, textual_strict=False, widechars='F,W'):
     """Take a ASCII art figure and store it, prepare for ``recognize``"""
     self.aspect_ratio = float(aspect_ratio)
     self.textual = textual
     self.textual_strict = textual_strict
     # XXX TODO tab expansion
     # detect size of input image, store as list of lines
     self.image = []
     max_x = 0
     y = 0
     # define character widths map
     charwidths = {}
     for key in ['F', 'H', 'W', 'Na', 'A', 'N']:
         if key in widechars.split(','):
             charwidths[key] = 2
         else:
             charwidths[key] = 1
     for line in text.splitlines():
         # extend length by 1 for each wide glyph
         line_len = sum(charwidths[east_asian_width(c)] for c in line)
         max_x = max(max_x, line_len)
         # pad a space for each wide glyph
         padded_line = ''.join(c+' '*(charwidths[east_asian_width(c)]-1) for c in line)
         self.image.append(padded_line)
         y += 1
     self.width = max_x
     self.height = y
     # make sure it's rectangular (extend short lines to max width)
     for y, line in enumerate(self.image):
         if len(line) < max_x:
             self.image[y] = line + ' '*(max_x-len(line))
     # initialize other data structures
     self.classification = [[None]*self.width for y in range(self.height)]
     self.shapes = []
     self.nominal_size = NOMINAL_SIZE
开发者ID:git-pull,项目名称:aafigure,代码行数:35,代码来源:aafigure.py

示例2: strpad

    def strpad(self, s, width):
        if width < 1:
            return str()
        if '\n' in s:
            s = s.replace('\n', '\\n')

        # take into account double-width characters
        buf = str()
        buf_width = 0
        for c in s:
            w = 2 if unicodedata.east_asian_width(c) == 'W' else 1
            if buf_width + w > width:
                break
            buf_width += w
            buf += c

        if len(buf) < len(s):
            # truncation occurred
            while buf_width + len(self.trunc_char) > width:
                c = buf[-1]
                w = 2 if unicodedata.east_asian_width(c) == 'W' else 1
                buf = buf[0:-1]
                buf_width -= w
            buf += ' ' * (width - buf_width - len(self.trunc_char))
            buf += self.trunc_char
        elif buf_width < width:
            # padding required
            buf += ' ' * (width - buf_width)

        return buf
开发者ID:MetaMemoryT,项目名称:OpenTrader,代码行数:30,代码来源:tabview.py

示例3: truncate_str

def truncate_str(s,width): # width = screen width
	wide_count = 0
	for i in range(len(s)):
		wide_count += unicodedata.east_asian_width(s[i]) in ('F','W')
		if wide_count + i >= width:
			return s[:i] + ('',' ')[
				unicodedata.east_asian_width(s[i]) in ('F','W')
				and wide_count + i == width]
	else: # pad the string to width if necessary
		return s + ' '*(width-len(s)-wide_count)
开发者ID:mmgen,项目名称:mmgen,代码行数:10,代码来源:obj.py

示例4: strwidth_ucs_2

def strwidth_ucs_2(width_data, string):
	return sum(((
		(
			width_data[east_asian_width(string[i - 1] + symbol)]
		) if 0xDC00 <= ord(symbol) <= 0xDFFF else (
			0
		) if combining(symbol) or 0xD800 <= ord(symbol) <= 0xDBFF else (
			width_data[east_asian_width(symbol)]
		)
	) for i, symbol in enumerate(string)))
开发者ID:13768324554,项目名称:powerline,代码行数:10,代码来源:unicode.py

示例5: get_text_width

def get_text_width(text):
    num_full_width = 0
    for c in text:
        eaw = unicodedata.east_asian_width(c)
        if eaw in (u'W', u'F', u'A'):
            num_full_width += 1
    return len(text) + num_full_width
开发者ID:qwerqwer1111,项目名称:md-table-formatter,代码行数:7,代码来源:md-table-formatter.py

示例6: main

def main():

    # 默认城市
    city = "深圳"
    # 判断汉字条件
    chz = True
    phz = True

    if len(sys.argv) > 2:
        print("\n请输入正确的命令:oneweathert.py [中国城市名称],目前本程序只支持一个城市的天气询查!")
        print("如:oneweathert.py 北京 , 不带城市名称,默认城市:" + city)
    else:
        if len(sys.argv) == 1:

            print("\n可输入城市名参数:oneweathert.py [中国城市名称/默认" + city + "],如:oneweathert.py 北京")
            winfo(city)

        elif len(sys.argv) == 2:

            # 判断参数是否为汉字,只支持汉字
            for cs in range(0, len(sys.argv[1])):

                if unicodedata.east_asian_width(sys.argv[1][cs]) != "Na":
                    chz = True
                else:
                    chz = False

                phz = chz and phz

            if not phz:
                print("\n请检查城市名称是否正确,本程序只支持中国中文城市名称!")
            else:
                city = sys.argv[1]
                winfo(city)
开发者ID:pocmon,项目名称:oneweather,代码行数:34,代码来源:oneweather.py

示例7: strong_width

def strong_width(s):
    if not s in _strong_width_map:
        w = 0
        for c in s:
            w += 2 if unicodedata.east_asian_width(c) in ("F", "W") else len(unicodedata.normalize('NFC', c))
        _strong_width_map[s] = w
    return _strong_width_map[s]
开发者ID:scyptnex,项目名称:bootstrap,代码行数:7,代码来源:amuse.py

示例8: textwrap

 def textwrap(ustr, width=80, leadingspace=6):
     import unicodedata
     
     if not ustr:
         return []
     
     lines = []
     i = 0
     buf = []
     for c in ustr:
         if unicodedata.east_asian_width(c) != 'Na':
             d = 2
         else:
             d = 1
         if i+d <= width:
             buf.append(c)
             i += d
         else:
             lines.append(''.join(buf))
             buf = [leadingspace*' ' + c]
             i = d + leadingspace
     if buf:
         lines.append(''.join(buf))
     
     return lines
开发者ID:LinYuanLab,项目名称:ulipad,代码行数:25,代码来源:Print.py

示例9: tt_width

def tt_width(s, index=0, ambiguous_as_wide=False):
    
    """Return logical width of the grapheme cluster at `s[index]` on 
    fixed-width typography
    
    Return value will be ``1`` (halfwidth) or ``2`` (fullwidth).
    
    Generally, the width of a grapheme cluster is determined by its leading 
    code point.
    
    >>> tt_width('A')
    1
    >>> tt_width('\\u8240')     # U+8240: CJK UNIFIED IDEOGRAPH-8240
    2
    >>> tt_width('g\\u0308')    # U+0308: COMBINING DIAERESIS
    1
    >>> tt_width('\\U00029e3d') # U+29E3D: CJK UNIFIED IDEOGRAPH-29E3D
    2
    
    If `ambiguous_as_wide` is specified to ``True``, some characters such as 
    greek alphabets are treated as they have fullwidth as well as ideographics 
    does.
    
    >>> tt_width('\\u03b1')     # U+03B1: GREEK SMALL LETTER ALPHA
    1
    >>> tt_width('\\u03b1', ambiguous_as_wide=True)
    2
    """
    cp = code_point(s, index)
    eaw = east_asian_width(cp)
    if eaw in ('W', 'F') or (eaw == 'A' and ambiguous_as_wide):
        return 2
    return 1
开发者ID:wtolson,项目名称:uniseg-python,代码行数:33,代码来源:wrap.py

示例10: width

 def width(c):
     if ((isinstance(c, type(u"")) and
          unicodedata.east_asian_width(c) == 'W')):
         return 2
     elif c == '\r':
         return 2
     return 1
开发者ID:sharad,项目名称:rc,代码行数:7,代码来源:icdiff.py

示例11: charwidth

def charwidth(char):
    # 私の環境では、Full-widthもAmbiguousもNot East Asianも
    # 半角文字として扱われていた
    if unicodedata.east_asian_width(char) == 'W':
        return 2
    else:
        return 1
开发者ID:yoshrc,项目名称:sch,代码行数:7,代码来源:schedule.py

示例12: __new__

	def __new__(cls,s,on_fail='die',msg=None):
		if type(s) == cls: return s
		cls.arg_chk(on_fail)
		for k in cls.forbidden,cls.allowed:
			assert type(k) == list
			for ch in k: assert type(ch) == str and len(ch) == 1
		try:
			s = s.strip()
			if type(s) != str:
				s = s.decode('utf8')
			for ch in s:
				# Allow:    (L)etter,(N)umber,(P)unctuation,(S)ymbol,(Z)space
				# Disallow: (C)ontrol,(M)combining
				# Combining characters create width formatting issues, so disallow them for now
				if unicodedata.category(ch)[0] in 'CM':
					t = { 'C':'control', 'M':'combining' }[unicodedata.category(ch)[0]]
					raise ValueError('{}: {} characters not allowed'.format(ascii(ch),t))
			me = str.__new__(cls,s)
			if cls.max_screen_width:
				me.screen_width = len(s) + len([1 for ch in s if unicodedata.east_asian_width(ch) in ('F','W')])
				assert me.screen_width <= cls.max_screen_width,(
					'too wide (>{} screen width)'.format(cls.max_screen_width))
			else:
				assert len(s) <= cls.max_len, 'too long (>{} symbols)'.format(cls.max_len)
			assert len(s) >= cls.min_len, 'too short (<{} symbols)'.format(cls.min_len)
			assert not cls.allowed or set(list(s)).issubset(set(cls.allowed)),\
				'contains non-allowed symbols: {}'.format(' '.join(set(list(s)) - set(cls.allowed)))
			assert not cls.forbidden or not any(ch in s for ch in cls.forbidden),\
				"contains one of these forbidden symbols: '{}'".format("', '".join(cls.forbidden))
			return me
		except Exception as e:
			return cls.init_fail(e,s)
开发者ID:mmgen,项目名称:mmgen,代码行数:32,代码来源:obj.py

示例13: visible_width

def visible_width(string):
    """Get the visible width of a unicode string.

    Some CJK unicode characters are more than one byte unlike ASCII and latin unicode characters.

    From: https://github.com/Robpol86/terminaltables/pull/9

    :param str string: String to measure.

    :return: String's width.
    :rtype: int
    """
    if '\033' in string:
        string = RE_COLOR_ANSI.sub('', string)

    # Convert to unicode.
    try:
        string = string.decode('u8')
    except (AttributeError, UnicodeEncodeError):
        pass

    width = 0
    for char in string:
        if unicodedata.east_asian_width(char) in ('F', 'W'):
            width += 2
        else:
            width += 1

    return width
开发者ID:liiight,项目名称:terminaltables,代码行数:29,代码来源:width_and_alignment.py

示例14: get_real_width

def get_real_width(str):
    """Gets real width of a string accounting for double width characters."""

    real_width = 0
    for char in str:
        real_width += 2 if east_asian_width(char) == 'W' else 1
    return real_width
开发者ID:enchuu,项目名称:yaytp,代码行数:7,代码来源:format.py

示例15: GetCharacterDisplayWidth

def GetCharacterDisplayWidth(char):
  """Returns the monospaced terminal display width of char.

  Assumptions:
    - monospaced display
    - ambiguous or unknown chars default to width 1
    - ASCII control char width is 1 => don't use this for control chars

  Args:
    char: The character to determine the display width of.

  Returns:
    The monospaced terminal display width of char: either 0, 1, or 2.
  """
  if not isinstance(char, unicode):
    # Non-unicode chars have width 1. Don't use this function on control chars.
    return 1

  # Normalize to avoid special cases.
  char = unicodedata.normalize('NFC', char)

  if unicodedata.combining(char) != 0:
    # Modifies the previous character and does not move the cursor.
    return 0
  elif unicodedata.category(char) == 'Cf':
    # Unprintable formatting char.
    return 0
  elif unicodedata.east_asian_width(char) in 'FW':
    # Fullwidth or Wide chars take 2 character positions.
    return 2
  else:
    # Don't use this function on control chars.
    return 1
开发者ID:flgiordano,项目名称:netcash,代码行数:33,代码来源:console_attr.py


注:本文中的unicodedata.east_asian_width函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。