Month: December 2019

  • Linux ‘cat’ in Python – almost complete

    Morning,

    Since I am striving to find useful content to post more often, I took homework for a ‘cat’ written in Python.

    It’s not elegant, and it’s not the best version but it works.

    # -*- coding: utf-8 -*-
    """
    Created on Wed Dec 25 10:28:39 2019
    @author: Sorin
    """
    import sys,getopt,os
    if os.path.isabs(sys.argv[-1:][0]):
        FILENAME= sys.argv[-1:][0]
    else:
        FILENAME = os.getcwd() + "\\" + sys.argv[-1:][0]
    
    def read_content(filename):
        try:
            f = open(filename, "r+")
            content = f.read()
            f.close()
        except IOError as e:
                print("File could not be opened:", e)
                sys.exit(3)
        return content
        
    def transform_content():
        content = read_content(FILENAME)
        content_list = content.split('\n')
        return content_list
    def number_nonempty_lines(content_list):
        i = 0
        for line in content_list:
            if line != '':
                content_list[i] = str(i) + " " + line
            i = i + 1
        return content_list
    def squeeze_blanks(content_list):   
        i = 0
        duplicate_index = []
        for line in content_list:
            if (line == "" or line == "$")  or (str.isdigit(line.split(' ')[0]) and (line.split(' ')[-1] == "" or line.split(' ')[-1] == "$")):
               duplicate_index.append(i+1)
            i = i + 1
        delete_index = []
        for j in range(len(duplicate_index) - 1):
            if  duplicate_index[j] + 1 == duplicate_index[j+1]:
                delete_index.append(duplicate_index[j])
        for element in delete_index:
            content_list.pop(element)
        return content_list    
            
    def number_all_lines(content_list):
        i = 0
        for line in content_list:
            content_list[i] = str(i) + " " + line
            i = i + 1
        return content_list
    
    def display_endline(content_list):
       return [line + "$" for line in content_list]
    
    def show_tabs(content_list):
        print(content_list)
        content_list = [ line.replace('\t','^I') for line in content_list]
        return content_list
    
    content_list =transform_content()
    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:-1], 'AbeEnstTv', ['show-all', 'number-nonblank', 'show-ends', 'number', 'show-blank', 'squeeze-blank' 'show-tabs', 'show-nonprinting', 'help', 'version'])
    except getopt.GetoptError:
         print("Something went wrong")
         sys.exit(2)
    for opt, arg in opts:
        if opt in ('-A','--show-all'):
            content_list = display_endline(content_list)
            content_list = show_tabs(content_list)
        elif opt in ('-b', '--number-nonblank'):
           content_list = number_nonempty_lines(content_list)
        elif opt in ('-n', '--number'):
           content_list = number_all_lines(content_list)
        elif opt in ('-E', '--show-ends'):
            content_list = display_endline(content_list)
        elif opt in ('-s', '--squeeze-blank'):
            content_list = squeeze_blanks(content_list)
        elif opt in ('-T', '--show-tabs'):
            content_list = show_tabs(content_list)
    print('\n'.join(content_list))

    Further improvements will be also posted. I must confess that there are still a couple of things to be fixed, like not running the same options twice, and the issue of putting it to work on very large files, but it will do in this form for now.

    Cheers

  • Reset Cinnamon desktop interface

    Hi,

    I recently had an issue with Cinnamon interface, more exactly, my menu panel dissapeared.

    After some quick searches on the net, I found this command:

    gsettings reset-recursively org.cinnamon

    It seems to do the trick.

    Cheers