• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python unicodedata.bidirectional函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: check_bidi

def check_bidi(label, check_ltr=False):
    # Bidi rules should only be applied if string contains RTL characters
    bidi_label = False
    for (idx, cp) in enumerate(label, 1):
        direction = unicodedata.bidirectional(cp)
        if direction == '':
            # String likely comes from a newer version of Unicode
            raise IDNABidiError('Unknown directionality in label {0} at position {1}'.format(repr(label), idx))
        if direction in ['R', 'AL', 'AN']:
            bidi_label = True
            break
    if not bidi_label and not check_ltr:
        return True

    # Bidi rule 1
    direction = unicodedata.bidirectional(label[0])
    if direction in ['R', 'AL']:
        rtl = True
    elif direction == 'L':
        rtl = False
    else:
        raise IDNABidiError('First codepoint in label {0} must be directionality L, R or AL'.format(repr(label)))

    valid_ending = False
    number_type = False
    for (idx, cp) in enumerate(label, 1):
        direction = unicodedata.bidirectional(cp)

        if rtl:
            # Bidi rule 2
            if not direction in ['R', 'AL', 'AN', 'EN', 'ES', 'CS', 'ET', 'ON', 'BN', 'NSM']:
                raise IDNABidiError(
                    'Invalid direction for codepoint at position {0} in a right-to-left label'.format(idx))
            # Bidi rule 3
            if direction in ['R', 'AL', 'EN', 'AN']:
                valid_ending = True
            elif direction != 'NSM':
                valid_ending = False
            # Bidi rule 4
            if direction in ['AN', 'EN']:
                if not number_type:
                    number_type = direction
                else:
                    if number_type != direction:
                        raise IDNABidiError('Can not mix numeral types in a right-to-left label')
        else:
            # Bidi rule 5
            if not direction in ['L', 'EN', 'ES', 'CS', 'ET', 'ON', 'BN', 'NSM']:
                raise IDNABidiError(
                    'Invalid direction for codepoint at position {0} in a left-to-right label'.format(idx))
            # Bidi rule 6
            if direction in ['L', 'EN']:
                valid_ending = True
            elif direction != 'NSM':
                valid_ending = False

    if not valid_ending:
        raise IDNABidiError('Label ends with illegal codepoint directionality')

    return True
开发者ID:qwer1234yy,项目名称:SMART,代码行数:60,代码来源:core.py


示例2: _rtl

def _rtl(text, default=False):
    if not text:
        return default
    first_character = text[0]
    if bidirectional(first_character) in ['RLE', 'RLO', 'R', 'AL']:
        return True
    elif bidirectional(first_character) in ['LRE', 'LRO', 'L']:
        return False
    elif len(text)>1:
        return _rtl(text[1:])
    return default
开发者ID:bijanebrahimi,项目名称:boogi,代码行数:11,代码来源:html.py


示例3: lookup

 def lookup(self):
     # look up all the external references we need.
     if self.uniNumber is None:
         return
     try:
         self.uniLetter = unicodeToChar(self.uniNumber)
     except:
         # print("GlyphName value error for %04X" % self.uniNumber)
         return
     if self.uniNumber in mathUniNumbers:
         self.isMath = True
     try:
         self.uniName = unicodelist.get(self.uniNumber)
         if self.uniName is None:
             self.uniNameProcessed = ""
         else:
             self.uniNameProcessed = self.uniName
         # NOTE: this is still a dependency on the unicodedata module.
         # Would be nice to extract this data directly from the unicode data
         # but the algotirhm is ot trivial..
         self.bidiType = unicodedata.bidirectional(self.uniLetter)
     except ValueError:
         self.uniName = None
         self.uniNameProcessed = ""
         self.uniLetter = None
         self.bidiType = None
     except:
         import traceback
         traceback.print_exc()
     self.uniRangeName = getRangeName(self.uniNumber)
开发者ID:LettError,项目名称:glyphNameFormatter,代码行数:30,代码来源:__init__.py


示例4: _character_direction

def _character_direction(ch):
    ch_bidi = bidirectional(ch)
    if ch_bidi in ['L', 'LRE', 'LRO']:
        return 'LTR'
    if ch_bidi in ['R', 'RLE', 'RLO', 'AL']:
        return 'RTL'
    return None
开发者ID:bijanebrahimi,项目名称:boogi,代码行数:7,代码来源:html.py


示例5: find_bidi

    def find_bidi(self, el):
        """Get directionality from element text."""

        for node in self.get_children(el, tags=False):

            # Analyze child text nodes
            if self.is_tag(node):

                # Avoid analyzing certain elements specified in the specification.
                direction = DIR_MAP.get(util.lower(self.get_attribute_by_name(node, 'dir', '')), None)
                if (
                    self.get_tag(node) in ('bdi', 'script', 'style', 'textarea') or
                    direction is not None
                ):
                    continue  # pragma: no cover

                # Check directionality of this node's text
                value = self.find_bidi(node)
                if value is not None:
                    return value

                # Direction could not be determined
                continue  # pragma: no cover

            # Skip `doctype` comments, etc.
            if self.is_special_string(node):
                continue

            # Analyze text nodes for directionality.
            for c in node:
                bidi = unicodedata.bidirectional(c)
                if bidi in ('AL', 'R', 'L'):
                    return ct.SEL_DIR_LTR if bidi == 'L' else ct.SEL_DIR_RTL
        return None
开发者ID:max00xam,项目名称:service.maxxam.teamwatch,代码行数:34,代码来源:css_match.py


示例6: _check_true_dir

    def _check_true_dir(self, text):
        is_rtl = False
        is_ltr = False
        quoted_text = False

        last_inline_html_char_pos = text.rfind(">")
        if last_inline_html_char_pos > -1:
            it_here = text[last_inline_html_char_pos+1:]
        else:
            it_here = text

        for ch in it_here:
            res = UD.bidirectional(ch)
            if ch == '"':
                quoted_text = not quoted_text
            elif not quoted_text and res in {'R', 'AL'}:
                is_rtl = True
            elif not quoted_text and res == 'L':
                is_ltr = True

        #print(text, it_here, is_rtl, is_ltr)

        if is_rtl:
            return 'rtl'
        elif is_ltr:
            return 'ltr'
        else:
            return 'auto'
开发者ID:OSUser,项目名称:mikidown,代码行数:28,代码来源:mdx_autodir.py


示例7: rtlString

def rtlString(source, lang):
    if lang and lang[0:2] in {"ar","he"}:
        line = []
        lineInsertion = 0
        words = []
        rtl = True
        for c in source:
            bidi = unicodedata.bidirectional(c)
            if rtl:
                if bidi == 'L':
                    if words:
                        line.insert(lineInsertion, ''.join(words))
                        words = []
                    rtl = False
                elif bidi in ('R', 'NSM', 'AN'):
                    pass
                else:
                    if words:
                        line.insert(lineInsertion, ''.join(words))
                        words = []
                    line.insert(lineInsertion, c)
                    continue
            else:
                if bidi == 'R' or bidi == 'AN':
                    if words:
                        line.append(''.join(words))
                        words = []
                    rtl = True
            words.append(c)
        if words:
            if rtl:
                line.insert(0, ''.join(words))
        return ''.join(line)
    else:
        return source
开发者ID:Bourne-Law,项目名称:Arelle,代码行数:35,代码来源:Locale.py


示例8: lookup

 def lookup(self):
     # look up all the external references we need.
     if self.uniNumber is None:
         return
     try:
         self.uniLetter = unicodeToChar(self.uniNumber)
     except:
         print("GlyphName valueerror for %04X" % self.uniNumber)
         return
     if self.uniNumber in mathUniNumbers:
         self.isMath = True
     try:
         # self.uniName = unicodedata.name(self.uniLetter)
         self.uniName = unicodelist.get(self.uniNumber)
         if self.uniName is None:
             self.uniNameProcessed = ""
         else:
             self.uniNameProcessed = self.uniName
         self.bidiType = unicodedata.bidirectional(self.uniLetter)
     except ValueError:
         self.uniName = None
         self.uniNameProcessed = ""
         self.uniLetter = None
         self.bidiType = None
     self.uniRangeName = getRangeName(self.uniNumber)
开发者ID:benkiel,项目名称:glyphNameFormatter,代码行数:25,代码来源:__init__.py


