Skip to content

Instantly share code, notes, and snippets.

View imanabu's full-sized avatar

Manabu Tokunaga imanabu

View GitHub Profile
@imanabu
imanabu / NLOG app.config AutoRotate7 Days
Last active September 25, 2023 02:51
NLog Configuration That Auto-Rotate with Config Section
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="console" xsi:type="Console" layout="${longdate} ${callsite} ${level} ${message}"/>
<target name="logfile" xsi:type="File" fileName="C:\temp\log.txt" layout="${longdate} ${callsite} ${level} ${message}"/>
<target name="rotatelog" xsi:type="File"
layout="${longdate} ${logger} ${message}"
fileName="c:/temp/logs/alogfile.txt"
@imanabu
imanabu / parseCSV.scala
Last active October 26, 2020 03:42
Scala Fully Parse CSV lines from a File, supports ignoring commans in quoted strings. Can generate a JSON DTO.
package dtos
import play.api.libs.json.{Format, Json}
case class ITableRow(
cols: Seq[String]
)
object ITableRow {
implicit val jsonFormatter: Format[ITableRow] = Format(Json.reads[ITableRow], Json.writes[ITableRow])
@imanabu
imanabu / JsonReadOptionDoubleJson
Last active May 14, 2020 13:52
implicit converter lets you read Json into Option[Double] Scala Data Type
object DoubleReads {
def reads(json: JsValue): JsResult[Option[Double]] = json match {
case JsNumber(n) => JsSuccess(Some(n.toDouble))
case JsString(s) => JsSuccess(Some(s.toDouble))
case _ => JsSuccess(None)
}
}
// Later in your code where you need
implicit val odr: json.Reads[Option[Double]] = DoubleReads.reads
@imanabu
imanabu / gist:ea2c2bc206caa9d93c08544cf51c7c77
Created February 11, 2020 04:44
Encode and Decode a 128-bit UUID to DICOM UID Segment
private static String dotEncode(long n) {
String s = String.format("%d", n);
if (s.startsWith("-")) {
return ".2" + s.substring(1);
}
return ".1" + s;
}
private static long dotDecode(String s) {
if (s.startsWith("1")) {
@imanabu
imanabu / json-date-time-serailizer.py
Created August 14, 2019 21:09
Python: Allow JSON serialize to work over datetime data type
def to_serializable(val):
"""
Allows the JSON serializer to work over datetime data type
:param val:
:return:
"""
if isinstance(val, datetime):
return val.isoformat() + "Z"
vf = type(val).__name__
@imanabu
imanabu / check-scan-dir.py
Created August 14, 2019 20:58
Python Dir Exists and Scan Directory
import os
import os.path
from pathlib import Path
if not Path.exists(Path(image_source_dir)):
raise Exception(f"The source not found: {image_source_dir}. Please correct this in Migrate.py")
index = 0
previousInfo: ImageInfo = None
for ent in Path(image_source_dir).iterdir():
@imanabu
imanabu / IgnoreForeignKeyConstriants.sql
Last active August 9, 2019 22:22
mySQL Ignore Foreign Key Constraint Checks When Deleting
use somedb;
SET FOREIGN_KEY_CHECKS=0;
delete from account where firstname='8';
select id, email, firstname, lastname from account;
@imanabu
imanabu / classLoaderDemo.java
Last active August 9, 2019 22:19
Read a Java8 Reousrce File and then Parse into String in 3 lines
ClassLoader classLoader = getClass().getClassLoader();
String fn = classLoader.getResource("some-file.txt").getFile();
String txt = new String(Files.readAllBytes(Paths.get(fn.substring(1)))); // Skips the first / of /c:/ in Windows
@imanabu
imanabu / MacThreadDump.md
Last active August 9, 2019 22:17
MacOS Taking App Thread Dump from Terminal
  1. Browse to the /Applications/.app/Contents/MacOS/ executable in Finder,
  2. In the context menu choose Open With, Terminal.
  3. To get a thread dump press Ctrl+\ in the Terminal window.
@imanabu
imanabu / ClassAndInstanceInheritance.ts
Created March 4, 2019 22:48
Do Not Confuse Class and Instance Inheritance With TypeScript
/**
* Combined class and instance inheritance in TypeScript
* This looks deceptively simple, but there are two layers of inheritance that's happening
* One is the Class inheritance. Another is the actual instance inheritance that's chained
* through the children array. The parent has its' instance based factory to generate its
* own children either daughters of sons
*/
class person {
constructor(name, parent = null) {
this.name = name;