Sommer, Matthias | 1 Apr 10:53 2008

simple, probably stupid question: single key in console / idle

Hello,
 
I'm just starting programming in Python. ATM I do write some smaller practices. In them I want to read single keys, if pressed. If none pressed I do not want to wait.
 
In the library reference I found "kbhit()" and "getch()" from msvcrt. I import msvcrt (running on Windows) but I cant get it to run. Is ther somewhere a sample I could peek on?
And is there somthing not windows specific?
 
My trial code is:
 

import msvcrt
 
def kb():
    while( 1 ):
        if msvcrt.kbhit():
            print "key seen"
            help = msvcrt.getch()
            #if( help < 255 ):
            print help, ord( help )
            if( ord( help ) == 27 ):
                print "cancel"
                break
 
It appears to never see a key.


Mit freundlichen Grüßen
Matthias Sommer
Entwicklung - Firmware/Elektronik
______________________________________________

Wincor Nixdorf Technology GmbH
Am Vogelherd 67
D – 98693 Ilmenau, Germany

Tel.:        +49 (0) 36 77 862-194
Fax:        +49 (0) 36 77 862-199
E-Mail:    matthias.sommer <at> wincor-nixdorf.com
               www.wincor-nixdorf.com

 
Wincor Nixdorf Technology GmbH
Sitz der Gesellschaft: Paderborn
Registergericht Paderborn HRB 3523
Geschäftsführer: Eckard Heidloff, Jürgen Wilde, Wolfgang Keller
Steuernummer: 339/5884/0020 - Ust-ID Nr.: DE243233085

Diese E-Mail enthält vertrauliche Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

This e-mail may contain confidential information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Evans Anyokwu | 1 Apr 14:42 2008
Picon

Testing 321

Please ignore, switching email address.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Alan Gauld | 1 Apr 14:43 2008

Re: simple, probably stupid question: single key in console / idle


"Sommer, Matthias" <Matthias.Sommer <at> wincor-nixdorf.com> wrote

> In the library reference I found "kbhit()" and "getch()" from 
> msvcrt.
> I import msvcrt (running on Windows) but I cant get it to run.
> Is ther somewhere a sample I could peek on?

You can look at the event handling topic in my tutor.
It goves an example of reading keys with getch()

--

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Michael Langford | 1 Apr 19:30 2008

Re: Interactive plots...

If you're just talking about something like this (ascii art) drawing:

--------------------------------------------------------------------------------------------
1:*          * * * * * * * *                    *

0:  * * * *                    * * * * * * * *   * * * * * * *
---------------------------------------------------------------------------------------------

Then I think you have a relatively simple alternative for a UI:

Create a scrollable UI in the toolkit of your choice. It scrolls left to right. Make a series of radiobutton selections with all of the UI components matching the background color. That way you only get the actual clickable dots. Then your users can click on the dot to move it to the opposite position (by overriding the on-click handler).

A rough tkinter solution without scrolling, the selection buttons removed, or the switching logic follows this reply. You can make the following nicer by rewriting the click command, and you can probably find a way to make tkinter hide the empty circles or use a different toolkit to implement the same idea. You should be able to put an X axis in to show time or whatever dimension your data is over.

     --Michael

from Tkinter import *

def createDataPoint(master,var):
  rb1 =Radiobutton(master,borderwidth=0,foreground='red',background='white',variable=var,value=1)
  rb1.grid()
  rb2=Radiobutton(master,borderwidth=0,foreground='red',background='white',variable=var,value=0)
  rb2.grid()

class Application(Frame):             
    def __init__(self, master=None,data=[1]):
        Frame.__init__(self, master)  
        self.grid()                   
        self.data = data
        self.createWidgets()

    def show(self):
        l = []
        for each in self.ivlist:
           l.append(each.get())
        print(l)

    def createWidgets(self):
        self.showButton = Button ( self, text="Show",
            command=self.show )       
        self.showButton.grid(row=0)
        self.ivlist = []  
        for i in range(len(self.data)):
            iv = IntVar(value=self.data[i])
            self.ivlist.append(iv)
            f = Frame(self)
            f.grid(column=i+1,row=1)
            createDataPoint(f,iv)

           



if "__main__"==__name__:
  data = [1,0,1,0,1,1,1,1,0,1]
  app = Application(data=data)                   
  app.master.title("Flippin DOTS")
  app.mainloop()               






On Thu, Mar 27, 2008 at 6:54 PM, David Perlman <dperlman <at> wisc.edu> wrote:
I am thinking about writing a program which will involve, among other
things, displaying a plot of a series of numbers.  The idea is that
you could click on the points and move them to change the numbers.
Reverse-plotting, I suppose.  It need not be complex; the numbers
will all be zero or one, and it's only necessary to flip the bits, so
click-and-drag is seriously overkill.  Really it would be better to
just double-click on a point to switch it from one value to the other.

Can anyone point me in the right direction?  I have written some
programs in python before, including TKinter, but this new project is
beyond the point that I know where to even start looking.  :)

In case you care, the application is in functional brain imaging; the
brain scans generate a certain number of time points (say 500) and
then the motion of the subject is also calculated.  Standard practice
is to generate a "censor" file composed of zeros and ones, where zero
indicates that that time point had excessive motion and must be
disregarded.  I want to display a graph of the motion over time, and
allow quick and easy interactive editing of the censor time series in
visual parallel to the motion graph.  This would save a lot of time;
at present everyone does this in Excel, which being a horrible
Windows program can't be integrated into the predominantly UNIX-based
processing pipeline.  And in any case, it requires manually typing
all the zeros, looking back and forth between the graph of motion and
the list of numbers.

I have already written a program to algorithmically generate the
censor time series from the motion data, but it is absolutely
essential to be able to manually double-check and if necessary make
minor edits.  I'd like to be able to keep that functionality in
Python rather than sending everyone back to Excel...  if possible!

Thanks very much for any help.

--
-dave----------------------------------------------------------------
"Pseudo-colored pictures of a person's brain lighting up are
undoubtedly more persuasive than a pattern of squiggles produced by a
polygraph.  That could be a big problem if the goal is to get to the
truth."  -Dr. Steven Hyman, Harvard



_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor



--
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Michael Langford | 1 Apr 19:51 2008

Re: Problem with logic while extracting data from binary file

I tried to extract methods, and invert your logic of your if/else statements as to put shorter code blocks higher, and tried to roll many of your if statements into the checks of the while loops. This is algorithm I ended up with. Is this what you were trying to do? The last while loop is pointless, as you unconditionally return from it.

    --Michael

def basicFields(data,start):
    group_num = data[start:start+2]
    element_num = data[start+2:start+4]
    vl_field = data[start+4:start+8]
    length = struct.unpack('hh', vl_field)[0]
    value = data[start+8:(start+8+length)]
    start = start+8+length
    element = group_num+element_num
   
    return (group_num,element_num,vl_field,length,value,start,element)

def parseSequence(data, start):
    group_num,element_num,vl_field,length,value,pos,element = basicFields(data,start)

    MY_SEQ = '\xfe\xff\x00\xe0'

    while start < 536: #length:  # 536
        group_num,element_num,vl_field,length,value,start,element = basicFields(data,start)
        if element != MY_SEQ:
            return element, start, value               
        else:
            data = value
            while start < 112: #length:  # 112, 112, 116, 116
                group_num,element_num,vl_field,length,value,start,element = basicFields(data,start)
                return element, start, value


On Fri, Mar 28, 2008 at 4:24 PM, Bryan Fodness <bryan.fodness <at> gmail.com> wrote:

Thanks again,
 
Still lost, even with watching the variables.  I see that it kicks out of the loop, but don't understand what I have done to cause this.  I'm sorry for repeated emails, but I have spent multiple days on this.  I have added another while loop that I think should work, but I can't seem to keep it in the while loop.  I feel like I am getting close.
 
It seems like it gets everything at the first level , but not the way I expected.  It seems to get the first value inside the first while loop, and then goes outside the loop to get the next three.  I would have expected it to return the values as it goes through the first while loop (since they are at the same level), then when it sees the nested identifier, go into the second while loop and return values.
 
Any insight would be wonderful.
 
def parseSequence(data, start):
    group_num = data[start:start+2]
    element_num = data[start+2:start+4]
    vl_field = data[start+4:start+8]
    length = struct.unpack('hh', vl_field)[0]
    value = data[start+8:(start+8+length)]
    pos = start+8+length
    element = (group_num+element_num)
    if element == '\xfe\xff\x00\xe0':
        data = value
        while start < 536: #length:  # 536

            group_num = data[start:start+2]
            element_num = data[start+2:start+4]
            vl_field = data[start+4:start+8]
            length = struct.unpack('hh', vl_field)[0]
            value = data[start+8:(start+8+length)]
            start = start+8+length

            element = (group_num+element_num)
            if element == '\xfe\xff\x00\xe0':
                data = value
                while start < 112: #length:  # 112, 112, 116, 116

                    group_num = data[start:start+2]
                    element_num = data[start+2:start+4]
                    vl_field = data[start+4:start+8]
                    length = struct.unpack('hh', vl_field)[0]
                    value = data[start+8:(start+8+length)]
                    start = start+8+length

                    element = (group_num+element_num)
                    return element, start, value
            else:
                return element, start, value                

    else:
        return  element, pos, value

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor




--
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
linuxian iandsd | 1 Apr 23:07 2008
Picon

Re: parse emails as they come in

well, my script is so simple ! nothing complicated

#!/usr/bin/python
#

import re, sys

a=open('/home/john/data/file_input.tmp', 'r')
b=open('/home/john/data/file_output', 'w')

aa=a.readlines()
n=0
for L in aa:
# I split every line because i only need what's after the ":"
# the email comes in the form "field : value"  in  17 lines
 La=L.split(':')   
 n=n+1
# 18 is the last line & it is an empty line that comes with every email
# so i quit there.
 if n==18:
  sys.exit()
# second line is a time value like this one "18:20:45"
# i don't need the ":" but i need the numbers
 elif n==2:
# as usual i remove the \n & put a ; in its place and that happens
# at the third element
  La3=re.sub('\n',';',La[3])
# i gather the time value also there is no need for : in between
  La123=La[1]+La[2]+La3
  b.write(La123)
# any other line is treated equaly like this
# i only replace \n by ;
 else:
  La1=re.sub('\n',';',La[1])
  b.write(La1)

# a little secret : this little script helps me load data from mail to a mysql database by converting it into ; separated values :)




On Tue, Apr 1, 2008 at 2:09 AM, Chris Fuller <cfuller084 <at> thinkingplanet.net> wrote:
Every five lines of the raw email, the headers, or the body?

A text file is just data.  You can navigate however you like, but you need to
choose a model, to give it some structure for you to work with,  Navigating
around at the byte level is probably going to be tedious, error prone, and
not very useful anyway.  Choosing five lines at a time is probably not going
to be much better.  There's no particular reason it can't be ten lines, or
two, unless you pick a model that

Maybe we could help more if you showed us what this "original script" is.  We
can help you pick a better model if the one implicit in your script isn't
working for you.

Also, you should probably reply to the mailing list.
I'll be more careful about the reply-to field from now on.

Cheers


On Monday 31 March 2008 14:43, you wrote:
> the mail module seems interesting.  but what  I was thinking of some way
> that would work on only five lines then moves to the next five lines & so
> on ... is that possible ? is there a way of navigating a text file ?
> process the line that we want,  maybe delete it or maybe add text to it &
> then save & close the file ?
>
>
> On Fri, Mar 28, 2008 at 1:08 PM, Chris Fuller
> <cfuller084 <at> thinkingplanet.net>
>
> wrote:
> > The email and mailbox modules might help you out.  Multiple email
> > messages will probably parse as an mbox format mailbox.
> >
> > http://docs.python.org/lib/module-email.html
> > http://docs.python.org/lib/module-mailbox.html
> >
> > Cheers
> >
> > On Friday 28 March 2008 03:14, linuxian iandsd wrote:
> > > good morning everybody !
> > >
> > > I have scripted a small program to parse a 5 lines email message as it
> > > comes in to my inbox (this is handled by procmail & here is a wonderful
> > > intro to it : http://linuxfocus.org/English/November1997/article8.html)
> > >
> > > so every email is being parsed & information is extracted from it.
> > >
> > > but sometimes two or more emails come in at once so the input file that
> >
> > my
> >
> > > python script has to parse is more than five lines !! my question is
> > > how
> >
> > do
> >
> > > i effeciently manage this from within my original script.
> > >
> > > thanks
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor <at> python.org
> > http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Alan Gauld | 1 Apr 22:29 2008

Re: simple, probably stupid question: single key in console / idle


"Alan Gauld" <alan.gauld <at> btinternet.com> wrote

>> I import msvcrt (running on Windows) but I cant get it to run.
>> Is ther somewhere a sample I could peek on?
> 

I just noticed the bit about IDLE in the subject.
msvcrt only works in a DOS console it won't work in 
IDLE because IDLE is detecting the keyboard events 
as GUI events within Tkinter. You will need to run 
your code in a command console not IDLE.

--

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Steve Willoughby | 1 Apr 23:17 2008

Re: parse emails as they come in

On Tue, Apr 01, 2008 at 09:07:04PM +0000, linuxian iandsd wrote:
> a=open('/home/john/data/file_input.tmp', 'r')
> b=open('/home/john/data/file_output', 'w')

This is collecting mail as it comes in?  If you have a mail
rule in place to dump mail into this file_input.tmp file,
you could run into trouble if multiple messages arrive close
enough together that you get a race condition.

I'd suggest just using something like procmail to invoke
your Python script directly on the incoming message, so
you don't have to dump it to a temporary input file.  
You'll be guaranteed to see one and only one mail per
invocation of your script (although it may invoke 
several copies of your script at the same time, so plan
for that, e.g., don't write to the same output filename
every time--or don't write to a file at all, just have
your script put the data into MySQL or whatever directly).

> aa=a.readlines()
> n=0
> for L in aa:

Generally speaking, it's better to let Python iterate 
through the lines of a file.  The above code sucks in
the entire (possibly huge) file into memory and then
iterates over that list.  Better:

for L in a:

or better yet:

for lines in input_file:

> # a little secret : this little script helps me load data from mail to a
> mysql database by converting it into ; separated values :)

