Tuesday, September 22, 2020

Simple GroupBy Function in for Python

After a lot of research I found that Python has no good groupby function that you can use in a single line (like Linq GroupBy of C#). For that, I've made a sample reusable groupby function that works with one or more keys, I wouldn't say it has great performance (actually far from it). But, it works and it is easier to use :) 

You may want to make changes based on your input:

import copy
import itertools

def yogee_groupby(collkeys):

    copied_collection = copy.deepcopy(coll)
    master_key = ""

    if len(keys) > 1:
        copied_collection = copy.deepcopy(coll)
        for key in keys:
            master_key = master_key + key
        
        for an_element in copied_collection:
            element_key = ""
            for key in keys:
                element_key = element_key + an_element[key]
            an_element[master_key] = element_key
    else:
        master_key = keys[0]

    grouped = {}
    sorted_collection = sorted(copied_collection, key = lambda item: item[master_key])
    for k, g in itertools.groupby(sorted_collection,  lambda item: item[master_key]):
        group = list(g)
        if len(keys) > 1:
            for an_element in group:
                an_element.pop(master_key, None)
        
        grouped[k] = group
    return grouped


def test_groupby_2():
    test_data = [
        {
            "E1""V1",
            "E2""V2",
            "E3": ["V31","V32","V33","V34"],
            "E3": {"E31":"V31","E32""V32","E33""V33","E34":"V34"}
        },
        {
            "E1""W1",
            "E2""W2",
            "E3": ["W31","W32","W33","W34"],
            "E3": {"E31":"W31","E32""W32","E33""W33","E34":"W34"}
        },
        {
            "E1""V1",
            "E2""V2",
            "E3": ["VV31","VV32","VV33","VV34"],
            "E3": {"E31":"VV31","E32""VV32","E33""VV33","E34":"VV34"}
        },
        {
            "E1""V1",
            "E2""V22",
            "E3": ["X31","X32","X33","X34"],
            "E3": {"E31":"X31","E32""X32","E33""X33","E34":"X34"}
        },
    ]
    grouped = yogee_groupby(test_data, ["E1""E2"])
    print(grouped)
    grouped = yogee_groupby(test_data, ["E1"])
    print(grouped)

test_groupby_2()



Tuesday, January 26, 2016

Anonymous method with Parameters 'i' -> Thread -> Iteration with 'i' -> Mess!

Problem:
You see IndexOutOfRangeException with message  "Index was outside the bounds of the array." when you thread out an anonymous method with iterator value as parameter.


Cause:
Surprised? How is it possible to see value i >= 4 (it was 4) in the 'for' loop there?
Well, it's 'possible' if your have written code as above.

Secret is that the anonymous method doesn't get called unless the Thread created doesn't becomes live. Once thread becomes live, it searches for it's anonymous method which is: MethodToCompute(NumberOfIterationPerThread[i])
Here, value of 'i' might have changed as 'for' loop to create thread runs in a different thread!

Solution:
solution is NOT to pass any value to threaded anonymous method which the parent thread may modify. In above case, I would create an extra variable to store value to pass to anonymous method which will be threaded out.

    for (int i = 0; i < NumberOfThreads; i++)
    {
        int iterationToRun = NumberOfIterationPerThread[i]; //magic line!
        threads[i] = new Thread(new ThreadStart(() => MethodToCompute(iterationToRun)));
        threads[i].IsBackground = true;
        threads[i].Start();
    }

Wednesday, June 10, 2015

NDK Directory Location on Windows 7

I am not big fan of Android on Windows 7 but that's what I've at present and now I've to install NDK for some reason. So, I downloaded Android NDK from https://developer.android.com/ndk/downloads/index.html According to the documentation, I've to double click the NDK download and it will automatically extract file. I ran and now I don't know where it has extracted the files are. I have to mention installed NDK path (ndk.dir) in "local.properties" (btw, I am using Android Studio). After searching whole C: suing below DOS command (I saw my nkd folder was "android-ndk-r10e"),


    dir /b /s /ad | findstr "android-ndk-r10e"

Found the directory. As I've downloaded 64 bit NDK, it's

    "C:\Windows\SysWOW64\android-ndk-r10e"

on my 64bit Windows 7 machine.

Hope this helps and you don't have to search C: like me.

Friday, September 19, 2014

The Ultimate Rounding Function

Your search for any requirement for rounding function is over. You need to round amount to quarter? dime? nickel? penny? a dollar? 10 dollar? Below function of single linen can provide you more than what you are looking for. for sure!
 decimal NearestOfPenny(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 0.01m, 0.5m);
 }

 decimal NearestOfNickel(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 0.05m, 0.5m);
 }

 decimal NearestOfDime(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 0.10m, 0.5m);
 }

 decimal NearestOfQuarter(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 0.25m, 0.5m);
 }

 decimal NearestOfDollar(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 1m, 0.5m);
 }

 decimal UpwardDollarOnlyIfReminderIsMoreThan70Cents(decimal amountToRound)
 {
     return UltimateRoundingFunction(amountToRound, 1m, 0.3m); 
     //i.e. 0.70 will round up. but 0.69 will be rounded to 0.. this magic is from value of 'fairness'.
 }
 
 //amountToRound => input amount
 //nearestOf => .25 if round to quater, 0.01 for rounding to 1 cent, 1 for rounding to $1
 //fairness => btween 0 to 0.9999999___. 
 //            0 means floor and 0.99999... means ceiling. But for ceiling, I would recommend, Math.Ceiling
 //            0.5 = Standard Rounding function. It will round up the border case. i.e. 1.5 to 2 and not 1.
 //            0.4999999... Non-standard rounding function. Where border case is rounded down. i.e. 1.5 to 1 and not 2.
 //            0.75 means first 75% values will be rounded down, rest 25% value will be rounded up.
 decimal UltimateRoundingFunction(decimal amountToRound, decimal nearstOf, decimal fairness)
 {
     return Math.Floor(amountToRound / nearstOf + fairness) * nearstOf;
 }

Saturday, August 30, 2014

Completely Read-Only Object as Return Value

NOTE: I am not proposing a 'secure' code. It's just to make your code dumb proof, not hack proof.
At the pinnacle of the insanity, when you have that bong affecting your normal senses, you may want to return that object from your beloved method which cannot be modified in anyway. You cannot even create a new object of the class that you are returning to have exclusive access to your return type. Anyways, whatever it can be used (?) for, it's here:
using System;
using System.Collections.ObjectModel;
using System.Security.Permissions;

public class ReadOnlyClassAsReturnValue
{
    private int sampleInt;
    // make regular property read-only by creating only 'get' call 
    public int SampleInt
    {
        get { return sampleInt; }
    }

    private bool[] sampleCollection;
    // Collections need to be made ReadOnlyCollection<T> . 
    //      NOTE: Though ReadOnlyCollection<T> can be casted as IList<T>, never 
    //          do that. It allows someone to to change collection in compilte
    //          time only to realized during runtime that it cannot be changed
    public ReadOnlyCollection<bool> SampleCollection
    {
        get { return Array.AsReadOnly(sampleCollection); } //gives ReadOnlyCollection<T>
    }

    // 'private' varialbes can only be assigned in contructor. If any variable need to be assigned after
    // object is created, it need to be made 'internal'.
    protected string sampleString = "x";
    public string SampleString
    {
        get { return sampleString; }
    }

    // Contructor is made 'protected' as this class is to be used by serious consumers only.
    // If ultimate (dumb) user is going to be outside your assembly, you can make it 
    // 'internal' instead of 'prorected' eliminating need to override this class and 
    // providing more protection than 'protected' (and 'proected internal').
    protected ReadOnlyClassAsReturnValue(int sampleInt, bool[] sampleCollection)
    {
        this.sampleInt = sampleInt;
        this.sampleCollection = sampleCollection;
    }
}

public class PrimaryUserClass
{
    // Inherit ReadOnlyClassAsReturnValue so internal methods/properties/variables can be assigned
    private class ReadOnlyObjectMaker : ReadOnlyClassAsReturnValue
    {
        public ReadOnlyObjectMaker(int sampleInt, bool[] sampleCollection)
            : base(sampleInt, sampleCollection)
        { }

        public string SampleString
        {
            set
            {
                this.sampleString = value;
            }
        }
    }

