Python源码示例:locale.atoi()

示例1
def numberFromString(self, string):
        # Uses the current system locale, irrespective of language choice.
        # Returns None if the string is not parsable, otherwise an integer or float.
        if platform=='darwin':
            return self.float_formatter.numberFromString_(string)
        else:
            try:
                return locale.atoi(string)
            except:
                try:
                    return locale.atof(string)
                except:
                    return None

    # Returns list of preferred language codes in RFC4646 format i.e. "lang[-script][-region]"
    # Where lang is a lowercase 2 alpha ISO 639-1 or 3 alpha ISO 639-2 code,
    # script is a capitalized 4 alpha ISO 15924 code and region is an uppercase 2 alpha ISO 3166 code 
示例2
def get_values(soup, row_class):
    """get values from a specific summary row based on the row class"""
    locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')  # for number parsing

    values = []

    cells = soup.find('tr', {'class': row_class}).find_all('td')

    for elem in cells[1:]:
        # if there's a child span with class "macro-value", use its value
        # otherwise use the cell text
        span = elem.find('span', {'class': 'macro-value'})
        if span:
            value = span.text
        else:
            value = elem.text

        if value.strip() != '':
            values.append(locale.atoi(value))

    return values 
示例3
def is_number(s):
    try:
        locale.atof(s)
        return True
    except ValueError:
        pass
    # put string like '3\/32' into numbers
    try:
        special_str = '\/'
        pos = s.find(special_str)
        if pos > 0:
            locale.atoi(s[:pos])
            locale.atoi(s[pos+len(special_str):])
            return True
    except ValueError:
        pass
    return False 
示例4
def get_method(self):
        def custom_int(data):
            data = locale.atoi(data)
            return int(data)

        return custom_int 
示例5
def cast(self, value):
        if self.regex.match(value):
            return int(value)

        try:
            value = float(value)
        except:
            return locale.atoi(value)

        if value.is_integer():
            return int(value)
        else:
            raise ValueError('Invalid integer: %s' % value) 
示例6
def cast(self, value):
        if value in ('', None):
            return None
        try:
            value = float(value)
        except:
            return locale.atoi(value)

        if value.is_integer():
            return int(value)
        else:
            raise ValueError('Invalid integer: %s' % value) 
示例7
def _test_atoi(self, value, out):
        self.assertEqual(locale.atoi(value), out) 
示例8
def convertNumber(data):
    locale.setlocale( locale.LC_ALL, 'en_US.UTF-8')
    num = locale.atoi(data)
    if is_number(num):
        return num
    else:
        msg = '[*] %s is not a number' % (data)
        logging.error(msg)
        return 0 
示例9
def cast(self, value):
        if value in ('', None):
            return None

        try:
            value = float(value)
        except:
            return locale.atoi(value)

        if value.is_integer():
            return int(value)
        else:
            raise ValueError('Invalid integer: %s' % value) 
示例10
def _test_atoi(self, value, out):
        self.assertEqual(locale.atoi(value), out) 
示例11
def CreateFromString(cls, text, consider_locale=True):
        """
        Create a FractionValue from a string.

        :param str text:
            The text with a fraction-value, that is, three integers separated by space and slash
            respectively.
            <value> <value>/<value>

        :param bool consider_locale:
            Consider the locale on converting string to float

        :rtype: FractionValue
        :returns:
            The fraction value

        :raises ValueError:
            If the text is not convertible to a FractionValue
        """
        text = str(text).strip()

        if consider_locale:
            string_to_float_callable = FloatFromString
        else:
            string_to_float_callable = float

        # First try to match only the fractional part.
        m = cls._FRACTION_PARTIAL_RE.match(text)
        if m is not None:
            number = None
        else:
            # If can't match a fraction try a mixed number
            m = cls._FRACTION_RE.match(text)
            if m is None:
                raise ValueError('Please enter a text in the form: "5 3/4"')
            number = m.group("float")

        if number is None:
            number = 0.0
        else:
            number = string_to_float_callable(number)

        if m.group("numerator") is not None and m.group("denominator") is not None:
            numerator = string_to_float_callable(m.group("numerator"))
            denominator = locale.atoi(m.group("denominator"))
            fraction = Fraction(numerator, denominator)
        else:
            fraction = Fraction(0.0, 1.0)

        return FractionValue(number, fraction) 
示例12
def unpickleVars(pdb2pqrID):
    """ Converts instance pickle from PDB2PQR into a dictionary for APBS """
    apbsOptions = {}
    #pfile = open("/home/samir/public_html/pdb2pqr/tmp/%s-input.p" % pdb2pqrID, 'r')
    pfile = open("%s%s%s/%s-input.p" % (INSTALLDIR, TMPDIR, pdb2pqrID, pdb2pqrID), 'r')
    inputObj = pickle.load(pfile)
    pfile.close()
    myElec = inputObj.elecs[0]

    apbsOptions['pqrname'] = pdb2pqrID+'.pqr'
    apbsOptions['pdbID'] = inputObj.pqrname[:-4]
    
    if myElec.cgcent[0:3] == "mol":
        apbsOptions['coarseGridCenterMethod'] = "molecule"
        apbsOptions['coarseGridCenterMoleculeID'] = locale.atoi(myElec.cgcent[4:])
    else:
        apbsOptions['coarseGridCenterMethod'] = "coordinate"
        apbsOptions['coarseGridCenter'] = myElec.cgcent

    if myElec.fgcent[0:3] == "mol":
        apbsOptions['fineGridCenterMethod'] = "molecule"
        apbsOptions['fineGridCenterMoleculeID'] = locale.atoi(myElec.fgcent[4:])
    else:
        apbsOptions['fineGridCenterMethod'] = "coordinate"
        apbsOptions['fineGridCenter'] = myElec.fgcent

    if myElec.gcent[0:3] == "mol":
        apbsOptions['gridCenterMethod'] = "molecule"
        apbsOptions['gridCenterMoleculeID'] = locale.atoi(myElec.gcent[4:])
    else:
        apbsOptions['gridCenterMethod'] = "coordinate"
        apbsOptions['gridCenter'] = myElec.gcent


    if myElec.lpbe == 1:
        apbsOptions['solveType'] = 'linearized'
    elif myElec.npbe == 1:
        apbsOptions['solveType'] = 'nonlinearized'

    if len(myElec.ion) == 0:
        apbsOptions['mobileIonSpecies[0]'] = None
    else:
        apbsOptions['mobileIonSpecies[1]'] = myElec.ion

    if len(myElec.write) <= 1:
        apbsOptions['format'] = 'dx'
    else:
        apbsOptions['format'] = myElec.write[1]

    apbsOptions['calculationType'] = myElec.method
    apbsOptions['dime'] = myElec.dime
    apbsOptions['pdime'] = myElec.pdime
    apbsOptions['async'] = myElec.async