code critique requested - just 60 lines



Hi, I would like some feedback on how you would improve the following
program:
http://www.bitbucket.org/metaperl/ptc_math/src/21979c65074f/payout.py

Basically, using non-strict dictionary keys can lead to bugs, so that
worried me. Also, I'm not sure that my code is as crisp and concise as
it could be. I also did not like the long string representation in the
Scenerio class. It is hard to read and error-prone to code.

Any feedback on how you would've written this differently is welcome,
either by commenting below, or by checking out the repo and checking
it back in!

class Rates:

def __init__(self, per_click, per_ref_click):
self.per_click = per_click
self.per_ref_click = per_ref_click

def __str__(self):
return 'per_click: %.2f per_ref_click: %.2f' %
(self.per_click, self.per_ref_click)


ad_rate = 200 # 2 dollars for 100 clicks

# http://code.activestate.com/recipes/278259/
def sumDict(d):
return reduce(lambda x,y:x+y, d.values())


rates = {}
rates['std'] = Rates(per_click=1, per_ref_click=0.5)
rates['vip'] = Rates(per_click=1.25, per_ref_click=1.25)

class Scenario:



def __init__(self, std_clicks, vip_clicks, upline_status):
self.clicks = {}
self.payout = {}
self.ad_rate = 200

self.clicks['std'] = std_clicks
self.clicks['vip'] = vip_clicks
self.upline_status = upline_status

def calc_profit(self):
for member_type in rates:
self.payout[member_type] = self.clicks[member_type] *
rates[member_type].per_click
if self.upline_status is None:
self.payout['upline'] = 0
else:
self.payout['upline'] = sumDict(self.clicks) *
rates[upline_status].per_ref_click
#print "rates: %s self.clicks: %d upline payout: %.1f\n" %
(rates[upline_status], sumDict(self.clicks), self.payout['upline'])
return ad_rate - sumDict(self.payout)


def __str__(self):
profit = self.calc_profit()
return 'upline_status: %s upline_payout: %.1f\n\tstd_clicks:
%d std_payout %.1f vip_clicks: %d vip_payout: %.1f\n\t\tProfit: %.1f
\n' % (self.upline_status, self.payout['upline'], self.clicks['std'],
self.payout['std'], self.clicks['vip'], self.payout['vip'], profit)



scenario = []

for upline_status in [None, 'std', 'vip']:
for vip_clicks in [0, 25, 50, 75, 100]:
std_clicks = 100 - vip_clicks
scenario.append(Scenario(std_clicks, vip_clicks,
upline_status))

# really, we could've printed the objects as they were made, but for
debugging, I kept creation and
# printing as separate steps
for s in scenario:
print s
.



Relevant Pages

  • Re: [patch] CFS (Completely Fair Scheduler), v2
    ... i'd like to thank everyone for the tremendous amount of feedback and ... mostly concentrated on bugs, regressions and debuggability. ... as usual, any sort of feedback, bugreports, fixes and suggestions are ... and the scheduling is whats funkity. ...
    (Linux-Kernel)
  • Re: Bug fixes in Office 2008 12.1.2
    ... I guess the fellow knew of these bugs, sent them to feedback *expected* them to be fixed. ... Display of recent fonts in font menus not surviving an application ... Focus randomly passing from Find & Replace window to document window: ...
    (microsoft.public.mac.office.word)
  • Re: Just random fixes with QC reports...
    ... >> people with a professional attitude. ... The signal to noise ratio in this group is bad ... Borland must handle this kind of feedback when their priority for new ... way to many bugs today, and this feedback will continue until they are on ...
    (borland.public.delphi.non-technical)
  • Re: 12.2.0 will not install
    ... They do read feedback. ... Are you suggesting that Microsoft cares about the feedback it receives? ... through with bugs. ... but it certainly wasn't due to incompetence or nonchalant cavalier ...
    (microsoft.public.mac.office)
  • Re: [kde-linux] kde4 help
    ... from the control center there isn't the possibility ... become more user-friendly adn easy-yo-configure every (mainly ... testing - looking for bugs etc.. ... My feedback is "In 4 days of use there isn't a bug! ...
    (KDE)