Friday, May 22, 2015

Creating python executables

I needed to create python executable so that people wont have to rely on my python version.
So these are the steps:
# Install "cx_Freeze"
# Create setup.py
$catsetup.py 
from cx_Freeze import setup, Executable
build_exe_options = {
"includes": ['numpy', 'pandas','matplotlib.backends.backend_qt4agg'],
"packages": [],
"excludes": ['tk','ttk','zmq','boto','tkinter','_gtkagg', '_tkagg', 'bsddb', 'curses', 'pywin.debugger','pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl', 'Tkconstants', 'Tkinter'],
"include_files": []}

setup(
    name = "appName",
    version = "0.1",
    description = "",
    author = "Dengar",
    options = {"build_exe": build_exe_options},
    executables = [Executable("sectorize.py")]
)
#python setup.py build

Tuesday, May 19, 2015

Alt+Tab not working in remote desktop of citrix receiver on Linux mint 17

I use citrix to connect to office computer. But Alt+Tab wasn't seem to be working when I used remote desktop. I found few solution about HKEY edit solution for windows, but none for linux mint. I did some trial and error and finally found a solution myself.

I found this file in installation directory of ICAClient:

[17:43:43 harsshal@lenovo:~]$ ll /opt/Citrix/ICAClient/config/All_Regions.ini

lrwxrwxrwx 1 root root 37 Nov 8 2013 /opt/Citrix/ICAClient/config/All_Regions.ini -> /etc/icaclient/config/All_Regions.ini
Went to link and started edited this file

[17:44:08 harsshal@lenovo:~]$ sudo vim /etc/icaclient/config/All_Regions.ini

Search for "TransparentKeyPassthrough" in "Virtual Channels" section and add "Remote" in front of it.

[Virtual Channels\Keyboard]
TransparentKeyPassthrough=Remote

Save and restart Remote Desktop Connection

Wednesday, April 8, 2015

Google CodeJam in python templates

I recently started learning python and designed these programs.

Following program is for http://code.google.com/codejam/contest/351101/dashboard#s=p2


 # -*- coding: utf-8 -*-
"""
Created on Tue Apr  7 15:40:09 2015

@author: hpatil
"""

import sys

def read_file( input_file, output_file):
    sys.stdin = open(input_file)
    if(output_file != '' ):
        sys.stdout = open(output_file,"w+")
    read_input()

def solver(line):
    last_digit=10
    for i in line:
        #print(i)
        #print(len(line))
        digit, mystring = digit2string(i)
        if(last_digit==digit):
            print(' ',end='')
        print(mystring,end='')
        last_digit=digit
    print()
    return

def digit2string(char):
    dial = [
        [' '],
        [],
        ['a','b','c'],
        ['d','e','f'],
        ['g','h','i'],
        ['j','k','l'],
        ['m','n','o'],
        ['p','q','r','s'],
        ['t','u','v'],
        ['w','x','y','z'],
    ]
    mystring = ""  
    for digit in range(len(dial)):
        for repeat in range(len(dial[digit])):
            if(char==dial[digit][repeat]):
                for times in range(repeat+1):
                    mystring+=str(digit)
                return digit,mystring
  
  
def read_input():  
    with sys.stdin as file:
        testCases = int(file.readline())
        for test in range(testCases):
            print("Case #{}: ".format(test+1), end="")
            line = file.readline().strip('\n')
            solver(line)

#read_file('input','')
read_input()


This one is for with integer reading for http://code.google.com/codejam/contest/351101/dashboard


# -*- coding: utf-8 -*-
__author__ = 'hpatil'

import sys

def show_graph(x,y):
    import matplotlib.pyplot as plt

    plt.plot (x, y)
    plt.grid(True)
    plt.show()

def solver(credit, nItems, items):
    for i in range(nItems):
        for j in range(i+1,nItems):
            sum = items[i] + items[j]
            if( sum == credit):
                print(i+1,j+1)
                return

def set_io( input_file, output_file):
    sys.stdin = open(input_file)
    sys.stdout = open(output_file,"w")
   
def read_input():   
    with sys.stdin as file:
        testCases = int(file.readline())
        for test in range(testCases):
            print("Case #{}: ".format(test+1), end="")
            credit = int(file.readline())
            nItems = int(file.readline())
            items = list(map(int,file.readline().split()))
            show_graph(range(len(items)), items)
            solver(credit, nItems, items)

set_io('input','out')
read_input()

Following program is for http://code.google.com/codejam/contest/3214486/dashboard which took me a loooooong time

# -*- coding: utf-8 -*-
"""
Created on Wed Apr  8 13:08:15 2015

@author: hpatil
Solution to problem : http://code.google.com/codejam/contest/3214486/dashboard

"""

lit = [
        #['abcdefg']
        '1111110',
        '0110000',
        '1101101',
        '1111001',
        '0110011',
        '1011011',
        '1011111',
        '1110000',
        '1111111',
        '1111011'
    ]


