#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Help in python code


    Hi,

    I am working on python code to parse kernel log,

    Code:
    def __current_time(c): return float(c['kernel_time_stamp'])
    Code:
    def __init_cur(): cur_state = dict() cur_state['kernel_time_stamp'] = ''  
    return cur_state KERNEL_TIME_LIMIT = 131072.0 KERNEL_TIME_STAMP = re.compile(r'(\d{2}):(\d{2}):(\d{2}) (\d{3}) (.*)') LOGCAT_TIME_STAMP =\ re.compile('^(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d{3}) (.*)') 
    def time_and_body(line): n = KERNEL_TIME_STAMP.search(line) 
    if n: groups = n.groups() return '{}.{}.{}.{}'.format(*groups[:4]), groups[4] return (None, None)


    after I call,

    cur_state['kernel_time_stamp'] = ''

    Here,

    def __current_time(c): return float(c['kernel_time_stamp'])

    Seeing this error,

    Traceback (most recent call last):
    File "C:\Bug2Go-scripts\pm_log_parser_new.py", line 1328, in <module>
    f = roll(fobj_in, fobj_out)
    File "C:\Bug2Go-scripts\pm_log_parser_new.py", line 937, in roll
    start_time = __current_time(cur_state))
    File "C:\Bug2Go-scripts\pm_log_parser_new.py", line 37, in __current_time
    return float(str(c['kernel_time_stamp']))
    ValueError: invalid literal for float(): None

    any help appreciated,
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    I can't tell from your un-wrapped code where things are getting done, but clearly c['kernel_time_stamp'] is still equal to '' (your initial setting) when you try to cast it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    k here is wrapped code,


    Code:
    # Log Processing Section Starts
    KERNEL_TIME_LIMIT = 131072.0
    
    KERNEL_TIME_STAMP = re.compile(r'(\d{2}):(\d{2}):(\d{2}) (\d{3}) (.*)')
        
    LOGCAT_TIME_STAMP =\
        re.compile('^(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d{3}) (.*)')
    def time_and_body(line):
         
        n = KERNEL_TIME_STAMP.search(line)
        if n:
            groups = n.groups()
            return '{}.{}.{}.{}'.format(*groups[:4]), groups[4]
        return (None, None)
    
    
    ##########################
    
    
    def __init_cur():
        cur_state = dict()
    
        cur_state['state'] = UNKNOWN
        cur_state['kernel_time_stamp'] = ''
        return cur_state
    
    ################################
    
    def __current_time(c):
        return float(c['kernel_time_stamp'])
    
    
    ###############################
    
    def roll(fobj_in, fobj_out):
        cur_state = __init_cur()
        live_sessions = dict()
        live_sessions['full'] = None
        live_sessions['discharge'] = None
        live_sessions['charge'] = None
        live_sessions['active'] = None
        live_sessions['suspend'] = None
        live_sessions['wakeup'] = None # started from wakeup_wakelock, ended 
        live_sessions['last_active'] = None
    
        try:
            l = fobj_in.readline()
          
            while (len(l)):
                t,b = time_and_body(l)
                if t is None:
                    cur_state['kernel_time_stamp'] = t
                    
                    if not live_sessions['full']:
                        live_sessions['full'] = FullLogSession(
                            start = t,
                            
                            start_time = __current_time(cur_state))
                        
                        live_sessions['discharge'] = DischargeSession(
                            start = t,
                            start_time = __current_time(cur_state))
                    for k in REGEX.keys():
                        r,f = REGEX[k]
                        m = r.match(b)
                        if m is not None:
                            if f in globals().keys():
                                hook = globals()[f]
                                hook(live_sessions, cur_state, m)
                l = fobj_in.readline()
            # close all live sessions
            __close_sessions(live_sessions, cur_state)
            live_sessions['full'].end = cur_state['kernel_time_stamp']
            live_sessions['full'].end_time = __current_time(cur_state)
            live_sessions['full'].duration = __duration(live_sessions['full'])\
                + live_sessions['full'].rtc_only
        except:
            __debug_print_all(live_sessions, cur_state)
            raise
        if not TECH and not VERBOSE:
            if cur_state['cpu1_on'] > 0:
                live_sessions['full'].cpu1_time += __current_time(cur_state) - cur_state['cpu1_on']
            if cur_state['charging_start'] > 0:
                live_sessions['full'].usb_time += __current_time(cur_state) - cur_state['charging_start']
        return live_sessions['full']

    This the code where I am getting above error...
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    I think the None comes from this business:
    Code:
                if t is None:
                    cur_state['kernel_time_stamp'] = t
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    string argument convert to float


    I have this string:

    cur_state['kernel_time_stamp']=18.23.12.234

    and when I write this function,
    Code:
    def __current_time(c):
        return float(c['kernel_time_stamp'])
    I cant convert this string into float because it have too many '.'..

    Is there any other solution I can convert string to number..

    its giving me below error if i use float,

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: invalid literal for float(): 18.23.12.234

    Help appreciated...
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    How, by hand, would you convert
    18.23.12.234
    to a number?

    Do you want a list of integers perhaps?
    list(map(int,'18.23.12.234'.split('.')))
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    got it..


    thanks for tips..


    I used,

    Code:
    def __current_time(c):
        return c['kernel_time_stamp']
    rather than converting into anything..coz i needed "18.23.12.234" this as a whole number..So, I guess I can that directly?

    without converting that into float or int?
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    help in reg-exp


    I have a string

    line =('02-28 15:09:30.738 0 0 I KERNEL : [ 1236.973264,0] **** active wakelocks ****')

    Code:
    KERNEL_TIME_STAMP = re.compile('^(.*)\[ *(\d+)\.(\d{6})[^\]]*\] (.*)')
        
    
    def time_and_body(line):
        print line 
        n = KERNEL_TIME_STAMP.search(line)
        if n is None:
            return (None, None)
        return (n.groups()[1]+'.'+n.groups()[2],n.groups()[3])
    can you help finding reg-exp for above string..

    I used "' re.compile('^(.*)\[ *(\d+)\.(\d{6})[^\]]*\] (.*)')'"

    its not working for me..

    want parse only this much portion of string "1236.973264"

    THanks for help
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    I would not use regular expressions.
    I would split the string at '[' and then split the second field at ',' and take the first field.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo