Exemplo n.º 1
0
def test_create_delete_temp_file():
    # test temporary_filename() and delete_file() functions
    fname = temporary_filename()
    with open(fname, 'w') as tmpfile:
        tmpfile.write('any content will do')
    assert os.path.isfile(fname) is True
    delete_file(fname)
    assert os.path.isfile(fname) is False
def test_create_delete_temp_file():
    # test temporary_filename() and delete_file() functions
    fname = temporary_filename()
    with open(fname, 'w') as tmpfile:
        tmpfile.write('any content will do')
    assert os.path.isfile(fname) is True
    delete_file(fname)
    assert os.path.isfile(fname) is False
Exemplo n.º 3
0
 def __init__(self, input_data, tax_year, reform, assump):
     # pylint: disable=too-many-branches,too-many-statements
     self.errmsg = ''
     # check name and existence of INPUT file
     inp = 'x'
     if isinstance(input_data, six.string_types):
         # remove any leading directory path from INPUT filename
         fname = os.path.basename(input_data)
         # check if fname ends with ".csv"
         if fname.endswith('.csv'):
             inp = '{}-{}'.format(fname[:-4], str(tax_year)[2:])
         else:
             msg = 'INPUT file name does not end in .csv'
             self.errmsg += 'ERROR: {}\n'.format(msg)
         # check existence of INPUT file
         self.cps_input_data = input_data.endswith('cps.csv')
         if not self.cps_input_data and not os.path.isfile(input_data):
             msg = 'INPUT file could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     elif isinstance(input_data, pd.DataFrame):
         inp = 'df-{}'.format(str(tax_year)[2:])
     else:
         msg = 'INPUT is neither string nor Pandas DataFrame'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # check name and existence of REFORM file
     ref = '-x'
     if reform is None:
         self.specified_reform = False
         ref = '-#'
     elif isinstance(reform, six.string_types):
         self.specified_reform = True
         # remove any leading directory path from REFORM filename
         fname = os.path.basename(reform)
         # check if fname ends with ".json"
         if fname.endswith('.json'):
             ref = '-{}'.format(fname[:-5])
         else:
             msg = 'REFORM file name does not end in .json'
             self.errmsg += 'ERROR: {}\n'.format(msg)
         # check existence of REFORM file
         if not os.path.isfile(reform):
             msg = 'REFORM file could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     else:
         msg = 'TaxCalcIO.ctor: reform is neither None nor str'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # check name and existence of ASSUMP file
     asm = '-x'
     if assump is None:
         asm = '-#'
     elif isinstance(assump, six.string_types):
         # remove any leading directory path from ASSUMP filename
         fname = os.path.basename(assump)
         # check if fname ends with ".json"
         if fname.endswith('.json'):
             asm = '-{}'.format(fname[:-5])
         else:
             msg = 'ASSUMP file name does not end in .json'
             self.errmsg += 'ERROR: {}\n'.format(msg)
         # check existence of ASSUMP file
         if not os.path.isfile(assump):
             msg = 'ASSUMP file could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     else:
         msg = 'TaxCalcIO.ctor: assump is neither None nor str'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # create OUTPUT file name and delete any existing output files
     self._output_filename = '{}{}{}.csv'.format(inp, ref, asm)
     delete_file(self._output_filename)
     delete_file(self._output_filename.replace('.csv', '.db'))
     delete_file(self._output_filename.replace('.csv', '-doc.text'))
     delete_file(self._output_filename.replace('.csv', '-tab.text'))
     delete_file(self._output_filename.replace('.csv', '-atr.html'))
     delete_file(self._output_filename.replace('.csv', '-mtr.html'))
     # initialize variables whose values are set in init method
     self.behavior_has_any_response = False
     self.calc = None
     self.calc_clp = None
     self.param_dict = None
Exemplo n.º 4
0
 def __init__(self,
              input_data,
              tax_year,
              baseline,
              reform,
              assump,
              outdir=None):
     # pylint: disable=too-many-arguments,too-many-locals
     # pylint: disable=too-many-branches,too-many-statements
     self.errmsg = ''
     # check name and existence of INPUT file
     inp = 'x'
     self.puf_input_data = False
     self.cps_input_data = False
     if isinstance(input_data, six.string_types):
         # remove any leading directory path from INPUT filename
         fname = os.path.basename(input_data)
         # check if fname ends with ".csv"
         if fname.endswith('.csv'):
             inp = '{}-{}'.format(fname[:-4], str(tax_year)[2:])
         else:
             msg = 'INPUT file name does not end in .csv'
             self.errmsg += 'ERROR: {}\n'.format(msg)
         # check existence of INPUT file
         self.puf_input_data = input_data.endswith('puf.csv')
         self.cps_input_data = input_data.endswith('cps.csv')
         if not self.cps_input_data and not os.path.isfile(input_data):
             msg = 'INPUT file could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     elif isinstance(input_data, pd.DataFrame):
         inp = 'df-{}'.format(str(tax_year)[2:])
     else:
         msg = 'INPUT is neither string nor Pandas DataFrame'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # check name and existence of BASELINE file
     bas = '-x'
     if baseline is None:
         bas = '-#'
     elif isinstance(baseline, six.string_types):
         # remove any leading directory path from BASELINE filename
         fname = os.path.basename(baseline)
         # check if fname ends with ".json"
         if fname.endswith('.json'):
             bas = '-{}'.format(fname[:-5])
         else:
             msg = 'BASELINE file name does not end in .json'
             self.errmsg += 'ERROR: {}\n'.format(msg)
         # check existence of BASELINE file
         if not os.path.isfile(baseline):
             msg = 'BASELINE file could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     else:
         msg = 'TaxCalcIO.ctor: baseline is neither None nor str'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # check name(s) and existence of REFORM file(s)
     ref = '-x'
     if reform is None:
         self.specified_reform = False
         ref = '-#'
     elif isinstance(reform, six.string_types):
         self.specified_reform = True
         # split any compound reform into list of simple reforms
         refnames = list()
         reforms = reform.split('+')
         for rfm in reforms:
             # remove any leading directory path from rfm filename
             fname = os.path.basename(rfm)
             # check if fname ends with ".json"
             if not fname.endswith('.json'):
                 msg = '{} does not end in .json'.format(fname)
                 self.errmsg += 'ERROR: REFORM file name {}\n'.format(msg)
             # check existence of REFORM file
             if not os.path.isfile(rfm):
                 msg = '{} could not be found'.format(rfm)
                 self.errmsg += 'ERROR: REFORM file {}\n'.format(msg)
             # add fname to list of refnames used in output file names
             refnames.append(fname)
         # create (possibly compound) reform name for output file names
         ref = '-'
         num_refnames = 0
         for refname in refnames:
             num_refnames += 1
             if num_refnames > 1:
                 ref += '+'
             ref += '{}'.format(refname[:-5])
     else:
         msg = 'TaxCalcIO.ctor: reform is neither None nor str'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # check name and existence of ASSUMP file
     asm = '-x'
     if assump is None:
         asm = '-#'
     elif isinstance(assump, six.string_types):
         # remove any leading directory path from ASSUMP filename
         fname = os.path.basename(assump)
         # check if fname ends with ".json"
         if fname.endswith('.json'):
             asm = '-{}'.format(fname[:-5])
         else:
             msg = 'ASSUMP file name does not end in .json'
             self.errmsg += 'ERROR: {}\n'.format(msg)
         # check existence of ASSUMP file
         if not os.path.isfile(assump):
             msg = 'ASSUMP file could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     else:
         msg = 'TaxCalcIO.ctor: assump is neither None nor str'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # check name and existence of OUTDIR
     if outdir is None:
         valid_outdir = True
     elif isinstance(outdir, six.string_types):
         # check existence of OUTDIR
         if os.path.isdir(outdir):
             valid_outdir = True
         else:
             valid_outdir = False
             msg = 'OUTDIR could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     else:
         valid_outdir = False
         msg = 'TaxCalcIO.ctor: outdir is neither None nor str'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # create OUTPUT file name and delete any existing output files
     output_filename = '{}{}{}{}.csv'.format(inp, bas, ref, asm)
     if outdir is None:
         self._output_filename = output_filename
         delete_old_files = True
     elif valid_outdir:
         self._output_filename = os.path.join(outdir, output_filename)
         delete_old_files = True
     else:
         delete_old_files = False
     if delete_old_files:
         delete_file(self._output_filename)
         delete_file(self._output_filename.replace('.csv', '.db'))
         delete_file(self._output_filename.replace('.csv', '-doc.text'))
         delete_file(self._output_filename.replace('.csv', '-tab.text'))
         delete_file(self._output_filename.replace('.csv', '-atr.html'))
         delete_file(self._output_filename.replace('.csv', '-mtr.html'))
         delete_file(self._output_filename.replace('.csv', '-pch.html'))
     # initialize variables whose values are set in init method
     self.behavior_has_any_response = False
     self.calc = None
     self.calc_base = None
     self.growmodel = None
     self.param_dict = None
     self.policy_dicts = list()
Exemplo n.º 5
0
 def __init__(self, input_data, tax_year, baseline, reform, assump,
              outdir=None):
     # pylint: disable=too-many-arguments,too-many-locals
     # pylint: disable=too-many-branches,too-many-statements
     self.errmsg = ''
     # check name and existence of INPUT file
     inp = 'x'
     self.puf_input_data = False
     self.cps_input_data = False
     if isinstance(input_data, str):
         # remove any leading directory path from INPUT filename
         fname = os.path.basename(input_data)
         # check if fname ends with ".csv"
         if fname.endswith('.csv'):
             inp = '{}-{}'.format(fname[:-4], str(tax_year)[2:])
         else:
             msg = 'INPUT file name does not end in .csv'
             self.errmsg += 'ERROR: {}\n'.format(msg)
         # check existence of INPUT file
         self.puf_input_data = input_data.endswith('puf.csv')
         self.cps_input_data = input_data.endswith('cps.csv')
         if not self.cps_input_data and not os.path.isfile(input_data):
             msg = 'INPUT file could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     elif isinstance(input_data, pd.DataFrame):
         inp = 'df-{}'.format(str(tax_year)[2:])
     else:
         msg = 'INPUT is neither string nor Pandas DataFrame'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # check name and existence of BASELINE file
     bas = '-x'
     if baseline is None:
         bas = '-#'
     elif isinstance(baseline, str):
         # remove any leading directory path from BASELINE filename
         fname = os.path.basename(baseline)
         # check if fname ends with ".json"
         if fname.endswith('.json'):
             bas = '-{}'.format(fname[:-5])
         else:
             msg = 'BASELINE file name does not end in .json'
             self.errmsg += 'ERROR: {}\n'.format(msg)
         # check existence of BASELINE file
         if not os.path.isfile(baseline):
             msg = 'BASELINE file could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     else:
         msg = 'TaxCalcIO.ctor: baseline is neither None nor str'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # check name(s) and existence of REFORM file(s)
     ref = '-x'
     if reform is None:
         self.specified_reform = False
         ref = '-#'
     elif isinstance(reform, str):
         self.specified_reform = True
         # split any compound reform into list of simple reforms
         refnames = list()
         reforms = reform.split('+')
         for rfm in reforms:
             # remove any leading directory path from rfm filename
             fname = os.path.basename(rfm)
             # check if fname ends with ".json"
             if not fname.endswith('.json'):
                 msg = '{} does not end in .json'.format(fname)
                 self.errmsg += 'ERROR: REFORM file name {}\n'.format(msg)
             # check existence of REFORM file
             if not os.path.isfile(rfm):
                 msg = '{} could not be found'.format(rfm)
                 self.errmsg += 'ERROR: REFORM file {}\n'.format(msg)
             # add fname to list of refnames used in output file names
             refnames.append(fname)
         # create (possibly compound) reform name for output file names
         ref = '-'
         num_refnames = 0
         for refname in refnames:
             num_refnames += 1
             if num_refnames > 1:
                 ref += '+'
             ref += '{}'.format(refname[:-5])
     else:
         msg = 'TaxCalcIO.ctor: reform is neither None nor str'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # check name and existence of ASSUMP file
     asm = '-x'
     if assump is None:
         asm = '-#'
     elif isinstance(assump, str):
         # remove any leading directory path from ASSUMP filename
         fname = os.path.basename(assump)
         # check if fname ends with ".json"
         if fname.endswith('.json'):
             asm = '-{}'.format(fname[:-5])
         else:
             msg = 'ASSUMP file name does not end in .json'
             self.errmsg += 'ERROR: {}\n'.format(msg)
         # check existence of ASSUMP file
         if not os.path.isfile(assump):
             msg = 'ASSUMP file could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     else:
         msg = 'TaxCalcIO.ctor: assump is neither None nor str'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # check name and existence of OUTDIR
     if outdir is None:
         valid_outdir = True
     elif isinstance(outdir, str):
         # check existence of OUTDIR
         if os.path.isdir(outdir):
             valid_outdir = True
         else:
             valid_outdir = False
             msg = 'OUTDIR could not be found'
             self.errmsg += 'ERROR: {}\n'.format(msg)
     else:
         valid_outdir = False
         msg = 'TaxCalcIO.ctor: outdir is neither None nor str'
         self.errmsg += 'ERROR: {}\n'.format(msg)
     # create OUTPUT file name and delete any existing output files
     output_filename = '{}{}{}{}.csv'.format(inp, bas, ref, asm)
     if outdir is None:
         self._output_filename = output_filename
         delete_old_files = True
     elif valid_outdir:
         self._output_filename = os.path.join(outdir, output_filename)
         delete_old_files = True
     else:
         delete_old_files = False
     if delete_old_files:
         delete_file(self._output_filename)
         delete_file(self._output_filename.replace('.csv', '.db'))
         delete_file(self._output_filename.replace('.csv', '-doc.text'))
         delete_file(self._output_filename.replace('.csv', '-tab.text'))
         delete_file(self._output_filename.replace('.csv', '-atr.html'))
         delete_file(self._output_filename.replace('.csv', '-mtr.html'))
         delete_file(self._output_filename.replace('.csv', '-pch.html'))
     # initialize variables whose values are set in init method
     self.calc = None
     self.calc_base = None
     self.param_dict = None
     self.policy_dicts = list()
Exemplo n.º 6
0
 def __init__(
         self,
         input_data,
         tax_year,
         reform,
         assump,
         growdiff_response,  # =None in static analysis
         aging_input_data,
         exact_calculations):
     """
     TaxCalcIO class constructor.
     """
     # pylint: disable=too-many-arguments
     # pylint: disable=too-many-locals
     # pylint: disable=too-many-branches
     # pylint: disable=too-many-statements
     # check for existence of INPUT file
     if isinstance(input_data, six.string_types):
         # remove any leading directory path from INPUT filename
         fname = os.path.basename(input_data)
         # check if fname ends with ".csv"
         if fname.endswith('.csv'):
             inp = '{}-{}'.format(fname[:-4], str(tax_year)[2:])
         else:
             msg = 'INPUT file named {} does not end in .csv'
             raise ValueError(msg.format(fname))
         # check existence of INPUT file
         if not os.path.isfile(input_data):
             msg = 'INPUT file named {} could not be found'
             raise ValueError(msg.format(input_data))
     elif isinstance(input_data, pd.DataFrame):
         inp = 'df-{}'.format(str(tax_year)[2:])
     else:
         msg = 'INPUT is neither string nor Pandas DataFrame'
         raise ValueError(msg)
     # construct output_filename and delete old output file if it exists
     if reform is None:
         self._reform = False
         ref = ''
     elif isinstance(reform, six.string_types):
         self._reform = True
         # remove any leading directory path from REFORM filename
         fname = os.path.basename(reform)
         # check if fname ends with ".json"
         if fname.endswith('.json'):
             ref = '-{}'.format(fname[:-5])
         else:
             msg = 'REFORM file named {} does not end in .json'
             raise ValueError(msg.format(fname))
     else:
         msg = 'TaxCalcIO.ctor reform is neither None nor str'
         raise ValueError(msg)
     if assump is None:
         asm = ''
     elif isinstance(assump, six.string_types):
         # remove any leading directory path from ASSUMP filename
         fname = os.path.basename(assump)
         # check if fname ends with ".json"
         if fname.endswith('.json'):
             asm = '-{}'.format(fname[:-5])
         else:
             msg = 'ASSUMP file named {} does not end in .json'
             raise ValueError(msg.format(fname))
     else:
         msg = 'TaxCalcIO.ctor assump is neither None nor str'
         raise ValueError(msg)
     self._output_filename = '{}{}{}.csv'.format(inp, ref, asm)
     delete_file(self._output_filename)
     # get parameter dictionaries from --reform and --assump files
     param_dict = Calculator.read_json_param_files(reform, assump)
     # make sure no behavioral response is specified in --assump
     beh = Behavior()
     beh.update_behavior(param_dict['behavior'])
     if beh.has_any_response():
         msg = '--assump ASSUMP cannot assume any "behavior"'
         raise ValueError(msg)
     # make sure no growdiff_response is specified in --assump
     gdiff_response = Growdiff()
     gdiff_response.update_growdiff(param_dict['growdiff_response'])
     if gdiff_response.has_any_response():
         msg = '--assump ASSUMP cannot assume any "growdiff_response"'
         raise ValueError(msg)
     # create gdiff_baseline object
     gdiff_baseline = Growdiff()
     gdiff_baseline.update_growdiff(param_dict['growdiff_baseline'])
     # create Growfactors clp object that incorporates gdiff_baseline
     gfactors_clp = Growfactors()
     gdiff_baseline.apply_to(gfactors_clp)
     # specify gdiff_response object
     if growdiff_response is None:
         gdiff_response = Growdiff()
     elif isinstance(growdiff_response, Growdiff):
         gdiff_response = growdiff_response
     else:
         msg = 'TaxCalcIO.ctor growdiff_response is neither None nor {}'
         raise ValueError(msg.format('a Growdiff object'))
     # create Growfactors ref object that has both gdiff objects applied
     gfactors_ref = Growfactors()
     gdiff_baseline.apply_to(gfactors_ref)
     gdiff_response.apply_to(gfactors_ref)
     # create Policy object and implement reform if specified
     if self._reform:
         pol = Policy(gfactors=gfactors_ref)
         pol.implement_reform(param_dict['policy'])
         clp = Policy(gfactors=gfactors_clp)
     else:
         pol = Policy(gfactors=gfactors_clp)
     # check for valid tax_year value
     if tax_year < pol.start_year:
         msg = 'tax_year {} less than policy.start_year {}'
         raise ValueError(msg.format(tax_year, pol.start_year))
     if tax_year > pol.end_year:
         msg = 'tax_year {} greater than policy.end_year {}'
         raise ValueError(msg.format(tax_year, pol.end_year))
     # set policy to tax_year
     pol.set_year(tax_year)
     if self._reform:
         clp.set_year(tax_year)
     # read input file contents into Records object(s)
     if aging_input_data:
         if self._reform:
             recs = Records(data=input_data,
                            gfactors=gfactors_ref,
                            exact_calculations=exact_calculations)
             recs_clp = Records(data=input_data,
                                gfactors=gfactors_clp,
                                exact_calculations=exact_calculations)
         else:
             recs = Records(data=input_data,
                            gfactors=gfactors_clp,
                            exact_calculations=exact_calculations)
     else:  # input_data are raw data that are not being aged
         recs = Records(data=input_data,
                        exact_calculations=exact_calculations,
                        gfactors=None,
                        adjust_ratios=None,
                        weights=None,
                        start_year=tax_year)
         if self._reform:
             recs_clp = copy.deepcopy(recs)
     # create Calculator object(s)
     con = Consumption()
     con.update_consumption(param_dict['consumption'])
     self._calc = Calculator(policy=pol,
                             records=recs,
                             verbose=True,
                             consumption=con,
                             sync_years=aging_input_data)
     if self._reform:
         self._calc_clp = Calculator(policy=clp,
                                     records=recs_clp,
                                     verbose=False,
                                     consumption=con,
                                     sync_years=aging_input_data)