<?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>thinkMoult &#187; Python</title>
	<atom:link href="http://thinkmoult.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkmoult.com</link>
	<description>Seriously who ever reads this description.</description>
	<lastBuildDate>Sun, 22 Jan 2012 04:58:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A Little Python Fun</title>
		<link>http://thinkmoult.com/2009/01/04/a-little-python-fun/</link>
		<comments>http://thinkmoult.com/2009/01/04/a-little-python-fun/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 06:07:53 +0000</pubDate>
		<dc:creator>Dion Moult</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[kde]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[pyqt]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[qt]]></category>

		<guid isPermaLink="false">http://thinkmoult.com/?p=360</guid>
		<description><![CDATA[When I last touched Python, I wrote a snippet to steal the latest comic off xkcd.com. Like most of the things that I do (this only applies to individual projects, not teamwork), there are long breaks until I make any progress. Probably it&#8217;s because I multi-task too much. At the same time, I never run [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>When I last touched Python, I wrote <a href="http://thinkmoult.com/2008/07/21/python-steals-xkcd-comics-snippet/">a snippet to steal the latest comic off xkcd.com</a>. Like most of the things that I do (this only applies to individual projects, not teamwork), there are long breaks until I make any progress. Probably it&#8217;s because I multi-task too much. At the same time, I never run out of things to do. Recently I&#8217;ve been blessed with a couple hours of fre etime, and so I&#8217;ve touched Python again, and decided it was time to learn some GUI.</p>
<p>Which toolkit? The Qt one of course (pronouned &#8216;cute&#8217;)! Powerful, cross-platform, native to KDE. Here&#8217;s my first (well first mentionable) GUI application coded with Python and the PyQt4 bindings. (Last mentionable GUI stuff was with VB6 &#8211; <em>Yes, I did do application programming when I was 9 years old</em>, <em>childish programs they were, but mentionable</em>).</p>
<p>The application is a simple text editor which supports opening/saving files, and warns you if you&#8217;ve made changes but haven&#8217;t saved yet. Yes, the process of making it was from a tutorial, though each step was figured out myself. Basically the tutorial says &#8220;Ok, now we&#8217;ll add a save button&#8221;, then I search the Python and PyQt4 documentation to figure out how, then cross reference my code with the tutorial. Here&#8217;s a screenshot:</p>
<p style="text-align: center;"><img class="size-full wp-image-361 aligncenter" title="2008-12-28-175239_1280x800_scrot" src="http://thinkmoult.com/wp-content/uploads/2008/12/2008-12-28-175239_1280x800_scrot.png" alt="2008-12-28-175239_1280x800_scrot" width="530" height="351" /></p>
<p>For the geeky, here is the code:</p>
<pre style="white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;">import sys

from PyQt4 import QtCore, QtGui
from editor import Ui_notepad

class StartQT4(QtGui.QMainWindow):

  def __init__(self, parent=None):

    QtGui.QWidget.__init__(self, parent)
    self.ui = Ui_notepad()
    self.ui.setupUi(self)

    # Connect signals

    QtCore.QObject.connect(self.ui.button_open,QtCore.SIGNAL("clicked()"), self.file_dialog)
    QtCore.QObject.connect(self.ui.button_save,QtCore.SIGNAL("clicked()"), self.file_save)
    QtCore.QObject.connect(self.ui.editor_window,QtCore.SIGNAL("textChanged()"), self.enable_save)

  def file_dialog(self):

    response = False

    if self.ui.button_save.isEnabled() and self.filename:

      message = QtGui.QMessageBox(self)

      message.setIcon(QtGui.QMessageBox.Question)
      message.addButton('Save', QtGui.QMessageBox.AcceptRole)
      message.addButton('Discard', QtGui.QMessageBox.DestructiveRole)
      message.addButton('Cancel', QtGui.QMessageBox.RejectRole)
      message.setText('Do you want to save changes?')
      message.setDetailedText('Unsaved changes in file: ' + str(self.filename))
      message.setWindowTitle('Notepad')

      message.exec_()

      response = message.clickedButton().text()

      if response == 'Save':

        self.file_save()
        self.ui.button_save.setEnabled(False)

      elif response == 'Discard':

        self.ui.button_save.setEnabled(False)

    if response != 'Cancel':

      fd = QtGui.QFileDialog(self)
      self.filename = fd.getOpenFileName()

      from os.path import isfile

      if isfile(self.filename):

        plik = open(self.filename).read()
        self.ui.editor_window.setText(plik)

        self.ui.button_save.setEnabled(False)

  def file_save(self):

    fd = QtGui.QFileDialog(self)
    self.filename = fd.getSaveFileName()

    from os.path import isfile

    if isfile(self.filename):

      plik = open(self.filename, 'w').write(self.ui.editor_window.toPlainText())

      self.ui.button_save.setEnabled(False)

  def enable_save(self):

    self.ui.button_save.setEnabled(True)

if __name__ == "__main__":

  app = QtGui.QApplication(sys.argv)
  myapp = StartQT4()
  myapp.show()
  sys.exit(app.exec_())</pre>
<p>Tada. You will also need another file called <a href="http://thinkmoult.com/wp-content/uploads/2008/12/editor.py">editor.py</a> (click to download) in the same directory from which the UI information will be grabbed from.</p>
<p>Ok, next Python project will be more advanced, which&#8217;ll be a graphical game of solitaire featuring custom widgets and drag and drop functionality. Oooh. Tough.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://thinkmoult.com/2009/01/04/a-little-python-fun/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python steals XKCD Comics &#8211; snippet</title>
		<link>http://thinkmoult.com/2008/07/21/python-steals-xkcd-comics-snippet/</link>
		<comments>http://thinkmoult.com/2008/07/21/python-steals-xkcd-comics-snippet/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 01:31:00 +0000</pubDate>
		<dc:creator>Dion Moult</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[comics]]></category>
		<category><![CDATA[lol]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[xkcd]]></category>

		<guid isPermaLink="false">http://thinkmoult.com/?p=44</guid>
		<description><![CDATA[Useful information stuff: After 70-640, the students who are enrolled for 640-822 start preparing for their 70-236 as well as 640-863 so that they can be exempted from 642-436 and 1Y0-259 and can appear directly in 70-536, saving time and resources. When somebody asks a programmer: Dude, why do you program? There&#8217;s nothing useful that [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p><span style="color: #c0c0c0;"><em>Useful information stuff:</em><br />
After <a href="http://www.testking-questions.com/exam/70-640.htm">70-640</a>, the students who are enrolled for <a href="http://www.testking-questions.com/exam/640-822.htm">640-822</a> start preparing for their <a href="http://www.testking-questions.com/exam/70-236.htm">70-236</a> as well as <a href="http://www.testking-questions.com/exam/640-863.htm">640-863</a> so that they can be exempted from <a href="http://www.testking-questions.com/exam/642-436.htm">642-436</a> and <a href="http://www.testking-questions.com/exam/1Y0-259.htm">1Y0-259</a> and can appear directly in <a href="http://www.testking-questions.com/exam/70-536.htm">70-536</a>, saving time and resources.</span></p>
<p>When somebody asks a programmer:<a href="http://xkcd.com"><img class="alignright" title="XKCD" src="http://imgs.xkcd.com/static/xkcdLogo.png" alt="XKCD Comics" width="185" height="83" /></a></p>
<blockquote><p>Dude, why do you program? There&#8217;s nothing useful that you can make that hasn&#8217;t been made already. It&#8217;s like making your own hand-phone when you could just buy it at a shop. Go get a life and do something productive.</p></blockquote>
<p>Then of course, the programmer smiles and replies:</p>
<blockquote style="text-align: left;"><p>Why, for:</p>
<ul>
<li>The money</li>
<li>The girls (&#8220;<em>ooh that looks terribly complex&#8230;&#8221;, </em>&#8220;<em>oh yes, it is&#8230;</em>&#8220;)</li>
<li>Nobody can check whether or not you&#8217;re really doing your work.</li>
</ul>
</blockquote>
<p>However, I personally think that programmers do it because they like to communicate with their computer. Since I&#8217;m learning Python, I like to learn it through making small things to speed up my day. I&#8217;ve made a blackjack game (OK, that slows down my day), a program that executes series of shell commands to speed up boring tasks, a to-do list program, and my latest creation:</p>
<p><strong>&#8220;Something-that-finds-the-latest-comic-on-xkcd.com-and-downloads-it-to-a-file&#8221;</strong></p>
<p>Of course, all you need to do is setup a cron-job to execute the snippet every time XKCD updates (Mondays, Wednesdays, Fridays) and bingo, you&#8217;ve just got yourself a personal archive of missed XKCD comics!</p>
<p>Here there be snippet:</p>
<blockquote>
<pre>#!/usr/bin/env python
import urllib
source = urllib.urlopen('http://xkcd.com/').read()
linebyline = source.splitlines()
found = 0
for value in linebyline:
    if found == 0:
        check = value.find('http://imgs.xkcd.com/comics/')

        if check != -1:
            found = 1
            # find the next occurance of the " to find end of URL.
            next = value.find('"', 10)
            image = value[check:next]
            length = len('http://imgs.xkcd.com/comics/')
            print 'Comic found: ' + image
            length = length + check
            filename = value[length:next]
            print 'Saved under: ' + filename
            path = '/home/dion/documents/Projects/Python/' # change this!
            image_file = urllib.urlretrieve(image, path + filename)

    else:
        break</pre>
</blockquote>
<p>Amazing, isn&#8217;t it? Here&#8217;s the latest one I grabbed:<img class="aligncenter" title="Impostor" src="http://imgs.xkcd.com/comics/impostor.png" alt="" width="591" height="248" /></p>
<p>&#8230;and <em>oh yes, it was terribly complicated</em>.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://thinkmoult.com/2008/07/21/python-steals-xkcd-comics-snippet/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

