0

I am trying to make some binary singal system by boolean variables I named with LIGHTx.

LIGHT1 = True
LIGHT2 = True
LIGHT3 = False
LIGHT4 = False

Next, I nest these variables into a list for future calculation,

signal = [LIGHT1, LIGHT2, LIGHT3, LIGHT4]

Currently I am using the idea from Python: Boolean List to Binary String and Convert base-2 binary number string to int to convert the list to int number which is my signal. Here, [1,1,0,0] means 12.

In [97]: boolList2BinString(signal)
Out[97]: 12

My questions are:

  1. How can I automatically updating the elements of "signal" by updating the value of the LIGHTs, rather than running signal = [LIGHT1, LIGHT2, LIGHT3, LIGHT4] again and again? Whitch means, in the rest of my codes, I only need to run LIGHTx = xxxx and boolList2BinString(signal). (Maybe some way like pointer of C++ ?)
  2. If it is impossible with question 1, is there any way that I can fix the order of the LIGHTs in the list?

[Update]

Please exclude the way that building the 'signal' list inside the 'boolList2BinString' function.

Original:

def boolList2BinString(lst):
    return int('0b' + ''.join(['1' if x else '0' for x in lst]), 2)

Building inside:

def boolList2BinString():
    osignal = [LIGHT1 , LIGHT2 , LIGHT3 , LIGHT4 ]
    return int('0b' + ''.join(['1' if x else '0' for x in signal ]), 2)

Thanks in advance!

3 Answers 3

0

you can create a class to hold the boolean, then it would act like a pointer

>>> class Light:
...     def __init__(self, value):
...             self.value = value
...     def get(self):
...             return(self.value)
...     def set(self, value):
...             self.value = value

then use light.get() to get the value, or light.set() to set the value

>>> Light1 = light(True)
>>> Light2 = light(False)
>>> lights = [light1, light2]
>>> for l in lights:
...     print(l.get())
... 
True
False

example showing set:

>>> for l in lights:
...     print(l.get())
... 
True
False

>>> light2.set(True)
>>> for l in lights:
...     print(l.get())
... 
True
True
>>> 
Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps you just need some sort of wrapper.

class Light(object):
    def __init__(self, state):
        self.state = state
    def false(self):
        self.state = False
    def true(self):
        self.state = True

LIGHT1 = Light(True)
LIGHT2 = Light(True)
LIGHT3 = Light(False)
LIGHT4 = Light(False)

signal = [LIGHT1, LIGHT2, LIGHT3, LIGHT4]

And then you simply can change states of each lights as LIGHT1.true() or LIGHT1.false() and value in signal list will be changed automatically.

LIGHT1.false()
LIGHT1.state  # False
LIGHT1.true()
LIGHT1.state  # True

The methods naming could be changed, of course.

Comments

0

You could use a special property class to set bits in an integer.

Here is an example:

class Light(property):
    def __init__(self, rank):
        self.rank= rank
        super(Light, self).__init__(self._get, self._set)
    def _get(self, signal):
        return (signal.value & (1 << self.rank)) != 0
    def _set(self, signal, value):
        if value:
            signal.value |= 1 << self.rank
        else:
            signal.value &= ~(1 << self.rank)

class Signal(object):
    def __init__(self, value=0):
        self.value = value
    l1 = Light(3)
    l2 = Light(2)

You can then use that simply:

>>> s = Signal()
>>> s.value
0
>>> s.l1
False
>>> s.l1 = True
>>> s.l1
True
>>> s.value
8

(Tested on both Python 2.7 and Python 3.5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.