<commit_before>#!/usr/bin/python

# ----------------------------------------------------------------
# drawtree: a tree-drawing program 
# It reads an S-expression from stdin then draws a tree to a
# specified picture file
#
# Example usage:
# 	cat sexp | ./drawtree.py output.png
#
# If you end up with a big image you can use programs like
# "Eye of GNOME" to scroll and zoom around them.
#
# project started by blockeduser, october 2013
# ----------------------------------------------------------------

# TODO: maybe latex or troff output would be nice if that's
#       not too hard

import pygame

import sys
import copy
import time
import math
import sexp_reader

from pygame.locals import *

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

# "t" is the tree
# encoding: child 0 is node, child 1 is left child,
#           child 2 is right child; recursively

# test tree 
t = [1, [2, 3, 4, 5], 3, [4, 5, 6, 7], [5, 1, [2, 123], 3, [4, [5, 6, 7], 8]]]

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

# =============== Tree routines ===============

# is it a tree node ?
def check(tn):
	return (type(tn) == list and len(tn)) or type(tn) == int

# get the headvalue
def head(tn):
	if type(tn) == list:
		return tn[0]
	else:
		return tn

# figure out height
def height(tn):
	l = [0]
	if type(tn) == list:
		for i in range(len(tn) - 1):
			l.append(height(tn[i + 1]))
	return 1 + max(l)

# get all the stuff that's at a given depth
def listd(tn, d):
	def listd_iter(tn, c, d, l):
		if c == d:
			l.append(tn)
		if c < d:
			for i in range(len(tn) - 1):
				listd_iter(tn[i+1], c + 1, d, l)
	l = []
	listd_iter(tn, 0, d, l)
	return l

# estimate required height in pixels
def est_height(tn):
	return height(tn) * 20

# estimate required width in pixels
def est_width(tn):
	l = []
	# for each depth-level,
	for d in range(height(tn)):
		# estimate reasonable width at this depth
		tot_text_pixels = 0
		stuff = listd(tn, d)
		for k in stuff:
			if type(k[0]) == str:
				tot_text_pixels += 15 * len(k[0])
		tot_text_pixels += 40
		w = max(tot_text_pixels, 40 * len(stuff))
		l.append(w)
	# choose the largest depth-width
	return max(l)

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

# deal with cli args
make_square = False
if "--square" in sys.argv:
	make_square = True
	sys.argv.remove("--square")

# get output file
if len(sys.argv) < 2:
	print "use: %s output.{png,bmp,tga,jpg}" % (sys.argv[0])
	print "N.B.: Input is an s-expression that goes into stdin"
	sys.exit(1)
outf = sys.argv[1]

# read input S-expression from one line of stdin
t = sexp_reader.sexp2list(raw_input())

# default video dimensions
w = 640
h = 480

# make the dimensions bigger if necessary
w = max(w, est_width(t))
h = max(h, est_height(t))

# make it square ?
if make_square:
	w = max(w, h)
	h = w

pygame.init()		# mandatory

# get a window
win = pygame.Surface((w, h))
font = pygame.font.Font(pygame.font.match_font(pygame.font.get_fonts()[0]), 15)
pygame.display.set_caption('tree')

# setup some colors for further use
black = pygame.Color(0, 0, 0)
blue = pygame.Color(0, 0, 255)
red = pygame.Color(255, 0, 0)
white = pygame.Color(255, 255, 255)

# routine that automatizes pygame text-rendering ugliness
def write_text(txt, x, y):
	text = font.render(txt, False, black)
	rect = text.get_rect()
	rect.topleft = (x, y)
	win.blit(text, rect)
<commit_msg>Make the program 3x faster (pygame is silly)<commit_after>#!/usr/bin/python

# ----------------------------------------------------------------
# drawtree: a tree-drawing program 
# It reads an S-expression from stdin then draws a tree to a
# specified picture file
#
# Example usage:
# 	cat sexp | ./drawtree.py output.png
#
# If you end up with a big image you can use programs like
# "Eye of GNOME" to scroll and zoom around them.
#
# project started by blockeduser, october 2013
# ----------------------------------------------------------------

# TODO: maybe latex or troff output would be nice if that's
#       not too hard

import pygame

import sys
import copy
import time
import math
import sexp_reader

from pygame.locals import *

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

# "t" is the tree
# encoding: child 0 is node, child 1 is left child,
#           child 2 is right child; recursively

# test tree 
t = [1, [2, 3, 4, 5], 3, [4, 5, 6, 7], [5, 1, [2, 123], 3, [4, [5, 6, 7], 8]]]

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

# =============== Tree routines ===============

# is it a tree node ?
def check(tn):
	return (type(tn) == list and len(tn)) or type(tn) == int

# get the headvalue
def head(tn):
	if type(tn) == list:
		return tn[0]
	else:
		return tn

# figure out height
def height(tn):
	l = [0]
	if type(tn) == list:
		for i in range(len(tn) - 1):
			l.append(height(tn[i + 1]))
	return 1 + max(l)

# get all the stuff that's at a given depth
def listd(tn, d):
	def listd_iter(tn, c, d, l):
		if c == d:
			l.append(tn)
		if c < d:
			for i in range(len(tn) - 1):
				listd_iter(tn[i+1], c + 1, d, l)
	l = []
	listd_iter(tn, 0, d, l)
	return l

# estimate required height in pixels
def est_height(tn):
	return height(tn) * 20

# estimate required width in pixels
def est_width(tn):
	l = []
	# for each depth-level,
	for d in range(height(tn)):
		# estimate reasonable width at this depth
		tot_text_pixels = 0
		stuff = listd(tn, d)
		for k in stuff:
			if type(k[0]) == str:
				tot_text_pixels += 15 * len(k[0])
		tot_text_pixels += 40
		w = max(tot_text_pixels, 40 * len(stuff))
		l.append(w)
	# choose the largest depth-width
	return max(l)

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

# deal with cli args
make_square = False
if "--square" in sys.argv:
	make_square = True
	sys.argv.remove("--square")

# get output file
if len(sys.argv) < 2:
	print "use: %s output.{png,bmp,tga,jpg}" % (sys.argv[0])
	print "N.B.: Input is an s-expression that goes into stdin"
	sys.exit(1)
outf = sys.argv[1]

# read input S-expression from one line of stdin
t = sexp_reader.sexp2list(raw_input())

# default video dimensions
w = 640
h = 480

# make the dimensions bigger if necessary
w = max(w, est_width(t))
h = max(h, est_height(t))

# make it square ?
if make_square:
	w = max(w, h)
	h = w

# pygame.init() initializes all the SDL modules and
# makes the quit routine super slow (like 1 full second to exit !!)
# apparently it's the audio module's fault
pygame.display.init()
pygame.font.init()

# get a window
win = pygame.Surface((w, h))
font = pygame.font.Font(pygame.font.match_font(pygame.font.get_fonts()[0]), 15)
pygame.display.set_caption('tree')

# setup some colors for further use
black = pygame.Color(0, 0, 0)
blue = pygame.Color(0, 0, 255)
red = pygame.Color(255, 0, 0)
white = pygame.Color(255, 255, 255)

# routine that automatizes pygame text-rendering ugliness
def write_text(txt, x, y):
	text = font.render(txt, False, black)
	rect = text.get_rect()
	rect.topleft = (x, y)
	win.blit(text, rect)

{'fraction_non_alphanumeric': 0.2231404958677686, 'fraction_numerical': 0.024508406953548018, 'mean_word_length': 3.2616879174256224, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2581223', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2443, 'n_words': 846}