[Tutor] new line to list of strings send by email

2016-01-08 Thread Emil Natan
Hello list,

I have a function which receives a string and sends it as a body of an
email.

It is a part of a program which does certain checks on network
infrastructure. When a check fails I append error message to a
error_collector list:


if self.check_axfr_refused(ip):
 error_collector.append('%s:%s AXFR test for %s FAILED' %
   (ns, ip, self.domainname))

At the  end I send the alert like this:

if len(error_collector) != 0:
email_body = str(error_collector)
email_alert(email_body)

The problem is the resulted email (expectedly) has the alert message as one
long string.

['pdns6.ultradns.co.uk.:204.74.115.1 AXFR test for amazon.com FAILED',
'pdns6.ultradns.co.uk.:2610:a1:1017::1 AXFR test for amazon.com FAILED',
'ns4.p31.dynect.net.:204.13.251.31 AXFR test for amazon.com FAILED',...]

I tried adding '\n' to end of each string error_collector collects, but
then these were simply added to the resulted email.

What I want to achieve is that each collected error is shown on a separate
line in the email. Any advice will be well appreciated.

Here is the email sending function if in interest:

def email_alert(message, recipient=DEFAULT_RECIPIENT, subject_prefix=''):
''' Send email alert. '''
# check if we are running in quiet mode
if QUIET.lower() == 'yes':
return
msg = MIMEText(message)
msg['From'] = SENDER
msg['To'] = recipient
msg['Subject'] = subject_prefix + SUBJECT

s = smtplib.SMTP(SMTP_SERVER)
s.send_message(msg)
s.quit()

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


Re: [Tutor] new line to list of strings send by email

2016-01-08 Thread Bob Gailer
On Jan 8, 2016 11:03 AM, "Emil Natan"  wrote:
>
> Hello list,
>
> I have a function which receives a string and sends it as a body of an
> email.
>
> It is a part of a program which does certain checks on network
> infrastructure. When a check fails I append error message to a
> error_collector list:
>
>
> if self.check_axfr_refused(ip):
>  error_collector.append('%s:%s AXFR test for %s FAILED' %
>(ns, ip, self.domainname))
>
> At the  end I send the alert like this:
>
> if len(error_collector) != 0:
> email_body = str(error_collector)
> email_alert(email_body)
Instead of str( str(error_collector) ) try '\n'.join(error_collector)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[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 bdr...@crosswire.org 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


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


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] 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] 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] New to this list ....

2012-03-30 Thread Cranky Frankie
Message: 1
Date: Fri, 30 Mar 2012 15:04:09 +0100
Barry Drake bdr...@crosswire.org 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=UTF8qid=1333131438sr=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] 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=UTF8qid=1333131438sr=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] 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=UTF8qid=1333131438sr=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] new on the list

2008-02-28 Thread Alan Gauld

[EMAIL PROTECTED] wrote

 This is my first post to this list.

Welcome Tony.

 I started learning to write bash scripts in November, first,
 and started learning Tcl in January

Good starts, although Python is a little different to Tcl its
generally easier to read. But their capabilities are similar.

 I've played a little with java, but haven't really
 gotten beyond Hello, World!, same with C.

Compiled languages take a bit more getting used to,
especially since both C and Java require more detailed
work from the programmer.

 from random import randint

 ing = (pepperoni, ham, onions, sausage, bacon, peppers, 
 spiders, mozzarella, anchovies, chocolate chips, dog 
 hairs, toe-nail clippings, fried clams, lollipops, paper 
 clips, calamari, mms, broccoli, gum drops, green beans)

 print Let's make a pizza with

 for i in range(5):
 p = randint(0,19) # someone told me I could have done len(ing), but 
 I do not know what 'len' is yet.

len() is a general purpose function inPython that will tell you
the length of things. In this case it would tell you how many
items in your tuple ing. It can also tell you how many characters
in a string etc.

There are lots of other things in the random module you could have
used, including random.choice...

 He told me to write a program that would print 5 lines, like this:
 *
 **
 ***

 I thought this one was easy.

Me too...

 t = *

 for i in range(5):
 print t * (i+1)

 How'd I do?

OK, but its not really configurable so maybe you should read
the number of lines from the user somehow? Also you could
avoid the addition each time by moving it to the range function

for i in range(1, i+1):
   print t * i

 (I wrote these using a simple text editor that I made with Tcl,
 too, http://www.linguasos.org/tcltext.html )

Fine but it will be easier to use a syntax aware full featured
editor like vim or Idle or emacs or Scite

But overall you are on the right lines.


-- 
Alan Gauld
Author of the Learn to Program web site
Temorarily at:
http://uk.geocities.com/[EMAIL PROTECTED]/
Normally:
http://www.freenetpages.co.uk/hp/alan.gauld



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] new on the list

2008-02-28 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
 Hi,
 
 I'm Tony.  I'm a translator.
 This is my first post to this list.

Welcome!


 tcl.tk makes
 it easy to build a gui, as easy as writing html, really).

Python has a version of tk also, called Tkinter. You might want to learn 
about it:
http://docs.python.org/lib/module-Tkinter.html

 I have made a couple of friends on-line, real
 hackers, who have decided to help teach me.

There are several good Python tutorials for non-programmers available 
on-line, you might want to read one of them:
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

The book Python Programming for absolute beginners is another good 
place to start. It has exercises similar to the ones your friends gave you.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] new on the list

