Skip to content

add more attributes to __repr__ methods #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/sphinx/source/whatsnew/v0.4.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Enhancements
* Added calculate_deltat method to the spa module to calculate the
time difference between terrestrial time and UT1. Specifying a scalar
is sufficient for most calculations. (:issue:`165`)
* Added more attributes to ModelChain, PVSystem, and Location printed
representations. (:issue:`254`)
* Added name attribute to ModelChain and PVSystem. (:issue:`254`)

Code Contributors
~~~~~~~~~~~~~~~~~
Expand Down
6 changes: 3 additions & 3 deletions pvlib/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def __init__(self, latitude, longitude, tz='UTC', altitude=0,
# super(Location, self).__init__(**kwargs)

def __repr__(self):
return ('{}: latitude={}, longitude={}, tz={}, altitude={}'
.format(self.name, self.latitude, self.longitude,
self.tz, self.altitude))
attrs = ['name', 'latitude', 'longitude', 'altitude', 'tz']
return ('Location: \n ' + '\n '.join(
(attr + ': ' + str(getattr(self, attr)) for attr in attrs)))

@classmethod
def from_tmy(cls, tmy_metadata, tmy_data=None, **kwargs):
Expand Down
26 changes: 20 additions & 6 deletions pvlib/modelchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,10 @@ def __init__(self, system, location,
dc_model=None, ac_model=None, aoi_model=None,
spectral_model=None, temp_model='sapm',
losses_model='no_loss',
name=None,
**kwargs):

self.name = name
self.system = system
self.location = location
self.clearsky_model = clearsky_model
Expand All @@ -322,12 +324,24 @@ def __init__(self, system, location,
self.solar_position = None

def __repr__(self):
return ('ModelChain for: ' + str(self.system) +
' orientation_strategy: ' + str(self.orientation_strategy) +
' clearsky_model: ' + str(self.clearsky_model) +
' transposition_model: ' + str(self.transposition_model) +
' solar_position_method: ' + str(self.solar_position_method) +
' airmass_model: ' + str(self.airmass_model))
attrs = [
'name', 'orientation_strategy', 'clearsky_model',
'transposition_model', 'solar_position_method',
'airmass_model', 'dc_model', 'ac_model', 'aoi_model',
'spectral_model', 'temp_model', 'losses_model'
]

def getmcattr(self, attr):
"""needed to avoid recursion in property lookups"""
out = getattr(self, attr)
try:
out = out.__name__
except AttributeError:
pass
return out

return ('ModelChain: \n ' + '\n '.join(
(attr + ': ' + getmcattr(self, attr) for attr in attrs)))

@property
def orientation_strategy(self):
Expand Down
23 changes: 13 additions & 10 deletions pvlib/pvsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ def __init__(self,
modules_per_string=1, strings_per_inverter=1,
inverter=None, inverter_parameters=None,
racking_model='open_rack_cell_glassback',
name=None,
**kwargs):

self.name = name

self.surface_tilt = surface_tilt
self.surface_azimuth = surface_azimuth

Expand All @@ -144,10 +147,10 @@ def __init__(self,
super(PVSystem, self).__init__(**kwargs)

def __repr__(self):
return ('PVSystem with tilt:' + str(self.surface_tilt) +
' and azimuth: ' + str(self.surface_azimuth) +
' with Module: ' + str(self.module) +
' and Inverter: ' + str(self.inverter))
attrs = ['name', 'surface_tilt', 'surface_azimuth', 'module',
'inverter', 'albedo', 'racking_model']
return ('PVSystem: \n ' + '\n '.join(
(attr + ': ' + str(getattr(self, attr)) for attr in attrs)))

def get_aoi(self, solar_zenith, solar_azimuth):
"""Get the angle of incidence on the system.
Expand Down Expand Up @@ -575,12 +578,12 @@ def __init__(self, pvsystem=None, location=None, **kwargs):
super(LocalizedPVSystem, self).__init__(**new_kwargs)

def __repr__(self):
return ('LocalizedPVSystem with tilt:' + str(self.surface_tilt) +
' and azimuth: ' + str(self.surface_azimuth) +
' with Module: ' + str(self.module) +
' and Inverter: ' + str(self.inverter) +
' at Latitude: ' + str(self.latitude) +
' and Longitude: ' + str(self.longitude))
attrs = [
'name', 'latitude', 'longitude', 'altitude', 'tz', 'surface_tilt',
'surface_azimuth', 'module', 'inverter', 'albedo', 'racking_model'
]
return ('LocalizedPVSystem: \n ' + '\n '.join(
(attr + ': ' + str(getattr(self, attr)) for attr in attrs)))


def systemdef(meta, surface_tilt, surface_azimuth, albedo, modules_per_string,
Expand Down
9 changes: 5 additions & 4 deletions pvlib/test/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def test_location_invalid_tz_type():

def test_location_print_all():
tus = Location(32.2, -111, 'US/Arizona', 700, 'Tucson')
expected_str = 'Tucson: latitude=32.2, longitude=-111, tz=US/Arizona, altitude=700'
expected_str = 'Location: \n name: Tucson\n latitude: 32.2\n longitude: -111\n altitude: 700\n tz: US/Arizona'
assert tus.__str__() == expected_str

def test_location_print_pytz():
tus = Location(32.2, -111, aztz, 700, 'Tucson')
expected_str = 'Tucson: latitude=32.2, longitude=-111, tz=US/Arizona, altitude=700'
expected_str = 'Location: \n name: Tucson\n latitude: 32.2\n longitude: -111\n altitude: 700\n tz: US/Arizona'
assert tus.__str__() == expected_str


Expand Down Expand Up @@ -283,5 +283,6 @@ def test_get_airmass_valueerror():

def test_Location___repr__():
tus = Location(32.2, -111, 'US/Arizona', 700, 'Tucson')
assert tus.__repr__()==('Tucson: latitude=32.2, longitude=-111, '+
'tz=US/Arizona, altitude=700')

expected = 'Location: \n name: Tucson\n latitude: 32.2\n longitude: -111\n altitude: 700\n tz: US/Arizona'
assert tus.__repr__() == expected
11 changes: 5 additions & 6 deletions pvlib/test/test_modelchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,12 @@ def test_ModelChain___repr__(system, location):

strategy = 'south_at_latitude_tilt'

mc = ModelChain(system, location, orientation_strategy=strategy)
mc = ModelChain(system, location, orientation_strategy=strategy,
name='my mc')

expected = 'ModelChain: \n name: my mc\n orientation_strategy: south_at_latitude_tilt\n clearsky_model: ineichen\n transposition_model: haydavies\n solar_position_method: nrel_numpy\n airmass_model: kastenyoung1989\n dc_model: sapm\n ac_model: snlinverter\n aoi_model: sapm_aoi_loss\n spectral_model: sapm_spectral_loss\n temp_model: sapm_temp\n losses_model: no_extra_losses'

assert mc.__repr__() == ('ModelChain for: PVSystem with tilt:32.2 and '+
'azimuth: 180 with Module: None and Inverter: None '+
'orientation_strategy: south_at_latitude_tilt clearsky_model: '+
'ineichen transposition_model: haydavies solar_position_method: '+
'nrel_numpy airmass_model: kastenyoung1989')
assert mc.__repr__() == expected


@requires_scipy
Expand Down
25 changes: 14 additions & 11 deletions pvlib/test/test_pvsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,19 +672,21 @@ def test_PVSystem_localize_with_latlon():


def test_PVSystem___repr__():
system = pvsystem.PVSystem(module='blah', inverter='blarg')
system = pvsystem.PVSystem(module='blah', inverter='blarg', name='pv ftw')

expected = 'PVSystem: \n name: pv ftw\n surface_tilt: 0\n surface_azimuth: 180\n module: blah\n inverter: blarg\n albedo: 0.25\n racking_model: open_rack_cell_glassback'

assert system.__repr__()==('PVSystem with tilt:0 and azimuth:'+
' 180 with Module: blah and Inverter: blarg')
assert system.__repr__() == expected


def test_PVSystem_localize___repr__():
system = pvsystem.PVSystem(module='blah', inverter='blarg')
system = pvsystem.PVSystem(module='blah', inverter='blarg', name='pv ftw')
localized_system = system.localize(latitude=32, longitude=-111)

assert localized_system.__repr__()==('LocalizedPVSystem with tilt:0 and'+
' azimuth: 180 with Module: blah and Inverter: blarg at '+
'Latitude: 32 and Longitude: -111')
expected = 'LocalizedPVSystem: \n name: None\n latitude: 32\n longitude: -111\n altitude: 0\n tz: UTC\n surface_tilt: 0\n surface_azimuth: 180\n module: blah\n inverter: blarg\n albedo: 0.25\n racking_model: open_rack_cell_glassback'

assert localized_system.__repr__() == expected


# we could retest each of the models tested above
# when they are attached to LocalizedPVSystem, but
Expand All @@ -707,11 +709,12 @@ def test_LocalizedPVSystem___repr__():
localized_system = pvsystem.LocalizedPVSystem(latitude=32,
longitude=-111,
module='blah',
inverter='blarg')
inverter='blarg',
name='my name')

expected = 'LocalizedPVSystem: \n name: None\n latitude: 32\n longitude: -111\n altitude: 0\n tz: UTC\n surface_tilt: 0\n surface_azimuth: 180\n module: blah\n inverter: blarg\n albedo: 0.25\n racking_model: open_rack_cell_glassback'

assert localized_system.__repr__()==('LocalizedPVSystem with tilt:0 and'+
' azimuth: 180 with Module: blah and Inverter: blarg at Latitude: 32 ' +
'and Longitude: -111')
assert localized_system.__repr__() == expected


def test_pvwatts_dc_scalars():
Expand Down
13 changes: 6 additions & 7 deletions pvlib/test/test_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,18 @@ def test_get_irradiance():
def test_SingleAxisTracker___repr__():
system = tracking.SingleAxisTracker(max_angle=45, gcr=.25,
module='blah', inverter='blarg')

assert system.__repr__() == 'SingleAxisTracker with max_angle: 45'
expected = 'SingleAxisTracker: \n axis_tilt: 0\n axis_azimuth: 0\n max_angle: 45\n backtrack: True\n gcr: 0.25\n name: None\n surface_tilt: 0\n surface_azimuth: 180\n module: blah\n inverter: blarg\n albedo: 0.25\n racking_model: open_rack_cell_glassback'
assert system.__repr__() == expected


def test_LocalizedSingleAxisTracker___repr__():
localized_system = tracking.LocalizedSingleAxisTracker(latitude=32,
longitude=-111,
module='blah',
inverter='blarg')
inverter='blarg',
gcr=0.25)

expected = 'LocalizedSingleAxisTracker: \n axis_tilt: 0\n axis_azimuth: 0\n max_angle: 90\n backtrack: True\n gcr: 0.25\n name: None\n surface_tilt: 0\n surface_azimuth: 180\n module: blah\n inverter: blarg\n albedo: 0.25\n racking_model: open_rack_cell_glassback\n latitude: 32\n longitude: -111\n altitude: 0\n tz: UTC'

assert localized_system.__repr__() == ('LocalizedSingleAxisTracker with '+
'max_angle: 90 at Location: None: '+
'latitude=32, longitude=-111, ' +
'tz=UTC, altitude=0')
assert localized_system.__repr__() == expected

20 changes: 13 additions & 7 deletions pvlib/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ def __init__(self, axis_tilt=0, axis_azimuth=0,
super(SingleAxisTracker, self).__init__(**kwargs)

def __repr__(self):
return ('SingleAxisTracker with max_angle: ' +
str(self.max_angle))
attrs = ['axis_tilt', 'axis_azimuth', 'max_angle', 'backtrack', 'gcr']
sat_repr = ('SingleAxisTracker: \n ' + '\n '.join(
(attr + ': ' + str(getattr(self, attr)) for attr in attrs)))
# get the parent PVSystem info
pvsystem_repr = super(SingleAxisTracker, self).__repr__()
# remove the first line (contains 'PVSystem: \n')
pvsystem_repr = '\n'.join(pvsystem_repr.split('\n')[1:])
return sat_repr + '\n' + pvsystem_repr


def singleaxis(self, apparent_zenith, apparent_azimuth):
tracking_data = singleaxis(apparent_zenith, apparent_azimuth,
Expand Down Expand Up @@ -158,12 +165,11 @@ def __init__(self, pvsystem=None, location=None, **kwargs):
super(LocalizedSingleAxisTracker, self).__init__(**new_kwargs)

def __repr__(self):
attrs = ['latitude', 'longitude', 'altitude', 'tz']
return ('Localized' +
super(LocalizedSingleAxisTracker, self).__repr__() +
' at Location: ' +
('{}: latitude={}, longitude={}, tz={}, altitude={}'
.format(self.name, self.latitude, self.longitude,
self.tz, self.altitude)))
super(LocalizedSingleAxisTracker, self).__repr__() + '\n ' +
'\n '.join(
(attr + ': ' + str(getattr(self, attr)) for attr in attrs)))


def singleaxis(apparent_zenith, apparent_azimuth,
Expand Down