Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lf-araujo/da038d0a71ace2fe21f7f3d291d4fcf4 to your computer and use it in GitHub Desktop.
Save lf-araujo/da038d0a71ace2fe21f7f3d291d4fcf4 to your computer and use it in GitHub Desktop.
Differences between Genie, Vala and Python
*This is part of a series of posts about the Genie programming language. I am updating my progress in learning the language from scratch in the hopes it will be useful for anyone also trying to get to programming in the linux/Gtk world.*
The main scientific programming languages are R and Python. These, however, are slow languages for general development. Recently I found out about Vala and it's python-like sister language Genie. It is a joy to learn and to use. Below I make a few comparisons between Python, Vala and Genie.
Vala seems to have a syntax similar to Java and C#, which are popular languages. Taking from the number of questions in StackExchange, I suppose it is becoming itself very popular for this reason. It is translated to C code trough the vala compiler and from C code to the binaries. That means Vala is a simpler syntax for C, which comes with all the advantages of that known, widely used, reliable language. The fun finding comes now. Their authors (not sure if the very same people, but a Gnome project nevertheless) have created Genie as a sister language. Another syntax to simplify the way one can write C code. Guess whose syntax it inherited? Python's. How amazing is that? In principle, anyone can use its knowledge with Python and the pythonic way of doing things and use it to write fast and powerful C applications. This post (and maybe some other future ones) will be about my attempt to find out whether it is easy or not, and how similar is Genie to Python.
I will take some exercises from the collection of introductory texts on Learning Python produced by the OpenCircle magazine and translate their code to Geanie. I compiled all the texts in a single e-book if you wantto take a look at [it](http://cur.lv/qqld6). The task of translating it into Genie code is being easier than I initially thought, however wether the language is really pythonic is out for the jury to decide.
## Hello world!
The first exercise is to look how a Hello world little program looks like in Genie. Well, here is a hello world code for python and vala, for comparison.
**Python**
```python
# !/usr/bin/env pythonic
print 'Hello. I am a python program.'
name = raw_input("What is your name? ")
print "Hello there, " + name + "!"
```
**Vala**
```vala
class Demo.HelloWorld : GLib.Object {
public static int main(string[] args) {
stdout.printf("Hello, World\n");
return 0;
}
}
```
**Genie**
```
[indent=4]
init
print "Hello. I am a python program." print "What's your name?"
var name = stdin.read_line()
print "Hello there, " + name + "!"
```
The language certainly needs more typing, however it looks to me similar to python and more readable than vala.
## Using print()
It is not as straightforward as in python, here is the py code:
`python print 2+2`
**In Genie:**
```
[indent=4]
init
var i = 2 + 2
print i.to_string()
```
## Making a loop
**In python**
```python
# ! /usr/bin/env python
for cntr in range(0,10):
print cntr
print 'All Done'
```
**In Genie**
```
[indent=4]
init
for var I = 1 to 10
print I.to_string()
print("All done!")
```
## A for loop and a password routine
**Python code**
```python
# \-----------------------------------------------
# password_test.py
# example of if/else, lists, assignments,raw_input,
# comments and evaluations
# \-----------------------------------------------
# Assign the users and passwords
users = ['Fred','John','Steve','Ann','Mary']
passwords = ['access','dog','12345','kids','qwerty']
# \-----------------------------------------------
# Get username and password
usrname = raw_input('Enter your username => ')
pwd = raw_input('Enter your password => ')
# \-----------------------------------------------
# Check to see if user is in the list
if usrname in users:
position = users.index(usrname) #Get the position in the list of the users
if pwd == passwords[position]: #Find the password at position
print 'Hi there, %s. Access granted.' % usrname
else:
print 'Password incorrect. Access denied.'
else:
print "Sorry...I don't recognize you. Access denied."
```
Now in Genie, using an array of string (it does not work adequately):
```
[indent=4]
init
users: array of string = {"Fred","John","Steve","Ann","Mary"}
passwords: array of string = {"access","dog","12345","kids","qwerty"}
print "Enter user name"
usrname:string = stdin.read_line()
print "Enter password"
pwd:string = stdin.read_line()
position:int = 0
for var user in users
if (user==usrname)
if pwd == passwords[position]
print "Hi there, %s. Access granted.", usrname
else
print "Password incorrect. Access denied."
else
print "Sorry...I don't recognize you. Access denied."
position++
```
Now a proper approach to the same problem, this time using a dictionary in
Genie:
```
[indent=4]
init
var access = new dict of string,string
access[ "Fred" ] = "access"
access[ "John" ] = "dog"
access[ "Steve" ] = "12345"
access[ "Ann" ] = "kids"
access[ "Mary" ] = "qwerty"
print "Enter user name"
username:string = stdin.read_line()
print "Enter password"
pwd:string = stdin.read_line()
if !(username in access.keys)
print "Sorry...I don't recognize you. Access denied."
else
if pwd == access[ username ]
print "Hi there, %s. Access granted.", username
else
print "Password incorrect. Access denied."
```
I had the help of very nice people from [StackExchange](http://stackoverflow.com/questions/32907351/fixing-a-for-loop-in-genie/32911282#32911282) to solve this problem.
Well, all in all, Genie is easy to write and read. Not as easy as python, however a lot easier than its sister Vala. Maybe I will make some more of these exercises. One thing that struck me was how hard it is to find information and documentation on genie. On the other hand, some fellows answered my questions at StackExchange almost immediately.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment