2021/11/19

Using SubSlicing in Python passing "Slice Spec" as a string

Python's implicit [start:end:step] notation makes slicing objects very easy.

But if you want to create a function that accepts optional params that allow you to manipulate the return then your left to create your own interface specifying how to slice the return data.

In the sample below two params are passed to tester

theList - A list object to be sliced
sliceSpec - a string specifying the python slice notation

Python provides a helper called "slice" which accepts three params (start, end, step) which are the components of an implied slice.

But given that 0 (zero) is a legitimate value for any of the arguments when an argument is skipped ":5" the missing must be represented as a "None"

Also the values passed to slice() must be integers and split() will return a list of strings

So the below example takes "sliceSpec" does the following

  1. if sliceSpec is None, then replace with an empty string
        otherwise split(":") fails
  2. Traverse the list returned by split()
    • substitute blank strings with None
    • convert any values to integers
  3. Pass the transformed results from split() into arguments passed to slice()
  4. use the slice operator to access the subList from "theList"

Example Code

def tester(theList, sliceSpec=None):

    returnList = theList[slice(*(int(i) if i != '' else None for i in (sliceSpec or str()).split(":")))]

    print(f"'{sliceSpec}' = {returnList}\n")


mylist = [ 1,2,3,4,5,6,7,8,9 ]

tester(mylist)
tester(mylist, None)
tester(mylist, "")
tester(mylist, ":5")
tester(mylist, "0:1")
tester(mylist, ":1")
tester(mylist, "1:-2")
tester(mylist, "::-1")

Results

$ python3 test

'None' = [1, 2, 3, 4, 5, 6, 7, 8, 9]

'None' = [1, 2, 3, 4, 5, 6, 7, 8, 9]

'' = [1, 2, 3, 4, 5, 6, 7, 8, 9]

':5' = [1, 2, 3, 4, 5]

'0:1' = [1]

':1' = [1]

'1:-2' = [2, 3, 4, 5, 6, 7]

'::-1' = [9, 8, 7, 6, 5, 4, 3, 2, 1]

 

 

2019/11/11

Zigbee Lighting - Sylvania 74099 4/8 button remote will not pair.


https://community.hubitat.com/t/sylvania-lightify-smart-switch-8-functions-4-buttons/10943

Update:

Ken Fraleigh from the Hubitat community pointed out that what I thought of as "Reset" (3-4) is actually the ZLL (Zigbee Light Link). This information came from the European Sylvania site where they have been discussion support for ZB 3.0 and Hue Bridge compatibility.


So I was reading through the threads above and others trying to get the Sylvania  4 button controller working with the Lightify Hub . We have a goal of keeping this cost effective, shopping for deals. Maybe Hubitat for x-mas,

Anyway while trying to get one re-paired was thinking back to another one I had and was sitting on the shelf waiting to go back. The Device has 4 buttons

12
34

The Manual and on-screen directions tell you to press 2 & 3 and hold for 2 seconds and it should pulse orange and re-pair.

So that's not working, I should have realized at this point that the 2 button isn't flashing. This is because I dropped onto the floor and it exploded like an old cell with removable battery parts going flying. Wish I had thought of this earlier, but that's ok.

So if 2-3 does something, what else exists ?

3 & 4: While held, flashes orange and then ends with red. (ZLL Pairing)
1 & 4: After a few seconds flashes green 10 times then ends with long green

1 & 4 We'll call power-on reboot. It's similar to what happens when you pull the tab keeping the battery from making contact.

3 & 4 Feels like some kind of reset, warning orange ending with Red. I was incorrect this is ZLL Pairing mode (Thanks Ken)

So back to the story, This was 1 of 2 I'd just gotten, 1 new and one as replacement for another which got paired to a system that was shipped back and couldn't get it paired to the Lightify. 

After I disassembled/reassembled the other one, and had re-paired it by pressing on the metal buttons to get it paired. If you have any question if your actually pressing 2 & 3, I'd recommend removing the face and checking out under the hood.To remove the front bezel, remove the battery cover and in the rounded corners you'll see the two plastic hook tabs holding the bezel in-place.

I unpacked the one destined to be returned and did the following:

Reboot:1 & 4
ZHA Pair:2 & 3
Reset
ZLL Pairing:
3 & 4

And look at that, it paired right up. 

Sylvania why didn't you just say that. Searched the site, Submitted tickets, Marketing-Speak back, no tech support.




2018/12/04

Mapping Private/Public Docker ports with AWS ECS and Docker Net=Bridge

I've done many searches trying to find a way to know what the public port numbers that have been assigned to my docker container are from inside the container. Docker has a way to query this information but that means exposing the Docker Daemon interface (socket or tcp) to the process running inside the container and sadly there are little security controls around that so understandably most are reluctant to expose that interface inside the container

Amazon's ECS agent has a lightweight which will expose details about the container which happens to include the Private/Public port mappings. I've included a link to the script I wrote here GitHub Gist along with the code below.

It requires the use of JQ which I happen to consider a fundamental tool if your going to manipulate JSON files from within bash.