0

I'm using this code to assign data to some variables if a form doesn't validate. This is some logic I'll be using a lot in my script. I want to create a function so that the else portion of this statement is stored in a function, so that I can just call it rather than pasting these lines each time.

if form.validate_on_submit():
        do something

else:

        brand_title=form.brand_title.data or ''
        carrier_format=form.carrier_format.data or ''
        recording_artist=form.recording_artist.data or ''
        producer=form.producer.data or ''
        session=form.session.data or ''
        tx_date=form.tx_date.data or ''
        network=form.network.data or ''
        programme_number=form.programme_number.data or ''
        start_time_1=form.start_time_1.data or ''

I've created a function like so:

def variables():
        brand_title=form.brand_title.data or ''
        carrier_format=form.carrier_format.data or ''
        recording_artist=form.recording_artist.data or ''
        producer=form.producer.data or ''
        session=form.session.data or ''
        tx_date=form.tx_date.data or ''
        network=form.network.data or ''
        programme_number=form.programme_number.data or ''
        start_time_1=form.start_time_1.data or ''

But how do I return the variables so that calling the function mirrors typing each line out (as in else section in the first section of code). I've read that simply returning each variable like so:

return (brand_title. carrier_format, recording_artist, producer, session, tx_date, network, programme_number, start_time_1)

would create a tuple, which doesn't seem like the correct option for my needs.

3
  • What type is form - what methods does it support? Commented Aug 2, 2017 at 16:52
  • 1
    Ahh... is it a WTForm? Commented Aug 2, 2017 at 16:54
  • Looks like you should take a copy of the form.data dict and return that to wherever it's needed... then access it via copy_of_form_data.get('session', '') Commented Aug 2, 2017 at 16:57

2 Answers 2

3

Might I suggest a small change to your function:

def variables():
    var_dict = {}

    var_dict['brand_title'] = form.brand_title.data or ''
    var_dict['carrier_format'] = form.carrier_format.data or ''
    var_dict['recording_artist'] = form.recording_artist.data or ''
    var_dict['producer'] = form.producer.data or ''
    var_dict['session'] = form.session.data or ''
    var_dict['tx_date'] = form.tx_date.data or ''
    var_dict['network'] = form.network.data or ''
    var_dict['programme_number'] = form.programme_number.data or ''
    var_dict['start_time_1'] = form.start_time_1.data or ''

    return var_dict

Using a dictionary to store your data is cleaner, especially when you have so many related variables.


If this is a WTForm, you might just access the data which is already in a dict with var_dict = form.data.copy(), and then access your fields with var_dict.get(<var>, '') as needed.

Sign up to request clarification or add additional context in comments.

4 Comments

Ironically, if this is a WTForm object as I suspect, then form.data is already a dict and the above is just redundant... might as well just have var_dict = form.data.copy() they can just return that, then access fields as var_dict.get('session', '') when required...
@JonClements Thanks for this. I have no experience with that sorta stuff, so I've taken your word for it and added a footnote.
Very trusting of you - be warned I'm just guessing though - but reasonably confident :)
hmm i'll test this now. But if I need to call each attribute with e.g. var_dict.get('session', '') this could be troublesome as the templates for each page use the variable name e.g. brand_title . It could get a bit messy if I need change each instance but i'll try this out and see
0

A dictionary would be a better. Also, you can create a list or other container with the names of the attributes you want and then generalize your logic into a loop:

def get_values(form):
    names = ['brand_title', 'carrier_format', 'recording_artist'] # add others
    return {name: getattr(form, name).data or '' for name in names}

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.