[PYTHON] Run the Matrix to your boss's terminal!

Livesense Advent Caelendar 2015 Day 12 is @sion_cojp.

Looking back ...

I used to work as an infrastructure engineer, and when I was sick, I ran a train to my boss's terminal. Now I am moving to a department as a web application engineer and developing APIs.

You are looking at this article.

Do you have this kind of thing at work?


I changed departments, but after all my boss made an unreasonable request. Let's go. .. .. </ b>

I understand. That feeling. Yeah. ~~ I've moved so much, so I wish I could buy expensive sushi. .. ~~ But I have to thank you for being able to move. Society is complicated, isn't it?





Let's run the Matrix to the terminal where the boss is working! !! </ b>







First of all, preparation.


① Find the host your boss is working on

I'll be sick in some way. (Kimon) Again, I sneaked into my boss's monitor from behind.


I'm laid back and wrestling with PHP's legacy code.

Stupid. </ b>

Get the server information that your boss is developing.

② Write a program to run the matrix.

Make it properly. Let's hit MATRIX_ANGRY_CHARS with anger. As you can see by running the program, please be assured that the angry part is not to be bald </ b>.
Let's thank MATRIX_THANKS_CHARS.

matrix_curses.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import locale
import time
import curses
import sys, string
import random
import struct
locale.setlocale(locale.LC_ALL, '')
encoding = locale.getpreferredencoding()

########################################################################
# const

MIN_SPEED = 1
MAX_SPEED = 2
MATRIX_ANGRY_CHARS = "It's okay to chat instead of emailing as much as late contact I want to type in Hadoken I almost passed away"
MATRIX_THANKS_CHARS = "THANKS DEAR TAKU !!!"
MATRIX_COLOR_WORD = 46
MATRIX_COLOR_BACK = 232

########################################################################

class FallingChar(object):
    matrixchr = list(MATRIX_ANGRY_CHARS)
    normal_attr = curses.A_NORMAL
    highlight_attr = curses.A_REVERSE

    def __init__(self, width, MIN_SPEED, MAX_SPEED):
        self.x = 0
        self.y = 0
        self.speed = 1
        self.char = ' '
        self.reset(width, MIN_SPEED, MAX_SPEED)

    def reset(self, width, MIN_SPEED, MAX_SPEED):
        self.char = random.choice(FallingChar.matrixchr).encode(encoding)
        self.x = random.randint(1, width - 1)
        self.y = 0
        self.speed = random.randint(MIN_SPEED, MAX_SPEED)

    def tick(self, scr):
        height, width = scr.getmaxyx()
        self.out_of_screen_reset(width, height)
        scr.addstr(self.y, self.x, self.char, curses.color_pair(1))
        self.char = random.choice(FallingChar.matrixchr).encode(encoding)
        self.y += 1
        if not self.out_of_screen_reset(width, height):
            scr.addstr(self.y, self.x, self.char, curses.A_REVERSE)

    def out_of_screen_reset(self, width, height):
        if self.x > width-2:
            self.reset(width, MIN_SPEED, MAX_SPEED)
            return True
        if self.y > height-2:
            self.reset(width, MIN_SPEED, MAX_SPEED)
            return True
        return False

def rand():
    a = 1
    while True:
        a ^= (a << 1);
        a ^= (a >> 135);
        a ^= (a << 104);
        yield a

r = rand()
def randint(_min, _max):
    n = r.next()
    return (n % (_max - _min)) + _min

def color():
    curses.start_color()
    curses.init_pair(1, MATRIX_COLOR_WORD, MATRIX_COLOR_BACK)
    curses.curs_set(0)
    curses.noecho()

def main():
    scr = curses.initscr()
    scr.nodelay(1)
    color()

    height, width = scr.getmaxyx()
    lines = []
    for i in range(8):
        l = FallingChar(width, MIN_SPEED, MAX_SPEED)
        lines.append(l)

    scr.refresh()
    while True:
        for line in lines:
            line.tick(scr)
        for i in range(30):
            x = randint(0, width-1)
            y = randint(0, height-1)
            scr.addstr(y, x, ' ')

        scr.refresh()
        time.sleep(0.04)

def last_main():
    curses.endwin()
    curses.curs_set(1)
    curses.reset_shell_mode()
    curses.echo()

    screen = curses.initscr()
    color()
    lastchr = string.join(list(MATRIX_THANKS_CHARS), " ")

    last_height, last_width = screen.getmaxyx()
    start_width = last_width/2 - len(lastchr)/2
    for i in range(last_height/2):
        screen.clear()
        screen.addstr(i, start_width, lastchr, curses.color_pair(1))
        screen.refresh()
        time.sleep(0.2)
    screen.getch()
    curses.endwin()

try:
    main()
except KeyboardInterrupt:
    try:
        last_main()
    except KeyboardInterrupt:
        curses.endwin()
        curses.curs_set(1)
        curses.reset_shell_mode()
        curses.echo()

Let's run in the test

$ python /tmp/matrix_curses.py




Good. </ b>

④ Let's run the matrix to the terminal of the boss!

$ w

It turns out that my boss, taku, is / dev / pts / ○○. Command input immediately.

$ python /tmp/matrix_curses.py > /dev/pts/○○





mymovie1.gif

Then press ctrl + c when you're happy or your boss's anger is maxed out.

mymovie2.gif



THANKS DEAR TAKU!!!

Achieved. I'm really thankful to you. </ b>

⑤ Finally

Do not use it when it is important, such as when there is a failure. Absolutely. Give it to your humorous, gentle and gentle boss who never gets angry!

Recommended Posts