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


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

how to do draw pattern with python?

Started byecho.hping@gmail.com
First post2012-09-21 06:36 -0700
Last post2012-09-22 13:08 +1000
Articles 14 — 10 participants

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


Contents

  how to do draw pattern with python? echo.hping@gmail.com - 2012-09-21 06:36 -0700
    Re: how to do draw pattern with python? Laszlo Nagy <gandalf@shopzeus.com> - 2012-09-21 15:54 +0200
    Re: how to do draw pattern with python? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-21 14:55 +0100
    Re: how to do draw pattern with python? Dave Angel <d@davea.name> - 2012-09-21 10:00 -0400
    Re: how to do draw pattern with python? Peter Otten <__peter__@web.de> - 2012-09-21 16:29 +0200
    Re: how to do draw pattern with python? Ismael Farfán <sulfurfff@gmail.com> - 2012-09-21 11:50 -0500
    Re: how to do draw pattern with python? Peter Otten <__peter__@web.de> - 2012-09-21 19:29 +0200
    Re: how to do draw pattern with python? Chris Angelico <rosuav@gmail.com> - 2012-09-22 03:31 +1000
    Re: how to do draw pattern with python? Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-21 11:32 -0600
      Re: how to do draw pattern with python? Hans Mulder <hansmu@xs4all.nl> - 2012-09-22 21:34 +0200
    Re: how to do draw pattern with python? Chris Angelico <rosuav@gmail.com> - 2012-09-22 03:35 +1000
    Re: how to do draw pattern with python? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-21 20:55 +0100
    Re: how to do draw pattern with python? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-21 16:12 -0400
    Re: how to do draw pattern with python? Chris Angelico <rosuav@gmail.com> - 2012-09-22 13:08 +1000

#29632 — how to do draw pattern with python?

Fromecho.hping@gmail.com
Date2012-09-21 06:36 -0700
Subjecthow to do draw pattern with python?
Message-ID<c35280cc-4b70-4030-b040-bafba2eacdd0@googlegroups.com>
may i know how to shift the bits using only looping and branching??

x....x
.x..x.
..xx..
..xx..
.x..x.
x....x

xx....
..x..x
...xx.
...xx.
..x..x
xx....

.xx...
x..x..
....xx
....xx
x..x..
.xx...

etc..

[toc] | [next] | [standalone]


#29634

FromLaszlo Nagy <gandalf@shopzeus.com>
Date2012-09-21 15:54 +0200
Message-ID<mailman.1010.1348235680.27098.python-list@python.org>
In reply to#29632
On 2012-09-21 15:36, echo.hping@gmail.com wrote:
> may i know how to shift the bits using only looping and branching??

>
> x....x
> .x..x.
> ..xx..
> ..xx..
> .x..x.
> x....x

What kinds of bits? What are these points and x-es anyway? Are they 
strings? Or binary data?

I recommend this for reading:

http://www.catb.org/esr/faqs/smart-questions.html

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


#29636

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-09-21 14:55 +0100
Message-ID<mailman.1012.1348236001.27098.python-list@python.org>
In reply to#29632
On 21/09/2012 14:36, echo.hping@gmail.com wrote:
> may i know how to shift the bits using only looping and branching??
>
> x....x
> .x..x.
> ..xx..
> ..xx..
> .x..x.
> x....x
>
> xx....
> ..x..x
> ...xx.
> ...xx.
> ..x..x
> xx....
>
> .xx...
> x..x..
> ....xx
> ....xx
> x..x..
> .xx...
>
> etc..
>

You write some code and test it.  If it doesn't work you cut and paste 
the smallest sample of code that can reproduce the problem, together 
with the full traceback if applicable, and you're likely to get plenty 
of answers.

-- 
Cheers.

Mark Lawrence.

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


#29637

FromDave Angel <d@davea.name>
Date2012-09-21 10:00 -0400
Message-ID<mailman.1013.1348236061.27098.python-list@python.org>
In reply to#29632
On 09/21/2012 09:36 AM, echo.hping@gmail.com wrote:
> may i know how to shift the bits using only looping and branching??

Yes, show us your code, and what isn't working, and we'll try to help
you complete the assignment.  It'd probably also be good to specify the
rest of the homework, like what version of what language it has to be
implemented in.

I don't see any bits, only strings of characters.  And it seems to me
that using slices is the most obvious mechanism for rotating
fixed-length strings.


> x....x
> .x..x.
> ..xx..
> ..xx..
> .x..x.
> x....x
>
> xx....
> ..x..x
> ...xx.
> ...xx.
> ..x..x
> xx....
>
> .xx...
> x..x..
> ....xx
> ....xx
> x..x..
> .xx...
>
> etc..

-- 

DaveA

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


#29640

FromPeter Otten <__peter__@web.de>
Date2012-09-21 16:29 +0200
Message-ID<mailman.1015.1348237725.27098.python-list@python.org>
In reply to#29632
echo.hping@gmail.com wrote:

> may i know how to shift the bits using only looping and branching??

import time

data = """\
x....x
.x..x.
..xx..
..xx..
.x..x.
x....x

""".splitlines()

data = [line * 12 for line in data] # optional

while True:
    print "\x1b[2J\x1b[0;0H" # optional
    for i, line in enumerate(data):
        print line
        data[i] = line[1:] + line[:1]
    time.sleep(.1)

Doing your homework since 2001 ;)

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


#29652

FromIsmael Farfán <sulfurfff@gmail.com>
Date2012-09-21 11:50 -0500
Message-ID<mailman.1020.1348246237.27098.python-list@python.org>
In reply to#29632
2012/9/21 Peter Otten <__peter__@web.de>:
> echo.hping@gmail.com wrote:
>
>     print "\x1b[2J\x1b[0;0H" # optional

Nice code : )

Could you dissect that weird string for us?

It isn't returning the cursor to (0,0), it's just like executing
clear(1), and looks like those line coloring scape sequences for bash.

Ismael


-- 
Do not let me induce you to satisfy my curiosity, from an expectation,
that I shall gratify yours. What I may judge proper to conceal, does
not concern myself alone.

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


#29654

FromPeter Otten <__peter__@web.de>
Date2012-09-21 19:29 +0200
Message-ID<mailman.1022.1348248500.27098.python-list@python.org>
In reply to#29632
Ismael Farfán wrote:

> 2012/9/21 Peter Otten <__peter__@web.de>:
>> echo.hping@gmail.com wrote:
>>
>>     print "\x1b[2J\x1b[0;0H" # optional
> 
> Nice code : )
> 
> Could you dissect that weird string for us?
> 
> It isn't returning the cursor to (0,0), it's just like executing
> clear(1), and looks like those line coloring scape sequences for bash.

"\x1b[2J" or ESC [2J should clear the screen and

"\x1b[1;1H" or ESC [1;1H should move the cursor to the origin (I got that 
wrong in the previous post)

There may be other problems -- I stopped reading

http://en.wikipedia.org/wiki/ANSI_escape_code

as soon as I got the desired effect (scrolling) in konsole.

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


#29657

FromChris Angelico <rosuav@gmail.com>
Date2012-09-22 03:31 +1000
Message-ID<mailman.1023.1348248717.27098.python-list@python.org>
In reply to#29632
On Sat, Sep 22, 2012 at 2:50 AM, Ismael Farfán <sulfurfff@gmail.com> wrote:
> 2012/9/21 Peter Otten <__peter__@web.de>:
>> echo.hping@gmail.com wrote:
>>
>>     print "\x1b[2J\x1b[0;0H" # optional
>
> Nice code : )
>
> Could you dissect that weird string for us?
>
> It isn't returning the cursor to (0,0), it's just like executing
> clear(1), and looks like those line coloring scape sequences for bash.

It's an ANSI escape sequence, or rather two of them. The first one
clears the screen, the second returns you to 0,0. (Isn't that implicit
in the 2J code? Maybe I'm misremembering.) But it depends on the
terminal responding to them, and not all terminals do. For instance,
most MUD clients parse only a very small subset of ANSI codes, eg
color codes only.

ChrisA

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


#29658

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-09-21 11:32 -0600
Message-ID<mailman.1024.1348248772.27098.python-list@python.org>
In reply to#29632
On Fri, Sep 21, 2012 at 10:50 AM, Ismael Farfán <sulfurfff@gmail.com> wrote:
> 2012/9/21 Peter Otten <__peter__@web.de>:
>> echo.hping@gmail.com wrote:
>>
>>     print "\x1b[2J\x1b[0;0H" # optional
>
> Nice code : )
>
> Could you dissect that weird string for us?
>
> It isn't returning the cursor to (0,0), it's just like executing
> clear(1), and looks like those line coloring scape sequences for bash.

They're called "ANSI escape codes". :-)

CSI 2J clears the screen.
CSI 0;0H means "move the cursor to row 0, column 0".  However, I don't
think that's valid ANSI, as the coordinates are 1-based.  Probably it
should have been "\x1b[2J\x1b[1;1H".

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


#29763

FromHans Mulder <hansmu@xs4all.nl>
Date2012-09-22 21:34 +0200
Message-ID<505e12e3$0$6903$e4fe514c@news2.news.xs4all.nl>
In reply to#29658
On 21/09/12 19:32:20, Ian Kelly wrote:
> On Fri, Sep 21, 2012 at 10:50 AM, Ismael Farfán <sulfurfff@gmail.com> wrote:
>> 2012/9/21 Peter Otten <__peter__@web.de>:
>>> echo.hping@gmail.com wrote:
>>>
>>>     print "\x1b[2J\x1b[0;0H" # optional
>>
>> Nice code : )
>>
>> Could you dissect that weird string for us?
>>
>> It isn't returning the cursor to (0,0), it's just like executing
>> clear(1), and looks like those line coloring scape sequences for bash.
> 
> They're called "ANSI escape codes". :-)
> 
> CSI 2J clears the screen.
> CSI 0;0H means "move the cursor to row 0, column 0".  However, I don't
> think that's valid ANSI, as the coordinates are 1-based.  Probably it
> should have been "\x1b[2J\x1b[1;1H".

Yes, the coordinates are 1-base, so it should have been
"\x1b[2J\x1b[1;1H".  Or, since 1;1 is the default, "\x1b[2J\x1b[H".

On my machine, clear(1) uses "\x1b[H\x1b[2J".

Using clear(1) appears to be the most portable way to do it:

import os, time

data = """\
x....x
.x..x.
..xx..
..xx..
.x..x.
x....x

""".splitlines()

data = [line * 12 for line in data] # optional

try:
    while True:
	os.system("clear") # optional
	for i, line in enumerate(data):
	    print line
	    data[i] = line[1:] + line[:1]
	time.sleep(.1)
except KeyboardInterrupt:
    pass



Hope this helps,

-- HansM

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


#29659

FromChris Angelico <rosuav@gmail.com>
Date2012-09-22 03:35 +1000
Message-ID<mailman.1025.1348248927.27098.python-list@python.org>
In reply to#29632
On Sat, Sep 22, 2012 at 3:31 AM, Chris Angelico <rosuav@gmail.com> wrote:
> It's an ANSI escape sequence, or rather two of them. The first one
> clears the screen, the second returns you to 0,0. (Isn't that implicit
> in the 2J code? Maybe I'm misremembering.)

Ah. From Wikipedia:
"If n is two, clear entire screen (and moves cursor to upper left on
MS-DOS ANSI.SYS)."

So adding \e[H is necessary.

ChrisA

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


#29666

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-09-21 20:55 +0100
Message-ID<mailman.1029.1348257273.27098.python-list@python.org>
In reply to#29632
On 21/09/2012 15:29, Peter Otten wrote:
> echo.hping@gmail.com wrote:
>
>> may i know how to shift the bits using only looping and branching??
>
> import time
>
> data = """\
> x....x
> .x..x.
> ..xx..
> ..xx..
> .x..x.
> x....x
>
> """.splitlines()
>
> data = [line * 12 for line in data] # optional
>
> while True:
>      print "\x1b[2J\x1b[0;0H" # optional
>      for i, line in enumerate(data):
>          print line
>          data[i] = line[1:] + line[:1]
>      time.sleep(.1)
>
> Doing your homework since 2001 ;)
>

I tried running your code but got this:-

c:\Users\Mark>pattern.py
   File "C:\Users\Mark\pattern.py", line 22
     Doing your homework since 2001
              ^
SyntaxError: invalid syntax

What am I doing wrong?

-- 
Cheers.

Mark Lawrence.

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


#29672

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-09-21 16:12 -0400
Message-ID<mailman.1034.1348258501.27098.python-list@python.org>
In reply to#29632
On Fri, 21 Sep 2012 11:50:35 -0500, Ismael Farfán <sulfurfff@gmail.com>
declaimed the following in gmane.comp.python.general:

> 2012/9/21 Peter Otten <__peter__@web.de>:
> > echo.hping@gmail.com wrote:
> >
> >     print "\x1b[2J\x1b[0;0H" # optional
> 
> Nice code : )
> 
> Could you dissect that weird string for us?

http://www.termsys.demon.co.uk/vtansi.htm
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

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


#29717

FromChris Angelico <rosuav@gmail.com>
Date2012-09-22 13:08 +1000
Message-ID<mailman.1059.1348283326.27098.python-list@python.org>
In reply to#29632
On Sat, Sep 22, 2012 at 5:55 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> I tried running your code but got this:-
>
> c:\Users\Mark>pattern.py
>   File "C:\Users\Mark\pattern.py", line 22
>
>     Doing your homework since 2001
>              ^
> SyntaxError: invalid syntax
>
> What am I doing wrong?

The problem is the first non-blank line after "I tried running your
code". Note the path designators and the way they appear ready to fall
over backwards at a moment's notice, just like their host operating
system. Switch to my new HomeworkOS, freely downloadable from my
personal server running my own protocols and completely incompatible
with TCP/IP, and this will work perfectly!

*searches the Yellow Pages for cheek-tongue-removal services*

ChrisA

[toc] | [prev] | [standalone]


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


csiph-web