Giter VIP home page Giter VIP logo

python's Introduction

Python

[email protected]:Ty-Nickel/Python.git

import fcntl, termios, struct, os import random import time, sys

def ioctl_GWINSZ(fd): ''' Get terminal size, by giving the proper file descriptor ''' try: cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) except: return None return cr

def get_terminal_size(): ''' Get terminal size, or assume one '''

# Either stdin (0), stdout (1), stderr (2)
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)

# If not available in either of these
# obtain the proper file descriptor
if not cr:
    try:
        fd = os.open(os.ctermid(), os.O_RDONLY)
        cr = ioctl_GWINSZ(fd)
        os.close(fd)
    except:
        pass

# If still not available
# get the information from the environment, or assume a size
if not cr:
    try:
        cr = (env['LINES'], env['COLUMNS'])
    except:
        cr = (25, 80)

return int(cr[1]), int(cr[0])

def get_rand_num(): ''' Get 0, 1, or 2 randomly for the matrix ''' return random.randint(0, 2)

def get_line(): ''' Return a line with numbers that is as long as the width of the current terminal ''' n = 0 line = ''

width, height = get_terminal_size()

while n < width:
    width, height = get_terminal_size()
    line += str(get_rand_num())
    n += 1
return line

def matrix(): ''' Create a pseudo-matrix of binary numbers scrolling in one horizontal line. It is inspired in how the code looks like in the movie The Matrix ''' n = 0 t = 0.50 # Seconds after which the line refreshes

width, height = get_terminal_size()

# Refresh the line until a keyboard interrupt
while n < height:
    try:
        # Only '0' and '1', with random spaces in between
        binary_line = get_line().replace('2', ' ')
        
        # Write to the terminal,
        # returning the cursor to the beginning of the line
        sys.stdout.write('\033[1m\033[32m' + binary_line + '\033[0m' + '\r')
        sys.stdout.flush()
        time.sleep(t)
    except KeyboardInterrupt, EOFError:
        # Ctrl+C, Ctrl+D
        sys.stdout.write('\n' + '=== END ===' + '\n')
        sys.stdout.flush()
        time.sleep(1.0)
        break

matrix()

python's People

Contributors

ty-nickel avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.