Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102028 > unrolled thread

import locale and print range on same line

Started byraiwil@gmail.com
First post2016-01-23 02:02 -0800
Last post2016-01-24 02:00 -0600
Articles 13 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  import locale and print range on same line raiwil@gmail.com - 2016-01-23 02:02 -0800
    Re: import locale and print range on same line Marko Rauhamaa <marko@pacujo.net> - 2016-01-23 12:36 +0200
      Re: import locale and print range on same line Vlastimil Brom <vlastimil.brom@gmail.com> - 2016-01-23 12:12 +0100
        Re: import locale and print range on same line Ramo <raiwil@gmail.com> - 2016-01-23 03:57 -0800
    Re: import locale and print range on same line Steven D'Aprano <steve@pearwood.info> - 2016-01-24 00:07 +1100
      Re: import locale and print range on same line Chris Angelico <rosuav@gmail.com> - 2016-01-24 00:19 +1100
        Re: import locale and print range on same line Steven D'Aprano <steve@pearwood.info> - 2016-01-24 00:45 +1100
          Re: import locale and print range on same line Chris Angelico <rosuav@gmail.com> - 2016-01-24 00:58 +1100
            Re: import locale and print range on same line Ramo <raiwil@gmail.com> - 2016-01-23 06:03 -0800
              Re: import locale and print range on same line Chris Angelico <rosuav@gmail.com> - 2016-01-24 01:53 +1100
          Re: import locale and print range on same line Terry Reedy <tjreedy@udel.edu> - 2016-01-23 21:45 -0500
          Re: import locale and print range on same line Chris Angelico <rosuav@gmail.com> - 2016-01-24 13:51 +1100
          Re: import locale and print range on same line eryk sun <eryksun@gmail.com> - 2016-01-24 02:00 -0600

#102028 — import locale and print range on same line

Fromraiwil@gmail.com
Date2016-01-23 02:02 -0800
Subjectimport locale and print range on same line
Message-ID<2bda88dd-82e3-4e43-b49c-3945a0befdc2@googlegroups.com>
Can someone tell me why next code doesn't work?

import locale; locale.setlocale(locale.LC_ALL, ""); for i in range(1,20,4): print(locale.format("%2f", i, 1))

It gives an error: SyntaxError: invalid syntax --> indicating 'for'

However I need to put the code on one single line.
When I separate them like below it works fine.

import locale
locale.setlocale(locale.LC_ALL, "")
for i in range(1,20,4):
   print(locale.format("%2f", i, 1))

[toc] | [next] | [standalone]


#102030

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-01-23 12:36 +0200
Message-ID<87zivw37ml.fsf@elektro.pacujo.net>
In reply to#102028
raiwil@gmail.com:

> Can someone tell me why next code doesn't work?
>
> import locale; locale.setlocale(locale.LC_ALL, ""); for i in
> range(1,20,4): print(locale.format("%2f", i, 1))
>
> It gives an error: SyntaxError: invalid syntax --> indicating 'for'
>
> However I need to put the code on one single line.
> When I separate them like below it works fine.
>
> import locale
> locale.setlocale(locale.LC_ALL, "")
> for i in range(1,20,4):
>    print(locale.format("%2f", i, 1))

The answer is in Python's syntax definition. Not everything is allowed
on a single line.

See <URL: https://docs.python.org/3/reference/grammar.html>

Only small_stmt's can be separated by semicolons.

A for statement is a compound_stmt, which is not a small_stmt.


Marko

[toc] | [prev] | [next] | [standalone]


#102032

FromVlastimil Brom <vlastimil.brom@gmail.com>
Date2016-01-23 12:12 +0100
Message-ID<mailman.175.1453547570.15297.python-list@python.org>
In reply to#102030
2016-01-23 11:36 GMT+01:00 Marko Rauhamaa <marko@pacujo.net>:
> raiwil@gmail.com:
>
>> Can someone tell me why next code doesn't work?
>>
>> import locale; locale.setlocale(locale.LC_ALL, ""); for i in
>> range(1,20,4): print(locale.format("%2f", i, 1))
>>
>> It gives an error: SyntaxError: invalid syntax --> indicating 'for'
>>
>> However I need to put the code on one single line.
>> When I separate them like below it works fine.
>>
>> import locale
>> locale.setlocale(locale.LC_ALL, "")
>> for i in range(1,20,4):
>>    print(locale.format("%2f", i, 1))
>
> The answer is in Python's syntax definition. Not everything is allowed
> on a single line.
>
> See <URL: https://docs.python.org/3/reference/grammar.html>
>
> Only small_stmt's can be separated by semicolons.
>
> A for statement is a compound_stmt, which is not a small_stmt.
>
>
> Marko
> --
> https://mail.python.org/mailman/listinfo/python-list

Hi,
if you realy can't use multiple lines (which is rather essencial for
python), you can circumvent this in some cases, e.g. using list
comprehension and ignoring the created list itself:
import locale; locale.setlocale(locale.LC_ALL, "");
[print(locale.format("%2f", i, 1)) for i in range(1,20,4)]

vbr

[toc] | [prev] | [next] | [standalone]


#102033

FromRamo <raiwil@gmail.com>
Date2016-01-23 03:57 -0800
Message-ID<f0299490-53ea-4217-b159-11e603530b9b@googlegroups.com>
In reply to#102032
This works also but I thought it was possible to do it easier:

import locale; locale.setlocale(locale.LC_ALL, ""); print('\n'.join(locale.format("%2f", i, 1) for i in range(1,20,4)))

[toc] | [prev] | [next] | [standalone]


#102036

FromSteven D'Aprano <steve@pearwood.info>
Date2016-01-24 00:07 +1100
Message-ID<56a37b1e$0$1612$c3e8da3$5496439d@news.astraweb.com>
In reply to#102028
On Sat, 23 Jan 2016 09:02 pm, raiwil@gmail.com wrote:

> However I need to put the code on one single line.

Why? Is the Enter key on your keyboard broken?



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#102037

FromChris Angelico <rosuav@gmail.com>
Date2016-01-24 00:19 +1100
Message-ID<mailman.176.1453555148.15297.python-list@python.org>
In reply to#102036
On Sun, Jan 24, 2016 at 12:07 AM, Steven D'Aprano <steve@pearwood.info> wrote:
> On Sat, 23 Jan 2016 09:02 pm, raiwil@gmail.com wrote:
>
>> However I need to put the code on one single line.
>
> Why? Is the Enter key on your keyboard broken?

Maybe it's for a python -c invocation.

ChrisA

[toc] | [prev] | [next] | [standalone]


#102039

FromSteven D'Aprano <steve@pearwood.info>
Date2016-01-24 00:45 +1100
Message-ID<56a383fa$0$1593$c3e8da3$5496439d@news.astraweb.com>
In reply to#102037
On Sun, 24 Jan 2016 12:19 am, Chris Angelico wrote:

> On Sun, Jan 24, 2016 at 12:07 AM, Steven D'Aprano <steve@pearwood.info>
> wrote:
>> On Sat, 23 Jan 2016 09:02 pm, raiwil@gmail.com wrote:
>>
>>> However I need to put the code on one single line.
>>
>> Why? Is the Enter key on your keyboard broken?
> 
> Maybe it's for a python -c invocation.


[steve@ando ~]$ python -c "for i in range(5):
>     print 'hello world'
> "
hello world
hello world
hello world
hello world
hello world
[steve@ando ~]$




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#102040

FromChris Angelico <rosuav@gmail.com>
Date2016-01-24 00:58 +1100
Message-ID<mailman.177.1453557489.15297.python-list@python.org>
In reply to#102039
On Sun, Jan 24, 2016 at 12:45 AM, Steven D'Aprano <steve@pearwood.info> wrote:
> On Sun, 24 Jan 2016 12:19 am, Chris Angelico wrote:
>
>> On Sun, Jan 24, 2016 at 12:07 AM, Steven D'Aprano <steve@pearwood.info>
>> wrote:
>>> On Sat, 23 Jan 2016 09:02 pm, raiwil@gmail.com wrote:
>>>
>>>> However I need to put the code on one single line.
>>>
>>> Why? Is the Enter key on your keyboard broken?
>>
>> Maybe it's for a python -c invocation.
>
>
> [steve@ando ~]$ python -c "for i in range(5):
>>     print 'hello world'
>> "
> hello world
> hello world
> hello world
> hello world
> hello world
> [steve@ando ~]$

Well, not everyone's shells are as awesome as bash... and not everyone
knows you can do that. That said, though... this is a good reason for
giving full context for the question. "I'm trying to X, but I get this
error because of Y. How would I go about fixing Y?" - response:
"Actually, you can do X in a slightly different way.".

Just sayin', there might be other reasons for wanting to put things
onto one line.

ChrisA

[toc] | [prev] | [next] | [standalone]


#102041

FromRamo <raiwil@gmail.com>
Date2016-01-23 06:03 -0800
Message-ID<a2ea8964-0f50-4580-90c3-fba52177bcaa@googlegroups.com>
In reply to#102040
The reason why I want to have it on onto one line has nothing to do with my question, "why doesn't it work on one line" :)

But if you want to know it, I use this python code in the commandline of a texteditor :)

Btw.. thank you all for your help. Very happy with it :)

[toc] | [prev] | [next] | [standalone]


#102043

FromChris Angelico <rosuav@gmail.com>
Date2016-01-24 01:53 +1100
Message-ID<mailman.179.1453560787.15297.python-list@python.org>
In reply to#102041
On Sun, Jan 24, 2016 at 1:03 AM, Ramo <raiwil@gmail.com> wrote:
> The reason why I want to have it on onto one line has nothing to do with my question, "why doesn't it work on one line" :)
>
> But if you want to know it, I use this python code in the commandline of a texteditor :)

Called it! :)

It actually has a lot to do with your question, because it puts
special restrictions on the answer. By giving context, you prevent
answers like Steven's that are trying to fix what is thought to be a
faulty assumption; instead, we'll attack the problem as an
intellectual challenge - how can we achieve this goal within these
restrictions? Worded that way, the question will definitely get
responses that aren't what we'd normally call "good code", but they
fit the situation. It's not uncommon to have questions like this,
especially from a security point of view; for instance: "Is it
possible to invoke os.system() without using any builtins or imports,
and without having an underscore anywhere in the code?" (answer: yes,
it is).

ChrisA

[toc] | [prev] | [next] | [standalone]


#102067

FromTerry Reedy <tjreedy@udel.edu>
Date2016-01-23 21:45 -0500
Message-ID<mailman.193.1453603541.15297.python-list@python.org>
In reply to#102039
On 1/23/2016 8:58 AM, Chris Angelico wrote:
> On Sun, Jan 24, 2016 at 12:45 AM, Steven D'Aprano <steve@pearwood.info> wrote:
>> On Sun, 24 Jan 2016 12:19 am, Chris Angelico wrote:
>>
>>> On Sun, Jan 24, 2016 at 12:07 AM, Steven D'Aprano <steve@pearwood.info>
>>> wrote:
>>>> On Sat, 23 Jan 2016 09:02 pm, raiwil@gmail.com wrote:
>>>>
>>>>> However I need to put the code on one single line.
>>>>
>>>> Why? Is the Enter key on your keyboard broken?
>>>
>>> Maybe it's for a python -c invocation.
>>
>>
>> [steve@ando ~]$ python -c "for i in range(5):
>>>      print 'hello world'
>>> "
>> hello world
>> hello world
>> hello world
>> hello world
>> hello world
>> [steve@ando ~]$
>
> Well, not everyone's shells are as awesome as bash...

Like Windows command prompt is not.  I tried:

C:\Users\Terry>python -c "for i in range(5):\n\tprint('hello world')"
   File "<string>", line 1
     for i in range(5):\n  print('hello world')
                                              ^
SyntaxError: unexpected character after line continuation character

-c does not preprocess the code string before executing.  I may propose 
that it do so.  However, Python is still pretty awesome.

C:\Users\Terry>python -c "exec('''for i in range(5):\n  print('hello 
world')''')"
hello world
hello world
hello world
hello world
hello world

> and not everyone knows you can do that.

One can even combine -i (interactive) with -c (code).

C:\Users\Terry> python -i -c "exec('''a=[]\nfor i in 
(1,2,3):\n\ta.append(i)''')"
 >>> a
[1, 2, 3]
 >>>

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#102068

FromChris Angelico <rosuav@gmail.com>
Date2016-01-24 13:51 +1100
Message-ID<mailman.194.1453603906.15297.python-list@python.org>
In reply to#102039
On Sun, Jan 24, 2016 at 1:45 PM, Terry Reedy <tjreedy@udel.edu> wrote:
> C:\Users\Terry>python -c "for i in range(5):\n\tprint('hello world')"
>   File "<string>", line 1
>     for i in range(5):\n  print('hello world')
>                                              ^
> SyntaxError: unexpected character after line continuation character
>
> -c does not preprocess the code string before executing.  I may propose that
> it do so.

As an alternative, maybe multiple -c parameters could result in
multiple lines? Something like:

python3 -c "for i in range(5):" -c " print('Hello, world')"

Currently, a second -c is unparsed (it ends up in sys.argv), so this
would technically be a behavioural change (but then, so would
preprocessing).

ChrisA

[toc] | [prev] | [next] | [standalone]


#102073

Fromeryk sun <eryksun@gmail.com>
Date2016-01-24 02:00 -0600
Message-ID<mailman.197.1453622476.15297.python-list@python.org>
In reply to#102039
On Sat, Jan 23, 2016 at 8:45 PM, Terry Reedy <tjreedy@udel.edu> wrote:
> On 1/23/2016 8:58 AM, Chris Angelico wrote:
>> On Sun, Jan 24, 2016 at 12:45 AM, Steven D'Aprano <steve@pearwood.info>
>> wrote:
>>> [steve@ando ~]$ python -c "for i in range(5):
>>>>
>>>>      print 'hello world'
>>>> "
>>>
>>> hello world
>>> hello world
>>> hello world
>>> hello world
>>> hello world
>>> [steve@ando ~]$
>>
>> Well, not everyone's shells are as awesome as bash...
>
> Like Windows command prompt is not.  I tried:
>
> C:\Users\Terry>python -c "for i in range(5):\n\tprint('hello world')"
>   File "<string>", line 1
>     for i in range(5):\n  print('hello world')
>                                              ^
> SyntaxError: unexpected character after line continuation character

cmd's parsing can be inscrutably finicky and limited, but luckily
enough this example works:

    C:\>py -2 -c ^
    More? "def f(n):^
    More?
    More?     for i in range(n):^
    More?
    More?         print 'hello world'^
    More?
    More? f(5)
    hello world
    hello world
    hello world
    hello world
    hello world

cmd prints the "More?" prompt when a line is continued. Note that it's
necessary to hit enter again after escaping the first enter. Also, as
is usual with cmd, the closing double quote is optional, so I omitted
it after f(5).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web