So I had to learn Python

wonder

Expert
Licensed User
Longtime User
As part of my day-job (which unfortunately is not making videogames (yet!)), I had to learn Python.
The purpose was (mainly), replace our Bash scripts with Python ones.

I was a little bit reluctant at first, but then OMG, PYTHON IS AWESOME!!!! :eek:

The syntax is really close to B4X, so it took me no more than a week to get the hang of it.
It's so easy and functional, programming goes like a breeze. Like you can write one million lines of code per hour! :D

If you're into scripting and automating stuff, I highly recommend you to have a look at it!

:)
 
Last edited:

imbault

Well-Known Member
Licensed User
Longtime User
I want to go Python either, @wonder , can you send some good web sites, good tutos... please

Thx

Patrick
 

wonder

Expert
Licensed User
Longtime User
Last edited:

wonder

Expert
Licensed User
Longtime User
So far, I've only been using Python to write command-line based scripts.
There are, however, like, 'one million libraries' to choose from, so you might find what you're looking for. :)

https://wiki.python.org/moin/GuiProgramming
 

imbault

Well-Known Member
Licensed User
Longtime User
@wonder, do you have (by chance) a more sophisticated and complex example showing the power, elegance and simplicity (in number of line codes) of this language,
oriented in scripting and automating (like you said in your first thread)

Thanks again
 

wonder

Expert
Licensed User
Longtime User
@wonder, do you have (by chance) a more sophisticated and complex example showing the power, elegance and simplicity (in number of line codes) of this language,
oriented in scripting and automating (like you said in your first thread)

Thanks again
Python doesn't look like much until you really start using it.

In my case, I'm using it to parse non-standard proprietary (mainframe) database reports into HTML / XML files.
Since I'm not authorized to use B4J (I tried...), Python is the next best thing. :)

I think the most powerful feature (so far) is the simplicity regarding the usage of classes and object-oriented programming.
 
Last edited:

RandomCoder

Well-Known Member
Licensed User
Longtime User
I've not actually had any need to use Python but did for a while start to learn the syntax as my son was studying it and I wanted to be able to help if needed. The language seems pretty easy to pick up although indenting loops and conditional statements felt strange with no need to close them off. The one thing that stood out and I liked the most was introspection, the ability to find out about a function of class and it's use.... https://www.learnpython.org/en/Code_Introspection

The site linked above is very good as is "learning Python the hard way", look it up I promise it's not as bad as it sounds ;)
 

eurojam

Well-Known Member
Licensed User
Longtime User
@wonder, do you have (by chance) a more sophisticated and complex example showing the power, elegance and simplicity (in number of line codes) of this language,
oriented in scripting and automating (like you said in your first thread)

Thanks again

Hi Patrick,

here is an example how to build a webserver with python: https://ruslanspivak.com/lsbaws-part1/
it is only 16 lines of code....awesome...nearly as perfect as B4x;-)

best
stefan
 

thedesolatesoul

Expert
Licensed User
Longtime User
Python is really nice and refreshing, and it saves so much time on syntax errors!

Example:
B4X:
#This is Python 2.7.x
words = ['Hello', 'world']
str = ''
for word in words:
    str = str + word + ' '
print str + '!'

This is nice, just another way to write this (without the loop):

B4X:
words = ['Hello', 'world']
words_to_print = words + ['!']
print ' '.join(words_to_print)

For B4X users I'd recommend learning the following:
- list comprehension (saves a lot of loops and conditionals)
- generators
- decorators

Also become familiar with lists and dictionaries as you will use them everywhere. (Things like zip, enumerate etc)
Their standard libraries provide a large amount of functionality itertools, functools, os, sys, re.

For web server stuff, definitely try out Flask or Django as these are skills that are also in demand in the market.
 

Daestrum

Expert
Licensed User
Longtime User
You can run Jython (java equiv of python 2.7) scripts inside B4J apps using my latest library.
 

Daestrum

Expert
Licensed User
Longtime User
Did they implement an interpreter in java? How fast is it and does it support all the python libs?

It's implemented as a scriptengine - same as nashorn.
There is a slight delay upon loading the script as it gets compiled to java, but then the functions are invoked from the compiled version.
Not tried any speed tests to compare to python(real).
some of the libs don't work - google Jython - they explain what works and what doesn't.

just tried simple loop to get some times
B4X:
def test8():
b = 0
for a in range(1,1000000):
b = b + a
return b

Takes 0.225 seconds
 
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
Not bad.
Though ideally we should be running the tests on the same machines.

time python test2.py
0.104906082153
0.103u 0.029s 0:00.14 85.7% 0+0k 0+0io 0pf+0w

B4X:
import time

def test8():
    b = 0
    for a in range(1,1000000):
        b = b + a
    return b

a = time.time()
test8()
b = time.time()
print b-a
 

Daestrum

Expert
Licensed User
Longtime User
Though ideally we should be running the tests on the same machines.

True, plus I did the start / end time inside the b4j app.
Mine ran on i5 3.2Ghz 8GB ram java9 (lib compiled against java 8-121)

As an aside but sort of related - I have jRuby running too now. :)
 
Top