def test_substract_list_logic(self): """ test performance and result """ l1 = [5, 7, 11, 11, 11, 12, 13] l2 = [7, 8, 11] res = substract_list(l1, l2) self.assertEqual(res, [5, 11, 11, 12, 13]) l1 = [5] l2 = [5] res = substract_list(l1, l2) self.assertEqual(res, [])
def owns_segments(self, segments, security): """ check if shareholder owns all those segments either as share does not check any kind of options. use owns_options_segments for this """ logger.info('checking if {} owns {}'.format(self, segments)) if isinstance(segments, str): segments = string_list_to_json(segments) logger.info('converted string to json') logger.info('getting current segments...') segments_owning = inflate_segments( self.current_segments(security=security)) failed_segments = [] logger.info('calculating segments not owning...') # shareholder does not own this failed_segments = substract_list(inflate_segments(segments), segments_owning) logger.info('check segment ownership done') return (len(failed_segments) == 0, deflate_segments(failed_segments), deflate_segments(segments_owning))
def test_substract_list_performance(self): """ test performance and result """ l1 = range(0, 10000000) l2 = range(0, 1000000) t0 = time.clock() res = substract_list(l1, l2) t1 = time.clock() delta = t1 - t0 print("substract list took {0:.4f} seconds.".format(delta)) self.assertEqual(res, range(1000000, 10000000)) if delta > 0.3: logger.error( 'BUILD performance error: test_substract_list_performance', extra={'delta': delta}) self.assertLess(delta, 0.5)
def current_segments(self, security, date=None): """ returns deflated segments which are owned by this shareholder. includes segments blocked for options. """ logger.info('current items: starting') date = date or datetime.datetime.now() # all pos before date qs_bought = self.buyer.filter(bought_at__lte=date) qs_sold = self.seller.filter(bought_at__lte=date) qs_bought = qs_bought.filter(security=security) qs_sold = qs_sold.filter(security=security) logger.info('current items: qs done') # -- flat list of bought items segments_bought = qs_bought.values_list('number_segments', flat=True) # flatten, unsorted with duplicates segments_bought = [ segment for sublist in segments_bought for segment in sublist ] # flat list of sold segments segments_sold = qs_sold.values_list('number_segments', flat=True) segments_sold = [ segment for sublist in segments_sold for segment in sublist ] logger.info('current items: flat lists done. inflating...') segments_owning = [] # inflate to have int only segments_bought = inflate_segments(segments_bought) segments_sold = inflate_segments(segments_sold) logger.info('current items: iterating through bought segments...') segments_owning = substract_list(segments_bought, segments_sold) logger.info('current items: finished') return deflate_segments(segments_owning)
def is_valid(self, raise_exception=False): """ validate cross data relations """ logger.info('position create validation...') res = super(PositionSerializer, self).is_valid(raise_exception) initial_data = self.initial_data security = initial_data.get('security') user = self.context.get("request").user company = user.operator_set.all()[0].company if security and Security.objects.get( company=company, title=security.get('title')).track_numbers: logger.info('validation: prepare data...') security = Security.objects.get(id=security['pk']) if (isinstance(initial_data.get('number_segments'), str) or isinstance(initial_data.get('number_segments'), unicode)): segments = string_list_to_json( initial_data.get('number_segments')) else: segments = initial_data.get('number_segments') # if we have seller (non capital increase) if initial_data.get('seller'): logger.info('validation: get seller segments...') seller = Shareholder.objects.get( pk=initial_data.get('seller')['pk']) owning, failed_segments, owned_segments = seller.owns_segments( segments, security) logger.info( 'validation: seller segs {} for security {} done'.format( segments, security)) # we need number_segments if this is a security with .track_numbers if not segments: raise serializers.ValidationError({ 'number_segments': [_('Invalid security numbers segments.')] }) # segments must be owned by seller elif initial_data.get('seller') and not owning: raise serializers.ValidationError({ 'number_segments': [ _('Segments "{}" must be owned by seller "{}". ' 'Available are {}').format(failed_segments, seller.user.last_name, owned_segments) ] }) # validate segment count == share count elif (security.count_in_segments(segments) != initial_data.get('count')): logger.info('validation: checking count...') raise serializers.ValidationError({ 'count': [ _('Number of shares in segments ({}) ' 'does not match count {}').format( security.count_in_segments(segments), initial_data.get('count')) ] }) # segment must not be used by option plan logger.info('validation: option plan validation...') inflated_segments = inflate_segments(segments) oplan_segments = inflate_segments( security.company.get_all_option_plan_segments()) if substract_list(inflated_segments, oplan_segments) != inflated_segments: raise serializers.ValidationError({ 'number_segments': [ _('Segment {} is blocked for options and cannot be' ' transfered to a shareholder.').format(segments) ] }) return res