Need some help with the "Light" module in Python3 please


#1

Python Beginner alert and long post warning :-o

I’m more or less a beginner with Python but I can follow the code and have used other programming languages.
I’ve managed to get mini-kit.py working by copying it into my “~/flotilla-offline/flotilla-python/library/flotilla/” folder along with all the other .py files, and running it from there.

Having had success with that I decided to try and get the “Light” module working. Using mini-kit.py as the basis for my new python light module.
I could see that first I probably needed to make some changes to __init__.py and light.py., as follows:


Extract from __init__.py

.
.
from .rainbow import Rainbow
from .light import Light ←ADDED BY ME

VID = "16d0"
PID = “08c3”

LANG_REQUIRES = "Oh no! I need a {module_type} on channel {channel}"
LANG_FOUND = "Yay! I’ve found a {module_type} on channel {channel}"
LANG_READY = “Everything connected properly. Let’s go!”

class Client:
_module_handlers = {
‘dial’: Dial,
‘slider’: Slider,
‘motion’: Motion,
‘matrix’: Matrix,
‘number’: Number,
‘touch’: Touch,
‘light’: Light, ←UNCOMMENTED BY ME
# ‘colour’: Colour,
‘joystick’: Joystick,
‘motor’: Motor,
# ‘weather’: Weather,
‘rainbow’: Rainbow
.
.


My light.py file ( This was based on the touch.py file)

from .module import Module

class Light(Module):
name = ‘light’

@property
def light(self):
    if len(self.data) >= 0:
        return int(self.data[0])
        return 0

This is my “mini-kit_light.py” program:

1 print(""“
2 This example requires the Light module,
3 you’ll find it in the mini kit.
4 Press Ctrl+C to exit.
5 “””)
6
7 import flotilla
8 import time
9
10 client = flotilla.Client(
11 requires={
12 ‘one’: flotilla.Light
13 })
14
15 def module_changed(channel, module):
16 light = client.first(flotilla.light)
17
18 while not client.ready:
19 pass
20
21 light = client.first(flotilla.Light)
22
23 try:
24 print(“OK”)
25 time.sleep(0.1)
26 except KeyboardInterrupt:
27 client.stop()

Before I started to write the code to display the output of the Light module I used the above for testing, just to see if it would get to line 24 .However, when I run it I get this error:

Traceback (most recent call last):
File “mini-kit_light.py”, line 12, in <module>
‘one’: flotilla.Light
AttributeError: ‘module’ object has no attribute ‘Light’

I’ve tried various things but cannot overcome this error. I’m thinking that the problem might be line 7 ? I don’t know what this import is for or what it contains.

If anyone could spare some time to point me in the right direction I would really appreciate it.

TIA Steve


#2

It’s tricky to see what you’re doing wrong, I suspect it might be more a case of it loading an unmodified library rather than anything you might have done wrong.

I’ll make these changes to the library and see what happens- I really should have sorted these out forever ago!

Update: Just changing init.py and light.py as you mentioned seemed to work: https://github.com/pimoroni/flotilla-python/commits/master


#3

Thanks for looking at this, I do appreciate it.

Sorry, but I’m not sure what you mean by that. Are you saying that my script “mini-kit_light.py” should not show the error ?

Also, does it matter that all my files are in “~/flotilla-offline/flotilla-python/library/flotilla/” ?


#4

The easiest way to make changes to the library, and to use it in general, is to install it by running:

sudo python3 setup.py develop

Which will link the files into the place where Python expects to see its libraries.

With that set up you should be able to run scripts anywhere, and “import flotilla” will always work.


#5

I ran [quote=“gadgetoid, post:4, topic:1668”]
sudo python3 setup.py develop
[/quote]but unfortunately the error still occurs.


#6

Today I have tried a different approach based on your first post.
I have used the new __init__.py and light.py files and modified mini-kit.py with the changes needed to connect the Light module. So I have connected the Touch, Light and Rainbow modules and I don’t get any error messages. I can continue with my coding, thanks.

I think I’ve seen an anomaly in the __init__.py code, the following code extract does not look correct, there are 2 channel one’s and no channel eight, but as a beginner I may be wrong:

    @property
    def channel_one(self):
        return self._modules[self._channel_names.index('one')]

    @property
    def channel_one(self):
        return self._modules[self._channel_names.index('one')]

    @property
    def channel_two(self):
        return self._modules[self._channel_names.index('two')]

    @property
    def channel_three(self):
        return self._modules[self._channel_names.index('three')]

    @property
    def channel_four(self):
        return self._modules[self._channel_names.index('four')]

    @property
    def channel_five(self):
        return self._modules[self._channel_names.index('five')]

    @property
    def channel_six(self):
        return self._modules[self._channel_names.index('six')]

    @property
    def channel_seven(self):
        return self._modules[self._channel_names.index('seven')]