Friday, 8 June 2018

Classes

My favourite subject, seriously!


A simple class

class MyClass(): pass

There are two types of class variables, class variables and Instance variables.




Class variables

class MyClass:
    # class variable
    var = 0         def __init__():
    # instance variable created at initialization.     self.var = 1 # not the same variable as class variable
        def method(self):
        # access a class variable from a class method.
        self.__class__.var1 = 1

Class variables are accessible to instance methods using the __class__ 

Kivy

I hold a real passion for developing GUI's using Kivy, its ability to be truly multi platform. I have used Ubuntu, Windows and Mac to develop code for Raspberry Pi that just works on the other platforms.

Where Kivy really lacks is its dependency on other packages with specific version such as Cython (love Cython) All of these programs can be overcome with a little persistence.

A simple 'Hello World' Kivy example:

import kivy

from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):

    def build(self):
        return Label(text='Hello world')

if __name__ == '__main__':
    MyApp().run()

Go on give it a go!




Thursday, 7 June 2018

Profile small code snippets

Had a requirement to test some code that was causing timing problems, so looked at a few ways that this may be achieved and decided on timeit a default python language package.

import timeit
     
t = timeit.Timer("def test(): print("def test()", "print('setup')")

print(t.timeit(10000))

print(t.repeat(3,10000))
       

In my code I use the following no requirement for setup so empty, just call the API method that I want to time.

import timeit

t = timeit.Timer("def example(): x10i.CachedReadAndResetSecuritySwitchFlags()", "")
print(t.timeit(10000))
print(t.repeat(3,10000))
       

And that's it simple!

Classes

My favourite subject, seriously! A simple class class MyClass(): pass There are two types of class variables, class variables and Inst...