Skip to content

Instantly share code, notes, and snippets.

View juanarrivillaga's full-sized avatar

Juan juanarrivillaga

View GitHub Profile
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)
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
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
# 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])
def f(arg1, arg2):
global some_var # global directive
some_var = arg1 + arg2
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]
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))
def sum1(a,b,c):
global total
total = a + b + c
def ar():
sum1(1, 2, 3)
print(total / 3)
ar()
# 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":
def flatten(items):
for item in items:
if isinstance(item, list):
yield from item
else:
yield item