<commit_before>import os
import psutil
import time

def cpu_usage():
    CPU_Pct = str(round(float(
        os.popen('''grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage }' ''').readline()),
                        2))

    # print results
    print("CPU Usage = " + CPU_Pct)
    return CPU_Pct

def mem_usage():
    mem = str(os.popen('free -t -m').readlines())
    """
    Get a whole line of memory output, it will be something like below
    ['             total       used       free     shared    buffers     cached\n', 
    'Mem:           925        591        334         14         30        355\n', 
    '-/+ buffers/cache:        205        719\n', 
    'Swap:           99          0         99\n', 
    'Total:        1025        591        434\n']
     So, we need total memory, usage and free memory.
     We should find the index of capital T which is unique at this string
    """
    T_ind = mem.index('T')
    """
    Than, we can recreate the string with this information. After T we have,
    "Total:        " which has 14 characters, so we can start from index of T +14
    and last 4 characters are also not necessary.
    We can create a new sub-string using this information
    """
    mem_G = mem[T_ind + 14:-4]
    """
    The result will be like
    1025        603        422
    we need to find first index of the first space, and we can start our substring
    from from 0 to this index number, this will give us the string of total memory
    """
    S1_ind = mem_G.index(' ')
    mem_T = mem_G[0:S1_ind]
    """
    Similarly we will create a new sub-string, which will start at the second value. 
    The resulting string will be like
    603        422
    Again, we should find the index of first space and than the 
    take the Used Memory and Free memory.
    """
    mem_G1 = mem_G[S1_ind + 8:]
    S2_ind = mem_G1.index(' ')
    mem_U = mem_G1[0:S2_ind]

    mem_F = mem_G1[S2_ind + 8:]
    print("Info: \n", mem)
    print('Summary = ' + mem_G)
    print('Total Memory = ' + mem_T + ' MB')
    print('Used Memory = ' + mem_U + ' MB')
    print('Free Memory = ' + mem_F + ' MB')
    tot_m, used_m, free_m = map(str, os.popen('free -t -m').readlines()[-1].split()[1:])
    print("MemV2")
    print('Total Memory = ' + tot_m + ' MB')
    print('Used Memory = ' + used_m + ' MB')
    print('Free Memory = ' + free_m + ' MB')

start_time = time.time()
#cpu_usage()
#mem_usage()
print('Total Time1', str(time.strftime("%Hh%Mm%Ss", time.gmtime((time.time() - start_time)))))

start_time = time.time()
print(psutil.cpu_percent(interval=1))
print('Total Time2', str(time.strftime("%Hh%Mm%Ss", time.gmtime((time.time() - start_time)))))

start_time = time.time()
print(psutil.cpu_percent(interval=1, percpu=True))
print('Total Time3', str(time.strftime("%Hh%Mm%Ss", time.gmtime((time.time() - start_time)))))

start_time = time.time()
print(psutil.cpu_count())
print(psutil.cpu_freq())
print(psutil.cpu_stats())
print(psutil.cpu_times())
print(psutil.cpu_times_percent())
print(psutil.virtual_memory())
print('Total Time4', str(time.strftime("%Hh%Mm%Ss", time.gmtime((time.time() - start_time)))))<commit_msg>Show CPU and RAM info on Raspberry 2<commit_after>import os
import psutil
import time

def cpu_usage():
    CPU_Pct = str(round(float(
        os.popen('''grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage }' ''').readline()),
                        2))

    # print results
    print("CPU Usage = " + CPU_Pct)
    return CPU_Pct

def mem_usage():
    mem = str(os.popen('free -t -m').readlines())
    """
    Get a whole line of memory output, it will be something like below
    ['             total       used       free     shared    buffers     cached\n', 
    'Mem:           925        591        334         14         30        355\n', 
    '-/+ buffers/cache:        205        719\n', 
    'Swap:           99          0         99\n', 
    'Total:        1025        591        434\n']
     So, we need total memory, usage and free memory.
     We should find the index of capital T which is unique at this string
    """
    T_ind = mem.index('T')
    """
    Than, we can recreate the string with this information. After T we have,
    "Total:        " which has 14 characters, so we can start from index of T +14
    and last 4 characters are also not necessary.
    We can create a new sub-string using this information
    """
    mem_G = mem[T_ind + 14:-4]
    """
    The result will be like
    1025        603        422
    we need to find first index of the first space, and we can start our substring
    from from 0 to this index number, this will give us the string of total memory
    """
    S1_ind = mem_G.index(' ')
    mem_T = mem_G[0:S1_ind]
    """
    Similarly we will create a new sub-string, which will start at the second value. 
    The resulting string will be like
    603        422
    Again, we should find the index of first space and than the 
    take the Used Memory and Free memory.
    """
    mem_G1 = mem_G[S1_ind + 8:]
    S2_ind = mem_G1.index(' ')
    mem_U = mem_G1[0:S2_ind]

    mem_F = mem_G1[S2_ind + 8:]
    print("Info: \n", mem)
    print('Summary = ' + mem_G)
    print('Total Memory = ' + mem_T + ' MB')
    print('Used Memory = ' + mem_U + ' MB')
    print('Free Memory = ' + mem_F + ' MB')
    tot_m, used_m, free_m = map(str, os.popen('free -t -m').readlines()[-1].split()[1:])
    print("MemV2")
    print('Total Memory = ' + tot_m + ' MB')
    print('Used Memory = ' + used_m + ' MB')
    print('Free Memory = ' + free_m + ' MB')

total_time = time.time()

start_time = time.time()
#cpu_usage()
#mem_usage()
print('Total Time1', str(time.strftime("%Hh%Mm%Ss", time.gmtime((time.time() - start_time)))))

start_time = time.time()
print(psutil.cpu_percent())
print('Total Time normal CPU', str(time.strftime("%Hh%Mm%Ss", time.gmtime((time.time() - start_time)))))

start_time = time.time()
print(psutil.cpu_percent(interval=1))
print('Total Time2', str(time.strftime("%Hh%Mm%Ss", time.gmtime((time.time() - start_time)))))

start_time = time.time()
print(psutil.cpu_percent(interval=1, percpu=True))
print('Total Time3', str(time.strftime("%Hh%Mm%Ss", time.gmtime((time.time() - start_time)))))

start_time = time.time()
print(psutil.cpu_count())
print(psutil.cpu_freq())
print(psutil.cpu_stats())
print(psutil.cpu_times())
print(psutil.cpu_times_percent())
print(psutil.virtual_memory())
print('Total Time4', str(time.strftime("%Hh%Mm%Ss", time.gmtime((time.time() - start_time)))))

print('Total FINAL Time', str(time.strftime("%Hh%Mm%Ss", time.gmtime((time.time() - total_time)))))

from subprocess import PIPE, Popen
# import RPi.GPIO as GPIO Not necessary for now
def get_cpu_temperature():
    """get cpu temperature using vcgencmd"""
    process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
    output, _error = process.communicate()
    return float(output[output.index('=') + 1:output.rindex("'")])
print("Temperature is:")
print(str(get_cpu_temperature()))


{'fraction_non_alphanumeric': 0.14585996307342708, 'fraction_numerical': 0.02698480329498651, 'mean_word_length': 2.786021505376344, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3369971', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2360, 'n_words': 683}