diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 0f3bb98..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.gitignore b/.gitignore deleted file mode 100644 index f7275bb..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -venv/ diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100755 index 26d3352..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index b1b7c45..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/fluent_python.iml b/.idea/fluent_python.iml deleted file mode 100755 index 5ce1bc4..0000000 --- a/.idea/fluent_python.iml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100755 index 105ce2d..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100755 index b0ee564..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100755 index 329a5f3..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100755 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/ads b/ads deleted file mode 100755 index fd40910..0000000 --- a/ads +++ /dev/null @@ -1,4 +0,0 @@ - - - - diff --git a/asdf b/asdf deleted file mode 100755 index 523b14b..0000000 --- a/asdf +++ /dev/null @@ -1 +0,0 @@ -a= '010000100110000101110010011100100110010101110100011101000010000001000110011011110110000101110100' \ No newline at end of file diff --git a/async_functions/interesting_async.py b/async_functions/interesting_async.py deleted file mode 100644 index 280ff7f..0000000 --- a/async_functions/interesting_async.py +++ /dev/null @@ -1,187 +0,0 @@ -import asyncio -import time - -# The event loop is the core of asyncio. It is responsible for: -# Managing and executing asynchronous functions (coroutines). -# Scheduling and switching between different tasks when they hit an await (yielding control). -# Handling I/O-bound operations efficiently without blocking execution. -# An async function can only run inside an event loop, so if event loop is not available, you can run async function. - -# asyncio.run(main()): -# start an event loop, run the async function, at last, close the event loop - -# loop = asyncio.get_event_loop() -# loop.run_until_complete(main()) -# loop.close() -# another way to run an async function, use the event loop directly. make sure to close it after running the function. - -# if the event loop already exist, such as fastapi, jupyter notebook, databricks notebook etc. -# you don't need to call asyncio.run(main()) -# you can run an async function directly using await: await async_func() - -# t1 = asyncio.create_task(task1()): -# t2 = asyncio.create_task(task2()): -# asyncio.create_task does not create event loop. -# It takes a coroutine and wraps it into an asyncio.Task. -# Imagine it like a list, you can add multiple coroutines to it, one by one -# The tasks are scheduled to run in the background inside the existing event loop. -# It does not block execution—other code continues running immediately. -# t1 is an instance of asyncio.Task, which is a subclass of asyncio.Future. it can be awaited. -# When event loop starts execute the tasks, the order matters: first come, first start, -# but you don't have to wait the first to finish to start the second one. -# the "await" switches it to start another one -# Use asyncio.create_task() when you want to schedule background tasks but don’t need to await them immediately. - -# results = await asyncio.gather(task1(), task2()) -# like create_task, it does not create event loop, but use event loop -# "gather" can add/schedule multiple tasks at once -# it returns results of the tasks in a list -# Use asyncio.gather() when you need to start multiple tasks at once and wait for all to complete. - - -# how does the event loop execute the async function/coroutine? -# every time you call "await something", the execution will pause and give control back to event loop -# event loop will then starts the available async functions/coroutines in the task list(), -# when meet next "await", it starts another async/coroutine, and so on and so forth - - -# -----case 1-------------------------------------------------------------------------------------------- -# in this block when you run asyncio.run(main()), the event loop will be created, -# when it execute to the first await: t1_result = await t1 -# it pause and yield back control to the event loop, -# the event loop then check the tasks in the list (we have two added by create_task()) -# it will start the one that added first, it is task1 -# inside task1, when the code execute to await asyncio.sleep(2), it pauses and yields back control to event loop again -# this time event loop will start the existing task 2. -# when it encounters await asyncio.sleep(1), it pauses and yields back control to event loop again -# but this time the event loop has no other tasks to starts -# the control will be return to whatever awaits that finishes first -# since it only sleep 1 second in task 2, when the sleep ends, the control gives back to task 2 -# (task 1 is still sleeping) -# task 2 get controls and proceed to next line of code, it will print "004 Task 2 asynchronous sleep finished" -# and collect the result. -# now task 2 is done and but task 1 is still sleeping. -# after 1 more second, the task 1 sleep ends, then proceed to print and return. -# task 1 is done, the control gives back to "t1_result = await t1" -# t1_result is assigned to the result of task 1 -# the code move on until it meets "t2_result = await t2" -# it will pause and yield back control to event loop, but now no other tasks to start -# and since t2 already finished, t2_result = await t2 will gain control back immediately -# the results of t2 will be assigned to t_2 result. -# the code moves on - -async def task1(): - print("002 Task 1 asynchronous sleep (2s) started") - await asyncio.sleep(2) # Task 1 "takes a break" - print("005 Task 1 asynchronous sleep (2s) finished") - return "Task 1 returned" - -async def task2(): - print("003 Task 2 asynchronous sleep started") - await asyncio.sleep(1) # Task 2 "takes a break" - print("004 Task 2 asynchronous sleep finished") - return "Task 2 returned" - -async def main(): - t1 = asyncio.create_task(task1()) # Task 1 is scheduled - t2 = asyncio.create_task(task2()) # Task 2 is scheduled - print("001 awaiting task 1 starts") - t1_result = await t1 # The event loop ensures t1 finishes - print(t1_result) - print("006 awaiting task 1 ends") - print("007 awaiting task 2 starts") - t2_result = await t2 # The event loop ensures t2 finishes - print(t2_result) - print("008 awaiting task 2 ends") - -asyncio.run(main()) - -# ---case 2------------------------------------------------------------------------------------------------------------- -# Here the order of crating tasks changed: task2 is added first -# remember for create_task, the order matters, the first added will be started first -# when the code runs to t1_result = await t1 -# the await will give control back to event loop, -# the event loop will start task2 first and then task1 in the background -# even if the code here is waiting for t1. - -async def main(): - t2 = asyncio.create_task(task2()) # Task 2 is scheduled - t1 = asyncio.create_task(task1()) # Task 1 is scheduled - print("001 awaiting task 1 starts") - t1_result = await t1 # The event loop ensures t1 finishes - print(t1_result) - print("006 awaiting task 1 ends") - print("007 awaiting task 2 starts") - t2_result = await t2 # The event loop ensures t2 finishes - print(t2_result) - print("008 awaiting task 2 ends") - -asyncio.run(main()) - -# -----case 3----------------------------------------------------------------------------------------------------------- -# here the after the asyncio.sleep(1), another 2 seconds of sync sleep is added. -# since this is the syn code, it does not yield back control -# during the syn sleep here, -# even if the task1 has finished sleep, it will not print -# task1 has to wai task2 to complete the sync code - -# so be careful when you write async function. If it has a sync func that takes longer (compute intensive) -# it will block other async tasks - -async def task2(): - print("003 Task 2 asynchronous sleep (1s) started") - await asyncio.sleep(1) # Task 2 "takes a break" - print("004 Task 2 asynchronous sleep (1s) finished") - print("004-1 Task 2 needs to sleep 1 more second (sync)") - time.sleep(1) - print("004-2 Task 2 wakes up from 1 second sleep (sync) ") - print("004-3 Task 1 asynchronous sleep (2s) is done") - print("004-4 However, Task 2 want to sleep another second") - print("004-5 Task 1 has to wait, sorry ...") - time.sleep(1) - print("004-6 Task 2 wakes up now") - print("004-7 Now let us go back to Task 1 and print it is finished ...") - return "Task 2 returned" - - -async def main(): - t1 = asyncio.create_task(task1()) # Task 1 is scheduled - t2 = asyncio.create_task(task2()) # Task 2 is scheduled - print("001 awaiting task 1 starts") - t1_result = await t1 # The event loop ensures t1 finishes - print(t1_result) - print("006 awaiting task 1 ends") - print("007 awaiting task 2 starts") - t2_result = await t2 # The event loop ensures t2 finishes - print(t2_result) - print("008 awaiting task 2 ends") - -asyncio.run(main()) - -# -----case 4----------------------------------------------------------------------------------------------------------- -# in this case when you run await task2(), it will start task2 immediately. -# when encounter the await asyncio.sleep(1), the it starts task1 -# Confusing? why in case 2, it is different? - -async def task1(): - print("002 Task 1 asynchronous sleep (2s) started") - await asyncio.sleep(2) # Task 1 "takes a break" - print("005 Task 1 asynchronous sleep (2s) finished") - return "Task 1 returned" - -async def task2(): - print("003 Task 2 asynchronous sleep started") - await asyncio.sleep(1) # Task 2 "takes a break" - print("004 Task 2 asynchronous sleep finished") - return "Task 2 returned" - -async def main(): - t1 = asyncio.create_task(task1()) # Task 1 is scheduled - print("001 start awaiting task 2") - await task2() - print("task2 is done") - await t1 - -asyncio.run(main()) - -# -----case 5---------------------------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/cafe.txt b/cafe.txt deleted file mode 100755 index 1c2e52c..0000000 --- a/cafe.txt +++ /dev/null @@ -1 +0,0 @@ -café \ No newline at end of file diff --git a/ch13_interface_protocol_ABC.py b/ch13_interface_protocol_ABC.py deleted file mode 100644 index 701fd3c..0000000 --- a/ch13_interface_protocol_ABC.py +++ /dev/null @@ -1,153 +0,0 @@ -class Vowel: - def __getitem__(self, i): - return "AEIOU"[i] - - -v = Vowel() -v[0] -for c in v: print(c) - - -from collections import namedtuple, abc - -Card = namedtuple('Card', ['rank', 'suit']) - -class FrenchDeck2(abc.MutableSequence): - ranks = [str(n) for n in range(2, 11)] + list('JQKA') - suits = 'spades diamonds clubs hearts'.split() - - def __init__(self): - self._cards = [Card(rank, suit) for suit in self.suits - for rank in self.ranks] - - def __len__(self): - return len(self._cards) - - def __getitem__(self, position): - return self._cards[position] - - def __setitem__(self, position, value): # <1> - self._cards[position] = value - - def __delitem__(self, position): # <2> - del self._cards[position] - - def insert(self, position, value): # <3> - self._cards.insert(position, value) - - -class FrenchDeck1(): - ranks = [str(n) for n in range(2, 11)] + list('JQKA') - suits = 'spades diamonds clubs hearts'.split() - - def __init__(self): - self._cards = [Card(rank, suit) for suit in self.suits - for rank in self.ranks] - - def __len__(self): - return len(self._cards) - - def __getitem__(self, position): - return self._cards[position] - -class FrenchDeck3(abc.MutableSequence): - ranks = [str(n) for n in range(2, 11)] + list('JQKA') - suits = 'spades diamonds clubs hearts'.split() - - def __init__(self): - self._cards = [Card(rank, suit) for suit in self.suits - for rank in self.ranks] - - def __len__(self): - return len(self._cards) - - def __getitem__(self, position): - return self._cards[position] - - def __setitem__(deck, position, value): # <1> - deck._cards[position] = value - - def __delitem__(self, position): # <2> - del self._cards[position] - - def insert(self, position, value): # <3> - self._cards.insert(position, value) - - -from random import shuffle -l = list(range(10)) -shuffle(l) -l - -import abc - -class Tombola(abc.ABC): # <1> - - @abc.abstractmethod - def load(self, iterable): # <2> - """Add items from an iterable.""" - - @abc.abstractmethod - def pick(self): # <3> - """Remove item at random, returning it. - - This method should raise `LookupError` when the instance is empty. - """ - - def loaded(self): # <4> - """Return `True` if there's at least 1 item, `False` otherwise.""" - return bool(self.inspect()) # <5> - - def inspect(self): - """Return a sorted tuple with the items currently inside.""" - items = [] - while True: # <6> - try: - items.append(self.pick()) - except LookupError: - break - self.load(items) # <7> - return tuple(items) - -import random -class BingoCage(Tombola): - def __init__(self, items): - self._randomizer = random.SystemRandom() - self._item = [] - self.load(items) - - def load(self, items): - self._item.extend(items) - self._randomizer.shuffle(self._item) - - def pick(self): - try: - return self._item.pop() - except IndexError: - raise LookupError('pick from empty BingoCage') - - def __call__(self): - self.pick() - - - -class LottoBlower(Tombola): - - def __init__(self, iterable): - self._balls = list(iterable) # <1> - - def load(self, iterable): - self._balls.extend(iterable) - - def pick(self): - try: - position = random.randrange(len(self._balls)) # <2> - except ValueError: - raise LookupError('pick from empty LottoBlower') - return self._balls.pop(position) # <3> - - def loaded(self): # <4> - return bool(self._balls) - - def inspect(self): # <5> - return tuple(self._balls) diff --git a/ch14_inheritance.py b/ch14_inheritance.py deleted file mode 100644 index 5d51b78..0000000 --- a/ch14_inheritance.py +++ /dev/null @@ -1,75 +0,0 @@ - -class DoppelDict(dict): - def __setitem__(self, key, value): - super().__setitem__(key, [value] *2) - -dd = DoppelDict(one=1) -dd -dd["two"] = 2 -dd.update(three=3) -dd - -""" -diamond1.py: Demo of diamond-shaped class graph. - -# tag::LEAF_MRO[] ->>> Leaf.__mro__ # doctest:+NORMALIZE_WHITESPACE - (, , , - , ) - -# end::LEAF_MRO[] - -# tag::DIAMOND_CALLS[] - >>> leaf1 = Leaf() # <1> - >>> leaf1.ping() # <2> - .ping() in Leaf - .ping() in A - .ping() in B - .ping() in Root - - >>> leaf1.pong() # <3> - .pong() in A - .pong() in B - -# end::DIAMOND_CALLS[] -""" - -# tag::DIAMOND_CLASSES[] -class Root: # <1> - def ping(self): - print(f'{self}.ping() in Root') - - def pong(self): - print(f'{self}.pong() in Root') - - def __repr__(self): - cls_name = type(self).__name__ - return f'' - - -class A(Root): # <2> - def ping(self): - print(f'{self}.ping() in A') - super().ping() - - def pong(self): - print(f'{self}.pong() in A') - # if comment out the next line, only pong in A will be implemented. - super().pong() - - -class B(Root): # <3> - def ping(self): - print(f'{self}.ping() in B') - super().ping() - - def pong(self): - print(f'{self}.pong() in B') - - -class Leaf(A, B): # <4> - def ping(self): - print(f'{self}.ping() in Leaf') - super().ping() -# end::DIAMOND_CLASSES[] - diff --git a/ch17_iterators.py b/ch17_iterators.py deleted file mode 100755 index db08c3d..0000000 --- a/ch17_iterators.py +++ /dev/null @@ -1,74 +0,0 @@ -b = "sentence" -for c in b: - print(c) - -b_it = iter(b) -while True: - try: - print(next(b_it)) - except StopIteration: - del b_it - break - - -import itertools -import functools -sample = [5,4,2,8,7,6,3,0,9,1] -sample1 = ["a", "b", "c"] -sample2 = [True, False] -# accumulate works on a iterable, returns a generator -accumulate_gen = itertools.accumulate(sample) -list(accumulate_gen) - -# reduce works on a iterable, returns an object -reduced_obj = functools.reduce(lambda x, y: x + y, sample) -print(reduced_obj) - -# map applies a func to each item of the iterable, returns a generator -mapped_obj = map(lambda x: 2*x, sample) -mapped_obj - -# filter function also create a generator -filtered_obj = filter(lambda x: x > 5, sample) -list(filtered_obj) - -# create a cartesian product of the input iterables. (combination for each item from each list) -product_obj = itertools.product(sample, sample1, sample2) -list(product_obj) - - -type(reduced_obj) -type(accumulate_gen) - - -from collections.abc import Generator -from typing import Union, NamedTuple - -class Result(NamedTuple): - count: int # type: ignore - average: float - -class Sentinel: - def __repr__(self): - return f'' - -STOP = Sentinel() - -SendType = Union[float, Sentinel] - -def averager2(verbose: bool=False) -> Generator[None, SendType, Result]: - total = 0.0 - count = 0 - average = 0.0 - while True: - term = yield - if verbose: - print("received", term) - if isinstance(term, Sentinel): - break - total += term - count += 1 - average = total / count - return Result(count, average) - -coro_average = averager2(True) diff --git a/ch17_sentence.py b/ch17_sentence.py deleted file mode 100755 index 2ce78b8..0000000 --- a/ch17_sentence.py +++ /dev/null @@ -1,33 +0,0 @@ -import re -import reprlib - -RE_WORD = re.compile(r'\w+') - - -class Sentence: - - def __init__(self, text): - self.text = text - self.words = RE_WORD.findall(self.text) - - def __getitem__(self, index): - return self.words[index] - - def __len__(self): - return len(self.words) - - def __repr__(self): - return 'Sentence(%s)' % reprlib.repr(self.text) - - -s = Sentence('"The time has come,", the walrus said') - -iter(s) -b = 123 -iter(b) - -try: - iter(b) -except TypeError: - print("the test object is not iterable") - diff --git a/ch1_data_model.py b/ch1_data_model.py old mode 100755 new mode 100644 diff --git a/ch2_sequence.py b/ch2_sequence.py old mode 100755 new mode 100644 index b99b7da..fe13990 --- a/ch2_sequence.py +++ b/ch2_sequence.py @@ -112,28 +112,13 @@ def main(): my_list = [[]] * 3 my_list -### This is the one will gave you the errors but still get through t = (1, 2, [30, 40]) -t[-1].append(99) +t[2] += [50, 60] t -t = (1, 2, [30, 40]) -t[-1] += [50, 60] -t -type(1) -int("1") - -dict_1 = {(1, 2, [3, 4]): "apple"} -dict_2 = {(1, 2, (3, 4)): "apple"} -dict_3 = {{1, 2, 3}: "apple"} -dict_4 = {frozenset({1, 2, 3}): "apple"} -dict_5 = {[1, 2, 3]: "apple"} -dict_6 = {True: "apple"} -dict_7 = {b"abc": "apple"} - -set_1 = {1, 2, (3, 4)} -set_2 = {1, 2, [3, 4]} -set_3 = {1, 2, {3, 4}} -set_4 = {1, 2, True, b"abc", frozenset({1, 2, 3})} + + + + diff --git a/ch3_dictionary.py b/ch3_dictionary.py old mode 100755 new mode 100644 index 20de933..143d3ec --- a/ch3_dictionary.py +++ b/ch3_dictionary.py @@ -1,68 +1,28 @@ -# dictionary comprehension + dial_codes = [ - (880, 'Bangladesh'), - (55, 'Brazil'), - (86, 'China'), - (91, 'India'), - (62, 'Indonesia'), - (81, 'Japan'), - (234, 'Nigeria'), - (92, 'Pakistan'), - (7, 'Russia'), - (1, 'United States'), + (880, 'Bangladesh'), + (55, 'Brazil'), + (86, 'China'), + (91, 'India'), + (62, 'Indonesia'), + (81, 'Japan'), + (234, 'Nigeria'), + (92, 'Pakistan'), + (7, 'Russia'), + (1, 'United States'), ] + country_dial = {country: dial for dial, country in dial_codes} print(country_dial) # Sorting country_dial by name, reversing the pairs again, uppercasing values, and filtering items with code < 70 -country_dial1 = {code: country.upper() for country, code in sorted(country_dial.items()) if code < 70} -print(country_dial1) - -d1 = {'a': 1, 'b': 3} -d2 = {'a': 2, 'b': 4, 'c': 6} -d1 | d2 - - - -# unpacking mapping -# ** can be used more than once in argument -def dump(**kwargs): - return kwargs -dump(**{"x": 1, "y": 2}, z=3, **{"a": 4, "b": 5}) - -def test_fun(a=1, b=2, c=3, d=4): - print(a, b, c, d) +{code: country.upper() for country, code in sorted(country_dial.items()) + if code < 70} -test_arg2 = {"a": 10, "b": 20} -test_arg2 = {"d": 40} -test_fun(**test_arg, **test_arg2) -# ** can be used more than once inside a dict liberal test_dict = {'a': 0, **{'x': 1}, 'y': 2, **{'z': 3, 'x': 4}} -fruits = {'apple': [1,2], 'banana': [3,4]} -# lookup key one -cherry = fruits.get('cherry', []) -cherry.append(5) -# lookup key two -fruits['cherry'] = cherry -fruits - -# if use setdfault, we only to lookup key once -fruits.setdefault('plum', []).append(6) -fruits -fruits.setdefault('banana', []).append(7) -fruits - -# Merging Mappings with | -d1 = {'a': 1, 'b': 3} -d2 = {'a': 2, 'b': 4, 'c': 6} -d1 | d2 -d1 |= d2 -d1 - - # dict.get(key, default) can be used to avoid KeyError test_dict.get("b", []) @@ -71,226 +31,3 @@ def test_fun(a=1, b=2, c=3, d=4): - -# dict views -a = {"a": 1, "b": 2} -a.items() -a.values() -a.keys() - -# values is a view of the dict value, a projection. It will be updated if the original dict change -values = a.values() -values -a["c"] = 3 -values - - -# set to remove duplicates -l = ['spam', 'spam', 'eggs', 'spam', 'bacon', 'eggs'] -set(l) - -# If you want to remove duplicates but also preserve the order of the first occurrence of each item, you can now use a plain dict to do it -list(dict.fromkeys(l).keys()) - -a = {i for i in range(10)} -b = {i for i in range(5, 15)} -# intersection -a & b -c = a & b -# union -a | b -# difference -a - b -# symmetric difference -a ^ b - -a-b -b-a - -c < b -c > b - - -# a dict_keys view can always be used as a set, because every key is hashable—by definition. -d1 = dict(a=1, b=2, c=3, d=4) -d2 = dict(b=20, d=40, e=50) -s = {"a", "b", "c"} -d1.keys() & d2.keys() -d1.keys() & s - - - ->>># example of defaultdict ->>>from collections import defaultdict ->>>fruits = defaultdict(list) ->>># updating the dict without checking the keys whether is exist or not ->>>fruits['apple'].append(1) ->>>fruits['banana'].append(7) ->>>fruits - - -class StrKeyDict(dict): - # StrKeyDict inherits from dict - def __missing__(self, key): - # check whether key is already a str, If it is, and it's missing, raise KeyError - if isinstance(key, str): - raise KeyError(key) - return self[str(key)] - - def get(self, key, default=None): - # The get method delegates to __getitem__ by using the self[key] notation; that - # gives the opportunity for our __missing__ to act. - try: - return self[key] - except KeyError: - return default - -# OrderedDict -from collections import OrderedDict -d1 = OrderedDict(a=1, b=2, c=3, d=4) -d1.popitem() -d1.popitem(last=False) - ->>> d1 = dict(a=1, b=3) ->>> d2 = dict(a=2, b=4, c=6) ->>> from collections import ChainMap ->>> chain = ChainMap(d1, d2) ->>> chain['a'] -1 ->>> chain['c'] -6 - ->>> chain['c'] = -1 ->>> d1 -{'a': 1, 'b': 3, 'c': -1} ->>> d2 -{'a': 2, 'b': 4, 'c': 6} - ->>> ct = collections.Counter('abracadabra') ->>> ct -Counter({'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1}) ->>> ct.update('aaaaazzz') ->>> ct -Counter({'a': 10, 'z': 3, 'b': 2, 'r': 2, 'c': 1, 'd': 1}) ->>> ct.most_common(3) -[('a', 10), ('z', 3), ('b', 2)] - - ->>> d = dict(a=10, b=20, c=30) ->>> values = d.values() ->>> values -dict_values([10, 20, 30]) ->>> len(values) -3 ->>> list(values) -[10, 20, 30] ->>> reversed(values) - ->>> values[0] -Traceback (most recent call last): - File "", line 1, in -TypeError: 'dict_values' object is not subscriptable - ->>> d['z'] = 99 ->>> d -{'a': 10, 'b': 20, 'c': 30, 'z': 99} ->>> values -dict_values([10, 20, 30, 99]) - -# define an attribute outside of __init__ -class MyClass: - def __init__(self): - self.a_val = 10 - def start(self, number): - self.b_val = 10 * number - def get_b(self): - return self.b_val - -# an improved way to define the attributes inside __init__ -class MyClass: - def __init__(self): - self.a_val = 10 - self.b_val = None - def start(self, number): - self.b_val = 10 * number - def get_b(self): - return self.b_val - ->>> from types import MappingProxyType ->>> d = {1: 'A'} ->>> d_proxy = MappingProxyType(d) ->>> d_proxy -mappingproxy({1: 'A'}) ->>> d_proxy[1] -'A' ->>> d_proxy[2] = 'x' -Traceback (most recent call last): - File "", line 1, in -TypeError: 'mappingproxy' object does not support item assignment - -l = ['spam', 'spam', 'eggs', 'spam', 'bacon', 'eggs'] -set(l) -list(set(l)) - -dict.fromkeys(l) -dict.fromkeys(l).keys() -list(dict.fromkeys(l).keys()) - -s1 = set([1,2,3,4,3,2]) -s2 = set([3,4,5,6,4]) -s1 -s2 -# s1 | s2 returns their union -s1 | s2 -# s1 & s2 returns their intersection -s1 & s2 -# s1 ^ s2 returns their symmetric difference -s1 ^ s2 -# s1 - s2 returns the difference -s1 - s2 -s2 - s1 - - -s1 = {1, 2, 3} -s1 = {} # empty dict -s1 = set() #empty set -frozen_s1 = frozenset(range(10)) # frozenset has no special syntax to represent - - ->>>{chr(i) for i in range(32, 40)} -{'&', '"', "'", '$', '#', '!', '%', ' '} - - -s3 = {3,4,5,6,7} ->>> d1 = dict(a=1, b=2, c=3, d=4) ->>> d2 = dict(b=20, d=40, e=50) ->>> d1.keys() & d2.keys() -{'b', 'd'} - ->>> s = {'a', 'e', 'i'} ->>> d1.keys() & s -{'a'} ->>> d1.keys() | s -{'a', 'c', 'b', 'd', 'i', 'e'} - - -{1,2} < s1 -{1,2,3,4} <= s1 -s1 < s2 ->>>{1,2} < s1 -True ->>>{1,2,3,4} <= s1 -True ->>>s1 < s2 -False ->>>{1,2,3,4,5} > s1 -True - - - ->>>found = len(set(needles) & set(haystack)) - -# another way: haystack can be any iterable types ->>>found = len(set(needles).intersection(haystack)) - -l1 = ["b", "a", "c", "a", "b", "c"] \ No newline at end of file diff --git a/ch4_unicode.py b/ch4_unicode.py deleted file mode 100755 index 58bc51a..0000000 --- a/ch4_unicode.py +++ /dev/null @@ -1,86 +0,0 @@ -cafe = bytes('café', encoding='utf_8') -cafe -cafe[0] -cafe[1] -cafe[2] -cafe[3] -cafe[4] - -tab_return_newline = 'abc\nabc\rab\tc\\' - -tab_return_newline - -tab='tab' -cafe[-1:] -bytes('c', encoding='utf')[0] - -bytes("ABC", encoding='utf8') -a = bytes.fromhex('41') -a.decode(encoding='utf8') - -bytes.fromhex('F0 9D 84 9E') -a -fp = open("cafe.txt", "w", encoding="utf-8") -fp -fp.write("café") -fp.close() -import os -os.stat('cafe.txt').st_size -fp2 = open('cafe.txt') -fp2 -fp2.encoding -fp2.read() - - -import locale -import sys - -expressions = """ - locale.getpreferredencoding() - type(my_file) - my_file.encoding - sys.stdout.isatty() - sys.stdout.encoding - sys.stdin.isatty() - sys.stdin.encoding - sys.stderr.isatty() - sys.stderr.encoding - sys.getdefaultencoding() - sys.getfilesystemencoding() - """ -my_file = open("dummy", "w") - -for expression in expressions.split(): - value = eval(expression) - print(f"{expression:>30} -> {value!r}") - - -from unicodedata import normalize, name -name("A") -name("é") -name("!") -name("#") -name("@") -name("^") -name("&") -name("~") -name("`") - -single_map = str.maketrans("""‚ƒ„ˆ‹‘’“”•–—˜›""", - """'f"^<''""---~>""") -single_map -multi_map = str.maketrans({ - '€': 'EUR', - '…': '...', - 'Æ': 'AE', - 'æ': 'ae', - 'Œ': 'OE', - 'œ': 'oe', - '™': '(TM)', - '‰': '', - '†': '**', - '‡': '***', -}) -multi_map.update(single_map) -multi_map - diff --git a/ch5_data_class_builder.py b/ch5_data_class_builder.py deleted file mode 100755 index 0d95dff..0000000 --- a/ch5_data_class_builder.py +++ /dev/null @@ -1,118 +0,0 @@ -import dataclasses -from inspect import get_annotations -from typing import get_type_hints -from collections import namedtuple -from typing import NamedTuple -import json -from dataclasses import dataclass, asdict, field - - -City = namedtuple('City', 'name country population coordinate') -tokyo = City('tokyo', 'JP', 36.12, (35, 139)) -tokyo -tokyo.population -tokyo.name -tokyo[2] - -tokyo._asdict() -tokyo._fields - -Coordinate = namedtuple('Coordinate', 'lat lon') -delhi_data = ('Delhi', 'IN', 21.9, Coordinate(28.6, 77.2)) -delhi = City._make(delhi_data) -delhi -delhi._asdict() - - -json.dumps(delhi._asdict()) - - -# Typed Named Tuple -class Coordinate(NamedTuple): - lat: float # every field must be annotated - lon: float - references: str = 'WGS84' - -# a plain class -# a is not an attribute, b and c are -class DemoPlainClass: - a: int - b: float=1.1 - c = 'spam' -pn = DemoPlainClass() -pn.a -pn.b -pn.c -pn.b = 2.3 -pn.c = "aaa" - - -# a and b are instance attributes, -# c is an old class attribute, -# a and b are read only after assignment -class DemoNTClass(NamedTuple): - a: int - b: float=1.1 - c = 'spam' - -DemoNTClass.__annotations__ -DemoNTClass.a -DemoNTClass.b -DemoNTClass.c - -nt = DemoNTClass(a=1, b=2.3) -nt.a -nt.b -nt.c - -nt.a = 2 - -nt._asdict() - -## dataclass -@dataclass -class DemoDataClass: - a: int - b: float = 1.1 - c = 'spam' - -DemoDataClass.__annotations__ -DemoDataClass.__doc__ -DemoDataClass.a # the attribute will only exist in an instance, not in a class -DemoDataClass.b -DemoDataClass.c - -dc = DemoDataClass(9) -dc.a -dc.b -dc.c - -dc.a = 8 -dc.b = 2.3 -dc.c = 'aa' -dc.z = "whatever" -dc.z - -# only a and b are class attributes and bound to the instance, -# c and z are not -asdict(dc) - -# Mutable default is not allowed -@dataclass() -class ClubMember: - name: str - guests: list = [] - -# Use default_factory instead -@dataclass() -class MemberClub: - name: str = "Mike" - book: int = 20 - guests: list[str] = field(default_factory=list) - -MemberClub.__annotations__ -MemberClub.__doc__ -mc = MemberClub("Sam", 1, ["a", "b"]) -mc.__annotations__ -asdict(mc) -mc \ No newline at end of file diff --git a/ch6_object_mutability.py b/ch6_object_mutability.py deleted file mode 100755 index f64c610..0000000 --- a/ch6_object_mutability.py +++ /dev/null @@ -1,91 +0,0 @@ -l1 = [3, [66,55,44], (7,8,9)] -# Create a shallow copy. Also l2 = l1[:] -l2 = list(l1) -# has no effect on l2 -l1.append(100) -# this affect l2 -l1[1].remove(55) -print("l2", l2) -print("l1", l1) -# This affect l1 -l2[1] += [33, 22] -# += operator create a new tuple, so it won't affect l1 -l2[2] += (10, 11) -print("l1", l1) -print("l2", l2) - - -# ######################################################################## -# A bad idea to use mutable parameter as default argument: -# ######################################################################## -class HauntedBus: - # list as default parameter here - def __init__(self, passengers=[]): - self.passengers = passengers - - def pick(self, name): - self.passengers.append(name) - - def drop(self, name): - self.passengers.remove(name) - -# Create bus1 -bus1 = HauntedBus(["Jack", "Alice"]) -bus1.pick("Mike") -bus1.drop("Jack") -# Everything is right -bus1.passengers - -# Create bus2 with default empty -bus2 = HauntedBus() -bus2.pick("Jack") -# everything looks ok so far -bus2.passengers - -# Now create bus3 with default empty -bus3 = HauntedBus() -# It should be empty, but now it has values. The reason is that the self.passenger becomes a reference of the default -# list which was empty initially. Now self.default is a attribute of bus2 and it points to the same object(list) as the -# default passenger parameter does. Since bus2 picked a name, the default list will be changed, too. -# Now if we create a new instance with default parameter, it will use the same object(list) which is already be filled. -bus3.passengers - - -# ######################################################################## -# Defensive programming with mutable parameter as default argument: better to make a copy of it -# ######################################################################## -class TwilightBus: - # The passergers parameter will be a mutable(list), so set None as default. This is right! - def __init__(self, passengers=None): - if passengers is None: - self.passengers = [] - # since we know passengers will be a list, here we should create a copy of it - # otherwise, self.passengers become an alias of the passed variable - # if we modify self.passengers, the original passed variable(same object) will be modified too - else: - self.passengers = passengers - - def pick(self, name): - self.passengers.append(name) - - def drop(self, name): - self.passengers.remove(name) - -basketball_team = ["Sue", "Tina", "Maya", "Dianna", "Pat"] -bus = TwilightBus(basketball_team) -bus.drop("Sue") -bus.drop("Tina") -# looks OK for the instance's attribute -bus.passengers -# But the original list is also changed -basketball_team - -# del delete references but not object -a = [1,2] -b = a -# This only delete the referenc a(label), not the object [1,2] -del a -b -# Now rebinding b to a different object, object [1,2] has no reference(label, variable name) pointing to it, -# therefore, the garbage collector will discard it -b = [3] \ No newline at end of file diff --git a/ch8_type_hint/double.py b/ch8_type_hint/double.py deleted file mode 100644 index 7b00836..0000000 --- a/ch8_type_hint/double.py +++ /dev/null @@ -1,18 +0,0 @@ -from typing import Any, Optional - - -def double(x: Any) -> Any: - return x * 2 - - -def double_object(x: object) -> object: - return x * 2 - - -def double_float(x: Optional[float] = None) -> float: - if isinstance(x, float): - return x * 2 - else: - return 1.0 - - diff --git a/code_for_bh_flags.sql b/code_for_bh_flags.sql deleted file mode 100644 index 72b0d99..0000000 --- a/code_for_bh_flags.sql +++ /dev/null @@ -1,1015 +0,0 @@ ---drop table #WANG_MEMS; - -CREATE VOLATILE TABLE #WANG_MEMS AS ( - SELECT - ab.admission_date (DATE) AS admission_date, - ab.member_dim_ck, - ab.external_member_id, - ab.plan_dim_ck, - ab.member_curr_ck, - ab.authorization_ck, - ab.event_reason_dim_ck, - ab.um_service_type_dim_ck, - ab.admission_source_dim_ck, - ab.attend_prov_aff_dim_ck, - ab.diagnosis_type, - p.plan_type, - m.member_amisys_nbr, - m.sex, - m.edw_member_ck, - m.mpi_id, - e.pro_generic_product_desc, - d.diag_code AS diag_code, - d.diag_desc AS diag_desc - - FROM etl_access_own.ft_authorization AS ab - - JOIN etl_access_own.dim_plan AS p - ON ab.plan_dim_ck = p.plan_dim_ck - - JOIN etl_access_own.dim_member_curr AS m - ON ab.member_dim_ck = m.member_dim_ck - - LEFT JOIN etl_access_own.dim_member_elig_curr AS e - ON ab.member_elig_curr_ck = e.member_eligibility_ck - - LEFT JOIN etl_access_own.dim_diagnosis AS d - ON ab.rpt_primary_diag_dim_ck = d.diag_code_dim_ck - - WHERE admission_date BETWEEN '2021-01-01 00:00:00' AND '2023-04-01 00:00:00' - AND external_member_id IS NOT NULL -- cannot join if either admission_date or member_id is null†† - AND admission_date IS NOT NULL -- since a visit is defined as a unique admit_date and member_id - AND (p.state_code in ('EF','FL','EK','TX','WA','EW','OR','MO','NH','NV','NM','CA','WI', - 'MS','EM','EE','EA','KA','EV','XW','ET','IN','LA','GA','ER','AR', 'IL',' AZ', 'OH', 'NE') - OR p.plan_dim_ck IN (41,62,68,82,83,108,127,109,130,132,213,44,69,45,51,126,107,105,110,106, - 104,203,202,201,204,205,76,102,1,101,103,73,21,2,26,125,12,15,20,61,32,115,49,59,23,14,50,55, - 117,146,124,128,54,118,22,10,27,60,37,70,122,112,228,223,114,123,138,140,136,5,121,16,57,143, - 144,145,209,210,211,212,226,39,119,75,40,4,137,74,46)) - AND left(auth_nbr, 2) NOT IN ('OP', 'OB') -) -WITH DATA -PRIMARY INDEX(authorization_ck) -ON COMMIT PRESERVE ROWS; - - -/******************************************************** -Step 1. Creating volatile tables to hold all diagnosis - related to the diagnosis flags business partners - want to look at/flag for the member -********************************************************/ - -/******************* -PTSD Diagnosis -*******************/ -CREATE VOLATILE TABLE #PTSD_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%PTSD%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -Trauma Diagnosis -*******************/ -CREATE VOLATILE TABLE #TRAUMA_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%TRAUMA%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -Self Harm Diagnosis -*******************/ -CREATE VOLATILE TABLE #SELF_HARM_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%SELF%HARM%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -Schizo Diagnosis -*******************/ -CREATE VOLATILE TABLE #SCHIZO_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%SCHIZO%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -OCD Diagnosis -*******************/ -CREATE VOLATILE TABLE #OCD_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%OBSESSIVE%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - -/******************* -COVID-19 Diagnosis -*******************/ -CREATE VOLATILE TABLE #COVID19_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%COVID-19%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -Depression Diagnosis -*******************/ -CREATE VOLATILE TABLE #DEPRESSION_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%DEPRESSION%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -Anxiety Diagnosis -*******************/ -CREATE VOLATILE TABLE #ANXIETY_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%ANXIETY%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -Dementia Diagnosis -*******************/ -CREATE VOLATILE TABLE #DEMENTIA_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%DEMENTIA%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -Parkinsons Diagnosis -*******************/ -CREATE VOLATILE TABLE #PARKINSONS_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%PARKINSONS%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -Bipolar Diagnosis -*******************/ -CREATE VOLATILE TABLE #BIPOLAR_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%BIPOLAR%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -Ideation Diagnosis -*******************/ -CREATE VOLATILE TABLE #SUICIDEATION_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%SUICIDAL IDEATION%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - -/******************* -Previous Suicide Diagnosis -*******************/ -CREATE VOLATILE TABLE #SUIC_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%SUICIDE%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - -/******************* -Previous Personality Disorder Diagnosis -*******************/ -CREATE VOLATILE TABLE #PD_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%PERSONALITY DISORDER%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - -/******************* -Borderline Personality Disorder Diagnosis -*******************/ -CREATE VOLATILE TABLE #BPD_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE '%BORDERLINE PERSONALITY DISORDER%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - -/******************* -Chronic Pain Diagnosis -*******************/ -CREATE VOLATILE TABLE #CHRONIC_PAIN_DIAGS AS -( -SELECT -DIAG_CODE -,DIAG_DESC - - FROM ETL_ACCESS_OWN.DIM_DIAGNOSIS - -WHERE DIAG_DESC LIKE 'CHRONIC%PAIN%' - ---Grab only one description per diagnosis code -QUALIFY ROW_NUMBER() OVER(PARTITION BY DIAG_CODE ORDER BY DIAG_DESC ASC) = 1 -) -WITH DATA -PRIMARY INDEX(DIAG_CODE) -ON COMMIT PRESERVE ROWS; - - -/******************* -BH Diagnosis flags -*******************/ -CREATE VOLATILE TABLE #BH_CLAIMS AS -( -SELECT DISTINCT -SERV.PLAN_DIM_CK -,SERV.CLAIM_NBR -/*Logic from Population health team to defint BH Claims*/ -,(CASE WHEN UPPER(DIAG_GROUP.MPC_DESC) LIKE '%PSYCHIATRY%' THEN 1 - WHEN UPPER(DIAG_GROUP.MPC_DESC) LIKE '%CHEMICAL_DEPENDENCY%' THEN 1 - ELSE 0 - END - ) BH_FLAG - - /*Service line table - multiple lines per claim*/ - FROM ETL_ACCESS_OWN.FT_SERVICE_TRANSACTION SERV - - - INNER JOIN ETL_ACCESS_OWN.DIM_MEMBER_CURR MEM - ON SERV.MEMBER_CURR_CK = MEM.MEMBER_CURR_CK - AND SERV.PLAN_DIM_CK = MEM.PLAN_DIM_CK - - /*Limit to our membership*/ - INNER JOIN (SELECT DISTINCT - MPI_ID - - FROM #WANG_MEMS - ) T05 - ON MEM.MPI_ID = T05.MPI_ID - - /*Get diagnosis group to determine BH*/ - LEFT JOIN ETL_ACCESS_OWN.DIM_DIAGNOSIS_GROUPER DIAG_GROUP - ON SERV.PRIMARY_DIAG_GROUPER_DIM_CK = DIAG_GROUP.DIAG_GROUPER_DIM_CK -) -WITH DATA -PRIMARY INDEX(PLAN_DIM_CK, CLAIM_NBR) -ON COMMIT PRESERVE ROWS; - - -/******************************************************** -Step 2. Making a table with the claims with these - specific diagnosis for the member's we're interested - in and when these claims actually happened -********************************************************/ - -DROP TABLE MANDA_OWN_TABLES.PYR8S_BH_DATA_T01; - -CREATE TABLE MANDA_OWN_TABLES.PYR8S_BH_DATA_T01 AS -( -SELECT -SERV.PLAN_DIM_CK -,SERV.MPI_ID -,SERV.SERV_DATE -,DIAG.CLAIM_NBR -,DIAG.DIAG_CODE -,(CASE WHEN DIAG.DIAG_CODE = PTSD.DIAG_CODE THEN 1 ELSE 0 END) PTSD -,(CASE WHEN DIAG.DIAG_CODE = TRAUMA.DIAG_CODE THEN 1 ELSE 0 END) TRAUMA -,(CASE WHEN DIAG.DIAG_CODE = SELF_HARM.DIAG_CODE THEN 1 ELSE 0 END) SELF_HARM -,(CASE WHEN DIAG.DIAG_CODE = SCHIZO.DIAG_CODE THEN 1 ELSE 0 END) SCHIZO -,(CASE WHEN DIAG.DIAG_CODE = OCD.DIAG_CODE THEN 1 ELSE 0 END) OCD -,(CASE WHEN DIAG.DIAG_CODE = COVID19.DIAG_CODE THEN 1 ELSE 0 END) COVID19 -,(CASE WHEN DIAG.DIAG_CODE = DEPRESSION.DIAG_CODE THEN 1 ELSE 0 END) DEPRESSION -,(CASE WHEN DIAG.DIAG_CODE = ANXIETY.DIAG_CODE THEN 1 ELSE 0 END) ANXIETY -,(CASE WHEN DIAG.DIAG_CODE = DEMENTIA.DIAG_CODE THEN 1 ELSE 0 END) DEMENTIA -,(CASE WHEN DIAG.DIAG_CODE = PARKINSONS.DIAG_CODE THEN 1 ELSE 0 END) PARKINSONS -,(CASE WHEN DIAG.DIAG_CODE = BIPOLAR.DIAG_CODE THEN 1 ELSE 0 END) BIPOLAR -,(CASE WHEN DIAG.DIAG_CODE = SUICIDTN.DIAG_CODE THEN 1 ELSE 0 END) SUICIDTN -,(CASE WHEN DIAG.DIAG_CODE = SUIC.DIAG_CODE THEN 1 ELSE 0 END) SUICIDE -,(CASE WHEN DIAG.DIAG_CODE = PD.DIAG_CODE THEN 1 ELSE 0 END) PERS_DIS -,(CASE WHEN DIAG.DIAG_CODE = BPD.DIAG_CODE THEN 1 ELSE 0 END) BPD_DIS -,(CASE WHEN DIAG.DIAG_CODE = CP.DIAG_CODE THEN 1 ELSE 0 END) CHRON_PAIN -,BH.BH_FLAG BEHAVIORAL_HEALTH - - FROM ETL_ACCESS_OWN.BRG_CLAIM_DIAGNOSIS DIAG - - /*Grab date of claim and only pull claims for our members*/ - INNER JOIN (SELECT - SERV.PLAN_DIM_CK - ,T05.MPI_ID - ,SERV.CLAIM_NBR - ,MIN(SERV.SERVICE_START_DATE_DIM_CK) SERV_DATE - - FROM ETL_ACCESS_OWN.FT_SERVICE_TRANSACTION SERV - - INNER JOIN ETL_ACCESS_OWN.DIM_MEMBER_CURR MEM - ON SERV.MEMBER_CURR_CK = MEM.MEMBER_CURR_CK - AND SERV.PLAN_DIM_CK = MEM.PLAN_DIM_CK - - /*Limit to our membership*/ - INNER JOIN (SELECT DISTINCT - MPI_ID - - FROM #WANG_MEMS - ) T05 - ON MEM.MPI_ID = T05.MPI_ID - - GROUP BY 1,2,3 - ) SERV - ON DIAG.CLAIM_NBR = SERV.CLAIM_NBR - - - /*Left join to each out our diagnosis tables to make flags*/ - LEFT JOIN #PTSD_DIAGS PTSD - ON DIAG.DIAG_CODE = PTSD.DIAG_CODE - - LEFT JOIN #TRAUMA_DIAGS TRAUMA - ON DIAG.DIAG_CODE = TRAUMA.DIAG_CODE - - LEFT JOIN #SELF_HARM_DIAGS SELF_HARM - ON DIAG.DIAG_CODE = SELF_HARM.DIAG_CODE - - LEFT JOIN #SCHIZO_DIAGS SCHIZO - ON DIAG.DIAG_CODE = SCHIZO.DIAG_CODE - - LEFT JOIN #OCD_DIAGS OCD - ON DIAG.DIAG_CODE = OCD.DIAG_CODE - - LEFT JOIN #COVID19_DIAGS COVID19 - ON DIAG.DIAG_CODE = COVID19.DIAG_CODE - - LEFT JOIN #DEPRESSION_DIAGS DEPRESSION - ON DIAG.DIAG_CODE = DEPRESSION.DIAG_CODE - - LEFT JOIN #ANXIETY_DIAGS ANXIETY - ON DIAG.DIAG_CODE = ANXIETY.DIAG_CODE - - LEFT JOIN #DEMENTIA_DIAGS DEMENTIA - ON DIAG.DIAG_CODE = DEMENTIA.DIAG_CODE - - LEFT JOIN #PARKINSONS_DIAGS PARKINSONS - ON DIAG.DIAG_CODE = PARKINSONS.DIAG_CODE - - LEFT JOIN #BIPOLAR_DIAGS BIPOLAR - ON DIAG.DIAG_CODE = BIPOLAR.DIAG_CODE - - LEFT JOIN #SUICIDEATION_DIAGS SUICIDTN - ON DIAG.DIAG_CODE = SUICIDTN.DIAG_CODE - - LEFT JOIN #SUIC_DIAGS SUIC - ON DIAG.DIAG_CODE = SUIC.DIAG_CODE - - LEFT JOIN #PD_DIAGS PD - ON DIAG.DIAG_CODE = PD.DIAG_CODE - - LEFT JOIN #BPD_DIAGS BPD - ON DIAG.DIAG_CODE = BPD.DIAG_CODE - - LEFT JOIN #CHRONIC_PAIN_DIAGS CP - ON DIAG.DIAG_CODE = CP.DIAG_CODE - - LEFT JOIN #BH_CLAIMS BH - ON DIAG.CLAIM_NBR = BH.CLAIM_NBR - AND DIAG.PLAN_DIM_CK = BH.PLAN_DIM_CK - -) -WITH DATA -PRIMARY INDEX(PLAN_DIM_CK, MPI_ID, CLAIM_NBR, SERV_DATE); - - -/******************************************************** -Step 3. Looking at specific opioids the member has - had within the past 30 days (prior to admission) -********************************************************/ - -/****************************************** -Buprenorphine Prescription in last 30 days -******************************************/ - -CREATE VOLATILE TABLE #BUPRENORPHINE AS ( -SELECT -T05.PLAN_DIM_CK -,T05.MPI_ID -,T05.admission_date -,Sum(CASE WHEN PHARMACY.AMT_PAID > 0 THEN 1 - ELSE 0 - END) AS BUPREN_CLAIMS - - - FROM ETL_ACCESS_OWN.FT_PHARMACY_CLAIM PHARMACY - - INNER JOIN ETL_ACCESS_OWN.DIM_DRUG DRUG - ON PHARMACY.NATIONAL_DRUG_CODE_NBR = DRUG.NATIONAL_DRUG_CODE_NBR - - /*Convert fill date to actual date*/ - INNER JOIN ETL_ACCESS_OWN.DIM_DATE DD - ON PHARMACY.FILL_DATE_DIM_CK = DD.DATE_DIM_CK - - INNER JOIN ETL_ACCESS_OWN.DIM_MEMBER_CURR MEM - ON PHARMACY.PLAN_DIM_CK = MEM.PLAN_DIM_CK - AND PHARMACY.PRESCRIBER_MEMBER_CURR_CK = MEM.MEMBER_CURR_CK - - /*join so we can make sure serv date is < admission date*/ - INNER JOIN #WANG_MEMS T05 - ON MEM.MPI_ID = T05.MPI_ID - -/*Pull only number of claims for 30 days prior to admission date*/ -WHERE 1=1 -AND DD.DATE_DATE BETWEEN (T05.admission_date - 30) AND T05.admission_date -AND UPPER(DRUG.GPI_10_SEGMENT_DESC) LIKE '%BUPRENORPHINE%' - -GROUP BY 1,2,3 -) -WITH DATA -PRIMARY INDEX(PLAN_DIM_CK, MPI_ID, admission_date) -ON COMMIT PRESERVE ROWS; - - -/**************************************** -Naloxone Prescription in last 30 days -*****************************************/ ---DROP TABLE #NALOXONE; -CREATE VOLATILE TABLE #NALOXONE AS ( -SELECT -PHARMACY.PLAN_DIM_CK -,T05.MPI_ID -,T05.admission_date -,Sum(CASE WHEN PHARMACY.AMT_PAID > 0 THEN 1 - ELSE 0 - END) AS NALOXONE_CLAIMS - - - FROM ETL_ACCESS_OWN.FT_PHARMACY_CLAIM PHARMACY - - INNER JOIN ETL_ACCESS_OWN.DIM_DRUG DRUG - ON PHARMACY.NATIONAL_DRUG_CODE_NBR = DRUG.NATIONAL_DRUG_CODE_NBR - - /*Convert fill date to actual date*/ - INNER JOIN ETL_ACCESS_OWN.DIM_DATE DD - ON PHARMACY.FILL_DATE_DIM_CK = DD.DATE_DIM_CK - - INNER JOIN ETL_ACCESS_OWN.DIM_MEMBER_CURR MEM - ON PHARMACY.PLAN_DIM_CK = MEM.PLAN_DIM_CK - AND PHARMACY.PRESCRIBER_MEMBER_CURR_CK = MEM.MEMBER_CURR_CK - - /*join so we can make sure serv date is < model run date*/ - INNER JOIN #WANG_MEMS T05 - ON MEM.MPI_ID = T05.MPI_ID - -/*Pull only number of claims for 30 days prior to model run date*/ -WHERE 1=1 -AND DD.DATE_DATE BETWEEN (T05.admission_date - 30) AND T05.admission_date -AND UPPER(DRUG.GPI_10_SEGMENT_DESC) LIKE '%NALOXONE%' - -GROUP BY 1,2,3 -) -WITH DATA -PRIMARY INDEX(PLAN_DIM_CK, MPI_ID, admission_date) -ON COMMIT PRESERVE ROWS; - -/*************************************************** -Other type of Opioid Prescription in last 30 days -***************************************************/ ---DROP TABLE #OPIOIDS; -CREATE VOLATILE TABLE #OPIOIDS AS ( -SELECT -PHARMACY.PLAN_DIM_CK -,T05.MPI_ID -,T05.admission_date -,Sum(CASE WHEN PHARMACY.AMT_PAID > 0 THEN 1 - ELSE 0 - END) AS OPIOID_CLAIMS - - - FROM ETL_ACCESS_OWN.FT_PHARMACY_CLAIM PHARMACY - - INNER JOIN ETL_ACCESS_OWN.DIM_DRUG DRUG - ON PHARMACY.NATIONAL_DRUG_CODE_NBR = DRUG.NATIONAL_DRUG_CODE_NBR - - /*Convert fill date to actual date*/ - INNER JOIN ETL_ACCESS_OWN.DIM_DATE DD - ON PHARMACY.FILL_DATE_DIM_CK = DD.DATE_DIM_CK - - INNER JOIN ETL_ACCESS_OWN.DIM_MEMBER_CURR MEM - ON PHARMACY.PLAN_DIM_CK = MEM.PLAN_DIM_CK - AND PHARMACY.PRESCRIBER_MEMBER_CURR_CK = MEM.MEMBER_CURR_CK - - /*join so we can make sure serv date is < model run date*/ - INNER JOIN #WANG_MEMS T05 - ON MEM.MPI_ID = T05.MPI_ID - -/*Pull only number of claims for 30 days prior to model run date*/ -WHERE 1=1 -AND DD.DATE_DATE BETWEEN (T05.admission_date - 30) AND T05.admission_date -AND (UPPER(DRUG.GPI_4_SEGMENT_DESC) LIKE '%OPIOID%' - AND UPPER(DRUG.GPI_10_SEGMENT_DESC) NOT LIKE ('%BUPRENORPHINE%') - AND UPPER(DRUG.GPI_10_SEGMENT_DESC) NOT LIKE ('%NALOXONE%') - ) - -GROUP BY 1,2,3 -) -WITH DATA -PRIMARY INDEX(PLAN_DIM_CK, MPI_ID, admission_date) -ON COMMIT PRESERVE ROWS; - -/******************************************************** -Step 4. Join this back to our original table, making sure - to only count the diagnosis prior to our model - run date -********************************************************/ - -DROP TABLE MANDA_OWN_TABLES.PYR8S_BH_DATA_T02; - -CREATE TABLE MANDA_OWN_TABLES.PYR8S_BH_DATA_T02 AS -( - SELECT - DISTINCT - T05.admission_date - ,T05.member_dim_ck - ,T05.external_member_id - ,T05.plan_dim_ck - ,T05.member_curr_ck - ,T05.authorization_ck - ,T05.event_reason_dim_ck - ,T05.um_service_type_dim_ck - ,T05.admission_source_dim_ck - ,T05.attend_prov_aff_dim_ck - ,T05.diagnosis_type - ,T05.plan_type - ,T05.member_amisys_nbr - ,T05.sex - ,T05.edw_member_ck - ,T05.mpi_id - ,T05.pro_generic_product_desc - ,T05.diag_code - ,T05.diag_desc - ,T06.PTSD_COUNT - ,T06.TRAUMA_COUNT - ,T06.SELF_HARM_COUNT - ,T06.SCHIZO_COUNT - ,T06.OCD_COUNT - ,T06.COVID19_COUNT - ,T06.DEPRESSION_COUNT - ,T06.ANXIETY_COUNT - ,T06.DEMENTIA_COUNT - ,T06.PARKINSONS_COUNT - ,T06.BIPOLAR_COUNT - ,T06.SUICIDTN_COUNT - ,T06.SUICIDE_COUNT - ,T06.PERS_DIS_COUNT - ,T06.BPD_COUNT - ,T06.CHRON_PAIN_COUNT - ,T06.BH_COUNT - ,BUPREN.BUPREN_CLAIMS BUPREN_COUNT - ,NALOX.NALOXONE_CLAIMS NALOXONE_COUNT - ,OPIOID.OPIOID_CLAIMS OPIOID_COUNT - - FROM #WANG_MEMS T05 - - - /*Aggregrate number of diags by member based on model run date*/ - LEFT JOIN (SELECT - T06.PLAN_DIM_CK - ,T06.MPI_ID - ,T05.admission_date - ,SUM(T06.PTSD) PTSD_COUNT - ,SUM(T06.TRAUMA) TRAUMA_COUNT - ,SUM(T06.SELF_HARM) SELF_HARM_COUNT - ,SUM(T06.SCHIZO) SCHIZO_COUNT - ,SUM(T06.OCD) OCD_COUNT - ,SUM(T06.COVID19) COVID19_COUNT - ,SUM(T06.DEPRESSION) DEPRESSION_COUNT - ,SUM(T06.ANXIETY) ANXIETY_COUNT - ,SUM(T06.DEMENTIA) DEMENTIA_COUNT - ,SUM(T06.PARKINSONS) PARKINSONS_COUNT - ,SUM(T06.BIPOLAR) BIPOLAR_COUNT - ,SUM(T06.SUICIDTN) SUICIDTN_COUNT - ,SUM(T06.SUICIDE) SUICIDE_COUNT - ,SUM(T06.PERS_DIS)PERS_DIS_COUNT - ,SUM(T06.BPD_DIS) BPD_COUNT - ,SUM(T06.CHRON_PAIN) CHRON_PAIN_COUNT - ,SUM(T06.BEHAVIORAL_HEALTH) BH_COUNT - - FROM MANDA_OWN_TABLES.PYR8S_BH_DATA_T01 T06 - - /*Convert serv date to actual date*/ - INNER JOIN ETL_ACCESS_OWN.DIM_DATE DD - ON T06.SERV_DATE = DD.DATE_DIM_CK - - /*join so we can make sure serv date is < model run date*/ - INNER JOIN #WANG_MEMS T05 - ON T06.PLAN_DIM_CK = T05.PLAN_DIM_CK - AND T06.MPI_ID = T05.MPI_ID - - WHERE DD.DATE_DATE <= T05.admission_date - - GROUP BY 1,2,3 - ) T06 - ON T05.PLAN_DIM_CK = T06.PLAN_DIM_CK - AND T05.MPI_ID = T06.MPI_ID - AND T05.admission_date = T06.admission_date - - - LEFT JOIN #BUPRENORPHINE BUPREN - ON T05.PLAN_DIM_CK = BUPREN.PLAN_DIM_CK - AND T05.MPI_ID = BUPREN.MPI_ID - AND T05.admission_date = BUPREN.admission_date - - LEFT JOIN #NALOXONE NALOX - ON T05.PLAN_DIM_CK = NALOX.PLAN_DIM_CK - AND T05.MPI_ID = NALOX.MPI_ID - AND T05.admission_date = NALOX.admission_date - - LEFT JOIN #OPIOIDS OPIOID - ON T05.PLAN_DIM_CK = OPIOID.PLAN_DIM_CK - AND T05.MPI_ID = OPIOID.MPI_ID - AND T05.admission_date = OPIOID.admission_date -) -WITH DATA -PRIMARY INDEX(PLAN_DIM_CK, MPI_ID, admission_date) -; - -/******************************************************** -Step 5. Adding flags for when the member was - first diagnosed with a chronic condition -********************************************************/ -CREATE VOLATILE TABLE #FIRST_CC_DIAG AS ( -SELECT -PLAN_DIM_CK -,MPI_ID -,MIN(SERV_DATE) FIRST_CC_DIAG - -FROM ( -SELECT -DIAG.PLAN_DIM_CK -,T05.MPI_ID -,DIAG.CLAIM_NBR -,CC.CHRONIC_CONDITION -,MIN(SERV.SERVICE_START_DATE_DIM_CK) SERV_DATE - - FROM ETL_ACCESS_OWN.BRG_CLAIM_DIAGNOSIS DIAG - - /*Limit to only chronic condition diagnosis*/ - INNER JOIN POP_HEALTH_APP_OWN.CHRONIC_CONDITION_DX_DIMS CC - ON DIAG.DIAG_CODE_DIM_CK = CC.DIAG_CODE_DIM_CK - - /*Pull claim date*/ - INNER JOIN ETL_ACCESS_OWN.FT_SERVICE_TRANSACTION SERV - ON DIAG.CLAIM_NBR = SERV.CLAIM_NBR - -INNER JOIN ETL_ACCESS_OWN.DIM_MEMBER_CURR MEM - ON SERV.MEMBER_CURR_CK = MEM.MEMBER_CURR_CK - AND SERV.PLAN_DIM_CK = MEM.PLAN_DIM_CK - - /*Limit to our Membership*/ - INNER JOIN #WANG_MEMS T05 - ON MEM.MPI_ID = T05.MPI_ID - AND MEM.PLAN_DIM_CK = T05.PLAN_DIM_CK - -GROUP BY 1,2,3,4 -) A -GROUP BY 1,2 -) -WITH DATA -PRIMARY INDEX(PLAN_DIM_CK, MPI_ID) -ON COMMIT PRESERVE ROWS; - -/******************************************************** -Step 6. Adding in actual flags to the dataset -********************************************************/ - -DROP TABLE MANDA_OWN_TABLES.PYR8S_BH_DATA_FINAL; - -CREATE TABLE MANDA_OWN_TABLES.PYR8S_BH_DATA_FINAL AS ( - SELECT - DISTINCT - T07.admission_date - ,T07.member_dim_ck - ,T07.external_member_id - ,T07.plan_dim_ck - ,T07.member_curr_ck - ,T07.authorization_ck - ,T07.event_reason_dim_ck - ,T07.um_service_type_dim_ck - ,T07.admission_source_dim_ck - ,T07.attend_prov_aff_dim_ck - ,T07.diagnosis_type - ,T07.plan_type - ,T07.member_amisys_nbr - ,T07.sex - ,T07.edw_member_ck - ,T07.mpi_id - ,T07.pro_generic_product_desc - ,T07.diag_code - ,T07.diag_desc - ,T07.ptsd_count - ,T07.trauma_count - ,T07.self_harm_count - ,T07.schizo_count - ,T07.ocd_count - ,T07.covid19_count - ,T07.depression_count - ,T07.anxiety_count - ,T07.dementia_count - ,T07.parkinsons_count - ,T07.bipolar_count - ,T07.suicidtn_count - ,T07.suicide_count - ,T07.pers_dis_count - ,T07.bpd_count - ,T07.chron_pain_count - ,T07.bh_count - ,T07.bupren_count - ,T07.naloxone_count - ,T07.opioid_count - ,CC.cc_diag_30_days - ,CC.cc_diag_60_days - ,CC.cc_diag_90_days - ,SUI.suicide_prob - ,IPR.orca_score - ,IPR.sud_seg_value - - FROM MANDA_OWN_TABLES.PYR8S_BH_DATA_T02 T07 - - /*Flagging time person first diagnosis occured*/ - LEFT JOIN (SELECT - T05.plan_dim_ck - ,T05.mpi_id - ,T05.admission_date - ,(CASE WHEN DD.date_date BETWEEN (T05.admission_date - 30) AND T05.admission_date - THEN 1 - ELSE 0 END) cc_diag_30_days - ,(CASE WHEN DD.date_date BETWEEN (T05.admission_date - 60) AND T05.admission_date - THEN 1 - ELSE 0 END) cc_diag_60_days - ,(CASE WHEN DD.date_date BETWEEN (T05.admission_date - 90) AND T05.admission_date - THEN 1 - ELSE 0 END) cc_diag_90_days - - FROM #WANG_MEMS T05 - - LEFT JOIN #FIRST_CC_DIAG CC - ON T05.plan_dim_ck = CC.plan_dim_ck - AND T05.mpi_id = CC.mpi_id - - INNER JOIN ETL_ACCESS_OWN.DIM_DATE DD - ON CC.first_cc_diag = DD.date_dim_ck - ) CC - ON T07.PLAN_DIM_CK = CC.plan_dim_ck - AND T07.MPI_ID = CC.mpi_id - AND T07.admission_date = CC.admission_date - - /*Get suicide probability scores*/ - LEFT JOIN (select - mpi_id, - plan_dim_ck, - suicide_prob, - model_run_date - from DATA_SCI_APP_OWN.HCA_DS_CLN_SUIC_OUTPUT - qualify row_number() over (partition by mpi_id, plan_dim_ck order by model_run_date desc ) = 1 - ) as SUI - ON T07.plan_dim_ck = SUI.plan_dim_ck AND T07.mpi_id = SUI.mpi_id - - /*get ipro risk scores*/ - LEFT JOIN (SELECT - ab.admission_date, - m.edw_member_ck, - iprom.sud_seg_value, - iprom.orca_score, - iprom.publish_date - FROM etl_access_own.ft_authorization AS ab - JOIN etl_access_own.dim_plan AS p - ON ab.plan_dim_ck = p.plan_dim_ck - JOIN etl_access_own.dim_member AS m - ON ab.member_dim_ck = m.member_dim_ck - left join etl_access_own.dim_ipro_member as iprom - on m.edw_member_ck = iprom.edw_member_ck and ab.admission_date > iprom.publish_date - WHERE ab.admission_date BETWEEN '2021-01-01 00:00:00' AND '2023-04-01 00:00:00' - AND iprom.publish_date BETWEEN '2021-01-01 00:00:00' AND '2023-04-01 00:00:00' - AND ab.external_member_id IS NOT NULL -- cannot join if either admission_date or member_id is null - AND ab.admission_date IS NOT NULL -- since a visit is defined as a unique admit_date and member_id - AND p.plan_dim_ck IN (41,62,68,82,83,108,127,109,130,132,213,44,69,45,51,126,107,105,110,106, - 104,203,202,201,204,205,76,102,1,101,103,73,21,2,26,125,12,15,20,61,32,115,49,59,23,14,50,55, - 117,146,124,128,54,118,22,10,27,60,37,70,122,112,228,223,114,123,138,140,136,5,121,16,57,143, - 144,145,209,210,211,212,226,39,119,75,40,4,137,74,46) - AND left(ab.auth_nbr, 2) NOT IN ('OP', 'OB') - QUALIFY ROW_NUMBER() OVER (PARTITION BY m.edw_member_ck, ab.admission_date ORDER BY iprom.publish_date DESC) = 1 - ) AS IPR - ON T07.edw_member_ck = IPR.edw_member_ck AND T07.admission_date = IPR.admission_date -) -WITH DATA -PRIMARY INDEX(plan_dim_ck, mpi_id, admission_date); - - -SELECT COUNT(*) FROM #WANG_MEMS; - -select Top 1000 * from MANDA_OWN_TABLES.PYR8S_BH_DATA_FINAL; - -select count(*) from MANDA_OWN_TABLES.PYR8S_BH_DATA_FINAL; -select count(bupren_count) from MANDA_OWN_TABLES.PYR8S_BH_DATA_FINAL; -select count(NALOXONE_COUNT) from MANDA_OWN_TABLES.PYR8S_BH_DATA_FINAL; -select count(OPIOID_COUNT) from MANDA_OWN_TABLES.PYR8S_BH_DATA_FINAL; - - -select admission_date - ,external_member_id - ,member_amisys_nbr - ,ptsd_count - ,trauma_count - ,self_harm_count - ,schizo_count - ,ocd_count - ,covid19_count - ,depression_count - ,anxiety_count - ,dementia_count - ,parkinsons_count - ,bipolar_count - ,suicidtn_count - ,suicide_count - ,pers_dis_count - ,bpd_count - ,chron_pain_count - ,bh_count - ,bupren_count - ,naloxone_count - ,opioid_count - ,cc_diag_30_days - ,cc_diag_60_days - ,cc_diag_90_days - ,suicide_prob - ,orca_score - ,sud_seg_value -from MANDA_OWN_TABLES.PYR8S_BH_DATA_FINAL; \ No newline at end of file diff --git a/decorator/decorator.py b/decorator/decorator.py deleted file mode 100644 index 4665695..0000000 --- a/decorator/decorator.py +++ /dev/null @@ -1,111 +0,0 @@ -from functools import cache -from dataclasses import dataclass -from functools import wraps -import json - -@cache -def add(x, y): - print("adding", x, "and", y) - return (x + y) - -add(3,2) -add(3,5) - -# slow -def lucas(n): - """Get the nth Lucas number.""" - if n == 1: - return 2 - if n == 2: - return 1 - return lucas(n-1) + lucas(n-2) - -# fast, when there are recursive calculation -@cache -def lucas(n): - """Get the nth Lucas number.""" - if n == 1: - return 2 - if n == 2: - return 1 - return lucas(n-1) + lucas(n-2) - -@dataclass -class Point: - x: float - y: float - z: float - - -a = Point(10,12,15) -b = Point(10,12,15) -a == b - -def square(n): return n**2 -def cube(n): return n**3 -operations = [square, cube] - -list(map(bool, ["word", 0, "", 2])) -[*map(bool, ["word", 0, "", 2])] - - -def greet_me(name="friend"): - def greet(): - print(f"hello {name}") - return greet - -greet_me() - -def count_calls(func): - """A decorator that counts the number of times a function is called.""" - def wrapper(*args, **kwargs): - wrapper.call_count += 1 - print(f"{func.__name__} has been called {wrapper.call_count} times") - return func(*args, **kwargs) - wrapper.call_count = 0 - - return wrapper - -@count_calls -def say_hello(): - """print hello world""" - print("hello world") - -say_hello.__doc__ - - - - -def count_calls(func): - """A decorator that counts the number of times a function is called.""" - @wraps(func) - def wrapper(*args, **kwargs): - wrapper.call_count += 1 - print(f"{func.__name__} has been called {wrapper.call_count} times.") - return func(*args, **kwargs) - - wrapper.call_count = 0 # Initialize the call count - return wrapper - -@count_calls -def say_hello(): - """print hello world""" - print("hello world") - -say_hello.__doc__ - - -def jsonify(func): - """jsonify the output of the function""" - @wraps(func) - def wrapper(*args, **kwargs): - results = func(*args, **kwargs) - return json.dumps(results) - - return wrapper - -@jsonify -def get_data(): - return {"name": "Peter", "age": 45} - -get_data() \ No newline at end of file diff --git a/dummy b/dummy deleted file mode 100755 index e69de29..0000000 diff --git a/example.txt b/example.txt deleted file mode 100755 index 95105f3..0000000 --- a/example.txt +++ /dev/null @@ -1,30 +0,0 @@ -inpatient_future_risk: -risk_score: -er_risk_score: -age: -dx_code: -sex: -msh4_val: -adt_type: -source: -plan_code: -trimester_1_dt: -trimester_3_dt: -exp_diff: -ccsr: -ip_bh_risk_score: -total_risk_score: -npi: -trauma_count: -schizo_count: -covid19_count: -bipolar_count: -suicidtn_count: -chron_pain_count: -bh_count: -opioid_count: -cc_diag_90_days: -suicide_prob: -sud_seg_value: -anxiety_count: -orca_score: \ No newline at end of file diff --git a/form.jpg b/form.jpg deleted file mode 100644 index 3cf1603..0000000 Binary files a/form.jpg and /dev/null differ diff --git a/form1.ppm b/form1.ppm deleted file mode 100644 index d11458b..0000000 Binary files a/form1.ppm and /dev/null differ diff --git a/pytho_class/class_and_instance.py b/pytho_class/class_and_instance.py deleted file mode 100755 index e0964ae..0000000 --- a/pytho_class/class_and_instance.py +++ /dev/null @@ -1,75 +0,0 @@ - -# Create a class -class Employee: - # this is a class variable - raise_amount = 1.04 - # employee number equals zero - number_of_employee = 0 - - def __init__(self, first, last, pay): - self.first = first - self.last = last - self.pay = pay - self.email = first + "." + last + "@gmail.com" - # here we use Employee (class variable) because for all the instance this number won't change - Employee.number_of_employee += 1 - - def fullname(self): - return f"{self.first} {self.last}" - - def apply_raise(self): - # We set self.raise_amount(this is a class variable, but can be changed for individual instance) - self.pay = int(self.pay * self.raise_amount) - - -# Instantiate the class, get instances -emp_1 = Employee("Tom", "Jerry", 30) -emp_2 = Employee("Bill", "Clinton", 50) - -# Get attributes of the instance -emp_1.pay -emp_1.first -emp_1.last - -# call method of the instance -# note that the fullname() automatically take self, here is the instance emp_1, as the first argument -# even though the 'self' is not explicitly pass to it -emp_1.fullname() - -# another way to call this function is use the class, but this time we need to explicitly pass an instance -Employee.fullname(emp_1) - - -# retrieve class attributes -# Here emp_1 and emp_2 do not have attribute raise_amount, it will retrieve it from the class -print(emp_1.raise_amount) -print(emp_2.raise_amount) -print(Employee.raise_amount) - -# Here it shows what attributes they have. We can see that emp_1 does not have attribute raise amount -# But class Employee has -print(emp_1.__dict__) -print(Employee.__dict__) - -# what if we do the following -emp_1.raise_amount = 1.05 -# we will see that only emp_1's value changed, but not the other two. This is because we just created an instance -# attribute which only belongs to emp_1 -print(emp_1.raise_amount) -print(emp_2.raise_amount) -print(Employee.raise_amount) -# we can see that emp_1 has this 'raise_amount' attribute now. -print(emp_1.__dict__) -print(Employee.__dict__) - -# if we change the class attribute now -# and we can see that emp2's and class Employee's raise_amount has changed -Employee.raise_amount = 1.06 -print(emp_1.raise_amount) -print(emp_2.raise_amount) -print(Employee.raise_amount) - -# after we instantiate two instances, the number increment by 2 -print(emp_1.number_of_employee) -print(emp_2.number_of_employee) -print(Employee.number_of_employee) \ No newline at end of file diff --git a/pytho_class/class_inheritance.py b/pytho_class/class_inheritance.py deleted file mode 100755 index e0d82f8..0000000 --- a/pytho_class/class_inheritance.py +++ /dev/null @@ -1,95 +0,0 @@ -# Create a class -class Employee: - # this is a class variable - raise_amount = 1.04 - # employee number equals zero - number_of_employee = 0 - - def __init__(self, first, last, pay): - self.first = first - self.last = last - self.pay = pay - self.email = first + "." + last + "@gmail.com" - # here we use Employee (class variable) because for all the instance this number won't change - Employee.number_of_employee += 1 - - def fullname(self): - return f"{self.first} {self.last}" - - def apply_raise(self): - # We set self.raise_amount(this is a class variable, but can be changed for individual instance) - self.pay = int(self.pay * self.raise_amount) - - -# Instantiate the class, get instances -emp_1 = Employee("Tom", "Jerry", 30) -emp_2 = Employee("Bill", "Clinton", 50) - -print(emp_1.pay) -emp_1.apply_raise() -print(emp_1.pay) - - -class Developer(Employee): - raise_amount = 1.10 - - def __init__(self, first, last, pay, prog_lang): - # super() is used to initiate the first, last, and pay parameters the same way as Employee - super().__init__(first, last, pay) - self.prog_lang = prog_lang - - -dev_1 = Developer("Tom", "Jerry", 30, "python") -dev_1.apply_raise() -dev_2 = Developer("Bill", "Clinton", 50, "java") - - -print(dev_1.pay) -print(dev_1.prog_lang) -# print(help(Developer)) - - -class Manager(Employee): - - def __init__(self, first, last, pay, employee_list=None): - super().__init__(first, last, pay) - if employee_list is None: - self.employee_list = [] - else: - self.employee_list = employee_list - - def add_emp(self, emp): - if emp not in self.employee_list: - self.employee_list.append(emp) - - def remove_emp(self, emp): - if emp in self.employee_list: - self.employee_list.remove(emp) - - def print_employee(self): - for emp in self.employee_list: - print("-->", emp.fullname()) - - -mgr_1 = Manager("Sue", "Smith", 100, [dev_1]) - -print(mgr_1.email) -mgr_1.add_emp(dev_2) -mgr_1.add_emp(emp_1) - -print("before removing ...") -mgr_1.print_employee() - -mgr_1.remove_emp(dev_1) -print("after removing ...") -mgr_1.print_employee() - -# mgr–1 is an instance of Manager and Employee, but not Developer -print(isinstance(mgr_1, Manager)) -print(isinstance(mgr_1, Employee)) -print(isinstance(mgr_1, Developer)) - -# Developer is a subclass of Employee, but not of Manager: -print(issubclass(Developer, Developer)) -print(issubclass(Developer, Employee)) -print(issubclass(Developer, Manager)) \ No newline at end of file diff --git a/pytho_class/class_property_decorator.py b/pytho_class/class_property_decorator.py deleted file mode 100755 index e69de29..0000000 diff --git a/pytho_class/class_special_methods.py b/pytho_class/class_special_methods.py deleted file mode 100755 index 7cc60e0..0000000 --- a/pytho_class/class_special_methods.py +++ /dev/null @@ -1,55 +0,0 @@ -# Create a class -class Employee: - # this is a class variable - raise_amount = 1.04 - # employee number equals zero - number_of_employee = 0 - - def __init__(self, first, last, pay): - self.first = first - self.last = last - self.pay = pay - self.email = first + "." + last + "@gmail.com" - # here we use Employee (class variable) because for all the instance this number won't change - Employee.number_of_employee += 1 - - def fullname(self): - return f"{self.first} {self.last}" - - def apply_raise(self): - # We set self.raise_amount(this is a class variable, but can be changed for individual instance) - self.pay = int(self.pay * self.raise_amount) - - # for debugging, logging, meant to be used by developer - def __repr__(self): - return f"Employee({self.first}, {self.last}, {self.pay})" - # for end user to use - def __str__(self): - return f"{self.fullname()}, {self.email}" - - # add method - def __add__(self, other): - return self.pay + other.pay - - - - -# Instantiate the class, get instances -emp_1 = Employee("Tom", "Jerry", 30) -emp_2 = Employee("Bill", "Clinton", 50) - -# print will try to call __str__ first, if not exist, will call __repr__ instead -print(Employee) -print(emp_1) -# call __repr__ and __str__ special methods -print(repr(emp_1)) -print(str(emp_1)) -# this will call the __add__ sepcial method -print(emp_1.pay + emp_2.pay) - -print(emp_1.__repr__()) -print(emp_1.__str__()) - -# addition for integer is to call the __add__ for integer class. -print(int.__add__(1, 2)) -1 + 2 diff --git a/pytho_class/classmethod_and_staticmethod.py b/pytho_class/classmethod_and_staticmethod.py deleted file mode 100755 index 935737c..0000000 --- a/pytho_class/classmethod_and_staticmethod.py +++ /dev/null @@ -1,60 +0,0 @@ -from datetime import datetime -# Create a class -class Employee: - # this is a class variable - raise_amount = 1.04 - # employee number equals zero - number_of_employee = 0 - - def __init__(self, first, last, pay): - self.first = first - self.last = last - self.pay = pay - self.email = first + "." + last + "@gmail.com" - # here we use Employee (class variable) because for all the instance this number won't change - Employee.number_of_employee += 1 - - def fullname(self): - return f"{self.first} {self.last}" - - def apply_raise(self): - # We set self.raise_amount(this is a class variable, but can be changed for individual instance) - self.pay = int(self.pay * self.raise_amount) - - @classmethod - def set_raise_amount(cls, amount): - cls.raise_amount = amount - - # alternative constructor - # class method as alternative constructor - @classmethod - def from_string(cls, emp_str): - first, last, pay = emp_str.split("-") - return cls(first, last, pay) - - # class method take cls as default, instance method take self as default. - # static method takes none, it is just a regular method - @staticmethod - def is_workday(day: datetime): - if day.weekday() in (5,6): - return False - return True - -# Instantiate the class, get instances -emp_1 = Employee("Tom", "Jerry", 30) -emp_2 = Employee("Bill", "Clinton", 50) - -Employee.set_raise_amount(1.05) - -print(Employee.raise_amount) -print(emp_2.raise_amount) -print(emp_1.raise_amount) - -# class method as alternative constructor -emp_3_str = "John-Doe-7000" -emp_3 = Employee.from_string(emp_3_str) -emp_3.pay - -# test staticmethod -today_dt = datetime.now() -Employee.is_workday(today_dt) diff --git a/python_question.py b/python_question.py deleted file mode 100644 index 7861464..0000000 --- a/python_question.py +++ /dev/null @@ -1,135 +0,0 @@ -# replace a character in a string text without using .replace() function -def replace_str(text, old, new): - result = "" - for s in text: - if s == old: - s = new - result += s - return result - -text = "D t C mpBl ckFrid yS le" -old = " " -new = "a" - -replace_str(text, old, new) - -# Use .replace() function -text = "D t C mpBl ckFrid yS le" -text.replace(old, new) - - -""" -Given a positive integer num, write a function that returns True if num is a perfect square else False. -""" -def perfect_square(num): - num_sqrt = int(num**0.5) - result = num == num_sqrt**2 - return result - -perfect_square(25) -perfect_square(10) - - -""" -without using any package -Given an integer n, return the number of trailing zeroes in n factorial n! -""" -def factorial_training_zeros(n): - fact = n - while n > 1: - fact *= n - 1 - n -= 1 - - count = 0 - for s in str(fact)[::-1]: - if s == "0": - count += 1 - else: - break - return count - - -""" -You are provided with a large string and a dictionary of the words. -You have to find if the input string can be segmented into words using the dictionary or not. -""" - -def word_segmentation(word, dictionary): - - for i in range(1, len(word)+1): - word1 = word[0:i] - if word1 in dictionary: - word2 = word[i:] - if (word2 in dictionary) \ - or (not word2) \ - or word_segmentation(word2, dictionary): - return True - return False - -w1 = "shank3cancauseautism" -w2 = "shank3cannotcauseautism" -dictionary = ["shank3", "can", "cause", "autism", "cam", "lack"] - -word_segmentation(w1, dictionary) - -""" -remove duplicates from a list or array -""" -list1 = [1,1,2,3,3] -list2 = [1,1,1,2,2,2,3,3,4,5,6,7,8] -list(set(list1)) -list(set(list2)) - -def remove_dups(array): - new_array = [array[0]] - for i in array: - if i != new_array[-1]: - new_array.append(i) - return new_array - -# just count the number of unique values (list are ordered) -def count_unique(array): - n_unique = 0 - for i in range(len(array) - 1): - if array[i] != array[i+1]: - n_unique += 1 - return n_unique - -remove_dups(list1) -remove_dups(list2) -count_unique(list1) -count_unique(list2) - - - - -""" -How to sort a list? -""" -l1 = [1, 2, 5, 6, 4,4, 7, 9, 10, 8, 10] -sorted(l1) - -def sort_list(l): - new_l = [l[0]] - for i in l[1:]: - if i <= new_l[0]: - new_l.insert(0, i) - elif i >= new_l[-1]: - new_l.append(i) - else: - for j in range(1, len(new_l) - 1): - if i >= new_l[j] and i <= new_l[j+1]: - new_l.insert(j+1, i) - break - return new_l - -sort_list(l1) - -""" -Write a function that returns True if there is a Pythagorean triplet that satisfies a2+ b2 = c2 -""" - -def pythagorean(l): - l1 = [i**2 for i in l] - l1.sort() - \ No newline at end of file diff --git a/regular_expression.py b/regular_expression.py deleted file mode 100644 index 03f1571..0000000 --- a/regular_expression.py +++ /dev/null @@ -1,144 +0,0 @@ -import re - -# put angles around all numbers -# catch them as a group and then replace the group. -text = "Turn the 35 boxes into the 100 boxes." -re.sub(r"([0-9]+)", r"<\1>", text) -re.sub(r'(\d+)', r'<\1>', text) - - -# capture group -# which text will the patter match -pattern = r"the (.*)er they (.*), the \1er we \2" -text1 = "the faster they ran, the faster we ran" -text2 = "the faster they ran, the faster we ate" - -re.match(pattern, text1) -re.match(pattern, text2) -matched = re.match(pattern, text1) -print(matched) -type(matched) -# entire matched string -matched.group(0) -# each group being captured -matched.group(1) -matched.group(2) -# start and end position for matched string -matched.start() -matched.end() - -# not capture group (?: pattern ). -# "some" just match no capture, "cats" match and capture -pattern = r"(?:some|a few) (people|cats) like some \1" -text1 = "some cats like some cats" -text2 = "some cats like some dogs" -re.match(pattern, text1) -re.match(pattern, text2) -# entire matched string -matched = re.match(pattern, text1) -matched.group(0) -# each group being captured -matched.group(1) -matched.group(2) - - -# The (?=pattern) is a part of a regular expression known as a "positive lookahead" assertion. -# It is used to match a specific pattern only if it is followed by another specific pattern, -# without including the second pattern in the match. - -# The (?!pattern) is a part of a regular expression known as a "negative lookahead" assertion. -# It is used to match a specific pattern only if it is NOT followed by another specific pattern. - -text = "I like apple pie, but not apple juice or apple sauce." - -# apple not followed by juice -pattern1 = r"apple(?! juice)" -# apple followed by pie -pattern2 = r"apple(?= pie)" - -# should find two -re.findall(pattern1, text) -# should find one -re.findall(pattern2, text) - -# match telephon number ###-###-#### -text = "Here is my telephone cellphone 984-209-8878, this is also my number: 9842098878 " -pattern1 = r"\d{3}-\d{3}-\d{4}" -pattern2 = r"\d{3}-\d{3}-\d{4}|\d{10}" -re.findall(pattern1, text) -re.findall(pattern2, text) - -# match the first occurrence from the beginning, not in the middle -re.match(pattern2, text) -# match the first occurrence any where in the string -matched = re.search(pattern2, text) -matched.group() - -# match gruppy or grappies -# findall only find the groups, not the whole text? -text = "it is gruppy or gruppies" -pattern = r"grupp(y|ies)" -pattern1 = r"grupp(?:y|ies)" -pattern2 = r"gruppy" -re.findall(pattern, text) -re.search(pattern, text) -re.search(pattern, text).group() - -re.findall(pattern1, text) -re.search(pattern1, text) - -re.findall(pattern2, text) - -# In the first pattern, we match the entire date as a whole. -# In the second pattern, we capture the day, month, and year separately. -# You can see the difference between matching and capturing in the output. - -text = "Today's date is 14-10-2023." - -# Match the entire date pattern -pattern = r"\d{2}-\d{2}-\d{4}" -match = re.search(pattern, text) - -if match: - matched_text = match.group() - print("Matched:", matched_text) -else: - print("No match") - -# Capture the day, month, and year individually -pattern = r"(\d{2})-(\d{2})-(\d{4})" -match = re.search(pattern, text) - -if match: - day = match.group(1) - month = match.group(2) - year = match.group(3) - print("Captured:") - print("Day:", day) - print("Month:", month) - print("Year:", year) -else: - print("No match") - - -text = "My phone number is 123-456-7890" #, and my other number is 987-654-3210." -pattern = r"is (\d{3})-(\d{3})-(\d{4})" -matches = re.findall(pattern, text) -print(matches) - - -import nltk -text = 'That U.S.A. poster-print costs $12.40...' -# note here ?: match but not capture the group -pattern = r"""(?x) # set flag to allow verbose regexps - (?:[A-Z]\.)+ # abbreviations, e.g. U.S.A. - |\w+(?:-w+)* # words with optional internal hyphens - |\$?\d+(?:\.\d+)?%? # currency and percentages, e.g. $12.40, 82% - |\.\.\. # ellipsis - |[][.,;?():-_] # these are separate tokens; includes ], [ . , ( ) etc -""" -tokens = nltk.regexp_tokenize(text, pattern) -print(tokens) - - - diff --git a/sudoku.py b/sudoku.py deleted file mode 100644 index 8bc4c1e..0000000 --- a/sudoku.py +++ /dev/null @@ -1,78 +0,0 @@ - -# sudoku to be solved -puzzle = [ - [9, 0, 0, 2, 6, 5, 8, 0, 0], - [7, 0, 0, 4, 0, 0, 0, 6, 0], - [6, 0, 0, 9, 0, 0, 0, 0, 0], - [0, 0, 0, 6, 3, 0, 2, 7, 0], - [2, 0, 8, 0, 0, 0, 6, 0, 0], - [3, 0, 6, 8, 2, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 2, 4, 0, 0], - [4, 3, 2, 0, 8, 6, 7, 0, 0], - [0, 6, 7, 0, 0, 0, 0, 0, 0] -] - - -def print_sudoku(puzzle): - for i in range(len(puzzle)): - for j in range(len(puzzle)): - if j == 8: - print(puzzle[i][j]) - else: - print(puzzle[i][j], end=" ") - - -# get empty cell -def get_empty(puzzle): - for row in range(len(puzzle)): - for col in range(len(puzzle[0])): - if puzzle[row][col] == 0: - return row, col - return None - - -# test validation -def is_valid(puzzle, candidate, row, col): - # check row - if candidate in puzzle[row]: - return False - # check column - if candidate in [puzzle[i][col] for i in range(len(puzzle))]: - return False - # check 3*3 box - start_row = row // 3 * 3 - start_col = col // 3 * 3 - for i in range(start_row, start_row + 3): - for j in range(start_col, start_col + 3): - if puzzle[i][j] == candidate: - return False - return True - - -# solve puzzle -def solve_sudoku(puzzle): - # get empty cell - empty_cell = get_empty(puzzle) - if not empty_cell: - return True - row, col = empty_cell - - for candidate in range(1, 10): - if is_valid(puzzle, candidate, row, col): - puzzle[row][col] = candidate - if solve_sudoku(puzzle): - return True - - puzzle[row][col] = 0 - - return False - - -if __name__ == "__main__": - print("input sudoku:") - print_sudoku(puzzle) - if solve_sudoku(puzzle): - print("The solved sudoku is as follow:") - print_sudoku(puzzle) - else: - print("The input sudoku is not solvable") \ No newline at end of file diff --git a/test b/test deleted file mode 100755 index 6157ea0..0000000 --- a/test +++ /dev/null @@ -1,14 +0,0 @@ -CREATE OR REPLACE TEMPORARY VIEW - -CREATE VOLATILE TABLE # - -lab_teams.cmle.carmenta - - - -LAB_TEAMS.CMLE.CARMENTA - -CMLE_APP_OWN.PYR8S - -TERADATA_PROD.ETL_LOAD_OWN.DIM_MEMBER_CURR MEM -teradata_prod.pop_health_app_own \ No newline at end of file diff --git a/test1 b/test1 deleted file mode 100755 index 8b13789..0000000 --- a/test1 +++ /dev/null @@ -1 +0,0 @@ - diff --git a/test_code/__init__.py b/test_code/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/test_code/__pycache__/__init__.cpython-39.pyc b/test_code/__pycache__/__init__.cpython-39.pyc deleted file mode 100644 index ee075b4..0000000 Binary files a/test_code/__pycache__/__init__.cpython-39.pyc and /dev/null differ diff --git a/test_code/__pycache__/test_path.cpython-39.pyc b/test_code/__pycache__/test_path.cpython-39.pyc deleted file mode 100644 index 3a01518..0000000 Binary files a/test_code/__pycache__/test_path.cpython-39.pyc and /dev/null differ diff --git a/test_code/__pycache__/test_path1.cpython-39.pyc b/test_code/__pycache__/test_path1.cpython-39.pyc deleted file mode 100644 index ff50a8c..0000000 Binary files a/test_code/__pycache__/test_path1.cpython-39.pyc and /dev/null differ diff --git a/test_code/test_path.py b/test_code/test_path.py deleted file mode 100644 index 0caf2ff..0000000 --- a/test_code/test_path.py +++ /dev/null @@ -1,18 +0,0 @@ -import os -from pathlib import Path -import test_path1 -from .. import test_flush - -os_path = os.path.abspath(__file__) -os_path_1 = os.path.dirname(os.getcwd()) -os_path_2 = os.getcwd() - -new_path = Path(__file__) -new_path_parents = Path(__file__).parents[1] - -print(__name__) -print(f"os path: {os_path}") -print(f"os path: {os_path_1}") -print(f"os path: {os_path_2}") -print(f"new path: {new_path}") -print(f"new path: {new_path_parents}") diff --git a/test_code/test_path1.py b/test_code/test_path1.py deleted file mode 100644 index 1b0c806..0000000 --- a/test_code/test_path1.py +++ /dev/null @@ -1 +0,0 @@ -print(__name__) diff --git a/test_code/wrap_terminal_output.py b/test_code/wrap_terminal_output.py deleted file mode 100644 index 5a61efa..0000000 --- a/test_code/wrap_terminal_output.py +++ /dev/null @@ -1,93 +0,0 @@ -import pandas as pd -import numpy as np - -pd.set_option("display.max_columns", None) -pd.set_option("display.width", 400) -np.set_printoptions(linewidth=400) - -data = """ -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| CVE | SEVERITY | CVSS | PACKAGE | VERSION | STATUS | PUBLISHED | DISCOVERED | DESCRIPTION | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| CVE-2019-12900 | critical | 9.80 | python | 3.9.7 | fixed in 3.10.3, 3.9.11, 3.8.13,... | > 3 years | < 1 hour | BZ2_decompress in decompress.c in bzip2 through | -| | | | | | > 3 years ago | | | 1.0.6 has an out-of-bounds write when there are | -| | | | | | | | | many selectors. | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| CVE-2016-10164 | critical | 9.80 | libxpm | 1:3.5.12-1.1~deb11u1 | fixed in 1:3.5.12-1 | > 6 years | < 1 hour | Multiple integer overflows in libXpm before | -| | | | | | > 6 years ago | | | 3.5.12, when a program requests parsing XPM | -| | | | | | | | | extensions on a 64-bit platform, allow remote | -| | | | | | | | | attackers to cau... | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| PRISMA-2022-0168 | high | 7.80 | pip | 23.1.2 | open | > 1 years | < 1 hour | An issue was discovered in pip (all versions) | -| | | | | | | | | because it installs the version with the highest | -| | | | | | | | | version number, even if the user had intended to | -| | | | | | | | | obtain... | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| CVE-2022-42919 | high | 7.80 | python | 3.9.7 | fixed in 3.10.9, 3.9.16 | > 6 months | < 1 hour | Python 3.9.x before 3.9.16 and 3.10.x before | -| | | | | | > 6 months ago | | | 3.10.9 on Linux allows local privilege escalation | -| | | | | | | | | in a non-default configuration. The Python | -| | | | | | | | | multiprocess... | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| CVE-2015-20107 | high | 7.60 | python | 3.9.7 | fixed in 3.10.8 | > 1 years | < 1 hour | In Python (aka CPython) up to 3.10.8, the mailcap | -| | | | | | > 1 years ago | | | module does not add escape characters into | -| | | | | | | | | commands discovered in the system mailcap file. | -| | | | | | | | | This may ... | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| CVE-2023-24329 | high | 7.50 | python | 3.9.7 | fixed in 3.11 | > 3 months | < 1 hour | An issue in the urllib.parse component of | -| | | | | | 85 days ago | | | Python before v3.11 allows attackers to bypass | -| | | | | | | | | blocklisting methods by supplying a URL that | -| | | | | | | | | starts with blan... | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| CVE-2022-45061 | high | 7.50 | python | 3.9.7 | fixed in 3.11.1, 3.10.9, 3.9.16,... | > 6 months | < 1 hour | An issue was discovered in Python before 3.11.1. | -| | | | | | > 5 months ago | | | An unnecessary quadratic algorithm exists in one | -| | | | | | | | | path when processing some inputs to the IDNA (RFC | -| | | | | | | | | 34... | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| CVE-2022-40898 | high | 7.50 | wheel | 0.37.0 | fixed in 0.38.1 | > 5 months | < 1 hour | An issue discovered in Python Packaging Authority | -| | | | | | > 4 months ago | | | (PyPA) Wheel 0.37.1 and earlier allows remote | -| | | | | | | | | attackers to cause a denial of service via | -| | | | | | | | | attacker co... | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| CVE-2020-10735 | high | 7.50 | python | 3.9.7 | fixed in 3.10.7, 3.9.14, 3.8.14,... | > 8 months | < 1 hour | A flaw was found in python. In algorithms with | -| | | | | | > 5 months ago | | | quadratic time complexity using non-binary bases, | -| | | | | | | | | when using int(\"text\"), a system could take 50ms | -| | | | | | | | | to... | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -| CVE-2018-25032 | high | 7.50 | python | 3.9.7 | fixed in 1.2.12 | > 1 years | < 1 hour | zlib before 1.2.12 allows memory corruption when | -| | | | | | > 1 years ago | | | deflating (i.e., when compressing) if the input | -| | | | | | | | | has many distant matches. | -+------------------+----------+------+---------------+------------------------+-------------------------------------+-------------+------------+----------------------------------------------------+ -""" - - -def wrap_output(data): - # Remove leading/trailing whitespaces and split the data into lines - lines = data.strip().split('\n') - - # Extract header row and content rows - header = [col.strip() for col in lines[1].split('|')[1:-1]] - rows = [line.strip().split('|')[1:-1] for line in lines[3:-2]] - - # Remove leading/trailing whitespaces from each cell - rows = [[col.strip() for col in row] for row in rows] - - # Create a pandas DataFrame - df = pd.DataFrame(rows, columns=header) - - # Drop None in CVE column - df = df.dropna(subset=["CVE"]) - - # Replace "" with None - df["CVE"] = df["CVE"].replace("", None) - - # Forward fill the missing values in the 'CVE' column - df['CVE'].fillna(method='ffill', inplace=True) - - # Combine the text for each column based on group - grouped_df = df.groupby('CVE').agg(lambda x: ' '.join(x.dropna())) - - # Remove white spaces. - grouped_df = grouped_df.applymap(lambda x: x.strip() if isinstance(x, str) else x) - - return grouped_df - diff --git a/test_flush.py b/test_flush.py deleted file mode 100755 index a8f19e2..0000000 --- a/test_flush.py +++ /dev/null @@ -1,197 +0,0 @@ -from time import sleep - -for i in range(10): - print( - f"\r{i}--\b{i+1} {i+2}", - end="", - flush=True - ) - sleep(0.5) -print(f"\r \r", end="") - - -pattern = r"\w*rupture\w*membrane\w*" -strings = [ - "prematurerupturedmembranes", - "primarycs39wks", - "prolongedruptureofmembranes", - "repeat", - "repeatat38weeksforiugr", - "repeatcsession", - "rightlegpainfluidcheck", - "ruptureofmembranes", -] - -for s in strings: - match = re.match(pattern, s) - if match: - print(f"Matched: {match.group()}") - else: - print(f"No match found in string: {s}") - - -bh_list = ['99acd', -'abnormalbehavior', -'acuteexacerbationofchronicparanoidschizophrenia', -'acutepsychosis', -'acutesycosis', -'agressiveschizophreniaf29', -'auditoryhallucinationspara', -'bh', -'bhdisastertransfer', -'bipolar', -'bipolardepression', -'bipolardisorder', -'ce', -'complaintnotgiven', -'confuseddisorganized', -'depression', -'depressionschizoaffectivedo', -'depressionsi', -'depressionwithpsyc', -'depressionwithsi', -'depressionwithsuic', -'depressionwithsuicidalideation', -'depressionwithsuicidalideations', -'depressionwithsuicidalideationsandattempt', -'depressionwthsuicialideation', -'depressivedo', -'disruptivemooddysregulationdisorder', -'dmdd', -'dxalcoholntoxication', -'eatingdisordermajordepressivedisorder', -'ereval', -'homicidalideation', -'intentionaloverdoseoftrazodonecmshcc', -'majordepression', -'majordepressionsuicidalideation', -'majordepressionsuicidalthoughts', -'majordepressionwithpsychosis', -'majordepressivedisorder', -'majordepressivedisorderrecurent', -'majordepressivedo', -'majordispressiondisorder', -'mdd', -'mddalcoholusedisorder', -'mddrecurrentsevere', -'mddwithpsychosis', -'medicalclearance', -'medicalclearancesuicidalattemps', -'mentalhealth', -'mentalhealthproblem', -'metalevaluation', -'mooddisorder', -'nocomplaintgiven', -'odd', -'overdose', -'paranoidschizophrenia', -'paranoidschizophreniaba', -'psycheval', -'psychosis', -'psychosisnos', -'psychosisunspecifiedtype', -'psychoticdisorder', -'psychproblem', -'ptsd', -'rash', -'schizoaffective', -'schizoaffectivedepressivetype', -'schizoaffectivedisorderdepressivetype', -'schizoaffectivedo', -'schizoaffectivedooverdosedepression', -'schizophrenia', -'schizophreniabipolar', -'schizophreniainacuteexacerbation', -'schizophreniaparanoidtype', -'schizophreniasuicidalideation', -'si', -'suicidal', -'suicidalideation', -'suicidalideations', -'suicideattempt', -'unknown', -'unspecifiedmooddisorder', -'unspecifiedpsychosis', -'unspeecifieddepressivedisorder', -] - -bh_regex_list = [ - r"\w*depressi\w*", - r"\w*schizo(?:phrenia|affective)?\w*", - r"\w*suicid(?:al)?\w*", - r"\w*psych(?:osis)?\w*", - r"\w*mood\w*disorder\w*", - r"\w*bipolar\w*", - r"\w*mentalhealth\w*", - r"\w*mdd\w*", - "99acd", - "abnormalbehavior", - "acutesycosis", - "auditoryhallucinationspara", - "bh", - "bhdisastertransfer", - "ce", - "complaintnotgiven", - "confuseddisorganized", - "dxalcoholntoxication", - "ereval", - "homicidalideation", - "intentionaloverdoseoftrazodonecmshcc", - "majordispressiondisorder", - "medicalclearance", - "metalevaluation", - "nocomplaintgiven", - "odd", - "overdose", - "ptsd", - "rash", - "si", - "unknown", -] - -from time import perf_counter - -start_time = perf_counter() -for i in range(100): - unmatched_strings = [] - for s in bh_list: - matched = re.search('|'.join(bh_regex_list), s) - if not matched: - unmatched_strings.append(s) -end_time = perf_counter() -print(end_time - start_time) - -start_time = perf_counter() -for i in range(100): - unmatched_strings = [] - for s in ob_list: - matched = False - for r in regex_list: - if re.search(r, s): - matched = True - break - if not matched: - unmatched_strings.append(s) -end_time = perf_counter() -print(end_time - start_time) - - - -print(unmatched_strings) - -with mlflow.start_run(run_name="model1") as run: - mlflow.log_param("label", "price") - mlflow.log_param("feature", "bedroom") - mlflow.spark.log_model("model_name", "model", input_example=train_df.limit(5).toPandas()) - mlflow.log_metric("rmse", rmse) - -from mlflow.tracking import MlflowClient -client = MlflowClient() -client.list_experiments() - -experiment_id = run.info.experiment_id -run_df = mlflow.search_runs(experiment_id) - - -run_df = mlflow.search_runs(experiment_id, order_by=["msr"]) - diff --git a/test_new b/test_new deleted file mode 100644 index 42bf416..0000000 --- a/test_new +++ /dev/null @@ -1,26 +0,0 @@ -ptsd_count, -trauma_count, -self_harm_count, -schizo_count, -ocd_count, -covid19_count, -depression_count, -anxiety_count, -dementia_count, -parkinsons_count, -bipolar_count, -suicidtn_count, -suicide_count, -pers_dis_count, -bpd_count, -chron_pain_count, -bh_count, -bupren_count, -naloxone_count, -opioid_count, -cc_diag_30_days, -cc_diag_60_days, -cc_diag_90_days, -suicide_prob, -orca_score, -sud_seg_value, \ No newline at end of file diff --git a/test_out.py b/test_out.py deleted file mode 100644 index f17859b..0000000 --- a/test_out.py +++ /dev/null @@ -1,33 +0,0 @@ -a= [ -"age", -"dx_code", -"sex", -"msh4_val", -"adt_type", -"source", -"trimester_1_dt", -"trimester_3_dt", -"exp_diff", -"ccsr", -"npi", - -"plan_code", -"inpatient_future_risk", -"risk_score", -"er_risk_score", -"ip_bh_risk_score", -"total_risk_score", -"trauma_count", -"schizo_count", -"covid19_count", -"bipolar_count", -"suicidtn_count", -"chron_pain_count", -"bh_count", -"opioid_count", -"cc_diag_90_days", -"suicide_prob", -"sud_seg_value", -"anxiety_count", -"orca_score" - ] diff --git a/tt.sql b/tt.sql deleted file mode 100755 index e69de29..0000000