Freddy Ewald Personal Website

Posts


  • How to learn efficient and effective

    Whenever I need to memorize things of the form A -> B, I use Anki. This free software comes for many platforms: Mac OS, Windows, Linux and allows the creation of flashcards. After creating the cards you can then go and quiz yourself and rate how good your answer was. Based on that an algorithm decides how often to show you the same card again. I personally find it super effective – if you don’t cheat.

  • Shell: Find all running java processes

    With the following command you can find all currently running java processes:

    ps aux | grep java

    Of course this works for any arbitrary process.

  • Better feeds with JSONfeed

    It seems that there is finally a format which can replace the old and often ugly looking XML documents. JsonFeed is dead-simple and provides the same functionality as XML based feeds. This simple example illustrates how nice and clean feeds in JSON can look like:

    {
    	"version": "https://jsonfeed.org/version/1",
    	"title": "My Example Feed",
    	"home_page_url": "https://example.org/",
    	"feed_url": "https://example.org/feed.json",
    	"items": [
    		{
    			"id": "2",
    			"content_text": "This is a second item.",
    			"url": "https://example.org/second-item"
    		},
    		{
    			"id": "1",
    			"content_html": "<p>Hello, world!</p>",
    			"url": "https://example.org/initial-post"
    		}
    	]
    }
    

    In the next days/weeks I will update this blog, which runs on Jekyll, and serve a JSONFeed for those interested.

  • Java Enum with values

    I wanted to be able to parse Java enums from a JSON file by not only referring to the numerical value of the enum and also the text in the JSON file should not be all uppercase. I wanted to be able to choose a different display value from the inner value in the Java code. So I came up with this code:

    /**
     * This is an enum which has an inner value for each
     * enumeration value. This allows parsing not only from
     * numerical values but also by their inner name.
     */
    public enum MySampleEnum {
        STARTING("starting"),
        RUNNING("running");
        STOPPING("stopping");
    
        /**
         * Inner value of the enumeration.
         */
        private String text;
    
        /**
         * Private constructor.
         * @param text Inner value of the enumeration.
         */
        private JobRunTypeEnum(String text) {
            this.text = text;
        }
    
        /**
         * Returns the string representation of the current enumeration.
         * @return Value of the enumeration.
         */
        @Override
        public String toString() {
            return text;
        }
    	/**
    	 * Gets the right enumeration value from a given String, if it exists.
    	 * @param text The String to search for.
    	 * @return Enum value if found, null otherwise.
    	 */
    	public static MySampleEnum fromString(String text) {
    	    if (text != null) {
    	        for (MySampleEnum val : MySampleEnum.values()) {
    	            if (text.equalsIgnoreCase(val.text)) {
    	                return val;
    	            }
    	        }
    	    }
    	    return null;
    	}
    }
    

    To achieve this, it is necessary to overwrite the default constructor of the enum and give it a parameter which is a String with the inner value. Also there needs to be a private field to hold the value. The toString() method should return the inner value and is therefore overridden.

    The best thing about this approach is in my eyes, that I am now able to parse the enum from any String just by calling MySampleEnum.fromString("running"). This allows me to integrate this in my JSON-parser and create more meaningful JSON objects.

  • Python Base62 Encoder & Decoder

    Base62 encoding is commonly used for ids where space matters. Many URL shortener service use this and also youtube calculates its video ids in Base62. The advantages are obvious. Instead of using the id 1337, you can just use LZ. This saves 50% (in this case, usually more) of the overall traffic needed to transmit this id. However, Base62 is not an ecryption.

    I created an easy to use Python module and put it on my GitHub account. As I try to improve code quality, this module has 100% code coverage and also passes all unit tests.

Page: 9 of 14