<?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>{Dev Tricks} &#187; Code</title>
	<atom:link href="http://dev-tricks.net/category/code/feed" rel="self" type="application/rss+xml" />
	<link>http://dev-tricks.net</link>
	<description>Blogging developper tips and tricks</description>
	<lastBuildDate>Thu, 17 May 2012 13:09:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to check if a string is valid utf-8</title>
		<link>http://dev-tricks.net/check-if-a-string-is-valid-utf8</link>
		<comments>http://dev-tricks.net/check-if-a-string-is-valid-utf8#comments</comments>
		<pubDate>Thu, 17 May 2012 13:09:44 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=387</guid>
		<description><![CDATA[Every day (at least) I&#8217;m facing a problem: how to check if a string is valid in utf-8 ? So I wrote a little C program, that I put on my github. Just be aware that pure ASCII is valid &#8230; <a href="http://dev-tricks.net/check-if-a-string-is-valid-utf8">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Every day (at least) I&#8217;m facing a problem: how to check if a string is valid in utf-8 ?<br />
So I wrote <a href="https://github.com/JulienPalard/is_utf8" title="is_utf8" target="_blank">a little C program, that I put on my github</a>. Just be aware that pure ASCII is valid UTF-8 and that&#8217;s not a bug: my program is checking if a string is valid utf-8, not if the string is in utf-8.</p>
<p>Enjoy :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/check-if-a-string-is-valid-utf8/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>emacs: standard input is not a tty</title>
		<link>http://dev-tricks.net/emacs-standard-input-is-not-a-tty</link>
		<comments>http://dev-tricks.net/emacs-standard-input-is-not-a-tty#comments</comments>
		<pubDate>Sun, 11 Dec 2011 11:54:02 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=377</guid>
		<description><![CDATA[Did you ever tried something like : $ find -name '*.c' &#124; xargs emacs or $ grep -rl snmp . &#124; xargs emacs and got the error &#8220;emacs: standard input is not a tty&#8221; ? That&#8217;s normal, as the stdin &#8230; <a href="http://dev-tricks.net/emacs-standard-input-is-not-a-tty">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Did you ever tried something like :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">'*.c'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> emacs
or
$ <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-rl</span> snmp . <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> emacs</pre></div></div>

<p>and got the error &#8220;emacs: standard input is not a tty&#8221; ?</p>
<p>That&#8217;s normal, as the stdin for emacs is here the pipe, not your tty, you need a workaround to leave the normal stdin to your emacs.</p>
<p>There are different approches, the one I used for a long time was a command substitution :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ emacs $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">'*.c'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>But other approches exists, redirecting /dev/tty to emacs&#8217; stdin :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">'*.c'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">sh</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">'emacs &quot;$@&quot; &lt; /dev/tty'</span> emacs</pre></div></div>

<p>And if you are searching for a specific pattern with grep, you should want to jump directly to the right line using the &#8216;+line file&#8217; syntax of emacs, and the shell substitution :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ emacs $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-rn</span> snmp services<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'s/([^:]*):([0-9]*):.*/+\2 \1/g'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>It&#8217;s good while having only one occurence of the pattern in each file, but if many occurences exists, the file is oppened only once, with a while you can open one emacs for each occurence of the searched pattern and the /dev/tty redirection :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-rn</span> snmp services<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'s/([^:]*):([0-9]*):.*/+\2 \1/g'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> line <span style="color: #c20cb9; font-weight: bold;">file</span>; <span style="color: #000000; font-weight: bold;">do</span> emacs <span style="color: #007800;">$line</span> <span style="color: #007800;">$file</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>tty; <span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/emacs-standard-input-is-not-a-tty/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Automating GNU screen startup</title>
		<link>http://dev-tricks.net/automating-gnu-screen-startup</link>
		<comments>http://dev-tricks.net/automating-gnu-screen-startup#comments</comments>
		<pubDate>Thu, 17 Nov 2011 22:13:58 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=367</guid>
		<description><![CDATA[At work I use GNU screen with one window per server (ssh connection), and when I loose my screen, it takes minutes to rebuild the naming and the ssh connections &#8230; So I searched and found a PHP version on &#8230; <a href="http://dev-tricks.net/automating-gnu-screen-startup">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>At work I use GNU screen with one window per server (ssh connection), and when I loose my screen, it takes minutes to rebuild the naming and the ssh connections &#8230;</p>
<p>So I searched and found a PHP version on <a href="http://snowulf.com/2011/09/13/automated-screen-launch/">Jon&#8217;s blog</a> but I don&#8217;t like PHP and don&#8217;t want a PHP cli on my machine (even to start screen !)</p>
<p>So I rewrote it in bash (not sh, bash, to have brace expansion !)</p>
<p>Enjoy :</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash -f</span>
&nbsp;
<span style="color: #007800;">SCREEN_NAME</span>=julien
<span style="color: #007800;">SERVERS</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>julien,root<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">@</span>dev root<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>www,sql<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> root<span style="color: #000000; font-weight: bold;">@</span>media<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #000000;">1</span>,<span style="color: #000000;">2</span>,<span style="color: #000000;">3</span>,<span style="color: #000000;">4</span>,<span style="color: #000000;">5</span>,<span style="color: #000000;">6</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">screen</span> <span style="color: #660033;">-dmS</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$SCREEN_NAME</span>&quot;</span>
<span style="color: #007800;">NUM</span>=<span style="color: #000000;">0</span>
<span style="color: #000000; font-weight: bold;">for</span> SERVER <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$SERVERS</span>
<span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #007800;">NUM</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>NUM + <span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #c20cb9; font-weight: bold;">screen</span> <span style="color: #660033;">-S</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$SCREEN_NAME</span>&quot;</span> <span style="color: #660033;">-X</span> <span style="color: #c20cb9; font-weight: bold;">screen</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> z<span style="color: #ff0000;">&quot;<span style="color: #007800;">$SERVER</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= z<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
    <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #c20cb9; font-weight: bold;">screen</span> <span style="color: #660033;">-S</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$SCREEN_NAME</span>&quot;</span> <span style="color: #660033;">-p</span> <span style="color: #007800;">$NUM</span> <span style="color: #660033;">-X</span> title <span style="color: #ff0000;">&quot;<span style="color: #007800;">$SERVER</span>&quot;</span>
        <span style="color: #c20cb9; font-weight: bold;">screen</span> <span style="color: #660033;">-S</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$SCREEN_NAME</span>&quot;</span> <span style="color: #660033;">-p</span> <span style="color: #007800;">$NUM</span> <span style="color: #660033;">-X</span> stuff <span style="color: #ff0000;">&quot;ssh <span style="color: #007800;">$SERVER</span> <span style="color: #007800;">$(printf \\r)</span>&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #ff0000;">&quot;Done,  now you can join your screen with :<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #ff0000;">&quot;$ screen -dr -S <span style="color: #007800;">$SCREEN_NAME</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span></pre></div></div>

<p>Don&#8217;t forgot to start your ssh-agent just before starting this script !</p>
<p>PS : If you don&#8217;t have a ssh-agent, you may want to remove the  $(printf \\r) and press enter yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/automating-gnu-screen-startup/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ashttp: vt100 screen scraping exported over HTTP</title>
		<link>http://dev-tricks.net/ashttp-vt100-screen-scraping-exported-over-http</link>
		<comments>http://dev-tricks.net/ashttp-vt100-screen-scraping-exported-over-http#comments</comments>
		<pubDate>Sat, 27 Aug 2011 13:08:21 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Sysadmin]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=358</guid>
		<description><![CDATA[Originally wrote for logtop I just wrote a vt100 screen scraper that listen to a port and serve the screen over HTTP. Basically, you want a `top` (or logtop ;-) ) to be displayed in your website back office ? &#8230; <a href="http://dev-tricks.net/ashttp-vt100-screen-scraping-exported-over-http">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Originally wrote for <a href="https://github.com/JulienPalard/logtop" title="https://github.com/JulienPalard/logtop">logtop</a> I just wrote a vt100 screen scraper that listen to a port and serve the screen over HTTP.</p>
<p>Basically, you want a `top` (or logtop ;-) ) to be displayed in your website back office ? But top outputs in your terminal and you don&#8217;t know how to capture it ? Use ashttp, like this :</p>
<pre lang=bash>
$ ashttp -p 8080 top
</pre>
<p>And then just open the port 8080 with an HTTP client and enjoy (typing some F5&#8230;) :</p>
<p><img src="http://dev-tricks.net/wp-content/uploads/2011/08/ashttp1.png" alt="" title="ashttp-capture" width="668" height="424" class="aligncenter size-full wp-image-362" /></p>
<p>You can find the code on <a href="https://github.com/JulienPalard/ashttp" title="https://github.com/JulienPalard/ashttp"> my github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/ashttp-vt100-screen-scraping-exported-over-http/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>emacs: Highlighting errors for c, python, and other languages</title>
		<link>http://dev-tricks.net/emacs-syntax-error-highlighting</link>
		<comments>http://dev-tricks.net/emacs-syntax-error-highlighting#comments</comments>
		<pubDate>Sat, 11 Jun 2011 11:54:12 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=343</guid>
		<description><![CDATA[Hi ! Today we&#8217;ll see how to highlight syntax errors in emacs, with exemples for C and Python. First you should learn about flymake-mode here : http://www.emacswiki.org/emacs/FlyMake In a nutshell Flymake is a minor mode to perform on the fly &#8230; <a href="http://dev-tricks.net/emacs-syntax-error-highlighting">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hi ! Today we&#8217;ll see how to highlight syntax errors in emacs, with exemples for C and Python.</p>
<p>First you should learn about flymake-mode here : <a href="http://www.emacswiki.org/emacs/FlyMake">http://www.emacswiki.org/emacs/FlyMake</a><br />
In a nutshell Flymake is a minor mode to perform on the fly checks on your files. It can run any external syntax checker by different means, I let you check out the documentation.</p>
<p>Quick and easy setup to highlight C syntax :<br />
You already have a Makefile, that&#8217;s good, so you just have to add a new rule to your Makefile, named &#8216;check-syntax&#8217; :</p>

<div class="wp_syntax"><div class="code"><pre class="make" style="font-family:monospace;">check<span style="color: #004400;">-</span>syntax<span style="color: #004400;">:</span>
    gcc <span style="color: #004400;">-</span>o <span style="color: #004400;">/</span>dev<span style="color: #004400;">/</span>null <span style="color: #004400;">-</span>D<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">DEFINE</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>S <span style="color: #004400;">$</span><span style="color: #004400;">&#123;</span><span style="color: #000088;">CHK_SOURCES</span><span style="color: #004400;">&#125;</span></pre></div></div>

<p>You can fix my -D$(DEFINE) $(CFLAGS) to match your compile options &#8230;<br />
Then open a .c file in your project, and if you .emacs file don&#8217;t automatically start flymake-mode, type &#8216;M-x flymake-mode&#8217; and enjoy error highlighting !</p>
<p>Next trick is, if you&#8217;re using a non graphical emacs, you don&#8217;t have, by default, the error message, so i&#8217;s a bit anying&#8230;<br />
So you&#8217;ll add some lines in you .emacs and a file in you .emacs.d<br />
First, download flymake-cursor.el here http://www.emacswiki.org/emacs/download/flymake-cursor.el and put it in your ~/.emacs.d/<br />
Then in your .emacs, add :</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>require 'cl<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>require 'flymake-cursor<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now, when you stop your cursor on an error, the message should appear in the minibuffer.</p>
<p>Last trick, for Python developers, how to use flymake-mode with pyflakes ?<br />
Just add this to you .emacs file, and tweak it if you want. You should install pyflakes in order to make it work.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; aptitude install pyflakes to check python code                                                                                                                                  </span>
<span style="color: #66cc66;">&#40;</span>require 'flymake-cursor<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>global-set-key <span style="color: #66cc66;">&#91;</span>f4<span style="color: #66cc66;">&#93;</span> 'flymake-goto-next-<span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span>load <span style="color: #ff0000;">&quot;flymake&quot;</span> t<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> flymake-pyflakes-init <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>temp-file <span style="color: #66cc66;">&#40;</span>flymake-init-create-temp-buffer-copy
                       'flymake-create-temp-inplace<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#40;</span>local-file <span style="color: #66cc66;">&#40;</span>file-relative-<span style="color: #b1b100;">name</span>
                        temp-file
                        <span style="color: #66cc66;">&#40;</span>file-name-directory buffer-file-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #ff0000;">&quot;pyflakes&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> local-file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'flymake-allowed-file-name-masks
               '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>.py<span style="color: #000099; font-weight: bold;">\\</span>'&quot;</span> flymake-pyflakes-init<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>add-hook 'find-file-hook 'flymake-find-file-hook<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Bonus trick you should try :<br />
Replace &#8220;pyflakes&#8221; in (list &#8220;pyflakes&#8221; (list local-file)))) to a shell script of you own, running pyflakes, pep8, etc&#8230;as I just found here :<br />
<a href="http://stackoverflow.com/questions/1259873/how-can-i-use-emacs-flymake-mode-for-python-with-pyflakes-and-pylint-checking-cod">http://stackoverflow.com/questions/1259873/how-can-i-use-emacs-flymake-mode-for-python-with-pyflakes-and-pylint-checking-cod</a></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
epylint <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null
pyflakes <span style="color: #ff0000;">&quot;$1&quot;</span>
pep8 <span style="color: #660033;">--ignore</span>=E221,E701,E202 <span style="color: #660033;">--repeat</span> <span style="color: #ff0000;">&quot;$1&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">true</span></pre></div></div>

<p>Enjoy !</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/emacs-syntax-error-highlighting/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Emacs: replace tabs with spaces</title>
		<link>http://dev-tricks.net/emacs-replace-tabs-with-spaces</link>
		<comments>http://dev-tricks.net/emacs-replace-tabs-with-spaces#comments</comments>
		<pubDate>Fri, 06 May 2011 16:27:59 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=340</guid>
		<description><![CDATA[When you want to replace tab with spaces or vice versa don&#8217;t use M-% (query-replace) but M-x tabidy or M-x untabify. They work on the current selection so if you want it to be applied to a whole buffer, try &#8230; <a href="http://dev-tricks.net/emacs-replace-tabs-with-spaces">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When you want to replace tab with spaces or vice versa don&#8217;t use M-% (query-replace) but M-x tabidy or M-x untabify. They work on the current selection so if you want it to be applied to a whole buffer, try C-x h (mark-whole-buffer) before to select the whole buffer.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/emacs-replace-tabs-with-spaces/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Searching and replacing in emacs</title>
		<link>http://dev-tricks.net/emacssearching-string-in</link>
		<comments>http://dev-tricks.net/emacssearching-string-in#comments</comments>
		<pubDate>Tue, 03 May 2011 17:16:16 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=337</guid>
		<description><![CDATA[Day two of my serie about emacs, about searching and replacing. Function name : isearch-forward Typical Key sequence : C-s How to get help : C-h f isearch-forward Usage : isearch-forward let you type a string to be searched incrementally &#8230; <a href="http://dev-tricks.net/emacssearching-string-in">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Day two of my serie about emacs, about searching and replacing.</p>
<p>Function name : isearch-forward<br />
Typical Key sequence : C-s<br />
How to get help : C-h f isearch-forward<br />
Usage : isearch-forward let you type a string to be searched incrementally in the current buffer, successive following C-s will jump to the next match.</p>
<p>Function name : isearch-forward-regexp<br />
Typical Key sequence : C-M-s or C-u C-s<br />
How to get help : C-h f isearch-forward-regexp<br />
Usage : isearch-forward-regexp let you type a regexp to be searched incrementally in the current buffer, successive following C-s will jump to the next match.</p>
<p>Function name : query-replace<br />
Typical Key sequence : M-%<br />
How to get help : C-h f query-replace<br />
Usage : M-% <em>search</em> RET <em>replace</em> RET<br />
Then, for each term found, query-replace will ask you what to do : space or y to replace, delete or n to skip, RET or q to exit, ! for &#8216;yes for all&#8217;, ? to get help about how to enter recursive edit / delete match and recursive edit / edit replacement string / &#8230;</p>
<p>Then you should read about replace-string, replace-regexp, occur, list-matching-lines, multi-occur, multi-occur-in-matching-buffers, how-many, flush-lines, and keep-lines.</p>
<p>Case sensitivity :<br />
A search is by defaut case insensitive, but if you input an upper case letter, it become case sensitive. M-c during a search toggle the case sensitivity.</p>
<p>Configuration variables :<br />
You may consult the documentation about those variable typing :<br />
C-h v <em>variable</em><br />
or<br />
M-x apropos-variable RET case-fold-search RET</p>
<ul>
<li>case-fold-search (Non-nil if searches and matches should ignore case.)</li>
<li>default-case-fold-search (Default value of `case-fold-search&#8217;)</li>
<li>tags-case-fold-search (Whether tags operations should be case-sensitive.)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/emacssearching-string-in/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Numeric arguments in emacs</title>
		<link>http://dev-tricks.net/emacsnumeric-arguments-in</link>
		<comments>http://dev-tricks.net/emacsnumeric-arguments-in#comments</comments>
		<pubDate>Mon, 02 May 2011 13:47:31 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[emacs]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=334</guid>
		<description><![CDATA[I&#8217;m starting a &#8216;emacs trick of the day&#8217; sequence with : Function name : universal-argument Typical Key sequence : C-u How to get help : C-h f universal-argument Usage : C-u receive a numeric argument that is given to the &#8230; <a href="http://dev-tricks.net/emacsnumeric-arguments-in">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m starting a &#8216;emacs trick of the day&#8217; sequence with :</p>
<p>Function name : universal-argument<br />
Typical Key sequence : C-u<br />
How to get help : C-h f universal-argument<br />
Usage : C-u receive a numeric argument that is given to the next called function, when no numeric argument is typed, the value defaults to 4.</p>
<p>So today you can try :<br />
C-u 9 C-n # that move cursor vertically down 9 lines<br />
C-u C-k # that kills 4 lines<br />
C-u C-u C-k # that kills 4 * 4 = 16 lines<br />
C-u 10 n # that enters nnnnnnnnnn<br />
You may ask, what about if I want to input 25 &#8217;6&#8242; ? C-u 256 can&#8217;t work &#8230; so you just have to separate with another C-u :<br />
C-u 25 C-u 1 # enters 6666666666666666666666666</p>
<p>Some functions does not have the simple &#8216;repeating&#8217; effect of receiving a numeric parameter, for example, running C-u C-l does not recenter 4 times your screen ! but the help page of recenter-top-bottom states that :<br />
> A prefix argument is handled like `recenter&#8217;:<br />
>  With numeric prefix ARG, move current line to window-line ARG.<br />
>  With plain `C-u&#8217;, move current line to window center.</p>
<p>A negative argument to C-l move the current line to the line ARG from the bottom of the screen.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/emacsnumeric-arguments-in/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nfsmount : rpc failed: 2</title>
		<link>http://dev-tricks.net/nfsmount_rpc_failed_2</link>
		<comments>http://dev-tricks.net/nfsmount_rpc_failed_2#comments</comments>
		<pubDate>Mon, 18 Apr 2011 12:45:03 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Sysadmin]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=331</guid>
		<description><![CDATA[For those, here on the internet, asking themselves what is this f*cking `rpc failed: 2` while mounting an NFS, i&#8217;ts possible that teh response is here : Your NFS client, trying to mount the NFS share will use RPC to &#8230; <a href="http://dev-tricks.net/nfsmount_rpc_failed_2">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For those, here on the internet, asking themselves what is this f*cking `rpc failed: 2` while mounting an NFS, i&#8217;ts possible that teh response is here :</p>
<p>Your NFS client, trying to mount the NFS share will use RPC to communicate with the serveur, il will go like :</p>
<p>> PORTMAP GETPORT(Program: NFS, Version: 3, Proto: TCP)<br />
< PORTMAP Port: 2049<br />
> PORTMAP GETPORT(Program: MOUNT, Version: 3, Proto: TCP)<br />
< PORTMAP Port 49066<br />
> MOUNT MNT(Program Version: 3, Path: /srv/nfsroot/ )<br />
< MOUNT Reply error 2, "remote can't support version #", Program Version (Minimum): 1, Program Version (Maximum): 2</p>
<p>You can see that the response is "remote can't support version #" and we should have found this solution in the <a href="http://www.ietf.org/rfc/rfc1831.txt">RFC 1831 (RPCv2)</a> stating :</p>
<pre>
  Given that a call message was accepted, the following is the status
   of an attempt to call a remote procedure.

      enum accept_stat {
         SUCCESS       = 0, /* RPC executed successfully             */
         PROG_UNAVAIL  = 1, /* remote hasn't exported program        */
         PROG_MISMATCH = 2, /* remote can't support version #        */
         PROC_UNAVAIL  = 3, /* program can't support procedure       */
         GARBAGE_ARGS  = 4, /* procedure can't decode params         */
         SYSTEM_ERR    = 5  /* errors like memory allocation failure */
      };
</pre>
<p>So the problem is you client is asking for a NFS version greater that your server runs &#8230; but if your server is running NFS v3, check a `ps aux | grep [r]pc.mountd` for :</p>
<p>root      1411  0.0  0.0  18808  1036 ?        Ss   Apr15   0:00 /usr/sbin/rpc.mountd &#8211;manage-gids &#8211;no-nfs-version 3</p>
<p>Did you catch the &#8211;no-nfs-version 3 ? If your server is compiled with NFSv3 support, drop the &#8211;no-nfs-version 3 in your configuration and it should work !</p>
<p>Enjoy !</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/nfsmount_rpc_failed_2/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python: Introducing ppipe : Parallel Pipe</title>
		<link>http://dev-tricks.net/python-introducing-ppipe-parallel-pipe</link>
		<comments>http://dev-tricks.net/python-introducing-ppipe-parallel-pipe#comments</comments>
		<pubDate>Fri, 15 Apr 2011 12:07:47 +0000</pubDate>
		<dc:creator>Julien Palard</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://dev-tricks.net/?p=322</guid>
		<description><![CDATA[I&#8217;ll speak about my pipe python module so if you didn&#8217;t know it, you should first read the first aricle about pipes The idea behind ppipe (parallel pipe) is to transparently make a pipe async and or multithreaded. As multithreading &#8230; <a href="http://dev-tricks.net/python-introducing-ppipe-parallel-pipe">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll speak about my pipe python module so if you didn&#8217;t know it, you should first read <a href="http://dev-tricks.net/pipe-infix-syntax-for-python">the first aricle about pipes</a></p>
<p>The idea behind ppipe (parallel pipe) is to transparently make a pipe async and or multithreaded. As multithreading isn&#8217;t an easy piece of code for everybody, with loads of pollutions like locks, queues, giving code far away from the actual simple task you tried to do&#8230;</p>
<p>The idea is that one kind of multithreading can be nicely handled with a simple design pattern well implemented in python : the queue. The queue handles all the locking part so you don&#8217;t have to worry about it, just make your workers work, enqueue, dequeue, work &#8230; but you still have to create workers !<br />
As pipe is not far away from the concept of queue, as, every part of a pipe command works on a piece of data and then git it to the next worker, it&#8217;s not hard to imagine an asynchronous pipe in which every part of a pipe command can work at the same time. Then it&#8217;s not hard to imagine that n threads can be started for a single step of a pipe command, leading to a completly multithreaded application having ~0 lines of code bloat for the tread generation / synchronization in your actual code.</p>
<p>So I tried to implement it, keeping the actual contract which is very simple that is : Every pipe should take an iterable as input. (I was tempted to change it to &#8216;every pipe must take a Queue as input&#8217; &#8230; but if I don&#8217;t change the contract, normal pipes and parallel pipes should be mixed.), so I created a branch you&#8217;ll found on <a href="https://github.com/julienpalard/pipe/tree/parallel_pipe">github</a> with a single new file &#8216;ppipe.py&#8217; that, actually, is not &#8216;importable&#8217; it&#8217;s only a proof of concept, that can be launched.</p>
<p>Here is the test I wrote using ppipe :</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Normal execution :&quot;</span>
<span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> | where<span style="color: black;">&#40;</span>fat_big_condition1<span style="color: black;">&#41;</span> \
          | where<span style="color: black;">&#40;</span>fat_big_condition2<span style="color: black;">&#41;</span> \
          | add | lineout
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Parallel with 1 worker&quot;</span>
<span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> | parallel_where<span style="color: black;">&#40;</span>fat_big_condition1<span style="color: black;">&#41;</span> \
          | where<span style="color: black;">&#40;</span>fat_big_condition2<span style="color: black;">&#41;</span> \
          | add | lineout
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Parallel with 2 workers&quot;</span>
<span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> | parallel_where<span style="color: black;">&#40;</span>fat_big_condition1, qte_of_workers=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> \
          | parallel_where<span style="color: black;">&#40;</span>fat_big_condition2, qte_of_workers=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> | add | stdout
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Parallel with 4 workers&quot;</span>
<span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> | parallel_where<span style="color: black;">&#40;</span>fat_big_condition1, qte_of_workers=<span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> \
          | parallel_where<span style="color: black;">&#40;</span>fat_big_condition2, qte_of_workers=<span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> | add | stdout</pre></div></div>

<p>The idea is to compare normal pipe (Normal execution) with asynchronous pipe (Parallel with 1 worker), as 1 worker is the default, and then 2 and 4 workers that can be given to a ppipe using &#8216;qte_of_workers=&#8217;.</p>
<p>fat_big_condition1 and 2 are just f*cking long running piece of code like fetching something far far away in the internet &#8230; but for our tests, let&#8217;s use time.sleep :</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> fat_big_condition1<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    log<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #483d8b;">&quot;Working...&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
    log<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #483d8b;">&quot;Done !&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> fat_big_condition2<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
    log<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #483d8b;">&quot;Working...&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
    log<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #483d8b;">&quot;Done !&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span></pre></div></div>

<p>They always return 1 &#8230; and they log using a simple log function that make fat_big_condition1 to log in the left column and fat_big_condition2 to log in the right column :</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">stdoutlock = Lock<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> log<span style="color: black;">&#40;</span>column, text<span style="color: black;">&#41;</span>:
    stdoutlock.<span style="color: black;">acquire</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">' '</span> <span style="color: #66cc66;">*</span> column <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">10</span>,
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">strftime</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%S&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
    <span style="color: #ff7700;font-weight:bold;">print</span> text
    stdoutlock.<span style="color: black;">release</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And that is the output (integers are the current second, so the times didn&#8217;t start at 0&#8230;):</p>
<pre>
Normal execution :
           57 Working...
           59 Done !
                     59 Working...
                     01 Done !
           01 Working...
           03 Done !
                     03 Working...
                     05 Done !
           05 Working...
           07 Done !
                     07 Working...
                     09 Done !
           09 Working...
           11 Done !
                     11 Working...
                     13 Done !

// As you can see here, only one condition is executed at a time,
// that is a normal behavior for a non-threaded program.

Parallel with 1 worker
           13 Working...
           15 Done !
                     15 Working...
           15 Working...
                     17 Done !
           17 Done !
           17 Working...
                     17 Working...
                     19 Done !
           19 Done !
           19 Working...
                     19 Working...
                     21 Done !
           21 Done !
                     21 Working...
                     23 Done !

// Just adding parallel_ to the first where, you now see that it's
// asynchronous and that the two conditions can work at the
// same time, interlacing a bit the output.

Parallel with 2 workers
           23 Working...
           23 Working...
           25 Done !
           25 Working...
           25 Done !
           25 Working...
                     25 Working...
                     25 Working...
           27 Done !
           27 Done !
                     27 Done !
                     27 Working...
                     27 Done !
                     27 Working...
                     29 Done !
                     29 Done !

Parallel with 4 workers
           55 Working...
           55 Working...
           55 Working...
           55 Working...
           57 Done !
           57 Done !
           57 Done !
           57 Done !
                     57 Working...
                     57 Working...
                     57 Working...
                     57 Working...
                     59 Done !
                     59 Done !
                     59 Done !
                     59 Done !

// And now with 2 and 4 workers you can clearly see what
// happens, with 2 workers, input is computed by pairs,
// and with 4 threads, all the input can be computed at once
// but the 4 workers of the 2nd condition have to wait the data
// before starting to work, so in the last test, you have 8 threads,
// only the 4 firsts are working the 2 first second, then only the 4
// others works.
</pre>
<p>To make the creation of ppipe simple, I excluded all the &#8216;threading&#8217; part in a function usable as a decorator, so writing a parallel_where give :</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@Pipe
@Threaded
<span style="color: #ff7700;font-weight:bold;">def</span> parallel_where<span style="color: black;">&#40;</span>item, output, condition<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> condition<span style="color: black;">&#40;</span>item<span style="color: black;">&#41;</span>:
        output.<span style="color: black;">put</span><span style="color: black;">&#40;</span>item<span style="color: black;">&#41;</span></pre></div></div>

<p>You can see the queue here ! :-)</p>
<p>Enjoy !</p>
]]></content:encoded>
			<wfw:commentRss>http://dev-tricks.net/python-introducing-ppipe-parallel-pipe/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

