Python locale.atoi() Examples

The following are 12 code examples of locale.atoi(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module locale , or try the search function .
Example #1
Source File: l10n.py    From EDMarketConnector with GNU General Public License v2.0 6 votes vote down vote up
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 
Example #2
Source File: myfitnesspal.py    From CloudBot with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #3
Source File: conll.py    From SeaRNN-open with MIT License 6 votes vote down vote up
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 
Example #4
Source File: single.py    From invana-bot with MIT License 5 votes vote down vote up
def get_method(self):
        def custom_int(data):
            data = locale.atoi(data)
            return int(data)

        return custom_int 
Example #5
Source File: type_classes.py    From DIVE-backend with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #6
Source File: types.py    From DIVE-backend with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #7
Source File: test_locale.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def _test_atoi(self, value, out):
        self.assertEqual(locale.atoi(value), out) 
Example #8
Source File: utility.py    From maltelligence with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #9
Source File: stdtypes.py    From parade with MIT License 5 votes vote down vote up
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) 
Example #10
Source File: test_locale.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def _test_atoi(self, value, out):
        self.assertEqual(locale.atoi(value), out) 
Example #11
Source File: _fraction_value.py    From barril with MIT License 4 votes vote down vote up
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) 
Example #12
Source File: apbs_cgi.py    From BioBlender with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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