Well, if you have a rigid enough naming convention to the point that you know 
there will always be a version directory in the path, you can make some 
assumptions in your code. If you know what position that directory will be at, 
and that the path you’re testing will always be valid, you can make even 
tighter assumptions.

Quick example that assumes you know there will be a version directory 
*somewhere*:


VER_TOK = re.compile('^(v|V)(\d+)$')
def getVersion(path):
    for part in path.split('/'):
        m = VER_TOK.match(part)
        if m is not None:
            return int(m.group(2))


As far as returning the index, that’s even easier:

VER_TOK = re.compile('^(v|V)(\d+)$')
def getVersionDirIndex(path):
    for i, part in enumerate(path.split('/')):
        if VER_TOK.match(part):
            return i


-Nathan



From: thoma 
Sent: Wednesday, August 29, 2012 2:38 PM
To: [email protected] 
Subject: [Nuke-python] re.search match and list index?

Hi everyone,

I'm trying to search a file path list (split by'/') for the version directory 
and return the index number of that object in that list. So if my path is:

x:\projects\sequences\shot_01\v003\

i want to search with something like

match = re.search( r'(v|V|_v|_V)\d+', myPath)

then get the index number of the result. 
Right now i'm using match.object() to get the matched value as a string but 
does this result contain the index value as well?

Full disclosure - I'm trying to piece together a set version to latest script 
that accounts for paths with version folders and version string in the file 
name because all the scripts i've found have certain assumptions about the 
folder structure hard-coded into the script. I've tried the one here:
http://www.nukepedia.com/python/nodegraph/version-to-latest/
...which looks like exactly what i want but I always get a
"'AttributeError: 'NoneType' object has no attribute 'group'"

Also, the built in version.py (apparently used by DD 'and Weta, apparently')
doesn't seem to do anything at all.

Any help is much appreciated!

Thomas


--------------------------------------------------------------------------------
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python

Reply via email to