Programming improvements 3

A few things of the many things I learned last months in Python.

For and while loops have an else option

I used to write code like this:

helper = False
for something in some_list:
    if condition:
       helper = True
       break
if helper == False:
   do_something()

That can be done so much easier:

for something in some_list:
    if condition:
       break
else:    # break wasn't called in the loop
    do_something

A similar option exist for while loops.

Few other little things were:

if helper == False:          ->            if not helper:

def method(somevar=""):      ->            def method(somevar=None):
    if somevar != "":                          if somevar:

List comprehensions were also a revelation. Instead of using:

newlist = []
for entry in some_list:
    if condition:
        newlist.append(entry)

condition could be something like “entry > 0”. Using a list comprehension, that can be a one liner and is also faster in the execution:

newlist = [entry for entry in some_list if condition]

While I still feel, it would have been nice, if I’d have learned these little things before (which comes from the thought I would be liked more, and from the expectation of perfectionism by me and because of that I also think others expect perfectionism from me – which is just not achievable), it is nice to pick new things up now. While I thought in my 20s that in many areas of life I can stop learning at some point, in the last years I learned that learning will never end in any part of life, and that is good! Because if I stop being willing to learn, I will go backwards.

Programming improvements 2

A few things of the many things I learned last weeks in Python.

This is what I used to do to conditionally set settings:

class MyClass:
    def method(self, args**):
        <some code block>
        if some_arg == "condition":
            self.option = "this setting"
            <run some code>
            <call a method>
        else:
            self.option = "another setting"
            <run some other code>
            <call a different method>
        <some more code block>

myclass = MyClass()
myclass.method(args)

And it could be much more complicated.

Using an object oriented approach, can be so much nicer.

class MyBaseClass:
    def method(self, args**):
        <some code block>
        self._submethod(subargs**)
        <some more code block>
    @abstractmethod
    def _submethod():
        pass

The private submethod would then be defined in the child classes. First for “condition”

class MyClass_condiontion(MyBaseClass):
    def _submethod()::
        self.option = "this setting"
        <run some code>
        <call a method>     # can be in this or the parent class

Second for the else:

class MyClass_else_condiontion(MyBaseClass):
    def _submethod()::
        self.option = "another setting"
        <run some other code>
        <call a different method>     # can be in this or the parent class

To call the classes one could use the condition:

if some_arg == "condition":
    myclass = MyClass_condiontion()
else:
    myclass = MyClass_else_condiontion()
myclass.method(args)

With this approach the coding is so much neater. Each method knows what it has to do and there is no fuss about the settings.

While I still feel, it would have been nice, if I’d have learned these little things before (which comes from the thought I would be liked more, and from the expectation of perfectionism by me and because of that I also think others expect perfectionism from me – which is just not achievable), it is nice to pick new things up now. While I thought in my 20s that in many areas of life I can stop learning at some point, in the last years I learned that learning will never end in any part of life, and that is good! Because if I stop being willing to learn, I will go backwards.

Programming improvements

A few things of the many things I learned last week in Python.

My draft when a new optional dictionary was added to the method:

def method(self, arg1, options={}):

However, a better version was suggested to me:

def method(self, arg1, options=None):
options = (options or {})

And the other big learning through code review was, when constructing a string from a dictionary. My complicated code:

for key1, value1 in options.items():
    if key1 == "searchString":
        for key, value in value1.items():
            result_string = f"someString.{key}.{value}"

The suggested version is so much neater:

for key, value in options.get("searchString", {}).items():
    result_string += f"someString.{key}.{value}"

While I still feel, it would have been nice, if I’d have learned these little things before (which comes from the thought I would be liked more, and from the expectation of perfectionism by me and because of that I also think others expect perfectionism from me – which is just not achievable), it is nice to pick new things up now. While I thought in my 20s that in many areas of life I can stop learning at some point, in the last years I learned that learning will never end in any part of life, and that is good! Because if I stop being willing to learn, I will go backwards.

How to code, an additional note

(by Ronny Errmann)

My last post could be read quite negatively, in the way that I didn’t make any progress. So I wanted to add some notes to counteract that feeling. When I looked into new methods actively, because I had a problem, I learned a lot. And I easily remember a few bits from the last years:

  • Replacing loops and conditions with numpy operations on the whole array or part of the array. This made execution so much faster. For example using
array[array > 0] = numpy.average(another_array, axis=1)
  • Making my program compatible to Windows. It’s just a few things to keep Python code compatible to different operating systems.
  • Adding multiprocessing to the program.

All of these helped me to learn new concepts and improve.

A beginners code

(by Ronny Errmann)

As mentioned in my last post, I started coding without asking for advice. Which meant I would do things inefficiently. For example to calculate the standard deviation I used the following code:

mittel=sum(l for l in temp)/len(temp)
stdev=math.sqrt(sum((l-mittel)**2 for l in temp)/(len(temp)-1))

Later I learned about numpy, which made things a bit easier:

stdev = numpy.std(temp, ddof=1)

This is also quite a bit more efficient in computing time as it is run in compiled code and not has the overhead of an interpreter language.

Looking back I can smile about the code. In an interview I was once asked about that program if I would do things differently now. To which my answer was a clear yes.

How I came to writing code

(by Ronny Errmann)

The first device I wrote my first programs was the CASIO CFX-9850G, a graphical calculator. We needed it for school (I think 8th grade) and to get it for a lower price, were suggested to buy it even half a year earlier. That was in the mid 90s and, out of interest and out of boredom, I read the manual to see what one can do with it. And by talking to friends, being showed programs from others, I started to understand other people’s code and then tried to solve smaller questions. With time the programs got bigger. In the end I programmed a battleship game and 4 wins, ready to be played in boring lessons against the person sitting next to you. While the programs are long gone (maybe the hand-copied code is still in a folder at my parents place), the calculator is still a part of my desk, although, primarily just as a calculator.

A few years later (or at the same time?) we learned Delphi in School. It was only one year with one or two hours a week, so it couldn’t go into details, however, I know I tried to improve the programs and write more complex ones after school or at home. The GUI made some parts easy, however, looking back it also prevented me from understanding that there is a big benefit for writing back-end code.

After School my interest in coding decreased, mostly because I thought I wasn’t good enough or didn’t see the application. And interest in learning background theory was limited at that age. When I started my Physics studies live was also busy enough. To analyse the laboratory experiments I would work with Excel/OpenOffice Calc and Origin. The built-in functions were enough to solve everything, not elegant, but efficient (in terms of my time).

In my masters project I found the limitations of OpenOffice Calc. To do calculations I just needed so many columns that I would crash it on a regular basis. However, it took until I had to do the first analysis of observational data, that I noticed I need to learn programming. Other people at the institute suggested Python as a start. And so my first basic program was just a python script that would do the steps I would do by hand by itself. It was very procedural, and when I look back, quite funny in how I wrote it. I definitely didn’t search for advice. Luckily this has changed over the years.