Today is OO-MISOKA(大晦日): the last day of the year. OO means 'grand, large' and 'MISOKA' means the last day of the month. 'MISOKA' is consisted of 3 sounds 'MI'(three), 'SO'(ten) and 'KA'(day). 'MISOJI'(三十路) means 30 to 40 years old.
Why '30th day' had meant the last day, not 31th day? It is because the lunar calendar used in Japan before AD1873. The 31th day did not exist in those days.
-----
Today most of Japanese eat a noodle 'TOSHI-KOSHI-SOBA'(beyond the year noodle). The long noodles represent the long life.
Wednesday, December 31, 2008
Tuesday, March 11, 2008
NodeBox
I had attended to "Python Developer Camp 2008 Winter" held in Matsumoto(Japan) on the last weekend. I learned about NodeBox. NodeBox is a software on MacOSX. You can obtain it from NodeBox | Home.
Using NodeBox you can make images and movies from a small amount of python code. It is very fun. You can change the number literal by dragging it with command key. It is nice idea.
Using NodeBox you can make images and movies from a small amount of python code. It is very fun. You can change the number literal by dragging it with command key. It is nice idea.
from math import sin, cos
SIZE = 431
size(SIZE, SIZE)
x = 19
y = 324
th = 1
dth = 0.5
ddth = 0.1
s = 30
colormode(RGB, 1)
stroke(color(0.5, 1, 0.5, 0.3))
strokewidth(2)
fill(color(0.5, 1, 0.5, 0.7))
beginpath()
moveto(x, y)
for i in range(313):
x += sin(th) * s
y += cos(th) * s
th += dth
dth += ddth
lineto(x, y)
print x, y
endpath()
Tuesday, February 26, 2008
Why I cancelled today's meeting
Why did I cancel today's meeting (Asiajin Meeting Tokyo #1) ?
I'm sorry. I found it was double-booked with another paid event. The event is from 26 to 28 I thought.
And this morning I found it was wrong. The event is from 27 to 29. I should go to my company to change my application of leisure. My coworker, amachang, had suffering large stress from making ready a presentaiton in English. I feel very sorry.
I'll make sure I will speak on the second meeting. I promise.
I'm sorry. I found it was double-booked with another paid event. The event is from 26 to 28 I thought.
And this morning I found it was wrong. The event is from 27 to 29. I should go to my company to change my application of leisure. My coworker, amachang, had suffering large stress from making ready a presentaiton in English. I feel very sorry.
I'll make sure I will speak on the second meeting. I promise.
Friday, February 15, 2008
[Python]Draw a fractal pattern(Julia set)
I wrote a program to draw a fractal pattern.
Julia set - Wikipedia, the free encyclopedia.
I found it is very easy to implement; it took much time to check the mathematical definition of Julia set than to implement. If you already know Python and don't know Python Imaging Library (PIL) yet, I strongly recommend to learn it!
Using PIL you can generate images like above from very simple code:
Here is a movie(2MB, c = -0.78 + i * 0.002 + 0.23j (i < 40), center = -0.5 + 0.2j, scale = 1.0). I think this type of movie isn't suitable for MPEG compression, it is raw avi.
Julia set - Wikipedia, the free encyclopedia.
I found it is very easy to implement; it took much time to check the mathematical definition of Julia set than to implement. If you already know Python and don't know Python Imaging Library (PIL) yet, I strongly recommend to learn it!
Using PIL you can generate images like above from very simple code:
import Image
import ImageDraw
SIZE = 128
image = Image.new("L", (SIZE, SIZE))
d = ImageDraw.Draw(image)
c = 0.5 + 0.2j
for x in range(SIZE):
for y in range(SIZE):
re = (x * 2.0 / SIZE) - 1.0
im = (y * 2.0 / SIZE) - 1.0
z=re+im*1j
for i in range(128):
if abs(z) > 2.0: break
z = z * z + c
d.point((x, y), i * 2)
image.save(r"c:\julia.png", "PNG")
Here is a movie(2MB, c = -0.78 + i * 0.002 + 0.23j (i < 40), center = -0.5 + 0.2j, scale = 1.0). I think this type of movie isn't suitable for MPEG compression, it is raw avi.
Thursday, February 14, 2008
[Python]Eliminate assignment before conditional statement
Python doesn't allow to write a statement in a condition clause.
The limitation keeps beginners away from the well-known bug;
using an assignment while thay want to evaluate equality of two values.
However, it is a little pain to be forced an assignment before a conditional statement for me as below.
One possible solution is introducing a stack.
By using my 'bigstack' library you can write as below.
The library introduces two function 'push' and 'pop' to the built-in namespace. You may easily imagine how it works. And the temporary variable 'm' is no longer needed.
The source code of the 'bigstack' library is as below. It is very simple.
I doesn't think it is the perfect solution, but it could make your code more pretty especially in a 'while' statement.
-----
p.s.
I'm sorry. It requires extra "else" clause to pop the value. worthless...
The limitation keeps beginners away from the well-known bug;
using an assignment while thay want to evaluate equality of two values.
However, it is a little pain to be forced an assignment before a conditional statement for me as below.
import re
data = "aaaabbbbaaaa"
m = re.search("b+", data)
if m:
print "'b+' is found at", m.start()
One possible solution is introducing a stack.
By using my 'bigstack' library you can write as below.
import re
import bigstack
data = "aaaabbbbaaaa"
if push(re.search("b+", data)):
print "'b+' is found at", pop().start()
The library introduces two function 'push' and 'pop' to the built-in namespace. You may easily imagine how it works. And the temporary variable 'm' is no longer needed.
The source code of the 'bigstack' library is as below. It is very simple.
I doesn't think it is the perfect solution, but it could make your code more pretty especially in a 'while' statement.
"""bigstack.py: singleton stack to eliminate temporary variables"""
import __builtin__
BIG_STACK = []
def push(x):
BIG_STACK.append(x)
return x
def pop():
return BIG_STACK.pop()
__builtin__.__dict__.update(
push=push,
pop=pop
)
-----
p.s.
I'm sorry. It requires extra "else" clause to pop the value. worthless...
Wednesday, February 13, 2008
[Python]Simple Impl. of Bloom Filter
Bloom filter is a data structure to tell whether an object is in given list or not. Of cource, if you hold the given list you can do it. The merit of bloom filter is it doesn't need to hold the original list. As a result you can save a memory space. However, it lost an accuracy of result as the price of space-efficiency. It is possible to answer 'yes' while a query is not in the list in a probability. Because you can estimate the probability, bloom filter remains useful.
The following source is a simple implementation of a bloom filter to learn algorithm.
The function 'hashes' calculates three hash value using three different hash function. Class 'BloomFilter' has a bit-array. For simple implementation, I use a list as a substitute for a bit array. It decleases the bloom filter's merit much so you shouldn't use the code in practical use.
The following list is the result of interactive execution on a python's shell.
When a string added, the bloom filter write '1' on the position of bit array corresponds to each hash value. When a string queried, the bloom filter checks each position and if and only if all of them are '1' it says 'True'. You can imagine more strings are added, the bit array has more '1', and bloom filter more possibly says 'True'. The probability can easily estimate. Given m is the size of bit array, k is the number of hash functions and n is the number of strings you added, the probability is (1 - exp(-float(k * n) / m)) ** k. In this case, m equals 1987 and k equals 3, so when n is 100 the prob. is 0.003 and when n is 1000 the prob. is 0.47. You have to increase the size of bit array if you want to add 1000 strings to a bloom filter.
The following source is a simple implementation of a bloom filter to learn algorithm.
SIZE = 1987
def hashes(s):
xs = [0, 0, 0]
for c in s:
o = ord(c)
xs[0] = xs[0] * 137 + o
xs[1] = xs[1] * 69 + o
xs[2] = xs[2] * 545 + o
return [x % SIZE for x in xs]
class BloomFilter(object):
def __init__(self):
self.bitarray = [0] * SIZE
def add(self, s):
for x in hashes(s):
self.bitarray[x] = 1
def query(self, s):
return all(
self.bitarray[x] == 1
for x in hashes(s))
The function 'hashes' calculates three hash value using three different hash function. Class 'BloomFilter' has a bit-array. For simple implementation, I use a list as a substitute for a bit array. It decleases the bloom filter's merit much so you shouldn't use the code in practical use.
The following list is the result of interactive execution on a python's shell.
>>> bf = BloomFilter()
>>> bf.add("hoge")
>>> bf.query("hoge")
True
>>> bf.query("hoga")
False
>>> bf.add("foo")
>>> bf.add("bar")
>>> bf.add("baz")
>>> bf.query("hoga")
False
>>> bf.query("foo")
True
When a string added, the bloom filter write '1' on the position of bit array corresponds to each hash value. When a string queried, the bloom filter checks each position and if and only if all of them are '1' it says 'True'. You can imagine more strings are added, the bit array has more '1', and bloom filter more possibly says 'True'. The probability can easily estimate. Given m is the size of bit array, k is the number of hash functions and n is the number of strings you added, the probability is (1 - exp(-float(k * n) / m)) ** k. In this case, m equals 1987 and k equals 3, so when n is 100 the prob. is 0.003 and when n is 1000 the prob. is 0.47. You have to increase the size of bit array if you want to add 1000 strings to a bloom filter.
I'll speak on Asiajin Meeting Tokyo #1
On 2/26(Tue), an English meeting will be held in Tokyo.
Asiajin Meeting Tokyo #1 on 26th(Tue), Feb. | Asiajin
I'll attend and speak on my web-service 'doukaku.org'. It is a community site for programmers. I'm nervous whether I can make myself understood, but to challenge is better than to hesitate, I think.
After the meeting, I'll upload my slide and wrote an entry about it.
Asiajin Meeting Tokyo #1 on 26th(Tue), Feb. | Asiajin
I'll attend and speak on my web-service 'doukaku.org'. It is a community site for programmers. I'm nervous whether I can make myself understood, but to challenge is better than to hesitate, I think.
After the meeting, I'll upload my slide and wrote an entry about it.
Thursday, January 3, 2008
Get values from Wii Remote (through IronPython and WiimoteLib.dll)
This entry is translation of my entry in Japanese. http://d.hatena.ne.jp/nishiohirokazu/20071227/1198746597
To get values from Wii Remote through IronPython is very easy.
At first you need to get WiimoteLi.dll from
Managed Library for Nintendo's Wiimote - Release: WiimoteLib v1.2.
Its document is nice.
And then save a following script as name "setup_wii.py"
In the script, at first I get a namespace "WiimoteLib" from wiimotelib.dll and import all objects in it.
Secondly I make an instance of "Wiimote" class. Its "WiimoteChanged" field is "event" and "+=" means "add event listener". I set a minimal event listener.
Finally I call connect its instance and tell it should return IR-Camera data and accelation data. You should read WiimoteLib.dll's document and choice the correct report type.
Let's use the script in interactive console.
To know what fields the object have, you should read WiimoteLib.dll's document. It is very comprehensive. Today I want to get the result of IR-Camera, so I did like below.
Reference:
Coding4Fun : Managed Library for Nintendo's Wiimote
Acknowledge:
The reference to my Japanese entry in IronPython URL's: Using the Wiimote from IronPython motivated me to write blog in English. Thanks!
To get values from Wii Remote through IronPython is very easy.
At first you need to get WiimoteLi.dll from
Managed Library for Nintendo's Wiimote - Release: WiimoteLib v1.2.
Its document is nice.
And then save a following script as name "setup_wii.py"
import clr
clr.AddReferenceToFile("wiimotelib.dll")
from WiimoteLib import *
wii = Wiimote()
def get_value(sender, args):
global a
a = args
wii.WiimoteChanged += WiimoteChangedEventHandler(get_value)
wii.Connect()
wii.SetReportType(wii.InputReport.IRAccel, True)
In the script, at first I get a namespace "WiimoteLib" from wiimotelib.dll and import all objects in it.
Secondly I make an instance of "Wiimote" class. Its "WiimoteChanged" field is "event" and "+=" means "add event listener". I set a minimal event listener.
Finally I call connect its instance and tell it should return IR-Camera data and accelation data. You should read WiimoteLib.dll's document and choice the correct report type.
Let's use the script in interactive console.
>>> import setup_wiiOK, I got the argument of the event listener.
>>> setup_wii.a
<WiimoteChangedEventArgs object at 0x000000000000002B>
To know what fields the object have, you should read WiimoteLib.dll's document. It is very comprehensive. Today I want to get the result of IR-Camera, so I did like below.
>>> setup_wii.a.WiimoteStateI got it! It is very easy!
<WiimoteState object at 0x000000000000002C>
>>> setup_wii.a.WiimoteState.IRState
<WiimoteLib.IRState object at 0x000000000000002D [WiimoteLib.IRState]>
>>> setup_wii.a.WiimoteState.IRState.X1
0.379091
Reference:
Coding4Fun : Managed Library for Nintendo's Wiimote
Acknowledge:
The reference to my Japanese entry in IronPython URL's: Using the Wiimote from IronPython motivated me to write blog in English. Thanks!
Blog in English
To write blog entries in English continuously is that I want to achive this year. Because of almost of my activity depends on Japanese language, I didn't be known by those who doesn't use Japanese. I should write more entries in English.
Subscribe to:
Posts (Atom)