:::: MENU ::::

Multiple PHP versions on MAC via Homebrew

Great post about having multiple PHP version in your console: https://getgrav.org/blog/macos-monterey-apache-multiple-php-versions.

Few snippets from the post:

brew tap shivammathur/php
brew install shivammathur/php/php@7.0
brew install shivammathur/php/php@7.1
brew install shivammathur/php/php@7.2
brew install shivammathur/php/php@7.3
brew install shivammathur/php/php@7.4
brew install shivammathur/php/php@8.0
brew unlink php && brew link --overwrite --force php@7.3


Setup Grafana email notification via SendGrid SMTP (also for Bitnami Grafana)

Setup API Key on SendGrid

  1. Go to your SendGrid account and go to API Keys section.
  2. Add new API Key with, at least, Mail Send permission.
  3. Copy generated API Key. Your key should start with SG.

Grafana setup

  1. You have to edit Grafana configuration. Refer to Grafana Configuration documentation on where to find your config file. For Bitnami Grafana distribution you can find it here: /opt/bitnami/grafana/conf/grafana.ini
  2. Look for [smtp] section and either update existing one or add new one
[smtp]
enabled = true
host = smtp.sendgrid.net:587
user = apikey
password = YOUR_SENDGRID_API_KEY
skip_verify = false
from_address = admin@grafana.localhost
from_name = Grafana

3. Restart Grafana: official documentation, Bitnami documentation.

Testing

  1. Go to Alert notification channels (/alerting/notifications)
  2. Create new channel with type email.
  3. Add email address
  4. Hit Test
  5. You should receive test notification sent by Grafana through SendGrid SMTP sevice!

Few remarks

enabled = true – by default it’s false and then SMTP is disabled.

smtp.sendgrid.net:587 – you can try different ports but this one is recommended by SendGrid here Integrating with the SMTP API.

user – you have to provide apikey as user, otherwise you will end up with 550 Unauthenticated senders not allowed.

skip_verify – in other SMTP servers you may have to set this to true, yet for SendGrid it will cause problems as described in Notification channel Email cannot integrate with Sendgrid.

from_adress – looks like you can put anything you want but I would recommend to put verified email address you have in SendGrid – check the Sender Authentication section.

Links

Grafana configuration reference

550 Unauthenticated senders not allowed error code description.

Integrating with the SMTP API


Azure Log Analytics / Application Insights – search everywhere

If you have Azure Log Analytics or Application Insights, where you have different data sources, like exceptions, requests, traces, customEvents, you may want to search for some certain phrase everywhere, because you don’t know in which data source you should check or in which column.

There is quite handy way of doing that – search in query.

Quick example:

search in (exceptions, requests, traces, customEvents) "ManagedIdentityCredential"

And details in documentation.


Add tag to your build and use it in release pipeline in Azure DevOps

Adding a tag to your build in Azure DevOps is very useful – you can use it later on with releases to accept only builds with certain tags. That comes handy when you are building a project with many different configuration which you will later on deploy via different releases. For example

BUILD (tag: client1) -> RELEASE CLIENT 1
BUILD (tag: client2) -> RELEASE CLIENT 2

You can use it on client basis or environment basis like dev, uat, prod.

You can add as tag whatever you want. For example variable value that you can set, when you are starting the build. You can skip next step if you want to use existing variable in your pipeline.

Create variable in you build pipeline

  1. Create or edit your existing pipeline
  2. Go to Variables/Pipeline variables tab
  3. Add your variable, for example EnvironmentTag and add default value.
  4. Select Settable at queue time to make sure that you can change it when you are scheduling your build.
Creating variable in build pipeline

Add a stage to tag your build

  1. Go to the Tasks tab
  2. It’s good to have 2 stages – one to perform all the tasks related to your build and the other to tag the build
  3. Add PowerShell or Bash task that will add tag based on EnvironmentTag variable value: Write-Host "##vso[build.addbuildtag]$(EnvironmentTag)"
Adding new stage with Powershell task to tag build.

Execute pipeline

When you go to the build pipeline summary and go to Run pipeline you should be able to go to Variables and change EnvironmentTag value.

Run your pipeline
Select Variables section
Change EnvironmentTag value

Check your tag

Once your build is completed you can go to your build and check if you have your tag in there.

Use tags in release pipelines

Once we have our build tagged, we can use that information on release pipeline level, and for example trigger automatic trigger based on branch and tag. Go to your release pipeline and edit Pre-deployment conditions for your stage. In this example I want to do automatic release of Prod stage once the build from branch main and with tag prod is created.

Automatic release once build from main branch with prod tag is created.

Print all environment variables in Azure DevOps for Linux Agents with Bash

If you are looking how to achieve the same goal with Windows agents and PowerShell see Print all environment variables in Azure DevOps for Windows Agents with Powershell.

If you’d like to see all the environment variables configured during your build or release on Linux agent just add the Bash task with:

printenv

so it looks like

Bash task with printenv

and when you’ll execute your build or release pipeline you should be able to see all the environment variables:


Python3 dunder methods summary with examples

Below you can find a list of Python dunder methods as well as when they are invoked. Those two pieces of information put together should give you a better overview when to use which. The post is inspired on those materials available online: Python 3 documentation, Python Dunder Methods by Fernando Souza, python-course.

Assume that we have a class called MyClass.

Basic customization

MethodInvokedNotesDocumentation
object.__new__(cls[, ...])instance = MyClass()Called to create a new instance of class cls.Python 3 Documentation
MyClass.__init__(self[, ...])instance = MyClass()Called after the instance has been created (by __new__()), but before it is returned to the caller. Python 3 Documentation
instance.__del__(self)del instanceCalled when the instance is about to be destroyed.Python 3 Documentation
instance.__repr__(self)repr(instance)Called by the repr() built-in function to compute the “official” string representation of an object. Python 3 Documentation
instance.__str__(self)str(instance)Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. Python 3 Documentation
instance.__bytes__(self)bytes(instance)Called by bytes to compute a byte-string representation of an object. Python 3 Documentation
instance.__format__(selfformat_spec)format(instance, spec)Called by the format() built-in function, and by extension, evaluation of formatted string literals and the str.format() method, to produce a “formatted” string representation of an object. Python 3 Documentation
x.__lt__(y)x < yPython 3 Documentation
x.__le__(y)x <= yPython 3 Documentation
x.__eq__(y)x == yPython 3 Documentation
x.__ne__(y)x != yPython 3 Documentation
x.__gt__(y)x > yPython 3 Documentation
x.__ge__(y)x >= yPython 3 Documentation
instance.__hash__(self)hash(instance)If a class does not define an __eq__() method it should not define a __hash__() operation either;
If it defines __eq__() but not __hash__(), its instances will not be usable as items in hashable collections.
If a class defines mutable objects and implements an __eq__() method, it should not implement __hash__(), since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).
Python 3 Documentation
instance.__bool__(self)bool(instance)Called to implement truth value testing and the built-in operation bool(); should return False or True. Python 3 Documentation
Summary of basic Python dunder methods.

Customizing attribute access

MethodInvokedNotesDocumentation
instance.__getattr__(selfname)value = instance.nameCalled when the default attribute access fails with an AttributeError. Meaning there is no name attribute in that object. Python 3 Documentation
instance.__getattribute__(selfname)value = instance.nameCalled unconditionally to implement attribute accesses for instances of the class. Invoked before looking at the actual attributes within the object. Python 3 Documentation
instance.__setattr__(selfnamevalue)instance.name = valueCalled when an attribute assignment is attempted.Python 3 Documentation
instance.__delattr__(selfname)del instance.nameLike __setattr__() but for attribute deletion instead of assignment. Python 3 Documentation
instance.__dir__(self)dir(instance)A sequence must be returned with a list of valid attributes for that object. dir() converts the returned sequence to a list and sorts it. Python 3 Documentation
Summary of attribute access dunder methods.

Container methods

MethodInvokedNotesDocumentation
instance.__len__(self)len(instance)Called to implement the built-in function len(). Should return the length of the object, an integer >= 0. Also, an object that doesn’t define a __bool__() method and whose __len__() method returns zero is considered to be false in a Boolean context.
Python 3 Documentation
instance.__getitem__(selfkey)value = instance[key]Called to implement evaluation of self[key]. For sequence types, the accepted keys should be integers and slice objects.Python 3 Documentation
instance.__setitem__(selfkeyvalue)instance[key] = valueCalled to implement assignment to self[key].Python 3 Documentation
instance.__delitem__(selfkey)del instance[key]Called to implement deletion of self[key].Python 3 Documentation
instance.__missing__(selfkey)value = instance[key]Called by dict.__getitem__() to implement self[key] for dict subclasses when key is not in the dictionary.
Python 3 Documentation
instance.__iter__(self)iter(instance)This method is called when an iterator is required for a container.Python 3 Documentation
instance.__reversed__(self)reversed(instance)Called (if present) by the reversed() built-in to implement reverse iteration. It should return a new iterator object that iterates over all the objects in the container in reverse order.Python 3 Documentation
instance.__contains__(selfitem)item in instanceCalled to implement membership test operators. Should return true if item is in self, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs.Python 3 Documentation
Summary of container dunder methods.

Numeric operations (Python 3 Documentation)

MethodOperation
x.__divmod__(self, y)divmod(x, y)
x.__rdivmod__(self, y)divmod(y, x)
x.__invert__(self)~x
x.__index__(self)operator.index(x)
x.__ceil__(self)ceil(x)
x.__floor__(self)floor(x)
x.__pos__(self)+x
x.__neg__(self)-x
x.__complex__(self)complex(x)
x.__float__(self)float(x)
x.__int__(self)int(x)
x.__round__(self[, n])round(x)
x.__trunc__(self)trunc(x)
x.__abs__(self)abs(x)
x.__add__(self, y)x + y
x.__sub__(self, y)x - y
x.__mul__(self, y)x * y
x.__pow__(self, y)x ** y
x.__truediv__(self, y)x / y
x.__floordiv__(self, y)x // y
x.__mod__(self, y)x % y
x.__and__(self, y)x & y
x.__or__(self, y)x | y
x.__xor__(self, y)x ^ y
x.__lshift__(self, y)x << y
x.__rshift__(self, y)x >> y
x.__matmul__(self, y)x @ y
x.__radd__(self, y)y + x
x.__rsub__(self, y)y - x
x.__rmul__(self, y)y * x
x.__rpow__(self, y)y ** x
x.__rtruediv__(self, y)y / x
x.__rfloordiv__(self, y)y // x
x.__rmod__(self, y)y % x
x.__rand__(self, y)y & x
x.__ror__(self, y)y | x
x.__rxor__(self, y)y ^ x
x.__rlshift__(self, y)y << x
x.__rrshift__(self, y)y >> x
x.__rmatmul__(self, y)y @ x
x.__iadd__(self, y)x += y
x.__isub__(self, y)x -= y
x.__imul__(self, y)x *= y
x.__ipow__(self, y)x **= y
x.__itruediv__(self, y)x /= y
x.__ifloordiv__(self, y)x //= y
x.__imod__(self, y)x %= y
x.__iand__(self, y)x &= y
x.__ior__(self, y)x |= y
x.__ixor__(self, y)x ^= y
x.__ilshift__(self, y)x <<= y
x.__irshift__(self, y)x >>= y
x.__imatmul__(self, y)x @= y
Summary of numeric dunder methods.

Context managers

MethodInvokedNotesDocumentation
instance.__enter__(self)with instance as obj:Enter the runtime context related to this object. The with statement will bind this method’s return value to the target(s) specified in the as clause of the statement, if any.Python 3 Documentation
instance.__exit__(selfexc_typeexc_valuetraceback)with instance as obj:Exit the runtime context related to this object. The parameters describe the exception that caused the context to be exited. If the context was exited without an exception, all three arguments will be None.Python 3 Documentation
Summary of context manager dunder methods.

Callable objects

MethodInvokedNotesDocumentation
instance.__call__(self[, args...])instance(args...)Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) roughly translates to type(x).__call__(x, arg1, ...).
3.3.7
Python 3 Documentation
Summary of callable objects dunder methods.

Descriptors

MethodInvokedNotesDocumentation
attr.__get__(selfinstanceowner=None)value = instance.attrCalled to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). The optional owner argument is the owner class, while instance is the instance that the attribute was accessed through, or None when the attribute is accessed through the owner.
Python 3 Documentation
attr.__set__(selfinstancevalue)instance.attr = valueCalled to set the attribute on an instance instance of the owner class to a new value, value.
Python 3 Documentation
attr.__delete__(selfinstance)del instance.attrCalled to delete the attribute on an instance instance of the owner class.Python 3 Documentation
attr.__set_name__(selfownername)class A:
attr = Attr()
calls
attr.__set_name__(A, 'attr')

Automatically called at the time the owning class owner is created. The object has been assigned to name in that class.
Python 3 Documentation
Summary of descriptors dunder methods.

Rename multiple files with find and rename

Nice shorthand to rename multiple files. First, make sure you have rename available:

brew install rename

and then use the snippet

find . -exec rename 's|FIND|REPLACE_WITH|' {} +

Dump python requirements for file or folder but not for the whole project

I don’t want to dump requirements for my whole project using:

pip freeze > requirements.txt

Instead of that I want to dump requirements for a file or particular folder. It’s a piece of cake with pipreqs. Install the package

pip install pipreqs

and then just run it with the folder or file for which you want to generate requirements.txt file

pipreqs my_folder



Pages:12345