Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python: for and if loop
#1
I made this function to code some values according to func_code value but it does not work as aaspected:

  def reg_val_list_binary2(s):
    r = []
    for i in func_code(s):
        try: 
            if  i == 3:
                r = reg_val_list3(s)
            elif  i == 1: 
                r = reg_val_list_binary(s)
        except AttributeError:
            pass
    return r
Finction reg_val_list3(s) is coded as follows:

   def reg_val_list3(s):
    v = []
    for p in s:
        if p.haslayer('ModbusADUResponse'):
            try:
                if p['ModbusADUResponse'][1].funcCode == 3:
                    v += p['ModbusADUResponse'][1].registerVal
            except AttributeError:
                pass
    return v
And function reg_val_list_binary(s):

   def reg_val_list_binary(s):
    r = []
    for x in reg_val_list1(s):
        r += [int(bit)for bit in str( bin(x) )[2:].zfill(8)] [::-1]
    return r
The problem is I have the impression that it cannot find func_code == 1 while it's in the func_code(s) list

Any idea?
Reply
#2
What is func_codes(s)? Are you sure it contains 1?

One problem you may be having is reg_val_list_binary2 has a loop but it only returns the last value. If there is more than one func_code in func_codes(s) you are only getting the results using the last func_code.
Reply
#3
How to ask Smart Questions
What to include in a post
Reply
#4
@deanhystad : I think that you are right about my problem with the binary2 function. Its the loop which causes problem.

How can I resolve this problem plz ?

Func_code contains certainely 3 and 1 values. He is its code:

    
   def func_code(s):
    r = []
    for p in s:
        if p.haslayer('ModbusADURequest'):
            try:
                if p['ModbusADURequest'][1].funcCode == 3:
                    r.append(3)
                elif p['ModbusADURequest'][1].funcCode == 1:
                    r.append(1)
            except AttributeError:
                pass
Reply
#5
How do you want it resolved? Should func_code only return one func code, or should reg_val_list_binary return multiple values. I don't know because I have no idea what it is you are trying to accomplish. One thing I can say for sure; Your function names are terrible! When you have to add a version number to differentiate functions it is a clear sign that one of more of the functions aren't doing anything useful and the code needs to be reorganized.

I think it may be time to step back and write down what it is you want to do, not how you are going to code it. Maybe you ran into a coding problem and that resulted in some confusion that made you lose focus on what you are trying to accomplish. Or maybe your goals were never clearly stated in the first place. Sometimes I don't know where I'm going to end up when I start a project and have to thrash around a bit to discover what my requirements really are. Either way you will not succeed until you have a clear description of what your program needs to do.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020