[Tutor] Help with List

2015-07-26 Thread Anurag Vyas
Hello Team,
I am noob to programming. I am trying to remove a item from a list. The
item that I am trying to remove has multiple entries in the list. I am not
able to remove all the entries.
Please Help. The code is in the attachment.

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


Re: [Tutor] Help with List

2015-07-26 Thread Steven D'Aprano
On Sun, Jul 26, 2015 at 10:04:19AM +0530, Anurag Vyas wrote:
> Hello Team,
> I am noob to programming. I am trying to remove a item from a list. The
> item that I am trying to remove has multiple entries in the list. I am not
> able to remove all the entries.
> Please Help. The code is in the attachment.

There is no attachment. Perhaps you forgot to attach it?

In general, if your code is less than 20 or 30 lines, you should just 
include it in the body of your email. If your code is more than 30 
lines, you should simplify it so that it is smaller.

To delete a single item from a list:

alist = ['a', 'b', 'c', 'd', 'e']
alist.remove('b')

To delete multiple entries with the same value:


alist = ['a', 'b', 'c', 'b', 'd', 'b', 'e', 'b', 'b']
try:
while True:
alist.remove('b')
except ValueError:
pass


but this will probably be more efficient:

alist = ['a', 'b', 'c', 'b', 'd', 'b', 'e', 'b', 'b']
alist = [item for item in alist if item != 'b']


That last one is called a "list comprehension", and is a short cut for a 
for-loop like this:


tmp = []
for item in alist:
if item != 'b':
tmp.append(item)

alist = tmp




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


Re: [Tutor] How to generate a pure tones and random noise using Python?

2015-07-26 Thread Paul Z
Hi Laura,

Thanks for you helpful reply, and it is exactly what I want.

I'm going to learn Python from write a ears training program. ;-)
And Is there have a library to scope(print) this wave form? (real-time and non 
real-time)

I have already started to learn "generate audio with python", Thank you. :-)

Paul Z


> To: p...@whoosh.cn
> CC: tutor@python.org; l...@openend.se
> From: l...@openend.se
> Subject: Re: [Tutor] How to generate a pure tones and random noise using 
> Python?
> Date: Sat, 25 Jul 2015 22:35:24 +0200
>
> In a message of Sat, 25 Jul 2015 19:15:31 +0800, Paul Z writes:
>>Hi All,
>>
>>I try to train my listening by using python. (estimating the frequency of 
>>sound)
>>So... Are there some way to generate a fixed frequency sound in different 
>>waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and different random 
>>noise. (eg. white noise & pink noise) ?
>>
>>I have search in web, some people say that I can use winsound which can 
>>generate a fixed frequency beep. However, It is Windows only. (I'm under 
>>Linux) and I think It is not a professional library to generate audio signal.
>>
>>How about pygame? and some examples?
>>any ideas?
>>
>>Thanks
>>
>>Paul Z
>>___
>>Tutor maillist - Tutor@python.org
>>To unsubscribe or change subscription options:
>>https://mail.python.org/mailman/listinfo/tutor
>
> I think you want this:
> https://zach.se/generate-audio-with-python/
> https://github.com/zacharydenton/wavebender
>
> pygame will not give you want you want.
>
> blender might. I am not sure but worth googling for.
>
> Laura
>
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to generate a pure tones and random noise using Python?

2015-07-26 Thread Paul Z
Hi Laura,

I just have read this webpage about Generate Audio with Python. 
It seem writing by Python 2? Esp. itertools (izip, imap).
I try to change izip and imap to zip and map, but I get error:

beatfreq, amplitude = remainder.split('/')
ValueError: need more than 1 value to unpack

Are there some way to conver it to the code of Python 3?

I'm too anxious to hear the sound from Python though I'm a absolute beginner.

Many thanks

Paul Z


> From: p...@whoosh.cn
> To: l...@openend.se
> Date: Sun, 26 Jul 2015 19:29:19 +0800
> Subject: Re: [Tutor] How to generate a pure tones and random noise using 
> Python?
> CC: tutor@python.org
>
> Hi Laura,
>
> Thanks for you helpful reply, and it is exactly what I want.
>
> I'm going to learn Python from write a ears training program. ;-)
> And Is there have a library to scope(print) this wave form? (real-time and 
> non real-time)
>
> I have already started to learn "generate audio with python", Thank you. :-)
>
> Paul Z
>
> 
>> To: p...@whoosh.cn
>> CC: tutor@python.org; l...@openend.se
>> From: l...@openend.se
>> Subject: Re: [Tutor] How to generate a pure tones and random noise using 
>> Python?
>> Date: Sat, 25 Jul 2015 22:35:24 +0200
>>
>> In a message of Sat, 25 Jul 2015 19:15:31 +0800, Paul Z writes:
>>>Hi All,
>>>
>>>I try to train my listening by using python. (estimating the frequency of 
>>>sound)
>>>So... Are there some way to generate a fixed frequency sound in different 
>>>waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and different random 
>>>noise. (eg. white noise & pink noise) ?
>>>
>>>I have search in web, some people say that I can use winsound which can 
>>>generate a fixed frequency beep. However, It is Windows only. (I'm under 
>>>Linux) and I think It is not a professional library to generate audio signal.
>>>
>>>How about pygame? and some examples?
>>>any ideas?
>>>
>>>Thanks
>>>
>>>Paul Z
>>>___
>>>Tutor maillist - Tutor@python.org
>>>To unsubscribe or change subscription options:
>>>https://mail.python.org/mailman/listinfo/tutor
>>
>> I think you want this:
>> https://zach.se/generate-audio-with-python/
>> https://github.com/zacharydenton/wavebender
>>
>> pygame will not give you want you want.
>>
>> blender might. I am not sure but worth googling for.
>>
>> Laura
>>
>
> ___
> 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


