Java: Simple Read/Write of Maps as Json

You can use tools like Jackson to serialize and deserialize maps to json an vice versa.

Example

@Test
public void loadMapFromFileAndSaveIt(){
    Map<Object, Object> map = loadMap("map.json");
    map.put("8", "8th");
    map.remove("7");
    saveMap(map,"/path/to/map2.txt");
}

private Map<Object, Object> loadMap(String string) {
    ObjectMapper mapper = new ObjectMapper(); //should be initialized outside!
    try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("map.json")) {
        return mapper.readValue(in, HashMap.class);
    }catch (Exception e) {
        throw new RuntimeException(e);
    }
}

private void saveMap(Map<Object, Object> map,String path) {
    try (PrintWriter out = new PrintWriter(path)) {
        out.println(toString(map));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public String toString(Object obj) {
    try (StringWriter w = new StringWriter();) {
        new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true).writeValue(w, obj);
        return w.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

If the file map.json on your classpath contains

{
 "1":"1th",
 "2":"2th",
 "3":"3th",
 "4":"4th",
 "5":"5th",
 "6":"6th",
 "7":"7th"
}

The code above will modify it and writes it to a file /path/to/map2.txt that will contain the modified map.

{
  "1" : "1th",
  "2" : "2th",
  "3" : "3th",
  "4" : "4th",
  "5" : "5th",
  "6" : "6th",
  "8" : "8th"
}

 

Unix tools introduced. Today: rsync

rsync is a very cool tool that can be used to copy files between hosts or between directories on the same host. Like the term ‘sync’ suggests the copy process can be controlled into great detail to modulate rsync’s behavior.  Take a look at the available options under: https://linux.die.net/man/1/rsync

This is my list of cool options.  I start with the most basic usage. The following command can be used to copy, and later on sync two directories.

rsync -avn /source/dir /target/dir  

The command ‘archives’ file attributes (-a) and displays some status info (-v).

In the given form, the command only does a dry-run (-n). To execute the command remove the -n.

The command uses the short form of --archive (-a) which translates to (-rlptgoD).

  • -r – recursive copy
  • -l – copy symlinks as symlinks
  • -p – set target permissions to be the same as the source
  • -t – set target mtime to be the same as the source. Use this to support fast incremental updates based on mtime.
  • -g – set target group to be the same as the source
  • -o – set target owner to be the same as the source
  • -D – if remote user is superuser this recreates devices and other special files.

More cool options

Move

--remove-source-files This will remove copied files from source.

Update

--update This forces rsync to skip any files which exist on the destination and have a modified time that is newer than the source file.

Delete

--delete Delete files on target that do not exist in source tree.

Backup

--backup Make a backup of modified or removed files on target.

--backup-dir=date +%Y.%m.%d Specify a backup dir on target.

What to copy?

--min-size=1 Do not copy empty files. This can be particularly interesting if you have corrupted files in the source.

--max-size=100K Copy only small files. Can be used to handle small and large files differently.

--existing Only override files that already exist on the target. Do not create new files on target.

--ignore-existing Only copy files that do not exist on target.

--exclude-from Define excludes in a file.

Scheduling, Bandwidth and Performance

--time-limit Ends rsync after a certain time limit.

--stop-at=y-m-dTh:m Ends rsync at a specific time.

--partial Allows partial copies in case of interruptions.

--bwlimit=100 Limits bandwidth Specify KBytes/second. Good option if transfer of large files is required.

Output

  • -h output numbers in a human-readable format.
  • --progress display progress.
  • -i log change info.
  • --log-file= define a log file.
  • --quiet no output.
  •  -v Output status info. You can add more ‘v’.
  • Forgot to log any progress info? Use the following command to see what rsync is about to do.
     ls -l /proc/$(pidof rsync)/fd/*

 

Erfolgreiche Methoden in der Softwareentwicklung

Heute möchte ich einmal ein paar Methoden in der Softwarentwicklung vorstellen, die ich seit Jahren erfolgreich verfolge. Leider sind viele der vorgestellten Methoden ziemlich unbekannt. Aber es existiert ja auch viel schlechte Software….

Rabbit Prototyping

Beim Rabbit Prototyping geht es darum, möglichst schnell Fortschritte zu erzielen und sei es nur für kurze Zeit. Die Fortschrittskurve, die dabei entsteht, könnte von einem hüpfenden Kaninchen stammen, daher der Name. Vorteile des Rabbit Prototypings: Die meiste Zeit geht es voran. Alle haben etwas zu tun. Kaninchen sind putzig.

Bugfree Developement

Viele Softwareteams führen komplizierte Methoden ein oder heuern gar weitere Teams an um Qualitätsicherung und ausgeklügelte Testverfahren zu implementieren. Dies ist alles nicht nötig. Mit Bugfree Development lässt sich eine Menge Geld sparen. Eigentlich braucht man die Entwickler nur zu bitten, ab sofort keine Bugs mehr zu produzieren. Oft reicht eine klare Ansage  um von den Vorteilen des Bugfree Developments zu profitieren. Wer einmal bugfree entwickelt hat, wird es nicht mehr missen wollen. Vieles ist einfach Einstellungssache!

Deadly Standup

Kleine Teams sind oft effektiver. Aber wie hält man das Team klein? Diese kompetitive Erweiterung zu Scrum wirkt Wunder: Die Teammitglieder stellen sich im Kreis auf. Wer zuletzt steht hat gewonnen. Beim Deadly Standup geht es ausnahmsweise einmal nicht darum die Arbeit jedes einzelnen zu erleichtern. Stattdessen sagen sich alle mal so richtig die Meinung. Da darf es auch ruhig mal persönlich werden. Wer will darf das Standup verlassen, ist aber dann der Looser.

Corona App

Wie können sich Handydaten nutzen lassen, um gezielt Infektionsketten zu erkennen?

1. Der Big Data Approach –  Der Staat sammelt  alle Handydaten ein und markiert bei Bedarf einzelne Punkte als Verdachtsfälle, bzw. als krank und gesund. Ein Alptraum? Aber möglicherweise immer noch besser, als für Jahre mit seinen Liebsten in der Stadtwohnung fest zu sitzen.

2. Der Small Data Approach – Die Bürger protokollieren selbst Treffen mit einer App. Im Falle einer Infektion informiert man per Knopfdruck relevante Kontaktpersonen. Der Vorteil gegenüber Variante 1, es findet keine Überwachung statt. Erst im Bedarfsfall werden Daten geteilt. Die Bürger wirken dabei aktiv mit.

Dieses Verfahen würde sogar analog funktionieren, aber mit digitaler Unterstützung könnte die Benachrichtigung und Erfassung genauer erfolgen.

Wie könnte sich Variante 2 umsetzen lassen? Das Protokollieren von Treffen erscheint auf den ersten Blick recht mühsam. Doch mit etwas Phantasie lassen sich hier sicherliche technische Lösungen finden, die einfach zu nutzen sind.

 

 

Impact of non-pharmaceutical interventions (NPIs) to reduce COVID-19 mortality and healthcare demand

Nicht-Pharmazeutische Strategien zur Bekämpfung von Corona.

https://www.imperial.ac.uk/media/imperial-college/medicine/sph/ide/gida-fellowships/Imperial-College-COVID19-NPI-modelling-16-03-2020.pdf

“Die Aussichten sind verzweifelnd. “, sagt Christian Drosten, und folgert, dass wir schneller einen Impfstoff entwickeln müssen als sonst. Zur Not unter Umgehung der Regularien.

“Adding household quarantine to case isolation and social distancing is the next best option, although we predict that there is a risk that surge capacity may be exceeded under this policy option(Figure 3 and Table 4). Combining all four interventions (social distancing of the entire population, case isolation, household quarantine and school and university closure)is predicted to have the largest impact, short of a complete lockdown which additionally prevents people going to work.”

Abbildung 3 zeigt, was passiert, wenn man die  Maßnahmen  nach fünf Monaten (blau hinterlegter Bereich) lockert. Soll heißen, um die rote Linie (Krankenhauskapazitäten) nicht zu überschreiten, müssen die kombinierten Maßnahmen bestehen bleiben bis mit Hilfe von Medikamenten eine Abflachung der Kurve erreicht werden kann.

Re: FAZ lesen macht optimistisch…

… eben erhielt ich folgende Rückmeldung zum letzten Post (paraphrasiert)

“Mich hatte es letzte Woche auch kräftig erwischt. Ich hätte nicht vermutet Corvid19 gehabt zu haben, nach dem Artikel bin ich mir nicht mehr so sicher: Der ganze Verlauf war äußerst merkwürdig (38.7 Fieber, nächster Tag nix, dann wieder Fieber, dann wieder “gesund”, nächster Tag Fieber. An einem Tag bin ich aufgewacht und habe gedacht, meine Lunge zerreißt – einmal Schleim und recht viel Blut (20%?) gespuckt, 6 Stunden später wieder alles gut ….) Soweit, so merkwürdig. Was ich aber echt komisch fand und bis eben mit der Viruserkrankung nicht recht in Verbindung gebracht habe: Geschmacksverlust. 4 Tage habe ich so gut wie nichts geschmeckt und auch nichts gerochen und machte mir schon Sorgen darob. Starkes Parfum direkt in die Nase roch immerhin ein bisschen.”

Die Hoffnung: Die Dunkelziffer und die Durchseuchung sind möglicherweise schon weiter fortgeschritten als bisher angenommen. Wenn die Erkrankten eine längere Immunität entwickeln, könnten die nächsten Wellen deutlich glimpflicher verlaufen. Das Problem: dies macht es für Risikogruppen derzeit nicht weniger riskant an belebte Orte zu gehen. Durchhalten!!!

Zu Dreist?

Hab mir gerade bei den Nachbarn* ein Päckchen Nudeln geliehen. Unsere waren aus und im Laden gab es keine mehr.

* bei allen Nachbarn. 😀