Re: [Tutor] call key on_press event multiple times when key is held down

2017-07-04 Thread eryk sun
On Tue, Jul 4, 2017 at 8:50 AM, Carlton Banks  wrote:
> I am using pynput  for keyboard events

You could use an event that enables a recording loop. The on_press and
on_release callbacks of the Listener [1] would set() and clear() this
event, respectively. For example:

import threading
from pynput import keyboard

def main():
do_record = threading.Event()

def on_press(key):
if key == keyboard.Key.cmd_l:
do_record.set()

def on_release(key):
if key == keyboard.Key.cmd_l:
do_record.clear()

with keyboard.Listener(on_press=on_press,
   on_release=on_release) as listener:

do_record.wait()

frames = []

while do_record.is_set():
print('Started recording')
# record and append audio frame

print('Stopped recording')
listener.join()

[1]: http://pythonhosted.org/pynput/keyboard.html#pynput.keyboard.Listener
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Re: call key on_press event multiple times when key is held down

2017-07-04 Thread Carlton Banks
> 
>> Interesting solution, but still find a bit "dirty hackish”
>> to involve a timer in this..  I guess it just would be neat if 
>> it just worked as i thought it to be.  But i guess i have to look into 
>> curses.
> 
> A timer based loop is actually the standard pattern for
> implementing a loop in an event driven environment (the
> main alternative is to launch a background thread).
> It's how most GUIs handle such scenarios.
> 
> The problem in a CLI solution is that you have to build your
> own timer event system (usually based on time.sleep() or
> similar). Hence the suggestion to use a GUI.
> 

Any suggestion on any GUI solutions?


Regards 
Carl

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Re: call key on_press event multiple times when key is held down

2017-07-04 Thread Alan Gauld via Tutor
On 04/07/17 12:00, Alan Gauld via Tutor wrote:

>> the library you are using, I normally just use the standard
>> library for such things, or build a simple GUI…
> 
> Standard library being?.. 

The Python standard library that ships with Python and
is documented on python.org. As in:

>> There are several but it depends on your OS.
>> In the standard library
>> Unix variants can use curses.
>> Windows users have msvcrt.


> Interesting solution, but still find a bit "dirty hackish”
> to involve a timer in this..  I guess it just would be neat if 
> it just worked as i thought it to be.  But i guess i have to look into curses.

A timer based loop is actually the standard pattern for
implementing a loop in an event driven environment (the
main alternative is to launch a background thread).
It's how most GUIs handle such scenarios.

The problem in a CLI solution is that you have to build your
own timer event system (usually based on time.sleep() or
similar). Hence the suggestion to use a GUI.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Re: call key on_press event multiple times when key is held down

2017-07-04 Thread Alan Gauld via Tutor
Forwarding to Tutor list


Please always use reply-All or reply-List to respond to tutor posts.



 Forwarded Message 

> Den 4. jul. 2017 kl. 11.35 skrev Alan Gauld via Tutor :
> 
> On 04/07/17 09:50, Carlton Banks wrote:
>> I am trying to record from my microphone while i press down a button,
> 
> First question is which OS?
> That makes a big difference in this kind of scenario.

I am currently testing it on OS X, but should also be working on a ubuntu 
machine.  

>> the library I am using is not able to detect on hold event. 
> 
> There isn't really any concept of a hold event, you usually
> have to detect repeated press events. However I don;t know
> the library you are using, I normally just use the standard
> library for such things, or build a simple GUI…

Standard library being?.. 

> 
>> It only detect on press, which happens once, 
> 
> That's unusual, usually you get a repeated stream of
> press events when you hold a key down. But it does
> depend on the OS and environment. Your library may
> indeed just be detecting the initial key down and
> be waiting for the key up event.

Hmm.. interesting but again it is currently being tested on OSX,
as it was the only machine available atm. 
> 
>> So does any of you know any libraries that monitor> state of the keyboard,
> 
> There are several but it depends on your OS.
> In the standard library
> Unix variants can use curses.
> Windows users have msvcrt.
> 
> And there are several 3rd party libraries that try
> to do in a platform independent way - I'm guessing
> the one you are using is one such.
> 
> Another way to tackle it is to switch your code so
> it works with a toggle. Set the toggle to on in
> the on_press handler. Set it to off in the
> on_release handler
> 
> Then trigger a timer loop that runs for as long
> as the toggle is set. This is probably easiest
> done within a GUI.
> 

Interesting solution, but still find a bit "dirty hackish”
to involve a timer in this..  I guess it just would be neat if 
it just worked as i thought it to be.  But i guess i have to look into curses.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Python2.7] Convert (2,188,1) into (188,1)

2017-07-04 Thread Albert-Jan Roskam


From: Tutor [tutor-bounces+sjeik_appie=hotmail@python.org] on behalf of 
Alan Gauld via Tutor [tutor@python.org]
Sent: Tuesday, July 4, 2017 8:37 AM
To: tutor@python.org
Subject: Re: [Tutor] [Python2.7] Convert (2,188,1) into (188,1)

On 28/06/17 16:48, Allan Tanaka via Tutor wrote:
> Hi. I have array shape like: (2,188,1).

I assume this is a numpy array rather than the standard
library array? If so consider asking on the scipy/numpy
support forum for more specialised help.

> I want to make it like this: (188,1). I try that
> using .reshape(188,1)

Assuming this is a numpy array: If you want a 1D array you can use ravel 
(faster) or flatten (better name IMHO): 
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel.html
arr.reshape(2*188, 1) may also work.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] call key on_press event multiple times when key is held down

2017-07-04 Thread Alan Gauld via Tutor
On 04/07/17 09:50, Carlton Banks wrote:
> I am trying to record from my microphone while i press down a button,

First question is which OS?
That makes a big difference in this kind of scenario.

> the library I am using is not able to detect on hold event. 

There isn't really any concept of a hold event, you usually
have to detect repeated press events. However I don;t know
the library you are using, I normally just use the standard
library for such things, or build a simple GUI...

> It only detect on press, which happens once, 

That's unusual, usually you get a repeated stream of
press events when you hold a key down. But it does
depend on the OS and environment. Your library may
indeed just be detecting the initial key down and
be waiting for the key up event.

> So does any of you know any libraries that monitor> state of the keyboard,

There are several but it depends on your OS.
In the standard library
Unix variants can use curses.
Windows users have msvcrt.

And there are several 3rd party libraries that try
to do in a platform independent way - I'm guessing
the one you are using is one such.

Another way to tackle it is to switch your code so
it works with a toggle. Set the toggle to on in
the on_press handler. Set it to off in the
on_release handler

Then trigger a timer loop that runs for as long
as the toggle is set. This is probably easiest
done within a GUI.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] call key on_press event multiple times when key is held down

2017-07-04 Thread Carlton Banks
I am trying to record from my microphone while i press down a button, problem 
is that the library I am using is not able to detect on hold event. It only 
detect on press, which happens once, which means that the microphone only 
records one sample..

import pyaudio
import wave
from pynput import keyboard

CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK) 
frames = []

def on_press(key):
if key == keyboard.Key.cmd_l:
print('- Started recording -'.format(key))
try:
data = stream.read(CHUNK)
frames.append(data)
except IOError: 
print 'warning: dropped frame' # can replace with 'pass' if no 
message desired 
else:
print('incorrect character {0}, press cmd_l'.format(key))


def on_release(key):
print('{0} released'.format(
key))
if key == keyboard.Key.cmd_l:
print('{0} stop'.format(key))
keyboard.Listener.stop
return False

print("* recording")


with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
I am using pynput  for keyboard events and 
pyaudio for recording. I am pretty sure that this should be the error, as the 
example script for recording, picks up samples in a for loop, which works. 

So does any of you know any libraries that monitors state of the keyboard, or 
any ideas on how to get this working?.. I tried with a while look inside the 
on_press callback but that resulted in me getting stuck that callback.


How can i fix this?


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Not returning out the series

2017-07-04 Thread Sibylle Koczian

Am 04.07.2017 um 04:40 schrieb Rafael Skovron:

Hi as a challenge I have got to sum a series i / (i+1). My code isn't
summing right. Any ideas why?


def main():
 print("{0:15s}{1:20s}".format("i","m(i)"))
 for i in range(1,20):
 print("{0:<15d}{1:<20.4f}".format(i,m(i)))

def m(i):
 total = 0
 for i in range(1,i+1,1):
 total+=(i/(i+1))
 return total


Python 2 or Python 3? What sums do you get and what sums do you expect?

BTW in your function m(i) it's very confusing that you use the name i 
both for the range limit and for the loop variable. It seems to be 
syntactically correct, but it's certainly not readable.


HTH
Sibylle

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Python2.7] Convert (2,188,1) into (188,1)

2017-07-04 Thread Alan Gauld via Tutor
On 28/06/17 16:48, Allan Tanaka via Tutor wrote:
> Hi. I have array shape like: (2,188,1). 

I assume this is a numpy array rather than the standard
library array? If so consider asking on the scipy/numpy
support forum for more specialised help.

> I want to make it like this: (188,1). I try that 
> using .reshape(188,1) 

.reshape() is not a valid function name. What object
are you attaching it to?

Show us at least a code snippet so we don't need
to guess. We need to see how you create the initial
array, then how you try to use reshape. Ideally a short
but complete example that exhibits the problem.

> but throws an error: total size of an array must be unchanged

Show us the full error trace, they contain a lot of information.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Not returning out the series

2017-07-04 Thread Alan Gauld via Tutor
On 04/07/17 03:40, Rafael Skovron wrote:

> def m(i):
> total = 0
> for i in range(1,i+1,1):
> total+=(i/(i+1))
> return total

convert the numerator to a float. Otherwise you
are using integer division. (I'm guessing you
are running Python 2.7?)

You could also import division from future:

from __future__ import division

Alternatively you could try running Python v3.

PS, Its probably not a great idea to use i as both
the iteration variable and the input parameter.
It easily causes confusion.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Not returning out the series

2017-07-04 Thread Rafael Skovron
Hi as a challenge I have got to sum a series i / (i+1). My code isn't
summing right. Any ideas why?


def main():
print("{0:15s}{1:20s}".format("i","m(i)"))
for i in range(1,20):
print("{0:<15d}{1:<20.4f}".format(i,m(i)))

def m(i):
total = 0
for i in range(1,i+1,1):
total+=(i/(i+1))
return total
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Python2.7] Convert (2,188,1) into (188,1)

2017-07-04 Thread Ashfaq
Is it a 3-item tuple?

On Wed, Jun 28, 2017 at 9:48 PM, Allan Tanaka via Tutor 
wrote:

> Hi. I have array shape like: (2,188,1). I want to make it like this:
> (188,1). I try that using .reshape(188,1) but throws an error: total size
> of an array must be unchanged
> Thank you for the help!
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor