Saturday, 24 July 2021

Python crash course part 8: file input and output

In today's post I'll provide some short samples of working with files in Python.

Let's begin with text files and let's create a file first.

# !/usr/bin/python3

def main():
    # create a file
    my_file = open('newfile.txt''w')
    # write some data to it:
    my_file.write("Anakin Skywalker")
    my_file.close()

    # contents of the file: 
    # Anakin Skywalker

if __name__ == "__main__":
    main()

The result of running this code is creating a file newfile.txt with content: Anakin Skywalker

'w' means that the we will write to the file, and if that file exits, it will be overwritten. Remember to call .close() on the file object after opening and doing some operation on it.

Let's extend our program, by calling the similar code again:

# !/usr/bin/python3

def main():
    # create a file
    my_file = open('newfile.txt''w')
    # write some data to it:
    my_file.write("Anakin Skywalker")
    my_file.close()

    # contents of the file: 
    # Anakin Skywalker

    # opening it again
    my_file = open('newfile.txt''w')
    my_file.write("Darth Vader")
    my_file.close()

    # contents of the file: 
    # Dath Vader

if __name__ == "__main__":
    main()

As the 'w' mode makes the write call overwrite the file contents, it will be just Darth Vader  after the call. What if we want to actually add something to the file, not deleting it contents?

That's what 'a' (append) mode is for:

# !/usr/bin/python3

def main():
    # create a file
    my_file = open('newfile.txt''w')
    # write some data to it:
    my_file.write("Anakin Skywalker")
    my_file.close()

    # contents of the file: 
    # Anakin Skywalker

    # opening it again
    my_file = open('newfile.txt''w')
    my_file.write("Darth Vader")
    my_file.close()

    # contents of the file: 
    # Dath Vader
    my_file = open('newFile.txt','a')
    my_file.write("\nLuke Skywalker")
    my_file.close()
    # contents:
    # Darth Vader
    # Luke Skywalker


if __name__ == "__main__":
    main()

Because 'a' mode is used instead of 'w', \nLuke Skywalker is added (appended) to the existing file. The \n before the text itself in the third write call is a new line sign between the first and second line. The contents of the file become:

Darth Vader
Luke Skywalker

We know how to write to file, but how to read from it? This is what open() with 'r' (read) parameter is for. We use it like this:

# !/usr/bin/python3

def main():
    # create a file
    my_file = open('newfile.txt''w')
    # write some data to it:
    my_file.write("Anakin Skywalker")
    my_file.close()

    # contents of the file: 
    # Anakin Skywalker

    # opening it again
    my_file = open('newfile.txt''w')
    my_file.write("Darth Vader")
    my_file.close()

    # contents of the file: 
    # Dath Vader
    my_file = open('newFile.txt','a')
    my_file.write("\nLuke Skywalker")
    my_file.close()
    # contents:
    # Darth Vader
    # Luke Skywalker
    
    to_read = open('newFile.txt','r')
    file_contents = to_read.readlines()
    for val in file_contents:
        print(val)

    #result:
    #Darth Vader
    #
    #Luke Skywalker   

if __name__ == "__main__":
    main()

The code above creates the file, adds 2 lines (Darth Vader and Luke Skywalker) to it, and the reads lines from the file, puts them in file_contents variable (list) and prints them to console.

The execution result is therefore:

Darth Vader

Luke Skywalker

As you can see, there is something strange with this result: there is an empty line between Darth and Luke, which is not in the file. That's because print(val) adds a new line character after the text it prints, and there is already one new line between Darth Vader and Luke Skywalker in the file.

In order to fix it, we can use an overloaded version of the print() function: print(val,end=''), which specifies that in the end of the text it should use an empty character instead of new line:

# !/usr/bin/python3

def main():
    # create a file
    my_file = open('newfile.txt''w')
    # write some data to it:
    my_file.write("Anakin Skywalker")
    my_file.close()

    # contents of the file: 
    # Anakin Skywalker

    # opening it again
    my_file = open('newfile.txt''w')
    my_file.write("Darth Vader")
    my_file.close()

    # contents of the file: 
    # Dath Vader
    my_file = open('newFile.txt','a')
    my_file.write("\nLuke Skywalker")
    my_file.close()
    # contents:
    # Darth Vader
    # Luke Skywalker

    to_read = open('newFile.txt','r')
    file_contents = to_read.readlines()
    for val in file_contents:
        print(val,end='')

if __name__ == "__main__":
    main()

The execution of the code above finally results in:

Darth Vader
Luke Skywalker

... which are contents of the file created above :).

We've covered text files - what about binary files? Here we're going to focus on reading from such file (let's be honest - writing to binary file is not very commond situation).

And reading from binary files is easy: we have to set 'rb' (read binary) instead of just 'r' (read) as our file open mode, and then we can read, for instance, a few bytes from the file. Those bytes become bytes array. So if we have, for instance, wav file (which should begin with letters RIFF in ASCII form), the reading of a first fragment of such file would look like this:

# !/usr/bin/python3

def main():

    binary_file = open('sound.wav','rb')
    bytes = binary_file.read(4#bytes
    print(bytes[0])
    print(bytes[1])
    print(bytes[2])
    print(bytes[3])
    
    print(str(bytes))

if __name__ == "__main__":
    main()

Which results in:

82
73
70
70
b'RIFF'

(which means it looks like some wav file indeed :) )

Thursday, 22 July 2021

Python crash course part 7: more on defining functions

 In the part 2 we've shown most basic ways of definining functions in Python.

Let's elaborate on that a bit more, using some examples.

First, a simple function just doing something, for instance printing text to screen (something tradinionally called a procedure rather than a function), is defined like this:

def just_do_it():
    print("Just do it")

... and called like this:

    just_do_it()

So execution of the program below:

# !/usr/bin/python3

def just_do_it():
    print("Just do it")

def main():

    just_do_it()

...results in the following output:

Just do it

What if we want to return value from function, so we can use it in the moment it's called? Well, let's simply use return keyword:

def please_return_something():
    return 3.14

The type doesn't have to be defined.

As an example we'll assign the value returned by the function above to pi variable, and then print it to the screen.

def main():

    pi = please_return_something()
    print(pi)

Result:

3.14

What if we want our function to take some parameters?

This is when the things became interesting, as Python allows us to defined both named and unnamed (positional) paramaters. By default, both. So we can define our function as:

def divide(nominatordenominator):
    return nominator/denominator

...and call it in two ways:

    print(divide(6,2))

Or:

    print(divide(nominator=6,denominator=2))

...both resulting in:

3.0

Please note that if we call our function with named parameters, their order doesn't matter. So call:

    print(divide(denominator=2,nominator=6))

...also results in:

3.0

The last thing for today will be default parameter value, which is defined using the = sign.

An example looks like this...

def power(ab = 2):
    return a**b

( ** is power operator, a**b means a to the power of b).

The above function has two parameters, but the second one (exponent) has default value. This means that if we skip the second param, our function becomes simply square function.

So the "full-param" call looks like this:

    print(power(2,3))

...and results in:

8

And the call using default parameter value can look like this:

print(power(3))

..resulting in:

9

There are more details about defining and calling functions in Python, which we can elaborate further on in future, but I think for now we've covered the most important features.


Tuesday, 20 July 2021

Python crash course part 6: basic exceptions.

Another basic language feature I'll cover in this tutorial is exception handling (which I wrote about in Kotlin a few days ago.

Traditionally, I'll show some examples. Let's begin with simple code, asking user for two numbers and dividing them. Of course, such program may encounter probably the most basic exception: division by 0. But also another one: if we put something not being a number, it cannot be converted to float and some other exception would be expected.

First, the code without any exeption handling would look like this:

# !/usr/bin/python3

def divide(ab):
    return a/b

def main():
    a = float(input('a = '))
    b = float(input('b = '))
    c = divide(a,b)
    print(c)

We create a function divide(ab) , returning division result. Then we get two numbers from user, a and b using input('... ') function and converting them to float using float() function (as input('a = ') returns simply a string).

If given two numbers which we can divide by eachother, the result is not surprising:

a = 10
b = 3
3.3333333333333335

But if we put 0 as b...

a = 10
b = 0
Traceback (most recent call last):
  File "d:\Projekty\Python\controlflow.py", line 13, in <module>
    main()
  File "d:\Projekty\Python\controlflow.py", line 9, in main     
    c = divide(a,b)
  File "d:\Projekty\Python\controlflow.py", line 4, in divide   
    return a/b
ZeroDivisionError: float division by zero

... we get a cute stacktrace. Well, division by 0 is not possible, and therefore results in an exception. Quite obvious :)

Before we try to deal with this proble, let's put another strange data as input to program: try to divide 10 by horse. Well, this also shouldn't be possible, right?

a = 10
b = horse
Traceback (most recent call last):
  File "d:\Projekty\Python\controlflow.py", line 17, in <module>
    main()
  File "d:\Projekty\Python\controlflow.py", line 9, in main
    b = float(input('b = '))
ValueError: could not convert string to float: 'horse'

... as expected. Of course, here another exception is caught (and in other place: not when trying to divide, but when trying to get float from quite horse-like input...)

What can we do with exception? We can catch it. First, a general exception:

# !/usr/bin/python3


def divide(ab):
    return a/b

def main():
    
    try:
        a = float(input('a = '))
        b = float(input('b = '))
        c = divide(a,b)
        print(c)
    except:
        print("Some exception caught!")

if __name__ == "__main__":
    main()

...which handles both division by 0:

a = 10
b = 0
Some exception caught!

... and division by horse :) 

a = 10
b = horse
Some exception caught!

The exception handling in Python is done using try except instruction. In the case above, no specific exception has been specified to be caught, so we get the same result in case of any exception (also known as Pokemon exception handling... Catch them all ;) ).

So what if we wanted to catch some specific exception? 

# !/usr/bin/python3


def divide(ab):
    return a/b

def main():
    
    try:
        a = float(input('a = '))
        b = float(input('b = '))
        c = divide(a,b)
        print(c)
    except ZeroDivisionError:
        print("Division by 0? Blasphemy!")

if __name__ == "__main__":
    main()

Result:

a = 10
b = 0
Division by 0? Blasphemy!

What if we want to catch more specific exceptions?

We can specify more except blocks:

# !/usr/bin/python3


def divide(ab):
    return a/b

def main():
    
    try:
        a = float(input('a = '))
        b = float(input('b = '))
        c = divide(a,b)
        print(c)
    except ZeroDivisionError:
        print("Division by 0? Blasphemy!")
    except ValueError:
        print("Some strange value...")

if __name__ == "__main__":
    main()

Division by 0 is then detected separately from division by horse:

a = 10
b = 0
Division by 0? Blasphemy!

and

a = 10
b = horse
Some strange value...

That's all for now - the custom exceptions and throwing them "by hand" will be one of our next topics in the nearest future :)



Monday, 19 July 2021

Unity editor scripting (part 3) - your own build (Android) tool

One of things I had scripted very early working as Unity developer, was building.

When doing a build "normally", you have to click through menu (File/Build Settings, or Ctrl+Shift+B), then choose name for the file (or use the old one, and accept the dialog asking whether to overwrite it), blah blah blah... 

But editor scripting allows us to avoid this pain: we can create a script, called from our custom menu (or via custom keyboard shortcut, like Ctrl+Shit+K, which I guess doesn't do anything interesting normally), which simply creates .aab or .apk file with, say, current date and time in name. No choosing name, no clicking through menus.

How to achieve such awesome result? Start with creating file Build.cs in your Assets/Editor folder and paste the following code there (should work for Unity 2020.3.10 and similar):



What's going on there? The GetEnabledScenes()  method simply returns all the enabled scenes in the current configuration. The build itself is taking place in PerformBuild() method. First (line 20) we get the scenes to be enabled in build. Then we get out desktop folder path (21). Then we create a string containing current time and date, so that every build could be easily identified and no two would overwrite (23), and then construct our file name with extension (24).

Then we create a BuildPlayerOptions object, containing all this information and specifying the target as Android.  And then, using EditorUserBuildSettings , we set up build features: enable Proguard, set the build to bundle (.aab, not .apk), and mark the build as release. Finally, the BuildPipeline.BuildPlayer(bpo);  runs the build itself.

In case you want .apk for debug, simply change the extension in line 26 to from .aab to .apk, then buildAppBundle in line 35 to false instead of true, and then  build type to AndroidBuildType.Release;  to AndroidBuildType.Debug in line 36.

Now having that scripted you can build your game using custom menu (without needing to specify file name everytime), or using keyboard schortcut Ctrl+Shift+K. But you can do one more thing - you can build your app without opening Unity. You can simply write a bash (Mac) or batch (Windows .bat) script to build the app for you, and then run it. It's helpful, if you build on different machine than you develop on.

For Windows, create a build.bat file with contents as follows (of course set appropriate the path to your Unity installation) and place it in parent folder of your project folder (not next to Assets, but next to folder containing Assets... I'll add a graph below ;D


Folder structure (it's really important)


So now when you run that script, it should build a nice .aab file on your Desktop :) If you want to use it that way (from the script, not via shortcut or menu), you should exit Unity before running it.

If you use Unity 2019 (not 2020), the script from the first listing may not work. But the problem is very simple: in Unity 2019.x you simply have to change the line 34 (the way minification is enabled has changed in the meantime):


Generally, if something does not work, doublecheck and triplecheck your file names and method names in both Unity and .bat scripts, also class and namespace and files placement. This whole system seems to be quite picky about it.

Side note: I've just realized a problem with using gists on blogger: you can't see the code in WYSIWYG mode... On the other hand, it's easier to write about code with line numbers ;) ... I'll have to think about it.

Sunday, 18 July 2021

Android Java to Kotlin migration by example part 11: basic exceptions

Today I'll show you how to catch and throw an exception in Kotlin.

The program illustrating the problem will be simple code, doing integer division. Such division throws an ArithemeticException if the divider is 0

Traditionally, let's begin with Java:

Listing 1 (Java)

import static java.lang.System.out;

public class MainClass {

static int divide(int a, int b) {
return a / b;
}

public static void main(String[] args) {
out.println("" + divide(6, 2));

try{
out.println(""+divide(3,0));
}catch(ArithmeticException ae){
out.println("division by 0!");
}
out.println("" + divide(3, 0));

}
}

We call our divide method 3 times: with (6,3) arguments (which simply gives us result: 2), with (3,0) arguments inside the try...catch block (which results in catching the exception and printing division by 0! to console, and finally with (3,0) arguments without try...catch block, which results in end of program execution, with message:

Listing 2 (result)

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at MainClass.divide(MainClass.java:6)
    at MainClass.main(MainClass.java:17)

So the program prints altogether:

Listing 3 (result)

3
division by 0!
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at MainClass.divide(MainClass.java:6)
    at MainClass.main(MainClass.java:17)

First, the result is simply computed. Then, as integer division by 0 is not possible, the program throws the ArithmeticException exception. The program continues, because we catch it. Then, the program throws it again, and - as it's not caught this time, the program execution ends.

The Kotlin version of program from Listing 1 is pretty straightforward:

Listing 4 (Kotlin)

package com.mypackage

import java.lang.ArithmeticException

fun divide(a: Int, b: Int): Int{
return a/b
}

fun main() {
println(""+ divide(6,2))
try{
println(""+ divide(3,0))
}catch (ae: ArithmeticException){
println("division by 0!")
}
println(""+ divide(3,0))
}

As mentioned in part 1 of this tutorial, the exception type declaration in catch block is a bit diffferent in Kotlin. The results are the similar:

3
division by 0!
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at com.mypackage.MainKt.divide(main.kt:6)
    at com.mypackage.MainKt.main(main.kt:16)
    at com.mypackage.MainKt.main(main.kt)

The only difference is the additional level of stacktrace in the logs... Which I, frankly, don't understand :D May have something to do with my main class configured in the project, being com.mypackage.MainKt?

But what if we want to define out own, custom exception? Let's define a simple one in Java and throw it in our divide method when the divisor is 0 (and then let's catch it instead of the ArithmeticException):

Listing 5 (Java)

import static java.lang.System.out;

class ReallyException extends Exception{
public ReallyException(String errorMessage) {
super(errorMessage);
}
}
public class MainClass {

static int divide(int a, int b) throws ReallyException {
if(b==0)
throw new ReallyException("Really? By 0?");
return a / b;
}

public static void main(String[] args) throws ReallyException {
out.println("" + divide(6, 2));

try{
out.println(""+divide(3,0));
}catch(ReallyException ae){
out.println("Exception message: " + ae.getMessage());
}
out.println("" + divide(3, 0));

}
}

Please not that as the ReallyException is not caught everywhere it could appear, we have to add the throws ReallyException directive to the main() method. The execution result is as follows:

Listing 6 (result)

3
Exception message: Really? By 0?
Exception in thread "main" ReallyException: Really? By 0?
    at MainClass.divide(MainClass.java:12)
    at MainClass.main(MainClass.java:24)

The Kotlin version of the program from Listing 5 would look like this:

Listing 7 (Kotlin)

package com.mypackage

class ReallyException(message:String): Exception(message)

fun divide(a: Int, b: Int): Int{
if(b==0)
throw ReallyException("Really? By 0?")
return a/b
}

fun main() {
println(""+ divide(6,2))
try{
println(""+ divide(3,0))
}catch (ae: ReallyException){
println("Exception message: " + ae.message)
}
println(""+ divide(3,0))
}

Traditional Kotlin brevity shows up: the (trivial) definition of the exception, which in Java took us 5 lines of code, here becomes... just 1 line :) Also, there is not need to declare our main() function throws anything.

One more thing: try in Kotlin, similarly to if and when, is an expression, which means it can return something. To be specific: it returns either the contents of try block in case exception doesn't occur, or catch block content in case of exception. Like this:

Listing 8 (Kotlin)

package com.mypackage

import java.lang.ArithmeticException

fun divide(a: Int, b: Int): Int{
return a/b
}

fun main() {

val result1 =
try { divide(4,2) } catch (e: ArithmeticException) { 9999 }
val result2 =
try { divide(4,0) } catch (e: ArithmeticException) { 9999 }

println(result1)
println(result2)
}

... resulting in:

2
9999

... as the first call simply returns 2, and the second one results in an exception, and therefore returns contents of  catch block, in our case being number 9999.

Saturday, 17 July 2021

Android Java to Kotlin migration by example part 10: higher-order functions

 A higher-order function is a function, which returns another function, or takes another function as parameter.

Today we'll begin with Kotlin code. Kotlin, being much more functional-oriented than Java (although Java is getting more functional since version 8), makes use of higher-order function much more elegant and readable.

Let's consider a mathematical function A, mapping some value to other value, for instance y=x*x (square). We can construct another function B (or functionAMultipliedBy2) which would map any x to functionA(x)*2.

In functional programming we can use a higher-order function in that case: create a function functionB, taking functionA as param. Such function can then be stored like a variable, and called. I guess it's more understandable in the code form. So let us take a look at a program defining our functionMultipliedBy2, getting a Float->Float function (getting float as param and returning a float) as parameter and returning Float->Float function (the function passed as a parameter multiplied by 2). 

Kotlin:

package com.mypackage

fun functionMultipliedBy2(
functionArg: (arg: Float) -> Float): (Float) -> Float
{
val funcToReturn = {a: Float ->functionArg(a)*2}
return funcToReturn
}

fun squareA(arg: Float): Float{
return arg*arg
}

fun main() {
println(squareA(2.0f)) // "4.0"

// we can pass squareA as parameter to functionMultiplied by 2
val squareDoubled = functionMultipliedBy2(::squareA)
// :: because it's class member

// squareDoubled is now a FUNCTION, taking Float
// as param and returning 2* square(param)

// we can simply call it:
println(squareDoubled(2.0f)) // "8.0"
}

The same code in Java would look like this:

import java.util.function.Function;

import static java.lang.System.out;

public class MainClass {

static Function<Float, Float> functionMultipliedBy2(
Function<Float, Float> functionArg)
{
Function<Float, Float> funcToReturn =
input -> functionArg.apply(input) * 2;
return funcToReturn;
}

static float squareA(float arg){
return arg*arg;
}

public static void main(String[] args){
out.println(squareA(2.0f)); // "4.0"

Function<Float, Float> squareDoubled =
functionMultipliedBy2(MainClass::squareA);
// -> ugly

// squareDoubled is an object of type Function
// getting Float as param and returning 2*square (param)

// call looks like this:
out.println(squareDoubled.apply(2.0f)); // "8.0"

}
}

The code execution result is the same in both cases and it's:

4.0
8.0

Please note how the Java code makes use of class Function objects to deal with functions. In Kotlin, we define returned value (or acccepted as argument) as function simply by (Float) -> Float notation.

What's more, in Kotlin we can simply call passed function. A Java Function object has to be used by calling .apply method to it (because it's an object, not a "real" function). I think Kotlin, as more functional-oriented languages, deals with this in much more "natural" way.

The higher-order function can also be used with lambda expressions. In Java:

import java.util.function.Function;

import static java.lang.System.out;

public class MainClass {

static Function<Float, Float> functionMultipliedBy2(
Function<Float, Float> functionArg)
{
Function<Float, Float> funcToReturn =
input -> functionArg.apply(input) * 2;
return funcToReturn;
}

public static void main(String[] args){
Function<Float, Float> squareRoot =
input -> (float)Math.sqrt((double)input);
Function<Float, Float> squareRootDoubled =
functionMultipliedBy2(squareRoot);
out.println(squareRootDoubled.apply(4.0f));
}
}

... as well as in Kotlin:

package com.mypackage

import kotlin.math.sqrt

fun functionMultipliedBy2(
functionArg: (arg: Float) -> Float): (Float) -> Float
{
val funcToReturn = {a: Float ->functionArg(a)*2}
return funcToReturn
}

fun main() {
val squareRoot = {arg:Float -> sqrt(arg.toDouble()).toFloat()}
val squareRootDoubled = functionMultipliedBy2(squareRoot)
println(squareRootDoubled(4.0f))
}

Again, Kotlin is much more concise - and allows us to simply call the function, without using any special .apply() methods.

Python crash course part 10: inheritance and polymorphism

In the last part we've shown how to create and use a class in Python. Today we're going to talk about inheritance: wchich means cre...