[python3]

Discussion in 'Python' started by Kevin Adams, Mar 19, 2010.

  1. Kevin Adams

    Kevin Adams Guest

    Greetings!

    Please forgive me if im posting this to the wrong group.

    I'm new to Python, learning Python3 from the O'rielly "Learning
    Python" book. Reading
    about operator overloading, specifically __getitem__. I put together
    a small bit of code to
    do some experimenting and threw in a timer so i can see it do its
    thing. For some reason
    the time.sleep(x) function doesnt work when my print function includes
    "end=''".

    Thanks in advance for any help.


    ---code---

    class TestClass():
    def __init__(self):
    self.data = "I was far from home and the spell of the eastern
    sea was upon me."

    def __getitem__(self,i):
    return self.data


    import time

    if __name__ == "__main__":

    me = TestClass()
    for x in me:
    print(x,end='') #if i remove the 'end=''' it performs as i'd
    expect
    time.sleep(int(2))
    Kevin Adams, Mar 19, 2010
    #1
    1. Advertising

  2. Kevin Adams

    Steve Holden Guest

    Kevin Adams wrote:
    > Greetings!
    >
    > Please forgive me if im posting this to the wrong group.
    >
    > I'm new to Python, learning Python3 from the O'rielly "Learning
    > Python" book. Reading
    > about operator overloading, specifically __getitem__. I put together
    > a small bit of code to
    > do some experimenting and threw in a timer so i can see it do its
    > thing. For some reason
    > the time.sleep(x) function doesnt work when my print function includes
    > "end=''".
    >
    > Thanks in advance for any help.
    >
    >

    Try the following changes:

    > ---code---
    >
    > class TestClass():
    > def __init__(self):
    > self.data = "I was far from home and the spell of the eastern
    > sea was upon me."
    >
    > def __getitem__(self,i):
    > return self.data
    >
    >
    > import time


    import sys
    >
    > if __name__ == "__main__":
    >
    > me = TestClass()
    > for x in me:
    > print(x,end='') #if i remove the 'end=''' it performs as i'd
    > expect

    sys.stdout.flush()

    > time.sleep(int(2))
    >


    It may just be that the output is being held in buffers until the
    program terminates. the fluch() methof pushes it straight out.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
    Steve Holden, Mar 19, 2010
    #2
    1. Advertising

  3. On Fri, Mar 19, 2010 at 6:42 PM, Kevin Adams <> wrote:
    > Greetings!
    >
    > Please forgive me if im posting this to the wrong group.
    >
    > I'm new to Python, learning Python3 from the O'rielly "Learning
    > Python" book.  Reading
    > about operator overloading, specifically __getitem__.   I put together
    > a small bit of code to
    > do some experimenting and threw in a timer so i can see it do its
    > thing.  For some reason
    > the time.sleep(x) function doesnt work when my print function includes
    > "end=''".
    >
    > Thanks in advance for any help.
    >
    >
    > ---code---
    >
    > class TestClass():
    >    def __init__(self):
    >        self.data = "I was far from home and the spell of the eastern
    > sea was upon me."
    >
    >    def __getitem__(self,i):
    >        return self.data
    >
    >
    > import time
    >
    > if __name__ == "__main__":
    >
    >    me = TestClass()
    >    for x in me:
    >        print(x,end='')  #if i remove the 'end='''  it performs as i'd
    > expect
    >        time.sleep(int(2))
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    I'm guessing wildly here but I think you have to flush the output.
    Can't remember how right now but it won't take much googling for it.
    Krister Svanlund, Mar 19, 2010
    #3
  4. Kevin Adams

    Terry Reedy Guest

    On 3/19/2010 1:42 PM, Kevin Adams wrote:

    > Please forgive me if im posting this to the wrong group.


    Just the right place. I would suggest a more informative title in the
    future though. 'Problem with print and sleep.', for instance.

    >
    > I'm new to Python, learning Python3 from the O'rielly "Learning
    > Python" book. Reading
    > about operator overloading, specifically __getitem__. I put together
    > a small bit of code to
    > do some experimenting and threw in a timer so i can see it do its
    > thing.


    Good idea. Welcome to Python.

    > For some reason
    > the time.sleep(x) function doesnt work when my print function includes
    > "end=''".


    I cut your code, pasted it into IDLE edit window, and made two minor
    modifications. For me (3.1, winxp) it works perfectly either way --
    chars print at 1 sec intervals either vertically or horizonatally. So I
    am not sure what you are claiming.
    >
    > Thanks in advance for any help.
    >
    >
    > ---code---
    >
    > class TestClass():
    > def __init__(self):
    > self.data = "I was far from home and the spell of the eastern
    > sea was upon me."


    I shortened string
    >
    > def __getitem__(self,i):
    > return self.data
    >
    > import time
    >
    > if __name__ == "__main__":
    >
    > me = TestClass()
    > for x in me:
    > print(x,end='') #if i remove the 'end=''' it performs as i'd
    > expect
    > time.sleep(int(2))


    int(2) == 2, so delete irrelevant call, and 1 sec is enough

    Terry Jan Reedy
    Terry Reedy, Mar 19, 2010
    #4
  5. Kevin Adams

    Kevin Adams Guest

    On Mar 19, 2:21 pm, Terry Reedy <> wrote:
    > On 3/19/2010 1:42 PM, Kevin Adams wrote:
    >
    > > Please forgive me if im posting this to the wrong group.

    >
    > Just the right place. I would suggest a more informative title in the
    > future though. 'Problem with print and sleep.', for instance.
    >
    >
    >
    > > I'm new to Python, learning Python3 from the O'rielly "Learning
    > > Python" book.  Reading
    > > about operator overloading, specifically __getitem__.   I put together
    > > a small bit of code to
    > > do some experimenting and threw in a timer so i can see it do its
    > > thing.

    >
    > Good idea. Welcome to Python.
    >
    > >  For some reason
    > > the time.sleep(x) function doesnt work when my print function includes
    > > "end=''".

    >
    > I cut your code, pasted it into IDLE edit window, and made two minor
    > modifications. For me (3.1, winxp) it works perfectly either way --
    > chars print at 1 sec intervals either vertically or horizonatally. So I
    > am not sure what you are claiming.
    >
    >
    >
    > > Thanks in advance for any help.

    >
    > > ---code---

    >
    > > class TestClass():
    > >      def __init__(self):
    > >          self.data = "I was far from home and the spell of the eastern
    > > sea was upon me."

    >
    > I shortened string
    >
    >
    >
    > >      def __getitem__(self,i):
    > >          return self.data

    >
    > > import time

    >
    > > if __name__ == "__main__":

    >
    > >      me = TestClass()
    > >      for x in me:
    > >          print(x,end='')  #if i remove the 'end='''  it performs as i'd
    > > expect
    > >          time.sleep(int(2))

    >
    > int(2) == 2, so delete irrelevant call, and 1 sec is enough
    >
    > Terry Jan Reedy


    Thanks to all!!!

    It did indeed turn out to be a need to flush the stdout.

    K

    ps. sorry for the brief subject, i wasnt finish and hit send before
    comming back to it.
    Kevin Adams, Mar 19, 2010
    #5
  6. Kevin Adams

    Terry Reedy Guest

    On 3/19/2010 2:17 PM, Steve Holden wrote:
    > Kevin Adams wrote:
    >> Greetings!
    >>
    >> Please forgive me if im posting this to the wrong group.
    >>
    >> I'm new to Python, learning Python3 from the O'rielly "Learning
    >> Python" book. Reading
    >> about operator overloading, specifically __getitem__. I put together
    >> a small bit of code to
    >> do some experimenting and threw in a timer so i can see it do its
    >> thing. For some reason
    >> the time.sleep(x) function doesnt work when my print function includes
    >> "end=''".
    >>
    >> Thanks in advance for any help.
    >>
    >>

    > Try the following changes:
    >
    >> ---code---
    >>
    >> class TestClass():
    >> def __init__(self):
    >> self.data = "I was far from home and the spell of the eastern
    >> sea was upon me."
    >>
    >> def __getitem__(self,i):
    >> return self.data
    >>
    >>
    >> import time

    >
    > import sys
    >>
    >> if __name__ == "__main__":
    >>
    >> me = TestClass()
    >> for x in me:
    >> print(x,end='') #if i remove the 'end=''' it performs as i'd
    >> expect

    > sys.stdout.flush()
    >
    >> time.sleep(int(2))

    >
    > It may just be that the output is being held in buffers until the
    > program terminates. the fluch() methof pushes it straight out.


    The IDLE Shell window must get print output back from the pythonw
    process without buffering. When I cut and pasted from IDLE editor to
    standard interpreter window, output was bunched after several seconds.
    OP should have started with short string and minimal delay so output
    would not take a whole minute. Flushing fixed problem in standard
    interpreter. I will have to remember that testing partial line output in
    IDLE does not show how it will act elsewhere.

    Kevin: when reporting a problem, be more specific as to what 'does not
    work' means.

    Terry Jan Reedy
    Terry Reedy, Mar 19, 2010
    #6
  7. Kevin Adams

    Kevin Adams Guest

    On Mar 19, 3:02 pm, Terry Reedy <> wrote:
    > On 3/19/2010 2:17 PM, Steve Holden wrote:
    >
    >
    >
    > > Kevin Adams wrote:
    > >> Greetings!

    >
    > >> Please forgive me if im posting this to the wrong group.

    >
    > >> I'm new to Python, learning Python3 from the O'rielly "Learning
    > >> Python" book.  Reading
    > >> about operator overloading, specifically __getitem__.   I put together
    > >> a small bit of code to
    > >> do some experimenting and threw in a timer so i can see it do its
    > >> thing.  For some reason
    > >> the time.sleep(x) function doesnt work when my print function includes
    > >> "end=''".

    >
    > >> Thanks in advance for any help.

    >
    > > Try the following changes:

    >
    > >> ---code---

    >
    > >> class TestClass():
    > >>      def __init__(self):
    > >>          self.data = "I was far from home and the spell of the eastern
    > >> sea was upon me."

    >
    > >>      def __getitem__(self,i):
    > >>          return self.data

    >
    > >> import time

    >
    > > import sys

    >
    > >> if __name__ == "__main__":

    >
    > >>      me = TestClass()
    > >>      for x in me:
    > >>          print(x,end='')  #if i remove the 'end='''  it performs as i'd
    > >> expect

    > >            sys.stdout.flush()

    >
    > >>          time.sleep(int(2))

    >
    > > It may just be that the output is being held in buffers until the
    > > program terminates. the fluch() methof pushes it straight out.

    >
    > The IDLE Shell window must get print output back from the pythonw
    > process without buffering. When I cut and pasted from IDLE editor to
    > standard interpreter window, output was bunched after several seconds.
    > OP should have started with short string and minimal delay so output
    > would not take a whole minute. Flushing fixed problem in standard
    > interpreter. I will have to remember that testing partial line output in
    > IDLE does not show how it will act elsewhere.
    >
    > Kevin: when reporting a problem, be more specific as to what 'does not
    > work' means.
    >
    > Terry Jan Reedy


    Terry: Will definately try to be more informative next time. Thanks
    again.

    K
    Kevin Adams, Mar 26, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrey Tatarinov

    python3: 'where' keyword

    Andrey Tatarinov, Jan 7, 2005, in forum: Python
    Replies:
    97
    Views:
    1,777
    Andrey Tatarinov
    Jan 16, 2005
  2. Andrey Tatarinov

    Python3: on removing map, reduce, filter

    Andrey Tatarinov, Jan 9, 2005, in forum: Python
    Replies:
    18
    Views:
    1,210
    Steven Bethard
    Jan 11, 2005
  3. Replies:
    37
    Views:
    824
    Steve Holden
    Jan 14, 2005
  4. Helmut Jarausch

    python3 - the hardest hello world ever ?

    Helmut Jarausch, Oct 14, 2008, in forum: Python
    Replies:
    23
    Views:
    792
  5. Andrew Berg
    Replies:
    0
    Views:
    321
    Andrew Berg
    Jun 16, 2012
Loading...

Share This Page