[Tutor] New to this list ....

2012-03-30 Thread Barry Drake
Hi there   I've just joined this list and thought I'd introduce 
myself.  I used to be fairly competent in c but never made the grade to 
c++.  I've done very little programming in the last couple of years or 
so.  I'm getting a Raspberry-pi for our local Junior school and am 
starting to learn Python so I can show the year five and year six kids 
how to write simple games.  I'm already enjoying this lovely language.  
One of the few c things I miss is the switch/case statement.  if and 
elif in it's place is a bit cumbersome.  Still, it works.


One of the things I wanted to do is to use a four integer array to get 
four integers returned from a function.  I ended up using what I think 
is a list. (I'm not really sure of the datatypes yet).  This is what I 
did, and it works, but looks very inelegant to me:


correct = 0
match = 0
wrong = 0
results = [correct, match, wrong]

results = getflag(flag_1, results)
results = getflag(flag_2, results)
results = getflag(flag_3, results)
results = getflag(flag_4, results)

Any thoughts?

Kind regards,Barry.

--
From Barry Drake - a member of the Ubuntu advertising team

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


Re: [Tutor] New to this list ....

2012-03-30 Thread Barry Drake

On 30/03/12 15:04, Barry Drake wrote:
One of the things I wanted to do is to use a four integer array to get 
four integers returned from a function.  I ended up using what I think 
is a list. (I'm not really sure of the datatypes yet).  This is what I 
did, and it works, but looks very inelegant to me:


correct = 0
match = 0
wrong = 0
results = [correct, match, wrong]

results = getflag(flag_1, results)
results = getflag(flag_2, results)
results = getflag(flag_3, results)
results = getflag(flag_4, results) 


Sorry - I meant three-digit array, and the indents in the code fragment 
above were all in line originally.


--
From Barry Drake is part of the Ubuntu advertising team.

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


Re: [Tutor] New to this list ....

2012-03-30 Thread Evert Rol
  Hi and welcome Barry,

> One of the things I wanted to do is to use a four integer array to get four 
> integers returned from a function.  I ended up using what I think is a list. 
> (I'm not really sure of the datatypes yet).  This is what I did, and it 
> works, but looks very inelegant to me:
> 
> correct = 0
>match = 0
>wrong = 0
>results = [correct, match, wrong]
> 
>results = getflag(flag_1, results)
>results = getflag(flag_2, results)
>results = getflag(flag_3, results)
>results = getflag(flag_4, results)
> 
> Any thoughts?

Not sure. In the sense that you can "optimise" (refactor) it in the same way 
you could do with C. Eg:

results = [0, 0, 0]
flags = [0, 1, 2, 3]
for flag in flags:
results = getflag(flag, results)


I skipped the constant variables, but that should be clear.
The only non-C thing here is the loop, but I would think that's pretty clear. 
Note that flag takes on integer values here of course.

It could probably be done even more fancy/Pythonic, but I don't think there's a 
reason to do that.

Of course, depending on the getflag function itself, you may be able to rewrite 
things differently, but that's for another question I guess.

Cheers,

  Evert

ps: I was going to comment on the indentation, but then spotted your other 
email. I am going to comment on the subject line though ;-), because that's not 
related to your actual question. A relevant subject helps a lot, among others 
for web searches from other people later one. 


> Kind regards,Barry.
> 
> -- 
> From Barry Drake - a member of the Ubuntu advertising team
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] New to this list ....

2012-03-30 Thread Mark Lawrence

On 30/03/2012 15:13, Barry Drake wrote:

On 30/03/12 15:04, Barry Drake wrote:

One of the things I wanted to do is to use a four integer array to get
four integers returned from a function. I ended up using what I think
is a list. (I'm not really sure of the datatypes yet). This is what I


Please read the tutorial if you haven't done so already at 
http://docs.python.org/tutorial/index.html



did, and it works, but looks very inelegant to me:

correct = 0
match = 0
wrong = 0
results = [correct, match, wrong]


Usually written

results = [0, 0, 0]

or even

results = [0] * 3



results = getflag(flag_1, results)
results = getflag(flag_2, results)
results = getflag(flag_3, results)
results = getflag(flag_4, results)


How is getflag defined and what are flag_1/2/3/4 ?



Sorry - I meant three-digit array, and the indents in the code fragment
above were all in line originally.



--
Cheers.

Mark Lawrence.

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


Re: [Tutor] New to this list ....

2012-03-30 Thread Barry Drake

On 30/03/12 16:19, Evert Rol wrote:

Not sure. In the sense that you can "optimise" (refactor) it in the same way 
you could do with C. Eg:
results = [0, 0, 0]
flags = [0, 1, 2, 3]
for flag in flags:
 results = getflag(flag, results)



That's exactly what I hoped for.  I hadn't realised I can initialise a 
list in one go - it seems that lists work a lot like the arrays I was 
used to in c.  Thanks to the others who took the time to answer.  Just 
now, Asokan's solution is a bit obscure to me - I'll work on that one, 
but the above is lovely and elegant; and easy to understand.  Someone 
asked about the getflag function - it is:


def getflag(thisflag, results):
if (thisflag == 2):
results[0] += 1
elif (thisflag == 1):
results[1] += 1
elif (thisflag == 0):
results[2] += 1
return(results)

In c, I would have used switch and case, but I gather there is no direct 
equivalent in Python ...   But it works as is.


--
From Barry Drake - a member of the Ubuntu advertising team.

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


Re: [Tutor] New to this list ....

2012-03-30 Thread Asokan Pichai
On Fri, Mar 30, 2012 at 9:12 PM, Barry Drake  wrote:
> On 30/03/12 16:19, Evert Rol wrote:
>>
>> Not sure. In the sense that you can "optimise" (refactor) it in the same
>> way you could do with C. Eg:
>> results = [0, 0, 0]
>> flags = [0, 1, 2, 3]
>> for flag in flags:
>>     results = getflag(flag, results)
>>
>
> That's exactly what I hoped for.  I hadn't realised I can initialise a list
> in one go - it seems that lists work a lot like the arrays I was used to in
> c.  Thanks to the others who took the time to answer.  Just now, Asokan's
> solution is a bit obscure to me - I'll work on that one, but the above is
> lovely and elegant; and easy to understand.  Someone asked about the getflag
> function - it is:
>

> def getflag(thisflag, results):
>    if (thisflag == 2):
>        results[0] += 1
>    elif (thisflag == 1):> In c, I would have used switch and case, but I 
> gather there is no direct
> equivalent in Python ...   But it works as is.
>
> --
> From Barry Drake - a member of the Ubuntu advertising team.
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

and directly index on thisflag
>        results[1] += 1
>    elif (thisflag == 0):
>        results[2] += 1
>    return(results)
>
For the specific values it may be simpler to write:

def getflag(thisflag, results):
 results[2- thisflag] += 1
 return results

Or you may rearrange the meaning of results
and write

 results[thisflag] += 1

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


[Tutor] problem with key in mailbox.Maildir

2012-03-30 Thread Kantesh Raj
hi everyone,
i am not able to get email from my mailbox by using key.
key is automatically removed after program termination but file is present
there.
i get data by same code method by using fresh key (which is generated by
adding email again)

import mailbox
mailb = mailbox.Maildir('~/Maildir',factory=none)
k=mailb.get_file('1333110477.M317089P8312Q1.Kantesh-Pc')
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.7/mailbox.py", line 359, in get_file
f = open(os.path.join(self._path, self._lookup(key)), 'rb')
  File "/usr/lib/python2.7/mailbox.py", line 529, in _lookup
raise KeyError('No message with key: %s' % key)
KeyError: 'No message with key: 1333110477.M317089P8312Q1.Kantesh-Pc

how to get email by key???
thanks in advance for help
-- 
Kantesh Raj
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Russel Winder
Barry,

On Fri, 2012-03-30 at 16:42 +0100, Barry Drake wrote:
[...]
> def getflag(thisflag, results):
>  if (thisflag == 2):
>  results[0] += 1
>  elif (thisflag == 1):
>  results[1] += 1
>  elif (thisflag == 0):
>  results[2] += 1
>  return(results)

Two thoughts spring to mind:

-- is it possible to arrange the data model such that the value of the
thisflag is the index into the sequence, then:

def getflag(thisflag, results):
results[thisflag] += 1
return results

-- seriously "overengineered" for this particular example but the
replacement for switch statement is a dictionary and function pointers.

from functools import partial

def alt(thisflag, results):
def setflag(x):
results[x] += 1
{
0: partial(setflag, 2),
1: partial(setflag, 1),
2: partial(setflag, 0),
}[thisflag]()
return results

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Peter Otten
Barry Drake wrote:

> On 30/03/12 16:19, Evert Rol wrote:
>> Not sure. In the sense that you can "optimise" (refactor) it in the same
>> way you could do with C. Eg: results = [0, 0, 0]
>> flags = [0, 1, 2, 3]
>> for flag in flags:
>>  results = getflag(flag, results)
>>
> 
> That's exactly what I hoped for.  I hadn't realised I can initialise a
> list in one go - it seems that lists work a lot like the arrays I was
> used to in c.  Thanks to the others who took the time to answer.  Just
> now, Asokan's solution is a bit obscure to me - I'll work on that one,
> but the above is lovely and elegant; and easy to understand.  Someone
> asked about the getflag function - it is:
> 
> def getflag(thisflag, results):
>  if (thisflag == 2):
>  results[0] += 1
>  elif (thisflag == 1):
>  results[1] += 1
>  elif (thisflag == 0):
>  results[2] += 1
>  return(results)
> 
> In c, I would have used switch and case, but I gather there is no direct
> equivalent in Python ...   But it works as is.

Here is an alternative to if...elif..., a table (python list) that 
translates from the flag to an index into the results table.

flag_to_result = [2, 1, 0]

def update_results(flag, results):
try:
results[flag_to_result[flag]] += 1
except IndexError: 
pass # ignore flags other than 0, 1, 2

results = [0, 0, 0]
flags = [0, 1, 2, 3, 4, 3, 2, 1]
for flag in flags:
update_results(flag, results)
print results

The following operates on an even higher level:

from collections import Counter
flags = [0, 1, 2, 3, 4, 3, 2, 1]
counter = Counter(flags)
results = [counter[2], counter[1], counter[0]]
print results


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


Re: [Tutor] New to this list ....

2012-03-30 Thread Prasad, Ramit
> > Hi there   I've just joined this list and thought I'd introduce 
myself.  

Welcome!

> > correct = 0
> > match = 0
> > wrong = 0
> > results = [correct, match, wrong]
> >
> > results = getflag(flag_1, results)
> > results = getflag(flag_2, results)
> > results = getflag(flag_3, results)
> > results = getflag(flag_4, results)

>> def getflag(thisflag, results):
>>  if (thisflag == 2):
>>  results[0] += 1
>>  elif (thisflag == 1):
>>  results[1] += 1
>>  elif (thisflag == 0):
>>  results[2] += 1
>>  return(results)
>> 
>> In c, I would have used switch and case, but I gather there is no direct
>> equivalent in Python ...   But it works as is.

C switch is just a different way of doing an if/elif tree, I do not 
really see any real difference. Although, if there is you can feel free 
to enlighten me. :)

Unlike C, the parenthesis in if statements and returns are not necessary.

>  if (thisflag == 2):
becomes
  if thisflag == 2:

>  return(results)
becomes 
  return results

Furthermore, the way Python binds names means that modifying the list
in getflags modifies it in the callee. No need to return and reassign 
results.

>>> def foo(x):
... x[0] += 1
... 
>>> bar = [ 1, 3, 4 ]
>>> foo( bar )
>>> print bar
[2, 3, 4]
>>> foo( bar )
>>> print bar
[3, 3, 4]

Be careful of "results = [0] * 3". This style works fine for immutable
types (int, float, str) but does not work as people new to Python think
it does. 

>>> def foo(x):
... x[0][0] += 1
... 
>>> bar = [ [0] ]*3
>>> print bar
[[0], [0], [0]]
>>> foo( bar )
>>> print bar
[[1], [1], [1]]
>>> foo( bar )
>>> print bar
[[2], [2], [2]]

This occurs because bar is a list containing 3 elements which are 
all the exact same object. Modifying one sub-list will modify the
"others" as well. 

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Prasad, Ramit
> Furthermore, the way Python binds names means that modifying the list
> in getflags modifies it in the callee. No need to return and reassign
> results.

I correct myself. It has nothing to do with name binding, but entirely
to do with Python's object model. 

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Russel Winder
Ramit,

On Fri, 2012-03-30 at 16:22 +, Prasad, Ramit wrote:
[...]
> C switch is just a different way of doing an if/elif tree, I do not 
> really see any real difference. Although, if there is you can feel free 
> to enlighten me. :)
[...]

