Exemplo n.º 1
0
 def test_formatter_parser(self):
     import _string
     l = list(_string.formatter_parser('abcd'))
     assert l == [('abcd', None, None, None)]
     #
     l = list(_string.formatter_parser('ab{0}cd'))
     assert l == [('ab', '0', '', None), ('cd', None, None, None)]
     #
     l = list(_string.formatter_parser('{0}cd'))
     assert l == [('', '0', '', None), ('cd', None, None, None)]
     #
     l = list(_string.formatter_parser('ab{0}'))
     assert l == [('ab', '0', '', None)]
     #
     l = list(_string.formatter_parser(''))
     assert l == []
     #
     l = list(_string.formatter_parser('{0:123}'))
     assert l == [('', '0', '123', None)]
     #
     l = list(_string.formatter_parser('{0!x:123}'))
     assert l == [('', '0', '123', 'x')]
     #
     l = list(_string.formatter_parser('{0!x:12{sdd}3}'))
     assert l == [('', '0', '12{sdd}3', 'x')]
Exemplo n.º 2
0
 def test_formatter_parser(self):
     import _string
     l = list(_string.formatter_parser('abcd'))
     assert l == [('abcd', None, None, None)]
     #
     l = list(_string.formatter_parser('ab{0}cd'))
     assert l == [('ab', '0', '', None), ('cd', None, None, None)]
     #
     l = list(_string.formatter_parser('{0}cd'))
     assert l == [('', '0', '', None), ('cd', None, None, None)]
     #
     l = list(_string.formatter_parser('ab{0}'))
     assert l == [('ab', '0', '', None)]
     #
     l = list(_string.formatter_parser(''))
     assert l == []
     #
     l = list(_string.formatter_parser('{0:123}'))
     assert l == [('', '0', '123', None)]
     #
     l = list(_string.formatter_parser('{0!x:123}'))
     assert l == [('', '0', '123', 'x')]
     #
     l = list(_string.formatter_parser('{0!x:12{sdd}3}'))
     assert l == [('', '0', '12{sdd}3', 'x')]
Exemplo n.º 3
0
    def execute(self) -> DataFrame:
        """
        Returns a DataFrame for which the SQL statement has been executed by
        the underlying SQL engine.

        >>> str0 = 'abc'
        >>> ks.sql("select {str0}")
           abc
        0  abc

        >>> str1 = 'abc"abc'
        >>> str2 = "abc'abc"
        >>> ks.sql("select {str0}, {str1}, {str2}")
           abc  abc"abc  abc'abc
        0  abc  abc"abc  abc'abc

        >>> strs = ['a', 'b']
        >>> ks.sql("select 'a' in {strs} as cond1, 'c' in {strs} as cond2")
           cond1  cond2
        0   True  False
        """
        blocks = _string.formatter_parser(self._statement)
        # TODO: use a string builder
        res = ""
        try:
            for (pre, inner, _, _) in blocks:
                var_next = "" if inner is None else self._convert(inner)
                res = res + pre + var_next
            self._normalized_statement = res

            sdf = self._session.sql(self._normalized_statement)
        finally:
            for v in self._temp_views:
                self._session.catalog.dropTempView(v)
        return DataFrame(sdf)
Exemplo n.º 4
0
def varnames_from_fmt(fmt: TemplateLike):
    import _string
    
    if isinstance(fmt, TemplateString):
        fmt = fmt.get_format_string()
    
    return [fname for _, fname, _, _ in _string.formatter_parser(fmt) if fname]
Exemplo n.º 5
0
    def __init__(self, format_string, default=None):
        self.default = default
        key = (format_string, default)

        try:
            self.result, self.fields = self.CACHE[key]
        except KeyError:
            self.result = []
            self.fields = []

            for literal_text, field_name, format_spec, conv in \
                    _string.formatter_parser(format_string):
                if literal_text:
                    self.result.append(literal_text)
                if field_name:
                    self.fields.append((
                        len(self.result),
                        self._field_access(field_name, format_spec, conv),
                    ))
                    self.result.append("")

            self.CACHE[key] = (self.result, self.fields)

        if len(self.result) == 1:
            if self.fields:
                self.format_map = self.fields[0][1]
            else:
                self.format_map = lambda _: format_string
            del self.result, self.fields
Exemplo n.º 6
0
def test():
    for _ in range(9999):
        try:
            list(_string.formatter_field_name_split(fstr())[1])
        except ValueError:
            pass

        try:
            list(_string.formatter_parser(fstr()))
        except ValueError:
            pass
Exemplo n.º 7
0
def _total_replacement_field_number(format_str: str) -> int:
    """Get the total number of replacement field in the format string

    :param format_str: The string which contains replacement field.
    :return: Total number of replacement field.
    """
    field_count = 0
    for _, field_name, _, _ in _string.formatter_parser(format_str):
        if field_name is not None:
            field_count += 1
    return field_count
Exemplo n.º 8
0
    def __init__(self, format_string, default=None):
        self.default = default
        self.result = []
        self.fields = []

        for literal_text, field_name, format_spec, conversion in \
                _string.formatter_parser(format_string):
            if literal_text:
                self.result.append(literal_text)
            if field_name:
                self.fields.append((len(self.result),
                                    self._field_access(field_name, format_spec,
                                                       conversion)))
                self.result.append("")
Exemplo n.º 9
0
    def __init__(self, format_string, default=None):
        self.default = default
        self.result = []
        self.fields = []

        for literal_text, field_name, format_spec, conversion in \
                _string.formatter_parser(format_string):
            if literal_text:
                self.result.append(literal_text)
            if field_name:
                self.fields.append((
                    len(self.result),
                    self._field_access(field_name, format_spec, conversion)
                ))
                self.result.append("")
Exemplo n.º 10
0
    def execute(self, index_col: Optional[Union[str, List[str]]]) -> DataFrame:
        """
        Returns a DataFrame for which the SQL statement has been executed by
        the underlying SQL engine.

        >>> from pyspark.pandas import sql_processor
        >>> # we will call 'sql_processor' directly in doctests so decrease one level.
        >>> sql_processor._CAPTURE_SCOPES = 2
        >>> sql = sql_processor.sql
        >>> str0 = 'abc'
        >>> sql("select {str0}")
           abc
        0  abc

        >>> str1 = 'abc"abc'
        >>> str2 = "abc'abc"
        >>> sql("select {str0}, {str1}, {str2}")
           abc  abc"abc  abc'abc
        0  abc  abc"abc  abc'abc

        >>> strs = ['a', 'b']
        >>> sql("select 'a' in {strs} as cond1, 'c' in {strs} as cond2")
           cond1  cond2
        0   True  False
        """
        blocks = _string.formatter_parser(self._statement)
        # TODO: use a string builder
        res = ""
        try:
            for (pre, inner, _, _) in blocks:
                var_next = "" if inner is None else self._convert(inner)
                res = res + pre + var_next
            self._normalized_statement = res

            sdf = self._session.sql(self._normalized_statement)
        finally:
            for v in self._temp_views:
                self._session.catalog.dropTempView(v)

        index_spark_columns, index_names = _get_index_map(sdf, index_col)

        return DataFrame(
            InternalFrame(spark_frame=sdf,
                          index_spark_columns=index_spark_columns,
                          index_names=index_names))
Exemplo n.º 11
0
    def __init__(self, format_string, default=None):
        self.default = default
        self.result = []
        self.fields = []

        for literal_text, field_name, format_spec, conversion in \
                _string.formatter_parser(format_string):
            if literal_text:
                self.result.append(literal_text)
            if field_name:
                self.fields.append((
                    len(self.result),
                    self._field_access(field_name, format_spec, conversion),
                ))
                self.result.append("")

        if len(self.result) == 1:
            if self.fields:
                self.format_map = self.fields[0][1]
            else:
                self.format_map = lambda _: format_string
            del self.result, self.fields
