Friday, February 28, 2020

Python - List of unique dictionaries

The usual way to find just the common elements in a set is to use Python's set class. Just add all the elements to the set, then convert the set to a list, and the duplicates are gone.
The problem, of course, is that a set() can only contain hashable entries, and a dict is not hashable.
If I had this problem, my solution would be to convert each dict into a string that represents the dict, then add all the strings to a set() then read out the string values as a list() and convert back to dict.
A good representation of a dict in string form is JSON format. And Python has a built-in module for JSON (called json of course).
Or, to make it work with Python 3.x (and recent versions of numpy), you need to convert array of dicts to numpy array of strings, e.g.
import numpy as np
list(np.unique(np.array(final_list).astype(str)))

Thursday, February 13, 2020

SQL - Add constraint for an existing column


-----alter an existing column to have default value
  update  tbl_name
  set appended = getdate()

  alter table  tbl_name
  alter column appended DATETIME NOT NULL ;

  ALTER TABLE tbl_name
  ADD CONSTRAINT DF_Constraint DEFAULT GetDate() FOR appended;


-----get two digit month
 CONVERT(char(2), GetDate(), 101)

Monday, February 3, 2020

Python - get all the files in specified folders


This function below returns a list of file under current directory based on the file extension.
e.g. 
file_list = filebrowser(".zip")


def filebrowser(ext=""):
    "Returns files with an extension"    return [f for f in glob.glob(f"*{ext}")]


This function below return specified files in a list.

import os
from fnmatch import fnmatch

# 28 states

state_list = ['al', 'ar', 'ca', 'co', 'fl', 'id', 'ks', 'ky', 'la', 
'mi', 'mo', 'ms', 'mt', 'nd', 'ne', 'nm', 'nv', 'ny', 'oh', 'ok', 
'pa', 'sd', 'tn', 'tx', 'ut', 'va', 'wv', 'wy']
for state in state_list:
    root = os.path.join(os.path.dirname(os.path.abspath(__file__)), state)
    pattern = "*.py"    
    exclude_files = ['__init__', '_unit_test']
    for path, subdirs, files in os.walk(root):
        for name in files:
            if fnmatch(name, pattern) 
               and all(str(name).find(sub_name) == -1 for sub_name in exclude_files):
                print(os.path.join(path, name))