addressalign-toparrow-leftarrow-rightbackbellblockcalendarcameraccwchatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscrosseditemptyheartfacebookfolderfullheartglobegmailgoogleimagesinstagramlinklocation-pinmagnifying-glassmailminusmoremuplabelShape 3 + Rectangle 1outlookpersonplusprice-ribbonImported LayersImported LayersImported Layersshieldstartickettrashtriangle-downtriangle-uptwitteruseryahoo

KataMedication, experimentation and fun

From: Karl S.
Sent on: Monday, September 6, 2010 5:31 PM

Hello Everybody!

I would like to share some thoughts I got after the last GothPy meeting involving the Kata called KataMedication.

Playing around and trying to simplify as much as possible I came up with this *nix command for querying one patient (that is me) on usage of two medications in the last five days:

cat karl.svartholm | egrep -i 'aspirin|valium' | cut -f 2 | awk '{print substr($0,length+1-5)}' | awk '{s+=$1} END {print s}' | awk '{c+=gsub(s,s)}END{print c}' s='2'

Given the existence of a file karl.svartholm containing the patient data:

aspirin[masked]
alvedon[masked]
valium [masked]

Yes, it all look outlandish. Let me start by explaining the data format: The last character on each line represents today, the one next to it yesterday and so forth. A zero means that the medication was not taken on that day and a one that it was. The data covers two weeks and during that time three medications was taken.

Now, let us go through the execution step by step. First get the whole patient file:

user@host:~/kata_medication$ cat karl.svartholm
aspirin[masked]
alvedon[masked]
valium[masked]
Then show only the medications we are interested in; Aspirin and Valium:
user@host:~/kata_medication$ cat karl.svartholm | egrep -i 'aspirin|valium'
aspirin[masked]
valium[masked]
Remove the names of the medications, leave only the data:
user@host:~/kata_medication$ cat karl.svartholm | egrep -i 'aspirin|valium' | cut -f 2
[masked]
[masked]
Show only the last five days, that is what the "-5" does at the end of the line:
user@host:~/kata_medication$ cat karl.svartholm | egrep -i 'aspirin|valium' | cut -f 2 | awk '{print substr($0,length+1-5)}'
11110
11100
Sum up the numbers from both rows:
user@host:~/kata_medication$ cat karl.svartholm | egrep -i 'aspirin|valium' | cut -f 2 | awk '{print substr($0,length+1-5)}' | awk '{s+=$1} END {print s}'
22210
Count the number of twos:
user@host:~/kata_medication$ cat karl.svartholm | egrep -i 'aspirin|valium' | cut -f 2 | awk '{print substr($0,length+1-5)}' | awk '{s+=$1} END {print s}' | awk '{c+=gsub(s,s)}END{print c}' s='2'
3

...and we got 3, that means three days which is the correct answer.

I have not tried to create the perfect solution or the smallest solution, I do not even see this as a proper solution, I see it as an interesting experiment.

So what do I want with this? I want to highlight the importance of questioning assumptions. Assumptions such as: We should use Object Oriented Programming, Test Driven Development/Design, Python and we need to write and persist code.
Further more I want you to tell me that all I have said and done is wrong :).

During this experiment I tried to follow the instructions below. I got them from http://bazaar.launchpad.net/~bache/%2Bjunk/KataPossession/annotate/13/README.txt

This is a Kata exercise concerned with what simple, well-tested code looks like.

You have a collection of data about patients which you wish to query. The format of the data is not specified, you can store it any way you please, (migrating the data to your chosen format is not included in the exercise). In any case, the data consists of patients, who may take medications, and have prescriptions for those medications. A prescription has a medication name, a dispense date and how many days worth of medication it comprises. What you want to find out from the data is if any patients have been taking dangerous combinations of medicines. You are to write a program to find out the number of days that a given patient has taken all of a list of particular medications, out of the past n days. You can assume that if the patient has a prescription for a medication, that they begin taking it on the dispense date, and stop when the supply has run out.

This program will be used by caregivers to spot if the patient is taking a bad combination of medicines.

You can go about this exercise in different ways. Perhaps the first thing to do is to try it out for yourself, and write some code to solve the problem. Then you might like to look at the solutions presented here.

note:
?????to run the "ExhibitA" tests you will need to install py.test: http://pytest.org
?????to run the "ExhibitB" tests you will need to install TextTest: http://texttest.org
?????to run the "ExhibitC" tests you need to have python 2.6, and also mockito installed: http://code.google.com/p/mockito/wiki/MockitoForPython"

PS.
As I was writing this I realized that the command could be shortened even more. Less readable maybe but it makes it more apparent that AWK does almost all the work. Shortened version: egrep -i 'aspirin|valium' karl.svartholm | awk '{print substr($0,length+1-5)}' | awk '{s+=$1} END {print s}' | awk '{c+=gsub(s,s)}END{print c}' s='2'

Cheers,
Karl Svartholm

Our Sponsors

  • Bache Consulting

    Paying for the meetup hosting and refreshments at meetings.

  • Dalke Scientific

    sponsors with refreshments for meetings.

  • Software Skills

    Providing a venue and refreshments for meetings.

  • Mecel

    Providing a venue and refreshments for meetings

  • Squeed

    Providing a venue and refreshments for meetings

  • Metrical

    Refreshments at meetings

  • Tajitsu Industries AB

    Providing a venue and refreshments for meetings.

  • Duego

    Providing a venue and refreshments for meetings

  • Spotify

    providing a venue and refreshments for meetings

  • Jeppesen Systems

    Providing a venue for meetings

  • Lavasoft

    Providing a venue for meetings

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy