[Tutor] accessing code for built in min()

2014-07-31 Thread ugajin

How do I look at the code for a python built in function e.g. min()?

Thanks

I am running 2.7.8 on OSX
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] accessing code for built in min()

2014-07-31 Thread Raúl Cumplido
Are you asking for the source code? For the CPython implementation, PyPy,
IronPython, Jhyton?

For the CPython implementation you can go to the repository:
http://hg.python.org/cpython/branches


Builtin implementations in C (for 2.7 version) are in the file:
http://hg.python.org/cpython/file/818989a48e96/Python/bltinmodule.c

If you want to take a look on the min builtin:
http://hg.python.org/cpython/file/818989a48e96/Python/bltinmodule.c#l1435

Thanks,
Raúl


On Thu, Jul 31, 2014 at 9:51 AM,  wrote:

> How do I look at the code for a python built in function e.g. min()?
>
> Thanks
>
> I am running 2.7.8 on OSX
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] accessing code for built in min()

2014-07-31 Thread ugajin
Thanks Raúl,

T'was the latter two. 

-u

 

 

 

-Original Message-
From: Raúl Cumplido 
To: uga...@talktalk.net
CC: tutor 
Sent: Thu, 31 Jul 2014 10:36
Subject: Re: [Tutor] accessing code for built in min()


Are you asking for the source code? For the CPython implementation, PyPy, 
IronPython, Jhyton?


For the CPython implementation you can go to the repository:
http://hg.python.org/cpython/branches




Builtin implementations in C (for 2.7 version) are in the file:

http://hg.python.org/cpython/file/818989a48e96/Python/bltinmodule.c



If you want to take a look on the min builtin:
http://hg.python.org/cpython/file/818989a48e96/Python/bltinmodule.c#l1435



Thanks,
Raúl




On Thu, Jul 31, 2014 at 9:51 AM,   wrote:

How do I look at the code for a python built in function e.g. min()?

Thanks

I am running 2.7.8 on OSX
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



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


[Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
I am on OSX, which needs to escape spaces in filenames with a backslash.
There are multiple files within one directory that all have the same
structure, one or more characters with zero or more spaces in the
filename, like this:
3 Song Title XYZ.flac.
I want to use Python to call ffmpeg to convert each file to an .mp3.
So far this is what I was trying to use:
import os, subprocess
track = 1
for filename in os.listdir('myDir'):
subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
track += 1


I am about to use re.sub to just replace all the '/s' with '\\/s', but
is there a simpler/more pythonic way to do this?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread Emile van Sebille

On 7/31/2014 12:53 PM, C Smith wrote:

I am on OSX, which needs to escape spaces in filenames with a backslash.
There are multiple files within one directory that all have the same
structure, one or more characters with zero or more spaces in the
filename, like this:
3 Song Title XYZ.flac.
I want to use Python to call ffmpeg to convert each file to an .mp3.
So far this is what I was trying to use:
import os, subprocess
track = 1
for filename in os.listdir('myDir'):
 subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
 track += 1


You might try using '"%s"' % filename so that the name is within quotes 
for the shell environment.


Emile


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


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
>Change:


>subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])

>to:

>subprocess.call(['ffmpeg', '-i', '"%s"' % filename, str(track)+'.mp3'])

I still get the same errors, the filenames are passed to the shell
without escaping the spaces.

>Why not using ffmpeg without jumping into Python. It's well documented, check 
>Google.

I guess you mean that the ability to change multiple files with ffmpeg
is possible. I hadn't considered that but I would rather do it with
Python, just for the practice.

On Thu, Jul 31, 2014 at 4:36 PM, Emile  wrote:
> On 7/31/2014 1:19 PM, C Smith wrote:
>>
>> I get
>> TypeError: unsupported operand type(s) for %: 'int' and 'str
>> I am not understanding the use of the list inside the subprocess.call().
>> I tried all of the following
>> subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3']) % filename
>> --gives type error stated above
>> subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3'] % filename)
>> --same
>> subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3' % filename])
>> -- TypeError: not all arguments converted during string formatting
>> and tried all three with the triple quotes, just to be sure.
>>
>> On Thu, Jul 31, 2014 at 4:04 PM, Emile van Sebille  wrote:
>>>
>>> You might try using '"%s"' % filename so that the name is within quotes
>>> for the shell environment.
>
>
> Change:
>
>
> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>
> to:
>
> subprocess.call(['ffmpeg', '-i', '"%s"' % filename, str(track)+'.mp3'])
>
> Emile
>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
Even when I am using:
re.sub('/s', '\\/s', filename)
I am still getting the same output, even trying to assign the above to
a new variable doesn't work (as I wasn't sure if re.sub returned a new
value or changed filename in place, I tried both with)
Does the Python interpreter strip off backslashes or something with strings?

On Thu, Jul 31, 2014 at 5:53 PM, C Smith  wrote:
>>Change:
>
>
>>subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>
>>to:
>
>>subprocess.call(['ffmpeg', '-i', '"%s"' % filename, str(track)+'.mp3'])
>
> I still get the same errors, the filenames are passed to the shell
> without escaping the spaces.
>
>>Why not using ffmpeg without jumping into Python. It's well documented, check 
>>Google.
>
> I guess you mean that the ability to change multiple files with ffmpeg
> is possible. I hadn't considered that but I would rather do it with
> Python, just for the practice.
>
> On Thu, Jul 31, 2014 at 4:36 PM, Emile  wrote:
>> On 7/31/2014 1:19 PM, C Smith wrote:
>>>
>>> I get
>>> TypeError: unsupported operand type(s) for %: 'int' and 'str
>>> I am not understanding the use of the list inside the subprocess.call().
>>> I tried all of the following
>>> subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3']) % filename
>>> --gives type error stated above
>>> subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3'] % filename)
>>> --same
>>> subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3' % filename])
>>> -- TypeError: not all arguments converted during string formatting
>>> and tried all three with the triple quotes, just to be sure.
>>>
>>> On Thu, Jul 31, 2014 at 4:04 PM, Emile van Sebille  wrote:

 You might try using '"%s"' % filename so that the name is within quotes
 for the shell environment.
>>
>>
>> Change:
>>
>>
>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>
>> to:
>>
>> subprocess.call(['ffmpeg', '-i', '"%s"' % filename, str(track)+'.mp3'])
>>
>> Emile
>>
>>
>>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
Okay I messed up with slash instead of backslash, so the re.sub()
works, but I am still curious about the previous question.

On Thu, Jul 31, 2014 at 6:14 PM, C Smith  wrote:
> Even when I am using:
> re.sub('/s', '\\/s', filename)
> I am still getting the same output, even trying to assign the above to
> a new variable doesn't work (as I wasn't sure if re.sub returned a new
> value or changed filename in place, I tried both with)
> Does the Python interpreter strip off backslashes or something with strings?
>
> On Thu, Jul 31, 2014 at 5:53 PM, C Smith  wrote:
>>>Change:
>>
>>
>>>subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>
>>>to:
>>
>>>subprocess.call(['ffmpeg', '-i', '"%s"' % filename, str(track)+'.mp3'])
>>
>> I still get the same errors, the filenames are passed to the shell
>> without escaping the spaces.
>>
>>>Why not using ffmpeg without jumping into Python. It's well documented, 
>>>check Google.
>>
>> I guess you mean that the ability to change multiple files with ffmpeg
>> is possible. I hadn't considered that but I would rather do it with
>> Python, just for the practice.
>>
>> On Thu, Jul 31, 2014 at 4:36 PM, Emile  wrote:
>>> On 7/31/2014 1:19 PM, C Smith wrote:

 I get
 TypeError: unsupported operand type(s) for %: 'int' and 'str
 I am not understanding the use of the list inside the subprocess.call().
 I tried all of the following
 subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3']) % filename
 --gives type error stated above
 subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3'] % filename)
 --same
 subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3' % filename])
 -- TypeError: not all arguments converted during string formatting
 and tried all three with the triple quotes, just to be sure.

 On Thu, Jul 31, 2014 at 4:04 PM, Emile van Sebille  wrote:
>
> You might try using '"%s"' % filename so that the name is within quotes
> for the shell environment.
>>>
>>>
>>> Change:
>>>
>>>
>>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>>
>>> to:
>>>
>>> subprocess.call(['ffmpeg', '-i', '"%s"' % filename, str(track)+'.mp3'])
>>>
>>> Emile
>>>
>>>
>>>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
Actually, I can get re.sub() to print the filenames where they look
like they would be in the correct format for the shell, like this:
10\ track \number \ten.flac
but the shell still says that no such file exists, and I am sure I am
operating on them in the right place because I can modify them. So, I
am still wondering about that too.

On Thu, Jul 31, 2014 at 6:20 PM, C Smith  wrote:
> Okay I messed up with slash instead of backslash, so the re.sub()
> works, but I am still curious about the previous question.
>
> On Thu, Jul 31, 2014 at 6:14 PM, C Smith  wrote:
>> Even when I am using:
>> re.sub('/s', '\\/s', filename)
>> I am still getting the same output, even trying to assign the above to
>> a new variable doesn't work (as I wasn't sure if re.sub returned a new
>> value or changed filename in place, I tried both with)
>> Does the Python interpreter strip off backslashes or something with strings?
>>
>> On Thu, Jul 31, 2014 at 5:53 PM, C Smith  
>> wrote:
Change:
>>>
>>>
subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>>
to:
>>>
subprocess.call(['ffmpeg', '-i', '"%s"' % filename, str(track)+'.mp3'])
>>>
>>> I still get the same errors, the filenames are passed to the shell
>>> without escaping the spaces.
>>>
Why not using ffmpeg without jumping into Python. It's well documented, 
check Google.
>>>
>>> I guess you mean that the ability to change multiple files with ffmpeg
>>> is possible. I hadn't considered that but I would rather do it with
>>> Python, just for the practice.
>>>
>>> On Thu, Jul 31, 2014 at 4:36 PM, Emile  wrote:
 On 7/31/2014 1:19 PM, C Smith wrote:
>
> I get
> TypeError: unsupported operand type(s) for %: 'int' and 'str
> I am not understanding the use of the list inside the subprocess.call().
> I tried all of the following
> subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3']) % filename
> --gives type error stated above
> subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3'] % filename)
> --same
> subprocess.call(['ffmpeg', '-i', '%s', str(track)+'.mp3' % filename])
> -- TypeError: not all arguments converted during string formatting
> and tried all three with the triple quotes, just to be sure.
>
> On Thu, Jul 31, 2014 at 4:04 PM, Emile van Sebille  wrote:
>>
>> You might try using '"%s"' % filename so that the name is within quotes
>> for the shell environment.


 Change:


 subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])

 to:

 subprocess.call(['ffmpeg', '-i', '"%s"' % filename, str(track)+'.mp3'])

 Emile



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


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread Steven D'Aprano
You may have already have solved your problem, unfortunately my 
emails are coming in slowly and out of order, but I have a suggestion:

On Thu, Jul 31, 2014 at 03:53:48PM -0400, C Smith wrote:
> I am on OSX, which needs to escape spaces in filenames with a backslash.

Same as any other Unix, or Linux, or, indeed, Windows.

> There are multiple files within one directory that all have the same
> structure, one or more characters with zero or more spaces in the
> filename, like this:
> 3 Song Title XYZ.flac.
> I want to use Python to call ffmpeg to convert each file to an .mp3.
> So far this is what I was trying to use:
> import os, subprocess
> track = 1
> for filename in os.listdir('myDir'):
> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
> track += 1

I believe that your problem is *not* the spaces, but that you're passing 
just the filename and not the directory. subprocess will escape the 
spaces for you. Also, let Python count the track number for you. Try 
this:


directory = '/path/to/the/directory'
for track, filename in enumerate(os.listdir(directory), 1):
pathname = os.path.join(directory, filename)
subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])


I expect something like that will work. You should be able to pass 
either an absolute path (beginning with /) or a relative path starting 
from the current working directory.

If this doesn't work, please show the full error that you receive. If it 
is a Python traceback, copy and paste the whole thing, if it's an ffmpeg 
error, give as much information as you can.



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


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread Steven D'Aprano
Oops, a silly bug:

On Fri, Aug 01, 2014 at 08:35:34AM +1000, Steven D'Aprano wrote:

> directory = '/path/to/the/directory'
> for track, filename in enumerate(os.listdir(directory), 1):
> pathname = os.path.join(directory, filename)
> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])

That should be pathname, of course:

subprocess.call(['ffmpeg', '-i', pathname, str(track)+'.mp3'])

Also, be aware that the order of files coming from listdir is not 
guaranteed to be sorted in any specific fashion, so if you're renaming 
files like "3 blah blah blah.flac" to "7.mp3" you may end up a little 
confused :-)


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


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
>for track, filename in enumerate(os.listdir(directory), 1):
It seems kinda counter-intuitive to have track then filename as
variables, but enumerate looks like it gets passed the filename then
track number. Is that correct and just the way enumerate works, a
typo, or am I missing something else here?

It is an ffmpeg error I am getting.
ffmpeg just gives its usual build information and the error is (for
each song title in the directory):
songTitleIsHere.flac: no such file or directory

So it looks like it is close to working because it finds the correct
file names, but doesn't recognize it for some reason.
Here is how I put in your code
import os, subprocess
directory = '/absolute/path/goes/here'
for track, filename in enumerate(os.listdir(directory), 1):
pathname = os.path.join(directory, filename)
subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])

So it goes to the right place, because every song title is listed out,
ffmpeg or the shell just don't recognize them correctly.

On Thu, Jul 31, 2014 at 6:35 PM, Steven D'Aprano  wrote:
> You may have already have solved your problem, unfortunately my
> emails are coming in slowly and out of order, but I have a suggestion:
>
> On Thu, Jul 31, 2014 at 03:53:48PM -0400, C Smith wrote:
>> I am on OSX, which needs to escape spaces in filenames with a backslash.
>
> Same as any other Unix, or Linux, or, indeed, Windows.
>
>> There are multiple files within one directory that all have the same
>> structure, one or more characters with zero or more spaces in the
>> filename, like this:
>> 3 Song Title XYZ.flac.
>> I want to use Python to call ffmpeg to convert each file to an .mp3.
>> So far this is what I was trying to use:
>> import os, subprocess
>> track = 1
>> for filename in os.listdir('myDir'):
>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>> track += 1
>
> I believe that your problem is *not* the spaces, but that you're passing
> just the filename and not the directory. subprocess will escape the
> spaces for you. Also, let Python count the track number for you. Try
> this:
>
>
> directory = '/path/to/the/directory'
> for track, filename in enumerate(os.listdir(directory), 1):
> pathname = os.path.join(directory, filename)
> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>
>
> I expect something like that will work. You should be able to pass
> either an absolute path (beginning with /) or a relative path starting
> from the current working directory.
>
> If this doesn't work, please show the full error that you receive. If it
> is a Python traceback, copy and paste the whole thing, if it's an ffmpeg
> error, give as much information as you can.
>
>
>
> --
> Steven
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
woops, I see it pathname != filename

On Thu, Jul 31, 2014 at 6:55 PM, C Smith  wrote:
>>for track, filename in enumerate(os.listdir(directory), 1):
> It seems kinda counter-intuitive to have track then filename as
> variables, but enumerate looks like it gets passed the filename then
> track number. Is that correct and just the way enumerate works, a
> typo, or am I missing something else here?
>
> It is an ffmpeg error I am getting.
> ffmpeg just gives its usual build information and the error is (for
> each song title in the directory):
> songTitleIsHere.flac: no such file or directory
>
> So it looks like it is close to working because it finds the correct
> file names, but doesn't recognize it for some reason.
> Here is how I put in your code
> import os, subprocess
> directory = '/absolute/path/goes/here'
> for track, filename in enumerate(os.listdir(directory), 1):
> pathname = os.path.join(directory, filename)
> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>
> So it goes to the right place, because every song title is listed out,
> ffmpeg or the shell just don't recognize them correctly.
>
> On Thu, Jul 31, 2014 at 6:35 PM, Steven D'Aprano  wrote:
>> You may have already have solved your problem, unfortunately my
>> emails are coming in slowly and out of order, but I have a suggestion:
>>
>> On Thu, Jul 31, 2014 at 03:53:48PM -0400, C Smith wrote:
>>> I am on OSX, which needs to escape spaces in filenames with a backslash.
>>
>> Same as any other Unix, or Linux, or, indeed, Windows.
>>
>>> There are multiple files within one directory that all have the same
>>> structure, one or more characters with zero or more spaces in the
>>> filename, like this:
>>> 3 Song Title XYZ.flac.
>>> I want to use Python to call ffmpeg to convert each file to an .mp3.
>>> So far this is what I was trying to use:
>>> import os, subprocess
>>> track = 1
>>> for filename in os.listdir('myDir'):
>>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>> track += 1
>>
>> I believe that your problem is *not* the spaces, but that you're passing
>> just the filename and not the directory. subprocess will escape the
>> spaces for you. Also, let Python count the track number for you. Try
>> this:
>>
>>
>> directory = '/path/to/the/directory'
>> for track, filename in enumerate(os.listdir(directory), 1):
>> pathname = os.path.join(directory, filename)
>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>
>>
>> I expect something like that will work. You should be able to pass
>> either an absolute path (beginning with /) or a relative path starting
>> from the current working directory.
>>
>> If this doesn't work, please show the full error that you receive. If it
>> is a Python traceback, copy and paste the whole thing, if it's an ffmpeg
>> error, give as much information as you can.
>>
>>
>>
>> --
>> Steven
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
Works now, thanks!

On Thu, Jul 31, 2014 at 6:57 PM, C Smith  wrote:
> woops, I see it pathname != filename
>
> On Thu, Jul 31, 2014 at 6:55 PM, C Smith  wrote:
>>>for track, filename in enumerate(os.listdir(directory), 1):
>> It seems kinda counter-intuitive to have track then filename as
>> variables, but enumerate looks like it gets passed the filename then
>> track number. Is that correct and just the way enumerate works, a
>> typo, or am I missing something else here?
>>
>> It is an ffmpeg error I am getting.
>> ffmpeg just gives its usual build information and the error is (for
>> each song title in the directory):
>> songTitleIsHere.flac: no such file or directory
>>
>> So it looks like it is close to working because it finds the correct
>> file names, but doesn't recognize it for some reason.
>> Here is how I put in your code
>> import os, subprocess
>> directory = '/absolute/path/goes/here'
>> for track, filename in enumerate(os.listdir(directory), 1):
>> pathname = os.path.join(directory, filename)
>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>
>> So it goes to the right place, because every song title is listed out,
>> ffmpeg or the shell just don't recognize them correctly.
>>
>> On Thu, Jul 31, 2014 at 6:35 PM, Steven D'Aprano  wrote:
>>> You may have already have solved your problem, unfortunately my
>>> emails are coming in slowly and out of order, but I have a suggestion:
>>>
>>> On Thu, Jul 31, 2014 at 03:53:48PM -0400, C Smith wrote:
 I am on OSX, which needs to escape spaces in filenames with a backslash.
>>>
>>> Same as any other Unix, or Linux, or, indeed, Windows.
>>>
 There are multiple files within one directory that all have the same
 structure, one or more characters with zero or more spaces in the
 filename, like this:
 3 Song Title XYZ.flac.
 I want to use Python to call ffmpeg to convert each file to an .mp3.
 So far this is what I was trying to use:
 import os, subprocess
 track = 1
 for filename in os.listdir('myDir'):
 subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
 track += 1
>>>
>>> I believe that your problem is *not* the spaces, but that you're passing
>>> just the filename and not the directory. subprocess will escape the
>>> spaces for you. Also, let Python count the track number for you. Try
>>> this:
>>>
>>>
>>> directory = '/path/to/the/directory'
>>> for track, filename in enumerate(os.listdir(directory), 1):
>>> pathname = os.path.join(directory, filename)
>>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>>
>>>
>>> I expect something like that will work. You should be able to pass
>>> either an absolute path (beginning with /) or a relative path starting
>>> from the current working directory.
>>>
>>> If this doesn't work, please show the full error that you receive. If it
>>> is a Python traceback, copy and paste the whole thing, if it's an ffmpeg
>>> error, give as much information as you can.
>>>
>>>
>>>
>>> --
>>> Steven
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> To unsubscribe or change subscription options:
>>> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
Huh, that is quite an annoyance about changing the order though. Any
ideas about that? I will look into it further in the meantime...

On Thu, Jul 31, 2014 at 6:57 PM, C Smith  wrote:
> Works now, thanks!
>
> On Thu, Jul 31, 2014 at 6:57 PM, C Smith  wrote:
>> woops, I see it pathname != filename
>>
>> On Thu, Jul 31, 2014 at 6:55 PM, C Smith  
>> wrote:
for track, filename in enumerate(os.listdir(directory), 1):
>>> It seems kinda counter-intuitive to have track then filename as
>>> variables, but enumerate looks like it gets passed the filename then
>>> track number. Is that correct and just the way enumerate works, a
>>> typo, or am I missing something else here?
>>>
>>> It is an ffmpeg error I am getting.
>>> ffmpeg just gives its usual build information and the error is (for
>>> each song title in the directory):
>>> songTitleIsHere.flac: no such file or directory
>>>
>>> So it looks like it is close to working because it finds the correct
>>> file names, but doesn't recognize it for some reason.
>>> Here is how I put in your code
>>> import os, subprocess
>>> directory = '/absolute/path/goes/here'
>>> for track, filename in enumerate(os.listdir(directory), 1):
>>> pathname = os.path.join(directory, filename)
>>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>>
>>> So it goes to the right place, because every song title is listed out,
>>> ffmpeg or the shell just don't recognize them correctly.
>>>
>>> On Thu, Jul 31, 2014 at 6:35 PM, Steven D'Aprano  
>>> wrote:
 You may have already have solved your problem, unfortunately my
 emails are coming in slowly and out of order, but I have a suggestion:

 On Thu, Jul 31, 2014 at 03:53:48PM -0400, C Smith wrote:
> I am on OSX, which needs to escape spaces in filenames with a backslash.

 Same as any other Unix, or Linux, or, indeed, Windows.

> There are multiple files within one directory that all have the same
> structure, one or more characters with zero or more spaces in the
> filename, like this:
> 3 Song Title XYZ.flac.
> I want to use Python to call ffmpeg to convert each file to an .mp3.
> So far this is what I was trying to use:
> import os, subprocess
> track = 1
> for filename in os.listdir('myDir'):
> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
> track += 1

 I believe that your problem is *not* the spaces, but that you're passing
 just the filename and not the directory. subprocess will escape the
 spaces for you. Also, let Python count the track number for you. Try
 this:


 directory = '/path/to/the/directory'
 for track, filename in enumerate(os.listdir(directory), 1):
 pathname = os.path.join(directory, filename)
 subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])


 I expect something like that will work. You should be able to pass
 either an absolute path (beginning with /) or a relative path starting
 from the current working directory.

 If this doesn't work, please show the full error that you receive. If it
 is a Python traceback, copy and paste the whole thing, if it's an ffmpeg
 error, give as much information as you can.



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


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
thanks, got it
import os, subprocess, re
directory = 'abs/path'
for track, filename in enumerate(os.listdir(directory), 1):
pathname = os.path.join(directory, filename)
subprocess.call(['ffmpeg', '-i', pathname, filename+str(track)+'.mp3'])

On Thu, Jul 31, 2014 at 7:02 PM, C Smith  wrote:
> Huh, that is quite an annoyance about changing the order though. Any
> ideas about that? I will look into it further in the meantime...
>
> On Thu, Jul 31, 2014 at 6:57 PM, C Smith  wrote:
>> Works now, thanks!
>>
>> On Thu, Jul 31, 2014 at 6:57 PM, C Smith  
>> wrote:
>>> woops, I see it pathname != filename
>>>
>>> On Thu, Jul 31, 2014 at 6:55 PM, C Smith  
>>> wrote:
>for track, filename in enumerate(os.listdir(directory), 1):
 It seems kinda counter-intuitive to have track then filename as
 variables, but enumerate looks like it gets passed the filename then
 track number. Is that correct and just the way enumerate works, a
 typo, or am I missing something else here?

 It is an ffmpeg error I am getting.
 ffmpeg just gives its usual build information and the error is (for
 each song title in the directory):
 songTitleIsHere.flac: no such file or directory

 So it looks like it is close to working because it finds the correct
 file names, but doesn't recognize it for some reason.
 Here is how I put in your code
 import os, subprocess
 directory = '/absolute/path/goes/here'
 for track, filename in enumerate(os.listdir(directory), 1):
 pathname = os.path.join(directory, filename)
 subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])

 So it goes to the right place, because every song title is listed out,
 ffmpeg or the shell just don't recognize them correctly.

 On Thu, Jul 31, 2014 at 6:35 PM, Steven D'Aprano  
 wrote:
> You may have already have solved your problem, unfortunately my
> emails are coming in slowly and out of order, but I have a suggestion:
>
> On Thu, Jul 31, 2014 at 03:53:48PM -0400, C Smith wrote:
>> I am on OSX, which needs to escape spaces in filenames with a backslash.
>
> Same as any other Unix, or Linux, or, indeed, Windows.
>
>> There are multiple files within one directory that all have the same
>> structure, one or more characters with zero or more spaces in the
>> filename, like this:
>> 3 Song Title XYZ.flac.
>> I want to use Python to call ffmpeg to convert each file to an .mp3.
>> So far this is what I was trying to use:
>> import os, subprocess
>> track = 1
>> for filename in os.listdir('myDir'):
>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>> track += 1
>
> I believe that your problem is *not* the spaces, but that you're passing
> just the filename and not the directory. subprocess will escape the
> spaces for you. Also, let Python count the track number for you. Try
> this:
>
>
> directory = '/path/to/the/directory'
> for track, filename in enumerate(os.listdir(directory), 1):
> pathname = os.path.join(directory, filename)
> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>
>
> I expect something like that will work. You should be able to pass
> either an absolute path (beginning with /) or a relative path starting
> from the current working directory.
>
> If this doesn't work, please show the full error that you receive. If it
> is a Python traceback, copy and paste the whole thing, if it's an ffmpeg
> error, give as much information as you can.
>
>
>
> --
> Steven
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
or more accurately
import os, subprocess, re
directory = '/abs/path'
for track, filename in enumerate(os.listdir(directory), 1):
pathname = os.path.join(directory, filename)
subprocess.call(['ffmpeg', '-i', pathname, filename[:-5]+'.mp3'])

On Thu, Jul 31, 2014 at 7:13 PM, C Smith  wrote:
> thanks, got it
> import os, subprocess, re
> directory = 'abs/path'
> for track, filename in enumerate(os.listdir(directory), 1):
> pathname = os.path.join(directory, filename)
> subprocess.call(['ffmpeg', '-i', pathname, filename+str(track)+'.mp3'])
>
> On Thu, Jul 31, 2014 at 7:02 PM, C Smith  wrote:
>> Huh, that is quite an annoyance about changing the order though. Any
>> ideas about that? I will look into it further in the meantime...
>>
>> On Thu, Jul 31, 2014 at 6:57 PM, C Smith  
>> wrote:
>>> Works now, thanks!
>>>
>>> On Thu, Jul 31, 2014 at 6:57 PM, C Smith  
>>> wrote:
 woops, I see it pathname != filename

 On Thu, Jul 31, 2014 at 6:55 PM, C Smith  
 wrote:
>>for track, filename in enumerate(os.listdir(directory), 1):
> It seems kinda counter-intuitive to have track then filename as
> variables, but enumerate looks like it gets passed the filename then
> track number. Is that correct and just the way enumerate works, a
> typo, or am I missing something else here?
>
> It is an ffmpeg error I am getting.
> ffmpeg just gives its usual build information and the error is (for
> each song title in the directory):
> songTitleIsHere.flac: no such file or directory
>
> So it looks like it is close to working because it finds the correct
> file names, but doesn't recognize it for some reason.
> Here is how I put in your code
> import os, subprocess
> directory = '/absolute/path/goes/here'
> for track, filename in enumerate(os.listdir(directory), 1):
> pathname = os.path.join(directory, filename)
> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>
> So it goes to the right place, because every song title is listed out,
> ffmpeg or the shell just don't recognize them correctly.
>
> On Thu, Jul 31, 2014 at 6:35 PM, Steven D'Aprano  
> wrote:
>> You may have already have solved your problem, unfortunately my
>> emails are coming in slowly and out of order, but I have a suggestion:
>>
>> On Thu, Jul 31, 2014 at 03:53:48PM -0400, C Smith wrote:
>>> I am on OSX, which needs to escape spaces in filenames with a backslash.
>>
>> Same as any other Unix, or Linux, or, indeed, Windows.
>>
>>> There are multiple files within one directory that all have the same
>>> structure, one or more characters with zero or more spaces in the
>>> filename, like this:
>>> 3 Song Title XYZ.flac.
>>> I want to use Python to call ffmpeg to convert each file to an .mp3.
>>> So far this is what I was trying to use:
>>> import os, subprocess
>>> track = 1
>>> for filename in os.listdir('myDir'):
>>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>> track += 1
>>
>> I believe that your problem is *not* the spaces, but that you're passing
>> just the filename and not the directory. subprocess will escape the
>> spaces for you. Also, let Python count the track number for you. Try
>> this:
>>
>>
>> directory = '/path/to/the/directory'
>> for track, filename in enumerate(os.listdir(directory), 1):
>> pathname = os.path.join(directory, filename)
>> subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
>>
>>
>> I expect something like that will work. You should be able to pass
>> either an absolute path (beginning with /) or a relative path starting
>> from the current working directory.
>>
>> If this doesn't work, please show the full error that you receive. If it
>> is a Python traceback, copy and paste the whole thing, if it's an ffmpeg
>> error, give as much information as you can.
>>
>>
>>
>> --
>> Steven
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread Peter Otten
C Smith wrote:

I'd throw in a check to verify that filename is indeed a flac:

> or more accurately
> import os, subprocess, re
> directory = '/abs/path'
> for track, filename in enumerate(os.listdir(directory), 1):
> pathname = os.path.join(directory, filename)
  if filename.endswith(".flac"):
  subprocess.call(['ffmpeg', '-i', pathname, filename[:-5]+'.mp3'])


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


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread C Smith
Nice, these are useful tools. I have been building something with just
basic stuff and avoiding learning any libraries. If I wanted to get
some insight on a larger program that is about 1000 lines, would that
be doable here?

On Thu, Jul 31, 2014 at 7:37 PM, Peter Otten <__pete...@web.de> wrote:
> C Smith wrote:
>
> I'd throw in a check to verify that filename is indeed a flac:
>
>> or more accurately
>> import os, subprocess, re
>> directory = '/abs/path'
>> for track, filename in enumerate(os.listdir(directory), 1):
>> pathname = os.path.join(directory, filename)
>   if filename.endswith(".flac"):
>   subprocess.call(['ffmpeg', '-i', pathname, filename[:-5]+'.mp3'])
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Need help with python script

2014-07-31 Thread McKinley, Brett D.
I would like to see if someone can help me with a python script.  I'm trying to 
export a file geodatabase feature class to csv file.  This is what I have so 
far:

import arcpy
import os
import csv
import domainvalues


def export_to_csv(dataset, output, dialect):
"""Output the data to a CSV file"""
# create the output writer
out_writer = csv.writer(open(output, 'wb'), dialect=dialect)
# return the list of field names and field values
header, rows = domainvalues.header_and_iterator(dataset)

# write the field names and values to the csv file
out_writer.writerow(map(domainvalues._encodeHeader, header))
for row in rows:
out_writer.writerow(map(domainvalues._encode, row))

if __name__ == "__main__":
# Get parameters
dataset_name = arcpy.GetParameterAsText(0)
output_file = arcpy.GetParameterAsText(1)
delim = arcpy.GetParameterAsText(2).lower()
dialect = 'excel'
if delim == 'comma':
pass
else:
dialect = 'excel-tab'
try:
export_to_csv(dataset_name, output_file, dialect)
except Exception as err:
arcpy.AddError('Error: {0}'.format(err))


I would like for the script to export only certain fields and also export only 
the newest records.  As for now, it's exporting everything.

Thanks
Brett McKinley


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


Re: [Tutor] Using subprocess on a series of files with spaces

2014-07-31 Thread Cameron Simpson

On 01Aug2014 08:35, Steven D'Aprano  wrote:
[...]

I want to use Python to call ffmpeg to convert each file to an .mp3.
So far this is what I was trying to use:
import os, subprocess
track = 1
for filename in os.listdir('myDir'):
subprocess.call(['ffmpeg', '-i', filename, str(track)+'.mp3'])
track += 1


This is basicly ok, except that as others have mentioned you need to put 
"myDir/" on the front of filename, because the file is in the subdirectory.  
probably also with the mp3 output filename depending what you intend.



I believe that your problem is *not* the spaces, but that you're passing
just the filename and not the directory. subprocess will escape the
spaces for you.


I wish people would not say this. subprocess DOES NOT escape the spaces, nor 
does it need to.


The only reason the OP imagines he needs to escape spaces it that he is 
probably testing his ffmpeg command in a shell (entirely reasonable). And in a 
shell you need to escape the spaces to prevent the shell deciding that they are 
separating words. An entirely syntactic issue.


However, the subprocess call above uses a list for the command, and that form 
DOES NOT pass anything to the shell. The command gets executed directly. And 
therefore no spaces need escaping at all.


Just getting this out before we further confuse the OP.

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


[Tutor] dict.get() vs. dict.get

2014-07-31 Thread memilanuk
What is the difference between dict.get() and dict.get as shown in the 
code below:


counts = dict()

for line in input_file:
words = line.split()
if len(words) == 0:
continue
else:
if words[0] != 'From:':
continue
else:
counts[words[1]] = counts.get(words[1], 0) + 1

max_key = max(counts, key=counts.get)
max_value = counts[max_key]

print max_key, max_value

I know the former (counts.get()) is supposed to return a value for a key 
if it exists, or else return the default value (in this case '0').  That 
part of the code is checking to see if the value contained in words[1] 
exists as a key in the dictionary counts or not; either way increment 
the associated value by +1.


The latter (counts.get) reflects some code I found online [1]:

print max(d.keys(), key=lambda x: d[x])

or even shorter (from comment):

print max(d, key=d.get)

that is to replace manually looping through the dict to find the max 
value like this:


max_value = None
max_key = None

for key, value in counts.items():
if max_value is None or max_value < value:
max_value = value
max_key = key

print max_key, max_value


So... the similarity between dict.get() and dict.get as used here is 
kinda confusing me.  When I do a search for 'dict.get' in the python 
docs or on google all I normally find is stuff referring to 'dict.get()'.


Any pointers would be much appreciated.


[1] 
http://stackoverflow.com/questions/12402015/print-the-key-of-the-max-value-in-a-dictionary-the-pythonic-way

--
Shiny!  Let's be bad guys.

Reach me @ memilanuk (at) gmail dot com

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


[Tutor] Python functions are first-class citizens (was: dict.get() vs. dict.get)

2014-07-31 Thread Ben Finney
memilanuk  writes:

> What is the difference between dict.get() and dict.get

The ‘foo()’ syntax calls ‘foo’.

‘dict.get’ is the function (an attribute of the ‘dict’ type), and you
can call that function by specifying parameters in parens ‘()’.

> counts[words[1]] = counts.get(words[1], 0) + 1

This line is calling the ‘get’ method of the ‘counts’ object. Since that
object likely doesn't have its own ‘get’ attribute, this will be the
‘dict.get’ function (since ‘coutns’ is an instance of ‘dict’).

That part of the expression – ‘counts.get(words[1], 0)’ – will evaluate
to whatever is the return value of that call.

> max_key = max(counts, key=counts.get)

This specifies ‘counts.get’, without calling it. The expression
‘counts.get’ evaluates to that function object.

That value is then used as the value for the ‘key’ parameter when
calling ‘max’ here.

> So... the similarity between dict.get() and dict.get as used here is
> kinda confusing me.

I hope that helps. They are the same function; but the former is
*calling* the function object, and optionally using the return value;
the latter is referring to the function object *as* a value.

> Any pointers would be much appreciated.

This use of functions and other callable objects as values, is sometimes
referred to as the fact Python has “first-class functions”
http://en.wikipedia.org/wiki/First-class_function>.

-- 
 \  “What we usually pray to God is not that His will be done, but |
  `\   that He approve ours.” —Helga Bergold Gross |
_o__)  |
Ben Finney

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


Re: [Tutor] dict.get() vs. dict.get

2014-07-31 Thread Steven D'Aprano
On Thu, Jul 31, 2014 at 07:56:23PM -0700, memilanuk wrote:
> What is the difference between dict.get() and dict.get as shown in the 
> code below:

Depending on how you look at it, the difference is either "nothing at 
all", or "the round brackets ()". And I'm not trying to be funny.

Experimenting at the interactive interpreter may shed some light on 
this:

py> d = {1: 'a', 2: 'b'}
py> d.get



So dict.get returns the "get" method of dict objects, but without 
actually calling it yet. It doesn't get called until add 
parentheses (round brackets) to it:

py> method = d.get  # Don't call it yet, just grab the method.
py> print method

py> method(2)  # Now call it.
'b'
py> method(3, 'ZZZ')
'ZZZ'


So adding brackets to the method object calls it. We can do the same 
with *any* function or method, not just "get":

py> len

py> len([])
0


Now, what's the difference between d.get and dict.get? The first one is 
*bound* to an instance, in this case d, while the second one is not. Let 
me explain.

Most methods operate on a specific instance. These two calls to "get" do 
different things, because the instance they are calling on are 
different:


instance1 = {1: "a", 2: "b"}
instance2 = {1: "AAA", 2: "BBB"}
instance1.get(1)  # returns "a"
instance2.get(1)  # returns "AAA"


Pretty obvious, right? And the same thing happens if you grab the 
method, it remembers which instance it came from:


method = instance2.get  # Don't call the method yet!
method(2)  # returns "BBB"



So far so good. But what happens if you access the method, not from an 
instance, but directly from the class?


method = dict.get  # comes from dict, not from instance1 or instance2


You still get a method, but now you get a method that doesn't know which 
instance to operate on. If the earlier examples were bound to an 
instance, this one is called an *unbound* method. So when you call it, 
you have to provide the instance first!


method(instance1, 2)  # returns "b"
method(instance2, 2)  # returns "BBB"


To look at it another way, when you define a method, you write it with a 
"self" argument:

class MyClass:
def do_something(self, x, y, z):
...


instance = MyClass()
instance.do_something(x, y, z)


In this case, Python sees that you're calling the method from instance, 
and automatically provides that as the "self" argument. But if you do 
this:


MyClass.do_something


there's no "self" yet, so you have to manually provide it.

And that's the difference between bound and unbound methods.


Does help explain what is going on? Feel free to ask more questions if 
needed.




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


Re: [Tutor] Python functions are first-class citizens

2014-07-31 Thread memilanuk

On 07/31/2014 08:22 PM, Ben Finney wrote:

memilanuk  writes:

>> So... the similarity between dict.get() and dict.get as used here is
>> kinda confusing me.
>
> I hope that helps. They are the same function; but the former is
> *calling* the function object, and optionally using the return value;
> the latter is referring to the function object *as* a value.

Conceptually, that makes sense.  Practically... having a little trouble 
getting my head around it - see below.




max_key = max(counts, key=counts.get)


This specifies ‘counts.get’, without calling it. The expression
‘counts.get’ evaluates to that function object.

That value is then used as the value for the ‘key’ parameter when
calling ‘max’ here.


Been reading a bit more in the mean time, trying to grok that 'key' 
parameter for max()... and of course the python docs for max(iterable, 
key=) refer to the docs for list.sort() ;)


Kind of diverging off the original question a bit... but since it did 
include the max() code in it... I'm having a bit of difficulty with the 
whole 'key=' parameter when combined with counts.get here.


So counts is the iterable, and counts.get is the key used to iterate 
through it?


I guess I'm not getting how the whole key, function object bit works 
here in actual practice.


if we have

counts = {'a':1, 'b':22, 'c':100}

then counts.get('b') should return 22.  I got that much.

And counts.get is just an uncalled version of that:

foo = counts.get
foo('b')

should return 22 as well.  Think I got that as well.

Where things are going pear-shaped is how counts.get can function as a 
'key' when we don't actually supply () (or anything inside them) to 
specify what k,v pair we want, and how that relates back to the iterable 
for max(), counts?


Monte


--
Shiny!  Let's be bad guys.

Reach me @ memilanuk (at) gmail dot com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python functions are first-class citizens

2014-07-31 Thread Alan Gauld

On 01/08/14 06:12, memilanuk wrote:


counts = {'a':1, 'b':22, 'c':100}

then counts.get('b') should return 22.  I got that much.

And counts.get is just an uncalled version of that:

foo = counts.get
foo('b')

should return 22 as well.  Think I got that as well.


Well done, thats the crux of it.


Where things are going pear-shaped is how counts.get can function as a
'key' when we don't actually supply () (or anything inside them)


The max() function calls the key function internally.

Here is a trivial example (untested code!)

>>> def f(aFunc):
  for n in range(3):
print("Your function returns",aFunc(n),"with input",n)
>>>
>>> def g(n):
  return n+2
>>>
>>> f(g)
Your function returns 2 with input 0
Your function returns 3 with input 1
Your function returns 4 with input 2

So f calls aFunc using n internally.
The specification of f requires that you pass it a function that accepts 
a single argument


max and sort etc work the same way.
They call your function internally.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] Python functions are first-class citizens

2014-07-31 Thread Ben Finney
memilanuk  writes:

> On 07/31/2014 08:22 PM, Ben Finney wrote:
> >> max_key = max(counts, key=counts.get)
> >
> > This specifies ‘counts.get’, without calling it. The expression
> > ‘counts.get’ evaluates to that function object.
> >
> > That value is then used as the value for the ‘key’ parameter when
> > calling ‘max’ here.
>
> Been reading a bit more in the mean time, trying to grok that 'key'
> parameter for max()... and of course the python docs for max(iterable,
> key=) refer to the docs for list.sort() ;)

The ‘max’ function can be told how to determine the ordering of items,
by specifying a key parameter. The parameter is specified by giving a
value; that value is a function.

The ‘max’ implementation uses the ‘key’ parameter by calling it. So in
this way, you can specify any behaviour you like, by making a function
that determines ordering however you like.

Generally, though, there are existing functions you can use; and a
dictionary's ‘get’ method is one of them.

This is all intentional, and is why the fact functions are first-class
in Python is such a strength.

> So counts is the iterable

More accurately, it is a collection. Whether it is iterable isn't
relevant to ‘max’.

> and counts.get is the key used to iterate through it?

No, ‘max’ does not necessarily iterate; it computes (in whatever way
makes sense) which item is the largest. You're specifying how items are
to be compared. Iteration is irrelevant to this.

> Where things are going pear-shaped is how counts.get can function as a
> key' when we don't actually supply () (or anything inside them) to
> specify what k,v pair we want, and how that relates back to the
> iterable for max(), counts?

A function is itself a value. That may take a little thinking to get
your head around, but all of this merely follows as a consequence.

You may also need to spend time distinguishing between syntax and value.
‘foo’ and ‘foo()’ are different in their syntax; when evaluated in an
expression, they have entirely different meanings. But the latter
differs only in that an extra function call occurs before evaluating the
result.

-- 
 \ “God was invented to explain mystery. God is always invented to |
  `\ explain those things that you do not understand.” —Richard P. |
_o__)Feynman, 1988 |
Ben Finney

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