<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nattster</title>
	<atom:link href="http://nattster.siamdev.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://nattster.siamdev.net</link>
	<description>ลั่ลล้า...</description>
	<lastBuildDate>Sun, 25 Dec 2011 05:13:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>หมาป่า แกะ กะหล่ำปลี</title>
		<link>http://nattster.siamdev.net/2011/12/fox-sheep-cabbage/</link>
		<comments>http://nattster.siamdev.net/2011/12/fox-sheep-cabbage/#comments</comments>
		<pubDate>Sun, 25 Dec 2011 05:06:46 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[คอมพิวเตอร์จ๋า]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=1288</guid>
		<description><![CDATA[เกมพาคนสัตว์สิ่งของข้ามแม่น้ำนี่มีหลายเวอร์ชั่นมาก เกมนึงที่น่าจะเคยเล่นกันคือ &#8220;เกมพาหมาป่า แกะ และกะหล่ำปีข้ามแม่น้ำ&#8221; ในเกมนี้ เราต้องพาของทั้งสามอย่างข้ามฝั่งแม่น้ำโดยมีกฎอยู่ว่า พาของข้ามแม่น้ำได้ทีละ 1 อย่าง ถ้าอยู่กันสองต่อสอง หมาป่าจะกินแกะ และ แกะจะกินกะหล่ำ อ่านกฎ อ่านกติกาเข้าใจยาก&#8230; คลิกที่รูปเพื่อลองเล่นเลยดีกว่าครับ เล่นเกมด่านแรกผ่านแล้วสินะครับ สำหรับ level 2 ในเว็บเมื่อกี้ เป็นเกมพามิชชันนารีกับยักษ์กินคนข้ามแม่น้ำ มีกฎกติกาคือ ต้องมีคนอยู่ในเรืออย่างน้อย 1 คน ถึงจะพายเรือข้ามฝั่งได้ (เรือเปล่าๆ วิ่งข้ามฝั่งเองไม่ได้ครับ) ถ้ายักษ์กินคนมีจำนวนมากกว่ามิชชันนารี ยักษ์จะจับมิชชันนารีกิน -&#62; game over ตอนเรียนวิชา Programming Language ผมมีโอกาสได้ลองเขียนโปรแกรมแก้ puzzle นี้ (มิชชันนารีและยักษ์กินคน) ด้วยภาษา Haskell ครับ โปรแกรมนี้จะให้ลำดับของสถานะที่ทำให้ชนะเกมนี้ครับ --h = number of humans on the left --g [...]]]></description>
			<content:encoded><![CDATA[<p>เกมพาคนสัตว์สิ่งของข้ามแม่น้ำนี่มีหลายเวอร์ชั่นมาก เกมนึงที่น่าจะเคยเล่นกันคือ &#8220;<a href="http://www.learn4good.com/games/puzzle/sheep_and_wolf.htm">เกมพาหมาป่า แกะ และกะหล่ำปีข้ามแม่น้ำ</a>&#8221; ในเกมนี้ เราต้องพาของทั้งสามอย่างข้ามฝั่งแม่น้ำโดยมีกฎอยู่ว่า</p>
<ul>
<li>พาของข้ามแม่น้ำได้ทีละ 1 อย่าง</li>
<li>ถ้าอยู่กันสองต่อสอง หมาป่าจะกินแกะ และ แกะจะกินกะหล่ำ</li>
</ul>
<p>อ่านกฎ อ่านกติกาเข้าใจยาก&#8230; คลิกที่รูปเพื่อลองเล่นเลยดีกว่าครับ</p>
<p><div id="attachment_1293" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.learn4good.com/games/puzzle/sheep_and_wolf.htm" target="_blank"><img class="size-medium wp-image-1293" title="เกมหมาป่า แกะ กะหล่ำ" src="http://nattster.siamdev.net/wp-content/uploads/2011/12/wolf-sheep-cabbage-300x179.png" alt="" width="300" height="179" /></a><p class="wp-caption-text">เกมหมาป่า แกะ กะหล่ำ</p></div><br />
<span id="more-1288"></span></p>
<p>เล่นเกมด่านแรกผ่านแล้วสินะครับ สำหรับ level 2 ในเว็บเมื่อกี้ เป็น<a href="http://www.learn4good.com/games/puzzle/boat.htm" target="_blank">เกมพามิชชันนารีกับยักษ์กินคนข้ามแม่น้ำ</a> มีกฎกติกาคือ</p>
<ul>
<li>ต้องมีคนอยู่ในเรืออย่างน้อย 1 คน ถึงจะพายเรือข้ามฝั่งได้ (เรือเปล่าๆ วิ่งข้ามฝั่งเองไม่ได้ครับ)</li>
<li>ถ้ายักษ์กินคนมีจำนวนมากกว่ามิชชันนารี ยักษ์จะจับมิชชันนารีกิน -&gt; game over</li>
</ul>
<div id="attachment_1289" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.learn4good.com/games/puzzle/boat.htm"><img class="size-medium wp-image-1289" title="Missionary Cannibal River Crossing" src="http://nattster.siamdev.net/wp-content/uploads/2011/12/missionary_cannibal-300x171.png" alt="" width="300" height="171" /></a><p class="wp-caption-text">มิชชันนารี และยักษ์กินคน</p></div>
<p>ตอนเรียนวิชา Programming Language ผมมีโอกาสได้ลองเขียนโปรแกรมแก้ puzzle นี้ (มิชชันนารีและยักษ์กินคน) ด้วยภาษา <a href="http://haskell.org/">Haskell</a> ครับ</p>
<p>โปรแกรมนี้จะให้ลำดับของสถานะที่ทำให้ชนะเกมนี้ครับ</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">--h = number of humans on the left</span>
<span style="color: #5d478b; font-style: italic;">--g = number of giants on the left</span>
<span style="color: #5d478b; font-style: italic;">--s = side (left/right)</span>
&nbsp;
left <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0</span>
right <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1</span>
&nbsp;
valid<span style="color: #339933; font-weight: bold;">_</span>state <span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">,</span> g<span style="color: #339933; font-weight: bold;">,</span> s<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>
    <span style="color: #06c; font-weight: bold;">if</span> <span style="color: green;">&#40;</span>h <span style="color: #339933; font-weight: bold;">&gt;=</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> <span style="color: green;">&#40;</span>h <span style="color: #339933; font-weight: bold;">&lt;=</span> <span style="color: red;">3</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> <span style="color: green;">&#40;</span>g <span style="color: #339933; font-weight: bold;">&gt;=</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> <span style="color: green;">&#40;</span>g <span style="color: #339933; font-weight: bold;">&lt;=</span> <span style="color: red;">3</span><span style="color: green;">&#41;</span>                <span style="color: #5d478b; font-style: italic;">--number of people valid?</span>
    <span style="color: #06c; font-weight: bold;">then</span> <span style="font-weight: bold;">not</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>g <span style="color: #339933; font-weight: bold;">&gt;</span> h<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> <span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">&gt;</span><span style="color: red;">0</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">||</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">-</span>g <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">-</span>h<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> <span style="color: green;">&#40;</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">-</span>h <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>    <span style="color: #5d478b; font-style: italic;">--num of giant &lt;= num of human</span>
    <span style="color: #06c; font-weight: bold;">else</span> False
&nbsp;
change <span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">,</span> g<span style="color: #339933; font-weight: bold;">,</span> s<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>hd<span style="color: #339933; font-weight: bold;">,</span> gd<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>         <span style="color: #5d478b; font-style: italic;">-- create a new state based on hd (human diff), gd (giant diff)</span>
    <span style="color: #06c; font-weight: bold;">if</span> s <span style="color: #339933; font-weight: bold;">==</span> left 
    <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">-</span>hd<span style="color: #339933; font-weight: bold;">,</span> g<span style="color: #339933; font-weight: bold;">-</span>gd<span style="color: #339933; font-weight: bold;">,</span> right<span style="color: green;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">else</span> <span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">+</span>hd<span style="color: #339933; font-weight: bold;">,</span> g<span style="color: #339933; font-weight: bold;">+</span>gd<span style="color: #339933; font-weight: bold;">,</span> left<span style="color: green;">&#41;</span>
&nbsp;
possible<span style="color: #339933; font-weight: bold;">_</span>states <span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">,</span> g<span style="color: #339933; font-weight: bold;">,</span> s<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">filter</span> valid<span style="color: #339933; font-weight: bold;">_</span>state <span style="color: green;">&#91;</span>change<span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">,</span>g<span style="color: #339933; font-weight: bold;">,</span>s<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">|</span>
                            <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span>y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">0</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">2</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
                                    <span style="color: #5d478b; font-style: italic;">--  H      G     HG     HH       GG</span>
solve x <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>                      <span style="color: #5d478b; font-style: italic;">-- reached max depth</span>
solve <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> depth <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>                 <span style="color: #5d478b; font-style: italic;">-- no more sibling node to traverse</span>
solve <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> depth <span style="color: #339933; font-weight: bold;">=</span>
    <span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">,</span> g<span style="color: #339933; font-weight: bold;">,</span> s<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> x;
        z <span style="color: #339933; font-weight: bold;">=</span> solve <span style="color: green;">&#40;</span>possible<span style="color: #339933; font-weight: bold;">_</span>states<span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">,</span>g<span style="color: #339933; font-weight: bold;">,</span>s<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>depth<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">in</span>  <span style="color: #06c; font-weight: bold;">if</span> <span style="color: green;">&#40;</span>h <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> <span style="color: green;">&#40;</span>g <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> <span style="color: green;">&#40;</span>s <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span>
        <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">,</span>g<span style="color: #339933; font-weight: bold;">,</span>s<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
        <span style="color: #06c; font-weight: bold;">else</span>
            <span style="color: #06c; font-weight: bold;">if</span> <span style="font-weight: bold;">length</span> z <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">0</span>        <span style="color: #5d478b; font-style: italic;">--if child got answer</span>
            <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">,</span>g<span style="color: #339933; font-weight: bold;">,</span>s<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">++</span>z   
            <span style="color: #06c; font-weight: bold;">else</span> solve xs depth    <span style="color: #5d478b; font-style: italic;">--otherise return siblings' answer</span>
&nbsp;
iter<span style="color: #339933; font-weight: bold;">_</span>dpn depth <span style="color: #339933; font-weight: bold;">=</span>                <span style="color: #5d478b; font-style: italic;">--Iterative deepening depth-first search</span>
    <span style="color: #06c; font-weight: bold;">let</span> ans <span style="color: #339933; font-weight: bold;">=</span> solve <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span>left<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> depth
    <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #06c; font-weight: bold;">if</span> <span style="font-weight: bold;">length</span> ans <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">0</span>
       <span style="color: #06c; font-weight: bold;">then</span> ans
       <span style="color: #06c; font-weight: bold;">else</span> iter<span style="color: #339933; font-weight: bold;">_</span>dpn <span style="color: green;">&#40;</span>depth<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- run &quot;iter_dpn 0&quot; to find answer</span></pre></div></div>

<p>สถานะ = 3-tuple (จำนวนมิชชันนารีฝั่งซ้าย, จำนวนยักษ์ฝั่งซ้าย, ฝั่งที่เรือจอดอยู่)<br />
สถานะเริ่มต้น = (3, 3, left) มิชชันนารี ยักษ์ และเรืออยู่ฝั่งซ้ายทั้งหมด<br />
สถานะจบเกม = (0, 0, right) มิชชันนารี ยักษ์ และเรือข้ามมาอยู่ฝั่งขวาหมดแล้ว</p>
<p>วิธีแก้ปัญหานี้ผมใช้ depth-first search เริ่มค้นจากสถานะเริ่มต้นไปหาสถานะจบเกม โดยไม่ท่องไปในกิ่งที่ทำให้แพ้ (มีคนน้อยกว่ายักษ์ ยักษ์เลยจับคนกิน)</p>
<p>บางคนอาจสงสัยว่า ถ้าใช้ depth-first search แล้วเราจะได้คำตอบที่สั้นที่สุดมั้ย (พายเรือน้อยครั้งที่สุด) คำตอบคือผมใช้ <a href="http://en.wikipedia.org/wiki/Iterative_deepening_depth-first_search">Iterative deepening depth-first search</a> ครับ<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2011/12/fox-sheep-cabbage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django: QuerySet is not a List</title>
		<link>http://nattster.siamdev.net/2011/11/django-queryset-is-not-a-list/</link>
		<comments>http://nattster.siamdev.net/2011/11/django-queryset-is-not-a-list/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 15:20:03 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[คอมพิวเตอร์จ๋า]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=1261</guid>
		<description><![CDATA[เวลาดึงข้อมูลจาก Model เช่น blogs = Blog.objects.all&#40;&#41; # ได้ QuerySet ของบล็อกหลายๆ อัน print blogs&#91;0&#93;.title print blogs&#91;0&#93;.body print blogs&#91;1&#93;.title print blogs&#91;1&#93;.body จะเห็นว่าเราใช้ QuerySet ได้เหมือนเป็นอาเรย์หรือ list เลย แต่สิ่งที่ต้องระวังคือ blogs เป็น QuerySet ไม่ใช่ list เวลาอัพเดทข้อมูลใน model ต้องระวัง bug แบบนี้ 1 2 3 blogs&#91;0&#93;.title = &#34;New Title&#34; blogs&#91;0&#93;.body = &#34;new content&#34; blogs&#91;0&#93;.save&#40;&#41; ถ้ารันโค้ดข้างบนแล้ว blogs[0] จะไม่เปลี่ยนแปลงค่าอะไรเลย เพราะ: blogs[0] ในแต่ละบรรทัดเป็น object คนละอันกัน [...]]]></description>
			<content:encoded><![CDATA[<p>เวลาดึงข้อมูลจาก Model เช่น</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">blogs = Blog.<span style="color: black;">objects</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>    <span style="color: #808080; font-style: italic;"># ได้ QuerySet ของบล็อกหลายๆ อัน</span>
<span style="color: #ff7700;font-weight:bold;">print</span> blogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">title</span>
<span style="color: #ff7700;font-weight:bold;">print</span> blogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">body</span>
<span style="color: #ff7700;font-weight:bold;">print</span> blogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">title</span>
<span style="color: #ff7700;font-weight:bold;">print</span> blogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">body</span></pre></div></div>

<p>จะเห็นว่าเราใช้ QuerySet ได้เหมือนเป็นอาเรย์หรือ list เลย<br />
แต่สิ่งที่ต้องระวังคือ <strong>blogs เป็น <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/">QuerySet</a> ไม่ใช่ list</strong></p>
<p>เวลาอัพเดทข้อมูลใน model ต้องระวัง </p>
<h3>bug แบบนี้</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">blogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">title</span> = <span style="color: #483d8b;">&quot;New Title&quot;</span>
blogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">body</span> = <span style="color: #483d8b;">&quot;new content&quot;</span>
blogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>ถ้ารันโค้ดข้างบนแล้ว blogs[0] จะไม่เปลี่ยนแปลงค่าอะไรเลย เพราะ:</p>
<ul>
<li>blogs[0] ในแต่ละบรรทัดเป็น object คนละอันกัน (คนละ instance กัน)</li>
<li>การเรียก blogs[0] แต่ละครั้ง QuerySet จะให้ object ใหม่ทุกครั้ง ทำให้การกำหนดค่ามีผลกับ object ในบรรทัดนั้นเฉยๆ</li>
<li>blogs[0].save() ในบรรทัดที่ 3 จะอ่านค่า blogs[0] จาก database และ save() กลับไปโดยยังไม่ได้แก้ไขอะไรเลย</li>
</ul>
<h3>ที่ถูกต้องควรเป็น</h3>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">b = blogs<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
b.<span style="color: black;">title</span> = <span style="color: #483d8b;">&quot;New Title&quot;</span>
b.<span style="color: black;">body</span> = <span style="color: #483d8b;">&quot;new content&quot;</span>
b.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>แต่ถ้าอยากใช้ได้เหมือน list จริงๆ ก็ทำได้โดยบังคับให้ Django มัน evaluate QuerySet ให้กลายเป็น list ด้วยฟังก์ชัน list<br />
เช่น</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">blogs = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>blogs<span style="color: black;">&#41;</span></pre></div></div>

<p>มีข้อเสียคือ<br />
* มี large memory overhead<br />
* blogs.filter ต่อไม่ได้&#8230; เพราะโดน evaluate ไปแล้ว<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2011/11/django-queryset-is-not-a-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>รวมความมึนของผม ใน python</title>
		<link>http://nattster.siamdev.net/2011/08/bug-bug-python/</link>
		<comments>http://nattster.siamdev.net/2011/08/bug-bug-python/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 11:30:13 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[คอมพิวเตอร์จ๋า]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=1272</guid>
		<description><![CDATA[rules = &#40; &#40; 'foo', 1&#41;, &#40; 'spam', 2&#41;, &#40; 'bar', 3&#41;, &#34;&#34;&#34; ( 'egg', 4), ( 'python', 5), &#34;&#34;&#34; &#41; &#160; for name, val in rules: print name, val โค้ดข้างบนพอรันแล้วจะเจอ ValueError (too many values to unpack) ตอนมองแว๊บแรก ก็คิดว่าโค้ดน่าจะโอเค แค่ comment egg กับ python ออกไปแบบหลายบรรทัด&#8230; แต่จริงๆ แล้วมันไม่ใช่ เราสร้าง list ที่มี (&#8216;foo&#8217;, 1), (&#8216;spam&#8217;, 2), (&#8216;bar&#8217;, [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">rules = <span style="color: black;">&#40;</span>
    <span style="color: black;">&#40;</span> <span style="color: #483d8b;">'foo'</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#40;</span> <span style="color: #483d8b;">'spam'</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#40;</span> <span style="color: #483d8b;">'bar'</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>,
<span style="color: #483d8b;">&quot;&quot;&quot;
    ( 'egg', 4),
    ( 'python', 5),
&quot;&quot;&quot;</span>
<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> name, val <span style="color: #ff7700;font-weight:bold;">in</span> rules:
    <span style="color: #ff7700;font-weight:bold;">print</span> name, val</pre></div></div>

<p>โค้ดข้างบนพอรันแล้วจะเจอ ValueError (too many values to unpack)<br />
ตอนมองแว๊บแรก ก็คิดว่าโค้ดน่าจะโอเค แค่ comment egg กับ python ออกไปแบบหลายบรรทัด&#8230; แต่จริงๆ แล้วมันไม่ใช่</p>
<p>เราสร้าง list ที่มี (&#8216;foo&#8217;, 1), (&#8216;spam&#8217;, 2), (&#8216;bar&#8217;, 3) และ &#8220;&#8221;" (&#8216;egg&#8217;, 4),&#8230;&#8221;"&#8221; &lt;&#8211; ก้อนนี้เป็น string แบบหลายบรรทัด ไม่ใช่ comment!!</p>
<p>ปล. ตอนนี้มีอยู่ 1 bug, เดี๋ยวมีอะไรเพิ่ม จะเอามาแปะอีกครับ<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2011/08/bug-bug-python/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Closure ใน Python</title>
		<link>http://nattster.siamdev.net/2011/07/closure-in-python/</link>
		<comments>http://nattster.siamdev.net/2011/07/closure-in-python/#comments</comments>
		<pubDate>Fri, 08 Jul 2011 10:21:42 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[คอมพิวเตอร์จ๋า]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=1252</guid>
		<description><![CDATA[แฮ่ วันนี้ลองเล่น closure ใน python เต็มที่เลยครับ funcs = &#91;&#93; i=0 def funcfactory&#40;&#41;: global i i += 1 j = i def callback&#40;&#41;: print j funcs.append&#40;callback&#41; &#160; funcfactory&#40;&#41; # ได้ callback ที่มี j = 1 funcfactory&#40;&#41; # ได้ callback ที่มี j = 2 funcfactory&#40;&#41; # ได้ callback ที่มี j = 3 print funcs for f in [...]]]></description>
			<content:encoded><![CDATA[<p>แฮ่ วันนี้ลองเล่น closure ใน python เต็มที่เลยครับ</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">funcs = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
i=<span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">def</span> funcfactory<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">global</span> i
    i += <span style="color: #ff4500;">1</span>
    j = i
    <span style="color: #ff7700;font-weight:bold;">def</span> callback<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> j
    funcs.<span style="color: black;">append</span><span style="color: black;">&#40;</span>callback<span style="color: black;">&#41;</span>
&nbsp;
funcfactory<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>    <span style="color: #808080; font-style: italic;"># ได้ callback ที่มี j = 1</span>
funcfactory<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>    <span style="color: #808080; font-style: italic;"># ได้ callback ที่มี j = 2</span>
funcfactory<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>    <span style="color: #808080; font-style: italic;"># ได้ callback ที่มี j = 3</span>
<span style="color: #ff7700;font-weight:bold;">print</span> funcs
<span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> funcs:
    f<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>list funcs จะเก็บ function &#8216;callback&#8217; ที่มีโค้ดแบบเดียวกัน (print j) แต่มี context ต่างกัน (j = 1, 2, 3)</p>
<p>closure = (function, environment ของฟังก์ชั่น)</p>
<p><strong>Edit:</strong> ข้อจำกัดสำคัญของ Closure ใน python คือ &#8220;อ่านได้อย่างเดียว&#8221; วิธีแก้ปัญหาทำได้โดยส่ง reference แทนตามตัวอย่างใน <a href="http://stackoverflow.com/questions/141642/what-limitations-have-closures-in-python-compared-to-language-x-closures?answertab=votes#tab-top">[ref]</a></p>
<p>ใน Python 3 มีคีย์เวิร์ด nonlocal ไว้ใช้อ้างถึงตัวแปรที่อยู่ใน Scope ก่อนหน้าได้<br />
<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2011/07/closure-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>เขาทำ progress ใน terminal กันยังไงหว่า?</title>
		<link>http://nattster.siamdev.net/2011/06/c-progress-terminal/</link>
		<comments>http://nattster.siamdev.net/2011/06/c-progress-terminal/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 15:43:55 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[คอมพิวเตอร์จ๋า]]></category>
		<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=1244</guid>
		<description><![CDATA[เคยเรียกคำสั่งอะไรใน terminal แล้วมันก็ขึ้นเลข 0%, 10%, 20%, &#8230; 100% ใน terminal ไหม (มันอัพเดทข้อความในบรรทัดเดิมได้เรื่อยๆ&#8230; มันทำยังไงหว่า?) อ่ะนี่ ตัวอักษรสำคัญเลขคือ \r (carriage return) #include&#60;stdio.h&#62; void delay&#40;&#41; &#123; for&#40;int i=0;i&#60;1000000;i++&#41; for&#40;int j=0;j&#60;10;j++&#41;; &#125; int main&#40;&#41; &#123; for&#40;int p=0;p&#60;=100;p++&#41; &#123; delay&#40;&#41;; printf&#40;&#34;\rPercent: %d &#34;, p&#41;; fflush&#40;stdout&#41;; &#125; printf&#40;&#34;\n&#34;&#41;; return 0; &#125; งืมๆ&#8230; มาพ่นโค้ด แล้วก็ไป ฮ่าๆ]]></description>
			<content:encoded><![CDATA[<p>เคยเรียกคำสั่งอะไรใน terminal แล้วมันก็ขึ้นเลข 0%, 10%, 20%, &#8230; 100% ใน terminal ไหม (มันอัพเดทข้อความในบรรทัดเดิมได้เรื่อยๆ&#8230; มันทำยังไงหว่า?)</p>
<p>อ่ะนี่ ตัวอักษรสำคัญเลขคือ <a href="http://en.wikipedia.org/wiki/Carriage_return">\r (carriage return)</a></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;stdio.h&gt;</span>
<span style="color: #0000ff;">void</span> delay<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">1000000</span><span style="color: #008080;">;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>j<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> p<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>p<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">100</span><span style="color: #008080;">;</span>p<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
       delay<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
       <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>Percent: %d &quot;</span>, p<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
       <span style="color: #0000dd;">fflush</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">stdout</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #008000;">&#125;</span>
   <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>งืมๆ&#8230; มาพ่นโค้ด แล้วก็ไป ฮ่าๆ<br />
<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2011/06/c-progress-terminal/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Keyboard ManMan</title>
		<link>http://nattster.siamdev.net/2011/01/keyboard-manman/</link>
		<comments>http://nattster.siamdev.net/2011/01/keyboard-manman/#comments</comments>
		<pubDate>Wed, 26 Jan 2011 12:54:27 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[คอมพิวเตอร์จ๋า]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[cpe]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[keyboard]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=1226</guid>
		<description><![CDATA[สวัสดีครับ ผมกำลังทำโปรเจคจบอยู่ครับ สิ่งที่ทำคือ Keyboard ManMan (คีย์บอร์ดแม่นๆ) คีย์บอร์ดนี้สามารถย่อขยายปุ่ม ตามความน่าจะเป็นที่จะโดนกดครับ เช่นในรูปทางซ้ายมือนี้ คีย์บอร์ดเดาว่าปุ่มถัดไปที่เราน่าจะกดคือ สระอี (ปุ่มสระอีมีขนาดใหญ่กว่าปุ่มอื่น) ตอนนี้จำเป็นต้องวัดผลครับว่า คีย์บอร์ดที่ทำช่วยให้พิมพ์เร็วขึ้นหรือไม่อย่างไร อยากรบกวนให้ดาวน์โหลดโปรแกรม 2 โปรแกรมจาก Android Market ครับ โปรแกรมแรกเป็นคีย์บอร์ด อีกโปรแกรมเป็นโปรแกรมวัดความเร็วการพิมพ์ครับ สามารถค้นใน Market ว่า Natt Piyapramote หรือใช้ Barcode ข้างล่างได้เลยครับ โปรแกรม Keyboard ManMan โปรแกรม Keyboard Labs เมื่อดาวน์โหลดโปรแกรมแล้วให้ Enable Keyboard โดย 1. กลับไปที่ Home และกดปุ่ม Menu 2. เลือกเมนู Settings -> Language &#038; keyboard 3. ติ๊กถูกที่ Keyboard [...]]]></description>
			<content:encoded><![CDATA[<p>สวัสดีครับ ผมกำลังทำโปรเจคจบอยู่ครับ</p>
<p>สิ่งที่ทำคือ Keyboard ManMan (คีย์บอร์ดแม่นๆ)<br />
<a href="http://nattster.siamdev.net/wp-content/uploads/2011/01/manman_adaptive_fb.png" rel="lightbox[1226]" title="manman_adaptive_fb"><img src="http://nattster.siamdev.net/wp-content/uploads/2011/01/manman_adaptive_fb.png" alt="" title="manman_adaptive_fb" width="320" height="312" class="alignleft size-full wp-image-1227" /></a><br />
คีย์บอร์ดนี้สามารถย่อขยายปุ่ม ตามความน่าจะเป็นที่จะโดนกดครับ เช่นในรูปทางซ้ายมือนี้ คีย์บอร์ดเดาว่าปุ่มถัดไปที่เราน่าจะกดคือ สระอี (ปุ่มสระอีมีขนาดใหญ่กว่าปุ่มอื่น)</p>
<p>ตอนนี้จำเป็นต้องวัดผลครับว่า คีย์บอร์ดที่ทำช่วยให้พิมพ์เร็วขึ้นหรือไม่อย่างไร</p>
<p>อยากรบกวนให้ดาวน์โหลดโปรแกรม 2 โปรแกรมจาก Android Market ครับ โปรแกรมแรกเป็นคีย์บอร์ด อีกโปรแกรมเป็นโปรแกรมวัดความเร็วการพิมพ์ครับ สามารถค้นใน Market ว่า <strong>Natt Piyapramote</strong> หรือใช้ Barcode ข้างล่างได้เลยครับ</p>
<table border="0">
<tr>
<th width="50%" valign="top">
<h2>โปรแกรม Keyboard ManMan</h2>
<p><a href="http://nattster.siamdev.net/wp-content/uploads/2011/01/manman_screenshot.png" rel="lightbox[1226]" title="manman_screenshot"><img src="http://nattster.siamdev.net/wp-content/uploads/2011/01/manman_screenshot-180x300.png" alt="" title="manman_screenshot" width="180" height="300" class="aligncenter size-medium wp-image-1228" /></a><br />
<img src="http://chart.apis.google.com/chart?chs=225x225&#038;cht=qr&#038;chl=market://details?id=net.siamdev.nattster.manman" />
</th>
<th valign="top">
<h2>โปรแกรม Keyboard Labs</h2>
<p><a href="http://nattster.siamdev.net/wp-content/uploads/2011/01/keyboard_lab_screenshot2.png" rel="lightbox[1226]" title="keyboard_lab_screenshot2"><img src="http://nattster.siamdev.net/wp-content/uploads/2011/01/keyboard_lab_screenshot2-180x300.png" alt="" title="keyboard_lab_screenshot2" width="180" height="300" class="aligncenter size-medium wp-image-1229" /></a><br />
<img src="http://chart.apis.google.com/chart?chs=225x225&#038;cht=qr&#038;chl=market://details?id=net.siamdev.nattster.KeyboardLabs" />
</th>
</tr>
</table>
<p>เมื่อดาวน์โหลดโปรแกรมแล้วให้ Enable Keyboard โดย</p>
<ul>
<ol>1. กลับไปที่ Home และกดปุ่ม Menu</ol>
<ol>2. เลือกเมนู Settings -> Language &#038; keyboard</ol>
<ol>3. ติ๊กถูกที่ Keyboard ManMan</ol>
</ul>
<p>เปิดโปรแกรม Keyboard Labs และลองพิมพ์โดยใช้คีย์บอร์ดแม่นแม่น และคีย์บอร์ดอื่นๆ ที่เพื่อนๆ เคยใช้มาก่อน เมื่อทดลองแล้วผลการทดลองจะปรากฏที่หน้าจอหลัก <strong>ถ้าเพื่อนๆ ต่อ Internet แล้ว อยากให้ช่วยกดปุ่ม &#8220;ส่งผลทดสอบให้นักพัฒนา&#8221; ด้วยครับ</strong> ผมจะได้ทราบว่าการย่อขยายปุ่ม/การเดาอื่นๆ จะช่วยให้พิมพ์ได้รวดเร็ว หรือผิดพลาดน้อยลงหรือไม่ครับ</p>
<p><strong>แนะนำติชมได้ที่ <a href="http://on.fb.me/manman_">http://on.fb.me/manman_</a> เลยนะคร้าบ</strong><br />
ขอบคุณครับ ^^<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2011/01/keyboard-manman/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>xclip ที่ไม่ใช่คลิปโป๊</title>
		<link>http://nattster.siamdev.net/2011/01/xclip/</link>
		<comments>http://nattster.siamdev.net/2011/01/xclip/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 19:45:27 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[คอมพิวเตอร์จ๋า]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=1216</guid>
		<description><![CDATA[ปกติผมมักจะเปิด Terminal เรียกคำสั่ง แล้ว Copy ผลลัพธ์ไปแปะในโปรแกรมอื่น เช่น Browser แต่รู้สึกชีวิตมันลำบากเหลือเกิน เลยไปค้นเจอโปรแกรม xclip เป็นโปรแกรมจัดการ Clipboard (ที่พักข้อมูลเวลาเรา Copy/Paste) บน Linux วิธีใช้ cat file.txt &#124; xclip # เอาผลลัพธ์ของคำสั่ง cat file.txt ไปใส่ใน clipboard กด Paste ที่โปรแกรมที่ต้องการได้ทันที คำสั่งอื่นๆ xclip -o # พิมพ์ข้อมูลที่มีอยู่ใน clipboard ออกมา xclip -o &#62; out.txt # เอาเนื้อหาใน clipboard ลงไฟล์ out.txt ติดตั้งด้วยคำสั่ง: sudo apt-get install xclip ตามปกติครับ]]></description>
			<content:encoded><![CDATA[<p>ปกติผมมักจะเปิด Terminal เรียกคำสั่ง แล้ว Copy ผลลัพธ์ไปแปะในโปรแกรมอื่น เช่น Browser<br />
แต่รู้สึกชีวิตมันลำบากเหลือเกิน เลยไปค้นเจอโปรแกรม xclip เป็นโปรแกรมจัดการ Clipboard (ที่พักข้อมูลเวลาเรา Copy/Paste) บน Linux </p>
<h3>วิธีใช้</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> file.txt <span style="color: #000000; font-weight: bold;">|</span> xclip        <span style="color: #666666; font-style: italic;"># เอาผลลัพธ์ของคำสั่ง cat file.txt ไปใส่ใน clipboard</span></pre></div></div>

<p>กด Paste ที่โปรแกรมที่ต้องการได้ทันที</p>
<h3>คำสั่งอื่นๆ</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">xclip <span style="color: #660033;">-o</span>      <span style="color: #666666; font-style: italic;"># พิมพ์ข้อมูลที่มีอยู่ใน clipboard ออกมา</span>
xclip <span style="color: #660033;">-o</span> <span style="color: #000000; font-weight: bold;">&gt;</span> out.txt    <span style="color: #666666; font-style: italic;"># เอาเนื้อหาใน clipboard ลงไฟล์ out.txt</span></pre></div></div>

<p><strong>ติดตั้งด้วยคำสั่ง: </strong> sudo apt-get install xclip ตามปกติครับ<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2011/01/xclip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ทำไมต้องมี Jagged Array?</title>
		<link>http://nattster.siamdev.net/2011/01/jagged-array/</link>
		<comments>http://nattster.siamdev.net/2011/01/jagged-array/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 08:12:13 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=1199</guid>
		<description><![CDATA[ใครเขียน C# จะงงๆ ว่าทำไม Array 2 มิติถึงประกาศได้ 2 แบบ คือ int&#91;,&#93; val = new int&#91;3, 3&#93;; int&#91;&#93;&#91;&#93; val = new int&#91;&#93;&#91;3&#93;; for&#40;int i=0; i &#60; 3; i++&#41; val&#91;i&#93; = new int&#91;3&#93;; เอ๊ะ มันก็ได้ array 2 มิติเหมือนๆ กันนี่นา จะมีสองแบบไปทำไม&#8230; พอไปค้นเว็บ ก็เลยอ๋อ&#8230;เราสามารถสร้าง Jagged Array ให้มีหน้าตาแบบนี้ก็ได้ครับ]]></description>
			<content:encoded><![CDATA[<p>ใครเขียน C# จะงงๆ ว่าทำไม Array 2 มิติถึงประกาศได้ 2 แบบ คือ</p>
<table width="100%">
<tr>
<td valign="top">

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#91;</span>,<span style="color: #008000;">&#93;</span> val <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span></pre></div></div>

<p><a href="http://nattster.siamdev.net/wp-content/uploads/2011/01/2darray.jpg" rel="lightbox[1199]" title="2darray"><img src="http://nattster.siamdev.net/wp-content/uploads/2011/01/2darray.jpg" alt="" title="2darray" width="250" height="156" class="alignleft size-full wp-image-1207" /></a>
</td>
<td valign="top">

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> val <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">for</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i<span style="color: #008000;">=</span><span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
    val<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span></pre></div></div>

<p><a href="http://nattster.siamdev.net/wp-content/uploads/2011/01/jagged.gif" rel="lightbox[1199]" title="jagged"><img src="http://nattster.siamdev.net/wp-content/uploads/2011/01/jagged.gif" alt="" title="jagged" width="250" height="195" class="alignleft size-full wp-image-1208" /></a>
</td>
</tr>
</table>
<p>เอ๊ะ มันก็ได้ array 2 มิติเหมือนๆ กันนี่นา จะมีสองแบบไปทำไม&#8230; พอไปค้นเว็บ ก็เลยอ๋อ&#8230;เราสามารถสร้าง Jagged Array ให้มีหน้าตาแบบนี้ก็ได้ครับ<br />
<a href="http://nattster.siamdev.net/wp-content/uploads/2011/01/triangle.jpg" rel="lightbox[1199]" title="triangle"><img src="http://nattster.siamdev.net/wp-content/uploads/2011/01/triangle.jpg" alt="" title="triangle" width="300" height="217" class="alignleft size-full wp-image-1209" /></a><!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2011/01/jagged-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python วนลูปบน list ที่โตขึ้นเรื่อยๆ</title>
		<link>http://nattster.siamdev.net/2010/12/python-%e0%b8%a7%e0%b8%99%e0%b8%a5%e0%b8%b9%e0%b8%9b%e0%b8%9a%e0%b8%99-list-%e0%b8%97%e0%b8%b5%e0%b9%88%e0%b9%82%e0%b8%95%e0%b8%82%e0%b8%b6%e0%b9%89%e0%b8%99%e0%b9%80%e0%b8%a3%e0%b8%b7%e0%b9%88/</link>
		<comments>http://nattster.siamdev.net/2010/12/python-%e0%b8%a7%e0%b8%99%e0%b8%a5%e0%b8%b9%e0%b8%9b%e0%b8%9a%e0%b8%99-list-%e0%b8%97%e0%b8%b5%e0%b9%88%e0%b9%82%e0%b8%95%e0%b8%82%e0%b8%b6%e0%b9%89%e0%b8%99%e0%b9%80%e0%b8%a3%e0%b8%b7%e0%b9%88/#comments</comments>
		<pubDate>Tue, 07 Dec 2010 13:15:27 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[คอมพิวเตอร์จ๋า]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=1194</guid>
		<description><![CDATA[a = &#91;1&#93; for i in a: print i if len&#40;a&#41; &#60; 10: a.append&#40;i+1&#41; ได้ผลลัพธ์เป็น 1 2 3 4 5 6 7 8 9 10 ตื่นเต้นดี for loop บน list ที่โตขึ้นเรื่อยๆ&#8230; ไม่รู้ว่าจะจบเมื่อไหร่]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">a = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> a:
    <span style="color: #ff7700;font-weight:bold;">print</span> i
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">10</span>:
        a.<span style="color: black;">append</span><span style="color: black;">&#40;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre></div></div>

<p>ได้ผลลัพธ์เป็น<br />
1<br />
2<br />
3<br />
4<br />
5<br />
6<br />
7<br />
8<br />
9<br />
10</p>
<p>ตื่นเต้นดี for loop บน list ที่โตขึ้นเรื่อยๆ&#8230; ไม่รู้ว่าจะจบเมื่อไหร่<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2010/12/python-%e0%b8%a7%e0%b8%99%e0%b8%a5%e0%b8%b9%e0%b8%9b%e0%b8%9a%e0%b8%99-list-%e0%b8%97%e0%b8%b5%e0%b9%88%e0%b9%82%e0%b8%95%e0%b8%82%e0%b8%b6%e0%b9%89%e0%b8%99%e0%b9%80%e0%b8%a3%e0%b8%b7%e0%b9%88/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chromium ที่ใช้ภาษาไทยได้สำหรับ Ubuntu</title>
		<link>http://nattster.siamdev.net/2010/10/chromium-thai-ubuntu/</link>
		<comments>http://nattster.siamdev.net/2010/10/chromium-thai-ubuntu/#comments</comments>
		<pubDate>Sat, 23 Oct 2010 01:31:57 +0000</pubDate>
		<dc:creator>nattster</dc:creator>
				<category><![CDATA[คอมพิวเตอร์จ๋า]]></category>
		<category><![CDATA[chromium]]></category>
		<category><![CDATA[ppa]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://nattster.siamdev.net/?p=1183</guid>
		<description><![CDATA[EDIT: ตอนนี้ Chromium บน Linux ใช้ภาษาไทยได้แล้วนะครับ โหลดจาก PPA ของ Chromium Dev Channel (ผมใช้รุ่น 11.0.672.2 (75134) อยู่ครับ) Chromium คือโปรเจค Open-source ที่เป็นฐานของ Google Chrome ที่เราใช้ๆ กัน ปัญหาใหญ่ของคนใช้ Ubuntu คือ Chrome มันแสดงผลสระภาษาไทยเลื่อนไปทางซ้าย ทำให้อ่านเว็บไม่รู้เรื่อง จากที่ได้พยายามทำ chromium-thai-ppa กับพี่ lewcpe ในงาน Barcamp Bangkhen ผมก็กลับมางมต่อที่บ้านอีก 2 วัน ในที่สุดก็สามารถอัพ Chromium ตัวที่แก้ภาษาไทยขึ้น Ubuntu PPA ได้แล้วครับ วิธีติดตั้งง่ายมาก 3 บรรทัดจบ (เปิด Terminal แล้วพิมพ์คำสั่งทีละบรรทัด) sudo apt-add-repository ppa:thai/chromium-thai-ppa [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>
<strong>EDIT:</strong> ตอนนี้ Chromium บน Linux ใช้ภาษาไทยได้แล้วนะครับ โหลดจาก <a href="https://launchpad.net/~chromium-daily/+archive/dev">PPA ของ Chromium Dev Channel</a> (ผมใช้รุ่น 11.0.672.2 (75134) อยู่ครับ)
</p></blockquote>
<p><a href="http://nattster.siamdev.net/wp-content/uploads/2010/10/flying_browser.png" rel="lightbox[1183]" title="flying_browser"><img src="http://nattster.siamdev.net/wp-content/uploads/2010/10/flying_browser.png" alt="" title="flying_browser" width="208" height="121" class="alignleft size-full wp-image-1187" /></a> Chromium คือโปรเจค Open-source ที่เป็นฐานของ Google Chrome ที่เราใช้ๆ กัน ปัญหาใหญ่ของคนใช้ Ubuntu คือ Chrome มันแสดงผลสระภาษาไทยเลื่อนไปทางซ้าย ทำให้อ่านเว็บไม่รู้เรื่อง</p>
<p>จากที่ได้พยายามทำ <a href="https://launchpad.net/~thai/+archive/chromium-thai-ppa">chromium-thai-ppa</a> กับพี่ <a href="http://twitter.com/lewcpe">lewcpe</a> ในงาน <a href="http://barcampbangkhen.org">Barcamp Bangkhen</a> ผมก็กลับมางมต่อที่บ้านอีก 2 วัน ในที่สุดก็สามารถอัพ Chromium ตัวที่แก้ภาษาไทยขึ้น Ubuntu PPA ได้แล้วครับ</p>
<p>วิธีติดตั้งง่ายมาก 3 บรรทัดจบ (เปิด Terminal แล้วพิมพ์คำสั่งทีละบรรทัด)</p>

<div class="wp_syntax"><div class="code"><pre class="generic" style="font-family:monospace;">sudo apt-add-repository ppa:thai/chromium-thai-ppa
sudo apt-get update
sudo apt-get install chromium-browser</pre></div></div>

<p>หมายเหตุ: Chromium รุ่นที่มีอยู่บน PPA ตอนนี้คือตัวเดียวกับใน Chromium Dev Channel (อาจพังได้เป็นบางครั้งบางคราว) เอามา patch ภาษาไทยนิดนึง</p>
<p><del datetime="2010-10-25T09:35:59+00:00">เดี๋ยวจะมาเขียนบล็อกเพิ่มเกี่ยวกับวิธีเอา package ขึ้น Ubuntu PPA ครับ (สำหรับคนใจร้อนสามารถ<a href="http://lewcpe.com/blog/archives/1222/ppa/">อ่านบล็อกพี่ลิ่ว</a>ก่อนได้เลยครับ)</del><br />
เขียน<a href="http://www.blognone.com/node/19430">วิธีสร้างและเอา package ขึ้น Ubuntu PPA </a>แล้วนะครับอยู่ที่ฟอรัม Project ของ Blognone<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://nattster.siamdev.net/2010/10/chromium-thai-ubuntu/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

