Re: New plugin: randompic.py

Marius Gedminas <[email protected]> Fri, 29 May 2009 22:21:13 +0300
Newsgroups gmane.comp.web.pyblosxom.devel
Message-ID <20090529192113.GB31316@platonas>
On Fri, May 29, 2009 at 12:14:31AM +0100, Toby Smithe wrote:
> I developed a very simple plug-in to choose a random image from a
> pre-configured directory, and save its name in a variable. I thought
> I'd contribute it as it could be useful to someone else, and it's sad
> to see "portico" all alone in the "images" section of the plug-in
> registry.
> 
> The script is attached, and it is licensed "MIT", with copyright
> attributed to myself. See the file for more information.

Some remakrs:

> import os, random
> 
> from PIL import Image
> """
> randompic.py pyblosxom plugin
> Chooses random image from directory

It is customary to put import statements *after* the docstring.
(Otherwise it's not a docstring, but a string expression that's
discarded).

> 
> randompic.py chooses a random image from a directory, storing its
> path in the variable,
> 
> $random_image
> 
> CONFIGURATION
> 
> In config.py, add:
>   py['randompic_directory'] = "/path/to/directory"
> 
> 
> LICENCE - MIT
> 
> Copyright (c) 2009 Toby Smithe <tsmithe-GeWIH/[email protected]>
> 
> Permission is hereby granted, free of charge, to any person
> obtaining a copy of this software and associated documentation
> files (the "Software"), to deal in the Software without
> restriction, including without limitation the rights to use,
> copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the
> Software is furnished to do so, subject to the following
> conditions:
> 
> The above copyright notice and this permission notice shall be
> included in all copies or substantial portions of the Software.
> 
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
> OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
> HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
> WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
> OTHER DEALINGS IN THE SOFTWARE.
> 
> """
> 
> 
> __author__ = 'Toby Smithe <tsmithe-GeWIH/[email protected]>'
> __version__ = '1'
> __url__ = 'http://fulltinreality.com/blog'
> 
> 
> def cb_prepare(args):
>   request = args["request"]

Please, please use 4-space indents like the rest of the world (except
Google).

>   config = request.getConfiguration()
>   data = request.getData()
>   directory = config["randompic_directory"]
>   images = []
> 
>   for f in os.listdir(directory):
>   	try: im = Image.open(os.path.join(directory, f))

Tabs are evil, avoid them.

>   	except: im = None
>   	
>   	if im is not None:
>   		images.append(f)

you could shorten that to

    for f in os.listdir(directory):
        try:
            images.append(Image.open(os.path.join(directory, f)))
        except:
            pass

>   
>   count = len(images)
>   if count < 1: return
>   index = random.randint(0, (count-1))
>   
>   data["random_image"] = images[index]

and this to

    if not images:
        return
    data["random_image"] = random.choice(images)

Cheers!
Marius Gedminas
-- 
After having done some test using hi-tech istruments (moving my mouse
during a kernel build) [...]
        -- Davide Libenzi on lkml

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT 
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & 
iPhoneDevCamp as they present alongside digital heavyweights like Barbarian 
Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com

_______________________________________________
Pyblosxom-devel mailing list
Pyblosxom-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/pyblosxom-devel
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iD8DBQFKIDWpkVdEXeem148RAh8TAJ9LqzHGfXydV0R0OFKhrLh62RrCOACfRDJs
oD+yM7kxCZHfzzYXWSbYoeA=
=dSJj
-----END PGP SIGNATURE-----