'fraid not -- though it depends on which compiler and how many cases.
For 3 or more cases compilers will generate a hardware supported jump
table: a switch statement is a jump table not a cascade of if
statements.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Prasad, Ramit
> [...]
> > C switch is just a different way of doing an if/elif tree, I do not
> > really see any real difference. Although, if there is you can feel free
> > to enlighten me. :)
> [...]
> 
> 'fraid not -- though it depends on which compiler and how many cases.
> For 3 or more cases compilers will generate a hardware supported jump
> table: a switch statement is a jump table not a cascade of if
> statements.

Good point, I had forgotten that. 

Although, it sounded like the OP was referring more to syntax and
coding practice than the low level implementation.

>I'm already enjoying this lovely language.  
>One of the few c things I miss is the switch/case statement.  if and 
>elif in it's place is a bit cumbersome.  Still, it works.


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Mark Lawrence

On 30/03/2012 15:04, Barry Drake wrote:


One of the few c things I miss is the switch/case statement. if and elif
in it's place is a bit cumbersome. Still, it works.


The recipe here

http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/

refers to several other recipes which you might want to take a look at, 
sorry I meant to mention this earlier.


--
Cheers.

Mark Lawrence.

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


[Tutor] Problem Stripping

2012-03-30 Thread leam hall
Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
but it doesn't seem to work like I thought.


res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
uname = res.stdout.read().strip()

>>> uname
'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
i686 i686 i386 GNU/Linux'

>>> uname.strip(':')
'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
i686 i686 i386 GNU/Linux'

>>> 'www.example.com'.strip('cmowz.')
'example'

Thoughts?

Leam
-- 
Mind on a Mission 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem Stripping

2012-03-30 Thread Evert Rol
> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
> but it doesn't seem to work like I thought.
> 
> 
> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
> uname = res.stdout.read().strip()
> 
 uname
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
> 
 uname.strip(':')
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
> 
 'www.example.com'.strip('cmowz.')
> 'example'
> 
> Thoughts?

Read the documentation carefully: 
http://docs.python.org/library/stdtypes.html#str.strip
It strips *any* of the characters in the strip() argument from either side of 
the string, up to the point where it can't strip a matching character anymore.
Since the uname variable doesn't not start with a ':' on either side, it can't 
strip anything, and stops immediately, returning the resulting (full) string.

You could look into split(':', maxsplit), with maxsplit not at its default 
value: http://docs.python.org/library/stdtypes.html#str.split
Otherwise, regular experssions would be a (bit more drastic) solution.

Btw, what *did* you actually expect it should do? That would give us a better 
hint into the result you want.

Good luck,

  Evert

 
> 
> Leam
> -- 
> Mind on a Mission 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Problem Stripping

2012-03-30 Thread Joel Goldstick
On Fri, Mar 30, 2012 at 1:09 PM, leam hall  wrote:
> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
> but it doesn't seem to work like I thought.
>
>
> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
> uname = res.stdout.read().strip()
>
 uname
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
 uname.strip(':')
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
>
 'www.example.com'.strip('cmowz.')
> 'example'
>
> Thoughts?
>
> Leam
> --
> Mind on a Mission 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

str.strip() removes characters from begining and end of the string --
Not any in between.  Notice

>>> example = "www.example.com".strip("wcomx")
>>> example
'.example.'
>>>

The x remains


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


Re: [Tutor] Problem Stripping

2012-03-30 Thread Prasad, Ramit

> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
> but it doesn't seem to work like I thought.
> 
> 
> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
> uname = res.stdout.read().strip()
> 
> >>> uname
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
> 
> >>> uname.strip(':')
> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
> i686 i686 i386 GNU/Linux'
> 
> >>> 'www.example.com'.strip('cmowz.')
> 'example'
> 
> Thoughts?

Read the documentation. It is excellent and explains this behavior.
http://docs.python.org/library/string.html

If you are trying to remove ':' from the result of uname what you 
really want is uname.replace(':','')

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Barry Drake

On 30/03/12 17:58, Mark Lawrence wrote:

The recipe here
http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/ 

refers to several other recipes which you might want to take a look 
at, sorry I meant to mention this earlier.




Oh, that's neat.  Not worth putting into my little project, but I've 
bookmarked that on for when I need a lot of cases.  It also shows how to 
define a class - that was something I had wondered about, but not yet 
tackled.


