Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8218239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:43:27+00:00 2026-06-07T12:43:27+00:00

payload is a comma separated chunk of data that represents the readings from an

  • 0

payload is a comma separated chunk of data that represents the readings from an inverter. These readings need to be stored in a table called InverterHistory. There will not always be a value for each field. The model already has been setup to allow null/blank for the fields. So I am trying to check to see if there is a value present before assigning it to a field. Here is what I have so far:

i = Inverter.objects.get(mac=u)
payload.reverse()

try:
    ac_volts_a = str(payload.pop())
    ac_volts_b = str(payload.pop())
    ac_volts_c = str(payload.pop())
    ac_current_a = str(payload.pop())
    ac_current_b = str(payload.pop())
    ac_current_c = str(payload.pop())
    dc_volts = str(payload.pop())
    dc_current = str(payload.pop())
    kw_out = str(payload.pop())
    mwh_total = str(payload.pop())
    current_time  = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")

    i_data = InverterHistory(
        inverter = i,
        if ac_volts_a: 
            voltage_ac_a = float(ac_volts_a),
        if ac_volts_b: 
            voltage_ac_b = float(ac_volts_b),   
        if ac_volts_c: 
            voltage_ac_c = float(ac_volts_c),
        if ac_current_a: 
            current_ac_a = float(ac_current_a), 
        if ac_current_b: 
            current_ac_b = float(ac_current_b),
        if ac_current_c: 
            current_ac_c = float(ac_current_c),
        if dc_volts: 
            voltage_dc = float(dc_volts),   
        if dc_current: 
            current_dc = float(dc_current), 
        if kw_out: 
            kwout = float(kw_out),
        if mwh_total: 
            mwh = float(mwh_total),
        recordTime  = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
    i_data.save()
except Exception, e:
    print >> sys.stderr, "Error While Recording Inverter History"
    print >> sys.stderr, e
    raise Http404

I am fairly new to python and don’t know how to properly code the above. When I try to compile the code I am getting an error where the if statements start. How can i fix the above code so it will compile and work as desired?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:43:30+00:00Added an answer on June 7, 2026 at 12:43 pm

    When you write i_data = InverterHistory(...), you are creating an object of type InverterHistory and the ... are the parameters you pass to the constructor. Constructing an object in Python is like calling a function or a method and you cannot put logic inside the parenthesis. So, you are getting an error because it is incorrect syntax to put an if statement there.

    What you need to do is move your logic out of the parenthesis and put all your variables inside a Python dict. You can then pass that dict as parameter to your call to InverterHistory.

    params = {
        'inverter': i,
        'recordTime': datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
    }
    if ac_volts_a:
        params['voltage_ac_a'] = float(ac_volts_a)
    if ac_volts_b: 
        params['voltage_ac_b'] = float(ac_volts_b)
    
    # Do the same with all your attributes
    # ...
    
    # Then you can create your object and pass it the dictionary you have created
    i_data = InverterHistory(**params)
    

    The **params unpacks the dictionary. This means that the interpreter will take all the key: value from the dictionary and turn them into key=value. This way, each param for which the if statement was True will be passed as a named parameter to InverterHistory

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse the payload from different push notification, but if user press
I would like to know the payload type, from the list that appears in
Launch app from homescreen can't get remote push payload? But launch from Notification Center
I'm implementing a Web service that returns a JSON-encoded payload. If the service call
I'm consuming an XML payload that looks something like this (for a more comprehensive
I have read about the true messaging and that instead of sending payload on
I am using .NET async send method (SendAsync) from Socket class. Do I need
How to get payload information from pdu with logica SMPP?
How does the 'wipeout data' command/payload executes on a lost device and powered off.
I'm trying to extract tcp payload from a packet , and here's a minimal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.