[Tutor] Repply

2008-12-26 Thread prasad rao
Thanks for help.

>
http://svn.python.org/view/python/trunk/Objects/listobject.c?rev=67498&view=markup

 Kent ! This is grek and latin to me.From the presence of header files it
looks C++.But headerfiles are not between '<'  and  '>' .

>But why are you trying to sort in this fashion?

Alan Gauld! Basic exercises in C++ are to find min , malimum and sorting.So
I
am just trying it in python.

Thank you Mark.

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


Re: [Tutor] commands versus subprocess, I'm confused

2008-12-26 Thread Reed O'Brien
On Dec 26, 2008, at 8:57, "Emad Nawfal (عماد نوفل)" mail.com> wrote:





2008/12/26 Kent Johnson 
On Fri, Dec 26, 2008 at 8:09 AM, Emad Nawfal (عماد نوفل)
 wrote:
> suppose I have an external program that prints "testing the  
subprocess

> module"
> I know I can run it through the commands module like this:
>
 a = commands.getoutput("python3.0 hello.py")
 a
> 'testing the subprocess module'


> I cannot figure out how to do the same thing in the subprocess  
module. Can

> somebody please explain how to get the same behaviour from, say,
> subprocess.call

Sometthing like this, I think:

proc = subprocess.Popen('python3.0 hello.py',
  shell=True,
  stdout=subprocess.PIPE,
  )
stdout_value = proc.communicate()[0]

(Courtesy of http://blog.doughellmann.com/2007/07/pymotw-subprocess.html 
)


Kent
Thank you Kent.
It works, but isn't the commands module much simpler?  I don't know  
why it's no more available in Python3.0


Subprocess was designed to replace all the os.popen classes since 2.4.

Commands is a wrapper for os.popen.

Aside from that commands AFAIK is unix only. Therefore it is less  
portable.


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


Re: [Tutor] commands versus subprocess, I'm confused

2008-12-26 Thread Alan Gauld

"Emad Nawfal (عماد نوفل)"  wrote


proc = subprocess.Popen('python3.0 hello.py',
  shell=True,
  stdout=subprocess.PIPE,
  )
stdout_value = proc.communicate()[0]



Thank you Kent.
It works, but isn't the commands module much simpler?  I don't know 
why it's

no more available in Python3.0


commands was one of many different ways to access command ouitput.
Subprocess was designed to replace all of them and provide a single
consistent mechanism.

So, yes we lost some simplicity but gained consistency.
You no longer need to figure out whether its best to use
os,system, os.popen(and which version of popen), os.spawn, commands 
etc


You just use subprocess...
HTH,
Alan G 



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


Re: [Tutor] beginsWith multiple prefixes

2008-12-26 Thread ALAN GAULD


> I simply prefer the built-in one. I had no idea it could take a tuple.

Me neither, that was a surprise goody in 2.5 that I hadn't seen before.

> What is amazing is that I learn  more from this list than I do from any other 
> source.

Me too, and I've been subscribed for over 10 years now! :-)

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


Re: [Tutor] commands versus subprocess, I'm confused

2008-12-26 Thread Emad Nawfal (عماد نوفل)
2008/12/26 Kent Johnson 

> On Fri, Dec 26, 2008 at 8:09 AM, Emad Nawfal (عماد نوفل)
>  wrote:
> > suppose I have an external program that prints "testing the subprocess
> > module"
> > I know I can run it through the commands module like this:
> >
>  a = commands.getoutput("python3.0 hello.py")
>  a
> > 'testing the subprocess module'
>
>
> > I cannot figure out how to do the same thing in the subprocess module.
> Can
> > somebody please explain how to get the same behaviour from, say,
> > subprocess.call
>
> Sometthing like this, I think:
>
> proc = subprocess.Popen('python3.0 hello.py',
>   shell=True,
>   stdout=subprocess.PIPE,
>   )
> stdout_value = proc.communicate()[0]
>
> (Courtesy of http://blog.doughellmann.com/2007/07/pymotw-subprocess.html)
>
> Kent
>
Thank you Kent.
It works, but isn't the commands module much simpler?  I don't know why it's
no more available in Python3.0


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

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


Re: [Tutor] commands versus subprocess, I'm confused

2008-12-26 Thread Kent Johnson
On Fri, Dec 26, 2008 at 8:09 AM, Emad Nawfal (عماد نوفل)
 wrote:
> suppose I have an external program that prints "testing the subprocess
> module"
> I know I can run it through the commands module like this:
>
 a = commands.getoutput("python3.0 hello.py")
 a
> 'testing the subprocess module'


> I cannot figure out how to do the same thing in the subprocess module. Can
> somebody please explain how to get the same behaviour from, say,
> subprocess.call

Sometthing like this, I think:

proc = subprocess.Popen('python3.0 hello.py',
   shell=True,
   stdout=subprocess.PIPE,
   )
stdout_value = proc.communicate()[0]

(Courtesy of http://blog.doughellmann.com/2007/07/pymotw-subprocess.html)

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


Re: [Tutor] to sort

2008-12-26 Thread Kent Johnson
On Fri, Dec 26, 2008 at 1:21 AM, prasad rao  wrote:

> By the way how can I view the builtin code for sort method?

Look at the source - Objects/listobject.c - starting at the comment
"Lots of code for an adaptive, stable, natural mergesort."
http://svn.python.org/view/python/trunk/Objects/listobject.c?rev=67498&view=markup

The source also includes an interesting and detailed description of
the sort algorithm:
http://svn.python.org/view/python/trunk/Objects/listsort.txt?rev=51013&view=auto

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


Re: [Tutor] beginsWith multiple prefixes

2008-12-26 Thread Emad Nawfal (عماد نوفل)
On Fri, Dec 26, 2008 at 6:47 AM, Alan Gauld wrote:

>
> "Kent Johnson"  wrote
>
>> for d in os.listdir():
>>>  if MyString(d).upper().beginswith():
>>>
>>
>> But that won't work, the result of calling upper() will be a normal
>> str, not a MyString.
>>
>
> Ah yes. Immutability of strings strikes again. upper() returns a new
> string, I forgot about that. pity.
>
> You can do
>
> if MyString(d.upper()).beginswith(...)
>
> But that loses a lot in elegance and is hardly better than using a
> fiunction.
>
> Alan G
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Thank you Alan and everybody. I simply prefer the built-in one. I had no
idea it could take a tuple.
What is amazing is that I learn  more from this list than I do from any
other source.

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com

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


[Tutor] commands versus subprocess, I'm confused

2008-12-26 Thread Emad Nawfal (عماد نوفل)
Hello Tutors, and Happy New Year and Holidays,
suppose I have an external program that prints "testing the subprocess
module"
I know I can run it through the commands module like this:

>>> a = commands.getoutput("python3.0 hello.py")
>>> a
'testing the subprocess module'
>>> len(a)
29
>>> b = a.split()
>>> b
['testing', 'the', 'subprocess', 'module']
>>> for word in b:
... if word[-1] == 'e': print word
...
the
module
>>>

I cannot figure out how to do the same thing in the subprocess module. Can
somebody please explain how to get the same behaviour from, say,
subprocess.call

-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington


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


Re: [Tutor] to sort

2008-12-26 Thread Alan Gauld


"prasad rao"  wrote 


a=[21,56,35,47,94,12]
 b=[]

  for x in a:
b.append (min(a))
a.remove (min(a))



It is not Compleating .Doing only 3 rounds.Why?


Think about what is happening.
for x in a

x takes the next value of x after each iteration.
Each iteration reduces the size of a so after 3 iterations 
x will be the 3rd value and there are only 3 values left. 
So x is at the end of the list and the loop stops.


So as Mark said, don't change the list while iterating over 
it - its a bit like cutting off the branch of the tree that you 
are sitting on!


But why are you trying to sort in this fashion? Its 
extremely inefficient compared to using the built in 
methods.


--
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] beginsWith multiple prefixes

2008-12-26 Thread Alan Gauld


"Kent Johnson"  wrote 


for d in os.listdir():
  if MyString(d).upper().beginswith():


But that won't work, the result of calling upper() will be a normal
str, not a MyString.


Ah yes. Immutability of strings strikes again. upper() returns 
a new string, I forgot about that. pity.


You can do

if MyString(d.upper()).beginswith(...)

But that loses a lot in elegance and is hardly better than 
using a fiunction.


Alan G

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