def display_calci(string):
    if(string=="ERROR!"):
        print(string)
    else:
        display = list("         ")
        print(''.join(display))
        # line 1
        if(string[5] =='1'):
            display[0]='|'
        if(string[0] =='1'):
            display[1]='^'
        if(string[6] =='1'):
            display[2]='_'
        if(string[1] =='1'):
            display[3]='|'
        display[4]='\n'
        if(string[4] =='1'):
            display[5]='|'
        if(string[3] =='1'):
            display[6]='_'
            display[7]='_'
        if(string[2] =='1'):
            display[8]='|'
        print(''.join(display))

def isDigitFeasible(active, disp_digit, digit):
    for led in range(7):
        if(active[led]=='1'):
            if( disp_digit[led] != lit[digit][led]):
                return 0
    return 1

def get_active_leds(active, disp_digits):
    active= list(active)
    count = int(disp_digits[0])
    for digit in range(1,count+1):
        for led in range(len(disp_digits[digit])):
            if(disp_digits[digit][led]=='1' ):
                active[led]='1'
    return ''.join(active)

def led_unused(active, disp_digits, possible_digit):
    not_used = list('1111111')
    active= list(active)
    count = int(disp_digits[0])
    for digit in range(1,count+1):
        possible_digit= (possible_digit+1)%10
        for led in range(len(lit[possible_digit])):
            if(lit[possible_digit][led]=='1' ):
                not_used[led]='0'
    return ''.join(not_used)

def print_digit(active, disp_digits, digit):
    result_string = ""
    unused = led_unused(active, disp_digits, digit)
    for led in range(len(active)):
        if(active[led]=='1'):
            result_string += lit[digit][led]
        else:
            if(lit[digit][led] == '1'):
                if(unused[led]=='1'):
                    return "ERROR!"
                else:
                    result_string += '0'
            else:
                result_string += '0'

    return result_string

def get_possible_digits(active, disp_digit, possible):
    possible_return = [ ]
    for digit in possible:
        if(isDigitFeasible(active, disp_digit, digit)):
            possible_return.append(digit)
    return possible_return

def solver(disp_digits):
    active= list('0000000')
    active = get_active_leds(active,disp_digits)
    if(debug):
        display_calci(active)
    count = int(disp_digits[0])
    possible = [9,8,7,6,5,4,3,2,1,0]
    for digit in range(1,count+1):
        possible = get_possible_digits(active,disp_digits[digit], possible)
        possible = list(map(lambda x: (x-1) % 10, possible))
    if(debug):
        print(possible)
    possible_soln = list(map(lambda x: print_digit(active,disp_digits,x), possible))
    if(debug):
        print(possible_soln)
    soln_set = set(possible_soln)
    if(len(soln_set) != 1):
        return "ERROR!"
    else:
        return list(soln_set)[0]

def read_input():
    with sys.stdin as file:
        nTests = int(file.readline())
        for tests in range (1, nTests+1):
            print("Case #" + str(tests)+": ", end="")
            disp_digits = file.readline().split('\n')[0].split(' ')
            if(debug):
                print(disp_digits[1:])
                list(map(lambda x: display_calci(x), disp_digits[1:]))
                print("-----------")
            answer_string = solver(disp_digits)
            if(debug):
                display_calci(answer_string)
            print(answer_string)

import sys
debug = int(sys.argv[1])
sys.stdin = open (sys.argv[2])
#sys.stdin = open ('input2')
#sys.stdin = open ('A-small-practice.in')
#sys.stdin = open ('A-large-practice.in')

read_input()

Friday, March 6, 2015

Wake on by USB devices on Linux mint 17

I am back after a looooooong time... after my firm started supporting RDP from Linux again. I tried Linux mint 17 on my old laptop and attached external Samsung monitor to it. I stopped using monitor of laptop completely. So wanted laptop to go to sleep and wake up without actual power buttons on it.

I realized that Linux mint doesn't support wake on USB mouse / keyboard out of the box. so I created following file and it started supporting wake on USB devices again.


[01:35:11 harsshal@dell:~]$ cat /etc/udev/rules.d/90-keyboardwakeup.rules #idVendor and idProduct taken from 'lsusb' SUBSYSTEM=="usb", ATTRS{idVendor}=="0461", ATTRS{idProduct}=="4d15" RUN+="/bin/sh -c 'echo enabled > /sys$env{DEVPATH}/../power/wakeup'" SUBSYSTEM=="usb", ATTRS{idVendor}=="413c", ATTRS{idProduct}=="2002" RUN+="/bin/sh -c 'echo enabled > /sys$env{DEVPATH}/../power/wakeup'" SUBSYSTEM=="usb", ATTRS{idVendor}=="413c", ATTRS{idProduct}=="1002" RUN+="/bin/sh -c 'echo enabled > /sys$env{DEVPATH}/../power/wakeup'"

PS: When I rebooted machine, for some reason, my idProduct was changed. So if these don't work after reboot, make sure numbers are correct using "lsusb" again.