Skip to content

Instantly share code, notes, and snippets.

@tom1299
tom1299 / python_collections.py
Last active November 21, 2022 10:29
A short explanation of python collections and numpy arrays
import numpy as np
# A List is a collection which is ordered and changeable.
# In Python lists are written with square brackets: [1,2,3,4,5]
numbers_as_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# A NumPy array is a collection which is ordered and changeable.
# Numpy arrays must contain elements of the same type.
numbers_as_numpy_array = np.array(numbers_as_list)
@tom1299
tom1299 / query.txt
Created October 21, 2022 10:12
A query for calculating the average running time of a job in a GitLab pipeline.
db.pipelines.aggregate([
{
$unwind: "$jobs"
},
{
$group: {
_id: {
"project": "$project_id",
"jobName": "$jobs.name"
},
@tom1299
tom1299 / .gitlab-ci.yml
Created April 28, 2022 06:26
Setup Kind in a GitLab CI/CD pipeline
stages:
- test
test:
image: tom1299/docker:20.10.14-dind
services:
- name: tom1299/docker:20.10.14-dind
stage: test
before_script:
- kind create cluster --wait 5m --config=./config.yaml
import re
import yaml
def find(element, dictionary):
"""
Find a value given a path like a.b.c
"""
keys = element.split('.')
rv = dictionary
for key in keys:
@tom1299
tom1299 / Add methods to a class programmatically in python
Last active March 4, 2022 13:47
How to add a method to a class in code using python
class Greeter:
pass
def create_greeting(name: str):
def greeting_method(self):
return print(f"Greetings {name}")
greeting_method.__name__ = f"greetings{name}"
return greeting_method
@tom1299
tom1299 / export_yaml_definition_k8s_deyploment.md
Last active December 23, 2019 13:41
Export yaml definition of k8s deployment

Export yaml definition of k8s deployment

The motiviation for this gist was the deprecation of the --export flag. Having the yaml definition of a running deployment has the advantage that it can be saved and edited. For example when updating the version of an image.

Obtaining the current definition

For this example we will just use a deployment of the Kubernetes Deployment documentation. This can be deployed using the command

kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml

To get the yaml definition the get command can be used:

@tom1299
tom1299 / openUrlWithProxy.groovy
Created May 3, 2018 13:18
Open url in groovy using a http proxy
setProxies("http");
setProxies("https");
println 'http://www.google.com'.toURL().text
def setProxies(String protocol) {
String port = new URI(System.getenv("${protocol}_proxy")).port
String host = new URI(System.getenv("${protocol}_proxy")).host
System.setProperty("http.proxyHost", host);