Skip to content

Instantly share code, notes, and snippets.

@cbwar
cbwar / example_multithread.rs
Last active February 9, 2025 12:11
Example of using a Mutex to share data between threads
#![allow(unused)]
use std::{collections::HashMap, sync::{Arc, Mutex}};
use rand::Rng;
#[derive(Debug)]
struct UserData {
name: String,
age: u32,
stage('tests') {
steps {
sh "XDEBUG_MODE=coverage APP_ENV=test php vendor/bin/phpunit --log-junit 'reports/junit.xml' --coverage-html 'reports/coverage' --coverage-clover 'reports/coverage/coverage.xml'|| true"
}
post {
always {
junit 'reports/junit.xml'
step([
$class: 'CloverPublisher',
cloverReportDir: 'reports/coverage',
@cbwar
cbwar / validate_url.ts
Created August 30, 2022 10:45
Validate url
export function validateUrl(url: string, throws: boolean = false): boolean {
try {
new URL(url)
} catch (err) {
if (throws) {
throw new Error(`invalid url '${url}': ${err}`)
}
return false
}
@cbwar
cbwar / unserialize.php
Created April 27, 2018 13:37
Unserialize to specified class
<?php
function unserializeToClass($object, $class = 'stdClass')
{
return unserialize(preg_replace('/^O:\d+:"[^"]++"/', 'O:' . strlen($class) . ':"' . $class . '"', $object));
}
@cbwar
cbwar / ip.py
Created April 5, 2017 12:26
Python: Get all ip addresses from computer (PyQt5)
# coding: utf-8
from PyQt5.QtNetwork import QNetworkInterface, QHostAddress
def get_ips():
"""Get all ip addresses from computer
:rtype: list
"""
ip_list = []
for interface in QNetworkInterface().allInterfaces():
@cbwar
cbwar / size.py
Last active January 9, 2024 09:09
Python: Human readable file size
def sizeof_fmt(num, suffix='o'):
"""Readable file size
:param num: Bytes value
:type num: int
:param suffix: Unit suffix (optionnal) default = o
:type suffix: str
:rtype: str
"""
for unit in ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z']:
@cbwar
cbwar / mime.py
Last active June 29, 2025 09:04
Python: Get mime type from file
# https://stackoverflow.com/questions/43580/how-to-find-the-mime-type-of-a-file-in-python
import magic
mime = magic.Magic(mime=True)
mime.from_file("testdata/test.pdf") # 'application/pdf'
@cbwar
cbwar / ssl.py
Created April 5, 2017 12:13
Python: Create self-signed ssl certificate
def create_ssl_certificate(distinguished_name, key_file, cert_file, overwrite=False, X509=None):
from OpenSSL import crypto
if not os.path.exists(cert_file) or not os.path.exists(key_file) or overwrite:
k = crypto.PKey()
k.generate_key(crypto.TYPE_RSA, 1024)
if X509 is None:
X509 = crypto.X509()
@cbwar
cbwar / file_command.py
Created April 5, 2017 11:02
Python: get command to open file from windows registry
# coding: utf-8
import os
import winreg
import sys
def read_extension_regkey(extension):
path = r"Software\Classes\{}\OpenWithProgIDs".format(extension)
print("read_extension_regkey path={}".format(path))
try:
@cbwar
cbwar / windows_shares.py
Last active April 5, 2017 11:02
Python: Manage windows share
# coding: utf-8
import win32net
import win32netcon
import win32security
import win32file
def create_share(directory, share_name):
"""Create a windows share
"""