Sunday, April 22, 2018

Mocking in Python

How do I mock in Python?

Here's how:

the key is to import unittest.mock, and then add the @mock.patch decorator to the unit tests where you want to use a mock object. This causes it to pass a mock object in.

In the second test below, notice that i am causing the mock object to throw an exception.

from unittest import TestCase, mock
from Commander import Commander
from Command import Command

class CommanderTests(TestCase):

    def test_whenrunAll_commandIsRan(self, mockCmd):
        commander = Commander();



    def test_whenrunAll_andCommandThrowsException_ExceptionDoesNotBubble(self, mockCmd):
        commander = Commander(); = Exception();



Saturday, March 24, 2018

Create shortcut to run python script from Notepad++

1. Run
2. Type cmd /C cd /d $(CURRENT_DIRECTORY) && python -i "$(FULL_CURRENT_PATH)" 
3. Click Save...
4. Choose a key combo, like Ctrl+F5
5. Click OK

Now you can press the shortcut to run your current script

Tuesday, March 20, 2018

Simulate key press event in Chrome

This is the correct way to send key events in chrome

var e = document.createEvent("Event")
e.keyCode = 38
e.which = 38


Note: 38 = up arrow


Searched for:
1. keyboardEvent code is empty
2. Chrome simulate key press

Tuesday, March 13, 2018

Paint in Raspberry Pi

I need to be able to create image files in Raspberry Pi (raspbian)

1. Install xpaint
  • sudo apt install xpaint

2. Open from the menu
  • 1. Pi button
  • 2. Graphics
  • 3. Xpaint

Sunday, March 11, 2018

how to make it so git doesn't prompt for password

Every time I push in git I have to enter my password, and I don't want to

1. ssh-keygen -t rsa -b 4096

Note: to avoid entering a password when I push, I left the password blank here. 
2. cat ~/.ssh/ | xclip -sel clip
Note: I didn't have xclip, so I had to install like this >> sudo apt install xclip
3. Add the key here<username>/ssh-keys/
4. Clone using this: git clone<username>/<reponame>.git

1. Raspbian OS on raspberry Pi
2. Using git with Bitbucket

Sunday, February 4, 2018

Sync over Async anti-pattern in C#

I was working on optimizing some code at work. This code was calling PostAsync to a web service. The problem was that Task.Wait() was used. This is called the Sync over Async anti-pattern, and causes a blocking wait. This basically means that thread is wasted, and could be causing unresponsiveness (like your UI freezing, depending on where you called it from)

Note: This also happens if you try to use the Task.Result right after the async call.

Here's an example:
        public string GetWeatherData_SyncOverAsync_Wait(string City)
            var http = new HttpClient();
            var url = $"{City}&APPID={key}";

            var responseTask = http.GetAsync(url);
            var contentTask = responseTask.Result.Content.ReadAsStringAsync();
            string responseData = contentTask.Result;
            return responseData;

await the Task. This releases the thread back to the threadpool until the task completes.

Step 1 - Put async in the method signature.

Step 2 - Make your method return a Task<T>.

Step 3 - await the async call

Step 4 - You will have to repeat these steps in the caller too, and that caller's callers. Basically you need async/await all the way up

        public async Task<string> GetWeatherDataAsync(string City)
            var http = new HttpClient();
            var url = $"{City}&APPID={key}";

            var responseTask = await http.GetAsync(url);
            string responseData = await responseTask.Content.ReadAsStringAsync();
            return responseData;

Sunday, January 21, 2018

Getting started with ReactJS

I followed a video tutorial about the basics of ReactJS. I created a Shopping List page where you can add items to the shopping list and remove them. Simple, yet an effective way to get introduced to using ReactJS.

Getting started
  1. Create a new pen on CodePen - 
  2. Settings > Javascript > Quick-add
    1. React
    2. React-DOM
    3. Then add JavaScript Preprocessor = Babel

MUST GO IN THIS ORDER!!!! Do not put react-dom first


The div here is allows React to hook into the page.

The CSS here is optional

Create the component

Create a function that generates list items
This is called a stateless component. 

Set state in the ShoppingList component
I'm initializing the state to a list of items on the shopping list

Hooking the state up to the HTML
I'm looping through the shopping list items and creating HTML list items and wiring up an onClick handler. This onClick handler is going to remove items when you click them (the actual code for that part is shown in the next section)

Add OnClick function
Put the onClick function inside the Shopping List component

Add a button and textbox that allows us to add more items

2. My code pen for this: 

There was an error in this gadget