This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Record = namedtuple('Record', 'computer_name ip_address id hist_item') | |
class KeyedRecord(Record): | |
__slots__ = () | |
def __getitem__(self, item): | |
try: | |
return getattr(self,item) | |
except TypeError: | |
return super(KeyedMatch, self).__getitem__(item) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: class Mutable: | |
...: def __init__(self,val=0): | |
...: self.val = val | |
...: def __iadd__(self, other): | |
...: return Mutable(self.val + other.val) | |
...: | |
In [2]: m1 = Mutable(10) | |
In [3]: m2 = m1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def split(n): | |
if len(n) < 1: | |
return '', '' | |
first = n[0] | |
rest = n[1:] | |
a, b = split(rest) | |
if first.islower() or first in ('_','.'): | |
return first + a, b | |
elif first.isupper() or first in (' ', '|'): | |
return a, first + b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# a list of lists is a perfectly sane container | |
a = [['Al', 'red', '1', '1', 'blue', 'green','', '65'], | |
['Bill', 'yellow', '1', '2', 'blue', 'red','', '55'], | |
['Alice', 'pink', '1', '3', 'blue', 'green','', '66'], | |
['Fred', 'pink', '1', '4', 'orange', 'puce','', '65']] | |
# keep in mind, 0-based index is the python way | |
print(a[1]) | |
print(a[2][0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f(arg1, arg2): | |
global some_var # global directive | |
some_var = arg1 + arg2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_of_patients = [['123456','321','John','Smith','P1','12/06/2017 03:34 PM', 'Loc 1', 'Panadol|Nurofen|Meloxicam'],['123336','311','Jane','Doe','P3','13/06/2017 03:34 PM', 'Loc 2', 'Asprin|Voltaren|Vitamin R']] | |
# note, we would ideally use a set for efficient look-up {'Asprin', 'Meloxicam', 'Nurofen'} | |
list_of_medications = ['Asprin', 'Meloxicam', 'Nurofen'] | |
for sub in list_of_patients: | |
sub[7] = [s for s in sub[7].split('|') if s in list_of_medications] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def numin_er(srch_string): | |
def inner(mem): | |
return srch_string.count(mem) | |
return inner | |
sublist = ('x', 'str', 'a', 'pr') | |
string = "rprpraxp" | |
result = sum(map(numin_er(string), sublist)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# your function works on a single element in your container | |
def converter(period): | |
period = period.lower() | |
if period[-1:] == "d": | |
period_unit = 1 | |
elif period[-1:] == "w": | |
period_unit = 7 | |
elif period[-1:] == "m": | |
period_unit = 30 | |
elif period[-1:] == "y": |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flatten(items): | |
for item in items: | |
if isinstance(item, list): | |
yield from item | |
else: | |
yield item | |