Why you should write a port of SLANGFOR.NET in any language

Have you ever wondered about how write a compiler? and still you don’t have any idea about it? If your answer is ‘Yes’ this article is for you.
I was also very curious about it and feared reading Dragon books :P. But i met a guy called Praseed Pai(http://praseedp.blogspot.com) who just told me that ‘Compiler is just another program’ :). He started a project in 2010 called SLANGFOR.net which is hosted at codeplex(http://slangfordotnet.codeplex.com). This project is an attempt to teach compiler construction by writing a small compiler. It is written in C#, and documented well to understand. Documentation is orgonized in 7 steps. Initial steps are dealing with arithmetic expressions and following steps play with variable declaration, assignment operations, control structures like conditions and loops and finally function. It will not help you to understand big theories behind compiler construction and all. but i swear at least you will get a confidence to write your own DSL.

Recently i ported SLANGFOR.net to Python. only interpreter part, still need to write a backend. project is hosted on github (https://github.com/faisalp4p/slang-python)
I have leaned lot of things by doing this. such as Lexical Analysis, AST, Recursive Descent Parsing, DSL. I took only six days to port it. may be because of the simplicity of python. After that we discussed about it and he gave some more ideas like how we can change it to support dictionaries and Object Oriented programming.

So i recommend you to port SLANGFOR.net to any language if you want to learn
some lessons in compiler construction.

PyQt/PySide – Music Player in 10 minutes

Nowadays creating an application is not a tedious task. everyone can create one with little effort.

I am going to tell you about how i wrote a simple music player in 10 minute using python.

Qt
Qt is a Cross platform C++ application development framework. K Desktop environment (KUBUNTU) is made using Qt. Qt provides interface for GUI Programming, Database, Threading, Networking, OpenGL etc. I never seen such a good cross platform framework. It support both Desktop and Mobile platforms.

Python + Qt = PyQt

We can do Qt application development using Python. Because python is such a language we can extend its feature by binding existing C, C++ libraries. So i am going to tell you how i created a music player with a little time using PySide.

Note :  There is PyQt and PySide. Both are different bindings for Qt framework. No much difference between them. PySide not support Qt5 but PyQt. for the sake of understanding tutorial consider PySide==PyQt.

Requirements:

QtCreator – To create interface (GUI)
PySide – Python package to develop music player
pyside-tools – To convert GUI file(.ui) to .py
pyside-phonon  – Manage audio files

1. Start Desiner Form using QtCreator. Name it ‘musicplayerUI.ui

2. Design a form for the music player. mine was:

snap2

3) Convert musicplayerUI.ui to musicplayerUI.py using the following command:

pyside-uic musicplayerUI.ui > musicplayerUI.py

4) Create a python file called “musicplayer.py” to implement music player, which contains:

import sys
from PySide.QtCore import *
from PySide.QtGui import *
from musicplayerUI import *

from PySide.phonon import Phonon

class AudioPlayer(QMainWindow, Ui_MusicPlayer):

    def __init__(self, parent=None):

        super(AudioPlayer, self).__init__(parent)
        self.setupUi(self)
        self.actionOpen.triggered.connect(self.open)
        self.actionQuit.triggered.connect(self.quite)
        self.horizontalSlider.setValue(0)
        self.horizontalSlider.setEnabled(False)
        self.horizontalSlider.sliderReleased.connect(self.slider_value_change)
        self.button.clicked.connect(self.play_or_pause)
        self.button.setText("Play")
        self.button.setEnabled(False)
        self.media_obj = Phonon.MediaObject(self)
        self.current_time = 0

    def play_or_pause(self):
        if Phonon.State.PlayingState == self.media_obj.state():
            self.media_obj.pause()
            self.button.setText("Play")
        elif Phonon.State.PausedState == self.media_obj.state():
            self.media_obj.play()
            self.button.setText("Pause")

    def slider_value_change(self):
        value = self.horizontalSlider.value()
        print value
        self.media_obj.seek(value)

    def open(self):
        dialog = QFileDialog()
        dialog.setViewMode(QFileDialog.Detail)
        filename = dialog.getOpenFileName(self,
             'Open audio file', '/home',
             "Audio Files (*.mp3 *.wav *.ogg)")[0]
        self.audio_output = Phonon.AudioOutput(Phonon.MusicCategory, self)
        Phonon.createPath(self.media_obj, self.audio_output)
        self.media_obj.setCurrentSource(Phonon.MediaSource(filename))
        self.media_obj.tick.connect(self.time_change)
        self.media_obj.totalTimeChanged.connect(self.total_time_change)
        self.media_obj.play()
        self.button.setEnabled(True)
        self.button.setText("Pause")
        self.horizontalSlider.setEnabled(True)

    def time_change(self, time):
        if not self.horizontalSlider.isSliderDown():
            self.horizontalSlider.setValue(time)

    def total_time_change(self, time):
        self.horizontalSlider.setRange(0, time)

    def quite(self):
        sys.exit()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = AudioPlayer()
    window.show()
    sys.exit(app.exec_())

5. Run musicplayer.py file. You can play music. enjoy!

I think program is self explanatory. Leave a comment if you have any queries, may i can help you!

Source : https://github.com/faisalp4p/MusicPlayerDemo

Thanks!