tags:

views:

169

answers:

5

here's my code!

import csv
import collections
import pdb

def do_work():
  (data,counter)=get_file('thefile.csv')
  b=samples_subset1(data,counter,'/pythonwork/samples_subset4.csv',1)
  medications_subset2(b,['BUPRENORPHINE'])

def write_file(data,filename):
  with open(filename,'wb') as outfile:
    writer=csv.writer(outfile)
    for row in data:
      writer.writerow(row)

def get_file(start_file):
  with open(start_file,'rb') as f:
    data=list(csv.reader(f))
    counter=collections.defaultdict(int)

    for row in data:
      counter[row[10]]+=1
  return (data,counter)

def samples_subset1(data,counter,output_file,sample_cutoff):
  b_counter=0
  b=[]
  for row in data:
    if counter[row[10]]>=sample_cutoff:
      b.append(row) 
      b_counter+=1
  write_file(b,output_file)
  return b

def medications_subset2(b,drug_input):

  brand_names={'MORPHINE':['ASTRAMORPH','AVINZA','CONTIN','DURAMORPH','INFUMORPH',
                     'KADIAN','MS CONTIN','MSER','MSIR','ORAMORPH',
                     'ORAMORPH SR','ROXANOL','ROXANOL 100'],
         'OXYCODONE':['COMBUNOX','DIHYDRONE','DINARCON','ENDOCET','ENDODAN',
                      'EUBINE','EUCODAL','EUKODAL','EUTAGEN','OXYCODONE WITH ACETAMINOPHEN CAPSULES',
                      'OXYCODONE WITH ASPIRIN,','OXYCONTIN','OXYDOSE','OXYFAST','OXYIR',
                      'PANCODINE','PERCOCET','PERCODAN','PROLADONE','ROXICET',
                      'ROXICODONE','ROXIPRIM','ROXIPRIN','TECODIN','TEKODIN',
                      'THECODIN','THEKOKIN','TYLOX'],
         'OXYMORPHONE':['NUMORPHAN','OPANA','OPANA ER'],
         'METHADONE':['ALGIDON','ALGOLYSIN','AMIDON','DEPRIDOL','DOLOPHINE','FENADONE',
                      'METHADOSE','MIADONE','PHENADONE'],
         'BUPRENORPHINE':['BUPRENEX','LEPTAN','SUBOXONE','SUBUTEX','TEMGESIC'],
         'HYDROMORPHONE':['DILAUDID','HYDAL','HYDROMORFAN','HYDROMORPHAN','HYDROSTAT',
                          'HYMORPHAN','LAUDICON','NOVOLAUDON','OPIDOL','PALLADONE',
                          'PALLADONE IR','PALLADONE SR'],
         'CODEINE':['ACETAMINOPHEN WITH CODEINE','ASPIRIN WITH CODEINE','EMPIRIN WITH CODEINE',
                    'FLORINAL WITH CODEINE','TYLENOL 3','TYLENOL 4','TYLENOL 5'],
         'HYDROCODONE':['ANEXSIA','BEKADID','CO-GESIC','CODAL-DH','CODICLEAR-DH',
                        'CODIMAL-DH','CODINOVO','CONATUSSIN-DC','CYNDAL-HD','CYTUSS-HC',
                        'DETUSSIN','DICODID','DUODIN','DURATUSS-HD','ENDAL-HC','ENTUSS',
                        'ENTUSS-D','G-TUSS','HISTINEX-D','HISTINEX-HC','HISTUSSIN-D','HISTUSSIN-HC',
                        'HYCET','HYCODAN','HYCOMINE','HYDROCODONE/APAP','HYDROKON',
                        'HYDROMET','HYDROVO','KOLIKODOL','LORCET','LORTAB',
                        'MERCODINONE','NOROCO','NORGAN','NOVAHISTEX','ORTHOXYCOL',
                        'POLYGESIC','STAGESIC','SYMTAN','SYNKONIN','TUSSIONEX','VICODIN',
                        'VICOPROFEN','XODOL','ZYDONE']}

  #pdb.set_trace()
  c=[]
  done=False
  for row in b:
    done=False    
    for drug in drug_input:
      for brand in brand_names[drug]:    
        if row[1].upper().find(brand)!=-1:
          c.append(row)
          print row[1]
          done=True
          break
      if done:
        break

  write_file(c,'/pythonwork/medications_subset3.csv')
  1. are my indentations easy to read and in accordance with standards?
  2. am i breaking up the code into usable functions in a clear, usable way?
  3. can you give general criticism / advice on how to improve my syntax?

thank you so much for your help

+2  A: 

Use 4 spaces instead of two.

Comment your code.

Nathan
These kind of comments should go in comments not in answers...
Teja Kantamneni
You can use any number of spaces or tabs as long as it is consistent (I use tabs so one user can set to see 2 spaces and I can use 4)
Mark
I disagree, Teja. Although OP's question is a good candidate for being closed, Nathan's answer does specifically address the questions that were being asked. This answer is fine.
Adam Crossland
While the number of spaces to indent is ultimately up to the programmer, PEP 8 does specify 4 spaces, and the vast, vast majority of Python code follows that convention.
Adam Crossland
Also don't mix tabs and spaces. Better yet, just use spaces, nobody wants to configure 40 different source code editor presets to be able to read/edit code from people who use different tab settings.
Longpoke
+4  A: 

The main things that stick out are using 4 space tabs instead of two, adding spaces between operators( =, ==, +=, <=, etc.), and commenting everywhere. Your variable names should also say what they contain, instead of things such as b.

Check out PEP 8 Style guide for more

Zonda333
+4  A: 

First: this is not a code-review site. You should have a specific question.

That said, since the code's here I'll comment on it.

  • Your indenting is fine. This is a non-issue in Python since there is a style-guide ruling: four spaces.
  • Another style-guide point: operators like = need spaces around them.
  • Use better function and variable names. b is terrible; it gives no idea what it's for. samples_subset1 is only marginally better.
  • Add docstrings and comments everywhere.
katrielalex
+1  A: 

Your indenting is fine - though not in line with PEP 8, which specifies four spaces. However, the code itself is not very Pythonic. Function names should be more descriptive than 'do_work()', and the whole looping mechanism at the bottom is very Java-styled, not Pythonic.

It's hard to give specific areas to focus on because you haven't commented the code (another must!), but I would advise you to look up list comprehensions, while loops, and other mechanisms to achieve the desired result.

Hint: Though Pythonic code should always be commented, if it's too hard to figure out what a looping construct is supposed to do just from looking at the code, it's probably not Pythonic.

thebackhand
i appreciate your answer! can you please tell me how i would change that triple for loop to be pythonic?
I__
+1  A: 

Consider adding a main function. See this article for guidelines.

Also, running pylint can help you improve your code and make it more pythonic.

GreenMatt