Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, January 7, 2013

Sublime Text - Evil MS Word Char Replace Plugin

Tired of all those "smart quotes" and other proprietary special chars you get from office docs you have to publish? I for one don't like having to weed out all the black diamonds with question marks that sometimes show in WordPress and other CMS frameworks when you copy and paste content writers have given you from Microsoft Word documents so I wrote a plugin for Sublime Text to do a mass search and replace. Feel free to set it to your own macro and add to the list of evil chars, I've left the macro code absent for brevity's sake, read the docs or checkout a currently installed plugin with macros to get the syntax.


[sourcecode language="python"]
# -*- coding: utf-8 -*-
import sublime, sublime_plugin


class NormalizeUnicodePunctuationCommand(sublime_plugin.TextCommand):

def run(self, edit):
for region in self.view.sel():
content = self.view.substr(region)

content = content.replace(u'“', '"')
content = content.replace(u'”', '"')
content = content.replace(u'“', '"')
content = content.replace(u'‹', '"')
content = content.replace(u'›', '"')
content = content.replace(u'«', '"')
content = content.replace(u'»', '"')
content = content.replace(u'»', '"')
content = content.replace(u'‘', "'")
content = content.replace(u'’', "'")
content = content.replace(u'–', '-')
content = content.replace(u'…', '...')

self.view.replace(edit, region, content)
[/sourcecode]

Monday, December 24, 2012

Python Terminal Snow

Got this cool script over at Sontek's gist account. Here's the script:
[sourcecode language="python"]
#!/usr/bin/env python
import os
import random
import time
import platform

snowflakes = {}

try:
# Windows Support
from colorama import init
init()
except ImportError:
pass

def get_terminal_size():
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
import struct
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return None
return cr

cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)

if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (os.environ['LINES'], os.environ['COLUMNS'])
except:
cr = (25, 80)
return int(cr[1]), int(cr[0])

columns, rows = get_terminal_size()

def clear_screen(numlines=100):
"""Clear the console.

numlines is an optional argument used only as a fall-back.
"""
if os.name == "posix":
# Unix/Linux/MacOS/BSD/etc
os.system('clear')
elif os.name in ("nt", "dos", "ce"):
# DOS/Windows
os.system('cls')
else:
# Fallback for other operating systems.
print('\n' * rows)

def get_random_flake():
if not platform.system() == 'Windows':
try:
# python3 support
try:
cmd = unichr
except NameError:
cmd = chr

flake = cmd(random.choice(range(0x2740, 0x2749)))

return flake
except:
pass

return " *"

def move_flake(col):
if snowflakes[col][0]+1 == rows:
snowflakes[col] = [1, get_random_flake()]
else:
print("\033[%s;%sH " % (snowflakes[col][0], col))

snowflakes[col][0] += 1

print("\033[%s;%sH%s" % (snowflakes[col][0], col, snowflakes[col][1]))

print("\033[1;1H")

if __name__ == "__main__":

clear_screen()

while True:
col = random.choice(range(1, int(columns)))

# its already on the screen, move it
if col in snowflakes.keys():
move_flake(col)
else:
# otherwise put it on the screen
flake = get_random_flake()
snowflakes[col] = [1, flake]

print("\033[%s;%sH%s" % (snowflakes[col][0], col,
snowflakes[col][1]))

# key any flakes on the screen moving
for flake in snowflakes.keys():
move_flake(flake)

time.sleep(0.1)
[/sourcecode]

And a short clip of it in action, Happy Holidays [:

http://youtu.be/z4kWzOHyk_g