Thursday, August 25, 2016

Network share is not showing new directories / files

Problem
I have a network share that's not updating. I added a new directory and put some files. When I login to another server I cannot see that new directory.

Solution
Add this registry entry:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters] “DirectoryCacheLifetime”=dword:00000000


Ref: https://thecitrixman.com/2013/02/11/scanned-files-not-showing-up-in-network-drives/

Tuesday, August 23, 2016

DateTime.ParseExact is throwing Format Exception when single H specified

Problem
DateTime.ParseExact("9", "H", CultureInfo.InvariantCulture) throws a FormatException.

It also throws the exception for the following cases:
1. "925", "Hmm"
2. "92525", "Hmmss"

Why? Because with a single "H", it tries to grab 1-2 characters. In the case of "925" it tries to grab "92" for the hour.


Solution

Let's say we want to allow the user to enter time in the following formats:

  • H
  • HH
  • Hmm
  • HHmm
  • Hmmss
  • HHmmss

What we can do is pad the string with a 0 to the left if the string length is odd. That is, if the time is in the formats H, Hmm, Hmmss, then we stick a 0 at the beginning and can simply use HH, HHmm, HHmmss.

For example


            string input = "925";
            string format = "";
            if (input.Length % 2 != 0)
                input = input.PadLeft(input.Length + 1, '0');

            switch(input.Length)
            {
                case 2:
                    format = "HH";
                    break;
                case 4:
                    format = "HHmm";
                    break;
                case 6:
                    format = "HHmmss";
                    break;
                default:
                    throw new ArgumentException("Time isn't within the expected lengths of 1-6");
            }

            DateTime dt = DateTime.ParseExact(input, format, CultureInfo.InvariantCulture);


            //do stuff with dt

Friday, August 19, 2016

Combining multiple #javascript files into one in #PyCharm using File Watcher + #Python script

Problem
I'm working on a website and currently have one large javascript file with several classes in it, which makes the code overall less readable / harder to maintain. But I also want to only include one script file in the HTML doc, and also only make the browser download one script instead of several.

Note: I am using PyCharm

Solution
There are plenty of third party tools that you can use to do this. However, I attempted to use one, and it didn't work. So instead of wasting more time, I decided to keep it simple and write my own tool that does it.

Here's the steps I did:
1. Split the big JS file into several small ones and put them in a subdirectory

2. Delete the big JS file

3. Add a Python file called combiner.py. This simply takes the list of files, reads them, and then outputs them into another file
import os

abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

files = ["smallfile1.js", "smallfile2.js", "smallfile3.js"]

outputFile = "";

for file in files:
    with open(file, 'r') as f:
        outputFile += " " + f.read()


with open("../bigfile.js", 'w+') as output:
    output.write(outputFile)

4. Configure the File Watcher in PyCharm so that it calls this script whenever I save one of the subfiles

  • File > Settings > File Watcher
  • Add new
  • Scope = point to the subdirectory containing the small JS files and click Include Recursively
  • Program = C:\Python27\python.exe
  • Arguments = <path to combiner.py>
5. Save it all

6. Try editing one of the small JS files and then saving it. combiner.py should run and you should now see the combined big JS file sitting in the correct spot.

Note: This does not minimize the JS file. Remember that optimizations are a waste of time until you can prove that you have a performance problem to solve. In my case, applying a minimizer to bigfile.js barely saves any space, so I'm not going to waste my time attempting to automate the minimization process UNTIL a performance problem arises where the time spent solving the problems has quantifiable benefits.
There was an error in this gadget