Skip to content

Instantly share code, notes, and snippets.

@bockor
bockor / gist:8ade83eef84cbc57da84390525838d2f
Created July 2, 2025 11:51
SQLITE DATE - TIME - DATETIME notes
SQLITE DATE - TIME - DATETIME YOUTUBE TUT
https://www.youtube.com/watch?v=nJRvz5Rhrx0
# Insert DEFAULT timestamp "now" as INTEGER
# and unixepoch prior SQLITE 3.38 (2022-02-22).
####################################################
create table t1 (c1 TEXT, c2 INTEGER DEFAULT (strftime('%s', 'now')));
insert into t1(c1) values ('drink'),('eat');
@bockor
bockor / gist:f3b450244ca72330f97ca1ed72da18e5
Created June 14, 2025 08:52
python dictionary nested get
"""
The walrus operator “:=” is an operator used to evaluate, assign, and return value from a single statement in Python.
It was introduced in Python 3.8
"""
sss = {'color': 'red', 'size': 'l', 'clips': {'number': 6, 'material': 'metal'}}
if value := sss.get("clips", {}).get("number"):
print(f"Number of clips: {value}")
"""
@bockor
bockor / gist:6a6b74b89ec22a59d8a6e77023baa3a8
Last active June 8, 2025 16:57
Convert a python dictionary into json format. Find and explain the errors
import json
my_favorite = {"product": "Stella", "flavors": ["walnut", "prune"], "ranking": None, "price": 1.15, "in_stock": False }
"""
json.dumps() function converts a subset of Python objects into a json string.
"""
my_favorite_json = json.dumps(my_favorite , indent=2, sort_keys=True)
print(my_favorite_json)
@bockor
bockor / gist:cbf433ef785470b578037a749c629b7c
Created April 30, 2025 06:37
playgarden for api - jwt
# app.py
# https://stackoverflow.com/questions/74213961/how-create-endpoints-in-multiple-files
# https://flask.palletsprojects.com/en/stable/blueprints/
from flask import Flask, jsonify, request
from candy import candy_bp
from beer import beer_bp
from netz import netz_bp
@bockor
bockor / gist:0cd1b3bfe213b0708ee82267c60574ec
Created December 11, 2024 12:36
namedtuple new style
from typing import NamedTuple
class PJ (NamedTuple):
make: str
model: str
color: str
straps: int
price: float
stars: int
@bockor
bockor / gist:fd1871f97adc9eae3064c96811b19951
Created February 13, 2023 14:33
python sorting ipaddresses
# try to sort ipv4 addr as strings
ss = ['114.122.102.2','114.122.11.1','118.123.12.13','122.14.113.3','192.144.1.5']
print([str(s) for s in sorted(ss)])
# this does not look righ to me ... let's invoke the ipaddress module
from ipaddress import IPv4Address
# and create the ipv4 addr instances
sso = [IPv4Address(s) for s in ss]
#print the sorted ipv4 addr
print([str(s) for s in sorted(sso)])
@bockor
bockor / gist:190656c3e75fc9716d3d93fd3907bb8b
Last active May 24, 2021 09:07
python difference two dates in years
import datetime
d1_date_str='29/08/1962'
d2_date_str= '06/06/1968'
d1_date_obj=datetime.datetime.strptime(d1_date_str, '%d/%m/%Y')
print(d1_date_obj.date())
d2_date_obj=datetime.datetime.strptime(d2_date_str, '%d/%m/%Y')
print(d2_date_obj.date())
#create datetime.timedelta object
diff = d2_date_obj - d1_date_obj
print(type(diff))
import zipfile
from datetime import datetime
# create zip file name
zip_file_name = 'myzip_{}.zip'.format(datetime.now().strftime("%d%m%Y"))
# create a ZipFile object and compress
zipObj = zipfile.ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED)
# Add multiple files to the zip
@bockor
bockor / gist:d1a1547b9e37a92f45c2c5fc14b3a096
Created March 3, 2021 11:51
ipython // count number of files per file extension type in directory
from collections import Counter
file_list=!find /home/bruno/tbc/uwsgi/ -type f -name "*"
#print(file_list)
found_ext=[]
for f in file_list:
found_ext.append(f.split('/')[-1].split('.')[-1])
#print(found_ext)
print(Counter(found_ext))
@bockor
bockor / gist:8264a1ae4bf54e2fbc00121dc8dd0e67
Last active March 3, 2021 12:14
infoblox notes // ipam utilization + tls/ssl unsecure messages surpression
import requests
import urllib3
import json
# Surpress SSL/TLS >Insecure Warnings
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
session = requests.Session()
session.auth = ('admin', 'infoblox')