I'd look at just gathering the raw data into Python variables and then
connecting to MySQL directly and executing a SQL statement to import the
data straight in.  You'll avoid a host of problems with properly quoting
data (what if a ';' is in one of the data fields?), as well as making it
unnecessary to carry out another post-processing step of gathering this
script's output and stuffing it into MySQL.

--

-- 
Steve Willoughby    |  Using billion-dollar satellites
steve <at> alchemy.com   |  to hunt for Tupperware.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

wesley chun | 1 Apr 23:46 2008
Picon

[ANN] May 2008 Python course

* apologies if you receive cross-posted duplicates *

FINAL REMINDER

Need to get up-to-speed with Python as quickly as possible? Come join
me, Wesley Chun, author of Prentice-Hall's well-received "Core Python
Programming," for another comprehensive intro course next month in
beautiful Northern California! I look forward to meeting you!  If you
miss this one, our next courses will likely be in Oct or Nov 2008.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(COMPREHENSIVE) INTRODUCTION TO PYTHON: Mon-Wed, 2008 May 5-7

Although this course may appear to those new to Python, it is also
perfect those who have tinkered with it and want to "fill in the gaps"
and/or want to get more in-depth formal training.  It combines the
best of both an introduction to the language as well as a "Python
Internals" training course.

We will immerse you in the world of Python in only a few days.  We
will show you more than just its syntax (which you don't really need a
book to learn, right?). Knowing more about how Python works under the
covers, including the relationship between data objects and memory
management, will make you a much more
effective Python programmer coming out of the gate. 3 hands-on labs
each day will help hammer the concepts home.

Come find out why Google, Yahoo!, Disney, ILM/LucasFilm, VMware, OLPC,
NASA, Ubuntu, YouTube, and Red Hat all use Python. Users supporting or
jumping to Plone, Zope, TurboGears, Django, Pylons, Jython,
IronPython, and Mailman will also benefit!

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

WHERE: near the San Francisco Airport (SFO/San Bruno), CA, USA

WEB:   http://cyberwebconsulting.com (click "Python Training")

LOCALS: easy freeway (101/280/380) with lots of parking plus public
transit (BART and CalTrain) access via the San Bruno stations, easily
accessible from all parts of the Bay Area

VISITORS: free shuttle to/from the airport, free high-speed internet,
free breakfast and regular evening receptions; fully-equipped suites

See website for costs, venue info, and registration.  Discounts are
 available for multiple registrations as well as for teachers/students.

Hope to see you there!
-- wesley

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
 http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
Guess?!? | 2 Apr 01:29 2008
Picon

Sync between Powerpoint slides and Video

Hello All,

I recently came across couple of websites (www.parleys.com and www.zentation.com) and loved the sync technology between ppt and streaming video. This makes website experience look fast and impressive. I was just curious to know the how is it all done. What technology/software is being used to sync powerpoint presentations with video?
If anyone has delved into implementing this in Python or Zope/Plone frameworks or any other technology ...Please share the knowledge ....

Any insight will be appreciated ...

Thanks,
G Arora.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Gmane