<?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>Wojciech Jamrozy blog &#187; Python</title>
	<atom:link href="http://www.wojtekrj.net/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wojtekrj.net</link>
	<description>Blog about computer science, programming and linux</description>
	<lastBuildDate>Sun, 23 Oct 2011 21:57:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[Python] Synchronization and threading examples</title>
		<link>http://www.wojtekrj.net/2009/08/python-synchronization-examples/</link>
		<comments>http://www.wojtekrj.net/2009/08/python-synchronization-examples/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 16:08:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Synchronizing]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://www.wojtekrj.net/?p=213</guid>
		<description><![CDATA[I want to present my code in Python solving Senate Bus Problem from The Little book of Semaphores (page 211). I&#8217;ve used several synchronizing objects such as Lock, Thread, Condition and Semaphore. Here is link to Python threading library. #!/usr/bin/env python # Wojciech Jamrozy # Script written during Operating Systems course VI 2009 # Problem: [...]]]></description>
			<content:encoded><![CDATA[<p>I want to present my code in Python solving Senate Bus Problem from <a href="http://www.greenteapress.com/semaphores/downey08semaphores.pdf ">The Little book of Semaphores</a> (page 211). I&#8217;ve used several synchronizing objects such as  Lock, Thread, Condition and Semaphore.<br />
<a target="_blank" href="http://docs.python.org/library/threading.html">Here </a>is link to Python threading library.</p>
<p><span id="more-213"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># Wojciech Jamrozy</span>
<span style="color: #808080; font-style: italic;"># Script written during Operating Systems course VI 2009</span>
<span style="color: #808080; font-style: italic;"># Problem: The Senate Bus Problem</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">time</span> <span style="color: #ff7700;font-weight:bold;">import</span> sleep
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">threading</span> <span style="color: #ff7700;font-weight:bold;">import</span> Lock, Thread, Condition, Semaphore
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">random</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
waiting_passengers = <span style="color: #ff4500;">0</span>
<span style="color: #dc143c;">mutex</span> = Lock<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
bus = Condition<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
on_board = Semaphore<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Configuration:
        NUMBER_OF_BUSES = <span style="color: #ff4500;">15</span>
        NUMBER_OF_PASSENGERS = <span style="color: #ff4500;">100</span>
        BUS_DELAY = <span style="color: #ff4500;">5</span>
        PASSENGER_DELAY = .8
        TIME_OF_BOARDING = <span style="color: #ff4500;">1</span>
        SEATS = <span style="color: #ff4500;">10</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Bus<span style="color: black;">&#40;</span>Thread<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, numer<span style="color: black;">&#41;</span>:
        Thread.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">numer</span> = numer
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">mutex</span>.<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;">&quot;Bus %d arrived&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">numer</span>,<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">global</span> waiting_passengers
        n = <span style="color: #008000;">min</span><span style="color: black;">&#40;</span>waiting_passengers, Configuration.<span style="color: black;">SEATS</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:
            bus.<span style="color: black;">acquire</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            bus.<span style="color: black;">notify</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            bus.<span style="color: black;">release</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            on_board.<span style="color: black;">acquire</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            waiting_passengers -= <span style="color: #ff4500;">1</span>
        sleep<span style="color: black;">&#40;</span>Configuration.<span style="color: black;">TIME_OF_BOARDING</span><span style="color: #66cc66;">*</span><span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Bus %d departed&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">numer</span>,<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">mutex</span>.<span style="color: black;">release</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Passenger<span style="color: black;">&#40;</span>Thread<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, numer<span style="color: black;">&#41;</span>:
        Thread.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">numer</span> = numer
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">global</span> waiting_passengers
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Passenger %d has come&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">numer</span>,<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">mutex</span>.<span style="color: black;">acquire</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        waiting_passengers += <span style="color: #ff4500;">1</span>
        <span style="color: #dc143c;">mutex</span>.<span style="color: black;">release</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        bus.<span style="color: black;">acquire</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        bus.<span style="color: black;">wait</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;">&quot;Passenger %d is on board&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">numer</span>,<span style="color: black;">&#41;</span>
        bus.<span style="color: black;">release</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        on_board.<span style="color: black;">release</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CreateBuses<span style="color: black;">&#40;</span>Thread<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>Configuration.<span style="color: black;">NUMBER_OF_BUSES</span><span style="color: black;">&#41;</span>:
            b = Bus<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
            b.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            sleep<span style="color: black;">&#40;</span><span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span>Configuration.<span style="color: black;">BUS_DELAY</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Creating buses finished - nacisnij ENTER aby zakonczyc&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CreatePassengers<span style="color: black;">&#40;</span>Thread<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>Configuration.<span style="color: black;">NUMBER_OF_PASSENGERS</span><span style="color: black;">&#41;</span>:
            o = Passenger<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
            o.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            sleep<span style="color: black;">&#40;</span><span style="color: #dc143c;">random</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span>Configuration.<span style="color: black;">PASSENGER_DELAY</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Creating passengers finished - press ENTER in order to finish&quot;</span>
&nbsp;
busy = CreateBuses<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
passengers = CreatePassengers<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
busy.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
passengers.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Press any key in order to finish<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span> 
<span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p><a class="a2a_button_wykop" href="http://www.addtoany.com/add_to/wykop?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;linkname=%5BPython%5D%20Synchronization%20and%20threading%20examples" title="Wykop" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/wykop.png" width="16" height="16" alt="Wykop"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;linkname=%5BPython%5D%20Synchronization%20and%20threading%20examples" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;linkname=%5BPython%5D%20Synchronization%20and%20threading%20examples" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_technorati_favorites" href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;linkname=%5BPython%5D%20Synchronization%20and%20threading%20examples" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a><a class="a2a_button_digg" href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;linkname=%5BPython%5D%20Synchronization%20and%20threading%20examples" title="Digg" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;linkname=%5BPython%5D%20Synchronization%20and%20threading%20examples" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_msdn" href="http://www.addtoany.com/add_to/msdn?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;linkname=%5BPython%5D%20Synchronization%20and%20threading%20examples" title="MSDN" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/msdn.png" width="16" height="16" alt="MSDN"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;linkname=%5BPython%5D%20Synchronization%20and%20threading%20examples" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_technotizie" href="http://www.addtoany.com/add_to/technotizie?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;linkname=%5BPython%5D%20Synchronization%20and%20threading%20examples" title="Technotizie" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technotizie.png" width="16" height="16" alt="Technotizie"/></a><a class="a2a_button_technet" href="http://www.addtoany.com/add_to/technet?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;linkname=%5BPython%5D%20Synchronization%20and%20threading%20examples" title="TechNet" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technet.png" width="16" height="16" alt="TechNet"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F08%2Fpython-synchronization-examples%2F&amp;title=%5BPython%5D%20Synchronization%20and%20threading%20examples" id="wpa2a_2">Share/Save</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.wojtekrj.net/2009/08/python-synchronization-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Python] Django FileField problem with polish/non-english letters</title>
		<link>http://www.wojtekrj.net/2009/07/python-django-filefield-problem-with-polishnon-english-letters/</link>
		<comments>http://www.wojtekrj.net/2009/07/python-django-filefield-problem-with-polishnon-english-letters/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 12:39:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[localization]]></category>

		<guid isPermaLink="false">http://www.wojtekrj.net/?p=204</guid>
		<description><![CDATA[I&#8217;ve created a Django model using FileField: class Car&#40;models.Model&#41;: ... car = models.FileField&#40;upload_to='upl/%Y/%m/%d'&#41; ... I wanted to upload files named using polish characters (for example: &#8220;auto-żółw&#8221;). Unfortunately,  Django erease polish letters (&#8220;auto-żółw&#8221; -&#62; &#8220;auto-w&#8221;). I&#8217;ve find out that the problem is in django.utils.text.get_valid_filename function. After I&#8217;ve changed line: return re.sub&#40;r'[^-A-Za-z0-9_.]', '', s&#41; to return re.sub&#40;'(?u)[^-.\w]', [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve created a Django model using FileField:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Car<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    ...
    <span style="color: black;">car</span> = models.<span style="color: black;">FileField</span><span style="color: black;">&#40;</span>upload_to=<span style="color: #483d8b;">'upl/%Y/%m/%d'</span><span style="color: black;">&#41;</span>
    ...</pre></div></div>

<p>I wanted to upload files named using polish characters (for example: &#8220;auto-żółw&#8221;). Unfortunately,  Django erease polish letters (&#8220;auto-żółw&#8221; -&gt; &#8220;auto-w&#8221;).<span id="more-204"></span></p>
<p>I&#8217;ve find out that the problem is in <em>django.utils.text.get_valid_filename</em> function.<br />
After I&#8217;ve changed line:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'[^-A-Za-z0-9_.]'</span>, <span style="color: #483d8b;">''</span>, s<span style="color: black;">&#41;</span></pre></div></div>

<p>to</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">sub</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'(?u)[^-.<span style="color: #000099; font-weight: bold;">\w</span>]'</span>, <span style="color: #483d8b;">''</span>, s<span style="color: black;">&#41;</span></pre></div></div>

<p>Django accepts polish letters, too.</p>
<p><a class="a2a_button_wykop" href="http://www.addtoany.com/add_to/wykop?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;linkname=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" title="Wykop" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/wykop.png" width="16" height="16" alt="Wykop"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;linkname=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;linkname=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_technorati_favorites" href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;linkname=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a><a class="a2a_button_digg" href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;linkname=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" title="Digg" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;linkname=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_msdn" href="http://www.addtoany.com/add_to/msdn?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;linkname=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" title="MSDN" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/msdn.png" width="16" height="16" alt="MSDN"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;linkname=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_technotizie" href="http://www.addtoany.com/add_to/technotizie?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;linkname=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" title="Technotizie" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technotizie.png" width="16" height="16" alt="Technotizie"/></a><a class="a2a_button_technet" href="http://www.addtoany.com/add_to/technet?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;linkname=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" title="TechNet" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technet.png" width="16" height="16" alt="TechNet"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.wojtekrj.net%2F2009%2F07%2Fpython-django-filefield-problem-with-polishnon-english-letters%2F&amp;title=%5BPython%5D%20Django%20FileField%20problem%20with%20polish%2Fnon-english%20letters" id="wpa2a_4">Share/Save</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.wojtekrj.net/2009/07/python-django-filefield-problem-with-polishnon-english-letters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Python,Algorithms] Fast Modular exponentiation script</title>
		<link>http://www.wojtekrj.net/2008/09/pythonalgorithms-fast-modular-exponentiation-script/</link>
		<comments>http://www.wojtekrj.net/2008/09/pythonalgorithms-fast-modular-exponentiation-script/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 19:26:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[exponentiation]]></category>

		<guid isPermaLink="false">http://www.wojtekrj.net/?p=125</guid>
		<description><![CDATA[If we want to know the last ten digits of number an – we have to evaluate expression an mod 1010. Using brute force approach, we have to do O(n) ( If you don&#8217;t understand big „O” notation, visit: Big O notation &#8211; it&#8217;s VERY important) multiplications and modulo divisions. When n is greater than [...]]]></description>
			<content:encoded><![CDATA[<p>If we want to know the last ten digits of number <em>a<sup>n</sup></em> – we have to evaluate expression <em>a<sup>n</sup> mod 10<sup>10</sup></em>. Using brute force approach, we have to do<em> O(n) </em>( If you don&#8217;t understand big „O” notation, visit: <a href="http://en.wikipedia.org/wiki/Big_O_notation" target="_blank">Big O notation</a> &#8211; it&#8217;s VERY important) multiplications and modulo divisions. When<em> n</em> is greater than <em>10<sup>9</sup></em>, it will take a lot of time. Fortunately, we can use more „mathematical” approach, which has<em> O(log n) </em>complexity.<span id="more-125"></span><br />
Firstly, we have to notice, that:<br />
(1) a<sup>2c</sup> mod r =  (a<sup>c</sup>)<sup>2</sup> mod r<br />
(2) a<sup>2c+1</sup> mod r =  a*(a<sup>c</sup>)<sup>2</sup> mod r</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># Author: Wojtek Jamrozy (www.wojtekrj.net)</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> modexp<span style="color: black;">&#40;</span>a, n, m<span style="color: black;">&#41;</span>:
	bits = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> n:
		bits.<span style="color: black;">append</span><span style="color: black;">&#40;</span>n<span style="color: #66cc66;">%</span>2<span style="color: black;">&#41;</span>
		n /= <span style="color: #ff4500;">2</span>
	solution = <span style="color: #ff4500;">1</span>
	bits.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> bits:
		solution = <span style="color: black;">&#40;</span>solution<span style="color: #66cc66;">*</span>solution<span style="color: black;">&#41;</span><span style="color: #66cc66;">%</span>m
		<span style="color: #ff7700;font-weight:bold;">if</span> x:
			solution = <span style="color: black;">&#40;</span>solution<span style="color: #66cc66;">*</span>a<span style="color: black;">&#41;</span><span style="color: #66cc66;">%</span>m
	<span style="color: #ff7700;font-weight:bold;">return</span> solution
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> modexp<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">34</span>, <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> modexp<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,<span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">7179869184</span>
<span style="color: #000000;">1787109376</span></pre></div></div>

<p>Function <em>pow(a,b)</em> is Python&#8217;s function, which computes <em>a<sup>b</sup></em><br />
Function <em>modexp(a, n, m)</em> computes <em>a<sup>n</sup> mod m</em>. Here is example, which shows running of the function a=2, n=34, m=10<sup>10</sup><br />
Firstly, we want to have bits of binary notation of <em>n</em> in list <em>bits</em>.<br />
Before reversion,<em> bits </em>have following content: [0, 1, 0, 0, 0, 1] (2<sup>1</sup> + 2<sup>5</sup> = 33)<br />
After reversion of list: <em>bits</em>= [1, 0, 0, 0, 1, 0]<br />
Bits of binary notation of <em>n</em> are in order from &#8220;bigger&#8221; powers to &#8220;lower&#8221; ones.<br />
I will use (b) for binary notation of number: 33 = 100001(b)<br />
At the beggining, <em>solution</em> = 2<sup>0</sup> mod 10<sup>10</sup> = 1. Then, during execution of <em>for</em> loop,<em> solution </em>has following values: 2<sup>1(b)</sup> mod 10<sup>10</sup>, 2<sup>10(b)</sup> mod 10<sup>10</sup>, 2<sup>100(b)</sup> mod 10<sup>10</sup>, 2<sup>1000(b)</sup> mod 10<sup>10</sup>, 2<sup>10001(b)</sup> mod 10<sup>10</sup> and finally  2<sup>100010(b)</sup> mod 10<sup>10</sup>.<br />
Using (1) and (2) we can calcualte every <em>solution&#8217;s</em> value if we know previous one.<br />
Loop <em>for</em> is executed for each element of <em>bits</em>. <em>Bits</em> size is <em>log<sub>2</sub>n</em>, so the complexity of whole alghorithm is <em>O(log n)</em></p>
<p>You can download code here:<br />
Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p><a class="a2a_button_wykop" href="http://www.addtoany.com/add_to/wykop?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Wykop" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/wykop.png" width="16" height="16" alt="Wykop"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_technorati_favorites" href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a><a class="a2a_button_digg" href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Digg" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_msdn" href="http://www.addtoany.com/add_to/msdn?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="MSDN" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/msdn.png" width="16" height="16" alt="MSDN"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_technotizie" href="http://www.addtoany.com/add_to/technotizie?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="Technotizie" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technotizie.png" width="16" height="16" alt="Technotizie"/></a><a class="a2a_button_technet" href="http://www.addtoany.com/add_to/technet?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;linkname=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" title="TechNet" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technet.png" width="16" height="16" alt="TechNet"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F09%2Fpythonalgorithms-fast-modular-exponentiation-script%2F&amp;title=%5BPython%2CAlgorithms%5D%20Fast%20Modular%20exponentiation%20script" id="wpa2a_6">Share/Save</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.wojtekrj.net/2008/09/pythonalgorithms-fast-modular-exponentiation-script/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[Python] Updater/archiver/synchronizer of directories</title>
		<link>http://www.wojtekrj.net/2008/07/python-updater/</link>
		<comments>http://www.wojtekrj.net/2008/07/python-updater/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 08:29:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[archiver]]></category>
		<category><![CDATA[file manipulation]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[updater]]></category>

		<guid isPermaLink="false">http://www.wojtekrj.net/?p=40</guid>
		<description><![CDATA[I have directory with data (music, photos, ebooks etc.) on my two computers. I wanted to have exactly same files in my data directory on both machines, but sometimes I changed content of the directory on one of computers. I&#8217;ve decided to write program, which would help me to update data directory on another machine. [...]]]></description>
			<content:encoded><![CDATA[<p>I have directory with data (music, photos, ebooks etc.) on my two computers. I wanted to have exactly same files in my data directory on both machines, but sometimes I changed content of the directory on one of computers. I&#8217;ve decided to write program, which would help me to update data directory on another machine.<span id="more-40"></span></p>
<p>Firstly, I&#8217;ve installed Samba, a good tool for managing it – gsambad and smbfs (mount and umount commands for the smbfs). You can use NFS instead of samba if you want. Then, I&#8217;ve made my “changed” data directory accessible for another computer.<br />
Lets assume, that <em>/data</em> is “unchanged” data directory on second computer, and <em>/datarem</em> is mounted “changed” data directory from first computer. You can mount this directory using for example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #660033;">-t</span> smbfs <span style="color: #660033;">-o</span> <span style="color: #007800;">iocharset</span>=utf8,<span style="color: #007800;">username</span>=<span style="color: #ff0000;">&quot;TYPE_SMB_USER&quot;</span>,<span style="color: #007800;">password</span>=<span style="color: #ff0000;">&quot;TYPE_PASS&quot;</span> <span style="color: #000000; font-weight: bold;">//</span>192.168.2.15<span style="color: #000000; font-weight: bold;">/</span>data <span style="color: #000000; font-weight: bold;">/</span>datarem</pre></div></div>

<p>Then you can use my script mov.py with following usage:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>mov.py changed_directory directory_to_update</pre></div></div>

<p>for example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>mov.py <span style="color: #000000; font-weight: bold;">/</span>datarem <span style="color: #000000; font-weight: bold;">/</span>data</pre></div></div>

<p>The script will scan <em>/datarem</em> recursively for files and directories. If corresponding file or directory doesn&#8217;t exist in <em>/data</em>, it will be created. If size of file in <em>/datarem</em> is different to a size of corresponding file in <em>/data</em>, the file in<em> /data</em> will be replaced. If sizes are equal, file in <em>/data</em> is keep unchanged (it doesn&#8217;t matter for me, but for some usages it may be a problem).</p>
<p>Here is code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># Created by Wojtek Jamrozy (www.wojtekrj.net)</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>, <span style="color: #dc143c;">shutil</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> update<span style="color: black;">&#40;</span>start, end<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>start<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">listdir</span><span style="color: black;">&#40;</span>start<span style="color: black;">&#41;</span>:
			s_ = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>start, f<span style="color: black;">&#41;</span>
			e_ = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>end, f<span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>s_<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>e_<span style="color: black;">&#41;</span>:
				<span style="color: #dc143c;">os</span>.<span style="color: black;">mkdir</span><span style="color: black;">&#40;</span>e_<span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Directory &quot;</span> +e_+<span style="color: #483d8b;">&quot; has been created<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
			update<span style="color: black;">&#40;</span>s_,e_<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">else</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>end<span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">getsize</span><span style="color: black;">&#40;</span>start<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">getsize</span><span style="color: black;">&#40;</span>end<span style="color: black;">&#41;</span>:
				<span style="color: #dc143c;">shutil</span>.<span style="color: black;">copyfile</span><span style="color: black;">&#40;</span>start,end<span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;File &quot;</span> +end+<span style="color: #483d8b;">&quot; has been updated<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			<span style="color: #dc143c;">shutil</span>.<span style="color: black;">copyfile</span><span style="color: black;">&#40;</span>start,end<span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;File &quot;</span> +end+<span style="color: #483d8b;">&quot; has been created<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
st = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
en = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>st<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Source directory does not exist!&quot;</span>
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>st<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>en<span style="color: black;">&#41;</span>:
	<span style="color: #dc143c;">os</span>.<span style="color: black;">mkdir</span><span style="color: black;">&#40;</span>en<span style="color: black;">&#41;</span>
update<span style="color: black;">&#40;</span>st,en<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>You can download it here:<br />
Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p>Most functions used in this script are easy to understand. You can find further information here:<br />
<a href="http://docs.python.org/lib/module-os.path.html" target="_blank">http://docs.python.org/lib/module-os.path.html</a><br />
<a href="http://www.python.org/doc/current/lib/module-shutil.html" target="_blank">http://www.python.org/doc/current/lib/module-shutil.html</a><br />
<a href="http://docs.python.org/lib/module-sys.html" target="_blank">http://docs.python.org/lib/module-sys.html</a></p>
<p><a class="a2a_button_wykop" href="http://www.addtoany.com/add_to/wykop?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Wykop" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/wykop.png" width="16" height="16" alt="Wykop"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_technorati_favorites" href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a><a class="a2a_button_digg" href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Digg" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_msdn" href="http://www.addtoany.com/add_to/msdn?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="MSDN" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/msdn.png" width="16" height="16" alt="MSDN"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_technotizie" href="http://www.addtoany.com/add_to/technotizie?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Technotizie" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technotizie.png" width="16" height="16" alt="Technotizie"/></a><a class="a2a_button_technet" href="http://www.addtoany.com/add_to/technet?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="TechNet" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technet.png" width="16" height="16" alt="TechNet"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;title=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" id="wpa2a_8">Share/Save</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.wojtekrj.net/2008/07/python-updater/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

