<?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>Erxin, Shang(Edwin, 尚尔鑫)&#039;s Home Page &#187; Algorithm</title>
	<atom:link href="http://www.shangerxin.com/?feed=rss2&#038;tag=algorithm" rel="self" type="application/rss+xml" />
	<link>http://www.shangerxin.com</link>
	<description>Knowledge should be free.</description>
	<lastBuildDate>Tue, 24 Aug 2021 02:27:23 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8.13</generator>
	<item>
		<title>Use Python to Create a Tree View Command Line Tool</title>
		<link>http://www.shangerxin.com/?p=258</link>
		<comments>http://www.shangerxin.com/?p=258#comments</comments>
		<pubDate>Thu, 12 Nov 2015 13:34:22 +0000</pubDate>
		<dc:creator><![CDATA[Shang Erxin]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.shangerxin.com/?p=258</guid>
		<description><![CDATA[In recent days I need to generate a directory free from the window command prompt. As I known there is a DOS tool called tree could do this. But it is not quite fit for my requirement. Here is an example which the graphic generated by built-in tree command in window. D:\PersonalTools&#62;tree D:\emacs-24.5 /A 卷 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>In recent days I need to generate a directory free from the window command prompt. As I known there is a DOS tool called tree could do this. But it is not quite fit for my requirement. Here is an example which the graphic generated by built-in tree command in window.</p>
<blockquote><p>D:\PersonalTools&gt;tree D:\emacs-24.5 /A<br />
卷 WareHouse 的文件夹 PATH 列表<br />
卷序列号为 64CA-A0C3</p>
<p>D:\EMACS-24.5<br />
+&#8212;admin<br />
|   +&#8212;charsets<br />
|   |   \&#8212;mapfiles<br />
|   +&#8212;coccinelle<br />
|   +&#8212;grammars<br />
|   +&#8212;notes<br />
|   +&#8212;nt<br />
|   \&#8212;unidata<br />
+&#8212;build-aux<br />
|   \&#8212;snippet<br />
+&#8212;doc<br />
|   +&#8212;emacs<br />
|   +&#8212;lispintro<br />
|   +&#8212;lispref<br />
|   +&#8212;man<br />
|   \&#8212;misc<br />
+&#8212;etc<br />
|   +&#8212;charsets<br />
|   +&#8212;e<br />
|   +&#8212;forms<br />
|   +&#8212;gnus<br />
|   +&#8212;images<br />
|   |   +&#8212;custom<br />
|   |   +&#8212;ezimage<br />
|   |   +&#8212;gnus<br />
|   |   +&#8212;gud<br />
|   |   +&#8212;icons<br />
|   |   |   +&#8212;allout-widgets<br />
|   |   |   |   +&#8212;dark-bg<br />
|   |   |   |   \&#8212;light-bg<br />
|   |   |   \&#8212;hicolor<br />
|   |   |       +&#8212;128&#215;128<br />
|   |   |       |   \&#8212;apps<br />
|   |   |       +&#8212;16&#215;16<br />
|   |   |       |   \&#8212;apps<br />
|   |   |       +&#8212;24&#215;24<br />
|   |   |       |   \&#8212;apps<br />
|   |   |       +&#8212;32&#215;32<br />
|   |   |       |   \&#8212;apps<br />
|   |   |       +&#8212;48&#215;48<br />
|   |   |       |   \&#8212;apps<br />
|   |   |       \&#8212;scalable<br />
|   |   |           +&#8212;apps<br />
|   |   |           \&#8212;mimetypes<br />
|   |   +&#8212;low-color<br />
|   |   +&#8212;mail<br />
|   |   +&#8212;mpc<br />
|   |   +&#8212;newsticker<br />
|   |   +&#8212;smilies<br />
|   |   |   +&#8212;grayscale<br />
|   |   |   \&#8212;medium<br />
|   |   \&#8212;tree-widget<br />
|   |       +&#8212;default<br />
|   |       \&#8212;folder<br />
|   +&#8212;nxml<br />
|   +&#8212;org<br />
|   +&#8212;refcards<br />
|   +&#8212;schema<br />
|   +&#8212;srecode<br />
|   +&#8212;themes<br />
|   \&#8212;tutorials<br />
+&#8212;info<br />
+&#8212;leim<br />
|   +&#8212;CXTERM-DIC<br />
|   +&#8212;MISC-DIC<br />
|   \&#8212;SKK-DIC<br />
+&#8212;lib<br />
+&#8212;lib-src<br />
+&#8212;lisp<br />
|   +&#8212;calc<br />
|   +&#8212;calendar<br />
|   +&#8212;cedet<br />
|   |   +&#8212;ede<br />
|   |   +&#8212;semantic<br />
|   |   |   +&#8212;analyze<br />
|   |   |   +&#8212;bovine<br />
|   |   |   +&#8212;decorate<br />
|   |   |   +&#8212;symref<br />
|   |   |   \&#8212;wisent<br />
|   |   \&#8212;srecode<br />
|   +&#8212;emacs-lisp<br />
|   +&#8212;emacs-parallel<br />
|   +&#8212;emulation<br />
|   +&#8212;erc<br />
|   +&#8212;eshell<br />
|   +&#8212;gnus<br />
|   +&#8212;international<br />
|   +&#8212;language<br />
|   +&#8212;leim<br />
|   |   +&#8212;ja-dic<br />
|   |   \&#8212;quail<br />
|   +&#8212;mail<br />
|   +&#8212;mh-e<br />
|   +&#8212;net<br />
|   +&#8212;nxml<br />
|   +&#8212;obsolete<br />
|   +&#8212;org<br />
|   +&#8212;play<br />
|   +&#8212;progmodes<br />
|   +&#8212;term<br />
|   +&#8212;textmodes<br />
|   +&#8212;url<br />
|   \&#8212;vc<br />
+&#8212;lwlib<br />
+&#8212;m4<br />
+&#8212;msdos<br />
+&#8212;nextstep<br />
|   +&#8212;Cocoa<br />
|   |   \&#8212;Emacs.base<br />
|   |       \&#8212;Contents<br />
|   |           \&#8212;Resources<br />
|   +&#8212;GNUstep<br />
|   |   \&#8212;Emacs.base<br />
|   |       \&#8212;Resources<br />
|   \&#8212;templates<br />
+&#8212;nt<br />
|   +&#8212;icons<br />
|   \&#8212;inc<br />
|       +&#8212;arpa<br />
|       +&#8212;netinet<br />
|       \&#8212;sys<br />
+&#8212;oldXMenu<br />
+&#8212;site-lisp<br />
\&#8212;src<br />
\&#8212;bitmaps</p></blockquote>
<p>A quite complicate file structure for emacs 24.5. There are several limitations for this command.</p>
<ol>
<li>It can&#8217;t control the iteration depth</li>
<li>When display with files it doesn&#8217;t generated the dash lines which is not clearly to indicate the file structure</li>
<li>If we could give a depth parameter I also want to display the sub items as ellipsis to indicate there is something under but which is omitted</li>
</ol>
<p>So I use python to write a custom version to enhance this command called <a href="https://github.com/shangerxin/PersontalTools">treex</a>. It support generated the same structure as the tree command but also provide additional control switch. The source code is already pushed to my personal repository in GitHub. Feel free to check it out.</p>
<p>The help information is</p>
<blockquote><p>usage: treex.py [-h] [-f] [-d DEPTH] [-m] [-e] path</p>
<p>A Cmd Line Tool for Graphically displays the folder structure of a drive or<br />
path</p>
<p>positional arguments:<br />
path</p>
<p>optional arguments:<br />
-h, &#8211;help            show this help message and exit<br />
-f, &#8211;file            Display the names of the files in each folder<br />
-d DEPTH, &#8211;depth DEPTH<br />
Display the depth number<br />
-m, &#8211;mark            Mark the item is file or directory<br />
-e, &#8211;ellipsis        Mark the next sub-item with ellipsis</p>
<p>Create by Edwin, Shang(Shang, Erxin) License under MIT. Version 1.0.0</p></blockquote>
<p>Try to use the treex to generate a depth 2 directory structure</p>
<blockquote><p>D:\PersonalTools&gt;c:\Python27\python.exe treex.py D:\emacs-24.5 -d 2<br />
Folder path listing<br />
D:\emacs-24.5<br />
+&#8212;admin<br />
|   +&#8212;charsets<br />
|   +&#8212;coccinelle<br />
|   +&#8212;grammars<br />
|   +&#8212;notes<br />
|   +&#8212;nt<br />
|   \&#8212;unidata<br />
+&#8212;build-aux<br />
|   \&#8212;snippet<br />
+&#8212;doc<br />
|   +&#8212;emacs<br />
|   +&#8212;lispintro<br />
|   +&#8212;lispref<br />
|   +&#8212;man<br />
|   \&#8212;misc<br />
+&#8212;etc<br />
|   +&#8212;charsets<br />
|   +&#8212;e<br />
|   +&#8212;forms<br />
|   +&#8212;gnus<br />
|   +&#8212;images<br />
|   +&#8212;nxml<br />
|   +&#8212;org<br />
|   +&#8212;refcards<br />
|   +&#8212;schema<br />
|   +&#8212;srecode<br />
|   +&#8212;themes<br />
|   \&#8212;tutorials<br />
+&#8212;info<br />
+&#8212;leim<br />
|   +&#8212;CXTERM-DIC<br />
|   +&#8212;MISC-DIC<br />
|   \&#8212;SKK-DIC<br />
+&#8212;lib<br />
+&#8212;lib-src<br />
+&#8212;lisp<br />
|   +&#8212;calc<br />
|   +&#8212;calendar<br />
|   +&#8212;cedet<br />
|   +&#8212;emacs-lisp<br />
|   +&#8212;emacs-parallel<br />
|   +&#8212;emulation<br />
|   +&#8212;erc<br />
|   +&#8212;eshell<br />
|   +&#8212;gnus<br />
|   +&#8212;international<br />
|   +&#8212;language<br />
|   +&#8212;leim<br />
|   +&#8212;mail<br />
|   +&#8212;mh-e<br />
|   +&#8212;net<br />
|   +&#8212;nxml<br />
|   +&#8212;obsolete<br />
|   +&#8212;org<br />
|   +&#8212;play<br />
|   +&#8212;progmodes<br />
|   +&#8212;term<br />
|   +&#8212;textmodes<br />
|   +&#8212;url<br />
|   \&#8212;vc<br />
+&#8212;lwlib<br />
+&#8212;m4<br />
+&#8212;msdos<br />
+&#8212;nextstep<br />
|   +&#8212;Cocoa<br />
|   +&#8212;GNUstep<br />
|   \&#8212;templates<br />
+&#8212;nt<br />
|   +&#8212;icons<br />
|   \&#8212;inc<br />
+&#8212;oldXMenu<br />
+&#8212;site-lisp<br />
\&#8212;src<br />
\&#8212;bitmaps</p></blockquote>
<p>Here is another result for generate with ellipsis and marks</p>
<blockquote><p>D:\PersonalTools&gt;c:\Python27\python.exe treex.py D:\emacs-24.5 -d 2 -e -m<br />
Folder path listing<br />
D:\emacs-24.5<br />
[d]+&#8212;admin<br />
[d]|   +&#8212;charsets<br />
[d]|   |   \&#8212;&#8230;<br />
[d]|   +&#8212;coccinelle<br />
[d]|   +&#8212;grammars<br />
[d]|   +&#8212;notes<br />
[d]|   +&#8212;nt<br />
[d]|   \&#8212;unidata<br />
[d]+&#8212;build-aux<br />
[d]|   \&#8212;snippet<br />
[d]+&#8212;doc<br />
[d]|   +&#8212;emacs<br />
[d]|   +&#8212;lispintro<br />
[d]|   +&#8212;lispref<br />
[d]|   +&#8212;man<br />
[d]|   \&#8212;misc<br />
[d]+&#8212;etc<br />
[d]|   +&#8212;charsets<br />
[d]|   +&#8212;e<br />
[d]|   +&#8212;forms<br />
[d]|   +&#8212;gnus<br />
[d]|   +&#8212;images<br />
[d]|   |   +&#8212;&#8230;<br />
[d]|   +&#8212;nxml<br />
[d]|   +&#8212;org<br />
[d]|   +&#8212;refcards<br />
[d]|   +&#8212;schema<br />
[d]|   +&#8212;srecode<br />
[d]|   +&#8212;themes<br />
[d]|   \&#8212;tutorials<br />
[d]+&#8212;info<br />
[d]+&#8212;leim<br />
[d]|   +&#8212;CXTERM-DIC<br />
[d]|   +&#8212;MISC-DIC<br />
[d]|   \&#8212;SKK-DIC<br />
[d]+&#8212;lib<br />
[d]+&#8212;lib-src<br />
[d]+&#8212;lisp<br />
[d]|   +&#8212;calc<br />
[d]|   +&#8212;calendar<br />
[d]|   +&#8212;cedet<br />
[d]|   |   +&#8212;&#8230;<br />
[d]|   +&#8212;emacs-lisp<br />
[d]|   +&#8212;emacs-parallel<br />
[d]|   +&#8212;emulation<br />
[d]|   +&#8212;erc<br />
[d]|   +&#8212;eshell<br />
[d]|   +&#8212;gnus<br />
[d]|   +&#8212;international<br />
[d]|   +&#8212;language<br />
[d]|   +&#8212;leim<br />
[d]|   |   +&#8212;&#8230;<br />
[d]|   +&#8212;mail<br />
[d]|   +&#8212;mh-e<br />
[d]|   +&#8212;net<br />
[d]|   +&#8212;nxml<br />
[d]|   +&#8212;obsolete<br />
[d]|   +&#8212;org<br />
[d]|   +&#8212;play<br />
[d]|   +&#8212;progmodes<br />
[d]|   +&#8212;term<br />
[d]|   +&#8212;textmodes<br />
[d]|   +&#8212;url<br />
[d]|   \&#8212;vc<br />
[d]+&#8212;lwlib<br />
[d]+&#8212;m4<br />
[d]+&#8212;msdos<br />
[d]+&#8212;nextstep<br />
[d]|   +&#8212;Cocoa<br />
[d]|   |   \&#8212;&#8230;<br />
[d]|   +&#8212;GNUstep<br />
[d]|   |   \&#8212;&#8230;<br />
[d]|   \&#8212;templates<br />
[d]+&#8212;nt<br />
[d]|   +&#8212;icons<br />
[d]|   \&#8212;inc<br />
[d]|       +&#8212;&#8230;<br />
[d]+&#8212;oldXMenu<br />
[d]+&#8212;site-lisp<br />
[d]\&#8212;src<br />
[d]    \&#8212;bitmaps</p></blockquote>
<p>The key technical point is the depth first traversal algorithms, and I implement it with nonrecursive mode to reduce the stack usage.</p>
<p>The other key point is how to generate the graphic. There is a pattern between the second line and the previous line. So I use it. The implementation could be found from the source.</p>
<p>That&#8217;s all. Happy coding.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shangerxin.com/?feed=rss2&#038;p=258</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
