Misc Snippets

Data Normalizing Formula newVal = (oldVal-min) / (max-min) Ugly Scala Example package com.hascode import scala.collection.mutable.LinkedList object NormalizerExample extends App { val dataSet = LinkedList(1., 6.5, 3., 6.2, 20., 31.2, 50.2, 12., 0.24, 1.224, 2.2, 3.) for ((num, index) <- dataSet.zipWithIndex) { dataSet(index) = (num - dataSet.min) / (dataSet.max - dataSet.min) } println("Normalized: " + dataSet) } Normalized: LinkedList(0.01521216973578863, 0.12921819759798853, 0.05947594797769014, 0.12324029048767723, 0.3982240175619966, 0.6213992163469515, 1.0, 1.0, 0.07531115879828326, 0.40498283261802576, 0.7319742489270387, 1.0)

March 1, 2010 · 1 min · 69 words · Micha Kops

Neo4j Snippets

Aggregate existing Labels MATCH (n) RETURN DISTINCT labels(n) Or unwinding label pairs MATCH (n) WITH DISTINCT labels(n) AS labels UNWIND labels AS label RETURN DISTINCT label ORDER BY label

March 1, 2010 · 1 min · 29 words · Micha Kops

PHP Snippets

WordPress anonymize IP in comments Add as last line in theme’s functions.php: function wpb_remove_commentsip($comment_author_ip) { return '127.0.0.1'; } add_filter('pre_comment_user_ip','wpb_remove_commentsip'); WordPress remove version info Add as last line in theme’s functions.php: function remove_version_info() { return ''; } add_filter('the_generator', 'remove_version_info'); List of all WordPress Hooks

March 1, 2010 · 1 min · 43 words · Micha Kops

Python Snippets

Virtual Environments Create and activate a venv python -m venv /path/to/new-env (1) source /path/to/new-env/bin/activate (2) 1 Creates a new virtual environment 2 Activate the new virtual environment Deactivate venv deactivate Save used dependencies to a file pip freeze > dependencies.txt Install dependencies from file pip install -r dependencies.txt Storing and fetching Credentials from the System Keyring I am using jaraco/keyring here: pip install keyring import keyring keyring.set_password("system", "db.sample.password", "xoxo") print(keyring.get_password("system", "db.sample.password")) ...

March 1, 2010 · 1 min · 100 words · Micha Kops

Scala Snippets

SBT – Eclipse Plugin Add to your ~/.sbt/plugins/plugins.sbt addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.1")

March 1, 2010 · 1 min · 13 words · Micha Kops

Spring Boot Snippets

Define and Configure Log Groups This allows to configure a group of loggers at the same time Define a log group named myaspect with two packages application.properties logging.group.myaspect=com.hascode.package1,com.hascode.package2 Configure the log group and set all loggers to level TRACE application.properties logging.level.myaspect=TRACE This is also possible as parameter on startup java -Dlogging.level.myaspect=TRACE myapp.jar Use JUnit 5 with Spring Boot Use newer versions of Surefire and Failsafe plugins: <properties> [..] <maven-failsafe-plugin.version>2.22.0</maven-failsafe-plugin.version> <maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version> </properties> ...

March 1, 2010 · 6 min · 1082 words · Micha Kops

SQL Snippets

Count unique / all values SELECT COUNT(DISTINCT <FIELDNAME>), COUNT(ALL <FIELDNAME>) FROM <TABLE>; Partially anonymize e-mail addresses UPDATE <TABLE_NAME> SET <EMAIL_FIELD>= INSERT( <EMAIL_FIELD>, POSITION('@' IN <EMAIL_FIELD>), 100, CONCAT(FLOOR(1 + (RAND() * 100)),'@hascode.com')) WHERE POSITION('@' IN <EMAIL_FIELD>)>0; Find duplicate entries SELECT COUNT(*), <FIELDNAME> FROM <TABLENAME> GROUP BY <FIELDNAME> HAVING COUNT(*)>1; MySQL Fix Zero Dates for Data Imports Error: ERROR 1067 (42000) at line 1234: Invalid default value for ‘datefield’ In newer MySQL Versions zero in date or zero dates are forbidden .. check this with: ...

March 1, 2010 · 1 min · 150 words · Micha Kops

XML Snippets

Ignore Namespaces in XPath Query e.g. Query for all xxx nodes ignoring their namespace: xmllint --xpath '//*[local-name()="xxx"]' input.xml An example parsing URLs from a sitemap XML. The URLs are located in //url/loc where all nodes are bound to the namespace http://www.sitemaps.org/schemas/sitemap/0.9. The following query ignores the namespace xmllint --xpath '//*[local-name()="url"]/*[local-name()="loc"]' sitemap.xml Pretty Print XML in the Console using xmllint echo '<blogs><blog url="https://www.hascode.com/">hasCode.com</blog></blogs>' | xmllint --format - <?xml version="1.0"?> <blogs> <blog url="https://www.hascode.com/">hasCode.com</blog> </blogs> ...

March 1, 2010 · 1 min · 99 words · Micha Kops