Exemplo n.º 12
0
    def vformat(self, format_string, kwargs):
        """Apply 'kwargs' to the initial format_string and return its result"""
        result = []
        append = result.append

        for literal_text, field_name, format_spec, conversion in \
                _string.formatter_parser(format_string):

            if literal_text:
                append(literal_text)

            if field_name:
                obj = self.get_field(field_name, kwargs)
                if conversion:
                    obj = self.conversions[conversion](obj)
                if format_spec:
                    format_spec = format_spec.format_map(kwargs)
                    obj = self.format_field(obj, format_spec)
                else:
                    obj = str(obj)
                append(obj)

        return "".join(result)
Exemplo n.º 13
0
 def parse(self, format_string):
     return _string.formatter_parser(format_string)
Exemplo n.º 14
0
Arquivo: string.py Projeto: za/cpython
 def parse(self, format_string):
     return _string.formatter_parser(format_string)
Exemplo n.º 15
0
 def test_formatter_parser_escape(self):
     import _string
     l = list(_string.formatter_parser("{{a}}"))
     assert l == [('{', None, None, None), ('a}', None, None, None)]
     l = list(_string.formatter_parser("{{{{"))
     assert l == [('{', None, None, None), ('{', None, None, None)]
Exemplo n.º 16
0
 def test_u_formatter_parser(self):
     import _string
     l = list(_string.formatter_parser('{0!x:12{sdd}3}'))
     assert l == [('', '0', '12{sdd}3', 'x')]
     for x in l[0]:
         assert isinstance(x, str)
Exemplo n.º 17
0
MethodWrapperType = type(object.__call__)
NoneType = type(None)
NotImplementedType = type(NotImplemented)
QuitterType = type(exit)
WrapperDescriptorType = type(type.__call__)

if PY2:
    StrFormatterIteratorType = type(str()._formatter_parser())
    StrFieldNameIteratorType = type(str()._formatter_field_name_split()[1])
    UnicodeFormatterIteratorType = type(unicode()._formatter_parser())
    UnicodeFieldNameIteratorType = type(unicode()._formatter_field_name_split()[1])
if PY3:
    import _string


    StrFormatterIteratorType = type(_string.formatter_parser(str()))
    StrFieldNameIteratorType = type(_string.formatter_field_name_split(str())[1])

if PY3:
    ModuleDefType = only(stubtool.name.search_for_type('builtins.moduledef', object))
    StdPrinterType = only(stubtool.name.search_for_type('builtins.stderrprinter', object))

if PY33:
    ManagedBufferType = type(only(gc.get_referents(memoryview(b''))))

if PY35:
    exec('async def func(): pass')
    coro = func()
    wrapper = coro.__await__()
    coro.close()
    CoroutineWrapperType = type(wrapper)
Exemplo n.º 18
0
 def parse(self, format_string):
     if PY3:
         return _string.formatter_parser(format_string)  # pragma: no cover
     else:
         return format_string._formatter_parser()  # pragma: no cover
Exemplo n.º 19
0
 def parse(self, format_string):
     if PY3:
         return _string.formatter_parser(format_string)  # pragma: no cover
     else:
         return format_string._formatter_parser()  # pragma: no cover
Exemplo n.º 20
0
 def test_u_formatter_parser(self):
     import _string
     l = list(_string.formatter_parser('{0!x:12{sdd}3}'))
     assert l == [('', '0', '12{sdd}3', 'x')]
     for x in l[0]:
         assert isinstance(x, str)
Exemplo n.º 21
0
 def test_formatter_parser_escape(self):
     import _string
     l = list(_string.formatter_parser("{{a}}"))
     assert l == [('{', None, None, None), ('a}', None, None, None)]
     l = list(_string.formatter_parser("{{{{"))
     assert l == [('{', None, None, None), ('{', None, None, None)]