def get_y_axis_type(self, ): """\brief return type of Y axis """ if len(self._data_list) == 0: raise od_exception(u'you must specify some data to get Y type') t = type(self._data_list[0][1]) return (float if t == int or t == float else t)
def set_dbid(self, dbid): """\brief Setter for property dbid \param dbid """ if dbid == None: raise od_exception('dbid must not be None') self._dbid = dbid
def set_stock_name(self, stock_name): """\brief Setter for property stock_name \param stock_name """ assert(isinstance(stock_name, basestring)) if len(stock_name) == 0: raise od_exception('stock_name must not be empty') self._stock_name = unicode(stock_name)
def set_commission(self, commission): """\brief Setter for property commission \param commission """ if float(commission) < 0: raise od_exception('commission must be >= 0') self._commission = float(commission) self['commission'] = self._commission
def set_market_name(self, market_name): """\brief Setter for property market_name \param market_name """ assert(isinstance(market_name, basestring)) if len(market_name) == 0: raise od_exception('market_name must not be empty') self._market_name = unicode(market_name)
def set_direction(self, direction): """\brief Setter for property direction \param direction """ ii = trunc(float(direction)) if ii not in [-1, 1]: raise od_exception('direction must be just -1 or 1') self._direction = ii self['direction'] = self._direction
def ret(*args, **kargs): rtt = None if hasattr(args[0], "_sqlite_connection"): if args[0]._sqlite_connection != None: rtt = func(*args, **kargs) else: raise od_exception_db_closed("Database is not opened now") else: raise od_exception("self has no attribute _sqlite_connection") return rtt
def ret(*args, **kargs): rtt = None if hasattr(args[0], "_sqlite_connection"): if args[0]._sqlite_connection == None: rtt = func(*args, **kargs) else: raise od_exception_db_opened("The database is still opened") else: raise od_exception("self has no attribute _sqlite_connection") return rtt
def calculate_height(self, context, width): """\brief calculate height with given width \param context - cairo context \param width - float, width in which to fit legend """ if self._family == None: raise od_exception('you must specify font before calculating its height') textheight = self.get_text_height(context) colons, lines = self._get_colons_and_lines(context, textheight, width) return lines * textheight + ((lines - 1) * textheight / 3.) # lines + space between lines (1/3 of line height)
def set_dtm(self, dtm): """\brief Setter for property dtm \param dtm """ if isinstance(dtm, datetime): self._dtm = dtm elif isinstance(dtm, basestring): self._dtm = datetime.strptime(dtm, '%Y-%m-%dT%H:%M:%S') else: raise od_exception('dtm must be datetime or string')
def find_paper_id(self, papers, market_name, stock_name): """\brief return id of paper if it is in database \param papers \param market_name \param stock_name """ for paper in papers: if paper.get_name() == stock_name and paper.get_market_name() == market_name: if paper.get_dbid() == None: raise od_exception('paper {0} of markte {1} is not in database yet'.format(stock_name, market_name)) return paper.get_dbid()
def get_deals(self, source): """\brief return list of deal objects \param source - open_ru_source class instance """ parsed = minidom.parse(source.get_filename()) report_type = self.get_report_type(parsed) if report_type == 'forts': return self.get_forts_deals(parsed) elif report_type == 'micex': return self.get_micex_deals(parsed) else: raise od_exception("report type must be either 'forts' or 'micex'")
def get_report_type(self, domparsed): """\brief determine type of report \param domparsed \retval "forts" if report type is forts \retval "micex" otherwise """ rep = domparsed.getElementsByTagName('report')[0] deals = rep.getElementsByTagName('common_deal')[0].getElementsByTagName('item') # list of deals types = [a.getAttribute('security_type') for a in deals] if set(types) == set([u'Акции']): return 'micex' elif set(types) == set(['FUT']): return 'forts' else: raise od_exception('wrong format of file: can not determine type of report')
def get_repo_deals(self, source): """\brief get repo deals from micex report \param source \return list of \ref report_deal instances """ parsed = minidom.parse(source.get_filename()) repdeal = parsed.getElementsByTagName('report')[0].getElementsByTagName('repo_deal') if len(repdeal) == 0: return [] repdeal = repdeal[0] items = repdeal.getElementsByTagName('item') report_type = self.get_report_type(parsed) if report_type not in ['micex', 'forts']: raise od_exception('report type must be micex or forts') ret = [] for item in items: d = report_deal() d.set_market_name(('FORTS' if report_type == 'forts' else 'MICEX')) d.set_stock_name(item.getAttribute('security_name')) d.set_dtm(item.getAttribute('exec_date')) d.set_points(item.getAttribute('deal_price')) d.set_count(item.getAttribute('quantity')) d.set_direction(item.getAttribute('exec_sign')) try: d.set_commission(item.getAttribute('broker_comm')) except: pass d.set_attributes({'db.no_position' : 1, 'deal.repo' : int(item.getAttribute('repo_part'))}) d.set_user_attributes({'REPO' : int(item.getAttribute('repo_part'))}) d.set_sha1(hashlib.sha1(reduce_by_string('', [item.getAttribute(attrname) for attrname in ['deal_time', 'security_type', 'security_name', 'grn_code', 'deal_price', 'exec_sign', 'repo_part', 'quantity']] + ['repo'])).hexdigest()) ret.append(d) return ret
def append_papers(self, model, papers): """\brief \param model \param papers list of \ref report_paper """ for paper in papers: pid = None stock_type = None if paper.get_market_name() == 'FORTS': stock_type = 'future' elif paper.get_market_name() == 'MICEX': stock_type = 'stock' else: raise od_exception('market_name must be "FORTS" or "MICEX"') pp = model.get_paper(stock_type, paper.get_name()) if pp != None: pid = pp['id'] else: pid = model.create_paper(stock_type, name = paper.get_name(), stock = paper.get_market_name()) paper.set_dbid(pid)
def get_chart_area_rectangle(self, ): """\brief return rectangle of the chart area in cairo context """ if self._chart_area_rectangle == None: raise od_exception('you must call draw, then get chart area rectangle') return self._chart_area_rectangle
def draw(self, context, rectangle): """\brief draw the mesh \param context - cairo context \param rectangle - cairo context rectangle """ if self._rectangle.get_lower_x_limit() == self._rectangle.get_upper_x_limit() or self._rectangle.get_lower_y_limit() == self._rectangle.get_upper_y_limit(): self._chart_area_rectangle = copy_cairo_rectangle(rectangle) return fextent = self.get_font_extent(context) if self._rectangle == None: raise od_exception('you must specify rectangle before drawing') if issubclass(self._rectangle.get_x_axis_type(), datetime): chartheight = rectangle.height - (2 * fextent[2]) - (4 * self._line_width) # height of the chart area numbers = trunc(chartheight / fextent[2]) # the max count of numbers can be displayed in vertical colon (y label) if numbers > 0: draw_numbers = self._generate_numbers(self._rectangle.get_lower_y_limit(), self._rectangle.get_upper_y_limit(), numbers) # generated numbers to draw chartwidth = rectangle.width - self._get_max_number_width(context, draw_numbers) - self._line_width * 2 # width of the chart area else: draw_numbers = [] chartwidth = rectangle.width self._chart_area_rectangle = cairo_rectangle(rectangle.x, rectangle.y, chartwidth, chartheight) y_numbers_coordinates = self._map_y_numbers_to_context(self._chart_area_rectangle, self._rectangle, draw_numbers) self._draw_vertical_colon_numbers(context, rectangle.x + chartwidth + self._line_width, y_numbers_coordinates, draw_numbers) context.set_source_rgb(*self._color) context.set_line_width(self._line_width * 0.5) context.set_dash([3, 7]) self._draw_horizontal_lines(context, rectangle.x, rectangle.x + chartwidth, y_numbers_coordinates) # draw dash lines in the chart area context.stroke() context.set_dash([1,0]) # draw rectangle and two horizontal lines at bottom of the chart context.set_line_width(self._line_width) context.rectangle(rectangle.x, rectangle.y, chartwidth, rectangle.height) line1y = rectangle.y + chartheight line2y = line1y + fextent[2] + (2 * self._line_width) context.move_to(rectangle.x, line1y) context.line_to(rectangle.x + chartwidth, line1y) context.move_to(rectangle.x, line2y) context.line_to(rectangle.x + chartwidth, line2y) context.stroke() context.set_dash([1,0]) # draw dates at bottom of the chart and vertical dash lines (small_coords, big_coords) = self._draw_horizontal_dates(context, self._chart_area_rectangle, line1y + self._line_width, line2y + self._line_width) # draw dates and return X coordinates of vertical lines in context coorinate system # draw vertical small lines context.stroke() context.set_line_width(self._line_width) context.set_source_rgb(*self._color) self._draw_vertical_lines(context, line1y, line2y, small_coords) # draw vertical big lines self._draw_vertical_lines(context, line2y, rectangle.y + rectangle.height, big_coords) context.stroke() # draw vertical dashes context.set_source_rgb(*self._color) context.set_line_width(self._line_width * 0.5) context.set_dash([3, 7]) self._draw_vertical_lines(context, rectangle.y, line1y, small_coords) context.stroke() context.set_dash([1,0]) else: raise NotImplementedError()
def get_x_axis_type(self, ): """\brief return type of axis X """ if len(self._data_list) == 0: raise od_exception(u'you must specify some data to get X type') return type(self._data_list[0][0])
def redraw(self, ): """\brief redraw with old context """ if self._old_context == None or self._old_rectangle == None: raise od_exception(u'draw must be called before redraw at least 1 time') self._draw(self._old_context, self._old_rectangle)