27 February, 2010 - 9:39 pm
Tags: caps lock, Java, programming, socket, upcase
Posted in english | No comments
Here is a simple server for those who are starting studying sockets or just needs a simple socket server example for reuse while writing your own behavior.
Features:
A client should enter a string and the server would answer the same string, with each symbol in up case, when possible.
Default port at 8080.
One client at time.
No multi [...]
22 February, 2010 - 5:16 pm
Tags: development, i18n, Java, JUnit, locale, programming, String, teste unitário, toString, unit testing
Posted in english | No comments
Today I was programming a toString method for a class widely used in a application, using the very useful String.format that provides a C’s like printf formatter.
@Override
public String toString() {
return String.format("VO[a: %.1f, b: %.1f, c: %.1f]", a, b, a+b);
}
%.1f means a float with one digit precision after the dot separator. The code [...]
O pre-requisito é o notify-send, um utilitário de linha de comando do libnotify. No Ubuntu:
sudo aptitude install libnotify-bin
E aqui o script em si:
sleep 5m; notify-send “aviso” “tirar o miojo do fogo”
Pronto, depois de cinco minutos isso vai aparecer:
20 January, 2010 - 10:55 pm
Tags: date, datetime, ISO 8601, Jochen Voss, Markus Kuhn, programming, Python, representation, sort, sorting, time
Posted in english | No comments
I was looking for a date and time representation useful for registering stock quotes in a simple plain file.
I found that the standard ISO 8601 is just the answer for this, it’s called “Data elements and interchange formats — Information interchange — Representation of dates and times”. Here is a example:
2010-01-20 22:14:38
There’s this good article from [...]
5 January, 2010 - 6:10 pm
Tags: AWT, fonts, Java, JVM, Larabie, Larabie Fonts, programming
Posted in english | Comments Off
Here’s a program that lists fonts available in your JVM. You can also set the environment variable JAVA_FONTS to specify the font directory.
import java.awt.GraphicsEnvironment;
public class ListFonts {
public static void main(String args[]){
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
for(String font:e.getAvailableFontFamilyNames()){
System.out.println(font);
}
}
}
By using pipes you can count how many fonts you have:
java ListFonts|wc -l
On my Ubuntu machine here I got 556 because [...]
25 December, 2009 - 3:04 pm
Tags: dtd, expat, game, programming, pygame, Python, sax, schema, urllib, XML
Posted in english | 1 comment
Here is a useful tip on Python XML decoding.
I was extending xml.sax.ContentHandler class in a example to decode maps for a Pygame application when my connection went down and I noticed that the program stop working raising a exception regarded a call to urlib (a module for retrieve resources by url). I noticed that the [...]
19 December, 2009 - 7:15 am
Tags: C, Java, Python, Qt, Tiled
Posted in english | 2 comments
I’m using the Tiled Map Editor for a while, I even wrote that tutorial about it. It’s a general purpose tile map editor, written in Java but now migrating to C++ with Qt, that can be easily used with my set of free pixelart tiles.
A map done with Tiled is stored in a file with [...]
11 December, 2009 - 7:47 am
Tags: game, orc, Pixelart, programming, pygame, Python
Posted in english | No comments
Here is a Pygame Sprite animation using the approach presented by Joe Wreschnig and Nicolas Crovatti. It’s not yet exactly what I need but is very suitable.
import pygame, random
from pygame.locals import *
class Char(pygame.sprite.Sprite):
x,y = (100,0)
def __init__(self, img, frames=1, modes=1, w=32, h=32, fps=3):
pygame.sprite.Sprite.__init__(self)
original_width, original_height = img.get_size()
self._w = w
self._h = h
self._framelist = []
for i in xrange(int(original_width/w)):
self._framelist.append(img.subsurface((i*w,0,w,h)))
self.image = [...]
This is a very simple example of how to open two images and display them added.
I got two pictures at project Commons from Wikimedia that were highlighted on Featured Pictures. I did a crop on both to have the same size, as I’m trying to make this example as simple as possible.
The first one is [...]
Here’s a simple video player that also performs facial detection thought the Open Computer Vision Library.
Here’s a code developed using codes from nashruddin.com and samples from OpenCV, including the haar classifier xml. More detailed explanation on the theory about how the OpenCV face detection algorithm works can be found here.
The code:
#include <highgui.h>
#include <stdio.h>
#include <cv.h>
CvHaarClassifierCascade *cascade;
CvMemStorage [...]
9 December, 2008 - 9:05 am
nice, i remember what is it now, heh.
where will you apply it??
9 December, 2008 - 11:49 am
sombriks, I also wrote a parallel version of gaussian elimination using MPI. My idea now is to benchmark them.
11 December, 2008 - 10:12 am
Hi, nice work. Just for language war, some time ago I’ve done a python version:
http://pastebin.com/f17960864
11 December, 2008 - 1:42 pm
Cool! I’ve pasted here:
import sys ############# # Buggy Gauss Elimination # (Zero division is not checked) # def det(rows): v = None if len(rows) == 2: r1 = rows[0] r2 = rows[1] v = r1[0] * r2[1] - r1[1] * r2[0] else: firstRow = rows[0] aboveRows = rows[1:] subDets = [] # At time I din't know the existence of enumerate for c in range(0, len(firstRow)): subMatrix = [] for ar in aboveRows: subRow = [] for c2 in range(0, len(ar)): if c != c2: subRow.append(ar[c2]) subMatrix.append(subRow) subDets.append(det(subMatrix) * firstRow[c]) evens = [subDets[e] for e in range(0, len(subDets), 2)] odds = [subDets[e] for e in range(1, len(subDets), 2)] v = reduce(lambda x, y: x+y, evens) - reduce(lambda x, y: x+y, odds) return v #non-recursive def solveSystem(rows): if det(rows) == 0: return None zerorColumnNth = 0 solvedSystem = rows for workRowNth in range(0, len(rows)-1): for prodRowNth in range(workRowNth+1, len(rows)): workRow = solvedSystem[workRowNth] prodRow = solvedSystem[prodRowNth] mul = -prodRow[zerorColumnNth] / workRow[zerorColumnNth] newProdRow = map(lambda a, b: a + b, map(lambda x: x*mul, workRow), prodRow) solvedSystem[prodRowNth] = newProdRow zerorColumnNth += 1 return solvedSystem #recursive def solveSystem2(rows): def solveLoop(rows, workNth, prodNth, zeroColumn, nrows): if prodNth < nrows: workRow = rows[workNth] prodRow = rows[prodNth] mul = -prodRow[zeroColumn] / workRow[zeroColumn] rows[prodNth] = map(lambda a, b: a + b, map(lambda x: x*mul, workRow), prodRow) return solveLoop(rows, workNth, prodNth+1, zeroColumn, nrows) else: if workNth = 0: row = rs[nth] vt = [vals[v]*row[v] for v in range(nth+1, len(row)-1)] if len(vt) > 0: coff = -reduce(lambda a, b: a+b, vt) else: coff = 0 vals[nth] = (coff + row[len(row)-1])/row[nth] return retroLoop(rs, nth-1, vals) return vals nrows = len(rows) return retroLoop(rows, nrows-1, [0 for i in range(0, nrows)]) if __name__ == '__main__': try: f = open(sys.argv[1], 'r') except IOError, msg: print(msg) sys.exit(1) rows = [] for line in f: if line != '\n': atoms = line.split(' ') r = [] for a in atoms: r.append(float(a)) rows.append(r) f.close(); maxColumn = 0 for r in rows: cs = len(r) if cs > maxColumn: maxColumn = cs for r in rows: cs = len(r) if cs != maxColumn: print("Matrix nao quad") sys.exit(1) ss = solveSystem2(rows) r = retroSub(ss) print(r) ############## # Sample input file: # # 10 2 3 4 5 # 6 17 8 9 10 # 11 12 23 14 15 # 16 17 18 29 20 # Should output: # [0.28248587570621464, 0.24858757062146891, 0.24293785310734467, 0.23728813559322035]22 December, 2008 - 2:32 pm
I’ll do just a observation: if A and B are matrixes, the operation B/A doesn’t exist. You have to do B*inv(A), in fact if C = inv(A) then C(i,j) = 1/A(i,j) because A is a digonal matrix.
Merry Christmas for everyone!!!
God will help us more in next year!
16 February, 2009 - 2:21 am
I’m not the only one who finds amusement in the irony of the C version being shorter and clearer than the Python one, am I?