On 21/04/2012 09:35, Norman Dunbar wrote:
Morning all,

On 20/04/12 21:40, Dilwyn Jones wrote:
This all sounds very interesting and possibly a fairly straightforward
language for S*BASIC users to learn. I notice there's versions of Python
for Windows as well as Linux etc. Anyone know if a Python program
written on one platform such as Windows, be run on another such as
Linux?
Yes. There's a few ways to run a python program on Linux/Unix either by telling the python interpreter the name of the file:

    python filename.py

or by putting this as the first line in the program:

    #!/usr/bin/env python

then making it executable:

    chmod u+x filename.py

then simply calling it:

    ./filename.py

Under windows, only the first option is available and if the program contains the #!/usr/bin/env python line, it gets treated as a comment and completely ignored.

There's also a python interpreter which sits there and waits for you to type something in, then "compiles" and executes it.


> Guess if the programs are written and saved using a text editor
there's a chance this might be possible, although probably endian issues
might arise with numbers, for example?
There's no problem with endians or numbers etc, well, not those problems anyway! There are gotchas to watch out for with numbers:

    print 3 / 4
    0

The '/' operator is equivalent to integer DIV. If you want to get a floating point answer, you need to float one or both operands:

    print float(3) / float(4)
    0.75

or

    print float(3) / 4
    0.75

or, simply:

    print 3 / 4.0
    0.75

Python is pretty nifty in it's ability to coerce variables from one type to another, so in the above, it sees one float argument and coerces the int argument into float and gives back a float answer.

It doesn't coerce strings to floats, or ints:

    print '313' + 300 + '13'

    Traceback (most recent call last):
      File "<pyshell#14>", line 1, in <module>
        print '313' + 300 + '13'
    TypeError: cannot concatenate 'str' and 'int' objects

But you can do it implicitly:

    print int('313') + 300 + int('13')
    626

And not necesarily in base 10 either:

    print int('313', 16) + 300 + int('13', 8)
    1098

Which is obviously 787 + 300 + 11.


Admittedly I know nothing about Python (yet... - it looks interesting)
You may not know Python yet, but you are using it frequently! Calibre, your most favourite program of recent times, is written in Python. So you can see it's a capable programming language.

The use of indents in interesting. You do do this in Python:

    #!/usr/bin/env python

    Dilwyn = 'Jones'
    Tony = 'Firshman'
    Malcolm = 'Cadman'

    if (Dilwyn == Tony):
            print "Tony and Dilwyn and the same person!"
    elif (Tony == Malcolm):
            print "Clones are people two!"
    else:
            print "Everyone is an individual."

The colons mark the start of a block, which must be indented (4 spaces is the Python standard). The block ends when the indent comes back out.

Typing the file above using into vi was interesting as it understands the indentation and did it for me automagically, probably based on the file name (ql.py).

In case anyone is wondering, the "else" clause is executed.

Arrays are the usual stuff but are called lists:

    ql_people = ["Dilwyn", "Tony", "Marcel", "Jochen", ]

To print them out, for example:

    for person in ql_people:
        print "This person is: $s" % person

They can also be dictionaries. These are like lists, but hava an access key, and use different open/close brackets:

    ql_people = {'Jones': 'Dilwyn', "Firshman": "Tony"}
    print ql_people['Jones']
    Dilwyn

So, you could use a dictionary to define a "record" of some sort:

    dj = {'name': 'Dilwyn Jones',
          'age': 32,
          'nationality': 'Welsh',
          'lives in': 'Tal-y-Bont'}

    print dj
    {'nationality': 'Welsh', 'age': 32, 'name': 'Dilwyn Jones',
    'lives in': 'Tal-y-Bont'}

    print dj['lives in']
    Tal-y-Bont

    print dj['name'], dj['age']
    Dilwyn Jones 32

You can even add functions to dictionaries. Then, a step up from dictionaries is a class. But I'll not bother with that, I think I've warbled on long enough!


For Python beginners there are a couple of decent books, one of which I got free from Amazon for my Kindle:

Hello Python: http://www.amazon.co.uk/Hello-Python-Anthony-Briggs/dp/1935182080/ref=sr_1_1?s=books&ie=UTF8&qid=1334995546&sr=1-1

Treading On Python Volume 1: http://www.amazon.co.uk/Treading-Python-Volume-1-ebook/dp/B00639H0AK/ref=sr_1_8?s=digital-text&ie=UTF8&qid=1334995630&sr=1-8

And a free online Python programming course: http://learnpythonthehardway.org/book/


It's quite an easy, neat language with many decent features, and (sorry Tony) far easier on the eye than Perl!

Linux users probably already have Python 2.x installed. Python 3.0 is coming/available but changes quite a lot.

Windows users can get an installer from http://python.org/download.

Have fun. ;-)

...


I think QLiberator at least can compile without line numbers (never
actually tried that). Perhaps George could tell us if Turbo can too.
I though Turbo read the source code from the loaded program in SuperBasic? So it won't be able to compile a file without line numbers because when you load one of those, it executes.

Doesn't QLiberator do the same thing? I don't remember being able to liberate a file, only the loaded program?


Cheers,
Norm.

PS. By the time you read this, I'll be elbow deep is fish poo! Today is the day I get to clean out the filters on the Koi pond. For such a few fish, they certainly crap a lot! :-(

Hi,

Why get the source to Python and produce a specific version for Sbasic.

There are already versions of Python written for Java (jPython) and .NET (IronPython).

I suppose we could produce sPython, which would run / compile Sbasic source code. But there would have be a facility to add extensions and Toolkits to it.

I think this would be alot easier than a full blown emulator.

I use Mint 12, which has Python 3 built in on installation, been using this for a while now, all very nice and very similiar to Sbasic/Superbasic.

Derek

_______________________________________________
QL-Users Mailing List
http://www.q-v-d.demon.co.uk/smsqe.htm

Reply via email to