Skip to content

Instantly share code, notes, and snippets.

View peterdalle's full-sized avatar

Peter M. Dahlgren peterdalle

View GitHub Profile
@peterdalle
peterdalle / convertip.sql
Last active July 15, 2018 18:11
Convert IP number to IP address in MySQL
# http://lite.ip2location.com/
# http://stackoverflow.com/questions/3650006/get-country-of-ip-address-with-php
# IP Number = 16777216*w + 65536*x + 256*y + z
# where IP Address = w.x.y.z
#
# To reverse IP number to IP address,
# w = int ( IP Number / 16777216 ) % 256
# x = int ( IP Number / 65536 ) % 256
# y = int ( IP Number / 256 ) % 256
@peterdalle
peterdalle / convertduration.vb
Created April 4, 2015 14:35
Convert ISO 8601 duration in the format PT::M::S to seconds
''' https://developers.google.com/resources/api-libraries/documentation/youtube/v3/csharp/latest/classGoogle_1_1Apis_1_1YouTube_1_1v3_1_1Data_1_1VideoContentDetails.html#a0527b84283db20cf2ef8f3de22f65606
Dim ts As TimeSpan = System.Xml.XmlConvert.ToTimeSpan(vid.ContentDetails.Duration)
Me.Length = ts.TotalSeconds()
@peterdalle
peterdalle / CreateBuildDate.bat
Last active August 29, 2015 14:20
Create property for build date in VB.NET
@echo off
echo Build date %DATE%
REM Write to file: BuildDate.vb
echo ' This file is autogenerated by CreateBuildDate.bat. Changes here will be lost. > BuildDate.vb
echo Namespace Snuffbox.Configuration >> BuildDate.vb
echo Public Partial Class Settings >> BuildDate.vb
echo Public Shared ReadOnly Property BuildDate As String >> BuildDate.vb
echo Get >> BuildDate.vb
echo Return "%DATE% %TIME:~0,8%" >> BuildDate.vb
@peterdalle
peterdalle / bensinpris.py
Last active August 29, 2015 14:24
Hämta det billigaste bensinpriset i Göteborg
# Visa det billigaste bensinpriset i Göteborg, via bensinpriser.se.
import requests # pip install requests
from bs4 import BeautifulSoup # pip install BeautifulSoup4
r = requests.get('http://www.bensinpriser.se/v%C3%A4stra-g%C3%B6talands-l%C3%A4n/g%C3%B6teborg/g%C3%B6teborg')
soup = BeautifulSoup(r.text, "lxml") # pip install lxml
rowIndex = 0
for row in soup.select(".resulttable tr"):
rowIndex = rowIndex + 1
@peterdalle
peterdalle / database-size.sql
Last active June 8, 2019 13:44
Get current MySQL database size in MB
-- Group by database (MB).
SELECT table_schema 'Database Name',
SUM(data_length + index_length ) / 1048576 'Database Size (MB)',
SUM(data_free) / 1048576 'Free Space (MB)'
FROM information_schema.TABLES
GROUP BY table_schema;
-- Group by database and table (bytes).
SELECT table_schema 'databasename',
table_name 'tablename',
@peterdalle
peterdalle / ShowInstalledPackages.r
Created July 21, 2015 17:59
List installed packages and versions
ip <- as.data.frame(installed.packages()[,c(1,3:4)])
rownames(ip) <- NULL
ip <- ip[is.na(ip$Priority),1:2,drop=FALSE]
print(ip, row.names=FALSE)
# From http://www.r-bloggers.com/list-of-user-installed-r-packages-and-their-versions/
@peterdalle
peterdalle / SverigesRiksdag.r
Last active August 29, 2015 14:27
Hämta namn, parti, valkrets och plats för alla 349 ledamöter i Sveriges riksdag
library(rvest) # install.packages("rvest")
library(plyr) # install.packages("plyr")
# Get html from url: Sveriges riksdagsledamöter 2014-2018.
riksdag.html <- html("https://sv.wikipedia.org/wiki/Lista_%C3%B6ver_ledam%C3%B6ter_av_Sveriges_riksdag_2014%E2%80%932018", encoding = "UTF8")
# Import table from html.
riksdag.tables <- html_table(riksdag.html, fill=TRUE, trim=TRUE)
riksdag.df <- riksdag.tables[[1]]
@peterdalle
peterdalle / ConvertToUTF8Collation.sh
Created August 21, 2015 18:36
Convert MySQL tables to UTF-8 collation in Bash
mysql --database=dbname -B -N -e "SHOW TABLES" \
| awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; SET foreign_key_checks = 1; "}' \
| mysql --database=dbname &
# http://stackoverflow.com/questions/105572/a-script-to-change-all-tables-and-fields-to-the-utf-8-bin-collation-in-mysql
@peterdalle
peterdalle / RemoveLinesInFile.py
Created November 18, 2015 14:25
Remove lines in file A that are present in file B
#!/usr/bin/python
# Delete all lines in file A (keep.txt) that are present in file B (delete.txt).
# Rules:
# 1. If line is found in B but not in A, then delete (i.e., do not merge files).
# 2. If line is found in B and in A too, then delete (i.e., delete duplicates).
# Read file with lines that we shall keep.
keepfile = open("keep.txt")
keeplines = keepfile.readlines()
@peterdalle
peterdalle / CreateNewLibraryFolder.r
Created November 26, 2015 12:31
Install R libraries to the C:\Users\<You>\R\win-library\x.y\ folder in case of error message "warning in install.packages lib='...' is not writable"
dir.create(Sys.getenv("R_LIBS_USER"), recursive = TRUE)
# http://stackoverflow.com/questions/5059692/unable-to-update-r-packages-in-default-library-on-windows-7