Re: [Tutor] Convert values in a list back and forth from ints

2009-01-07 Thread prasad rao
. class Value:def __init__(self, vs = ""):
>...   self.vs = vs
>...   self.v = self.asInt()
>...def asInt(self):
>...   try: self.v = int(self.vs)
>...   except: self.v = None
>...   return self.v
>...def asString(self):
>...vs = str(self.vs)
>...return self.vs
>...def setStringValue(self, s):
>...self.vs = s
>...self.asInt()
>...def setIntValue(self, i):
>...self.v = int(i)
>...self.vs = str(self.v)

hello
Sorry to interject.
This class seems asymmetric.

Class Value:
def __init__(self,inte='',stri=''):
  self.inte=inte
  self.stri=stri
def setvalue(inte='',stri=''):
 self.stri=str(self.inte)
 self.int=int(self.stri)
I tried. But failed.What is wrong with above code?

 >>> Class Value:
  def __init__(self,inte='',stri=''):
  self.inte=inte
  self.stri=stri
  def setvalue(inte='',stri=''):
 self.stri=str(self.inte)
 self.int=int(self.stri)

SyntaxError: invalid syntax

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


Re: [Tutor] Getting multi-line input from user

2009-01-07 Thread Alan Gauld


"wormwood_3"  wrote


On point 2, say I enter:
Enter text, 'done' on its own line to quit:
I am a sentence. I am another sentence.

I am a new paragraph.
done

What I get out is:
I am a sentence. I am another sentence.  I am a new paragraph.

But that just shows the double new lines of my new paragraph as an 
extra space.


raw_input strips off the newline character created by Enter.
You need to add it back in:

entry = raw_input()
user_input.append(entry+'\n')

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk


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


Re: [Tutor] Getting multi-line input from user

2009-01-07 Thread Alan Gauld


"Andre Engels"  wrote 


The newline character is written \n in Python, so if you replace

' '.join(user_input)

by

'\n'.join(user_input)


Yep, that's better than my suggestion of adding 
the \n at append time. :-)


Alan G.

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


[Tutor] SMTP Module Help

2009-01-07 Thread Marco Petersen
I'm using Python 2.5.4. I wanted to try out the SMTP module. I tried to 
send an email through my Gmail account but it keeps saying that the 
connection was refused.


This is the code that I used :

import smtplib
msg = 'Test'


server = smtplib.SMTP('smtp.gmail.com')
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login('marco.m.peter...@gmail.com', 'password')
server.sendmail('marco.m.peter...@gmail.com', 
'marcoleepeter...@gmail.com', msg)

server.close()

This error message keeps coming up:


Traceback (most recent call last):
 File "C:/Python25/send_mail.py", line 5, in 
   server = smtplib.SMTP('smtp.gmail.com')
 File "C:\Python25\Lib\smtplib.py", line 244, in __init__
   (code, msg) = self.connect(host, port)
 File "C:\Python25\Lib\smtplib.py", line 310, in connect
   raise socket.error, msg
error: (10061, 'Connection refused')


Can anyone help me with this?

Thanks.

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


Re: [Tutor] Convert values in a list back and forth from ints

2009-01-07 Thread Andre Engels
On Wed, Jan 7, 2009 at 9:45 AM, prasad rao  wrote:

> hello
> Sorry to interject.
> This class seems asymmetric.
> Class Value:
> def __init__(self,inte='',stri=''):
>   self.inte=inte
>   self.stri=stri
> def setvalue(inte='',stri=''):
>  self.stri=str(self.inte)
>  self.int=int(self.stri)
> I tried. But failed.What is wrong with above code?
>
>  >>> Class Value:
>   def __init__(self,inte='',stri=''):
>   self.inte=inte
>   self.stri=stri
>   def setvalue(inte='',stri=''):
>  self.stri=str(self.inte)
>  self.int=int(self.stri)
>
> SyntaxError: invalid syntax

'class' should not start with a capital. I do by the way wonder what
your code is supposed to accomplish - why have arguments inte and stri
for setvalue when you don't use them (you use the existing value of
self.stri).



-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] re

2009-01-07 Thread prasad rao
Hello
I am trying to get a value as integer and a string.

>>> class Value:
  def __init__(self,inte='',stri=''):
  self.inte=inte
  self.stri=stri
  def setvalue(self,inte='',stri=''):
 self.stri=str(self.inte)
 self.inte=int(self.stri)


>>> v=Value(45)
>>> print v.stri

>>> v.inte
45
>>> v.stri
''
>>> c=Value('36')
>>> c.stri
''
>>>
But it is not converting.How can I do it?

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


[Tutor] good day

2009-01-07 Thread Kgotlelelo Legodi
good day

I just started using python and i want to know how can i solve a boundary value 
problem for ordinary differential equations using shooting method in python.use 
the general equation to demonstrate the python code.

Thank you


-- 
This message is subject to the CSIR's copyright terms and conditions, e-mail 
legal notice, and implemented Open Document Format (ODF) standard. 
The full disclaimer details can be found at 
http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner, 
and is believed to be clean.  MailScanner thanks Transtec Computers for their 
support.

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


Re: [Tutor] re

2009-01-07 Thread Andre Engels
On Wed, Jan 7, 2009 at 10:57 AM, prasad rao  wrote:
> Hello
> I am trying to get a value as integer and a string.
 class Value:
>   def __init__(self,inte='',stri=''):
>   self.inte=inte
>   self.stri=stri
>   def setvalue(self,inte='',stri=''):
>  self.stri=str(self.inte)
>  self.inte=int(self.stri)
>
 v=Value(45)
 print v.stri
 v.inte
> 45
 v.stri
> ''
 c=Value('36')
 c.stri
> ''

> But it is not converting.How can I do it?
> Prasad
>

Let's take a step back, and see what is happening here.

 v=Value(45)

This means that you create a Value object. If you create an object,
its __init__ is called. What does that ini look like?

def __init__(self,inte='',stri='')

We disregard the 'self' for now, and look at the other arguments. The
_first_ argument of your function will be mapped to inte, the second
to stri. In this case you have only one argument, so this argument
(45) will be mapped to inte, and stri will take its default value
(which is '')

>   self.inte=inte
>   self.stri=stri

So after this, self.inte will be set to inte (that is 45), and
self.stri to stri (that is ''). Which indeed is exactly what you get
when you look up v.inte and v.stri.

 c=Value('36')

If we go through the analysis above, you will find that after this
c.inte = '36'
c.stri = ''


What would you _want_ to do? I think what you want is the following:
you give a single argument, which can either be an integer or a string
representing an integer, and the local variables inte and string
should then give the integer and string representation of that
argument. This can be done in the following way:

class Value:
def __init__(self,val):  # I do not add a default value for val,
because I will always call Value(somevalue), not
Value()
self.inte = int(val) # Whether val is a string or an integer,
this will be its integer representation
self.stri = str(val) # similar

(when typing this in you can of course do without the #s and whatever
I have written after that)

-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive programming.

2009-01-07 Thread A.T.Hofkamp

WM. wrote:

Norman Khine wrote:

 >>> i = 5
 >>> j = 7
 >>> if i <= j:
... print 'nudge', 'nudge'
... else:
... print 'whatever'
...
nudge nudge
 >>>


Yes, I understand how your program works.  What I do not understand is 
how you got it.  My program came out in IDLE as you see it.  No ..., 
different indentation, an error message before I could add the else 
alternative.  (Which, as a Pythonista, one should know, is "Wink-wink".)


In IDLE in the window with the ">>>" prompt:

After the colon at the 'if' line, you press ENTER (ie you tell Python 'the 
line has ended here').
The interpreter then supplies the "..." to denote that you are entering a 
multi-line statement.


You then continue entering the next lines (with some indenting for both 
branches!!), pressing ENTER after each line.


At the end (just above the "nudge nudge" output), Python also gives a "..." 
prompt, to give you the option to enter more statements under the last print.
Since you don't have any more statements, just press ENTER (saying, 'I am 
finished').


Then Python will execute the complete if statement (and one of the branches), 
and output the result.




The normal approach however for entering a program is to use an editor window.
Click on 'new file' somewhere in IDLE (I am not an IDLE user nor do I have the 
program around, so I cannot give you more precise directions unfortunately), 
and enter the Python program without the ">>>" and "..." stuff at the start of 
the line.
Once you are finished, save it, and press 'run' (it is in a menu somewhere if 
I remember correctly).


Then the Python interpreter will run all statements in the file.

With this approach you can also load (and run) previous made programs (which 
is a quite useful feature, I might add :) ).

(Instead of 'new file', use 'load file' or 'edit file').


Good luck,
Albert

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


Re: [Tutor] good day

2009-01-07 Thread A.T.Hofkamp

Kgotlelelo Legodi wrote:

good day

I just started using python and i want to know how can i solve a boundary

value problem for ordinary differential equations using shooting method in
python.use the general equation to demonstrate the python code.


I have no background in numeric calculations, but for starters, you may want 
to investigate the numpy and/or scipy Python packages. Google will probably 
also give you good starting points of existing numeric code/libraries.



As for actually writing the program, I am afraid you'll have to do that 
yourself.


Sincerely,
Albert

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


Re: [Tutor] SMTP Module Help

2009-01-07 Thread A.T.Hofkamp

Marco Petersen wrote:
I'm using Python 2.5.4. I wanted to try out the SMTP module. I tried to 
send an email through my Gmail account but it keeps saying that the 
connection was refused.


This is the code that I used :

import smtplib
msg = 'Test'


server = smtplib.SMTP('smtp.gmail.com')
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login('marco.m.peter...@gmail.com', 'password')
server.sendmail('marco.m.peter...@gmail.com', 
'marcoleepeter...@gmail.com', msg)

server.close()

This error message keeps coming up:


Traceback (most recent call last):
 File "C:/Python25/send_mail.py", line 5, in 
   server = smtplib.SMTP('smtp.gmail.com')
 File "C:\Python25\Lib\smtplib.py", line 244, in __init__
   (code, msg) = self.connect(host, port)
 File "C:\Python25\Lib\smtplib.py", line 310, in connect
   raise socket.error, msg
error: (10061, 'Connection refused')


'Connection refused' usually means that the other side doesn't like you for 
some reason.
(In other words, it doesn't look like a Python problem but like a network 
problem.)


Since it happens before you can start the protocol, my guess is that 
smtp.gmail.com does not accept your machine as valid to speak SMTP with (at 
least not at the port you are trying).


The person to talk to would be the owner/admin of smtp.gmail.com.


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


[Tutor] linked list with cycle structure

2009-01-07 Thread David Hláčik
Hello guys,

I have a linked list where *number of elements is unlimited* and
**last element points on random (can be first, last, in middle ,
anywhere) element within linked list** - this is important . My goals
is to create an architecture /scheme for **algoritmus which will count
total number of elements** of such linked list.
Yes , maybe it sounds strange, but we need to implement this and i
would be very gladfull for your toughts.

Thanks in advance and wishing you a sucessfull year!

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


[Tutor] Brand new to python have simple problem

2009-01-07 Thread mickth

I'm brand new to python and every time i start to learn it i get the same
problem and end up giving up and moving on to another language.

the print statement doesn't work in either the comand line or IDLE i get
this in IDLE:
SyntaxError: invalid syntax (, line 1)

and just SyntaxError: invalid syntax in the command line.

Even doing the hello world or doing simple sums. If I type "hello, world" i
get the right outcome but print "Hello world" i get the error. When i type
print it's purple in IDLE if that means anything.

I've tried reinstalling python and tried an earlier version with same
results. i'm running windows vista home prem 32bit
  
I know i may seem an imbecile for posting such a simple thing but i'm
determined to learn it this time i've already learnt vb and C and want to
broaden out!

Thanks for any help


-- 
View this message in context: 
http://www.nabble.com/Brand-new-to-python-have-simple-problem-tp21325502p21325502.html
Sent from the Python - tutor mailing list archive at Nabble.com.

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


Re: [Tutor] Opening a window to fit an image

2009-01-07 Thread Michael Lam
I got a similar problem, except that calling it twice wouldn't work for me. I 
tried looking everywhere and I seemed to be doing everything exactly like other 
people. After fiddling around, I got this order:

pygame.init()
pygame.display.set_icon(pygame.image.load("image.bmp"))
window = pygame.display.set_mode((640, 480
pygame.display.set_caption('Caption')

Something wrong with using Surface.convert() as I originally used a 
load_image() function that did that.

Michael

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


Re: [Tutor] Interactive programming.

2009-01-07 Thread Kent Johnson
On Wed, Jan 7, 2009 at 5:28 AM, A.T.Hofkamp  wrote:
> WM. wrote:
>>
>> Norman Khine wrote:
>>>
>>>  >>> i = 5
>>>  >>> j = 7
>>>  >>> if i <= j:
>>> ... print 'nudge', 'nudge'
>>> ... else:
>>> ... print 'whatever'
>>> ...
>>> nudge nudge
>>>  >>>
>
>> Yes, I understand how your program works.  What I do not understand is how
>> you got it.  My program came out in IDLE as you see it.  No ..., different
>> indentation, an error message before I could add the else alternative.
>>  (Which, as a Pythonista, one should know, is "Wink-wink".)
>
> In IDLE in the window with the ">>>" prompt:
>
> After the colon at the 'if' line, you press ENTER (ie you tell Python 'the
> line has ended here').
> The interpreter then supplies the "..." to denote that you are entering a
> multi-line statement.

No, IDLE does not supply the ..., that is the source of the confusion.

If you use the interpreter directly from a command/terminal window it
will supply the ... continuation prompt but the IDLE shell will not.

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


[Tutor] shooting method in python code

2009-01-07 Thread Kgotlelelo Legodi
good day

I just started using python and i want to know how can i solve a boundary value 
problem for ordinary differential equations using shooting method in python.use 
the general equation to demonstrate the python code.

Thank you

Kgotlelelo Legodi
Natural Resources and the Environment
CSIR
012 841 3402 (Tel)
012 841 4322 (Fax)
072 344 3846 (Cell)
kle...@csir.co.za


-- 
This message is subject to the CSIR's copyright terms and conditions, e-mail 
legal notice, and implemented Open Document Format (ODF) standard. 
The full disclaimer details can be found at 
http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner, 
and is believed to be clean.  MailScanner thanks Transtec Computers for their 
support.

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


Re: [Tutor] linked list with cycle structure

2009-01-07 Thread A.T.Hofkamp

David Hláčik wrote:

Hello guys,

I have a linked list where *number of elements is unlimited* and
**last element points on random (can be first, last, in middle ,
anywhere) element within linked list** - this is important . My goals
is to create an architecture /scheme for **algoritmus which will count
total number of elements** of such linked list.
Yes , maybe it sounds strange, but we need to implement this and i
would be very gladfull for your toughts.


Python supplies a list data structure for you, so in 'normal' Python, you 
wouldn't bother with linked lists and all the assoicated problems.


However, if you really want:
Make a class for a list-element, with a next/prev variable (if you have a 
double-linked list).
Instantiate as many objects as you like from that class, and let the next/prev 
variables from one object refer to other objects that you instantiated.

(Python doesn't care where a variable refers to.)

For the NULL value you can use None.


Sincerely,
Albert

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


Re: [Tutor] Brand new to python have simple problem

2009-01-07 Thread Andre Engels
On Wed, Jan 7, 2009 at 7:06 AM, mickth  wrote:
>
> I'm brand new to python and every time i start to learn it i get the same
> problem and end up giving up and moving on to another language.
>
> the print statement doesn't work in either the comand line or IDLE i get
> this in IDLE:
> SyntaxError: invalid syntax (, line 1)
>
> and just SyntaxError: invalid syntax in the command line.
>
> Even doing the hello world or doing simple sums. If I type "hello, world" i
> get the right outcome but print "Hello world" i get the error. When i type
> print it's purple in IDLE if that means anything.
>
> I've tried reinstalling python and tried an earlier version with same
> results. i'm running windows vista home prem 32bit
>
> I know i may seem an imbecile for posting such a simple thing but i'm
> determined to learn it this time i've already learnt vb and C and want to
> broaden out!

Could you please give an exact copy (both your commands and the
reactions, with copy and paste) of the idle session that leads to the
error message? Here is what I get (working), you might be able to find
from this where it goes wrong, but if not, please show what you get so
we can look at it:

>>> "Hello world"
'Hello world'
>>> print "Hello world"
Hello world
>>>



-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] good day

2009-01-07 Thread Kent Johnson
On Wed, Jan 7, 2009 at 4:46 AM, Kgotlelelo Legodi  wrote:
> good day
>
> I just started using python and i want to know how can i solve a boundary 
> value problem for ordinary differential equations using shooting method in 
> python.use the general equation to demonstrate the python code.

Googling "python boundary value problem" finds these:
http://books.google.com/books?id=WiDie-hev1kC&pg=PA295&lpg=PA295&dq=python+boundary+value+problem&source=web&ots=naWIAjsXpq&sig=lw9U0c-lQe1rUtlWv8W0cKXUGHI&hl=en&sa=X&oi=book_result&resnum=1&ct=result#PPA295,M1

http://lbolla.wordpress.com/2008/04/14/bvp/
http://www.scilab.org/doc/manual/Docu-html659.html
http://projects.scipy.org/pipermail/scipy-user/2006-October/009483.html

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


Re: [Tutor] SMTP Module Help

2009-01-07 Thread Kent Johnson
On Tue, Jan 6, 2009 at 4:45 AM, Marco Petersen
 wrote:
> I'm using Python 2.5.4. I wanted to try out the SMTP module. I tried to send
> an email through my Gmail account but it keeps saying that the connection
> was refused.
>
> This is the code that I used :
>
> import smtplib
> msg = 'Test'
>
>
> server = smtplib.SMTP('smtp.gmail.com')

>From the docs here
http://mail.google.com/support/bin/answer.py?answer=77662
Google SMTP uses port 587, not the standard SMTP port 25. Try
  server = smtplib.SMTP('smtp.gmail.com', 587)

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


Re: [Tutor] Interactive programming.

2009-01-07 Thread A.T.Hofkamp

Kent Johnson wrote:

On Wed, Jan 7, 2009 at 5:28 AM, A.T.Hofkamp  wrote:

WM. wrote:

Norman Khine wrote:

 >>> i = 5
 >>> j = 7
 >>> if i <= j:
... print 'nudge', 'nudge'
... else:
... print 'whatever'
...
nudge nudge
 >>>

Yes, I understand how your program works.  What I do not understand is how
you got it.  My program came out in IDLE as you see it.  No ..., different
indentation, an error message before I could add the else alternative.
 (Which, as a Pythonista, one should know, is "Wink-wink".)

In IDLE in the window with the ">>>" prompt:

After the colon at the 'if' line, you press ENTER (ie you tell Python 'the
line has ended here').
The interpreter then supplies the "..." to denote that you are entering a
multi-line statement.


No, IDLE does not supply the ..., that is the source of the confusion.

If you use the interpreter directly from a command/terminal window it
will supply the ... continuation prompt but the IDLE shell will not.


Thank you Kent, I didn't know that.

The story stays the same, except that you don't get the "... " from Python.
That means that the second line and further are all shifted 4 positions to the 
left, so you'd need to enter something like:


>>> i = 5
>>> j = 7
>>> if i <= j:
 print 'nudge', 'nudge'
else:
 print 'whatever'


I agree that this is highly confusing.


The best solution is probably to use the editor window instead, and save/run 
your programs from there.


Sincerely,
Albert

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


Re: [Tutor] linked list with cycle structure

2009-01-07 Thread Kent Johnson
On Wed, Jan 7, 2009 at 5:59 AM, David Hláčik  wrote:
> Hello guys,
>
> I have a linked list where *number of elements is unlimited* and
> **last element points on random (can be first, last, in middle ,
> anywhere) element within linked list** - this is important . My goals
> is to create an architecture /scheme for **algoritmus which will count
> total number of elements** of such linked list.

So the list is effectively endless because it has a cycle?

You could walk the list, adding elements (or their ids) to a set,
stopping when you get to an element that is in the set already. Then
the size of the set is the size of the list.

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


Re: [Tutor] Brand new to python have simple problem

2009-01-07 Thread Kent Johnson
On Wed, Jan 7, 2009 at 1:06 AM, mickth  wrote:
>
> I'm brand new to python and every time i start to learn it i get the same
> problem and end up giving up and moving on to another language.
>
> the print statement doesn't work in either the comand line or IDLE i get
> this in IDLE:
> SyntaxError: invalid syntax (, line 1)
>
> and just SyntaxError: invalid syntax in the command line.
>
> Even doing the hello world or doing simple sums. If I type "hello, world" i
> get the right outcome but print "Hello world" i get the error. When i type
> print it's purple in IDLE if that means anything.

What version of Python are you using? In Python 3.0, print is a
function, not a statement, so you need parentheses:
  print("Hello world")

Most Python books and tutorials are written for Python 2.x. Make sure
your references match the Python version you are using, there are
significant changes in 3.0.

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


Re: [Tutor] Getting multi-line input from user

2009-01-07 Thread wormwood_3
Joining with newline works perfectly. 

Thanks everyone!

 ___
Samuel Huckins


Homepage - http://samuelhuckins.com
Tech blog - http://dancingpenguinsoflight.com/
Photos - http://www.flickr.com/photos/samuelhuckins/
AIM - samushack | Gtalk - samushack | Skype - shuckins





From: Alan Gauld 
To: tutor@python.org
Sent: Wednesday, January 7, 2009 3:58:18 AM
Subject: Re: [Tutor] Getting multi-line input from user


"Andre Engels"  wrote 
> The newline character is written \n in Python, so if you replace
> 
> ' '.join(user_input)
> 
> by
> 
> '\n'.join(user_input)

Yep, that's better than my suggestion of adding the \n at append time. :-)

Alan G.

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


Re: [Tutor] SMTP Module Help

2009-01-07 Thread Ole Henning Jensen

Marco Petersen wrote:
I'm using Python 2.5.4. I wanted to try out the SMTP module. I tried to 
send an email through my Gmail account but it keeps saying that the 
connection was refused.


This is the code that I used :

import smtplib
msg = 'Test'


server = smtplib.SMTP('smtp.gmail.com')
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login('marco.m.peter...@gmail.com', 'password')
server.sendmail('marco.m.peter...@gmail.com', 
'marcoleepeter...@gmail.com', msg)

server.close()

This error message keeps coming up:


Traceback (most recent call last):
 File "C:/Python25/send_mail.py", line 5, in 
   server = smtplib.SMTP('smtp.gmail.com')
 File "C:\Python25\Lib\smtplib.py", line 244, in __init__
   (code, msg) = self.connect(host, port)
 File "C:\Python25\Lib\smtplib.py", line 310, in connect
   raise socket.error, msg
error: (10061, 'Connection refused')


Can anyone help me with this?



I must admit that I know nothing of the SMTP module, but I do know that 
with gmail you need to use an SSL secure connection on port 995.


I doesn't seem to be aplied in your script as far as I can tell.

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


[Tutor] New to tkSnack

2009-01-07 Thread CHATURVEDI ALPANA
Hi All,
  I am very new to tkSnack. Trying to understand it. Please help me out. 

I thought of a very simple scenario. I would like to play a audio from line-in 
and then check if the audio is present. If there is no audio for 20 seconds 
then i need to raise an error. Is it possible to do this with tkSnack?If yes, 
then how can we do it?

Regards,
Alpana




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


Re: [Tutor] SMTP Module Help

2009-01-07 Thread Damon Timm
On 1/6/09, Marco Petersen  wrote:
> I'm using Python 2.5.4. I wanted to try out the SMTP module. I tried to send
> an email through my Gmail account but it keeps saying that the connection
> was refused.

I used this example to get emailing from python through gmail smtp to work:

http://codecomments.wordpress.com/2008/01/04/python-gmail-smtp-example/

Note, there are a couple errors in the code that prevent it from
working out-of-the-box ... however, if you read the comments they get
worked out.

Also, I think I would echo what the other folks mentioned here, is
that you probably need to specify the port (587) as in:

server = smtplib.SMTP('smtp.gmail.com', 587)

Damon

>
> This is the code that I used :
>
> import smtplib
> msg = 'Test'
>
>
> server = smtplib.SMTP('smtp.gmail.com')
> server.set_debuglevel(1)
> server.ehlo()
> server.starttls()
> server.ehlo()
> server.login('marco.m.peter...@gmail.com', 'password')
> server.sendmail('marco.m.peter...@gmail.com',
> 'marcoleepeter...@gmail.com', msg)
> server.close()
>
> This error message keeps coming up:
>
>
> Traceback (most recent call last):
>  File "C:/Python25/send_mail.py", line 5, in 
>   server = smtplib.SMTP('smtp.gmail.com')
>  File "C:\Python25\Lib\smtplib.py", line 244, in __init__
>   (code, msg) = self.connect(host, port)
>  File "C:\Python25\Lib\smtplib.py", line 310, in connect
>   raise socket.error, msg
> error: (10061, 'Connection refused')
>
>
> Can anyone help me with this?
>
> Thanks.
>
> -Marco
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SMTP Module Help

2009-01-07 Thread Chris Babcock
On Wed, 07 Jan 2009 15:47:25 +0100
Ole Henning Jensen  wrote:

> Marco Petersen wrote:
> > I'm using Python 2.5.4. I wanted to try out the SMTP module. I
> > tried to send an email through my Gmail account but it keeps saying
> > that the connection was refused.

> > error: (10061, 'Connection refused')
> > 
> > 
> > Can anyone help me with this?
> > 
> 
> I must admit that I know nothing of the SMTP module, but I do know
> that with gmail you need to use an SSL secure connection on port 995.

Yes, 995 is the port for POPS. For inbound mail, you probably want to
use IMAPS on port 993. The original question is about SMTP, however.

Google is offering SMTP submission over TLS on ports 465 and 587. Port
465 was defined as SMTP over SSL in some reference works, but the IETF
still has it listed for a router protocol. Port 587 has been reserved
for mail submission. More importantly for home users, your ISP should
NOT block port 587 (RFC 4409).

Best,
Chris

-- 

Thank you everyone! USAK is live on its new connection.
So far you have given $198 towards next quarter.

To make a donation, use this link (opens on PayPal):
http://tinyurl.com/USAK2009


signature.asc
Description: PGP signature
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] df type function in python

2009-01-07 Thread Spencer Parker
I am looking for a function in python that would operate similar to df.  I
would use df, but I am unable to parse the data that it gives me from
there.  If it would give me a single line I could then split it out if I
needed to, but I can't see how to actually do that.  What I am trying to do
is get some information on a drive I have mounted.  I have a disk image that
I have mounted on temp folder that I need to see how much total pace it is
taking up.  It gives me a more accurate representation of my xen disk images
that just an ls -sk or something on the file.

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


Re: [Tutor] Interactive programming.

2009-01-07 Thread WM.

IDLE 2.6
>>> i = 1
>>> j = 11
>>> if  j > 1:
print j
else:

SyntaxError: invalid syntax
>>>

I am getting a little dizzy here.

I know about text editor, code, save, F5.

Many tutorials say that it is funner and faster to test an idea 
'interactively', using IDLE.  Nothing is said about fiddling the output.


In the above bit the 'else' is in the wrong place because IDLE or PYTHON 
terminated the script.


I hate to be a bore here, but if the mechanics of the program are not as 
they are said to be in the tutorials, how am I to proceed?

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


Re: [Tutor] good day

2009-01-07 Thread Luke Paireepinart
On Wed, Jan 7, 2009 at 4:35 AM, A.T.Hofkamp  wrote:
> Kgotlelelo Legodi wrote:
>>
>> good day
>>
>> I just started using python and i want to know how can i solve a boundary
>
> value problem for ordinary differential equations using shooting method in
> python.use the general equation to demonstrate the python code.
>
>
> I have no background in numeric calculations, but for starters, you may want
> to investigate the numpy and/or scipy Python packages. Google will probably
> also give you good starting points of existing numeric code/libraries.
>
>
> As for actually writing the program, I am afraid you'll have to do that
> yourself.
>
Also, this sounds a lot like you just copied and pasted it from a
homework assignment.
We're not going to be able to provide you with code samples in that
case, but you're welcome to ask more questions and post about any
problems you run into.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive programming.

2009-01-07 Thread Kent Johnson
On Wed, Jan 7, 2009 at 11:46 AM, WM.  wrote:
> IDLE 2.6
 i = 1
 j = 11
 if  j > 1:
>print j
>else:
>
> SyntaxError: invalid syntax

>
> I am getting a little dizzy here.
>
> I know about text editor, code, save, F5.
>
> Many tutorials say that it is funner and faster to test an idea
> 'interactively', using IDLE.  Nothing is said about fiddling the output.
>
> In the above bit the 'else' is in the wrong place because IDLE or PYTHON
> terminated the script.

You have to backspace before typing 'else:' to remove the (automatic)
block indentation. IDLE auto-indents after the 'if:' and it will
continue to indent each line until you tell it to stop by backspacing
over the indent.

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


Re: [Tutor] Interactive programming.

2009-01-07 Thread Edwin Boyette


--- On Wed, 1/7/09, WM.  wrote:


From: WM. 
Subject: Re: [Tutor] Interactive programming.
To: "A.T.Hofkamp" 
Cc: tutor@python.org
Date: Wednesday, January 7, 2009, 11:46 AM


IDLE 2.6
>>> i = 1
>>> j = 11
>>> if  j > 1:
    print j
    else:
        
SyntaxError: invalid syntax
>>>

I am getting a little dizzy here.

I know about text editor, code, save, F5.

Many tutorials say that it is funner and faster to test an idea 
'interactively', using IDLE.  Nothing is said about fiddling the output.

In the above bit the 'else' is in the wrong place because IDLE or PYTHON 
terminated the script.

I hate to be a bore here, but if the mechanics of the program are not as they 
are said to be in the tutorials, how am I to proceed?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
 
 
You could wrapt it all up in a function and then call the function in idle.
 
 
>>> def nudge_nudge():
 i = 5
 j = 7
 if i <= j:
  print "nudge","nudge"
 else:
  print "whatever"

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


Re: [Tutor] df type function in python

2009-01-07 Thread Spencer Parker
Is there anyway to get rid of the header information?  I just want the
output that it gives me for the device.

On Wed, Jan 7, 2009 at 11:02 AM, Stuart Sears  wrote:

> On 07/01/09 16:42, Spencer Parker wrote:
>
>> I am looking for a function in python that would operate similar to df.
>>  I would use df, but I am unable to parse the data that it gives me
>> from there. If it would give me a single line I could then split it out
>> if I needed to, but I can't see how to actually do that. What I am
>> trying to do is get some information on a drive I have mounted. I have a
>> disk image that I have mounted on temp folder that I need to see how
>> much total pace it is taking up. It gives me a more accurate
>> representation of my xen disk images that just an ls -sk or something on
>> the file.
>>
>
> Which version of df do you have? Which OS is this on?
>
> If you want one line per device/mountpoint, df -P will do so on linux
> systems nowadays. I have no idea whether any of this has been ported across
> to other *NIX systems.
>
> Regards,
>
> Stuart
> --
> Stuart Sears RHCA etc.
> "It's today!" said Piglet.
> "My favourite day," said Pooh.
>



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


Re: [Tutor] df type function in python

2009-01-07 Thread Kent Johnson
On Wed, Jan 7, 2009 at 1:08 PM, Spencer Parker  wrote:
> Is there anyway to get rid of the header information?  I just want the
> output that it gives me for the device.

If you know how to get the output of df into Python (e.g. with the
subprocess module or a pipe) then you can use the str.splitlines()
method to divide it into lines and pick out the line you want.

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


Re: [Tutor] df type function in python

2009-01-07 Thread Spencer Parker
What I did was just grep for anything starting with "/"

It then just outputs it to a single line now...and I can split it as
needed.  Thanks again!!1

On Wed, Jan 7, 2009 at 11:33 AM, Kent Johnson  wrote:

> On Wed, Jan 7, 2009 at 1:08 PM, Spencer Parker 
> wrote:
> > Is there anyway to get rid of the header information?  I just want the
> > output that it gives me for the device.
>
> If you know how to get the output of df into Python (e.g. with the
> subprocess module or a pipe) then you can use the str.splitlines()
> method to divide it into lines and pick out the line you want.
>
> Kent
>



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


Re: [Tutor] df type function in python

2009-01-07 Thread Stuart Sears
Redirecting to the list as I seem to have replied off-list rather 
inadvertently... headers...>


On 07/01/09 16:42, Spencer Parker wrote:

I am looking for a function in python that would operate similar to df.
  I would use df, but I am unable to parse the data that it gives me
from there. If it would give me a single line I could then split it out
if I needed to, but I can't see how to actually do that. What I am
trying to do is get some information on a drive I have mounted. I have a
disk image that I have mounted on temp folder that I need to see how
much total pace it is taking up. It gives me a more accurate
representation of my xen disk images that just an ls -sk or something on
the file.


Which version of df do you have? Which OS is this on?

If you want one line per device/mountpoint, df -P will do so on linux 
systems nowadays. I have no idea whether any of this has been ported 
across to other *NIX systems.


Regards,

Stuart
--
Stuart Sears RHCA etc.
"It's today!" said Piglet.
"My favourite day," said Pooh.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] df type function in python

2009-01-07 Thread Stuart Sears

On 07/01/09 18:08, Spencer Parker wrote:

Is there anyway to get rid of the header information?  I just want
the output that it gives me for the device.


parse the output and junk the first line?

I'm no subprocess expert, but a bit of playing about suggests this would
work:

from subprocess import Popen, PIPE

df = Popen('df -P', shell=True, stdout=PIPE)

no_header = df.stdout.readlines()[1:]

I'm sure there are more elegant ways to do this, but it does work.

tuning the df -P command to only give you the device your are interested 
in should reduce the output a little more.



Regards,

Stuart

ps there's need to reply to me directly, I'm subscribed to the list, 
albeit mostly to cower at the feet of those who inhabit it :)

--
Stuart Sears RHCA etc.
"It's today!" said Piglet.
"My favourite day," said Pooh.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] df type function in python

2009-01-07 Thread Roel Schroeven
Spencer Parker schreef:
> I am looking for a function in python that would operate similar to df. 
> I would use df, but I am unable to parse the data that it gives me from
> there.  If it would give me a single line I could then split it out if I
> needed to, but I can't see how to actually do that.  What I am trying to
> do is get some information on a drive I have mounted.  I have a disk
> image that I have mounted on temp folder that I need to see how much
> total pace it is taking up.  It gives me a more accurate representation
> of my xen disk images that just an ls -sk or something on the file.

Some time ago I wrote something like that for a script of mine:

import subprocess

def df(filesystem):
"""Report disk usage.
Return a dictionary with total, used, available. Sizes are reported
in blocks of 1024 bytes."""
output = subprocess.Popen(
['/bin/df', '-B 1024', filesystem],
stdout=subprocess.PIPE).communicate()[0]
lines = output.split('\n')
fs, blocks_total, blocks_used, blocks_available, used, mount =
lines[1].split()
blocks_total = int(blocks_total)
blocks_used = int(blocks_used)
blocks_available = int(blocks_available)
return dict(
total=blocks_total,
used=blocks_used,
available=blocks_available)

I use from a cronjob in a script that monitors the disk usage and sends
me a mail if the disk gets too full.

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
  -- Isaac Asimov

Roel Schroeven

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


[Tutor] Opsware Global Shell Scripting

2009-01-07 Thread Kayvan Sarikhani
Hi there...I'm new to Python scripting, with maybe 3 days under my belt thus
far. Besides the occasional shell script, the last time I ever touched a
programming language was probably at least 12 years ago, if that gives you
any indication of my experience. :)

I don't know if this is the proper place to ask, but I've run into the
problem below.

Right now I'm beginning to work within the Opsware Global Shell, which if
you're unfamiliar with it, basically is exactly what it sounds like...the
goal is to launch a script which changes directories to a list of servers,
prints the server name, remotes into managed virtual servers, checks the
date, and outputs this into a file. I do have a bash script as follows,
which does the job:

#!/bin/bash
#
# This script checks the dates on all managed systems.
OUTFILE="/home/ksarikhani/public/bin/timecheck.txt"
rm -f $OUTFILE
cd "/opsw/Server/@/"
for SERVER_NAME in *
do
   echo  $SERVER_NAME
  echo  $SERVER_NAME >> $OUTFILE
rosh -n $SERVER_NAME -l $LOGNAME \
"date" >> $OUTFILE
done
# Last line in date.sh.

Even though the above does what I need it to do, I'd REALLY like to
determine the equivalent in Python...I've tried several different methods,
but about as far as I get is printing the server name. This is what I've
cobbled together so far.

#!/usr/bin/python
import os, sys
sys.stdout = open('timecheck.txt','w')
for servername in os.listdir('/opsw/Server/@'):
print '---', servername
os.system('rosh -n $SERVER_NAME -l $LOGNAME')
os.system('date')
sys.stdout.close()

The problem is this...for logging into systems via the global shell, one has
to change directories to the one with the list of all managed servers (or
specify the path for each one), and then launch a "rosh -n  -l
" to be able to get into it.

However, when I launch the script, what happens is the following message:
*rosh: Username must be specified with -l or via path*

And this is a continual loop that I can't break.

This is of course not a Python error, but as you might guess from looking at
the script, the whole $SERVER_NAME piece is probably wrong. I thought maybe
I could do something like this...

*os.system('rosh -n', servername, '-l $LOGNAME')*

But of course os.system allows exactly one argument, and not three.

This is where I'm stuck, and I've been digging into the books and examples
and forums and just not quite sure what to look for. I would have liked to
have completed the script without asking for help, but nothing quite seems
to fit what I'm doing.

Any suggestions or guidance would be highly appreciated. Thanks!

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


Re: [Tutor] Interactive programming.

2009-01-07 Thread WM.

Norman Khine wrote:

 >>> i = 5
 >>> j = 7
 >>> if i <= j:
... print 'nudge', 'nudge'
... else:
... print 'whatever'
...
nudge nudge


The above is just what the tutorials said would happen.
Can anyone give me a step-by-step in IDLE 2.6 that would make this happen?


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


Re: [Tutor] Opsware Global Shell Scripting

2009-01-07 Thread Sander Sweers
On Wed, Jan 7, 2009 at 22:04, Kayvan Sarikhani  wrote:
> #!/usr/bin/python
> import os, sys
> sys.stdout = open('timecheck.txt','w')
> for servername in os.listdir('/opsw/Server/@'):
> print '---', servername
> os.system('rosh -n $SERVER_NAME -l $LOGNAME')
> os.system('date')
> sys.stdout.close()
>
> The problem is this...for logging into systems via the global shell, one has
> to change directories to the one with the list of all managed servers (or
> specify the path for each one), and then launch a "rosh -n  -l
> " to be able to get into it.
>
> This is of course not a Python error, but as you might guess from looking at
> the script, the whole $SERVER_NAME piece is probably wrong.

Indeed the variables are wrong

> I thought maybe
> I could do something like this...
>
> os.system('rosh -n', servername, '-l $LOGNAME')
>
> But of course os.system allows exactly one argument, and not three.

Almost, look at the below session in idle. The %s are replaced by the
variables servername and logname. See string formatting [1] for more
info.

>>> servername = 'testserver'
>>> logname = 'logname'
>>> print 'rosh -m %s -l %s' % (servername, logname)
rosh -m testserver -l logname

So you need to use something like below in your loop.

roshCommand = 'rosh -m %s -l %s' % (servername, logname)

os.system(roshCommand)

I do not see the servername variable being assigned a value in your
script so the above will fail. I assume it will be something like

logname = '/var/log/somelogfile'

You might want to look at the subprocess module as this gives you a
lot more control but is also more complicated.

Greets
Sander

[1] http://docs.python.org/library/stdtypes.html#string-formatting-operations
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] linked list with cycle structure

2009-01-07 Thread David Hláčik
Hi,

so okay, i will create a helping set, where i will be adding elements
ID, when element ID will be allready in my helping set i will stop and
count number of elements in helping set. This is how long my cycled
linked list is.

But what if i have another condition , and that is *i can use only
helping memory with constant size* ? This means i am not able to
create any set and adding elements there. I need to have a constant
size variables . This is complication a complication for me.

Thanks in advance!

David

On Wed, Jan 7, 2009 at 2:22 PM, Diez B. Roggisch  wrote:
>
> David Hláčik wrote:
>
> > dictionary with cycle structure
> >
> > Hello guys,
> >
> > I have a linked list where *number of elements is unlimited* and
> > **last element points on random (can be first, last, in middle ,
> > anywhere) element within linked list** - this is important . My goals
> > is to create an architecture /scheme for **algoritmus which will count
> > total number of elements** of such linked list.
> > Yes , maybe it sounds strange, but we need to implement this and i
> > would be very gladfull for your toughts.
>
> Time for homework again? Last time sorting in O(n), now this. How about you
> try something yourself and show us the results - then we might comment on
> enhancements or problems.
>
> Diez
> --
> http://mail.python.org/mailman/listinfo/python-list
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive programming.

2009-01-07 Thread Sander Sweers
On Wed, Jan 7, 2009 at 22:45, WM.  wrote:
> Norman Khine wrote:
>>
>>  >>> i = 5
>>  >>> j = 7
>>  >>> if i <= j:
>> ... print 'nudge', 'nudge'
>> ... else:
>> ... print 'whatever'
>> ...
>> nudge nudge
>
> The above is just what the tutorials said would happen.
> Can anyone give me a step-by-step in IDLE 2.6 that would make this happen?

The above is not from idle but from the interactive python command
line. If you are on windows it is an entry in the python 2.6 start
menu entry.

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


Re: [Tutor] Interactive programming.

2009-01-07 Thread WM.

IDLE 2.6
>>> i = 1
>>> if i > 1:
print 'x'
else:
print 'y'


y
>>>
Last post on this topic, I guess.
I think that the script looks pretty lame, though.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive programming.

2009-01-07 Thread Kent Johnson
On Wed, Jan 7, 2009 at 4:45 PM, WM.  wrote:
> Norman Khine wrote:
>>
>>  >>> i = 5
>>  >>> j = 7
>>  >>> if i <= j:
>> ... print 'nudge', 'nudge'
>> ... else:
>> ... print 'whatever'
>> ...
>> nudge nudge
>
> The above is just what the tutorials said would happen.
> Can anyone give me a step-by-step in IDLE 2.6 that would make this happen?

Type these characters exactly into the IDLE shell window, where
 is the return or Enter key and  is the backspace or
delete key (the one next to the ]} key not the one in the numeric
keypad):

if i<=j:print 'nudge'else:print
'whatever'

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


Re: [Tutor] Interactive programming.

2009-01-07 Thread Kent Johnson
On Wed, Jan 7, 2009 at 5:36 PM, WM.  wrote:
> IDLE 2.6
 i = 1
 if i > 1:
>print 'x'
> else:
>print 'y'
>
>
> y

> Last post on this topic, I guess.
> I think that the script looks pretty lame, though.

It was always lame :-) but you got it to work...

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


Re: [Tutor] Opsware Global Shell Scripting

2009-01-07 Thread Kayvan Sarikhani
On Wed, Jan 7, 2009 at 4:57 PM, spir  wrote:

> Le Wed, 7 Jan 2009 16:04:28 -0500,
> "Kayvan Sarikhani"  a écrit :
>
> > #!/bin/bash
> > #
> > # This script checks the dates on all managed systems.
> > OUTFILE="/home/ksarikhani/public/bin/timecheck.txt"
> > rm -f $OUTFILE
> > cd "/opsw/Server/@/"
> > for SERVER_NAME in *
> > do
> >echo  $SERVER_NAME
> >   echo  $SERVER_NAME >> $OUTFILE
> > rosh -n $SERVER_NAME -l $LOGNAME \
> > "date" >> $OUTFILE
> > done
> > # Last line in date.sh.
>
> > #!/usr/bin/python
> > import os, sys
> > sys.stdout = open('timecheck.txt','w')
> > for servername in os.listdir('/opsw/Server/@'):
> > print '---', servername
> > os.system('rosh -n $SERVER_NAME -l $LOGNAME')
> > os.system('date')
> > sys.stdout.close()
> >
>
> The issue is, when comparing the bash script to the python one, that
> $SERVER_NAME makes sense only
> for bash -- as well as $LOGNAME that you properly changed.
>
> > *os.system('rosh -n', servername, '-l $LOGNAME')*
>
> This goes on the right way. Actually, os.system's arg is a plain string. To
> construct it out of
> several substrings you have to use matching string operators (actually
> methods). To be explicit:
>
> string_shell_command = 'rosh -n' + servername + '-l $LOGNAME'
> or
> string_shell_command = 'rosh -n %s -l $LOGNAME' %servername
> and then:
> os.system(string_shell_command)
>
> Than, I'm not fully sure this will be enough unless $LOGNAME is an env
> variable known by the
> system (I don't know well linux yet). It seems to be, as it is not defined
> in your bash script.
>
> Denis


Ah, that makes sense...I didn't know we could devise our own commands in
strings like that. That helps me quite a lot, actually. Thank you very much,
Denis!

For the record, $LOGNAME works fine as a variable...it's a standard env
variable in Linux to display the current user's username. There's other ways
of getting it, but that seems to be the most exact from what I can gather.

Thanks again, when I make some progress or complete it (hopefully), I'll
post further.

Kayvan

PS - Sorry for the re-send, forgot to include the tutor list.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Opsware Global Shell Scripting

2009-01-07 Thread Kayvan Sarikhani
On Wed, Jan 7, 2009 at 5:09 PM, Sander Sweers wrote:

> On Wed, Jan 7, 2009 at 22:04, Kayvan Sarikhani 
> wrote:
> >
> > This is of course not a Python error, but as you might guess from looking
> at
> > the script, the whole $SERVER_NAME piece is probably wrong.
>
> Indeed the variables are wrong
>

Right...I thought so, but wasn't sure if there was any kind of equivalent
within Python...couldn't hurt to try. :)


> Almost, look at the below session in idle. The %s are replaced by the
> variables servername and logname. See string formatting [1] for more
> info.
>
> >>> servername = 'testserver'
> >>> logname = 'logname'
> >>> print 'rosh -m %s -l %s' % (servername, logname)
> rosh -m testserver -l logname
>
> So you need to use something like below in your loop.
>
> roshCommand = 'rosh -m %s -l %s' % (servername, logname)
>
> os.system(roshCommand)
>
> I do not see the servername variable being assigned a value in your
> script so the above will fail. I assume it will be something like
>
> logname = '/var/log/somelogfile'
>
> You might want to look at the subprocess module as this gives you a
> lot more control but is also more complicated.
>
> Greets
> Sander
>
> [1]
> http://docs.python.org/library/stdtypes.html#string-formatting-operations
>

Yes, it did fail...rosh needs the full path or the -n identifier to remote
shell into the VM's, and you're right that it didn't find it.

As for the $LOGNAME variable, that piece works for identifying the username
which is what I needed for that...it doesn't actually point to a log file or
the like. If I misunderstood what you meant, my apologies.

Thanks very much though, this is also a lot of help and definitely points me
in the right direction. I'll certainly check out string formating and see
where I can go from there. I did look at the subprocess module but I'm still
not sure I understand it...seems to have a lot of options and is flexible,
but I still need to examine how it's used.

Thanks!

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


Re: [Tutor] Interactive programming.

2009-01-07 Thread Edwin Boyette
Don't try to make interactive programming something its not. It's handy if you 
have something short to try out, want to evaluate a function at some value 
etc.  Don't rage at the hammer for not being an allen wrench  get an allen 
wrench.

--- On Wed, 1/7/09, Kent Johnson  wrote:


From: Kent Johnson 
Subject: Re: [Tutor] Interactive programming.
To: "WM." 
Cc: "Sander Sweers" , tutor@python.org
Date: Wednesday, January 7, 2009, 5:38 PM


-Inline Attachment Follows-


On Wed, Jan 7, 2009 at 5:36 PM, WM.  wrote:
> IDLE 2.6
 i = 1
 if i > 1:
>        print 'x'
> else:
>        print 'y'
>
>
> y

> Last post on this topic, I guess.
> I think that the script looks pretty lame, though.

It was always lame :-) but you got it to work...

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


[Tutor] Can subprocess run functions ?

2009-01-07 Thread Damon Timm
Hi everyone - I was playing with subprocess (with some success, I
might add) to implement threading in my script (audio conversion).  My
goal is to be able to spawn off threads to make use of my
multiprocessor system (and speed up encoding).  With your help, I was
successful.

Anyhow, subprocess is working -- but I wonder if there is a way I can
send the entire *function* into its own subprocess ?  Because, in my
case, I want my function to: [a] convert files, [b] tag files, [c] do
some other stuff to files. Steps [b] and [c] require step [a] to be
complete ... but the minute I spawn off step [a] it acts like it is
already done (even though it is still working) ... I was hoping they
could all run in a single thread, one after another ...

I tried just giving subprocess.Popen the function name (rather than
the external program) but that didn't work; and I read through the
docs over at python.org ... but I can't find my answer.

With the code I have, I am not sure how to both wait for my subprocess
to finish (in the function) and allow the multithreading bit to work
together ... I have experimented myself but didn't really get
anywhere.  I commented in where I want to "do other stuff" before it
finishes ... wonder if you can take a look and show me where I may try
to head next?

Thanks!
--
import time
import subprocess

totProcs = 2 #number of processes to spawn before waiting
flacFiles = [["test.flac","test.mp3"],["test2.flac","test2.mp3"],\
["test3.flac","test3.mp3"],["test4.flac","test4.mp3"],\
["test5.flac","test5.mp3"],["test6.flac","test6.mp3"]]
procs = []

def flac_to_mp3(flacfile,mp3file):
print "beginning to process " + flacfile
p = subprocess.Popen(["flac","--decode","--stdout","--silent",flacfile],
stdout=subprocess.PIPE)
p1 = subprocess.Popen(["lame","--silent","-",mp3file], stdin=p.stdout)

# I want to do more stuff before this function ends, but need to
wait for p1 to finish first;
# and, at the same time, I need to "return" p1 so the while loop
(below) works [I think]

return p1

while flacFiles or procs:
procs = [p for p in procs if p.poll() is None]
while flacFiles and len(procs) < totProcs:
file = flacFiles.pop(0)
procs.append(flac_to_mp3(file[0],file[1]))
time.sleep(1)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] linked list with cycle structure

2009-01-07 Thread Robert Kern

Terry Reedy wrote:

David Hláčik wrote:

But what if i have another condition , and that is *i can use only
helping memory with constant size* ? This means i am not able to
create any set and adding elements there. I need to have a constant
size variables . This is complication a complication for me.


Interesting problem.  If it is homework, there must be an answer.


It's also an interview question I've seen reasonably often, particularly with 
that last complication.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: [Tutor] Can subprocess run functions ?

2009-01-07 Thread wesley chun
> Anyhow, subprocess is working -- but I wonder if there is a way I can
> send the entire *function* into its own subprocess ?


this has been a highly-desired feature for quite awhile.

starting in 2.6, you can use the new multiprocessing module
(originally called pyprocessing):
http://docs.python.org/library/multiprocessing.html

there is a backport to 2.4 and 2.5 here:
http://pypi.python.org/pypi/multiprocessing/2.6.0.2

there are similar packages called pypar and pprocess:
http://datamining.anu.edu.au/~ole/pypar/
http://www.boddie.org.uk/python/pprocess.html

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can subprocess run functions ?

2009-01-07 Thread Damon Timm
On Wed, Jan 7, 2009 at 7:36 PM, wesley chun  wrote:
> this has been a highly-desired feature for quite awhile.
>
> starting in 2.6, you can use the new multiprocessing module
> (originally called pyprocessing):
> http://docs.python.org/library/multiprocessing.html
>
> there is a backport to 2.4 and 2.5 here:
> http://pypi.python.org/pypi/multiprocessing/2.6.0.2
>
> there are similar packages called pypar and pprocess:
> http://datamining.anu.edu.au/~ole/pypar/
> http://www.boddie.org.uk/python/pprocess.html
>
> hope this helps!

Thanks Wesley - it does!

As is often the case, the minute I ask a question like this I have
some anxiety that the answer must be right under my nose and I rush
about the tubes of the internet searching for a clue ... I also found:

http://chrisarndt.de/projects/threadpool/threadpool.py.html

Since I have been doing a bit of reading since I started, I was able
to download this "threadpool", import it, and it actually get it to
work.  Here is what I messily put together (using my first referenced
script, as an example) ... I think I may stick with this for a while,
since it seems well-thought out and, as far as I can tell, works!

(No shame in "borrowing" ... though not as cool as making it up myself.)

import subprocess
import threadpool
import os

totProcs = 2 #number of processes to spawn before waiting
flacFiles = 
["test.flac","test2.flac","test3.flac","test4.flac","test5.flac","test6.flac"]

def flac_to_mp3(flacfile):
print "Processing: " + flacfile
mp3file = flacfile.rsplit('.', 1)[0] + '.mp3'

p = subprocess.Popen(["flac","--decode","--stdout","--silent",flacfile],
stdout=subprocess.PIPE)
p1 = subprocess.Popen(["lame","--silent","-",mp3file], stdin=p.stdout)
p1.communicate()

#Test file size so we know it is actually waiting until it has been created.
size = os.path.getsize(mp3file)
return str("File: " + mp3file + "* Size: " + str(size))

def print_result(request, result):
print "* Result from request #%s: %r" % (request.requestID, result)

pool = threadpool.ThreadPool(totProcs)
convert = threadpool.makeRequests(flac_to_mp3,flacFiles,print_result)
[pool.putRequest(req) for req in convert]
pool.wait()

print "All Done!"



> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> "Python Fundamentals", Prentice Hall, (c)2009
>http://corepython.com
>
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Interactive programming.

2009-01-07 Thread Alan Gauld
"Edwin Boyette"  wrote 

Don't try to make interactive programming something its not. 
It's handy if you have something short to try out, want to 
evaluate a function at some value etc. 
Don't rage at the hammer for not being an allen wrench 


But in this case it is a valid complaint. Almost every other IDE 
with an interactive prompt does pro[per indentation, IDLE is the 
exception. Yet IDLE is theone most beginners use first!


I think this is a really serious bug in Idle and is one of the main 
reasons I always recommend beginners use Pythonwin or 
PyCrust or some other shell window. The Idle editor windows 
are fine but the shell window sucks!



--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] Opsware Global Shell Scripting

2009-01-07 Thread Alan Gauld


"Kayvan Sarikhani"  wrote

For the record, $LOGNAME works fine as a variable...it's a standard 
env

variable in Linux to display the current user's username.


In that case os.getenv('LOGNAME') should retrieve it for you

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk 



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


[Tutor] simple array access

2009-01-07 Thread Artie Ziff
Hello,

I used python list comprehension to create a grid (list of lists) of
Objects (instances of MyItem class). Can anyone make recommendations to
achieve a simple access to the elements. My attempt at array access
(like this: array[1,2] ) does not work. What am I overlooking? Thanks in
advance! :)

If anyone has time to show a better way to achieve same, then I am
interested to learn! :)

###

from pprint import *

class MyItem:
  def __init__(self, value):
self.data=value
  def __repr__(self):
return 'MyItem(%s)' % (self.data)

class Grid:
  def __init__(self, x, y, value):
self.data = [[MyItem(float(value))
  for i in range(x)] for j in range(y)]


if __name__ == "__main__":
  grid = Grid(2, 3, 0.42)
  pprint(grid)
  pprint(grid.data)
  # next line fails to access array element
  pprint (grid.data[1,2])


# EOF #

OUTPUT:

<__main__.Grid instance at 0x7beb8>
[[MyItem(0.42), MyItem(0.42)],
 [MyItem(0.42), MyItem(0.42)],
 [MyItem(0.42), MyItem(0.42)]]
Traceback (most recent call last):
  File "multidim04.py", line 20, in 
pprint (grid.data[1,2])
TypeError: list indices must be integers


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


Re: [Tutor] simple array access

2009-01-07 Thread jadrifter
On Wed, 2009-01-07 at 18:12 -0800, Artie Ziff wrote:
> Hello,
> 
> I used python list comprehension to create a grid (list of lists) of
> Objects (instances of MyItem class). Can anyone make recommendations to
> achieve a simple access to the elements. My attempt at array access
> (like this: array[1,2] ) does not work. What am I overlooking? Thanks in
> advance! :)
> 
Hello,

You might want to take a look at NumPy
http://numpy.scipy.org/

John Purser

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


Re: [Tutor] simple array access

2009-01-07 Thread wesley chun
> My attempt at array access (like this: array[1,2] ) does not work. What am
> I overlooking? Thanks in advance! :)
> :
>  pprint (grid.data[1,2])


is that really your name?!? you're famous! ;-) welcome to python!

i'd try...

pprint(grid.data[0][1])

... as many languages start counting at 0 instead of 1, and you needed
a quick syntax fix too.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

"Python Web Development with Django", Addison Wesley, (c) 2009
http://withdjango.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] variable number of inputs

2009-01-07 Thread Mr Gerard Kelly
How can you make a function accept a variable number of inputs without
any particular limit?

Like when you define a function you usually go:

def func(a,b,c,d,e)

and if you want to give a default value for e you can use e=0 for example.

But what if you want to be able to call the function and put in as many
arguments as you want. Say it's a really simple function just adding
them all together or something (I know sum does that, but I'm just
thinking of a simple example).
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] variable number of inputs

2009-01-07 Thread bob gailer

Mr Gerard Kelly wrote:

How can you make a function accept a variable number of inputs without
any particular limit?

Like when you define a function you usually go:

def func(a,b,c,d,e)

and if you want to give a default value for e you can use e=0 for example.

But what if you want to be able to call the function and put in as many
arguments as you want. Say it's a really simple function just adding
them all together or something (I know sum does that, but I'm just
thinking of a simple example).
  


def func(*a) # a will be a tuple of whatever arguments are passed in the call.


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


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


Re: [Tutor] variable number of inputs

2009-01-07 Thread wesley chun
On Wed, Jan 7, 2009 at 7:01 PM, bob gailer  wrote:
> Mr Gerard Kelly wrote:
>>
>> How can you make a function accept a variable number of inputs without
>> any particular limit?
>>:
>> But what if you want to be able to call the function and put in as many
>> arguments as you want.
>
> def func(*a) # a will be a tuple of whatever arguments are passed in the call.


if any of the variables are keyworded, you'll need a vararg dictionary
too, **kwargs.

a very popular idiom you'll see in function signatures looks like this:

def func(*args, **kwargs)

this is the most flexible Python function definition because this
function can accept *any* number and type of arguments you can give
it, i.e.,

func()
func(a)
func(a, b)
func(a, b, c, d, e)
func(a, b, c, d, e=0)
func(a, b, 999, xxx=['foo', 'bar'], yyy=42)
:

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] simple array access

2009-01-07 Thread Edwin Boyette


--- On Wed, 1/7/09, Artie Ziff  wrote:


From: Artie Ziff 
Subject: [Tutor] simple array access
To: Tutor@python.org
Date: Wednesday, January 7, 2009, 9:12 PM


Hello,

I used python list comprehension to create a grid (list of lists) of
Objects (instances of MyItem class). Can anyone make recommendations to
achieve a simple access to the elements. My attempt at array access
(like this: array[1,2] ) does not work. What am I overlooking? Thanks in
advance! :)

If anyone has time to show a better way to achieve same, then I am
interested to learn! :)

###

from pprint import *

class MyItem:
  def __init__(self, value):
    self.data=value
  def __repr__(self):
    return 'MyItem(%s)' % (self.data)

class Grid:
  def __init__(self, x, y, value):
    self.data = [[MyItem(float(value))
                  for i in range(x)] for j in range(y)]


if __name__ == "__main__":
  grid = Grid(2, 3, 0.42)
  pprint(grid)
  pprint(grid.data)
  # next line fails to access array element
  pprint (grid.data[1,2])


# EOF #

OUTPUT:

<__main__.Grid instance at 0x7beb8>
[[MyItem(0.42), MyItem(0.42)],
[MyItem(0.42), MyItem(0.42)],
[MyItem(0.42), MyItem(0.42)]]
Traceback (most recent call last):
  File "multidim04.py", line 20, in 
    pprint (grid.data[1,2])
TypeError: list indices must be integers


Cheers,
Art

 
## There are probably much easier, elegant ways to populate a list
## This is fast enough to generate to express the idea
## There is a row/col method also but I haven't used it
## List comprehensions may be prettier/faster (check Kent's page I believe he 
has 
## write up on them

some_sequence = []
i = 0
k = []
for i in range (10):
 k.append(i)
for i in range (10):
    some_sequence.append(k)

for stuff in some_sequence:
    print stuff
    
print "Some sequence has 10 outer indexes beginning at [0] and ending at [9]"
print "The first element of index [0] is the value 0, and can be accessed at 
some_sequence[0][0] :" + str(some_sequence[0][0])
print "The last element of index [9] ## the 10th line or index is the value 9, 
and can be accessed at some_sequence[9][9] :" + str(some_sequence[9][9])
  
 
    

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