0

get_asset_type_v001.py

import re

def get_asset_type(filename):
    """Check filename for asset type"""
    global is_vfx_file, is_edit_file, proj_yy, client, show, edit_version, edit_stage, tv, pv, date, seq_nr, seq_name, shot_nr, shot_name
    is_vfx_file = False
    is_edit_file = False
    if re.match(edit_pattern, filename):  # Check if file is a edit prev
        print("\nFile is an Edit prev. Filename: {}".format(filename))
        proj_yy, client, show, edit_version, edit_stage, tv, pv, date, *_ = filename.split("_")
        is_edit_file = True
    if re.match(vfx_pattern, filename):  # Check if file is a VFX prev
        print("\nFile is a VFX prev. Filename: {}".format(filename))
        seq_nr, seq_name, shot_nr, shot_name, *_ = filename.split("_")
        is_vfx_file = True

# get_asset_type(sample_vfx_filename) # Call function to return is vfx file and print result
# print(is_vfx_file)  # Check if it's a vfx file

Printing the function in the same file works like charm.

print('_'.join(list((seq_nr, seq_name, shot_nr, shot_name))))

main_programm_file_v001.py

from get_asset_type_v001 import *  # Import function ti check what asset type it is


filename = sample_vfx_filename

get_asset_type(filename)  # Code workes fine till this line
print('_'.join(list((seq_nr, seq_name, shot_nr, shot_name))))  # The variables in this line "seq_nr..." are making the problem

But trying to use the code from the imported function in another file raises the following error. Seems like I can't access the global var.

NameError: name 'seq_nr' is not defined

8
  • 1
    seq_nr is only defined if re.match(vfx_pattern, filename) succeeds. Did it? Check the value of is_vfx_file. Commented Mar 25, 2020 at 16:45
  • 2
    On another note, don't use so many global variables. get_asset_type should return an object that encapsulates the information returned by filename.split("_"). Commented Mar 25, 2020 at 16:46
  • NameError: name 'is_vfx_file' is not defined Commented Mar 25, 2020 at 16:56
  • So no variable gets through .. :/ Commented Mar 25, 2020 at 16:58
  • 1
    They weren't defined at the time you imported the module (they are only created when you call the function), and even if they were, there is a difference between the local name is_vfx_file in main_programm_file_v001.py imported from the module and is_vfx_file.seq_nr itself. Commented Mar 25, 2020 at 16:59

2 Answers 2

1

I would redefine the function to return a value that includes metadata about what type of filename was passed. One example:

import re

def get_asset_type(filename):
    """Check filename for asset type"""
    if re.match(edit_pattern, filename):
        print("\nFile is an Edit prev. Filename: {}".format(filename))
        fields = filename.split("_")
        return {'type': 'edit',
                'proj_yy': fields[0],
                'client': fields[1],
                ...}
    if re.match(vfx_pattern, filename):  # Check if file is a VFX prev
        print("\nFile is a VFX prev. Filename: {}".format(filename))
        fields = filename.split("_")
        return {'type': 'vfx',
                'seq_nr': fields[0],
                'seq_name': fields[1],
                ...}

It's likely that your regular expressions can be defined using capture groups to avoid the need to subsequently split the file name; if the pattern matches, the return value will already contain the relevant components.

Then in your main program:

import get_asset_type_v001

filename = sample_vfx_filename

result = get_asset_type_v001.get_asset_type(filename)
if result['type'] == 'vfx':
    print('_'.join([result['seq_nr'], result['seq_name'], ...]))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your approach. It's actually the first one that seems to work! I appreciate all your effort that went into this. Two more questions regarding this. Isn't Return the same thing as Global just a different way to return it? And secondly, you wrote I should use capture groups. So I read my way through them and used them. But I'm not sure if it makes the code easier to write or to understand, even if it is an interesting approach. I've written my code with the capture groups below. Thanks again.
return and global are completely different. Returning a value makes no assumptions about what variables are or will be defined outside of the function itself.
0

option with capture groups:

def get_asset_type(filename): # V003
    """Check filename for asset type"""
    n = re.match(edit_pattern, filename)
    if n:  # Check if file is a edit prev
        print("\nFile is an Edit prev. Filename: {}".format(filename))
        fields = filename.split("_")
        return {'type': 'edit',
                'proj_yy': n.group('proj_yy'),
                'client': n.group('client'),
                'show': n.group('show'),
                'edit_version': n.group('edit_version'),
                'edit_stage': n.group('edit_stage'),
                'tv': n.group('tv'),
                'pv': n.group('pv'),
                'date': n.group('date'),
                }
    m = re.match(vfx_pattern, filename)
    if m:  # Check if file is a VFX prev
        print("\nFile is a VFX prev. Filename: {}".format(filename))
        print(m.group('seq_name'))
        return {'type': 'vfx',
                'seq_nr': m.group('seq_nr'),
                'seq_name': m.group('seq_name'),
                'shot_nr': m.group('shot_nr'),
                'shot_name': m.group('shot_name')
                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.