def gen_smpl(self, idx, n_digits, n_nums, var_digits=0): """ generate an example of a simple addition """ assert (n_digits >= 1) assert (var_digits < n_digits) q_tab = Tabular(' c r ', row_height=1.2) # contents... nums = [gen_rnd(n_digits, var_digits) for n in range(0, n_nums)] sgns = [sign(randint(-1000000, 1000000)) for n in range(0, n_nums)] sign_syms = list(map(lambda x: '+' if x == 1 else '-', sgns)) sum_str = ''.join( [sign_syms[n] + str(nums[n]) for n in range(0, n_nums)]) if sum_str[0] == '+': sum_str = sum_str[1:] # tail if leading with a '+' mth = Math(escape=False, inline=True) mth.append(NoEscape(sum_str + '=')) q_tab.add_row((bold(str(idx) + ':'), mth)) a_tab = Tabular(' l l ', row_height=1.1) a_idx = bold(str(idx) + ":") res = sum(multiply(nums, sgns)) a_tab.add_row((a_idx, res)) #print(sum_str, '=', res) return (q_tab, a_tab)
def gen_smpl(self, idx, n_digits, n_nums, var_digits=0): """ generate an example of a simple addition """ assert (n_digits >= 1) assert (var_digits < n_digits) q_tab = Tabular(' c r ', row_height=1.2) nums = [gen_rnd(n_digits, var_digits) for n in range(0, n_nums)] sum_str = functools.reduce(lambda x, y: str(x) + '-' + str(y), nums) mth = Math(escape=False, inline=True) mth.append(NoEscape(sum_str + '=')) q_tab.add_row((bold(str(idx) + ':'), mth)) a_tab = Tabular(' l l ', row_height=1.1) a_idx = bold(str(idx) + ":") a_tab.add_row((a_idx, nums[0] - sum(nums[1:]))) return (q_tab, a_tab)
def display(ap, doc): properties, effects = analyze(ap) with doc.create(Subsection(ap['name'])): doc.append('Definition:') with doc.create(Itemize(options='noitemsep')) as itemize: if 'url' in ap: itemize.add_item('url: {}'.format(ap['url'])) itemize.add_item('floor: {}'.format(ap['floor'])) itemize.add_item('starting price: {:.2f} Mkr'.format( ap['startPrice'] / 1e6)) if 'booliEstimate' in ap: itemize.add_item('Booli estimated price: {:.2f} Mkr'.format( ap['booliEstimate'] / 1e6)) itemize.add_item('fee: {:.0f} kr/month'.format(ap['fee'])) ma = Math(inline=True, escape=False) ma.append('\\text{{area: }} {:.0f}\\,\\mathrm{{m}}^2'.format( ap['area'])) itemize.add_item(ma) itemize.add_item( 'commute time: {:.0f} min of which {:.0f} min is walking. Frequency every {:.0f} min.' .format(ap['commuteTime'], ap['commuteWalkTime'], ap['commutePeriod'])) doc.append('Results:') with doc.create(Itemize(options='noitemsep')) as itemize: itemize.add_item('interest: {:.1%}'.format(properties['interest'])) itemize.add_item('amortization: {:.1%}'.format( properties['amortization'])) itemize.add_item( 'monthly cost: {a:.0f} kr/month (incl. amortization)'.format( a=properties['monthlyCost'])) # print('m2 price: {p:.0f}% of local market value'.format(p=100*ap['price']/(ap['area']*m2Price[ap['location']]))) # print('Effective commute time: ',effectiveCommuteTime,' min') # print('Monthly apartment bill:',monthlyPrice) doc.append('Value breakdown:\n') with doc.create(Center()) as centered: with centered.create(Tabular('l|r')) as table: tot = 0 for e in effects: table.add_row((e, '{p}{v:.0f} kr/month'.format( k=e, v=effects[e], p='+' if effects[e] > 0 else ''))) tot += effects[e] table.add_hline() table.add_row(('TOTAL', '{t:.0f} kr/month'.format(t=tot)))
def main(): geometry_options = {"tmargin": "2cm", "lmargin": "2cm"} doc = Document(geometry_options=geometry_options) doc.packages.append(Package('enumitem')) doc.packages.append(Package('hyperref')) with doc.create(Section('Personal preferences')): doc.append('Positive features:\n') with doc.create(Center()) as centered: with centered.create(Tabular('l|r')) as table: table.add_hline() for p in proValues: table.add_row( (p, '+{v:.0f} kr/month'.format(v=proValues[p]))) table.add_hline() doc.append('Negative features:\n') with doc.create(Center()) as centered: with centered.create(Tabular('l|r')) as table: table.add_hline() for p in conValues: table.add_row( (p, '{v:.0f} kr/month'.format(v=-conValues[p]))) table.add_hline() doc.append('Intermediate results:\n') with doc.create(Itemize(options='noitemsep')) as itemize: itemize.add_item('Time value: {v:.1f} kr/min'.format(v=timeValue)) doc.append( 'Estimated size for different regions (only considering interest and average prices):\n' ) with doc.create(Center()) as centered: with centered.create(Tabular('l|r')) as table: table.add_hline() for k in m2Price: for i in range(len(m2s) - 1): if m2ValPerm2[i] < m2Price[k] * interest / MONTHS_PER_YEAR: ma = Math(inline=True, escape=False) ma.append('{a}-{b}\\,\\mathrm{{m}}^2'.format(a=m2s[i], b=m2s[i + 1])) table.add_row((k, ma)) table.add_hline() break with doc.create(Section('Apartments')): for a in aps: display(a, doc) with doc.create(Section('Summary')): doc.append('Effective values (kr/month):') saps = [a for a in aps if 'soldFor' in a] asaps = [analyze(a, price=a['soldFor']) for a in saps] aaps = [analyze(a) for a in aps] scats = set() stots = [] for a in asaps: tot = 0 for e in a[1]: scats.add(e) tot += a[1][e] stots.append(tot) with doc.create(Center()) as centered: with centered.create(Tabular('l' + '|r' * (len(saps)))) as table: table.add_row(['SCORE'] + [a['name'][:6] + '..' for a in saps]) table.add_hline() for c in scats: table.add_row([c] + [ '{:.0f}'.format(a[1][c]) if c in a[1] else '' for a in asaps ]) table.add_hline() table.add_row(['TOTAL'] + ['{:.0f}'.format(t) for t in stots]) table.add_empty_row() table.add_row(['PRICES'] + ['' for a in saps]) table.add_row( ['Ad price'] + ['{:.0f}'.format(ap['startPrice']) for ap in saps]) table.add_row(['Sold price'] + ['{:.0f}'.format(ap['soldFor']) for ap in saps]) table.add_row(['Booli'] + [ '{:.0f}'.format(ap['booliEstimate']) if 'booliEstimate' in ap else '-' for ap in saps ]) table.add_row(['My top bid'] + [ '{:.0f}'.format( solveForPrice(ap, goalValue) // bidStep * bidStep) for ap in saps ]) paps = [a for a in aps if 'soldFor' not in a] aaps = [analyze(a) for a in paps] cats = set() tots = [] for a in aaps: tot = 0 for e in a[1]: cats.add(e) tot += a[1][e] tots.append(tot) with doc.create(Center()) as centered: with centered.create(Tabular('l' + '|r' * (len(paps)))) as table: table.add_row(['SCORE (est.)'] + [a['name'][:6] + '..' for a in paps]) table.add_hline() for c in cats: table.add_row([c] + [ '{:.0f}'.format(a[1][c]) if c in a[1] else '' for a in aaps ]) table.add_hline() table.add_row(['TOTAL'] + ['{:.0f}'.format(t) for t in tots]) table.add_row(['accept est.'] + [ '{:.0f}'. format(sum(analyze(a, price=a['acceptPrice'])[1].values()) ) if 'acceptPrice' in a else '-' for a in paps ]) table.add_empty_row() table.add_row(['PRICES'] + ['' for a in paps]) table.add_row( ['Ad price'] + ['{:.0f}'.format(ap['startPrice']) for ap in paps]) table.add_row(['Booli'] + [ '{:.0f}'.format(ap['booliEstimate']) if 'booliEstimate' in ap else '-' for ap in paps ]) table.add_row(['Accept'] + [ '{:.0f}'.format(ap['acceptPrice']) if 'acceptPrice' in ap else '-' for ap in paps ]) table.add_row(['Top bid'] + [ '{:.0f}'.format(ap['bids'][-1]) if 'bids' in ap else '-' for ap in paps ]) table.add_row(['My top bid'] + [ '{:.0f}'.format( solveForPrice(ap, goalValue) // bidStep * bidStep) for ap in paps ]) table.add_row(['Action'] + [(('BID' if sum( analyze(ap, price=ap['bids'][-1])[1].values() ) > goalValue else 'STOP') if 'bids' in ap else '') for ap in paps]) # for a in aps: # properties,effects=analyze(a) # table.add_row((a['name'],'{:.0f} kr/month'.format(sum(effects.values())))) doc.generate_pdf('apartment', clean_tex=False)