2008-02-28 Thread bhaaluu
On Thu, Feb 28, 2008 at 4:51 AM, Alan Gauld [EMAIL PROTECTED] wrote:
  [EMAIL PROTECTED] wrote

   (I wrote these using a simple text editor that I made with Tcl,
   too, http://www.linguasos.org/tcltext.html )

  Fine but it will be easier to use a syntax aware full featured
  editor like vim or Idle or emacs or Scite

  But overall you are on the right lines.


  --
  Alan Gauld
  Author of the Learn to Program web site
  Temorarily at:
  http://uk.geocities.com/[EMAIL PROTECTED]/
  Normally:
  http://www.freenetpages.co.uk/hp/alan.gauld

I also run Python on Linux.  I've tried several of the Python IDEs
(Integrated Development Environments), such as IDLE, Eric, and
so forth, but the best (for me) has been vim. I use the following
.vimrc file:

-8-Cut Here-8---
 .vimrc

 Created by Jeff Elkner 23 January 2006
 Last modified 2 February 2006

 Turn on syntax highlighting and autoindenting
syntax enable
filetype indent on
 set autoindent width to 4 spaces (see
 http://www.vim.org/tips/tip.php?tip_id=83)
set et
set sw=4
set smarttab
 set line number (added by bhaaluu)
set nu
 Bind f2 key to running the python interpreter on the currently active
 file.  (courtesy of Steve Howell from email dated 1 Feb 2006).
map f2 :w\|!python %cr
-8-Cut Here-8---

I run vim in Konsole, but any Xterm works, AFAIK.
Since the Traceback exceptions in Python usually have a line number,
I added that 'feature' to the .vimrc file.
Note the last line that starts with 'map'. That allows you to run your
typed-in Python program from within vim by simply pressing the F2 function key.
At the end of the run, you'll be prompted to press Enter to return to editing
your program in the vim editor.

Since I have vim linked to the 'vi' command, all I have to do to start editing
a new program is to enter a command similar to this at the bash prompt:

$ vi myNewPythonProgram.py

Then, I stay in vim to edit the program, run it, modify it, debug it, etc.
The syntax highlighting and autoindent features enabled in the .vimrc file
make programming in Python a fun and enjoyable experience.

Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m
You assist an evil system most effectively by obeying its
orders and decrees. An evil system never deserves such
allegiance.  Allegiance to it means partaking of the evil.
A good person will resist an evil system with his or her
whole soul. [Mahatma Gandhi]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] new on the list

2008-02-27 Thread tonytraductor
Hi,

I'm Tony.  I'm a translator.
This is my first post to this list.
I've been using Linux, pretty well exclusively,
for about 8 years, but I never got under the hood
more than learning the shell basics to do 
basic stuff that I needed to do, manipulating config files.
(Although I did recently make my own distro for translators,
but it's just a remaster of PCFluxboxOS with some changes to
the software to include some tools for translators, linguasos.org).
But, I've finally decided that I want more control
over my machine than just manipulating configs, and I want some
software that currently doesn't exist, too.
So, I've decided to learn to program.
This was in November, so, I'm very, very new to programming.
I started learning to write bash scripts in November, first,
and started learning Tcl in January (I feel like
I'm making a lot of progress with Tcl, and have
even written a program for translator project mgmt
that a number of folks are using already transprocalc.org
a very basic program, really, but tcl.tk makes
it easy to build a gui, as easy as writing html, really).
I've played a little with java, but haven't really
gotten beyond Hello, World!, same with C.
But, ESR says if I want to be a hacker, 
Python is the best place to start, 
so, this week I started learning python.

I have made a couple of friends on-line, real
hackers, who have decided to help teach me.

One gave me an assignment to write a program
that would generate 5 random numbers, using a loop.
I wrote:

# Let's make a pizza.
# pizza.py
# escrito por anthony baldwin / [EMAIL PROTECTED]

from random import randint

print I'm hungry!
print Let's make a nice pizza, eh!

ing = (pepperoni, ham, onions, sausage, bacon, peppers, spiders, 
mozzarella, anchovies, chocolate chips, dog hairs, toe-nail 
clippings, fried clams, lollipops, paper clips, calamari, mms, 
broccoli, gum drops, green beans)

print Let's make a pizza with

for i in range(5):
p = randint(0,19) # someone told me I could have done len(ing), but I 
do not know what 'len' is yet.
print ing[p]

print 
print That's a nice pizza, eh! 
print Mangeamos!

The other told me that the following was used to separate the sheep
from the goats, so to speak, which I don't quite understand, since it
took me all of 10 minutes to figure it out. 
He told me to write a program that would print 5 lines, like this:
*
**
***

*
With a loop, and in such fashion that it could be easily extended to
continue up to any number of lines.
I thought this one was easy.
I did:

# twinkle stars python / twinkle.py
# escrito por anthony baldwin / [EMAIL PROTECTED]

t = *
print Twinkle, twinkle little stars.

for i in range(5):
print t * (i+1)

print Twinkle, twinkle...

So.

How'd I do?

(I wrote these using a simple text editor that I made with Tcl,
too, http://www.linguasos.org/tcltext.html )

/tony

--
http://www.LinguasOS.org - Linux for Translators
http://www.BaldwinLinguas.com - translating  interpreting
http://www.TransProCalc.org - Free translation project mgmt software

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor