A union of curiosity and data science

Knowledgebase and brain dump of a database engineer


C# Dictionary from Key Value Pair String (URL)

I was searching for an elegant way to break a url into key value pairs and add them to a dictionary. 

Most of the code I came across appeared to be overkill so I wrote the function (getKeyValuePairs) below.

I return the dictionary from a function/method call because there seems to be some confusion in a few places on the web about being able to achieve this.

    class Program
    {
        static void Main(string[] args)
        {
            string message = "EventType=Information&EventDateGMT=2016-05-19 12:00:00&Source=Calling and Source Apps&Severity=99&Message=Hello World.!.";
            Dictionary<string, string> dict = getKeyValuePairs(message, '&', '=');
            foreach (var i in dict)  Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);

            Console.WriteLine("hit any key when ready."); Console.ReadKey();
        }

        private static Dictionary<string, string> getKeyValuePairs(string data, char setDelimiter, char keyValueDelimiter)
        {
            return data.Split(setDelimiter).ToDictionary(x => x.Split(keyValueDelimiter)[0], x => x.Split(keyValueDelimiter)[1]);
        }     

    }


Preparing Visual Studio 2015 For Python Development

Setup a Visual Studio Python Environment in 5 Minutes using PTVS (Python Tools for Visual Studio)

 

You'll need:

 

Download the MSI:
Git "Python Tools for Visual Studio" : https://github.com/Microsoft/PTVS
2012 and 2012: https://github.com/Microsoft/PTVS/releases/v2.1.1
2
015 : https://ptvs.azureedge.net/download/PTVS%202.2.5%20VS%202015.msi

 

Install the tools:
Double click your MSI.

 

Create a new Visual Studio  Python Project:
File >New >Project : Installed >Templates >Python
Choose Python Application. 

PTVS Comes with many different python projects types including:

  • Django Web Projects
  • Flask /Jade Web Projects
  • Python Applications (Console Apps)
  • IronPython Projects

 

Setup the Python environment in VS (if not auto detected):

In most cases PTVS will find your python environment and set the location in VS. if this is not the case, follow the below steps to tell VS where your python environment lives.

Go to  Tools >Options >Python Tools >Environment Options. 
Click add environment, add a name for your environment. ex: "Python 3.4 Annaconda".
Add the path to your python.exe. 

Test your setup:

 In Visual Studio navigate to your PythonApplication1.py file and add the following line of code:
print("hello World")

Press F5 on your keyboard or click start. 

Congratulations! You're now setup for Python development using Visual Studio. Happy Dev'n. 

Setting Up Eclipse for Python Development

Install Java: https://java.com/en/download/

Install Eclipse (Neon is the latest as of this post):  https://eclipse.org/downloads/

Install Python: https://www.python.org/downloads/

 

Startup Eclipse:
Accept the default workspace or create your own.
Get PyDev : Go to Help > Install New Software...

Click "Add...", enter "PyDev" for name
Enter the following location: http://pydev.org/updates.
Click Ok.

Check PyDev

Accept Agreement

Accept Certificate

Restart Eclipse

Setup the interpreter: go to eclipse preferences. 

 

Once in preferences: Open PyDev > Interpreters > Python Interpreters

 Select "New" > Enter a name (The name of your current python version). 
Add the path to the python executable. 

Select All Libs > click OK

Now that we have our interpreter setup, let's test all of this stuff. 

create a new project: File > New > PyDev Project

HelloWorld. Set the Grammar and the Interpreter > click finish.