Thread: Code Review.

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    Code Review.


    Code:
    import time, os
    
    def URLWrite(file='links.txt', folder='images', theClass='', alt='', width='' , height=''):
        """
        Creates a file having HTML code for the images in the specified foler.
    
        Arguments:    
            file -> the file in which the links will be stored
            folder -> folder to grab the images from, must be int he same directory
            theClass -> specifies the class(es) to be added to the tags
            alt -> specifies the alternate text to be displayed if the image is not displayed
            width -> specifies the width of the image
            height -> specifies the height of the image
        """
        start = time.clock()
        images = os.listdir(path=folder)
        with open(file, 'w') as file:
            count = 0
            for names in images:
                print('Writing link for the image: [' + names + ']...')
                file.write('<img class="' + theClass + '" alt="' + alt + '" src="images/' + str(names) + '" width="' + width + '" height="' + height + '" />' + '\n')
                count += 1
        end = time.clock() - start
        print('\n\nWritten ' + str(count) + ' links in ' + str(round(end, 3)) + ' seconds.')
    
    
    if __name__ == '__main__':
        URLWrite(file='ImageLinks.html', folder='images', theClass="full-image slider shadow", alt="This is an image of the photo gallery", width='500px', height='500px')
    On a scale of 0-10, how was it and how can it be improved?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Given that I called URLWrite as
    Code:
    URLWrite(file='ImageLinks.html', folder=r'C:\Users\dlambert\Pictures', theClass="full-image slider shadow", alt="This is an image of the photo gallery", width='500px', height='500px')
    from some other directory on my computer, the output from your program was
    Code:
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/Bayes.png" width="500px" height="500px" />
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/desktop.ini" width="500px" height="500px" />
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/element.xcf" width="500px" height="500px" />
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/followedSharePointInstructions.png" width="500px" height="500px" />
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/negative_hypergeometric_plot.eps" width="500px" height="500px" />
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/negative_hypergeometric_plot.jpg" width="500px" height="500px" />
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/negative_hypergeometric_plot.png" width="500px" height="500px" />
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/pd.png" width="500px" height="500px" />
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/pgf-license-info.png" width="500px" height="500px" />
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/relationships_of_the_genasys_backend.png" width="500px" height="500px" />
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/relationships_of_the_genasys_backend.xcf" width="500px" height="500px" />
    Some of the files aren't images. When I viewed the html file with chrome the images didn't appear.

    Is there an option for absolute pathnames?


    The html file is incomplete. Are you including this output in a grander html file? If so, it's probably ok. Otherwise, I'd expected html like this:

    <!DOCTYPE html>
    <!--[if IE 6]>
    <html id="ie6" lang="en-US">
    <![endif]-->
    <!--[if IE 7]>
    <html id="ie7" lang="en-US">
    <![endif]-->
    <!--[if IE 8]>
    <html id="ie8" lang="en-US">
    <![endif]-->
    <!--[if !(IE 6) | !(IE 7) | !(IE 8) ]><!-->
    <html lang="en-US">
    <!--<![endif]-->
    <head>
    etceteras


    At first my only comment was that setting count=0 within a with context didn't make sense. Not wrong, but also not part of the context. Then I realized that count will be len(images) so get rid of count---at least the count+=1 bit, and use instead str(len(images)) . And then I tested the program.

    I also wished the message said
    "written so many links to (the file name) in so many seconds ..."

    Otherwise, the argument documentation was great. I consulted it.
    Last edited by b49P23TIvg; February 14th, 2014 at 02:27 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Some of the files aren't images.
    I think a small conditional statement can take care of that, I didn't because the code I wrote was only for me, but I'll try and improve it for the future, thanks.


    Is there an option for absolute pathnames?
    Check the other reply.

    The html file is incomplete. Are you including this output in a grander html file? If so, it's probably ok. Otherwise, I'd expected html like this:
    Yes, I wanted the images for a photo-gallery which required links to images in HTML...

    str(len(images))
    Thank you! Can't believe I didn't notice it before...

    "written so many links to (the file name) in so many seconds ..."
    Just made it to this:
    Code:
    print('\n\nWritten ' + str(len(images)) + ' links to ' + str(file.name)  + ' in ' + str(round(end, 3)) + ' seconds.')
    Otherwise, the argument documentation was great. I consulted it.
    Thanks again.
    Last edited by Akshat1; February 15th, 2014 at 08:46 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Upgraded version: http://pastie.org/private/4sjw4usiadunpgspi80jg
    Last edited by Akshat1; February 15th, 2014 at 08:52 AM.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Still doesn't write absolute path names, boo hoo.
    Code:
    <img class="full-image slider shadow" alt="This is an image of the photo gallery" src="images/invi.png" width="" height="500px" />
    I've decided it's ok to get the computer to do some work. Rather than all that typing or however you do it, I use .split() to generate [ 'a','list', 'of', 'strings']
    Code:
        imagesExt = 'ani bmp cal fax gif img jbg jbig jpe jpeg jiff jpg mac pbm pcd pcx pct pcit pgm png ppm psd ras tga tiff wmf'.split()
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Still doesn't write absolute path names, boo hoo.
    It is meant to grab images from a folder, not grab a single image from a folder.

    I've decided it's ok to get the computer to do some work. Rather than all that typing or however you do it, I use .split() to generate [ 'a','list', 'of', 'strings']
    Code:
        imagesExt = 'ani bmp cal fax gif img jbg jbig jpe jpeg jiff jpg mac pbm pcd pcx pct pcit pgm png ppm psd ras tga tiff wmf'.split()
    Thanks for this.
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    src="/absolute/path/invi.png"

    I'm hoping for an option that writes the absolute path.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by b49P23TIvg
    src="/absolute/path/invi.png"

    I'm hoping for an option that writes the absolute path.
    Sorry, but the code is not meant for a single image...and absolute path like C:\User... works!

IMN logo majestic logo threadwatch logo seochat tools logo