[Tutor] a few question about my evolving program

Clayton Kirkwood crk at godblessthe.us
Wed Aug 12 07:33:48 CEST 2015

> -----Original Message-----
> From: Cameron Simpson [mailto:cs at zip.com.au]
> Sent: Tuesday, August 11, 2015 8:46 PM
> To: Clayton Kirkwood <crk at godblessthe.us>
> Cc: tutor at python.org
> Subject: Re: [Tutor] a few question about my evolving program
> On 11Aug2015 20:23, Clayton Kirkwood <crk at godblessthe.us> wrote:
> >Question 1:
> >What is the purpose and how is the following definition focused on the *?
> >Turns out, you can't actually put the asterisk in the code, so what
> >does it mean?
> >os.stat(path, *, dir_fd=None, follow_symlinks=True)
> It is generally better to make separate posts for separate questions,
> otherwise the discussions get all mixed up. And you can pick better
> lines that way.
> Python function definition syntax and semantics are defined here:
>   https://docs.python.org/3/reference/compound_stmts.html#function-
> definitions
> and that in turn points to parameters:
>   https://docs.python.org/3/glossary.html#term-parameter
> Give them a read. The bare "*" in a function definition says that the
> keyword parameters (dir_fd and follow_symlinks in your example) may only
> be supplied to function calls in keyword form, i.e.:
>   os.stat(pathname, follow_symlinks=True)
> Without the bare "*", unused positional parameters are assigned to the
> keywords parameters, allowing:
>   os.stat(pathname, None, True)
> to set these two. The bare "*" forbids this, which avoids a lot of
> and common errors.

Well, thanks. The asterisk is only used in the *description* of the function
call and says that everything following must be keyword defined. If the
definition of the function call doesn't have the asterisk, then if possible,
assignments can be used or just using ordered (positional) callout is
possible. The issue comes down to the documentation being too efficient and
saying things only once. When learning it is difficult to remember every
nuance even reading through the documentation more than once, because it
isn't always inherently obvious what something means and when specifically
it is important that that specific character must be remembered.

> >Question 2:
> >My current code:
> >See "Look here" below.
> [...]
> >    for current_filename in current_file_list:
> >#        print( "looking at file  ", filename, "  in
> >top_directory_file_list:   ", top_directory_file_list )
> >#        print( "and in current_directory_path:  ",
> >
> >Look here:
> >
> >         if current_filename in target_directory_file_list:
> >#top_directory_file_list
> >That's it:<)) Go down to the bottom now:<))
> >
> >            current_stat_info = os.stat(current_directory_path + '/' +
> >current_filename, follow_symlinks = False )
> >            current_file_size = current_stat_info.st_size
> >            if current_file_size ==
> >                #the filename is a duplicate and the size is a duplicate:
> >they are the same file
> >                print( "file ", current_filename, "size: ",
> >current_file_size, " found in both current_directory_path ",
> >current_directory_path,
> >                  " and ", target_directory, "\n")
> >                duplicate_files =+ 1
> >
> >             else:
> >                print( "file ", current_filename, " not a duplicate\n")
> >
> >current_filename = 'IMG00060.jpg'
> >
> >target_directory_file_list = ['2010-11-02 15.58.30.jpg', '2010-11-02
> >15.58.45.jpg', '2010-11-25 09.42.59.jpg', '2011-03-19 19.32.09.jpg',
> >'2011-05-28 17.13.38.jpg', '2011-05-28 17.26.37.jpg', '2012-02-02
> >20.16.46.jpg', '218.JPG', 'honda accident 001.jpg', 'honda accident
> >002.jpg', 'honda accident 003.jpg', 'honda accident 004.jpg', 'honda
> >accident 005.jpg', 'honda accident 006.jpg', 'honda accident 007.jpg',
> >'Image (1).jpg', 'Image.jpg', 'IMG.jpg', 'IMG00003.jpg',
> >'IMG00040.jpg', 'IMG00058.jpg', 'IMG_0003.jpg', 'IMG_0004.jpg',
> >'IMG_0005.jpg', 'IMG_0007.jpg', 'IMG_0008.jpg', 'IMG_0009.jpg',
> >'IMG_0010.jpg', 'Mak diploma handshake.jpg', 'New Picture.bmp', 'temp
> >121.jpg', 'temp 122.jpg', 'temp 220.jpg', 'temp 320.jpg', 'temp
> >321.jpg', 'temp 322.jpg', 'temp 323.jpg', 'temp 324.jpg', 'temp
> >325.jpg', 'temp 326.jpg', 'temp 327.jpg', 'temp 328.jpg', 'temp
> >329.jpg', 'temp 330.jpg', 'temp 331.jpg', 'temp 332.jpg', 'temp
> >333.jpg', 'temp 334.jpg', 'temp 335.jpg', 'temp 336.jpg', 'temp
> >337.jpg', 'temp 338.jpg', 'temp 339.jpg', 'temp 340.jpg', 'temp
> >341.jpg', 'temp 342.jpg', 'temp 343.jpg']
> >
> >As you can see the current_filename does not exist in
> >target_directory_file list. Yet, I fall through to the next line. Yes,
> >the indents are all fine: I wouldn't have gotten to running code
> >otherwise.  I turned my head upside down and still couldn't see why it
> doesn't work and what I am missing?
> Have you put in a print statement to prove this, and also to display
> current_filename and target_directory_file on that next line?

I am using the PyCharm debugger in this instance. I validated by going to
the directory itself and verifying what the value of what
target_directory_file_list says in the debugger. The file doesn't exist in
that directory although it does exist in the current directory file list.

> Can you reduce this to a MUCH smaller program (eg 10 lines long) showing
> the same problem? For example by hardwiring the values of
> current_filename and
> target_directory_file:
>   current_filename = 'foo'
>   target_directory_file_list = ['2010-11-02 15.58.30.jpg', '2010-11-02
>   15.58.45.jpg', '2010-11-25 09.42.59.jpg', '2011-03-19 19.32.09.jpg',
>   '2011-05-28 17.13.38.jpg', '2011-05-28 17.26.37.jpg', '2012-02-02
>   20.16.46.jpg', '218.JPG', 'honda accident 001.jpg', 'honda accident
>   002.jpg', 'honda accident 003.jpg', 'honda accident 004.jpg', 'honda
>   accident 005.jpg', 'honda accident 006.jpg', 'honda accident 007.jpg',
>   'Image (1).jpg', 'Image.jpg', 'IMG.jpg', 'IMG00003.jpg', 'IMG00040.jpg',
>   'IMG00058.jpg', 'IMG_0003.jpg', 'IMG_0004.jpg', 'IMG_0005.jpg',
>   'IMG_0007.jpg', 'IMG_0008.jpg', 'IMG_0009.jpg', 'IMG_0010.jpg', 'Mak
> diploma
>   handshake.jpg', 'New Picture.bmp', 'temp 121.jpg', 'temp 122.jpg', 'temp
>   220.jpg', 'temp 320.jpg', 'temp 321.jpg', 'temp 322.jpg', 'temp
>   'temp 324.jpg', 'temp 325.jpg', 'temp 326.jpg', 'temp 327.jpg', 'temp
>   328.jpg', 'temp 329.jpg', 'temp 330.jpg', 'temp 331.jpg', 'temp
>   'temp 333.jpg', 'temp 334.jpg', 'temp 335.jpg', 'temp 336.jpg', 'temp
>   337.jpg', 'temp 338.jpg', 'temp 339.jpg', 'temp 340.jpg', 'temp
>   'temp 342.jpg', 'temp 343.jpg']
>   if current_filename in target_directory_file_list:
>     print("IN! (unexpected!)")
>   else:
>     print("NOT IN")
> If the small program works correctly, that may point you to the issue in
> larger program.

I understand what you are saying. However, you guys are constantly asking
for the original code. Now you are saying you are saying not the original
code, rather a made up snippet, which very likely, for newbies like me,
would bet bungled in creating a snippet. Can't win for losing.

Clayton, but I am getting better!

> Cheers,
> Cameron Simpson <cs at zip.com.au>

More information about the Tutor mailing list