Re: [Tutor] Socket Module

2015-07-26 Thread Martin A. Brown


Hello Nym,


Here is the updated code: https://bpaste.net/show/358583e1a0bd


It's short.  I have included inline here:

  import socket

  ListOfIPAddresses = []

  with open('top500ips.csv', 'r') as f:
  for line in f:
  line = line.strip()
  ListOfIPAddresses.append(line)

  for address in ListOfIPAddresses:
  try:
  ResolvedAddresses = socket.gethostbyaddr(address)
  except:
  print('No Resolution Available')

  print(ResolvedAddresses)

The issue that I am running into now is that for some reason, the 
script is not resolving known-public IP addresses that I am 
passing through. For example, some of the IPs, that I have used 
are for sites like facebook (173.252.120.6) github 
(207.97.227.239), however the script is not able to resolve them.


But its interesting that I am able to resolve them using nslookup 
on windows command prompt. Which means my laptop's DNS setting is 
fine.


The apparent (?) DNS lookup failure
---
At time X, you run your Python program and something (perhaps in the 
DNS resolution process) fails and you see "No Resolution Available", 
but you do not know what has failed, nor for which address lookup.


At time Y, you run 'nslookup' at the shell prompt, receive an answer 
and conclude that your script is operating properly.  While this is 
may appear logical, it is an incorrect conclusion.


One coding error (a dangerous habit to perpetuate)
--
When performing the DNS lookup, you are using something called 
a 'bare except'.  This will catch any and all errors, even if it's 
something unrelated like, for example, a signal.  This is a bad and 
dangerous habit.  In general, you should catch only the exceptions 
that you can do something about.


In addition, this will offer you more information about the problem. 
Here's a simple example, where I'm only changing two lines:


  for address in ListOfIPAddresses:
  try:
  ResolvedAddresses = socket.gethostbyaddr(address)
  except socket.herror as e:
  print("No resolution available for %s: %s" % (address, e))

This will give you a little more information about what, 
specifically, the failure is in your call to socket.gethostbyaddr()


Comment on NXdomain responses
-
I picked at random an address that had no PTR record and tried to 
call socket.gethostbyaddr('8.97.227.2').  What do you think I got 
for my trouble?  When running through the code block above, I saw 
the following output to my terminal:


  No resolution available for 8.97.227.2: [Errno 0] Resolver Error 0 (no error)

In short, there is no guarantee that anybody has properly set up 
reverse DNS entries (DNS PTR records) for the addresses you are 
looking up.  Although the vast majority of lookups occur 
successfully and smoothly, there are many things that can go wrong 
in the network and on an end host which can cause transient errors 
during DNS lookups, and it is possible that you have already 
encountered some of these problems (even though I would not expect 
to hit very many errors looking up PTR records for only 500 IPs).


May I wish you good luck resolving not just your addresses, but also 
your problem!


-Martin

--
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to generate a pure tones and random noise using Python?

2015-07-26 Thread Pablo Lucena
>
>
> I just have read this webpage about Generate Audio with Python.
> It seem writing by Python 2? Esp. itertools (izip, imap).
> I try to change izip and imap to zip and map, but I get error:
>
> ​​
> beatfreq, amplitude = remainder.split('/')
> ​​
> ValueError: need more than 1 value to unpack
>
> Are there some way to conver it to the code of Python 3?
>
> I'm too anxious to hear the sound from Python though I'm a absolute
> beginner.
>
> Many thanks
>
> Paul Z
>
>
​If you are using Python 3, replace calls to itertools.izip and
itertools.imap with simply "zip" and "map" respectively. Why not just use
Pyton2 instead, as there may be other things that will break.

​beatfreq, amplitude =
> ​​
> remainder.split('/')


​This throwing an exception means there is an issue with the data you are
trying to split, not necessarily a Py2 vs Py3 thing. What string is
assigned `remainder`?

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


Re: [Tutor] How to generate a pure tones and random noise using Python?

2015-07-26 Thread Alan Gauld

On 25/07/15 12:15, Paul Z wrote:

Are there some way to generate a fixed frequency sound in different

> waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and
> different random noise. (eg. white noise & pink noise) ?

While there are several options in Python (check out the
Activestate recipes) the simplest way on Linux may be to
create a file with the relevant signal and then play that
through aplay using the subprocess module.

One such module(by B Walker) that I found on Activestate
is this (slightly tweaked):

def sinebeep():
header=[ 82, 73, 70, 70, 100, 31, 0, 0, 87, 65, 86, 69,
 102, 109, 116, 32, 16, 0, 0, 0, 1, 0, 1, 0,
 64, 31, 0, 0, 64, 31, 0, 0, 1, 0, 8, 0, 100,
 97, 116, 97, 64, 31, 0, 0 ]
waveform=[ 79, 45, 32, 45, 79, 113, 126, 113 ]
wavefile=open("beep.wav", "w+")

for hdr in header:
wavefile.write(chr(hdr))
for sample in range(0, 1000, 1):
for wf in waveform:
wavefile.write(chr(wf))

wavefile.close()

if __name__ == '__main__':
sinebeep()

You can then play the resultant file with

$ aplay beep.wav  #you might need sudo

Its not very clever but you could create a series of beep
files each with a different frequency.

PS.
There is also a command line program called beep that does
what you want but it didn't work for me... YMMV


HTH

--
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