Skip to content

Instantly share code, notes, and snippets.

@lucnap
lucnap / gist:4c63c94480a58806c98d0f623802200c
Created September 27, 2020 07:48
Python: map field names to indices in database cursor
def generatefieldmap(cursor):
""" Given a DB API 2.0 cursor object that has been executed, returns
a dictionary that maps each field name to a column index; 0 and up. """
results = {}
column = 0
for d in cursor.description:
results[d[0]] = column
column = column + 1
@lucnap
lucnap / gist:fc3b67f6120fc26f8696064fec94de03
Last active September 26, 2020 08:34
Compile python programs to Widows executable
Install PyInstaller
if using Virtual Env (very likely) compile spcifying the folder of the modules
Example:
pyinstaller main.py --paths venv\Lib\site-packages --onefile
otherwise you will get an error of module not found when moving the executable to another machine
This is very useful when using libraries like MySQLdb for example
@lucnap
lucnap / gist:a4c755b779e6c40b70115a593c4c2167
Created August 25, 2020 16:13
Node.Js search and replace in files in directory recursively
const fs = require('fs');
const path = require('path');
const walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach(file => {
filelist = fs.statSync(path.join(dir, file)).isDirectory()
? walkSync(path.join(dir, file), filelist)
: filelist.concat(path.join(dir, file));
@lucnap
lucnap / lunaenergianaz.php
Created April 24, 2020 14:27
Blocks outgoing order confirmation email
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class LunaEnergiaNaz extends Module {
@lucnap
lucnap / gist:c250be1333475736ddd77ad2dea854ef
Last active April 23, 2020 07:31
Cloudflare htaccess restrict IP
SetEnvIF CF-Connecting-IP "10.20.30.40" MySecretIP
<Files index.php>
order allow,deny
allow from env=MySecretIP
</Files>
@lucnap
lucnap / gist:d4f8884b51d48d7af651bf3485fd2302
Last active June 25, 2019 17:25
Resizing and Padding image to keep a constant aspect ratio with ImageMagick
Resizing and Padding image to keep a constant aspect ratio with ImageMagick
This command center any given image in an area of 768x1024 and resize to fit the aspect ratio 768x1024
magick mogrify -resize 768x1024 -background "#fff" -gravity center -extent 768x1024 *Copia.jpg
Example Batch file, ResizeAndPadImage.bat
@lucnap
lucnap / note su mysql
Created June 5, 2019 18:25
mysql secure passwords on command line
# impostare la password in modo sicuro con il seguente
mysql_config_editor set --login-path=mypath1 --host=localhost --user=username --password
# questo al fine di poter utilizzare mysqldump in modo sicuro con
mysqldump --login-path=mypath1 nomeDelDataBase > /path/dove/metto/i/backup/nomeDelDatabase.sql
@lucnap
lucnap / PrefixFromCountryISO
Created June 3, 2019 11:31
Seleziona il giusto prefisso in base al codice ISO
strNazione = Doc.CustomField(584)
prefisso = ""
Select Case strNazione
Case "HU", "HUN"
prefisso = "36"
Case "UA", "UKR"
prefisso = "380"
Case "CH", "CHE"
prefisso = "41"
@lucnap
lucnap / replace_value_in_all_fields.php
Created May 23, 2019 08:41
How to replace a string in all fields of a table in Mysql with PHP
<?php
$host = 'localhost';
$user = 'root';
$pass = '--------------';
$db = 'rmadata';
$conn = new mysqli($host, $user, $pass, $db);
$thisword = "\r\n";
@lucnap
lucnap / paginations.php
Created April 19, 2019 13:09
Paginate data in PHP from database and from json
<?php
/*
Generic functions to paginate data from a database select and from json data
thanks to
https://stackoverflow.com/a/23910460
https://stackoverflow.com/a/3707457
http://www.sqlitetutorial.net/sqlite-sample-database/
*/