Categories
Uncategorised

List of dicts to table with PrettyTable

Given:

summaryList = [{"Event Name": "Pod_Service_MTTR_Notify", "Count": 1}, {"Event Name": "Pod_Service_No_2_alert", "Count": 20}]
if summaryList:
    summaryTable = PrettyTable(summaryList[0])
    for i in summaryList:
        summaryTable.add_row(i.values())
Categories
Uncategorised

Rate Limiter / Cool Down timer in python3 and redis

My main source of data… jira service desk… cannot be trusted. Oh yes, 90% of the time it acts sane, but once in a while, someone misconfigures a big panda alert and we get 1000 new incidents in moments.

My colleague got flooded with pager duties and emails one Saturday, and I’m sure it made him cry. I need to rate limit these actions.

My plan is to use incr function to increment a redis key. The idea is that you create a new key every x seconds, with an expiry (equal to x) and then when you increment redis will respond with the current value, so you can just test the if loop.

First things first. I know how to do something every second int(time)) but every 10 minutes?

from time import time, sleep
while True:
     everyXSeconds = 10
     curTime = int(time())
     key = curTime - curTime % everyXSeconds
     print(key)
     sleep(1)

I’m only calling time() once because calling time() twice in the mod actually give different nanosecond values… and I just know I am going to hit some corner case where that crosses a second boundary and messes up EVERYTHING

BUT… the above is actually crap and not needed at all. That is a crap way to rate limit cause, for example, if I am trying to stop an email flood it will still send 10 emails every X seconds. As in, the count will be reset every X seconds.

To me the following makes a bit more sense:

while True:
    curTime = time()
    print(f"curTime is {curTime}")
    everyXSeconds = 10
    rateBeginTime = curTime - everyXSeconds
    count = red.zcount(queueName, rateBeginTime, curTime)
    print(f"current hit queue is :{count}")
    limitMap = {curTime: curTime}
    red.zadd(queueName, limitMap)
    sleep(1)
    remove = red.zremrangebyscore(queueName, 0, rateBeginTime)
    print(f"removed {remove} keys")

So thats just what I used to test…. but basically you are using a sorted list and adding and removing as needed

Categories
Uncategorised

Cryptomnicon (spoiler warning)

It has been a long time since I felt the need to write about a book I read.

Particular items of note:

Ityme A: There are some very funny bits in it. I got a good chuckle from some bits of it. It was a bit more funny than termination shock.

Ityme B: It doesn’t shy away from sex. It is still a bit awkward (as anything from an American/ British writer is going to be really) but better than most writers of this ilk.

Ityme C: I liked the Andrew Lobe bit at the end. I actually forgot he was the lawyer until I google him afterwards… A passionate Lawyer indeed. Yes some of the criticism of the character could be valid but when taken as part of the book as a whole I think it fits in nicely in it’s current treatment.

Ityme D: I read numerous reviews before hand saying the first half of the book didn’t have a plot. It clearly does, it’s just temporally non-contiguous .

Ityme E: It is a good book. I also enjoyed Termination shock without realising it was the same author. I stopped “reading” (listening) to the vagabond one but now I have more of a sense of Stephensons style I will go back to it as I find it enjoyable.

Ityme F: The books do not come to an abrupt stop; to quote one person ,”It is as if the publisher called him and said “ok finish up, we need the manuscript tomorrow”. The story has a lovely natural ending, and I found it most satisfactory (I imagine it is hard to end an epic)

Ityme G: I want some of the websites and servers mentioned in the book to still work!

Categories
Music quick

Chase and Status

Friday night jungle is MASSIVE. https://www.youtube.com/watch?v=OICeFYUu0j0

This goes out to all the chocolate mouses in the building….. Hold onn!

Categories
Uncategorised

XXX and other such texts.

A Marlon Brando of a whale shat on me today.

Categories
Parenting quick

Lying awake in bed at 5am, panic in the air

What are the cheeky elves going to do tonight?

Categories
personal

Father passed away

Father passed away there after a struggle with cancer.
I’m still in it, but how few people have contacted me to send condolences is mental. Is it cause I’m not on Facebook?
The people I work with, most of whom I have never even met in real life, sent the nicest messages.
My team has a great culture the kinda sucks people into it, it’s nice.
I’m still in it, and I guess this is my outlet for self indulgence…

Categories
Python quick

Working in prod

Look, I’m not, a professional let us say. Sometimes I work in prod shell.

In order to keep myself aware of same, make this little change to the .tmux file so you always have a visual queue that you are in prod:

# Status Bar
set -g status-bg red
(as opposed to black)
Categories
stories

Right before the flood comes

We will all need to turn into mermaids, when the flood comes after breakfast.

Categories
Python

Nested Dict Gets in python

Update- the below doesn’t work at all actually.. you do need to do a try-except block cause if any of the values are, for example NONE. The whole thing craps out with an exception.

I’ll leave the below to…. Show my workings!

Trying to figure out the best way to parse out complex JSON.

Saying

Level = issue.get('fields').get('customfield_1', 'NA')

Doesn’t actually work if ‘fields’ is missing. Error is:

AttributeError: 'NoneType' object has no attribute 'get'

you actually need to say:

In [1]: d = {'parent':{'test':True}}

In [2]: d
Out[2]: {'parent': {'test': True}}

In [3]: d.get('parent')
Out[3]: {'test': True}

In [4]: d.get('parentt')

In [5]: d.get('parent').get('test')
Out[5]: True

In [6]: d.get('parenT').get('test')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-142a5af0ba34> in <module>
----> 1 d.get('parenT').get('test')

AttributeError: 'NoneType' object has no attribute 'get'

In [7]: d.get('parenT', {}).get('test')

In other words, in the case of nested gets, make sure the second attribute (the one that returns if your key is missing) is an empty dict… ie {}