[melbourne-pug] hi, python peeps

Anthony Briggs anthony.briggs at gmail.com
Wed Aug 1 10:18:40 CEST 2012


Better not to use builtins as variable names in the first place though.

On 1 August 2012 18:08, William ML Leslie <william.leslie.ttg at gmail.com>wrote:

> list = list() is an unbound local error. (worries me a bit that people
> can't figure out python's scoping rules - are we not explaining them
> properly? They are mostly straightforward if you ignore class scope.)
>
> On 01/08/2012 6:02 PM, "Mike Dewhirst" <miked at dewhirst.com.au> wrote:
>
> On 1/08/2012 5:19pm, Sam Watkins wrote:
> >
> > hi there,
> >
> > I'm learning python, I like it and thought...
>
> I think the trend nowadays in python coding is to be a little more
> explicit than yesteryear - perhaps more like this ...
>
> #def ls(dir, hidden=0, relative=1):
> def ls(dir, hidden=False, relative=True):
>     #list = []
>     # personally I'm uncomfortable using Python core words but ymmv
>     list = list()
>     for nm in os.listdir(dir):
>         #if not hidden and nm[0] == '.':
>         if not hidden and nm.startswith('.'):
>             continue
>         if not relative:
>             nm = os.path.join(dir, nm)
>         list.append(nm)
>     list.sort()
>     return list
>
> #def find(root, files=1, dirs=0, hidden=0, relative=1, topdown=1):
> def find(root, files=True, dirs=False, hidden=False, relative=True,
> topdown=True):
>     root = os.path.join(root, '')  # add slash if not there
>     #root_len = len(root)
>     for parent, ldirs, lfiles in os.walk(root, topdown=topdown):
>         if relative:
>             #parent = parent[root_len:]
>             parent = parent[len(root):]
>         if dirs and parent:
>             yield os.path.join(parent, '')
>         if not hidden:
>             #lfiles   = [nm for nm in lfiles if nm[0] != '.']
>             lfiles   = [nm for nm in lfiles if not nm.startswith('.')]
>             #ldirs[:] = [nm for nm in ldirs  if nm[0] != '.']  # in place
>             ldirs[:] = [nm for nm in ldirs  if not nm.startswith('.')]  #
> in place
>         if files:
>             lfiles.sort()
>             for nm in lfiles:
>                 nm = os.path.join(parent, nm)
>                 yield nm
>
>
>
>
>
>
>
> >
> >    http://sam.nipl.net/code/python/find.py
> >
> > I'm very happy to do a similar code review ...
>
>
> _______________________________________________
> melbourne-pug mailing list
> melbourne-pug at python.org
> http://mail.python.org/mailman/listinfo/melbourne-pug
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20120801/c2588507/attachment-0001.html>


More information about the melbourne-pug mailing list