    //NO ONE CAN CHANGE THIS RETURN VALUE
    public ReadOnlyClassAsReturnValue ReadOnlyClassReturner()
    {
        ReadOnlyObjectMaker returnValue = new ReadOnlyObjectMaker(10, new bool[] { true, false });
        returnValue.SampleString = "I can assign this";
        return returnValue;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var returnValue = new PrimaryUserClass().ReadOnlyClassReturner();
        //returnValue cannot be modified. Why? Just for fun. Someone has to hack it to change anythingin returnValue.
    }
}

Tuesday, February 18, 2014

Counting Number of REAL lines in Your C# Code

If you are here, it means one (or more) of the three things: 1. You are the boss, 2. Your boss told you to count number of lines 3. You are a serious programmer.
Anyways, let's come to the point. Each programer is different.  Problem with just literally counting the line has many issues which this piece of code attempts to resolve as mentioned below:
0. Comments of // and /* kind are ignored.
1. A statement written in multiple line is considered single line.
2. brackets are (i.e. '{') not considered lines.
3. 'using namespace' line are ignored.


        private int CountNumberOfLinesInCSFilesOfDirectory(string dirPath)
        {
            FileInfo[] csFiles = new DirectoryInfo(txtPath.Text.Trim())
                                        .GetFiles("*.cs", SearchOption.AllDirectories);

            int totalNumberOfLines = 0;
            Parallel.ForEach(csFiles, fo =>
            {
                Interlocked.Add(ref totalNumberOfLines, CountNumberOfLine(fo));
            });
            return totalNumberOfLines;
        }

        private int CountNumberOfLine(Object tc)
        {
            FileInfo fo = (FileInfo)tc;
            int count = 0;
            int inComment = 0;
            using (StreamReader sr = fo.OpenText())
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (IsRealCode(line.Trim(), ref inComment))
                        count++;
                }
            }
            return count;
        }

        private bool IsRealCode(string trimmed, ref int inComment)
        {
            if (trimmed.StartsWith("/*") && trimmed.EndsWith("*/"))
                return false;
            else if (trimmed.StartsWith("/*"))
            {
                inComment++;
                return false;
            }
            else if (trimmed.EndsWith("*/"))
            {
                inComment--;
                return false;
            }

            return
                   inComment == 0
                && !trimmed.StartsWith("//")
                && (trimmed.StartsWith("if")
                    || trimmed.StartsWith("else if")
                    || trimmed.StartsWith("using (")
                    || trimmed.StartsWith("else  if")
                    || trimmed.Contains(";")
                    || trimmed.StartsWith("public") //method signature
                    || trimmed.StartsWith("private") //method signature
                    || trimmed.StartsWith("protected") //method signature
                    );
        }


There is (literally) no solution for below (un)kind of programers:
1. Write multiple statements in single line.
2. Who declare useless variables.
3.Whose every other line is debug.write
4. Inappropriate use of compiler directive (directive itself is not considered as line btw)
5. Bulk copy programmers who suffer from schizophrenia and lives only in their set of classes. Don't trust or reuse anything else.
6. Who still likes to reinvent the wheel.

Apart from above six non-solvable issues, if you can improve my code, it will be helpful to everyone.

Thursday, April 04, 2013

Firefox 20 Feature - Click To Play

If you, my friend, are annoyed by zillions of flash advertisements running on your browser for which you haven't subscribed, have a look at the latest version of Firefox 20. It has a hidden lovable feature called "Click To Play". If enabled, it will deactivate all Flash (and Java) plugins except the one which you select to activate in a page. I've a netbook ('cheap laptop') which cannot handle the army of flash advertisements so I chose to activate this hidden Click_To_Play feature on my recently updated Firefox.

How to Enable Click_To_Play:

  1. Open a new firefox tab and type "about:config" in address bar.
  2. Click on button which says "I will be careful" etc..
  3. Now search for Click_To_Play in "Search" box.
  4. Double click preference named "plugins.click_to_play". It will change value of the preference to 'true'.
  5. You are done. Go to any web site and click only the plugin which you know you want to activate.  
Happy Browsing and don't let the Flash ads bite!