I'm really grateful for the answers I have received.  It will take me a 
while to get my head around some of the less obvious code fragment that 
folk have kindly posted, but I will play around with all of them in due 
course.  Already the code I am playing with has shrunk to about half the 
number of lines it was before I joined this list and I'm even more than 
ever impressed with the language.  I also like the fact that it can be 
played with as an interpreted language.  Compiling and debugging c code 
could be a pain at times.  In Python, a few print statements together 
with the syntax error messages from the interpreter make it very easy to 
see what's happening.


Kind regards,Barry.

--
From Barry Drake - a member of the Ubuntu advertising team.

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


Re: [Tutor] Problem Stripping

2012-03-30 Thread Emile van Sebille

On 3/30/2012 10:09 AM leam hall said...

Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
but it doesn't seem to work like I thought.



... but it works as advertised...

Help on built-in function strip:

strip(...)
S.strip([chars]) -> string or unicode

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping



If you're expecting the ":"s to be stripped out, try using replace:

Help on built-in function replace:

replace(...)
S.replace (old, new[, count]) -> string

Return a copy of string S with all occurrences of substring
old replaced by new.  If the optional argument count is
given, only the first count occurrences are replaced.


HTH,

Emile




res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
uname = res.stdout.read().strip()


uname

'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
i686 i686 i386 GNU/Linux'


uname.strip(':')

'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
i686 i686 i386 GNU/Linux'


'www.example.com'.strip('cmowz.')

'example'

Thoughts?

Leam





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


Re: [Tutor] Problem Stripping

2012-03-30 Thread Joel Goldstick
On Fri, Mar 30, 2012 at 1:25 PM, Joel Goldstick
 wrote:
> On Fri, Mar 30, 2012 at 1:09 PM, leam hall  wrote:
>> Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
>> but it doesn't seem to work like I thought.
>>
>>
>> res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
>> uname = res.stdout.read().strip()
>>
> uname
>> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
>> i686 i686 i386 GNU/Linux'
>>
> uname.strip(':')
>> 'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
>> i686 i686 i386 GNU/Linux'
>>
> 'www.example.com'.strip('cmowz.')
>> 'example'
>>
>> Thoughts?
>>
>> Leam
>> --
>> Mind on a Mission 
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> str.strip() removes characters from begining and end of the string --
> Not any in between.  Notice
>
 example = "www.example.com".strip("wcomx")
 example
> '.example.'

>
> The x remains


You could do list comprehension
>
>>> n = "".join([x for x in "this has : some : colons" if x not in ':'])
>>> n
'this has  some  colons'
>
-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem Stripping

2012-03-30 Thread Mark Lawrence

On 30/03/2012 18:09, leam hall wrote:

Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
but it doesn't seem to work like I thought.


What do you expect it to do?




res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
uname = res.stdout.read().strip()


uname

'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
i686 i686 i386 GNU/Linux'


uname.strip(':')

'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
i686 i686 i386 GNU/Linux'


'www.example.com'.strip('cmowz.')

'example'

Thoughts?

Leam


Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit 
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>> help(''.strip)
Help on built-in function strip:

strip(...)
S.strip([chars]) -> string or unicode

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping

So it appears that your examples are doing precisely what is defined above.

HTH.

--
Cheers.

Mark Lawrence.

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


Re: [Tutor] New to this list ....

2012-03-30 Thread Barry Drake

On 30/03/12 17:22, Prasad, Ramit wrote:
Unlike C, the parenthesis in if statements and returns are not 
necessary. Furthermore, the way Python binds names means that 
modifying the list in getflags modifies it in the callee. No need to 
return and reassign results. 


This is lovely.  It's so much friendlier than c.  I'm used to c 
variables going out of scope once you leave the called function.  I 
imagine if you want to leave the variables unchanged, you have to 
re-assign them inside the function.  I quite like it that way.
Be careful of "results = [0] * 3". This style works fine for immutable 
types (int, float, str) but does not work as people new to Python 
think it does.


Thanks for the warning.

--
From Barry Drake - a member of the Ubuntu advertising team.

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


Re: [Tutor] New to this list ....

2012-03-30 Thread Prasad, Ramit
[snip] 
>I'm used to c
> variables going out of scope once you leave the called function.  I
> imagine if you want to leave the variables unchanged, you have to
> re-assign them inside the function.
[snip]

Lists are mutable objects. When you pass a list to a function you bind 
a name in the functions namespace to the list object. Every name 
binding to that object will have the ability to modify the list. 

If you want to modify the list but not change it for others usually
you do something like

new_list = list( old_list )
OR  
new_list = old_list[:]

Now if you wanted to change an immutable object (like int) then you 
would have to return object because the name binding is only the 
function's scope. It should also be noted that you can modify
the list but you cannot reassign the list from the function.
Consider:

>>> 
>>> def blah( a ):
... a = [] 
... 
>>> b = [ 1, 3, 4 ] 
>>> blah( b )
>>> print b
[1, 3, 4]
>>>

The reason b is untouched is because a = [] just binds the name 'a' to
a new list object while the name 'b' is still bound to the original 
list object. To bind 'b' to the new list I would have had to return it
from blah().

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem Stripping

2012-03-30 Thread Prasad, Ramit
> strip(...)
>  S.strip([chars]) -> string or unicode
> 
>  Return a copy of the string S with leading and trailing
>  whitespace removed.
>  If chars is given and not None, remove characters in chars instead.
>  If chars is unicode, S will be converted to unicode before stripping

You can get this text on the interpreter by doing help(str.strip) or 
help(''.strip). There is even an interactive help you can access by doing 
help(). I find it very useful, although usefulness varies with 3rd party 
code.

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Emile van Sebille

On 3/30/2012 10:56 AM Prasad, Ramit said...


Lists are mutable objects. When you pass a list to a function you bind
a name in the functions namespace to the list object. Every name
binding to that object will have the ability to modify the list.

If you want to modify the list but not change it for others usually
you do something like

new_list = list( old_list )
OR
new_list = old_list[:]


Be aware though that this copies only the 'top' level objects -- if 
those object are themselves mutable you may still have issues:



>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [a,b]
>>> d = c[:]
>>> d.append(1)
>>> d
[[1, 2, 3], [4, 5, 6], 1]
>>> c
[[1, 2, 3], [4, 5, 6]]
>>> # so far so good
...
>>> b.append(7)
>>> d
[[1, 2, 3], [4, 5, 6, 7], 1]
>>> c
[[1, 2, 3], [4, 5, 6, 7]]
>>>


To avoid this, look at copy.deepcopy as an alternative:

>>> d = copy.deepcopy(c)
>>> d
[[1, 2, 3], [4, 5, 6, 7]]
>>> b.append(8)
>>> c
[[1, 2, 3], [4, 5, 6, 7, 8]]
>>> d
[[1, 2, 3], [4, 5, 6, 7]]
>>>


HTH,

Emile


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


Re: [Tutor] New to this list ....

2012-03-30 Thread Russel Winder
Barry,

On Fri, 2012-03-30 at 18:27 +0100, Barry Drake wrote:
> On 30/03/12 17:58, Mark Lawrence wrote:
> > The recipe here
> > http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/
> >  
> >
> > refers to several other recipes which you might want to take a look 
> > at, sorry I meant to mention this earlier.
> >
> 
> Oh, that's neat.  Not worth putting into my little project, but I've 
> bookmarked that on for when I need a lot of cases.  It also shows how to 
> define a class - that was something I had wondered about, but not yet 
> tackled.

Be aware of the sensible warnings though.  switch in C is generally O(1)
whereas this Python simulacrum remains O(n).  The people who comment
saying C switch is always O(n) have clearly never looked at any compiler
output.

Personally, I am in the camp that says: don't use a device that makes it
appear the performance is not what it is.  Thus I would prefer
if/elif/else cascade over this device since it is simpler and more
obvious that it is O(n).

But it is a nice example of what Python can achieve even though I would
not use it myself.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] What to call a string with embedded descriptor?

2012-03-30 Thread Tim Johnson
This is kind of a theoretical question. I.E. I am looking for a
keyword to do research on.
Consider the following string:

'3:2,6:2,4:3,5:0|age50height63nametimvalue'

If the string above is split on the first occurance of '|',
the result is a 'leftmost' component which can be decomposed into a
nested list of integers which can then be used to parse the
'rightmost' string into a dictionary.

What would be a generic term or a pythonist term for such a string?

Thanks
-- 
Tim 
tim at tee jay forty nine dot com or akwebsoft dot com
http://www.akwebsoft.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] New to this list ....

2012-03-30 Thread Cranky Frankie
Message: 1
Date: Fri, 30 Mar 2012 15:04:09 +0100
Barry Drake  wrote:

<< I'm getting a Raspberry-pi for our local Junior school and am
starting to learn Python so I can show the year five and year six kids
how to write simple games.>>

Here's what you need - he starts simple and winds up with some nice games:

http://www.amazon.com/Python-Programming-Absolute-Beginner-Edition/dp/1435455002/ref=sr_1_6?ie=UTF8&qid=1333131438&sr=8-6




-- 
Frank L. "Cranky Frankie" Palmeri
Risible Riding Raconteur & Writer
“The problem with quotes on the Internet is that
it is often difficult to verify their authenticity.”
- Abraham Lincoln
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What to call a string with embedded descriptor?

2012-03-30 Thread S.Irfan Rizvi
please help me disable this...i made big mistake searching your site


On Fri, Mar 30, 2012 at 1:57 PM, Tim Johnson  wrote:
> This is kind of a theoretical question. I.E. I am looking for a
> keyword to do research on.
> Consider the following string:
>
> '3:2,6:2,4:3,5:0|age50height63nametimvalue'
>
> If the string above is split on the first occurance of '|',
> the result is a 'leftmost' component which can be decomposed into a
> nested list of integers which can then be used to parse the
> 'rightmost' string into a dictionary.
>
> What would be a generic term or a pythonist term for such a string?
>
> Thanks
> --
> Tim
> tim at tee jay forty nine dot com or akwebsoft dot com
> http://www.akwebsoft.com
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
       Regards
     ~ S. Irfan Rizvi
 ADR-TS
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What to call a string with embedded descriptor?

2012-03-30 Thread Emile van Sebille

On 3/30/2012 12:57 PM S.Irfan Rizvi said...

please help me disable this...i made big mistake searching your site


note that at the bottom of every email you're getting from this list has 
a link to the unsubscribe page...





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







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


Re: [Tutor] What to call a string with embedded descriptor?

2012-03-30 Thread Emile van Sebille

On 3/30/2012 11:57 AM Tim Johnson said...

This is kind of a theoretical question. I.E. I am looking for a
keyword to do research on.
Consider the following string:

'3:2,6:2,4:3,5:0|age50height63nametimvalue'

If the string above is split on the first occurance of '|',
the result is a 'leftmost' component which can be decomposed into a
nested list of integers which can then be used to parse the
'rightmost' string into a dictionary.

What would be a generic term or a pythonist term for such a string?



I've called them formatted or structured -- you could also say 
proprietary I imagine.  I'm not aware if there's a formal name...


Emile



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


[Tutor] Syntax error help

2012-03-30 Thread chris knarvik
Alright i have been trying to right a (relatively) simple to calculate area
and volume below is my current working code
def areamenu():
print 'Square (1)'
print 'triangle (2)'
print 'rectangle (3)'
print 'trapazoid (4)'
print 'circle (5)'

def squareacalc():
sidelength = input('enter side length: ')
print ' the side length is' sidelength ** 2

def displaymenu():
print 'please make a selection';
print 'Area (1)';
choice = input(raw_input('enter selection number'):
if (choice == 1):
Areamenu():

else:
print 'choice' , choice, ' is wrong try again'

def selctiona():
Areamenu();
choicea = input(raw_input'enter selection');
if (choicea == 1):
squareacalc()



 print 'good bye'

I keep getting this error
Traceback (most recent call last):
  File "", line 1, in 
import Area
  File "C:\Python27\Area.py", line 10
areamenu()
   ^
SyntaxError: invalid syntax

can anyone tell me what im doing wrong i cant see the problem
help would be appreciated
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Syntax error help

2012-03-30 Thread Evert Rol
> Alright i have been trying to right a (relatively) simple to calculate area 
> and volume below is my current working code
> def areamenu():
> print 'Square (1)'
> print 'triangle (2)'
> print 'rectangle (3)'
> print 'trapazoid (4)'
> print 'circle (5)'
> 
> def squareacalc():
> sidelength = input('enter side length: ')
> print ' the side length is' sidelength ** 2
> 
> def displaymenu():
> print 'please make a selection';
> print 'Area (1)';
> choice = input(raw_input('enter selection number'):

You're missing a closing parenthesis here.

But see comments below.


> if (choice == 1):
> Areamenu():
> 
> else:
> print 'choice' , choice, ' is wrong try again'
> 
> def selctiona():
> Areamenu();
> choicea = input(raw_input'enter selection');

And here you're missing an openening parenthesis.


> if (choicea == 1):
> squareacalc()
> 
> 
> 
>  print 'good bye'
> 
> I keep getting this error
> Traceback (most recent call last):
>   File "", line 1, in 
> import Area
>   File "C:\Python27\Area.py", line 10
> areamenu()
>^
> SyntaxError: invalid syntax
> 
> can anyone tell me what im doing wrong i cant see the problem
> help would be appreciated

A syntax error is often a typing error in the code. In this case, forgotten 
parentheses, but could be forgotten colons or an unclosed string.
The parentheses problem can often be caught by using a good editor: these often 
lightlight when you close a set of parentheses, so you can spot syntax errors 
while you are typing.


There are a number of other things wrong here, though.
Style-wise: Python does not need (and prefers not to have) closing semicolons. 
In addition, there is no need to surround if statements with parentheses: "if 
choice == 1:" is perfectly fine and much more legible.
Perhaps you think (coming from another language): "but it doesn't hurt, and I 
like it this way". But then you're still programming in that other language, 
and just translating to Python; not actually coding in Python.

Also, this is odd, wrong and pretty bad:

   choice = input(raw_input('enter selection number')):

Use either raw_input() (for Python 2.x) or input() (Python 3.x).
It's wrong because you are waiting for input, then use that input as the next 
prompting string for further input. Like this
>>> choice = input(raw_input('enter selection number'))
enter selection number1
12
>>> print choice
2

(the 12 is the 1 I entered before, plus a 2 I just entered as a second entry.)
So just use one function, and the appropriate one for the Python version.
Lastly, choice will be a string, since input() and raw_input() return a string.
In the if-statement, however, you are comparing that string to an integer. 
Python does not do implicit conversion, so you'll have to convert the string to 
an integer first, or compare to a string instead.
(Something similar happens for the sidelength, btw.)
Last thing I see glancing over the code: you define areamenu(), but call 
Areamenu(). Python is case sensitive, so you'd have to call areamenu() instead.


This may be a bit more information than you asked for, but hopefully you don't 
mind.

Good luck,

  Evert




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


Re: [Tutor] What to call a string with embedded descriptor?

2012-03-30 Thread Tim Johnson
* Emile van Sebille  [120330 12:21]:
<> >If the string above is split on the first occurance of '|',
> >the result is a 'leftmost' component which can be decomposed into a
> >nested list of integers which can then be used to parse the
> >'rightmost' string into a dictionary.
> >
> >What would be a generic term or a pythonist term for such a string?
> >
> 
> I've called them formatted or structured -- you could also say 
> proprietary I imagine.  I'm not aware if there's a formal name...
  Back in the day when I worked in C and did a bit of reconstructing
  proprietory database schemes in legacy systems, it was common for
  a binary file to start with a "map" which defined how to search
  the subsequent content. I never did know if there was a formal
  terminology for such a setup. I guess there isn't.

  Thanks for the reply.
-- 
Tim 
tim at tee jay forty nine dot com or akwebsoft dot com
http://www.akwebsoft.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Syntax error help

2012-03-30 Thread bob gailer

On 3/30/2012 4:26 PM, chris knarvik wrote:
Alright i have been trying to right a (relatively) simple to calculate 
area and volume below is my current working code


Suggestion: start with a VERY SIMPLE program and get that working. Then 
add one new feature at a time.


Is the following in Area.py? If it is then the traceback could not have 
come from importing this code, because line 10 does not mention areamenu().

In fact areamenu() appears ONLY in line 1, but not by itself!


def areamenu():
print 'Square (1)'
print 'triangle (2)'
print 'rectangle (3)'
print 'trapazoid (4)'
print 'circle (5)'

def squareacalc():
sidelength = input('enter side length: ')
print ' the side length is' sidelength ** 2

def displaymenu():
print 'please make a selection';
print 'Area (1)';
choice = input(raw_input('enter selection number'):
if (choice == 1):
Areamenu():

else:
print 'choice' , choice, ' is wrong try again'

def selctiona():
Areamenu();
choicea = input(raw_input'enter selection');
if (choicea == 1):
squareacalc()



 print 'good bye'

I keep getting this error
Traceback (most recent call last):
  File "", line 1, in 
import Area
  File "C:\Python27\Area.py", line 10
areamenu()
   ^
SyntaxError: invalid syntax

can anyone tell me what im doing wrong i cant see the problem
help would be appreciated

What are you using to run the above? I'll guess the interactive window 
of IDLE. Try reload(Area). Once a module is imported you must reload to 
import it again. (I am assuming you made changes after the initial error.)


The above code should give you something like:
Traceback (most recent call last):
  File "", line 1, in 
  File "Script3.py", line 10
print ' the side length is' sidelength ** 2
 ^
SyntaxError: invalid syntax

fix that (do you know what to do?) then you should get a syntax error 
for line15. Why is there a : at the end of that line?

then you have 1 more trailing : to deal with.
then there is a missing )
then there is a missing (

Once you fix all the problems then you should see
good bye
since that is the only executable code in Area.py other than def statements.

Suggestion: start with a VERY SIMPLE program and get that working. Then 
add one new feature at a time.


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] New to this list ....

2012-03-30 Thread Barry Drake

On 30/03/12 19:18, Cranky Frankie wrote:

Here's what you need - he starts simple and winds up with some nice games:
http://www.amazon.com/Python-Programming-Absolute-Beginner-Edition/dp/1435455002/ref=sr_1_6?ie=UTF8&qid=1333131438&sr=8-6


Wow!  I found an e-book copy online and got it.  Looks good!  I've 
looked at 'Snake Wrangling for Kids'.  Informative, but the example code 
is not going to inspire kids.  Al Sweigarts two e-books are a bit 
better, but I think I'm going to get more mileage from the above.  Thanks.


Kind regards,Barry.

--
From Barry Drake - a member of the Ubuntu advertising team.

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


Re: [Tutor] Syntax error help

2012-03-30 Thread Modulok
On 3/30/12, chris knarvik  wrote:
> Alright i have been trying to right a (relatively) simple to calculate area
> and volume below is my current working code
> def areamenu():
> print 'Square (1)'
> print 'triangle (2)'
> print 'rectangle (3)'
> print 'trapazoid (4)'
> print 'circle (5)'
>
> def squareacalc():
> sidelength = input('enter side length: ')
> print ' the side length is' sidelength ** 2
>
> def displaymenu():
> print 'please make a selection';
> print 'Area (1)';
> choice = input(raw_input('enter selection number'):
> if (choice == 1):
> Areamenu():
>
> else:
> print 'choice' , choice, ' is wrong try again'
>
> def selctiona():
> Areamenu();
> choicea = input(raw_input'enter selection');
> if (choicea == 1):
> squareacalc()
>
>
>
>  print 'good bye'
>
> I keep getting this error
> Traceback (most recent call last):
>   File "", line 1, in 
> import Area
>   File "C:\Python27\Area.py", line 10
> areamenu()
>^
> SyntaxError: invalid syntax
>
> can anyone tell me what im doing wrong i cant see the problem
> help would be appreciated

Chris,

Your code has numerous problems. First, the only lines that should end in a
colon ':' are lines that define something, i.e. class and function signatures,
etc. Your code has them sprinkled in places where they really don't belong. For
instance:

choice = input(raw_input('enter selection number'):  #<-- No.

Additionally, python statements are almost never terminated with a semicolon
';'. Your code has those in random places as well. Python statements *can* be
semicolon terminated, but this should *only* be used if there is more than one
statement per line. (Generally a discouraged practice, unless in the case of a
list comprehensions):

choicea = input(raw_input'enter selection'); #<-- Not needed.

The other problem I see is a lot of mis-matched parentheses. For instance your
code has places where there are two opening parentheses, but only one closing
parenthesis:

choice = input(raw_input('enter selection number'):

Should be:

choice = input(raw_input('enter selection number'))

However, the above line is pretty nasty. It's making an unnecessary function
call. Above, its waiting for user input to the 'input' function, and using the
return value of the 'raw_input' function for that input, which itself is
waiting for user input. i.e. an input waiting for an input. It should instead
be something like this:

choicea = input('enter selection: ')

*If* you decide to use the 'raw_input()' function instead, remember to
type-cast the value that gets entered. By default with 'raw_input()' it is
interpreted as a string and not an integer. Thus, the comparison 'if (choicea
== 1)' wouldn't work with 'raw_input()' expected.

This is a problem as well:

print ' the side length is' sidelength ** 2

The print statement only takes a single argument, or multiple arguments *if*
they are comma separated, e.g:

print ' the side length is', sidelength ** 2

However, this whole thing would be better written using string substitution::

print ' the side length is %s' % (sidelength ** 2)

This is also incorrect:

Areamenu()

Your code defines a function named 'areamenu', not one named 'Areamenu'.
Remember, python is case sEnsItiVe. e.g: Foo() and foo() are different.

Good luck!
-Modulok-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to this list ....

2012-03-30 Thread Emile van Sebille

On 3/30/2012 2:41 PM Barry Drake said...

On 30/03/12 19:18, Cranky Frankie wrote:

Here's what you need - he starts simple and winds up with some nice
games:
http://www.amazon.com/Python-Programming-Absolute-Beginner-Edition/dp/1435455002/ref=sr_1_6?ie=UTF8&qid=1333131438&sr=8-6






If you haven't already found reference to it, you might also find pygame 
interesting.


http://pygame.org/docs/tut/newbieguide.html

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


Re: [Tutor] Syntax error help

2012-03-30 Thread x23ch...@gmail.com
Thanks I've fixed

Sent from my iPod

On Mar 30, 2012, at 5:30 PM, bob gailer  wrote:

> On 3/30/2012 4:26 PM, chris knarvik wrote:
>> Alright i have been trying to right a (relatively) simple to calculate area 
>> and volume below is my current working code
> 
> Suggestion: start with a VERY SIMPLE program and get that working. Then add 
> one new feature at a time.
> 
> Is the following in Area.py? If it is then the traceback could not have come 
> from importing this code, because line 10 does not mention areamenu().
> In fact areamenu() appears ONLY in line 1, but not by itself!
> 
>> def areamenu():
>>print 'Square (1)'
>>print 'triangle (2)'
>>print 'rectangle (3)'
>>print 'trapazoid (4)'
>>print 'circle (5)'
>> 
>> def squareacalc():
>>sidelength = input('enter side length: ')
>>print ' the side length is' sidelength ** 2
>> 
>> def displaymenu():
>>print 'please make a selection';
>>print 'Area (1)';
>>choice = input(raw_input('enter selection number'):
>>if (choice == 1):
>>Areamenu():
>> 
>>else:
>>print 'choice' , choice, ' is wrong try again'
>> 
>> def selctiona():
>>Areamenu();
>>choicea = input(raw_input'enter selection');
>>if (choicea == 1):
>>squareacalc()
>> 
>> 
>> 
>> print 'good bye'
>> 
>> I keep getting this error
>> Traceback (most recent call last):
>>  File "", line 1, in 
>>import Area
>>  File "C:\Python27\Area.py", line 10
>>areamenu()
>>   ^
>> SyntaxError: invalid syntax
>> 
>> can anyone tell me what im doing wrong i cant see the problem
>> help would be appreciated
>> 
> What are you using to run the above? I'll guess the interactive window of 
> IDLE. Try reload(Area). Once a module is imported you must reload to import 
> it again. (I am assuming you made changes after the initial error.)
> 
> The above code should give you something like:
> Traceback (most recent call last):
>  File "", line 1, in 
>  File "Script3.py", line 10
>print ' the side length is' sidelength ** 2
> ^
> SyntaxError: invalid syntax
> 
> fix that (do you know what to do?) then you should get a syntax error for 
> line15. Why is there a : at the end of that line?
> then you have 1 more trailing : to deal with.
> then there is a missing )
> then there is a missing (
> 
> Once you fix all the problems then you should see
> good bye
> since that is the only executable code in Area.py other than def statements.
> 
> Suggestion: start with a VERY SIMPLE program and get that working. Then add 
> one new feature at a time.
> 
> -- 
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
> 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Syntax error help

2012-03-30 Thread bob gailer

On 3/30/2012 6:20 PM, x23ch...@gmail.com wrote:

Thanks I've fixed
Great. Please share with us your new program and tell us what you do to 
run it.


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Problem Stripping

2012-03-30 Thread Mark Lawrence

On 30/03/2012 18:28, Joel Goldstick wrote:

On Fri, Mar 30, 2012 at 1:25 PM, Joel Goldstick
  wrote:

On Fri, Mar 30, 2012 at 1:09 PM, leam hall  wrote:

Python 2.4.3 on Red Hat 5. Trying to use strip to remove characters
but it doesn't seem to work like I thought.


res = subprocess.Popen(['uname', '-a'], stdout=subprocess.PIPE)
uname = res.stdout.read().strip()


uname

'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
i686 i686 i386 GNU/Linux'


uname.strip(':')

'Linux myserver 2.6.18-274.el5PAE #1 SMP Fri Jul 8 17:59:09 EDT 2011
i686 i686 i386 GNU/Linux'


'www.example.com'.strip('cmowz.')

'example'

Thoughts?

Leam
--
Mind on a Mission
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


str.strip() removes characters from begining and end of the string --
Not any in between.  Notice


example = "www.example.com".strip("wcomx")
example

'.example.'




The x remains



You could do list comprehension



n = "".join([x for x in "this has : some : colons" if x not in ':'])
n

'this has  some  colons'




Yuck :(

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit 
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>> "this has : some : colons".replace(':', '')
'this has  some  colons'

--
Cheers.

Mark Lawrence.

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


Re: [Tutor] Problem Stripping

2012-03-30 Thread bob gailer

Then, of course, there's "15:45".replace(':','')

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Syntax error help

2012-03-30 Thread bob gailer

Please always reply-all so a copy goes to the list.

On 3/30/2012 8:01 PM, chris knarvik wrote:
that was incomplete it was supposed to be ive fixed most of my 
problems with your help


That's great. Would you post the correct program so we can all learn? 
And possibly make other helpful suggestions.



what do you mean what do i do to run it

There are several ways to execute (run) a Python program.
You can double-click the file in an explorer.
You can at a command prompt type (e.g.) >python Area.py
or you can start an interactive session then type >>>import Area
or you can use an IDE such as IDLE.
  within IDLE you can write the program in an edit window then RUN
  or you can use the interactive window and type >>>import Area
Which of these (or what else) do you do

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Syntax error help

2012-03-30 Thread S.Irfan Rizvi
Please remove me from listi can't do itthey are doing it Attention
On Mar 30, 2012 9:21 PM, "bob gailer"  wrote:

> Please always reply-all so a copy goes to the list.
>
> On 3/30/2012 8:01 PM, chris knarvik wrote:
>
>> that was incomplete it was supposed to be ive fixed most of my problems
>> with your help
>>
>
> That's great. Would you post the correct program so we can all learn? And
> possibly make other helpful suggestions.
>
>  what do you mean what do i do to run it
>>
> There are several ways to execute (run) a Python program.
> You can double-click the file in an explorer.
> You can at a command prompt type (e.g.) >python Area.py
> or you can start an interactive session then type >>>import Area
> or you can use an IDE such as IDLE.
>  within IDLE you can write the program in an edit window then RUN
>  or you can use the interactive window and type >>>import Area
> Which of these (or what else) do you do
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor