<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>delor's notes</title>
 <link href="http://delor.github.com/atom.xml" rel="self"/>
 <link href="http://delor.github.com/"/>
 <updated>2010-09-13T14:50:26-07:00</updated>
 <id>http://delor.github.com/</id>
 <author>
   <name>Bartłomiej Piech</name>
   <email>bartek.piech@delor.pl</email>
 </author>

 
 <entry>
   <title>Mądrości</title>
   <link href="http://delor.github.com/2010/02/06/madrosci/"/>
   <updated>2010-02-06T00:00:00-08:00</updated>
   <id>http://delor.github.com/2010/02/06/madrosci</id>
   <content type="html">&lt;p&gt;Chroń dupę papierem.&lt;/p&gt;

&lt;p&gt;Kto często sejwuje ten mniej pracuje.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Obsługa wejścia/wyjścia i systemu plików</title>
   <link href="http://delor.github.com/pas/2010/02/03/obsluga-wejscia-wyjscia-i-systemu-plikow/"/>
   <updated>2010-02-03T00:00:00-08:00</updated>
   <id>http://delor.github.com/pas/2010/02/03/obsluga-wejscia-wyjscia-i-systemu-plikow</id>
   <content type="html">&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.python.org/library/functions.html&quot;&gt;Wbudowane operacje we/wy&lt;/a&gt; cd.

&lt;ul&gt;
&lt;li&gt;raw_input&lt;/li&gt;
&lt;li&gt;open&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.python.org/library/stdtypes.html#file-objects&quot;&gt;metody plikowe&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;close, flush, read, readline, readlines, seek, tell, write, writelines&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.python.org/library/os.html#module-os&quot;&gt;Moduł os&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;chdir, mkdir, makedirs, remove, unlink, removedirs, rename, rmdir, stat&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.python.org/library/os.path.html#module-os.path&quot;&gt;Moduł os.path&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;abspath, basename, dirname, exists, expanduser, splitext&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://diveintopython.org/scripts_and_streams/command_line_arguments.html&quot;&gt;Obsługa argumentów linii poleceń&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;moduł &lt;a href=&quot;http://docs.python.org/library/getopt.html#module-getopt&quot;&gt;getopt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;moduł &lt;a href=&quot;http://docs.python.org/library/optparse.html#module-optparse&quot;&gt;optparse&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;div class=&quot;question&quot;&gt;
  &lt;p&gt;Napisać program wyświetlający na standardowym wyjściu plik zadany w
  wierszu poleceń.&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;a href=&quot;http://docs.python.org/library/stdtypes.html#file-objects&quot;&gt;
        File Objects&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;http://diveintopython.org/file_handling/file_objects.html&quot;&gt;
        Dive Into Python - Working with File Object&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;




&lt;div class=&quot;answer&quot;&gt;
&lt;pre class=&quot;brush: python; collapse: true&quot;&gt;
import sys

file = sys.argv[1]
file = open(file)
sys.stdout.write(file.read())
file.close()
&lt;/pre&gt;
&lt;/div&gt;




&lt;div class=&quot;question&quot;&gt;
  &lt;p&gt;Napisać program kopiujący plik zadany pierwszym parametrem wiersza poleceń
  na plik zadany drugim parametrem wiersza poleceń.&lt;/p&gt;
&lt;/div&gt;




&lt;div class=&quot;answer&quot;&gt;
&lt;pre class=&quot;brush: python; collapse: true&quot;&gt;
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


try:
    source_file = sys.argv[1]
    destintion_file = sys.argv[2]

    source_file = open(source_file, 'r')
    destintion_file = open(destintion_file, 'w')

    destintion_file.write(source_file.read())
except IndexError, err:
    print &gt;&gt; sys.stderr, err
except IOError, err:
    print &gt;&gt; sys.stderr, err
&lt;/pre&gt;
&lt;/div&gt;




&lt;div class=&quot;question&quot;&gt;
  &lt;p&gt;Pliki mp3 oprócz muzyki mogą zawierać dodatkowe informacje o utworze w
     postaci etykiet (tagów) typu ID3v1 lub ID3v2. Etykieta ID3v1 zajmuje
     ostatnie 128 bajtów w pliku i zawiera następujące informacje
     (w tej kolejności):&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;3 bajty - napis „TAG”&lt;/li&gt;
    &lt;li&gt;30 bajtów - tytuł utworu&lt;/li&gt;
    &lt;li&gt;30 bajtów - artysta&lt;/li&gt;
    &lt;li&gt;30 bajtów - tytuł albumu&lt;/li&gt;
    &lt;li&gt;4 bajty - rok (jako czteroliterowy napis)&lt;/li&gt;
    &lt;li&gt;30 bajtów - komentarz&lt;/li&gt;
    &lt;li&gt;1 bajt - gatunek&lt;/li&gt;
  &lt;/ul&gt;
  &lt;p&gt;Pola są uzupełniane znakami pustymi (ASCII 0) do wymaganej długości.&lt;/p&gt;
  &lt;p&gt;Napisać program, który dostaje w parametrze nazwę pliku. Jeżeli plik
     zawiera etykietę ID3v1 to program wyświetla informacje o utworze.&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;ID3.org, ID3v1&lt;/li&gt;
    &lt;li&gt;Kody gatunków ID3&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;




&lt;div class=&quot;answer&quot;&gt;
&lt;pre class=&quot;brush: python; collapse: true&quot;&gt;
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


genres = { 0: 'Blues', 1:'Classic Rock', 2: 'Country', 3: 'Dance', 4: 'Disco',
          5: 'Funk', 6: 'Grunge', 7: 'Hip-Hop', 8: 'Jazz', 9: 'Metal',
          10: 'New Age', 11: 'Oldies', 12: 'Other', 13: 'Pop', 14: 'R&amp;B',
          15: 'Rap', 16: 'Reggae', 17: 'Rock', 18: 'Techno', 19: 'Industrial',
          20: 'Alternative', 21: 'Ska', 22: 'Death Metal', 23: 'Pranks',
          24: 'Soundtrack', 25: 'Euro-Techno', 26: 'Ambient', 27: 'Trip-Hop',
          28: 'Vocal', 29: 'Jazz+Funk', 30: 'Fusion', 31: 'Trance',
          32: 'Classical', 33: 'Instrumental', 34: 'Acid', 35: 'House',
          36: 'Game', 37: 'Sound Clip', 38: 'Gospel', 39: 'Noise',
          40: 'AlternRock', 41: 'Bass', 42: 'Soul', 43: 'Punk', 44: 'Space',
          45: 'Meditative', 46: 'Instrumental Pop', 47: 'Instrumental Rock',
          48: 'Ethnic', 49: 'Gothic', 50: 'Darkwave', 51: 'Techno-Industrial',
          52: 'Electronic', 53: 'Pop-Folk', 54: 'Eurodance', 55: 'Dream',
          56: 'Southern Rock', 57: 'Comedy', 58: 'Cult', 59: 'Gangsta',
          60: 'Top 40', 61: 'Christian Rap', 62: 'Pop/Funk', 63: 'Jungle',
          64: 'Native American', 65: 'Cabaret', 66: 'New Wave',
          67: 'Psychadelic', 68: 'Rave', 69: 'Showtunes', 70: 'Trailer',
          71: 'Lo-Fi', 72: 'Tribal', 73: 'Acid Punk', 74: 'Acid Jazz',
          75: 'Polka', 76: 'Retro', 77: 'Musical', 78: 'Rock &amp; Roll',
          79: 'Hard Rock' }

try:
    mp3_file = sys.argv[1]
    mp3_file = open(mp3_file)
    mp3_file.seek(-128, 2)
    #print mp3_file.read(128)
    #sys.exit(1)
    tag = mp3_file.read(3)
    print tag
    if tag in ('TAG', 'ID3'):
        tag = {}
        tag['title'] = mp3_file.read(30).strip()
        tag['artist'] = mp3_file.read(30).strip()
        tag['album'] = mp3_file.read(30).strip()
        tag['year'] = mp3_file.read(4).strip()
        tag['comment'] = mp3_file.read(30).strip()
        genre = ord(mp3_file.read(1))
        try:
            tag['genre'] = genres[genre]
        except KeyError:
            tag['genre'] = 'undefined/unknown'
        print tag
        print &quot;&quot;&quot;
        title = %(title)s
        artist = %(artist)s
        album = %(album)s
        year = %(year)s
        comment = %(comment)s
        genre = %(genre)s
        &quot;&quot;&quot; % tag

except IndexError, err:
    print err
except IOError, err:
    print err
&lt;/pre&gt;
&lt;/div&gt;




&lt;div class=&quot;question&quot;&gt;
  &lt;p&gt;Napisać program dostający w parametrze wiersza poleceń nazwę katalogu
     i wyświetlający na standardowym wyjściu wszystkie podkatalogi znajdujące
     się bezpośrednio w nim - każdy w osobnym wierszu. W przypadku jakiegoś
     błędu odpowiedni komunikat powinien zostać wyświetlony na standardowym
     wyjściu błędów i program powinien zakończyć się z kodem błędu różnym od
     zera.&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;Files and Directories&lt;/li&gt;
    &lt;li&gt;Moduł os.path&lt;/li&gt;
    &lt;li&gt;Dive Into Python (Working with Directories) (po polsku)&lt;/li&gt;
    &lt;li&gt;Errors and Exceptions&lt;/li&gt;
    &lt;li&gt;Funkcja sys.exit()&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;




&lt;div class=&quot;question&quot;&gt;
  &lt;p&gt;Zmodyfikować program z zadania nr 4 tak, aby w przypadku otrzymania opcji
     -r program wyświetlał także (najlepiej z odpowiednim wcięciem) podkatalogi
     wszystkich wyświetlanych katalogów.&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;$ ./my_ls.py kat
kat1
kat2
  podkat21
  podkat22
kat3
  podkat31
    podpodkat311
  podkat32
    podpodkat321
    podpodkat322
kat4&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;




&lt;div class=&quot;question&quot;&gt;
  &lt;p&gt;Napisać program wyświetlający na standardowym wyjściu informację o
     rozmiarze pliku zadanego w parametrze wiersza poleceń, np.:&lt;/p&gt;
  &lt;pre&gt;&lt;code&gt;$ ./rozm.py plik.txt
plik.txt 2345678&lt;/code&gt;&lt;/pre&gt;
  &lt;p&gt;Program obsługuje następujące opcje:&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;-h, --help - wyświetla krótką informację o tym co program robi i jak go
        używać (m. in. wymienia opcje), a następnie kończy działanie&lt;/li&gt;
    &lt;li&gt;-o plik - zapisuje informacje do pliku plik zamiast wyświetlać je na
        standardowym wyjściu&lt;/li&gt;
    &lt;li&gt;-s - informacja o rozmiarze wyświetlona jest w przybliżeniu i z
        odpowiednią jednostką (np. 234B, 23K, 345M, 4G)&lt;/li&gt;
    &lt;li&gt;-r - jeżeli w parametrze zostanie podany katalog, to wyświetlany jest
        łączny rozmiar plików w tym katalogu i jego podkatalogach&lt;/li&gt;
  &lt;/ul&gt;
  &lt;ul&gt;
    &lt;li&gt;Moduł getopt&lt;/li&gt;
    &lt;li&gt;Moduł optparse&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;




&lt;script type=&quot;text/javascript&quot;&gt;
    SyntaxHighlighter.all()
&lt;/script&gt;

</content>
 </entry>
 
 <entry>
   <title>Argumenty wiersza poleceń i zmienne środowiskowe</title>
   <link href="http://delor.github.com/pas/2010/01/30/argumenty-wiersza-polecen-i-zmienne-srodowiskowe/"/>
   <updated>2010-01-30T00:00:00-08:00</updated>
   <id>http://delor.github.com/pas/2010/01/30/argumenty-wiersza-polecen-i-zmienne-srodowiskowe</id>
   <content type="html">&lt;ul&gt;
&lt;li&gt;argumenty wiersza poleceń: lista &lt;a href=&quot;&quot; title=&quot;http://docs.python.org/library/sys.html#sys.argv&amp;quot; &amp;quot;sys.argv — System-specific parameters and functions — Python documentation&quot;&gt;sys.argv&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;zmienne środowiskowe: słownik os.environ&lt;/li&gt;
&lt;li&gt;Wbudowane operacje we/wy

&lt;ul&gt;
&lt;li&gt;print&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;moduł &lt;a href=&quot;&quot; title=&quot;http://docs.python.org/library/sys.html&amp;quot; &amp;quot;sys — System-specific parameters and functions — Python documentation&quot;&gt;sys&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Zadania&lt;/h2&gt;

&lt;div class=&quot;question&quot;&gt;
&lt;p&gt;Napisać program wyświetlający w czytelnej postaci swoją nazwę i wszystkie argumenty wiersza poleceń, np.:&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;
$ python program.py -a 'Cos tam'
nazwa programu: './program.py'
argument nr 1: '-a'
argument nr 2: 'Cos tam'
&lt;/code&gt;
&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://diveintopython.org/scripts_and_streams/command_line_arguments.html&quot;&gt;Dive Into Python - Handling command-line arguments&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;




&lt;div class=&quot;answer&quot;&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;nazwa programu:&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ilosc_argumentow&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numer&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ilosc_argumentow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;argument nr &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%i&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;




&lt;div class=&quot;question&quot;&gt;
&lt;p&gt;Napisać program, który dla każdej z zadanych w parametrach wiersza poleceń zmiennych środowiskowych wyświetli w osobnym wierszu napis postaci NAZWA=WARTOŚĆ, np.:&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;
$ env ABC=abc
$ ./program.py HOME ABC
HOME=/home/mklisow
ABC=abc
&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;Jeżeli któraś z wymienionych zmiennych nie jest zdefiniowana, to program ma tylko wyświetlić informację o tym na standardowym wyjściu błędów i zakończyć działanie z kodem błędu będącym niewielką liczbą całkowitą większą od zera.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://docs.python.org/library/os.html#process-parameters&quot;&gt;Process Parameters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://diveintopython.org/scripts_and_streams/stdin_stdout_stderr.html&quot;&gt;Dive Into Python - Standard input, output, and error&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;




&lt;div class=&quot;answer&quot;&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;os&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;zmienna&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zmienna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;os&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;environ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zmienna&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;KeyError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stderr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;#39;zmienna &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; nie jest ustawiona&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>Wstęp do programowania w języku Python</title>
   <link href="http://delor.github.com/pas/2010/01/18/wstep-do-programowania-w-jezyku-python/"/>
   <updated>2010-01-18T00:00:00-08:00</updated>
   <id>http://delor.github.com/pas/2010/01/18/wstep-do-programowania-w-jezyku-python</id>
   <content type="html">&lt;h2&gt;Python — uruchamianie interpretera&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;interaktywne: &lt;code&gt;python&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;wsadowe (nieinteraktywne): &lt;code&gt;python nazwa_pliku&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;automatyczne przez powłokę
pierwszy wiersz pliku: &lt;code&gt;#!/usr/bin/env python&lt;/code&gt; oraz &lt;code&gt;chmod a+x plik&lt;/code&gt; aby plik był uruchamialny&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Python — specyfika języka&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;komentarze &lt;code&gt;#&lt;/code&gt; jak w powłoce&lt;/li&gt;
&lt;li&gt;obowiązkowe wcięcia (zamiast begin...end) standard pep8 definiuje 4 spacje na wcięcie&lt;/li&gt;
&lt;li&gt;specyficzne operatory:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;**&lt;/code&gt; - potęga&lt;/li&gt;
&lt;li&gt;&lt;code&gt;*&lt;/code&gt; - mnożenie, powielanie&lt;/li&gt;
&lt;li&gt;&lt;code&gt;//&lt;/code&gt; - dzielenie całkowite&lt;/li&gt;
&lt;li&gt;&lt;code&gt;%&lt;/code&gt; - modulo, formatowanie napisów&lt;/li&gt;
&lt;li&gt;&lt;code&gt;+&lt;/code&gt; - dodawanie, łączenie&lt;/li&gt;
&lt;li&gt;&lt;code&gt;==&lt;/code&gt;, &lt;code&gt;!=&lt;/code&gt; - równe, różne&lt;/li&gt;
&lt;li&gt;&lt;code&gt;x &amp;gt; y &amp;gt;= z&lt;/code&gt; - łańcuchy porównań&lt;/li&gt;
&lt;li&gt;&lt;code&gt;in&lt;/code&gt;, &lt;code&gt;not in&lt;/code&gt; - należy, nie należy&lt;/li&gt;
&lt;li&gt;&lt;code&gt;and&lt;/code&gt;, &lt;code&gt;or&lt;/code&gt; (uwaga na zwracane wartości!)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;podstawienia z operacją: &lt;code&gt;+=&lt;/code&gt;, &lt;code&gt;-=&lt;/code&gt;, &lt;code&gt;*=&lt;/code&gt;, &lt;code&gt;/=&lt;/code&gt;, &lt;code&gt;%=&lt;/code&gt;, &lt;code&gt;**=&lt;/code&gt;, ...&lt;/li&gt;
&lt;li&gt;wszystko jest obiektem&lt;/li&gt;
&lt;li&gt;mocna kontrola zgodności typów danych (mimo dynamicznie ustalonego typu danej), konwersja do napisu (funkcja str)&lt;/li&gt;
&lt;li&gt;obowiązek inicjowania zmiennej&lt;/li&gt;
&lt;li&gt;specyficzne typy danych: słowniki, listy, krotki&lt;/li&gt;
&lt;li&gt;mapowanie i filtrowanie list&lt;/li&gt;
&lt;li&gt;moduły (&lt;code&gt;import moduł&lt;/code&gt;, &lt;code&gt;from moduł import obiekty&lt;/code&gt;, funkcja &lt;code&gt;dir&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;wyjątki

&lt;pre&gt;&lt;code&gt;try:
    # kod, który może spowodować wystąpienie wyjątku
except:
    # kod obsługujący wyjątek
else:
    # kod, który zostanie wykonany gdy nie wystąpił wyjątek
finally:
    # kod, który zawsze zostanie wykonany
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;raise&lt;/code&gt; wyrzuca wyjątek&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Zadania&lt;/h2&gt;

&lt;div class=&quot;question&quot;&gt;
&lt;p&gt;Napisać na trzy sposoby standardowy program wypisujący &quot;Hello world!&quot; (interaktywnie, wsadowo i jako skrypt).&lt;/p&gt;
&lt;/div&gt;




&lt;div class=&quot;question&quot;&gt;
&lt;p&gt;Napisać funkcję, która dla dwóch argumentów całkowitoliczbowych znajdzie ich największy wspólny dzielnik.&lt;/p&gt;
&lt;/div&gt;




&lt;div class=&quot;answer&quot;&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;nwd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;/div&gt;

</content>
 </entry>
 
 <entry>
   <title>Test</title>
   <link href="http://delor.github.com/2010/01/17/test/"/>
   <updated>2010-01-17T00:00:00-08:00</updated>
   <id>http://delor.github.com/2010/01/17/test</id>
   <content type="html">&lt;p&gt;Testing some stuff.&lt;/p&gt;

&lt;h2&gt;code generated with pyments&lt;/h2&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;foo&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;foo&amp;#39;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;with line numbers&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;&lt;span class=&quot;lineno&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;foo&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;2&lt;/span&gt;   &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;foo&amp;#39;&lt;/span&gt;
&lt;span class=&quot;lineno&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;embeded gist&lt;/p&gt;

&lt;script src=&quot;http://gist.github.com/284902.js?file=test.rb&quot;&gt;&lt;/script&gt;




&lt;pre class=&quot;brush: python&quot;&gt;
def foo():
    print 'bar'
&lt;/pre&gt;




&lt;div class=&quot;question&quot;&gt;
&lt;script type=&quot;syntaxhighlighter&quot; class=&quot;brush: py&quot;&gt;&lt;![CDATA[
def foo():
    print 'bar'
]]&gt;&lt;/script&gt;
&lt;/div&gt;




&lt;div class=&quot;answer&quot;&gt;
&lt;script type=&quot;syntaxhighlighter&quot; class=&quot;brush: py&quot;&gt;&lt;![CDATA[
def foo():
    print 'bar'
]]&gt;&lt;/script&gt;
&lt;/div&gt;




&lt;script type=&quot;text/javascript&quot;&gt;
    SyntaxHighlighter.all()
&lt;/script&gt;

</content>
 </entry>
 

</feed>

