예제 #1
0
def test_json_serialization(caplog):
    caplog.set_level(logging.DEBUG)

    tmp_file_path = os.path.join(TEST_DATA_DIR,
                                 "test_round_trip_serialization.json.local")
    test_obj = DummyClass.from_details(field1=21,
                                       field2={"2.1": DummyClass2(field1=1)})
    test_obj.dump_to_file(filename=tmp_file_path)
    test_obj_2 = JsonObject.read_from_file(
        filename=tmp_file_path,
        name_to_json_class_index_extra=json_class_index)
    assert test_obj.field1 == test_obj_2.field1
    assert test_obj.__str__() == test_obj_2.__str__()
    assert isinstance(test_obj_2.field2["2.1"], DummyClass2)

    tmp_file_path = os.path.join(TEST_DATA_DIR,
                                 "test_round_trip_serialization.toml.local")
    test_obj = DummyClass.from_details(field1=21,
                                       field2={"2.1": DummyClass2(field1=1)})
    test_obj.dump_to_file(filename=tmp_file_path)
    test_obj_2 = JsonObject.read_from_file(
        filename=tmp_file_path,
        name_to_json_class_index_extra=json_class_index)
    assert test_obj.field1 == test_obj_2.field1
    assert test_obj.__str__() == test_obj_2.__str__()
예제 #2
0
def get_panchangam(city, year, script, computeLagnams=False):
    fname_det = os.path.expanduser('~/Documents/%s-%s-detailed.json' %
                                   (city.name, year))
    fname = os.path.expanduser('~/Documents/%s-%s.json' % (city.name, year))

    if os.path.isfile(fname) and not computeLagnams:
        sys.stderr.write('Loaded pre-computed panchangam from %s.\n' % fname)
        return JsonObject.read_from_file(filename=fname)
    elif os.path.isfile(fname_det):
        # Load pickle, do not compute!
        sys.stderr.write('Loaded pre-computed panchangam from %s.\n' % fname)
        return JsonObject.read_from_file(filename=fname_det)
    else:
        sys.stderr.write(
            'No precomputed data available. Computing panchangam... ')
        sys.stderr.flush()
        panchangam = Panchangam(city=city, year=year, script=script)
        panchangam.compute_angams(computeLagnams=computeLagnams)
        panchangam.assignLunarMonths()
        sys.stderr.write('done.\n')
        sys.stderr.write('Writing computed panchangam to %s...' % fname)
        try:
            if computeLagnams:
                panchangam.dump_to_file(filename=fname_det)
            else:
                panchangam.dump_to_file(filename=fname)
        except EnvironmentError:
            logging.warning("Not able to save.")
            logging.error(traceback.format_exc())
        return panchangam
예제 #3
0
def test_return_none_by_default(caplog):
    caplog.set_level(logging.DEBUG)
    tmp_file_path = os.path.join(TEST_DATA_DIR, "test_none_value.json")
    with pytest.raises(AttributeError):
        test_obj_2 = JsonObject.read_from_file(
            filename=tmp_file_path,
            name_to_json_class_index_extra=json_class_index,
            _default_to_none=False)
        assert test_obj_2.field2 == None
    test_obj_2 = JsonObject.read_from_file(
        filename=tmp_file_path,
        name_to_json_class_index_extra=json_class_index,
        _default_to_none=True)
    assert test_obj_2.field2 == None
예제 #4
0
def get_panchangam(city,
                   year,
                   script,
                   fmt='hh:mm',
                   compute_lagnams=False,
                   precomputed_json_dir="~/Documents",
                   ayanamsha_id=swe.SIDM_TRUE_CITRA,
                   allow_precomputed=True):
    fname_det = os.path.expanduser('%s/%s-%s-detailed.json' %
                                   (precomputed_json_dir, city.name, year))
    fname = os.path.expanduser('%s/%s-%s.json' %
                               (precomputed_json_dir, city.name, year))

    if os.path.isfile(fname) and not compute_lagnams and allow_precomputed:
        sys.stderr.write('Loaded pre-computed panchangam from %s.\n' % fname)
        return JsonObject.read_from_file(filename=fname)
    elif os.path.isfile(fname_det) and allow_precomputed:
        # Load pickle, do not compute!
        sys.stderr.write('Loaded pre-computed panchangam from %s.\n' % fname)
        return JsonObject.read_from_file(filename=fname_det)
    else:
        sys.stderr.write(
            'No precomputed data available. Computing panchangam...\n')
        panchangam = periodical.Panchangam(city=city,
                                           start_date='%d-01-01' % year,
                                           end_date='%d-12-31' % year,
                                           script=script,
                                           fmt=fmt,
                                           compute_lagnams=compute_lagnams,
                                           ayanamsha_id=ayanamsha_id)
        panchangam.year = year
        sys.stderr.write('Writing computed panchangam to %s...\n' % fname)

        try:
            if compute_lagnams:
                panchangam.dump_to_file(filename=fname_det)
            else:
                panchangam.dump_to_file(filename=fname)
        except EnvironmentError:
            logging.warning("Not able to save.")
            logging.error(traceback.format_exc())
        # Save without festival details
        # Festival data may be updated more frequently and a precomputed panchangam may go out of sync. Hence we keep this method separate.
        panchangam.update_festival_details()
        return panchangam
예제 #5
0
def test_serialization_omit_nones(caplog):
    caplog.set_level(logging.DEBUG)
    tmp_file_path = os.path.join(TEST_DATA_DIR, "test_none_value.json")
    test_obj_2 = JsonObject.read_from_file(
        filename=tmp_file_path,
        name_to_json_class_index_extra=json_class_index,
        _default_to_none=True)
    test_obj_2.field3 = None
    test_obj_2_map = test_obj_2.to_json_map()
    assert "field3" not in test_obj_2_map
예제 #6
0
def test_panchanga_chennai_2019():
    panchangam_2019 = JsonObject.read_from_file(
        filename=os.path.join(TEST_DATA_PATH, 'Chennai-2019.json'))
    panchangam_2019.update_festival_details()
    orig_ics_file = os.path.join(TEST_DATA_PATH, 'Chennai-2019-devanagari.ics')
    current_ics_output = os.path.join(TEST_DATA_PATH,
                                      'Chennai-2019-devanagari.ics.local')
    ics_calendar = compute_calendar(panchangam_2019, all_tags=True)
    write_to_file(ics_calendar, current_ics_output)

    with open(orig_ics_file) as orig_tex:
        with open(current_ics_output) as current_tex:
            assert orig_tex.read() == current_tex.read()
예제 #7
0
def test_panchanga_chennai():
    panchangam_expected_chennai_18 = JsonObject.read_from_file(
        filename=os.path.join(TEST_DATA_PATH, 'Chennai-2018.json'))
    city = City('Chennai', "13:05:24", "80:16:12", "Asia/Calcutta")
    panchangam = jyotisha.panchangam.spatio_temporal.annual.Panchangam(
        city=city,
        year=2018,
        script=sanscript.DEVANAGARI,
        ayanamsha_id=swe.SIDM_LAHIRI,
        compute_lagnams=False)
    if str(panchangam) != str(panchangam_expected_chennai_18):
        panchangam.dump_to_file(filename=os.path.join(
            TEST_DATA_PATH, 'Chennai-2018-actual.json.local'))
    assert str(panchangam) == str(panchangam_expected_chennai_18)
예제 #8
0
def test_panchanga_chennai_18():
    panchangam_expected_chennai_18 = JsonObject.read_from_file(
        filename=os.path.join(TEST_DATA_PATH, 'Chennai-2018.json'))
    city = City('Chennai', "13:05:24", "80:16:12", "Asia/Calcutta")
    panchangam = annual.Panchangam(city=city,
                                   year=2018,
                                   script=sanscript.DEVANAGARI,
                                   ayanamsha_id=swe.SIDM_LAHIRI,
                                   compute_lagnams=False)
    if panchangam.__str__(floating_point_precision=4
                          ) != panchangam_expected_chennai_18.__str__(
                              floating_point_precision=4):
        panchangam.dump_to_file(filename=os.path.join(
            TEST_DATA_PATH, 'Chennai-2018-actual.json.local'),
                                floating_point_precision=4)
        panchangam_expected_chennai_18.dump_to_file(filename=os.path.join(
            TEST_DATA_PATH, 'Chennai-2018-expected.json.local'),
                                                    floating_point_precision=4)
    assert str(panchangam) == str(panchangam_expected_chennai_18)
예제 #9
0
def test_panchanga_chennai_2018():
    panchangam_2018 = JsonObject.read_from_file(
        filename=os.path.join(TEST_DATA_PATH, 'Chennai-2018.json'))
    panchangam_2018.update_festival_details()
    orig_tex_file = os.path.join(TEST_DATA_PATH,
                                 'daily-cal-2018-Chennai-deva.tex')
    daily_template_file = open(
        os.path.join(
            CODE_ROOT,
            'jyotisha/panchangam/data/templates/daily_cal_template.tex'))
    current_tex_output = os.path.join(TEST_DATA_PATH,
                                      'daily-cal-2018-Chennai-deva.tex.local')
    writeDailyTeX(panchangam_2018,
                  daily_template_file,
                  compute_lagnams=False,
                  output_stream=open(current_tex_output, 'w'))

    with open(orig_tex_file) as orig_tex:
        with open(current_tex_output) as current_tex:
            assert orig_tex.read() == current_tex.read()
예제 #10
0
def test_panchanga_orinda():
    panchangam_expected_orinda_19 = JsonObject.read_from_file(
        filename=os.path.join(TEST_DATA_PATH, 'Orinda-2019.json'))
    city = City('Orinda', '37:51:38', '-122:10:59', 'America/Los_Angeles')
    panchangam = annual.Panchangam(city=city,
                                   year=2019,
                                   script=sanscript.DEVANAGARI,
                                   ayanamsha_id=swe.SIDM_LAHIRI,
                                   compute_lagnams=False)

    if panchangam.__str__(floating_point_precision=4
                          ) != panchangam_expected_orinda_19.__str__(
                              floating_point_precision=4):
        panchangam.dump_to_file(filename=os.path.join(
            TEST_DATA_PATH, 'Orinda-2019-actual.json.local'),
                                floating_point_precision=4)
        panchangam_expected_orinda_19.dump_to_file(filename=os.path.join(
            TEST_DATA_PATH, 'Orinda-2019-expected.json.local'),
                                                   floating_point_precision=4)
    assert panchangam.__str__(
        floating_point_precision=4) == panchangam_expected_orinda_19.__str__(
            floating_point_precision=4)
예제 #11
0
def json_compare(actual_object, expected_content_path):
    if not os.path.exists(expected_content_path):
        logging.warning("File must have been deliberately deleted as obsolete. So, will dump a new file for future tests.")
        actual_object.dump_to_file(filename=expected_content_path,
                                floating_point_precision=4)
        return 

    expected = JsonObject.read_from_file(filename=expected_content_path)
    try:
        # The below would be actually slower (1min+), and leads to bug output dump in case of failure.
        # assert str_actual == str_expected 
        # The below is better, but still slower (35s and leads to bug output dump in case of failure.
        # assert actual == expected

        # The below is faster - 20s and produces concise difference.
        collection_helper.assert_approx_equals(x=actual_object, y=expected, floating_point_precision=4)
    except:
        # firefox does not identify files not ending with .json as json. Hence not naming .json.local.
        actual_content_path = expected_content_path.replace(".json", "_actual.local.json")
        actual_object.dump_to_file(filename=actual_content_path, floating_point_precision=4)
        traceback.print_exc()
        raise
예제 #12
0
def test_panchanga_chennai_19():
    panchangam_expected_chennai_19 = JsonObject.read_from_file(
        filename=os.path.join(TEST_DATA_PATH, 'Chennai-2019.json'))
    panchangam_expected_chennai_19.update_festival_details()
    city = City('Chennai', "13:05:24", "80:16:12", "Asia/Calcutta")
    panchangam = annual.get_panchangam(city=city,
                                       year=2019,
                                       script=sanscript.DEVANAGARI,
                                       ayanamsha_id=swe.SIDM_TRUE_CITRA,
                                       compute_lagnams=False,
                                       allow_precomputed=False)

    if panchangam.__str__(floating_point_precision=4
                          ) != panchangam_expected_chennai_19.__str__(
                              floating_point_precision=4):
        panchangam.dump_to_file(filename=os.path.join(
            TEST_DATA_PATH, 'Chennai-2019-actual.json.local'),
                                floating_point_precision=4)
        panchangam_expected_chennai_19.dump_to_file(filename=os.path.join(
            TEST_DATA_PATH, 'Chennai-2019-expected.json.local'),
                                                    floating_point_precision=4)
    assert panchangam.__str__(
        floating_point_precision=4) == panchangam_expected_chennai_19.__str__(
            floating_point_precision=4)