示例9: info

    def info(self, char):
        cat = unicodedata.category(char)
        if cat == 'Cn':
            raise UnassignedCharacter

        catname = self.categories[cat]
        bidi = self.bidis[unicodedata.bidirectional(char)]
        name = unicodedata.name(char, 'an unnamed character').decode('ascii')

        if cat[0] == 'C' or cat in ('Zp', 'Zl'):
            example = u''
        elif cat[0] == 'M' and cat[1] != 'c':
            example = u'\N{DOTTED CIRCLE}' + char
        else:
            example = char

        haninfo = u''
        if 'CJK' in name and 'IDEOGRAPH' in name:
            unihan = Unihan(char)
            haninfo = unicode(unihan)
            if haninfo:
                haninfo = u'. ' + haninfo + u'.'

        return {'code': u'%04X' % ord(char),
                'name': name.title().replace('Cjk', 'CJK'), 'char': char,
                'example': example, 'category': catname.lower(), 'bidi': bidi,
                'unihan': haninfo}
开发者ID:gflerm,项目名称:ibid,代码行数:27,代码来源:conversions.py


示例10: print_Unicode_info

def print_Unicode_info(char, short):
    name = unicodedata.name(char, "UNKNOWN")
    decCodepoint = ord(char)
    hexCodepoint = hex(decCodepoint)
    lower = char.lower()
    upper = char.upper()
    category = unicodedata.category(char)
    bidirectional = unicodedata.bidirectional(char)
    mirrored = True if (unicodedata.mirrored(char) == 1) else False
    nfc = unicodedata.normalize("NFC", char)
    nfd = unicodedata.normalize("NFD", char)

    if (short):
        print(char + "\t" + name + " (U+" + str(hexCodepoint).upper().replace("0X", "") + ")")
    else:
        print("Name          " + name)
        print("Character     " + char)
        print("Dec Codepoint " + str(decCodepoint))
        print("Hex Codepoint " + str(hexCodepoint))
        print("Lowercase     " + lower)
        print("Uppercase     " + upper)
        print("Category      " + category)
        print("Bidirectional " + bidirectional)
        print("Mirrored      " + str(mirrored))
        print("NFC           " + nfc)
        print("NFD           " + nfd)
开发者ID:GitBruno,项目名称:glyphIgo,代码行数:26,代码来源:glyphIgo.py


示例11: drawText

def drawText(canvas, x, y, text, en = False, bold = False, size = 12):
    wrkText = text
    isArabic = False
    isBidi = False
    for c in wrkText:
        cat = unicodedata.bidirectional(c)
        if cat == "AL" or cat == "AN" or cat == "FA":
            isArabic = True
            isBidi = True
            break
        elif cat == "R" or cat == "RLE" or cat == "RLO":
            isBidi = True
    if isArabic:
        wrkText = a_forms.fuse(wrkText)
        wrkText = a_process.shape(wrkText)

    if isBidi:
        wrkText = get_display(wrkText)

    if bold:
        canvas.setFont('BNazanin', size)
    else:
        canvas.setFont('Nazanin', size)

    canvas.drawRightString(x, canvas._pagesize[1] - y, wrkText)
开发者ID:hamed-moghimi,项目名称:nikbad,代码行数:25,代码来源:pdf.py


示例12: get_embedding_levels

def get_embedding_levels(text, storage, upper_is_rtl=False, debug=False):
    """Get the paragraph base embedding level and direction,
    set the storage to the array of chars"""

    prev_surrogate = False
    base_level = storage['base_level']
    has_rtl = False
    # preset the storage's chars
    for _ch in text:
        if _IS_UCS2 and (_SURROGATE_MIN <= ord(_ch) <= _SURROGATE_MAX):
            prev_surrogate = _ch
            continue
        elif prev_surrogate:
            _ch = prev_surrogate + _ch
            prev_surrogate = False

        if upper_is_rtl and _ch.isupper():
            bidi_type = 'R'
        else:
            try:
                bidi_type = bidirectional(_ch)
            except:
                bidi_type = None

        has_rtl |= (bidi_type == 'R')

        storage[
            'chars'].append({'ch': _ch, 'level': base_level, 'type': bidi_type,
                             'orig': bidi_type})
    if debug:
        debug_storage(storage, base_info=True)

    return has_rtl
开发者ID:insiderr,项目名称:insiderr-app,代码行数:33,代码来源:algorithm.py


示例13: _truncate_invalid_chars

    def _truncate_invalid_chars(value, length):
        '''Safety check: make sure we aren't truncating within the boundaries
        of a multibyte character. Also, add a LTR BOM if the last character
        is RTL.
        '''
        value = smart_str(value)
        if length:
            value = value[:length]
            valid = False
            while not valid and len(value):
                try:
                    test = value.decode('utf8')

                    # check for RTL encoding without order marker terminator
                    direction = bidirectional(test[-1])
                    if direction in RTL_TYPES:
                        # this is RTL, we need 3 bytes for the BOM
                        if len(value) > (length - 3):
                            # not enough room - keep chopping
                            raise ValueError('Not enough room to truncate')
                        else:
                            test += u'\u200e'  # LTR BOM
                            return smart_str(test)
                    else:
                        valid = True
                        del test
                except (UnicodeDecodeError, ValueError):
                    # chop a letter off the end and try again
                    value = value[:-1]
        return value
开发者ID:alvaromartin,项目名称:baph,代码行数:30,代码来源:models.py


示例14: rtlString

def rtlString(source, lang):
    if lang and source and lang[0:2] in set([u"ar",u"he"]):
        line = []
        lineInsertion = 0
        words = []
        rtl = True
        for c in source:
            bidi = unicodedata.bidirectional(c)
            if rtl:
                if bidi == u'L':
                    if words:
                        line.insert(lineInsertion, u''.join(words))
                        words = []
                    rtl = False
                elif bidi in (u'R', u'NSM', u'AN'):
                    pass
                else:
                    if words:
                        line.insert(lineInsertion, u''.join(words))
                        words = []
                    line.insert(lineInsertion, c)
                    continue
            else:
                if bidi == u'R' or bidi == u'AN':
                    if words:
                        line.append(u''.join(words))
                        words = []
                    rtl = True
            words.append(c)
        if words:
            if rtl:
                line.insert(0, u''.join(words))
        return u''.join(line)
    else:
        return source
开发者ID:sternshus,项目名称:not_arelle2.7,代码行数:35,代码来源:Locale.py


示例15: __init__

 def __init__(self, myData, headerData, comboData):
     QtGui.QMainWindow.__init__(self)
     self.ui = Ui_MainWindow()
     self.ui.setupUi(self)
     self.ui.OKButton.clicked.connect(self.OKClicked)
     self.ui.CancelButton.clicked.connect(self.CancelClicked)
     self.__model = LinkerTable(myData, headerData)
     self.ui.tableView.setModel(self.__model)
     # Prepare the checkbox column
     for row in range(0, self.__model.rowCount(self)):
         self.ui.tableView.openPersistentEditor(self.__model.index(row, 0))
     self.__combo_model = LinkerCombo(comboData)
     self.ui.targetLexCombo.setModel(self.__combo_model)
     self.ret_val = 0
     self.cols = 7
     self.ui.targetLexCombo.currentIndexChanged.connect(self.ComboClicked)
     self.ui.FilterCheckBox.clicked.connect(self.FilterClicked)
     self.ComboClicked()
     
     myHPG = self.__combo_model.getCurrentHPG()
     myHeadword = myHPG.getHeadword()
     # Check for right to left data and set the combobox direction if needed
     for i in range(0, len(myHeadword)):
         if unicodedata.bidirectional(myHeadword[i]) in (u'R', u'AL'):
             self.ui.targetLexCombo.setLayoutDirection(QtCore.Qt.RightToLeft)
             self.__combo_model.setRTL(True)
             break
开发者ID:rmlockwood,项目名称:FLExTrans,代码行数:27,代码来源:LinkSenseTool.py


示例16: get_base_direction

def get_base_direction(text):
    """Find the base direction of a text string according to the first
    character with strong bidi type.

    Returns ``0`` for LTR, ``1`` for RTL and ``-1`` for undefined (no strong
    characters found).
    """
    text = force_unicode(text)

    # Find first character with strong bidi type.
    first = None
    for c in text:
        bidi_type = bidirectional(c)
        if bidi_type in _strong_types:
            first = bidi_type
            break

    if first:
        if first in _rtl_types:
            return 1
        else:
            return 0
    else:
        # Text composed of weak bidi characters.
        return -1
