Re: [Tutor] Help with running an API

2014-10-26 Thread Anish Tambe
The usage of the api as documented here - https://github.com/zachwill/fred
- suggests :

>>> import fred

# Save your FRED API key.
>>> fred.key('my_fred_api_key')

# Interact with economic data categories.
>>> fred.category()
...

Cheers,
Anish Tambe
On 26 Oct 2014 00:23, "Joel Goldstick"  wrote:

> On Sat, Oct 25, 2014 at 12:08 PM, Mark Meanwell 
> wrote:
> > Hi Folks - new to python and trying to run an API. Running version
> 2.7.3. on
> > Windows 7 machine.
> >
> >  Here is the scenario for the given API (FRED API in this case):
> >
> > easy_install Fred from C:\ - this installs to C:\site packages
> >
> > then I fire up the python shell and run file created for fred api:
> >
> > from fredapi import Fred
> > fred = Fred(api_key='my api key')
> > data = fred.get_series('SP500')
> >
> > keep getting below error message:
> > ImportError: cannot import name Fred
> >
> > thanks for any suggestions!
>
> I'm not familiar with FRED, but a quick look on stackoverflow used the
> lowercase name to import
>
> >
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
>
>
>
> --
> Joel Goldstick
> http://joelgoldstick.com
> ___
> 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


[Tutor] if you're interested in the code thus far...

2014-10-26 Thread Clayton Kirkwood
__author__ = 'SYSTEM'

import string

#PricingDividends

raw_table = ('''

a: Asky: Dividend Yield

b: Bid d: Dividend per Share

b2: Ask (Realtime)   r1: Dividend Pay Date

b3: Bid (Realtime)q: Ex-Dividend Date

p: Previous Close

o: Open

Date

c1: Change  d1: Last Trade Date

c: Change & Percent Change   d2: Trade Date

c6: Change (Realtime)t1: Last Trade Time

k2: Change Percent (Realtime)

p2: Change in Percent

Averages

c8: After Hours Change (Realtime)   m5: Change From 200 Day Moving
Average

c3: Commission m6: Percent Change From 200 Day Moving Average

g: Day?s Low  m7: Change From 50 Day Moving Average

h: Day?s High m8: Percent Change From 50 Day Moving Average

k1: Last Trade (Realtime) With Timem3: 50 Day Moving Average

l: Last Trade (With Time)   m4: 200 Day Moving Average

l1: Last Trade (Price Only)

t8: 1 yr Target Price

Misc

w1: Day?s Value Change   g1: Holdings Gain Percent

w4: Day?s Value Change (Realtime)g3: Annualized Gain

p1: Price Paid g4: Holdings Gain

m: Day?s Rangeg5: Holdings Gain Percent (Realtime)

m2: Day?s Range (Realtime)   g6: Holdings Gain (Realtime)

52 Week PricingSymbol Info

k: 52 Week Highv: More Info

j: 52 week Low  j1: Market Capitalization

j5: Change From 52 Week Low   j3: Market Cap (Realtime)

k4: Change From 52 week High  f6: Float Shares

j6: Percent Change From 52 week Low   n: Name

k5: Percent Change From 52 week High n4: Notes

w: 52 week Range   s: Symbol

s1: Shares Owned

x: Stock Exchange

j2: Shares Outstanding

Volume

v: Volume

a5: Ask Size

b6: Bid Size Misc

k3: Last Trade Size   t7: Ticker Trend

a2: Average Daily Volumet6: Trade Links

i5: Order Book (Realtime)

Ratiosl2: High Limit

e: Earnings per Share l3: Low Limit

e7: EPS Estimate Current Yearv1: Holdings Value

e8: EPS Estimate Next Year  v7: Holdings Value (Realtime)

e9: EPS Estimate Next Quarter   s6 Revenue

b4: Book Value

j4: EBITDA

p5: Price / Sales

p6: Price / Book

r: P/E Ratio

r2: P/E Ratio (Realtime)

r5: PEG Ratio

r6: Price / EPS Estimate Current Year

r7: Price / EPS Estimate Next Year

s7: Short Ratio

''')

 

 

import re, string

col_position, code, description = 0, [], []

key_name = raw_table.replace('\t','\n')

for each_line in  key_name.splitlines():

if ':' in each_line:

c, d = each_line.split(':')

code.append(c)

description.append(d.strip())

print( col_position, code[col_position], description[col_position])

col_position += 1

 

output_line_len = 120

current_output_pos = index = 0

description_output_string = code_output_string = ''

for description_position, code_position in zip(description, code):

#for description_position in description:

#code_position = code[index]

description_position_len = len(description_position)

current_output_pos += description_position_len

#index += 1

if current_output_pos >= output_line_len:

print(description_output_string)

print(code_output_string)

code_output_string=description_output_string=''

current_output_pos=-1   #start new line

 

description_output_string += '{:^}|'.format(description_position)

code_output_string+='{0:^{1}}|'.format(code_position,
description_position_len)

current_output_pos+=1  #takes care of '|' at end of string

 

 

and a subset of output:

 

Ask|Dividend Yield|Bid|Dividend per Share|Ask (Realtime)|Dividend Pay
Date|Bid (Realtime)|Ex-Dividend Date|

a |  y   | b |d |  b2  |   r1|
b3  |   q|

Previous Close|Open|Change|Last Trade Date|Change & Percent Change|Trade
Date|Change (Realtime)|Last Trade Time|

  p   | o  |  c1  |  d1   |   c   |d2
|   c6|  t1   |

Change Percent (Realtime)|Change in Percent|After Hours Change
(Realtime)|Change From 200 Day Moving Average|Commission|

   k2|   p2| c8  |
m5|c3|

P

 

Spacing not correct on above output obviously!!

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


[Tutor] Python GPIO Code Help Needed

2014-10-26 Thread Bill Bright
I have been working on a piece of code that I got from another tutorial. The 
code polls the GPIO pins on a Raspberry Pi. When it detects a switch being 
flipped, it plays a corresponding audio file. My problem is, if the switch 
remains flipped, the audio repeats again and again. What I would like to do is 
when a switch is flipped have the audio play, then have the code pause until 
the switch is returned to normal (not repeating the audio), and then return to 
the while loop to poll for the next switch flip. I am apparently not smart 
enough to figure out the correct code. I really need some help with this and I 
would appreciate any assistance.

Here is the code I'm working with.

#!/usr/bin/env python
from time import sleep
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
while True:
if ( GPIO.input(23) == False ):
os.system('mpg321 -g 95 a.mp3')
if ( GPIO.input(24) == False ):
os.system('mpg321 -g 95 b.mp3')
if ( GPIO.input(25)== False ):
os.system('mpg321 -g 95 c.mp3')
sleep(1.1);
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if you're interested in the code thus far...

2014-10-26 Thread Alan Gauld

On 25/10/14 23:46, Clayton Kirkwood wrote:

__author__ = 'SYSTEM'


You are still setting __author__ which is a bit suspect.
Leave double underscores to python.


import string


You are still importing string twice, and you don't use it anywhere
that I can see.


#PricingDividends

raw_table = ('''


I assume this will eventually come from a file?
You are not really going to store it with your code?
If you are then using a string is pointless and causing
you lots of extra work.


a: Asky: Dividend Yield
b: Bid d: Dividend per Share
b2: Ask (Realtime)   r1: Dividend Pay Date
b3: Bid (Realtime)q: Ex-Dividend Date

...

s7: Short Ratio
''')


You don't need parens as well as triple quotes.
The quotes alone are sufficient.


import re, string


second string import... And you don't seem to be using re either?


col_position, code, description = 0, [], []
key_name = raw_table.replace('\t','\n')


I assume this is because you don't control the file format? Since 
otherwise you would just use newlines in the file, right?




for each_line in  key_name.splitlines():
 if ':' in each_line:
 c, d = each_line.split(':')
 code.append(c)
 description.append(d.strip())
 print( col_position, code[col_position], description[col_position])
 col_position += 1


You could use enumerate in the for loop and that would set the 
col_position value for you:


for col_position,each_line in enumerate(key_name.splitlines()):



output_line_len = 120
current_output_pos = index = 0
description_output_string = code_output_string = ''
for description_position, code_position in zip(description, code):


Why not just put the codes and descriptions in tuples when you read them 
in the loop above? Why use zip? In other words where you do


>  c, d = each_line.split(':')
>  code.append(c)
>  description.append(d.strip())

Why not just join the pair there:

>  c, d = each_line.split(':')
   values.append((c,d))

or even just

>  values.append(each_line.split(':'))

It seems as if you are splitting the values into two lists only
to zip those lists together again in the next loop?


 description_position_len = len(description_position)
 current_output_pos += description_position_len

 if current_output_pos >= output_line_len:
 print(description_output_string)
 print(code_output_string)
 code_output_string=description_output_string=''
 current_output_pos=-1   #start new line

 description_output_string += '{:^}|'.format(description_position)

 code_output_string+='{0:^{1}}|'.format(code_position,
description_position_len)

 current_output_pos+=1  #takes care of '|' at end of string


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] if you're interested in the code thus far...

2014-10-26 Thread Peter Otten
Clayton Kirkwood wrote:

> for description_position, code_position in zip(description, code):
> description_position_len = len(description_position)
> current_output_pos += description_position_len
> if current_output_pos >= output_line_len:
> print(description_output_string)
> print(code_output_string)
> code_output_string=description_output_string=''
> current_output_pos=-1   #start new line
> 
> description_output_string += '{:^}|'.format(description_position)
> code_output_string+='{0:^{1}}|'.format(code_position,
> description_position_len)
> 
> current_output_pos+=1  #takes care of '|' at end of string

If you want to ensure that the actual line length is never more than 
output_line_len -- the above doesn't do that.

Hint: reconsider this line:

> current_output_pos=-1   #start new line


> and a subset of output:

Some pairs are not printed. Can you figure out which and why?

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


Re: [Tutor] Python GPIO Code Help Needed

2014-10-26 Thread Dominik George
Hi,

>if ( GPIO.input(23) == False ):
>os.system('mpg321 -g 95 a.mp3')

while not GPIO.input(23):
pass

... would be the simplest solution.

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


Re: [Tutor] Python GPIO Code Help Needed

2014-10-26 Thread Dave Angel
Bill Bright  Wrote in message:
> I have been working on a piece of code that I got from another tutorial. The 
> code polls the GPIO pins on a Raspberry Pi. When it detects a switch being 
> flipped, it plays a corresponding audio file. My problem is, if the switch 
> remains flipped, the audio repeats again and again. What I would like to do 
> is when a switch is flipped have the audio play, then have the code pause 
> until the switch is returned to normal (not repeating the audio), and then 
> return to the while loop to poll for the next switch flip. I am apparently 
> not smart enough to figure out the correct code. I really need some help with 
> this and I would appreciate any assistance.
> 
> Here is the code I'm working with.
> 
> #!/usr/bin/env python
> from time import sleep
> import os
> import RPi.GPIO as GPIO
> GPIO.setmode(GPIO.BCM)
> GPIO.setup(23, GPIO.IN)
> GPIO.setup(24, GPIO.IN)
> GPIO.setup(25, GPIO.IN)
> while True:
> if ( GPIO.input(23) == False ):
> os.system('mpg321 -g 95 a.mp3')
> if ( GPIO.input(24) == False ):
> os.system('mpg321 -g 95 b.mp3')
> if ( GPIO.input(25)== False ):
> os.system('mpg321 -g 95 c.mp3')
> sleep(1.1);
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 
> 

So what you're saying is that you want t do an action anytime you
 notice a change in the input state from True to False.  So you
 just need to store the old state in your own variable. Whever old
 is True and new is False, perform the action.

In your initialization, create a new list of bools, length at least 26.
oldstates = [True] * 32

Now write a function changed, that detects the state change for
 one of the inputs :

def changed(num):
newstate = GPIO.input(num)
change = oldstates[num] and not newstate
oldstates[num] = newstate
return change

Now each of the if clauses can be written :

  if changed(23):
   os.system

Hopefully this will give you some ideas how you could generalize
 and/or streamline the code. If you decide to use more than 3
 switches, or if some want to detect the opposite transition, or
 ...


-- 
DaveA

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


Re: [Tutor] Python GPIO Code Help Needed

2014-10-26 Thread Crush
Could this work?

/usr/bin/env python

import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)

pin_list = ['GPIO.input(23)', 'GPIO.input(23)', 'GPIO.input(23)']

for item in pin_list:
   if item == false:
   os.system('mpg321 -g 95 a.mp3')

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


Re: [Tutor] Python GPIO Code Help Needed

2014-10-26 Thread Anish Tambe
On Sun, Oct 26, 2014 at 6:35 AM, Bill Bright  wrote:

> I have been working on a piece of code that I got from another tutorial.
> The code polls the GPIO pins on a Raspberry Pi. When it detects a switch
> being flipped, it plays a corresponding audio file. My problem is, if the
> switch remains flipped, the audio repeats again and again. What I would
> like to do is when a switch is flipped have the audio play, then have the
> code pause until the switch is returned to normal (not repeating the
> audio), and then return to the while loop to poll for the next switch flip.
> I am apparently not smart enough to figure out the correct code. I really
> need some help with this and I would appreciate any assistance.
>
> Here is the code I'm working with.
>
> #!/usr/bin/env python
> from time import sleep
> import os
> import RPi.GPIO as GPIO
> GPIO.setmode(GPIO.BCM)
> GPIO.setup(23, GPIO.IN)
> GPIO.setup(24, GPIO.IN)
> GPIO.setup(25, GPIO.IN)
> while True:
> if ( GPIO.input(23) == False ):
> os.system('mpg321 -g 95 a.mp3')
> if ( GPIO.input(24) == False ):
> os.system('mpg321 -g 95 b.mp3')
> if ( GPIO.input(25)== False ):
> os.system('mpg321 -g 95 c.mp3')
> sleep(1.1);
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


Using callbacks instead of polling might help.

You can do something like this -

def play(channel) :
#your action here
if channel == 23 :
os.system('mpg321 -g 95 a.mp3')

GPIO.add_event_detect(23, GPIO.RISING, callback=play)

and similarly for the other pins.

Check the "Events and Callback Functions" section of this tutorial -
http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/

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


[Tutor] Differentiating vowels from consonants

2014-10-26 Thread William Becerra
Hello, I'm new to programming
Running Python 2.7.8 on Windows 8 OS

I was reading http://www.sthurlow.com/python/lesson07/
Here there is an example of the for loop with a Cheerleader
program but the program is not able to print grammatically correct.


word = raw_input("Who do you go for? ")

for letter in word:
call = "Gimme a " + letter + "!"
print call
print letter + "!"

print "What does that spell?"
print word + "!"

I tried changing the code so that  the program
can recognize vowels from consonants and write the correct article (a or an)
here is my code.

word = raw_input("Who do You Support: ")
vowels = ('a', 'e', 'i', 'o', 'u')

for letter in word:
if letter == vowels:
call = 'Give me an ' + letter + '!'
print call
print letter + '!'
else :
call = 'Give me a ' + letter + '!'
print call
print letter + '!'

print 'What does that say'
print word + '!!'


My code also isn't able to recognise vowels.
For example if i write Manchester as the raw_input
i get :
give me a M
give me a a
etc.
I would like it to say
give me a M
give an a

So here is my question. What is wrong with my code and how can I
change it to get the result I want.
Thank You
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Differentiating vowels from consonants

2014-10-26 Thread Alan Gauld

On 26/10/14 09:01, William Becerra wrote:


word = raw_input("Who do You Support: ")
vowels = ('a', 'e', 'i', 'o', 'u')


You could just use a string

vowels = 'aeiou'


for letter in word:
 if letter == vowels:


you want

if letter in vowels:


 call = 'Give me an ' + letter + '!'
 print call
 print letter + '!'
 else :
 call = 'Give me a ' + letter + '!'
 print call


But I'd use a helper function instead:

def article(letter):
return 'an' if letter in 'aeiou' else 'a'

and write the call assignment as

call = 'Give me ' + article(letter) + letter + '!'

Which saves the if/else structure with its repeated code.
Of course you may not have covered functions yet in which
case your solution is fine.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] Python GPIO Code Help Needed

2014-10-26 Thread Alan Gauld

On 26/10/14 01:05, Bill Bright wrote:

The code polls the GPIO pins on a Raspberry Pi.

> When it detects a switch being flipped, it plays

a corresponding audio file.


Does that mean in either direction? Or does it only play when the switch 
goes ON?

(I'm not familiar with the Pi (although somebody recently
bought me one as a gift!) so don't know what these switches
look like - I'm guessing they are DIPs?)


What I would like to do is when a switch is flipped have

> the audio play, then have the code pause until the switch
> is returned to normal (not repeating the audio),
> and then return to the while loop to poll for the next switch flip.

What happens if one of the other switches is flipped while you are 
waiting? You need to think through all the possible combinations
that could happen at any one time and the resultant actions. (In 
professional coding this would usually involve a "state machine")


So the questions I have:
1) If I flip a switch a tune plays.
- What if I flip another switch while the first is playing?
- What if I flip another switch after the tune finishes?
- What if I restore the switch?
- do I play it again? Do I play a different tune? or None?

2) What if I flip multiple switches together?
- Do all the related tunes play? or none?

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] Auto-response for your message to the "Tutor" mailing list

2014-10-26 Thread Dominik George
Hi,

Am 26. Oktober 2014 13:20:13 MEZ, schrieb tutor-boun...@python.org:
>Your message for tutor@python.org, the Python programming tutor list,
>has been received and is being delivered.  This automated response is
>sent to those of you new to the Tutor list, to point out a few
>resources that can help with answering your own questions, or improve
>the chances of getting a useful answer from the other subscribers.

why is it I get this messages repeatedly despite having been reading and 
writing on the list for over a year now?

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


Re: [Tutor] Auto-response for your message to the "Tutor" mailing list

2014-10-26 Thread Alan Gauld

On 26/10/14 17:18, Dominik George wrote:

Hi,

Am 26. Oktober 2014 13:20:13 MEZ, schrieb tutor-boun...@python.org:

Your message for tutor@python.org, the Python programming tutor list,
has been received and is being delivered.  This automated response is
sent to those of you new to the Tutor list, to point out a few
resources that can help with answering your own questions, or improve
the chances of getting a useful answer from the other subscribers.


why is it I get this messages repeatedly despite having been reading and 
writing on the list for over a year now?


The list server puts you on moderation when you first join.
Usually I recognise regular posters names after a while
and take them off - less work for me!

However, on looking at the admin screens you are not on moderation...

The other thing that might trigger it is if you are sending from a 
different address than the one you registered with?


It all looks OK at first glance,..

--
Alan G
Tutor list moderator.

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


Re: [Tutor] if you're interested in the code thus far...

2014-10-26 Thread Clayton Kirkwood
Good eyes on the logic


!-Original Message-
!From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
!Behalf Of Peter Otten
!Sent: Sunday, October 26, 2014 3:53 AM
!To: tutor@python.org
!Subject: Re: [Tutor] if you're interested in the code thus far...
!
!Clayton Kirkwood wrote:
!
!> for description_position, code_position in zip(description, code):
!> description_position_len = len(description_position)
!> current_output_pos += description_position_len
!> if current_output_pos >= output_line_len:
!> print(description_output_string)
!> print(code_output_string)
!> code_output_string=description_output_string=''
!> current_output_pos=-1   #start new line

Changing this line to current_ouput_pos = description_position_len
I believe I was losing the next lines final field on each subsequent line
Thank-you

Clayton

!>
!> description_output_string += '{:^}|'.format(description_position)
!> code_output_string+='{0:^{1}}|'.format(code_position,
!> description_position_len)
!>
!> current_output_pos+=1  #takes care of '|' at end of string
!
!If you want to ensure that the actual line length is never more than
!output_line_len -- the above doesn't do that.
!
!Hint: reconsider this line:
!
!> current_output_pos=-1   #start new line
!
!
!> and a subset of output:
!
!Some pairs are not printed. Can you figure out which and why?
!
!___
!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] Auto-response for your message to the "Tutor" mailing list

2014-10-26 Thread Danny Yoo
>> why is it I get this messages repeatedly despite having been reading and
writing on the list for over a year now?

This has happened to me once in a while too.  I conjecture that it might be
a bug with Mailman, but I'd have to dive into the code to be certain.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] User Input with Multiple Lines

2014-10-26 Thread Malik Brahimi
So I am working with input that is pasted from a PDF file, and I was
wondering if there is any way I can do so without the program terminating
abruptly because of the newlines. I know that multiple lines of input can
be achieved via a loop with string concatenation, but this is not what I'm
looking for. Remember, I am trying to directly cut and paste a PDF
paragraph with newlines into a console program without an unexpected exit.
Take a look at the following PDF for an example: Example PDF

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


Re: [Tutor] Auto-response for your message to the "Tutor" mailing list

2014-10-26 Thread Marc Tompkins
On Sun, Oct 26, 2014 at 1:15 PM, Danny Yoo  wrote:

>
> >> why is it I get this messages repeatedly despite having been reading
> and writing on the list for over a year now?
>
> This has happened to me once in a while too.  I conjecture that it might
> be a bug with Mailman, but I'd have to dive into the code to be certain.
>
>
I get these every few months, especially (but not exclusively) if I've been
silent for a while.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] User Input with Multiple Lines

2014-10-26 Thread Ben Finney
Malik Brahimi  writes:

> So I am working with input that is pasted from a PDF file, and I was
> wondering if there is any way I can do so without the program
> terminating abruptly because of the newlines.

Can you construct a *very* short and simple example program, which
demonstrates the problem, and show it here?

-- 
 \   “The cost of education is trivial compared to the cost of |
  `\ ignorance.” —Thomas Jefferson |
_o__)  |
Ben Finney

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


[Tutor] Python Questions Help

2014-10-26 Thread Caroline H
Hi Python Tutor,

I'm having a lot of trouble with this python problem and I'm wondering if
you can help me.
"Given the lists, lst1 and lst2 , create a new sorted list consisting of
all the elements of lst1 that also appears in lst2 . For example, if lst1
is [4, 3, 2, 6, 2] and lst2 is [1, 2, 4], then the new list would be [2, 2,
4]. Note that duplicate elements in lst1 that appear in lst2 are also
duplicated in the new list. Associate the new list with the variable
new_list , and don't forget to sort the new list."

The code I wrote almost works, it just needs to be able to add a number
more than once in the third list.
For example,
lst1 = [2,5,6,7,2]
lst2 = [2,4]

it comes up with new_list = [2] when I need it to come up with new_list =
[2,2]

The code I have so far is:

new_list = []
i = 0
j = 0
if len(lst1)<=len(lst2):
for i in range(len(lst1)):
if lst1[i] in lst2:
new_list.append(lst1[i])
else:
for j in range(len(lst2)):
if lst2[j] in lst1:
new_list.append(lst2[j])
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python GPIO Code Help Needed

2014-10-26 Thread Bill Bright
Thanks to everyone for the help. The code below seems to work. My question is 
why does oldstates need to multiplied by 32? Does this mean that the code will 
only work for 32 switches before I have to reset the Pi?

Code:
#!/usr/bin/env python

from time import sleep
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
oldstates=[True]*32

def changed(channel):
newstate = GPIO.input(channel)
change = oldstates[channel] and not newstate
oldstates[channel] = newstate
return change

while True:
if changed(23):
os.system('mpg321 -g 95 a.mp3')
if changed(24):
os.system('mpg321 -g 95 b.mp3')
if changed(25):
os.system('mpg321 -g 95 c.mp3')
sleep(1.1);___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if you're interested in the code thus far...

2014-10-26 Thread Clayton Kirkwood


!-Original Message-
!From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On
!Behalf Of Alan Gauld
!Sent: Sunday, October 26, 2014 2:11 AM
!To: tutor@python.org
!Subject: Re: [Tutor] if you're interested in the code thus far...
!
!On 25/10/14 23:46, Clayton Kirkwood wrote:
!> __author__ = 'SYSTEM'
!
!You are still setting __author__ which is a bit suspect.
!Leave double underscores to python.

This is something being created when I started with this file.

!
!> import string
!
!You are still importing string twice, and you don't use it anywhere that
!I can see.
!
!> #PricingDividends
!>
!> raw_table = ('''
!
!I assume this will eventually come from a file?
!You are not really going to store it with your code?
!If you are then using a string is pointless and causing you lots of
!extra work.
!
!> a: Asky: Dividend Yield
!> b: Bid d: Dividend per Share
!> b2: Ask (Realtime)   r1: Dividend Pay Date
!> b3: Bid (Realtime)q: Ex-Dividend Date
!...
!> s7: Short Ratio
!> ''')
!
!You don't need parens as well as triple quotes.
!The quotes alone are sufficient.

Thanks.

!
!> import re, string
!
!second string import... And you don't seem to be using re either?
!

Used them before and then removed the need for them.

!> col_position, code, description = 0, [], [] key_name =
!> raw_table.replace('\t','\n')
!
!I assume this is because you don't control the file format? Since
!otherwise you would just use newlines in the file, right?

Correct. I cut and pasted the data. Not going to change (probably) no sense
in putting it in a file

!
!>
!> for each_line in  key_name.splitlines():
!>  if ':' in each_line:
!>  c, d = each_line.split(':')
!>  code.append(c)
!>  description.append(d.strip())
!>  print( col_position, code[col_position],
!description[col_position])
!>  col_position += 1
!
!You could use enumerate in the for loop and that would set the
!col_position value for you:
!
!for col_position,each_line in enumerate(key_name.splitlines()):

Good info, but I tried it and the debugger indicated an IndexError:
list.index out of range.
Interesting because when I cursor over the various items on the line, they
seem fine: col_position was 1
However, I don't think that this solution will work for me, because there
are lines with no : these being category type lines such as Date toward the
top of the data. But I now have new info on enumerate.

!
!
!> output_line_len = 120
!> current_output_pos = index = 0
!> description_output_string = code_output_string = ''
!> for description_position, code_position in zip(description, code):
!
!Why not just put the codes and descriptions in tuples when you read them
!in the loop above? Why use zip? In other words where you do
!
! >  c, d = each_line.split(':')
! >  code.append(c)
! >  description.append(d.strip())
!
!Why not just join the pair there:
!
! >  c, d = each_line.split(':')
!values.append((c,d))
!
!or even just
!
! >  values.append(each_line.split(':'))
!

Uh, because I didn't know about it:<)))

!It seems as if you are splitting the values into two lists only to zip
!those lists together again in the next loop?
!
!>  description_position_len = len(description_position)
!>  current_output_pos += description_position_len
!>
!>  if current_output_pos >= output_line_len:
!>  print(description_output_string)
!>  print(code_output_string)
!>  code_output_string=description_output_string=''
!>  current_output_pos=-1   #start new line
!>
!>  description_output_string += '{:^}|'.format(description_position)
!>
!>  code_output_string+='{0:^{1}}|'.format(code_position,
!> description_position_len)
!>
!>  current_output_pos+=1  #takes care of '|' at end of string
!

This has led to a problem, however. How do I determine if a value is in one
of the tuples:
Key in [a, word], [key, word]

I thought there might be a comprehension or something like a list.key or
something.

Thanks,

Clayton 


!HTH
!--
!Alan G
!Author of the Learn to Program web site
!http://www.alan-g.me.uk/
!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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Python GPIO Code Help Needed

2014-10-26 Thread Bill Bright
Thanks to all for the help. The code below seems to work. My question is why 
does oldstates need to be multiplied by 32? Does this means the code will only 
read 32 switch changes? That would be a problem.

Code:
#!/usr/bin/env python

from time import sleep
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.IN)
oldstates=[True]*32

def changed(channel):
newstate = GPIO.input(channel)
change = oldstates[channel] and not newstate
oldstates[channel] = newstate
return change

while True:
if changed(23):
os.system('mpg321 -g 95 a.mp3')
if changed(24):
os.system('mpg321 -g 95 b.mp3')
if changed(25):
os.system('mpg321 -g 95 c.mp3')
sleep(1.1);___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python GPIO Code Help Needed

2014-10-26 Thread Alan Gauld

On 26/10/14 22:42, Bill Bright wrote:

Thanks to all for the help. The code below seems to work. My question is
why does oldstates need to be multiplied by 32?


Its creating 32 instances of True. So you can cater for up to 32 
different switch positions.



will only read 32 switch changes? That would be a problem.


No it can manage up to 32 different switches. Each switch can toggle 
between True/False as often as it likes but oldstates will store the 
previous state for each switch.



oldstates=[True]*32

def changed(channel):
 newstate = GPIO.input(channel)
 change = oldstates[channel] and not newstate
 oldstates[channel] = newstate


Here is where it compares the old and new state of the given switch/channel


while True:
 if changed(23):
 os.system('mpg321 -g 95 a.mp3')
 if changed(24):
 os.system('mpg321 -g 95 b.mp3')
 if changed(25):
 os.system('mpg321 -g 95 c.mp3')
 sleep(1.1);


HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] User Input with Multiple Lines

2014-10-26 Thread eryksun
On Sun, Oct 26, 2014 at 3:25 PM, Malik Brahimi  wrote:
> So I am working with input that is pasted from a PDF file, and I was
> wondering if there is any way I can do so without the program terminating
> abruptly because of the newlines.

input() and raw_input() grab a single line of text. The rest of the
lines, if any, remain in the input buffer. As you know, you can loop
to build a list and then '\n'.join() the list. To instead grab the
whole buffer, you can use sys.stdin.read(). In this case you need to
manually write EOF to the buffer at the start of a new line. In a
POSIX terminal that's Ctrl-D. In the Windows console use
Ctrl-Z+Return. You may already be familiar with these as a shortcut
for exiting the shell.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Questions Help

2014-10-26 Thread Alan Gauld

On 26/10/14 22:15, Caroline H wrote:


For example,
lst1 = [2,5,6,7,2]
lst2 = [2,4]

it comes up with new_list = [2] when I need it to come up with new_list
= [2,2]


Check the logic in the if statement. Walk through it and se if it does 
what you expect. You are basically doing the right thing, except...



The code I have so far is:

new_list = []
i = 0
j = 0
if len(lst1)<=len(lst2):
 for i in range(len(lst1)):
 if lst1[i] in lst2:
 new_list.append(lst1[i])
else:
 for j in range(len(lst2)):
 if lst2[j] in lst1:
 new_list.append(lst2[j])


Rather than using indexing you should use the 'for' loop directly
on the lists:

for item in list
   if item in otherlist:
  newlist.append(item)


Its much easier to read and leaves Python to do all the index management 
for you.


Its rare to need to use indexes in conjunction with a
'for' loop.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] if you're interested in the code thus far...

2014-10-26 Thread Alan Gauld

On 26/10/14 22:12, Clayton Kirkwood wrote:


!On 25/10/14 23:46, Clayton Kirkwood wrote:
!> __author__ = 'SYSTEM'
!
!You are still setting __author__ which is a bit suspect.
!Leave double underscores to python.

This is something being created when I started with this file.


How are you creating your code? Are you using some kind of IDE?


!> raw_table = ('''
!
!> a: Asky: Dividend Yield
!> b: Bid d: Dividend per Share
!> b2: Ask (Realtime)   r1: Dividend Pay Date
!> b3: Bid (Realtime)q: Ex-Dividend Date
!...
!> s7: Short Ratio
!> ''')
!> col_position, code, description = 0, [], [] key_name =
!> raw_table.replace('\t','\n')
!
!I assume this is because you don't control the file format? Since
!otherwise you would just use newlines in the file, right?

Correct. I cut and pasted the data. Not going to change (probably) no sense
in putting it in a file


Are you saying the data will stay with your code? If that's the case 
then reformat the text into a Python data structure and avoid all the 
parsing that makes up about half your code.



!> for each_line in  key_name.splitlines():
!>  if ':' in each_line:
!>  c, d = each_line.split(':')
!>  code.append(c)
!>  description.append(d.strip())
!>  print( col_position, code[col_position],
!description[col_position])
!>  col_position += 1
!
!You could use enumerate in the for loop and that would set the
!col_position value for you:
!

...


However, I don't think that this solution will work for me, because there
are lines with no :


Ah yes, I didn't notice you only increment the counter inside the if.


!> output_line_len = 120
!> current_output_pos = index = 0
!> description_output_string = code_output_string = ''
!> for description_position, code_position in zip(description, code):
!
!Why not just put the codes and descriptions in tuples when you read them
!in the loop above? Why use zip? In other words where you do
!
! >  c, d = each_line.split(':')
! >  code.append(c)
! >  description.append(d.strip())
!
!Why not just join the pair there:
!
! >  c, d = each_line.split(':')
!values.append((c,d))
!
!or even just
!
! >  values.append(each_line.split(':'))
!

Uh, because I didn't know about it:<)))

!It seems as if you are splitting the values into two lists only to zip
!those lists together again in the next loop?
!

...


This has led to a problem, however. How do I determine if a value is in one
of the tuples:
Key in [a, word], [key, word]


I'm not sure exactly what you mean, I'd probably need to see
how you implemented it but, using my example above, you can do

key in values[index]

So if values looks like:

values = [(1,2),(2,3),]

then the test

if 2 in values[0]:  will be true

and

if 2 in values[1]:  will also be true.

If you need to test the specific tuple item you could do:

if 2 == values[0][0]:  will be false because you only test 1st item

and

if 2 == values[0][1]:  will be true coz you test the 2nd item.

If you wanted to extract all the tuples containing 2 you could use a 
comprehension like:


value2 = [tup for tup in values if 2 in tup]


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] Python GPIO Code Help Needed

2014-10-26 Thread Dave Angel
Please use text mail for posting,  and please use reply-list, or
 whatever your mailer calls it when adding to a thread. So far,
 you've got at least 3 threads covering a single topic.

Bill Bright  Wrote in message:
> 
> 
 The code below seems to work. My question is why does oldstates need to be 
multiplied by 32? Does this means the code will only read 32 switch changes? 
That would be a problem.

If you're just learning Python,  you need to actually play with
 each new concept you encounter. When you multiply a list by an
 int n, you create a new list, n times as big. Experiment with
 this in the interpreter,  till you understand. 

["A", "f", 42] *3
[] * 4000
[True, False] * 10

So you wind up with 32 flags in the oldstates list. Why 32? I
 don't have a rpi, so I took the wild guess that a Raspberry Pi
 might have 32 inputs. You're only using 3, so you could have used
 a list of 3, or a dict. But I was trying for the minimum
 complication. 

Others have mentioned an event loop in GPIO. Assuming such exists,
 it would be better to use it.
-- 
DaveA

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


Re: [Tutor] Python GPIO Code Help Needed

2014-10-26 Thread Danny Yoo
>> def changed(channel):
>>  newstate = GPIO.input(channel)
>>  change = oldstates[channel] and not newstate
>>  oldstates[channel] = newstate


Hi Bill,

I would suggest documenting the intent of this function, as it isn't
obvious at first.  A comment string would be appropriate.

I would also suggest that the function should be named "isTurnedOn" or
"isTurnedOff", as it is returning True only under a specific
transition: when the old state of the channel is True, and the new
state of the channel is False.  It doesn't check the transition going
the other direction, from False to True.  In contrast, I think of the
word "changed" to mean both directions for the transition.

To use more technical terms: the "XOR" boolean operator is what I was
expecting to see in the definition of changed(), and I was surprised
when didn't see it. :P
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Questions Help

2014-10-26 Thread Dave Angel
Please don't use html mail in a text newsgroup. And especially
 don't select black on black for your foreground and background
 colors.


Caroline H  Wrote in message:

> 
 create a new sorted list consisting of all the elements of lst1 that also 
appears in lst2 . 

Are you permitted at this stage of your class to use sets? If so,
 study their methods,  and see if you can come up with a
 oneliner.

If you cannot use sets, please post your code again in a text
 message and I'll try to see what the problem is. It can't be what
 your words say, since a list allows any number of
 dups.

You also might need to establish some constraints on your input
 data. For example what are you supposed to do if either or both
 of the original lists has duplicates? 

-- 
DaveA

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