开发者ID:AmesianX,项目名称:pootle,代码行数:25,代码来源:bidi.py


示例17: fix_rtl

def fix_rtl(string):
  # If there are any strongly RTL chars, hint that we're already in an LTR context, and want to be afterwards
  rtl_chars = [c for c in string if unicodedata.bidirectional(c) in ['R', 'AL', 'RLE', 'RLO', 'RLI']]
  if rtl_chars:
    return u'\u200e' + string + u'\u200e'

  return string
开发者ID:londonhackspace,项目名称:hamming-scripts,代码行数:7,代码来源:doorbot-irccat.py


示例18: match_dir

    def match_dir(self, el, directionality):
        """Check directionality."""

        # If we have to match both left and right, we can't match either.
        if directionality & ct.SEL_DIR_LTR and directionality & ct.SEL_DIR_RTL:
            return False

        # Element has defined direction of left to right or right to left
        direction = DIR_MAP.get(util.lower(self.get_attribute_by_name(el, 'dir', '')), None)
        if direction not in (None, 0):
            return direction == directionality

        # Element is the document element (the root) and no direction assigned, assume left to right.
        is_root = self.match_root(el)
        if is_root and direction is None:
            return ct.SEL_DIR_LTR == directionality

        # If `input[type=telephone]` and no direction is assigned, assume left to right.
        name = self.get_tag(el)
        is_input = name == 'input'
        is_textarea = name == 'textarea'
        is_bdi = name == 'bdi'
        itype = util.lower(self.get_attribute_by_name(el, 'type', '')) if is_input else ''
        if is_input and itype == 'tel' and direction is None:
            return ct.SEL_DIR_LTR == directionality

        # Auto handling for text inputs
        if ((is_input and itype in ('text', 'search', 'tel', 'url', 'email')) or is_textarea) and direction == 0:
            if is_textarea:
                value = []
                for node in el.contents:
                    if self.is_content_string(node):
                        value.append(node)
                value = ''.join(value)
            else:
                value = self.get_attribute_by_name(el, 'value', '')
            if value:
                for c in value:
                    bidi = unicodedata.bidirectional(c)
                    if bidi in ('AL', 'R', 'L'):
                        direction = ct.SEL_DIR_LTR if bidi == 'L' else ct.SEL_DIR_RTL
                        return direction == directionality
                # Assume left to right
                return ct.SEL_DIR_LTR == directionality
            elif is_root:
                return ct.SEL_DIR_LTR == directionality
            return self.match_dir(self.get_parent(el), directionality)

        # Auto handling for `bdi` and other non text inputs.
        if (is_bdi and direction is None) or direction == 0:
            direction = self.find_bidi(el)
            if direction is not None:
                return direction == directionality
            elif is_root:
                return ct.SEL_DIR_LTR == directionality
            return self.match_dir(self.get_parent(el), directionality)

        # Match parents direction
        return self.match_dir(self.get_parent(el), directionality)
开发者ID:max00xam,项目名称:service.maxxam.teamwatch,代码行数:59,代码来源:css_match.py


示例19: text_dir

def text_dir(c):
    """Classify a character as 'R'/'L'/''."""
    dir = unicodedata.bidirectional(c)
    if dir in ('L',):
        return 'L'
    if dir in ('R', 'AL'):
        return 'R'
    return ''
开发者ID:amitramon,项目名称:plainMail2HTML,代码行数:8,代码来源:hibidi.py


示例20: ub

 def ub(self,bidi):
     """
     Match a character with a given Unicode bidirectional class
     """
     import unicodedata
     x, e = self.rule_anything()
     if unicodedata.bidirectional(x[0]) is bidi:
         return x, e
     else:
         e[1] = expected("bidi:"+ bidi)
         raise _MaybeParseError(*e)
开发者ID:thepian,项目名称:themaestro,代码行数:11,代码来源:runtime.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python unicodedata.category函数代码示例发布时间:2022-05-27
下一篇:
Python unicodecsv.writer函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap