<commit_before>##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo import models, fields, api, _
from odoo.exceptions import UserError

class ResPartner(models.Model):
    _inherit = "res.partner"

    # TODO recreamos credit_copy y debit_copy porque odo da error si mandamos
    # company_id =.. en el conexto, termina arrojando:
    # ("account_move_line"."company_id" = (1
    credit_copy = fields.Monetary(
        'Credit',
        compute='_compute_debit_credit',
    )
    debit_copy = fields.Monetary(
        'Debit',
        compute='_compute_debit_credit',
    )
    new_credit = fields.Monetary(
        compute='_compute_new_debit_credit',
        inverse='_inverse_new_credit'
    )
    new_debit = fields.Monetary(
        compute='_compute_new_debit_credit',
        inverse='_inverse_new_debit'
    )

    @api.multi
    def _compute_new_debit_credit(self):
        move_id = self._context.get('active_id', False)
        AccountMoveLine = self.env['account.move.line']
        if not move_id:
            return False
        company_id = self._context.get(
            'company_id',
            self.env.user.company_id.id)

        for rec in self:
            for account_field, field, from_field in [
                    ('property_account_receivable_id',
                        'new_credit', 'credit_copy'),
                    ('property_account_payable_id',
                        'new_debit', 'debit_copy')]:
                account = getattr(
                    rec.with_context(force_company=company_id), account_field)
                move_line = AccountMoveLine.search([
                    ('move_id', '=', move_id),
                    ('partner_id', '=', rec.id),
                    ('account_id', '=', account.id),
                ], limit=1)
                rec[field] = rec[from_field] + move_line.balance

    @api.multi
    def _compute_debit_credit(self):
        move_id = self._context.get('active_id', False)
        move = self.env['account.move'].browse(move_id)
        company_id = self._context.get('company_id', False)
        AccountMoveLine = self.env['account.move.line']
        for rec in self:
            for internal_type, field in [
                    ('receivable', 'credit_copy'), ('payable', 'debit_copy')]:
                domain = [
                    ('partner_id', '=', rec.id),
                    # not reconciled for performance
                    ('full_reconcile_id', '=', False),
                    ('account_id.internal_type', '=', internal_type),
                    ('move_id.state', '=', 'posted'),
                ]
                if company_id:
                    domain.append(('company_id', '=', company_id))
                if move:
                    domain.append(('date', '<=', move.date))
                rec[field] = sum(
                    AccountMoveLine.search(domain).mapped('balance'))

    @api.multi
    def _inverse_new_debit(self):
        self._set_new_credit_debit(
            'new_debit', 'debit_copy', 'property_account_payable_id')

    @api.multi
    def _inverse_new_credit(self):
        self._set_new_credit_debit(
            'new_credit', 'credit_copy', 'property_account_receivable_id')

    @api.multi
    def _set_new_credit_debit(
            self, new_value_field, old_value_field, account_field):
        company_id = self._context.get('company_id', False)
        if not company_id:
            raise UserError(_(
                'Company is required in context to set partner balance'))
        for rec in self:
            account = getattr(
                rec.with_context(force_company=company_id), account_field)
            new_value = rec[new_value_field] or 0.0
            value_diff = new_value - (rec[old_value_field] or 0.0)
            account._helper_update_line(value_diff, rec)
<commit_msg>[FIX] account_move_helper do not filter reconciled entries<commit_after>##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo import models, fields, api, _
from odoo.exceptions import UserError

class ResPartner(models.Model):
    _inherit = "res.partner"

    # TODO recreamos credit_copy y debit_copy porque odo da error si mandamos
    # company_id =.. en el conexto, termina arrojando:
    # ("account_move_line"."company_id" = (1
    credit_copy = fields.Monetary(
        'Credit',
        compute='_compute_debit_credit',
    )
    debit_copy = fields.Monetary(
        'Debit',
        compute='_compute_debit_credit',
    )
    new_credit = fields.Monetary(
        compute='_compute_new_debit_credit',
        inverse='_inverse_new_credit'
    )
    new_debit = fields.Monetary(
        compute='_compute_new_debit_credit',
        inverse='_inverse_new_debit'
    )

    @api.multi
    def _compute_new_debit_credit(self):
        move_id = self._context.get('active_id', False)
        AccountMoveLine = self.env['account.move.line']
        if not move_id:
            return False
        company_id = self._context.get(
            'company_id',
            self.env.user.company_id.id)

        for rec in self:
            for account_field, field, from_field in [
                    ('property_account_receivable_id',
                        'new_credit', 'credit_copy'),
                    ('property_account_payable_id',
                        'new_debit', 'debit_copy')]:
                account = getattr(
                    rec.with_context(force_company=company_id), account_field)
                move_line = AccountMoveLine.search([
                    ('move_id', '=', move_id),
                    ('partner_id', '=', rec.id),
                    ('account_id', '=', account.id),
                ], limit=1)
                rec[field] = rec[from_field] + move_line.balance

    @api.multi
    def _compute_debit_credit(self):
        move_id = self._context.get('active_id', False)
        move = self.env['account.move'].browse(move_id)
        company_id = self._context.get('company_id', False)
        AccountMoveLine = self.env['account.move.line']
        # TODO use sql with groupby and sum
        for rec in self:
            for internal_type, field in [
                    ('receivable', 'credit_copy'), ('payable', 'debit_copy')]:
                domain = [
                    ('partner_id', '=', rec.id),
                    ('account_id.internal_type', '=', internal_type),
                    ('move_id.state', '=', 'posted'),
                ]
                if company_id:
                    domain.append(('company_id', '=', company_id))
                if move:
                    domain.append(('date', '<=', move.date))
                rec[field] = sum(
                    AccountMoveLine.search(domain).mapped('balance'))

    @api.multi
    def _inverse_new_debit(self):
        self._set_new_credit_debit(
            'new_debit', 'debit_copy', 'property_account_payable_id')

    @api.multi
    def _inverse_new_credit(self):
        self._set_new_credit_debit(
            'new_credit', 'credit_copy', 'property_account_receivable_id')

    @api.multi
    def _set_new_credit_debit(
            self, new_value_field, old_value_field, account_field):
        company_id = self._context.get('company_id', False)
        if not company_id:
            raise UserError(_(
                'Company is required in context to set partner balance'))
        for rec in self:
            account = getattr(
                rec.with_context(force_company=company_id), account_field)
            new_value = rec[new_value_field] or 0.0
            value_diff = new_value - (rec[old_value_field] or 0.0)
            account._helper_update_line(value_diff, rec)
<|endoftext|>


<commit_before>from testcase import TestCase
from testutils import RunTests

from mock import Mock, patch, sentinel

# for use in the test
something  = sentinel.Something
something_else  = sentinel.SomethingElse

class PatchTest(TestCase):

    def testSinglePatch(self):
        class Something(object):
            attribute = sentinel.Original

        @apply
        @patch(Something, 'attribute', sentinel.Patched)
        def test():
            self.assertEquals(Something.attribute, sentinel.Patched, "unpatched")

        self.assertEquals(Something.attribute, sentinel.Original, "patch not restored")

    def testMultiplePatch(self):
        class Something(object):
            attribute = sentinel.Original
            next_attribute = sentinel.Original2

        @apply
        @patch(Something, 'attribute', sentinel.Patched)
        @patch(Something, 'next_attribute', sentinel.Patched2)
        def test():
            self.assertEquals(Something.attribute, sentinel.Patched, "unpatched")
            self.assertEquals(Something.next_attribute, sentinel.Patched2, "unpatched")

        self.assertEquals(Something.attribute, sentinel.Original, "patch not restored")
        self.assertEquals(Something.next_attribute, sentinel.Original2, "patch not restored")

    def testPatchModule(self):
        import __main__
        __main__.something = sentinel.Something

        @apply
        @patch('__main__', 'something', sentinel.Something2)
        def test():
            self.assertEquals(__main__.something, sentinel.Something2, "unpatched")

        self.assertEquals(__main__.something, sentinel.Something, "patch not restored")

        import UnitTests.patchtest as PTModule

        @patch('UnitTests.patchtest', 'something', sentinel.Something2)
        @patch('UnitTests.patchtest', 'something_else', sentinel.SomethingElse)
        def test():
            self.assertEquals(PTModule.something, sentinel.Something2, "unpatched")
            self.assertEquals(PTModule.something_else, sentinel.SomethingElse, "unpatched")
        test()

        self.assertEquals(PTModule.something, sentinel.Something, "patch not restored")
        self.assertEquals(PTModule.something_else, sentinel.SomethingElse, "patch not restored")

        # Test the patching and restoring works a second time
        test()

        self.assertEquals(PTModule.something, sentinel.Something, "patch not restored")
        self.assertEquals(PTModule.something_else, sentinel.SomethingElse, "patch not restored")

        mock = Mock()
        mock.return_value = sentinel.Handle
        @patch('__builtin__', 'open', mock)
        def test():
            self.assertEquals(open('filename', 'r'), sentinel.Handle, "open not patched")
        test()
        test()

        self.assertNotEquals(open, mock, "patch not restored")

    def testPatchWithTwoArgs(self):
        class Test(object):
            something = sentinel.Original
            something2 = sentinel.Original2

        @apply
        @patch(Test, 'something')
        def test(mock):
            self.assertEquals(mock, Test.something, "Mock not passed into test function")
            self.assertTrue(isinstance(mock, Mock),
                            "patch with two arguments did not create a mock")

        @patch(Test, 'something')
        @patch(Test, 'something2')
        def test(this1, this2, mock1, mock2):
            self.assertEquals(this1, sentinel.this1, "Patched function didn't receive initial argument")
            self.assertEquals(this2, sentinel.this2, "Patched function didn't receive second argument")
            self.assertEquals(mock1, Test.something, "Mock not passed into test function")
            self.assertEquals(mock2, Test.something2, "Second Mock not passed into test function")
            self.assertTrue(isinstance(mock2, Mock),
                            "patch with two arguments did not create a mock")
            self.assertTrue(isinstance(mock2, Mock),
                            "patch with two arguments did not create a mock")

            # A hack to test that new mocks are passed the second time
            self.assertNotEquals(outerMock1, mock1, "unexpected value for mock1")
            self.assertNotEquals(outerMock2, mock2, "unexpected value for mock1")
            return mock1, mock2

        outerMock1 = None
        outerMock2 = None
        outerMock1, outerMock2 = test(sentinel.this1, sentinel.this2)

        # Test that executing a second time creates new mocks
        outerMock1, outerMock2 = test(sentinel.this1, sentinel.this2)

if __name__ == '__main__':
    RunTests(PatchTest)
<commit_msg>Minor test change.<commit_after>from testcase import TestCase
from testutils import RunTests

from mock import Mock, patch, sentinel

# for use in the test
something  = sentinel.Something
something_else  = sentinel.SomethingElse

class PatchTest(TestCase):

    def testSinglePatch(self):
        class Something(object):
            attribute = sentinel.Original

        @apply
        @patch(Something, 'attribute', sentinel.Patched)
        def test():
            self.assertEquals(Something.attribute, sentinel.Patched, "unpatched")

        self.assertEquals(Something.attribute, sentinel.Original, "patch not restored")

    def testMultiplePatch(self):
        class Something(object):
            attribute = sentinel.Original
            next_attribute = sentinel.Original2

        @apply
        @patch(Something, 'attribute', sentinel.Patched)
        @patch(Something, 'next_attribute', sentinel.Patched2)
        def test():
            self.assertEquals(Something.attribute, sentinel.Patched, "unpatched")
            self.assertEquals(Something.next_attribute, sentinel.Patched2, "unpatched")

        self.assertEquals(Something.attribute, sentinel.Original, "patch not restored")
        self.assertEquals(Something.next_attribute, sentinel.Original2, "patch not restored")

    def testPatchModule(self):
        import __main__
        __main__.something = sentinel.Something

        @apply
        @patch('__main__', 'something', sentinel.Something2)
        def test():
            self.assertEquals(__main__.something, sentinel.Something2, "unpatched")

        self.assertEquals(__main__.something, sentinel.Something, "patch not restored")

        import UnitTests.patchtest as PTModule

        @patch('UnitTests.patchtest', 'something', sentinel.Something2)
        @patch('UnitTests.patchtest', 'something_else', sentinel.SomethingElse)
        def test():
            self.assertEquals(PTModule.something, sentinel.Something2, "unpatched")
            self.assertEquals(PTModule.something_else, sentinel.SomethingElse, "unpatched")
        test()

        self.assertEquals(PTModule.something, sentinel.Something, "patch not restored")
        self.assertEquals(PTModule.something_else, sentinel.SomethingElse, "patch not restored")

        # Test the patching and restoring works a second time
        test()

        self.assertEquals(PTModule.something, sentinel.Something, "patch not restored")
        self.assertEquals(PTModule.something_else, sentinel.SomethingElse, "patch not restored")

        mock = Mock()
        mock.return_value = sentinel.Handle
        @patch('__builtin__', 'open', mock)
        def test():
            self.assertEquals(open('filename', 'r'), sentinel.Handle, "open not patched")
        test()
        test()

        self.assertNotEquals(open, mock, "patch not restored")

    def testPatchWithTwoArgs(self):
        class Test(object):
            something = sentinel.Original
            something2 = sentinel.Original2

        @apply
        @patch(Test, 'something')
        def test(mock):
            self.assertEquals(mock, Test.something, "Mock not passed into test function")
            self.assertTrue(isinstance(mock, Mock),
                            "patch with two arguments did not create a mock")

        @patch(Test, 'something')
        @patch(Test, 'something2')
        def test(this1, this2, mock1, mock2):
            self.assertEquals(this1, sentinel.this1, "Patched function didn't receive initial argument")
            self.assertEquals(this2, sentinel.this2, "Patched function didn't receive second argument")
            self.assertEquals(mock1, Test.something, "Mock not passed into test function")
            self.assertEquals(mock2, Test.something2, "Second Mock not passed into test function")
            self.assertTrue(isinstance(mock2, Mock),
                            "patch with two arguments did not create a mock")
            self.assertTrue(isinstance(mock2, Mock),
                            "patch with two arguments did not create a mock")

            # A hack to test that new mocks are passed the second time
            self.assertNotEquals(outerMock1, mock1, "unexpected value for mock1")
            self.assertNotEquals(outerMock2, mock2, "unexpected value for mock1")
            return mock1, mock2

        outerMock1 = None
        outerMock2 = None
        outerMock1, outerMock2 = test(sentinel.this1, sentinel.this2)

        # Test that executing a second time creates new mocks
        test(sentinel.this1, sentinel.this2)

if __name__ == '__main__':
    RunTests(PatchTest)
<|endoftext|>


<commit_before># -*- coding:utf-8 -*-
from nsnqtlib.strategies.strategy import  basestrategy,reportforms
import pandas as pd

class macd(basestrategy):
    '''
           ��д��������������������
    '''

    def __init__(self,startdate=(2013, 1, 1),enddate=[],emafast=12,emaslow=26,demday=9):
        self.emafast = emafast
        self.emaslow = emaslow
        self.demday = demday
        self.tempstatus = {}
        self.status = False
        self.count = 0
        self.gain_grads = 0.1
        self.loss_grads = -0.10

        super(macd,self).__init__(startdate,enddate)

    def setprocedure(self,lst,count):
        status = self.status
        macd = self.macdlist[count]
        diff = self.difflist[count]
        data = lst[count]
        procedure = {"data":data,
                     "diff":diff,
                     "macd":macd,
                     "status":status
                     }
        self.tempstatus[self.collection].append([lst[0],procedure])

    def buy(self,lst,count):
        ''' input:
                lst: [] ,row data in every stock data,default is in self.formatlist = ["date","volume","close","high","low","open","pre_close"]
                count: float, the number to the row since first row
            ouput:
                bool, can buy or not buy
                [], buy record,if can't buy,is empty list
        '''
        self.setprocedure(lst,count)
        rst = False
        if self.buy_condition1(count) and \
            self.buy_condition2(count) and self.status:
            rst = True
        self.setstatus(lst,count)
        return rst

    def buy_condition1(self,count):
        if self.macdlist[count]>0 and \
            self.macdlist[count-1]<0:
            return True
        return False

    def buy_condition2(self,count):
        if self.difflist[count] <0 :
            return True
        return False

    def setstatus(self,lst,count):
        if self.macdlist[count]<0 and \
            self.macdlist[count-1]>0:
            self.status = True
            self.count = count
        elif self.macdlist[count]>0 and \
            self.macdlist[count-1]<0:
            self.status = False
        elif self.difflist[count] > 0 or self.demlist[count] >0 :
            self.status = False

    def getstocktype(self):
        if self.collection.startswith("300"):
            return [1,0,0,0]
        elif self.collection.startswith("002"):
            return [0,1,0,0]
        elif self.collection.startswith("000"):
            return [0,0,1,0]
        else:
            return [0,0,0,1]

    def mean(self,lst):
        total=0
        for i in lst:
            total += i[1]
        return total/len(lst)

    def getfeatures(self,lst,count):
        """
        MACD最小值   上一轮MACD小于0的天数   股票类型  换手率比值(前一交易日) 换手率比值（前二交易日） 换手率比值（前十天均值）
        """
        minmacd = min(self.macdlist[self.count:count])
        days = count - self.count
        stocktype = self.getstocktype()
        trading_rate1 = lst[count][1]/lst[count-1][1]
        trading_rate2 = lst[count][1]/lst[count-2][1]
        try:
            trading_rate10 = lst[count][1]/self.mean(lst[count-10:count])
        except:
            trading_rate10 = (trading_rate2 + trading_rate1)/2
        return [minmacd,days,*stocktype,trading_rate1,trading_rate2,trading_rate10]

    def sell(self,lst,count,buyrecord):
        sell_date = self.timestamp2date(lst[count][0])
        close = lst[count][2]
        currentday_high = lst[count][3]
        currentday_low = lst[count][4]
        gain_grads = self.gain_grads
        loss_grads = self.loss_grads
        buy_price = buyrecord[0][2]
        hold_days = count - buyrecord[1]
        buy_date = self.timestamp2date(buyrecord[0][0])
        collection = buyrecord[2]
        feature = buyrecord[3]
#         if self.sell_condition(lst,count):
#             return True,[collection,buy_date,sell_date,hold_days,(close-buy_price)/buy_price,feature]

        if self.stopgain_condition(buy_price,currentday_high,gain_grads):
            return True,[collection,buy_date,sell_date,hold_days,gain_grads,feature]
        elif self.stoploss_condition(buy_price,currentday_low,loss_grads):
            return True,[collection,buy_date,sell_date,hold_days,(close-buy_price)/buy_price,feature]
        return False,None

    def sell_condition(self,lst,count):
        meanday = 20
        close = lst[count][2]
        meanlist = [i[2] for i in lst[count-meanday+1:count+1]]
        if close <= sum(meanlist)/meanday:
            return True
        return False

    def stopgain_condition(self,buy_price,current_price,grads=0.1):
        if (current_price-buy_price)/buy_price >= grads:
            return True
        return False

    def stoploss_condition(self,buy_price,current_price,grads=-0.05):
        if (current_price-buy_price)/buy_price <= grads:
            return True
        return False

    def setenv(self,collection):
        self.collection = collection
        self.tempstatus[collection] = []
        data = self._getdata(collection)
        self.datalst = [l for l in data[self.formatlist].fillna(0).values if l[1] !=0]
        self.datalst = self.rehabilitation(self.datalst)
        self.setmacdlist(self.datalst)
        return

    def setmacdlist(self,lst):
        s_ema = f_ema = lst[0][2]
        dem = 0
        self.difflist = []
        self.demlist = []
        self.macdlist = []
        for line in lst:
            s_ema = (s_ema*(self.emaslow-1)+ 2*line[2])/(self.emaslow+1)
            f_ema = (f_ema*(self.emafast-1)+ 2*line[2])/(self.emafast+1)
            dif = f_ema-s_ema
            self.difflist.append(dif)

            dem = (dem*(self.demday-1)+ 2*dif)/(self.demday+1)
            self.demlist.append(dem)
            self.macdlist.append(2*(dif-dem))

    def saveprocedure(self,filename="procedure_records.csv"):
        df = pd.DataFrame(self.trading_records,columns=["stock","data"])
        df.to_csv(filename)
        return

class status():
    def __init__(self,lst,count,):
        self.lst = lst
        self.count = count
        self.macd = macd()

    def createstatusfromdata(self):
        looplist = self.macd.setlooplist()
        pass

    def createstatus(self):
        pass

    def getstatus(self):
        pass

    def savestatus(self):
        pass

if __name__ == '__main__':
    s = macd()
    s.setlooplist()
    s.looplist_historyreturn()
    s.savetrading2csv("macd.csv")
    s.saveholding2csv("macdhold.csv")
    df = pd.read_csv('macd.csv')
    report = reportforms(df)
    report.cumulative_graph()
    report.positiongain(100)

<commit_msg>modify default date<commit_after># -*- coding:utf-8 -*-
from nsnqtlib.strategies.strategy import  basestrategy,reportforms
import pandas as pd

class macd(basestrategy):
    '''
           ��д��������������������
    '''

    def __init__(self,startdate=(2013, 1, 1),enddate=[],emafast=12,emaslow=26,demday=9):
        self.emafast = emafast
        self.emaslow = emaslow
        self.demday = demday
        self.tempstatus = {}
        self.status = False
        self.count = 0
        self.gain_grads = 0.1
        self.loss_grads = -0.10

        super(macd,self).__init__(startdate,enddate)

    def setprocedure(self,lst,count):
        status = self.status
        macd = self.macdlist[count]
        diff = self.difflist[count]
        data = lst[count]
        procedure = {"data":data,
                     "diff":diff,
                     "macd":macd,
                     "status":status
                     }
        self.tempstatus[self.collection]={lst[0]:procedure}

    def buy(self,lst,count):
        ''' input:
                lst: [] ,row data in every stock data,default is in self.formatlist = ["date","volume","close","high","low","open","pre_close"]
                count: float, the number to the row since first row
            ouput:
                bool, can buy or not buy
                [], buy record,if can't buy,is empty list
        '''
        self.setprocedure(lst,count)
        rst = False
        if self.buy_condition1(count) and \
            self.buy_condition2(count) and self.status:
            rst = True
        self.setstatus(lst,count)
        return rst

    def buy_condition1(self,count):
        if self.macdlist[count]>0 and \
            self.macdlist[count-1]<0:
            return True
        return False

    def buy_condition2(self,count):
        if self.difflist[count] <0 :
            return True
        return False

    def setstatus(self,lst,count):
        if self.macdlist[count]<0 and \
            self.macdlist[count-1]>0:
            self.status = True
            self.count = count
        elif self.macdlist[count]>0 and \
            self.macdlist[count-1]<0:
            self.status = False
        elif self.difflist[count] > 0 or self.demlist[count] >0 :
            self.status = False

    def getstocktype(self):
        if self.collection.startswith("300"):
            return [1,0,0,0]
        elif self.collection.startswith("002"):
            return [0,1,0,0]
        elif self.collection.startswith("000"):
            return [0,0,1,0]
        else:
            return [0,0,0,1]

    def mean(self,lst):
        total=0
        for i in lst:
            total += i[1]
        return total/len(lst)

    def getfeatures(self,lst,count):
        """
        MACD最小值   上一轮MACD小于0的天数   股票类型  换手率比值(前一交易日) 换手率比值（前二交易日） 换手率比值（前十天均值）
        """
        minmacd = min(self.macdlist[self.count:count])
        days = count - self.count
        stocktype = self.getstocktype()
        trading_rate1 = lst[count][1]/lst[count-1][1]
        trading_rate2 = lst[count][1]/lst[count-2][1]
        try:
            trading_rate10 = lst[count][1]/self.mean(lst[count-10:count])
        except:
            trading_rate10 = (trading_rate2 + trading_rate1)/2
        return [minmacd,days,*stocktype,trading_rate1,trading_rate2,trading_rate10]

    def sell(self,lst,count,buyrecord):
        sell_date = self.timestamp2date(lst[count][0])
        close = lst[count][2]
        currentday_high = lst[count][3]
        currentday_low = lst[count][4]
        gain_grads = self.gain_grads
        loss_grads = self.loss_grads
        buy_price = buyrecord[0][2]
        hold_days = count - buyrecord[1]
        buy_date = self.timestamp2date(buyrecord[0][0])
        collection = buyrecord[2]
        feature = buyrecord[3]
#         if self.sell_condition(lst,count):
#             return True,[collection,buy_date,sell_date,hold_days,(close-buy_price)/buy_price,feature]

        if self.stopgain_condition(buy_price,currentday_high,gain_grads):
            return True,[collection,buy_date,sell_date,hold_days,gain_grads,feature]
        elif self.stoploss_condition(buy_price,currentday_low,loss_grads):
            return True,[collection,buy_date,sell_date,hold_days,(close-buy_price)/buy_price,feature]
        return False,None

    def sell_condition(self,lst,count):
        meanday = 20
        close = lst[count][2]
        meanlist = [i[2] for i in lst[count-meanday+1:count+1]]
        if close <= sum(meanlist)/meanday:
            return True
        return False

    def stopgain_condition(self,buy_price,current_price,grads=0.1):
        if (current_price-buy_price)/buy_price >= grads:
            return True
        return False

    def stoploss_condition(self,buy_price,current_price,grads=-0.05):
        if (current_price-buy_price)/buy_price <= grads:
            return True
        return False

    def setenv(self,collection):
        self.collection = collection
        self.tempstatus[collection] = []
        data = self._getdata(collection)
        self.datalst = [l for l in data[self.formatlist].fillna(0).values if l[1] !=0]
        self.datalst = self.rehabilitation(self.datalst)
        self.setmacdlist(self.datalst)
        return

    def setmacdlist(self,lst):
        s_ema = f_ema = lst[0][2]
        dem = 0
        self.difflist = []
        self.demlist = []
        self.macdlist = []
        for line in lst:
            s_ema = (s_ema*(self.emaslow-1)+ 2*line[2])/(self.emaslow+1)
            f_ema = (f_ema*(self.emafast-1)+ 2*line[2])/(self.emafast+1)
            dif = f_ema-s_ema
            self.difflist.append(dif)

            dem = (dem*(self.demday-1)+ 2*dif)/(self.demday+1)
            self.demlist.append(dem)
            self.macdlist.append(2*(dif-dem))

    def saveprocedure(self,filename="procedure_records.csv"):
        df = pd.DataFrame(self.trading_records,columns=["stock","data"])
        df.to_csv(filename)
        return

class status():
    def __init__(self,lst,count,):
        self.lst = lst
        self.count = count
        self.macd = macd()

    def createstatusfromdata(self):
        looplist = self.macd.setlooplist()
        pass

    def createstatus(self):
        pass

    def getstatus(self):
        pass

    def savestatus(self):
        pass

if __name__ == '__main__':
    s = macd()
    s.setlooplist()
    s.looplist_historyreturn()
    s.savetrading2csv("macd.csv")
    s.saveholding2csv("macdhold.csv")
    df = pd.read_csv('macd.csv')
    report = reportforms(df)
    report.cumulative_graph()
    report.positiongain(100)

<|endoftext|>


<commit_before>"""Adapter for using Jinja2 with Django."""
import functools
import imp
import logging

from django import http
from django.conf import settings
from django.template.context import get_standard_processors
from django.utils.importlib import import_module
from django.utils.translation import trans_real

import jinja2

VERSION = (0, 3)
__version__ = '.'.join(map(str, VERSION))

log = logging.getLogger('jingo')

_helpers_loaded = False

class Environment(jinja2.Environment):

    def get_template(self, name, parent=None, globals=None):
        """Make sure our helpers get loaded before any templates."""
        load_helpers()
        return super(Environment, self).get_template(name, parent, globals)

    def from_string(self, source, globals=None, template_class=None):
        load_helpers()
        return super(Environment, self).from_string(source, globals,
                                                    template_class)

def get_env():
    """Configure and return a jinja2 Environment."""
    # Mimic Django's setup by loading templates from directories in
    # TEMPLATE_DIRS and packages in INSTALLED_APPS.
    x = ((jinja2.FileSystemLoader, settings.TEMPLATE_DIRS),
         (jinja2.PackageLoader, settings.INSTALLED_APPS))
    loaders = [loader(p) for loader, places in x for p in places]

    opts = {'trim_blocks': True,
            'extensions': ['jinja2.ext.i18n'],
            'autoescape': True,
            'auto_reload': settings.DEBUG,
            'loader': jinja2.ChoiceLoader(loaders),
            }

    if hasattr(settings, 'JINJA_CONFIG'):
        if hasattr(settings.JINJA_CONFIG, '__call__'):
            config = settings.JINJA_CONFIG()
        else:
            config = settings.JINJA_CONFIG
        opts.update(config)

    e = Environment(**opts)
    return e

def render(request, template, context=None, **kwargs):
    """
    Shortcut like Django's ``render_to_response``, but better.

    Minimal usage, with only a request object and a template name::

        return jingo.render(request, 'template.html')

    With template context and keywords passed to
    :class:`django.http.HttpResponse`::

        return jingo.render(request, 'template.html',
                            {'some_var': 42}, status=209)
    """
    rendered = render_to_string(request, template, context)
    return http.HttpResponse(rendered, **kwargs)

def render_to_string(request, template, context=None):
    """
    Render a template into a string.
    """
    def get_context():
        # Only call the context processors once per request.
        if not hasattr(request, '_jingo_context'):
            request._jingo_context = {}
            for processor in get_standard_processors():
                request._jingo_context.update(processor(request))
        ctx = {} if context is None else context.copy()
        ctx.update(request._jingo_context)
        return ctx

    # If it's not a Template, it must be a path to be loaded.
    if not isinstance(template, jinja2.environment.Template):
        template = env.get_template(template)

    return template.render(**get_context())

def load_helpers():
    """Try to import ``helpers.py`` from each app in INSTALLED_APPS."""
    # We want to wait as long as possible to load helpers so there aren't any
    # weird circular imports with jingo.
    global _helpers_loaded
    if _helpers_loaded:
        return
    _helpers_loaded = True

    from jingo import helpers

    for app in settings.INSTALLED_APPS:
        try:
            app_path = import_module(app).__path__
        except AttributeError:
            continue

        try:
            imp.find_module('helpers', app_path)
        except ImportError:
            continue

        import_module('%s.helpers' % app)

class Register(object):
    """Decorators to add filters and functions to the template Environment."""

    def __init__(self, env):
        self.env = env

    def filter(self, f):
        """Adds the decorated function to Jinja's filter library."""
        self.env.filters[f.__name__] = f
        return f

    def function(self, f):
        """Adds the decorated function to Jinja's global namespace."""
        self.env.globals[f.__name__] = f
        return f

    def inclusion_tag(self, template):
        """Adds a function to Jinja, but like Django's @inclusion_tag."""
        def decorator(f):
            @functools.wraps(f)
            def wrapper(*args, **kw):
                context = f(*args, **kw)
                t = env.get_template(template).render(context)
                return jinja2.Markup(t)
            return self.function(wrapper)
        return decorator

env = get_env()
register = Register(env)

from jinja2 import utils

class _MarkupEscapeHelper(object):
    """
    A replacement for jinja's _MarkupEscapeHandler that doesn't use lambdas.
    Exceptions in the lambdas seem to cause the gc_refs crashes from
    bug 560381.
    """

    def __init__(self, obj):
        self.obj = obj

    def __getitem__(self, key):
        """
        We don't want localizers to take down the site when their string
        interpolation variables don't match the real strings.

        If we have {% trans users=... %}blah {{ users }}{% endtrans %}
        and the localizer has "blah %(user)s" in their gettext catalog, there will
        be a KeyError when Python looks for the users variable.
        """
        try:
            return self.obj[key]
        except KeyError:
            lang = trans_real.get_language()
            log.error(u'[%s] KeyError: %r in %r' % (lang, key, self.obj))

    def __str__(self):
        return str(utils.escape(self.obj))

    def __unicode__(self):
        return unicode(utils.escape(self.obj))

    def __repr__(self):
        return str(utils.escape(repr(self.obj)))

    def __int__(self):
        return int(self.obj)

    def __float__(self):
        return float(self.obj)

utils._MarkupEscapeHelper = _MarkupEscapeHelper
<commit_msg>jinja moved _MarkupEscapeHelper so this doesn't work anymore<commit_after>"""Adapter for using Jinja2 with Django."""
import functools
import imp
import logging

from django import http
from django.conf import settings
from django.template.context import get_standard_processors
from django.utils.importlib import import_module
from django.utils.translation import trans_real

import jinja2

VERSION = (0, 3)
__version__ = '.'.join(map(str, VERSION))

log = logging.getLogger('jingo')

_helpers_loaded = False

class Environment(jinja2.Environment):

    def get_template(self, name, parent=None, globals=None):
        """Make sure our helpers get loaded before any templates."""
        load_helpers()
        return super(Environment, self).get_template(name, parent, globals)

    def from_string(self, source, globals=None, template_class=None):
        load_helpers()
        return super(Environment, self).from_string(source, globals,
                                                    template_class)

def get_env():
    """Configure and return a jinja2 Environment."""
    # Mimic Django's setup by loading templates from directories in
    # TEMPLATE_DIRS and packages in INSTALLED_APPS.
    x = ((jinja2.FileSystemLoader, settings.TEMPLATE_DIRS),
         (jinja2.PackageLoader, settings.INSTALLED_APPS))
    loaders = [loader(p) for loader, places in x for p in places]

    opts = {'trim_blocks': True,
            'extensions': ['jinja2.ext.i18n'],
            'autoescape': True,
            'auto_reload': settings.DEBUG,
            'loader': jinja2.ChoiceLoader(loaders),
            }

    if hasattr(settings, 'JINJA_CONFIG'):
        if hasattr(settings.JINJA_CONFIG, '__call__'):
            config = settings.JINJA_CONFIG()
        else:
            config = settings.JINJA_CONFIG
        opts.update(config)

    e = Environment(**opts)
    return e

def render(request, template, context=None, **kwargs):
    """
    Shortcut like Django's ``render_to_response``, but better.

    Minimal usage, with only a request object and a template name::

        return jingo.render(request, 'template.html')

    With template context and keywords passed to
    :class:`django.http.HttpResponse`::

        return jingo.render(request, 'template.html',
                            {'some_var': 42}, status=209)
    """
    rendered = render_to_string(request, template, context)
    return http.HttpResponse(rendered, **kwargs)

def render_to_string(request, template, context=None):
    """
    Render a template into a string.
    """
    def get_context():
        # Only call the context processors once per request.
        if not hasattr(request, '_jingo_context'):
            request._jingo_context = {}
            for processor in get_standard_processors():
                request._jingo_context.update(processor(request))
        ctx = {} if context is None else context.copy()
        ctx.update(request._jingo_context)
        return ctx

    # If it's not a Template, it must be a path to be loaded.
    if not isinstance(template, jinja2.environment.Template):
        template = env.get_template(template)

    return template.render(**get_context())

def load_helpers():
    """Try to import ``helpers.py`` from each app in INSTALLED_APPS."""
    # We want to wait as long as possible to load helpers so there aren't any
    # weird circular imports with jingo.
    global _helpers_loaded
    if _helpers_loaded:
        return
    _helpers_loaded = True

    from jingo import helpers

    for app in settings.INSTALLED_APPS:
        try:
            app_path = import_module(app).__path__
        except AttributeError:
            continue

        try:
            imp.find_module('helpers', app_path)
        except ImportError:
            continue

        import_module('%s.helpers' % app)

class Register(object):
    """Decorators to add filters and functions to the template Environment."""

    def __init__(self, env):
        self.env = env

    def filter(self, f):
        """Adds the decorated function to Jinja's filter library."""
        self.env.filters[f.__name__] = f
        return f

    def function(self, f):
        """Adds the decorated function to Jinja's global namespace."""
        self.env.globals[f.__name__] = f
        return f

    def inclusion_tag(self, template):
        """Adds a function to Jinja, but like Django's @inclusion_tag."""
        def decorator(f):
            @functools.wraps(f)
            def wrapper(*args, **kw):
                context = f(*args, **kw)
                t = env.get_template(template).render(context)
                return jinja2.Markup(t)
            return self.function(wrapper)
        return decorator

env = get_env()
register = Register(env)
<|endoftext|>


<commit_before>from functools import partial

from django.core.urlresolvers import reverse
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.db.models.signals import post_save

from cms.models.fields import PlaceholderField
from cms.models.pluginmodel import CMSPlugin
from aldryn_people.models import Person
import reversion
from reversion.revisions import VersionAdapter
from parler.models import TranslatableModel, TranslatedFields
from parler import cache

# TODO: The followng class and registration function shall be extracted in a
# command addon module (as soon as we have one).

class TranslatableVersionAdapter(VersionAdapter):
    revision_manager = None
    follow_placeholders = True

    def __init__(self, model):
        super(TranslatableVersionAdapter, self).__init__(model)

        # Register the translation model to be tracked as well, by following
        # all placeholder fields, if any.
        root_model = model._parler_meta.root_model
        self.revision_manager.register(root_model)

        # Also add the translations to the models to follow.
        self.follow = list(self.follow) + [model._parler_meta.root_rel_name]

        # ...and the placeholders
        if self.follow_placeholders:
            self.follow += model._meta.placeholder_field_names
            post_save.connect(self._add_plugins_to_revision, sender=model)

        # And make sure that when we revert them, we update the translations
        # cache (this is normally done in the translation `save_base` method,
        # but it is not caled when reverting changes).
        post_save.connect(self._update_cache, sender=root_model)

    def _update_cache(self, sender, instance, raw, **kwargs):
        """Update the translations chache when restoring from a revision."""
        if raw:
            # Raw is set to true (only) when restoring from fixtures or,
            # django-reversion
            cache._cache_translation(instance)

    def _add_plugins_to_revision(self, sender, instance, **kwargs):
        """Manually add plugins to the revision.

        This method is an updated and adapter version of
        https://github.com/divio/django-cms/blob/develop/cms/utils/helpers.py#L34
        but instead of working on pages, works on models with placeholder
        fields.
        """
        rev_ctx = self.revision_manager._revision_context_manager

        def add_to_context(obj):
            adapter = self.revision_manager.get_adapter(obj.__class__)
            version_data = adapter.get_version_data(obj)
            rev_ctx.add_to_context(self.revision_manager, obj, version_data)

        if rev_ctx.is_active() and not rev_ctx.is_managing_manually():
            ph_ids = [getattr(instance, '{0}_id'.format(name))
                      for name in instance._meta.placeholder_field_names]

            # Add all plugins to the revision
            for plugin in CMSPlugin.objects.filter(placeholder_id__in=ph_ids):
                plugin_instance, _ = plugin.get_plugin_instance()
                if plugin_instance:
                    add_to_context(plugin_instance)
                add_to_context(plugin)

register_translatable = partial(
    reversion.register, adapter_cls=TranslatableVersionAdapter,
    revision_manager=reversion.default_revision_manager)

@register_translatable
class Article(TranslatableModel):
    translations = TranslatedFields(
        title = models.CharField(_('Title'), max_length=234),
    )

    content = PlaceholderField('aldryn_newsblog_article_content',
                               related_name='aldryn_newsblog_articles')

    slug = models.SlugField(
        verbose_name=_('Slug'),
        max_length=255,
        unique=True,
        blank=True,
        help_text=_(
            'Used in the URL. If changed, the URL will change. '
            'Clean it to have it re-created.'),
    )

    author = models.ForeignKey(Person)
    owner = models.ForeignKey(User)
    namespace = models.CharField(max_length=123, blank=True, default='')
    category = models.CharField(max_length=123, blank=True, default='')
    publishing_date = models.DateTimeField()

    def get_absolute_url(self):
        return reverse(
            'aldryn_newsblog:article-detail', kwargs={'slug': self.slug})

class LatestEntriesPlugin(CMSPlugin):

    latest_entries = models.IntegerField(
        default=5,
        help_text=_('The number of latests entries to be displayed.')
    )

    def __unicode__(self):
        return str(self.latest_entries).decode('utf8')

    def copy_relations(self, oldinstance):
        self.tags = oldinstance.tags.all()

    def get_articles(self):
        articles = Article.objects.filter_by_language(self.language)
        return articles[:self.latest_entries]
<commit_msg>Don't raise an error if the model does not define any placeholders.<commit_after>from functools import partial

from django.core.urlresolvers import reverse
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.db.models.signals import post_save

from cms.models.fields import PlaceholderField
from cms.models.pluginmodel import CMSPlugin
from aldryn_people.models import Person
import reversion
from reversion.revisions import VersionAdapter
from parler.models import TranslatableModel, TranslatedFields
from parler import cache

# TODO: The followng class and registration function shall be extracted in a
# command addon module (as soon as we have one).

class TranslatableVersionAdapter(VersionAdapter):
    revision_manager = None
    follow_placeholders = True

    def __init__(self, model):
        super(TranslatableVersionAdapter, self).__init__(model)

        # Register the translation model to be tracked as well, by following
        # all placeholder fields, if any.
        root_model = model._parler_meta.root_model
        self.revision_manager.register(root_model)

        # Also add the translations to the models to follow.
        self.follow = list(self.follow) + [model._parler_meta.root_rel_name]

        # ...and the placeholders
        placeholders = getattr(model._meta, 'placeholder_field_names', None)
        if self.follow_placeholders and placeholders:
            self.follow += placeholders
            post_save.connect(self._add_plugins_to_revision, sender=model)

        # And make sure that when we revert them, we update the translations
        # cache (this is normally done in the translation `save_base` method,
        # but it is not caled when reverting changes).
        post_save.connect(self._update_cache, sender=root_model)

    def _update_cache(self, sender, instance, raw, **kwargs):
        """Update the translations chache when restoring from a revision."""
        if raw:
            # Raw is set to true (only) when restoring from fixtures or,
            # django-reversion
            cache._cache_translation(instance)

    def _add_plugins_to_revision(self, sender, instance, **kwargs):
        """Manually add plugins to the revision.

        This method is an updated and adapter version of
        https://github.com/divio/django-cms/blob/develop/cms/utils/helpers.py#L34
        but instead of working on pages, works on models with placeholder
        fields.
        """
        rev_ctx = self.revision_manager._revision_context_manager

        def add_to_context(obj):
            adapter = self.revision_manager.get_adapter(obj.__class__)
            version_data = adapter.get_version_data(obj)
            rev_ctx.add_to_context(self.revision_manager, obj, version_data)

        if rev_ctx.is_active() and not rev_ctx.is_managing_manually():
            ph_ids = [getattr(instance, '{0}_id'.format(name))
                      for name in instance._meta.placeholder_field_names]

            # Add all plugins to the revision
            for plugin in CMSPlugin.objects.filter(placeholder_id__in=ph_ids):
                plugin_instance, _ = plugin.get_plugin_instance()
                if plugin_instance:
                    add_to_context(plugin_instance)
                add_to_context(plugin)

register_translatable = partial(
    reversion.register, adapter_cls=TranslatableVersionAdapter,
    revision_manager=reversion.default_revision_manager)

@register_translatable
class Article(TranslatableModel):
    translations = TranslatedFields(
        title = models.CharField(_('Title'), max_length=234),
    )

    content = PlaceholderField('aldryn_newsblog_article_content',
                               related_name='aldryn_newsblog_articles')

    slug = models.SlugField(
        verbose_name=_('Slug'),
        max_length=255,
        unique=True,
        blank=True,
        help_text=_(
            'Used in the URL. If changed, the URL will change. '
            'Clean it to have it re-created.'),
    )

    author = models.ForeignKey(Person)
    owner = models.ForeignKey(User)
    namespace = models.CharField(max_length=123, blank=True, default='')
    category = models.CharField(max_length=123, blank=True, default='')
    publishing_date = models.DateTimeField()

    def get_absolute_url(self):
        return reverse(
            'aldryn_newsblog:article-detail', kwargs={'slug': self.slug})

class LatestEntriesPlugin(CMSPlugin):

    latest_entries = models.IntegerField(
        default=5,
        help_text=_('The number of latests entries to be displayed.')
    )

    def __unicode__(self):
        return str(self.latest_entries).decode('utf8')

    def copy_relations(self, oldinstance):
        self.tags = oldinstance.tags.all()

    def get_articles(self):
        articles = Article.objects.filter_by_language(self.language)
        return articles[:self.latest_entries]
<|endoftext|>


<commit_before><commit_msg>Not done yet<commit_after><|endoftext|>


<commit_before>from __future__ import print_function

import contextlib
import imp
import os
import shutil
import subprocess
import sys
import tempfile
from ctypes import *

import numpy as np

from numba import unittest_support as unittest
from numba.pycc import find_shared_ending, find_pyext_ending, main
from numba.pycc.decorators import clear_export_registry
from .support import TestCase

from numba.tests.support import static_temp_directory

base_path = os.path.dirname(os.path.abspath(__file__))

def unset_macosx_deployment_target():
    """Unset MACOSX_DEPLOYMENT_TARGET because we are not building portable
    libraries
    """
    macosx_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', None)
    if macosx_target is not None:
        del os.environ['MACOSX_DEPLOYMENT_TARGET']

class BasePYCCTest(TestCase):

    def setUp(self):
        self.tmpdir = static_temp_directory('test_pycc')

    def tearDown(self):
        # Since we're executing the module-under-test several times
        # from the same process, we must clear the exports registry
        # between invocations.
        clear_export_registry()

    @contextlib.contextmanager
    def check_c_ext(self, extdir, name):
        sys.path.append(extdir)
        try:
            lib = __import__(name)
            yield lib
        finally:
            sys.path.remove(extdir)
            sys.modules.pop(name, None)

class TestLegacyAPI(BasePYCCTest):

    def test_pycc_ctypes_lib(self):
        """
        Test creating a C shared library object using pycc.
        """
        unset_macosx_deployment_target()

        source = os.path.join(base_path, 'compile_with_pycc.py')
        cdll_modulename = 'test_dll_legacy' + find_shared_ending()
        cdll_path = os.path.join(self.tmpdir, cdll_modulename)
        if os.path.exists(cdll_path):
            os.unlink(cdll_path)

        main(args=['--debug', '-o', cdll_path, source])
        lib = CDLL(cdll_path)
        lib.mult.argtypes = [POINTER(c_double), c_void_p, c_void_p,
                             c_double, c_double]
        lib.mult.restype = c_int

        lib.multf.argtypes = [POINTER(c_float), c_void_p, c_void_p,
                              c_float, c_float]
        lib.multf.restype = c_int

        res = c_double()
        lib.mult(byref(res), None, None, 123, 321)
        self.assertEqual(res.value, 123 * 321)

        res = c_float()
        lib.multf(byref(res), None, None, 987, 321)
        self.assertEqual(res.value, 987 * 321)

    def test_pycc_pymodule(self):
        """
        Test creating a CPython extension module using pycc.
        """
        self.skipTest("lack of environment can make the extension crash")
        unset_macosx_deployment_target()

        source = os.path.join(base_path, 'compile_with_pycc.py')
        modulename = 'test_pyext_legacy'
        out_modulename = os.path.join(self.tmpdir,
                                      modulename + find_pyext_ending())
        if os.path.exists(out_modulename):
            os.unlink(out_modulename)

        main(args=['--debug', '--python', '-o', out_modulename, source])

        with self.check_c_ext(self.tmpdir, modulename) as lib:
            res = lib.multi(123, 321)
            self.assertPreciseEqual(res, 123 * 321)
            res = lib.multf(987, 321)
            self.assertPreciseEqual(res, 987.0 * 321.0)

    def test_pycc_bitcode(self):
        """
        Test creating a LLVM bitcode file using pycc.
        """
        unset_macosx_deployment_target()

        modulename = os.path.join(base_path, 'compile_with_pycc')
        bitcode_modulename = os.path.join(self.tmpdir, 'test_bitcode_legacy.bc')
        if os.path.exists(bitcode_modulename):
            os.unlink(bitcode_modulename)

        main(args=['--debug', '--llvm', '-o', bitcode_modulename,
                   modulename + '.py'])

        # Sanity check bitcode file contents
        with open(bitcode_modulename, "rb") as f:
            bc = f.read()

        bitcode_wrapper_magic = b'\xde\xc0\x17\x0b'
        bitcode_magic = b'BC\xc0\xde'
        self.assertTrue(bc.startswith((bitcode_magic, bitcode_wrapper_magic)), bc)

class TestCC(BasePYCCTest):

    def setUp(self):
        super(TestCC, self).setUp()
        from . import compile_with_pycc
        self._test_module = compile_with_pycc
        imp.reload(self._test_module)

    @contextlib.contextmanager
    def check_cc_compiled(self, cc):
        cc.debug = True
        cc.output_dir = self.tmpdir
        cc.compile()

        with self.check_c_ext(self.tmpdir, cc.name) as lib:
            yield lib

    def check_cc_compiled_in_subprocess(self, lib, code):
        prolog = """if 1:
            import sys
            sys.path.insert(0, %(path)r)
            import %(name)s as lib
            """ % {'name': lib.__name__,
                   'path': os.path.dirname(lib.__file__)}
        code = prolog.strip(' ') + code
        subprocess.check_call([sys.executable, '-c', code])

    def test_cc_properties(self):
        cc = self._test_module.cc
        self.assertEqual(cc.name, 'pycc_test_simple')

        # Inferred output directory
        d = self._test_module.cc.output_dir
        self.assertTrue(os.path.isdir(d), d)

        # Inferred output filename
        f = self._test_module.cc.output_file
        self.assertFalse(os.path.exists(f), f)
        self.assertTrue(os.path.basename(f).startswith('pycc_test_simple.'), f)
        if sys.platform == 'linux':
            self.assertTrue(f.endswith('.so'), f)
            if sys.version_info >= (3,):
                self.assertIn('.cpython', f)

    def test_compile(self):
        with self.check_cc_compiled(self._test_module.cc) as lib:
            res = lib.multi(123, 321)
            self.assertPreciseEqual(res, 123 * 321)
            res = lib.multf(987, 321)
            self.assertPreciseEqual(res, 987.0 * 321.0)
            res = lib.square(5)
            self.assertPreciseEqual(res, 25)

    def test_compile_helperlib(self):
        with self.check_cc_compiled(self._test_module.cc_helperlib) as lib:
            res = lib.power(2, 7)
            self.assertPreciseEqual(res, 128)
            for val in (-1, -1 + 0j, np.complex128(-1)):
                res = lib.sqrt(val)
                self.assertPreciseEqual(res, 1j)
            res = lib.random(42)
            expected = np.random.RandomState(42).random_sample()
            self.assertPreciseEqual(res, expected)
            res = lib.size(np.float64([0] * 3))
            self.assertPreciseEqual(res, 3)

            code = """if 1:
                from numpy.testing import assert_equal, assert_allclose
                res = lib.power(2, 7)
                assert res == 128
                res = lib.random(42)
                assert_allclose(res, %(expected)s)
                """ % {'expected': expected}
            self.check_cc_compiled_in_subprocess(lib, code)

    def test_compile_nrt(self):
        with self.check_cc_compiled(self._test_module.cc_nrt) as lib:
            # Sanity check
            self.assertPreciseEqual(lib.zero_scalar(1), 0.0)
            res = lib.zeros(3)
            self.assertEqual(list(res), [0, 0, 0])

            code = """if 1:
                res = lib.zero_scalar(1)
                assert res == 0.0
                res = lib.zeros(3)
                assert list(res) == [0, 0, 0]
                """
            self.check_cc_compiled_in_subprocess(lib, code)

class TestDistutilsSupport(TestCase):

    def setUp(self):
        # Copy the test project into a temp directory to avoid
        # keeping any build leftovers in the source tree
        self.tmpdir = tempfile.mkdtemp(prefix='test_pycc_distutils-')
        source_dir = os.path.join(base_path, 'pycc_distutils_usecase')
        self.usecase_dir = os.path.join(self.tmpdir, 'work')
        shutil.copytree(source_dir, self.usecase_dir)

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def test_setup_py(self):
        # Compute PYTHONPATH to ensure the child processes see this Numba
        import numba
        numba_path = os.path.dirname(os.path.dirname(numba.__file__))
        env = dict(os.environ)
        if env.get('PYTHONPATH', ''):
            env['PYTHONPATH'] = numba_path + os.pathsep + env['PYTHONPATH']
        else:
            env['PYTHONPATH'] = numba_path

        def run_python(args):
            p = subprocess.Popen([sys.executable] + args,
                                 cwd=self.usecase_dir,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 env=env)
            out, err = p.communicate()
            rc = p.wait()
            if rc != 0:
                self.fail("setup.py failed with the following output:\n"
                          + err.decode('utf-8', 'ignore'))

        run_python(["setup.py", "build_ext", "--inplace"])
        code = """if 1:
            import pycc_compiled_module as lib
            assert lib.get_const() == 42
            res = lib.ones(3)
            assert list(res) == [1.0, 1.0, 1.0]
            """
        run_python(["-c", code])

if __name__ == "__main__":
    unittest.main()
<commit_msg>Improve error output<commit_after>from __future__ import print_function

import contextlib
import imp
import os
import shutil
import subprocess
import sys
import tempfile
from ctypes import *

import numpy as np

from numba import unittest_support as unittest
from numba.pycc import find_shared_ending, find_pyext_ending, main
from numba.pycc.decorators import clear_export_registry
from .support import TestCase

from numba.tests.support import static_temp_directory

base_path = os.path.dirname(os.path.abspath(__file__))

def unset_macosx_deployment_target():
    """Unset MACOSX_DEPLOYMENT_TARGET because we are not building portable
    libraries
    """
    macosx_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', None)
    if macosx_target is not None:
        del os.environ['MACOSX_DEPLOYMENT_TARGET']

class BasePYCCTest(TestCase):

    def setUp(self):
        self.tmpdir = static_temp_directory('test_pycc')

    def tearDown(self):
        # Since we're executing the module-under-test several times
        # from the same process, we must clear the exports registry
        # between invocations.
        clear_export_registry()

    @contextlib.contextmanager
    def check_c_ext(self, extdir, name):
        sys.path.append(extdir)
        try:
            lib = __import__(name)
            yield lib
        finally:
            sys.path.remove(extdir)
            sys.modules.pop(name, None)

class TestLegacyAPI(BasePYCCTest):

    def test_pycc_ctypes_lib(self):
        """
        Test creating a C shared library object using pycc.
        """
        unset_macosx_deployment_target()

        source = os.path.join(base_path, 'compile_with_pycc.py')
        cdll_modulename = 'test_dll_legacy' + find_shared_ending()
        cdll_path = os.path.join(self.tmpdir, cdll_modulename)
        if os.path.exists(cdll_path):
            os.unlink(cdll_path)

        main(args=['--debug', '-o', cdll_path, source])
        lib = CDLL(cdll_path)
        lib.mult.argtypes = [POINTER(c_double), c_void_p, c_void_p,
                             c_double, c_double]
        lib.mult.restype = c_int

        lib.multf.argtypes = [POINTER(c_float), c_void_p, c_void_p,
                              c_float, c_float]
        lib.multf.restype = c_int

        res = c_double()
        lib.mult(byref(res), None, None, 123, 321)
        self.assertEqual(res.value, 123 * 321)

        res = c_float()
        lib.multf(byref(res), None, None, 987, 321)
        self.assertEqual(res.value, 987 * 321)

    def test_pycc_pymodule(self):
        """
        Test creating a CPython extension module using pycc.
        """
        self.skipTest("lack of environment can make the extension crash")
        unset_macosx_deployment_target()

        source = os.path.join(base_path, 'compile_with_pycc.py')
        modulename = 'test_pyext_legacy'
        out_modulename = os.path.join(self.tmpdir,
                                      modulename + find_pyext_ending())
        if os.path.exists(out_modulename):
            os.unlink(out_modulename)

        main(args=['--debug', '--python', '-o', out_modulename, source])

        with self.check_c_ext(self.tmpdir, modulename) as lib:
            res = lib.multi(123, 321)
            self.assertPreciseEqual(res, 123 * 321)
            res = lib.multf(987, 321)
            self.assertPreciseEqual(res, 987.0 * 321.0)

    def test_pycc_bitcode(self):
        """
        Test creating a LLVM bitcode file using pycc.
        """
        unset_macosx_deployment_target()

        modulename = os.path.join(base_path, 'compile_with_pycc')
        bitcode_modulename = os.path.join(self.tmpdir, 'test_bitcode_legacy.bc')
        if os.path.exists(bitcode_modulename):
            os.unlink(bitcode_modulename)

        main(args=['--debug', '--llvm', '-o', bitcode_modulename,
                   modulename + '.py'])

        # Sanity check bitcode file contents
        with open(bitcode_modulename, "rb") as f:
            bc = f.read()

        bitcode_wrapper_magic = b'\xde\xc0\x17\x0b'
        bitcode_magic = b'BC\xc0\xde'
        self.assertTrue(bc.startswith((bitcode_magic, bitcode_wrapper_magic)), bc)

class TestCC(BasePYCCTest):

    def setUp(self):
        super(TestCC, self).setUp()
        from . import compile_with_pycc
        self._test_module = compile_with_pycc
        imp.reload(self._test_module)

    @contextlib.contextmanager
    def check_cc_compiled(self, cc):
        #cc.debug = True
        cc.output_dir = self.tmpdir
        cc.compile()

        with self.check_c_ext(self.tmpdir, cc.name) as lib:
            yield lib

    def check_cc_compiled_in_subprocess(self, lib, code):
        prolog = """if 1:
            import sys
            sys.path.insert(0, %(path)r)
            import %(name)s as lib
            """ % {'name': lib.__name__,
                   'path': os.path.dirname(lib.__file__)}
        code = prolog.strip(' ') + code
        subprocess.check_call([sys.executable, '-c', code])

    def test_cc_properties(self):
        cc = self._test_module.cc
        self.assertEqual(cc.name, 'pycc_test_simple')

        # Inferred output directory
        d = self._test_module.cc.output_dir
        self.assertTrue(os.path.isdir(d), d)

        # Inferred output filename
        f = self._test_module.cc.output_file
        self.assertFalse(os.path.exists(f), f)
        self.assertTrue(os.path.basename(f).startswith('pycc_test_simple.'), f)
        if sys.platform == 'linux':
            self.assertTrue(f.endswith('.so'), f)
            if sys.version_info >= (3,):
                self.assertIn('.cpython', f)

    def test_compile(self):
        with self.check_cc_compiled(self._test_module.cc) as lib:
            res = lib.multi(123, 321)
            self.assertPreciseEqual(res, 123 * 321)
            res = lib.multf(987, 321)
            self.assertPreciseEqual(res, 987.0 * 321.0)
            res = lib.square(5)
            self.assertPreciseEqual(res, 25)

    def test_compile_helperlib(self):
        with self.check_cc_compiled(self._test_module.cc_helperlib) as lib:
            res = lib.power(2, 7)
            self.assertPreciseEqual(res, 128)
            for val in (-1, -1 + 0j, np.complex128(-1)):
                res = lib.sqrt(val)
                self.assertPreciseEqual(res, 1j)
            res = lib.random(42)
            expected = np.random.RandomState(42).random_sample()
            self.assertPreciseEqual(res, expected)
            res = lib.size(np.float64([0] * 3))
            self.assertPreciseEqual(res, 3)

            code = """if 1:
                from numpy.testing import assert_equal, assert_allclose
                res = lib.power(2, 7)
                assert res == 128
                res = lib.random(42)
                assert_allclose(res, %(expected)s)
                """ % {'expected': expected}
            self.check_cc_compiled_in_subprocess(lib, code)

    def test_compile_nrt(self):
        with self.check_cc_compiled(self._test_module.cc_nrt) as lib:
            # Sanity check
            self.assertPreciseEqual(lib.zero_scalar(1), 0.0)
            res = lib.zeros(3)
            self.assertEqual(list(res), [0, 0, 0])

            code = """if 1:
                res = lib.zero_scalar(1)
                assert res == 0.0
                res = lib.zeros(3)
                assert list(res) == [0, 0, 0]
                """
            self.check_cc_compiled_in_subprocess(lib, code)

class TestDistutilsSupport(TestCase):

    def setUp(self):
        # Copy the test project into a temp directory to avoid
        # keeping any build leftovers in the source tree
        self.tmpdir = tempfile.mkdtemp(prefix='test_pycc_distutils-')
        source_dir = os.path.join(base_path, 'pycc_distutils_usecase')
        self.usecase_dir = os.path.join(self.tmpdir, 'work')
        shutil.copytree(source_dir, self.usecase_dir)

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def test_setup_py(self):
        # Compute PYTHONPATH to ensure the child processes see this Numba
        import numba
        numba_path = os.path.dirname(os.path.dirname(numba.__file__))
        env = dict(os.environ)
        if env.get('PYTHONPATH', ''):
            env['PYTHONPATH'] = numba_path + os.pathsep + env['PYTHONPATH']
        else:
            env['PYTHONPATH'] = numba_path

        def run_python(args):
            p = subprocess.Popen([sys.executable] + args,
                                 cwd=self.usecase_dir,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 env=env)
            out, err = p.communicate()
            rc = p.wait()
            if rc != 0:
                out = " stdout ".center(50, "-") + "\n" + out.decode('utf-8', 'ignore')
                err = " stderr ".center(50, "-") + "\n" + err.decode('utf-8', 'ignore')
                self.fail("setup.py failed with the following output:\n%s\n%s"
                          % (out, err))

        run_python(["setup.py", "build_ext", "--inplace"])
        code = """if 1:
            import pycc_compiled_module as lib
            assert lib.get_const() == 42
            res = lib.ones(3)
            assert list(res) == [1.0, 1.0, 1.0]
            """
        run_python(["-c", code])

if __name__ == "__main__":
    unittest.main()
<|endoftext|>


<commit_before>#!/usr/bin/env python

# Based on previous work by
# Charles Menguy (see: http://stackoverflow.com/questions/10217067/implementing-a-full-python-unix-style-daemon-process)
# and Sander Marechal (see: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/)

# Adapted by M.Hendrix [2015]

import sys, os, time, atexit, syslog
from signal import SIGTERM

class Daemon:
	"""
	A generic daemon class.

	Usage: subclass the Daemon class and override the run() method
	"""
	def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
		self.stdin = stdin
		self.stdout = stdout
		self.stderr = stderr
		self.pidfile = pidfile

	def daemonize(self):
		"""
		do the UNIX double-fork magic, see Stevens' "Advanced
		Programming in the UNIX Environment" for details (ISBN 0201563177)
		http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
		"""
		try:
			pid = os.fork()
			if pid > 0:
				# exit first parent
				sys.exit(0)
		except OSError, e:
			sys.stderr.write("fork no.1 failed: %d (%s)\n" % (e.errno, e.strerror))
			sys.exit(1)

		# decouple from parent environment
		os.chdir("/")
		os.setsid()
		os.umask(0)

		# do second fork
		try:
			pid = os.fork()
			if pid > 0:
				# exit from second parent
				sys.exit(0)
		except OSError, e:
			sys.stderr.write("fork no.2 failed: %d (%s)\n" % (e.errno, e.strerror))
			sys.exit(1)

		# redirect standard file descriptors
		sys.stdout.flush()
		sys.stderr.flush()
		si = file(self.stdin, 'r')
		so = file(self.stdout, 'a+')
		se = file(self.stderr, 'a+', 0)
		os.dup2(si.fileno(), sys.stdin.fileno())
		os.dup2(so.fileno(), sys.stdout.fileno())
		os.dup2(se.fileno(), sys.stderr.fileno())

		# write pidfile
		atexit.register(self.delpid)
		pid = str(os.getpid())
		file(self.pidfile,'w+').write("%s\n" % pid)

	def delpid(self):
		os.remove(self.pidfile)

	def start(self):
		"""
		Start the daemon
		"""
		# Check for a pidfile to see if the daemon already runs
		try:
			pf = file(self.pidfile,'r')
			pid = int(pf.read().strip())
			pf.close()
		except IOError:
			pid = None

		if pid:
			message = "pidfile %s already exist. Daemon already running?\n"
			sys.stderr.write(message % self.pidfile)
			sys.exit(1)

		# Start the daemon
		message = "Started process \n"
		sys.stderr.write(message)
		syslog.syslog(syslog.INFO,message)
		self.daemonize()
		self.run()

	def stop(self):
		"""
		Stop the daemon
		"""
		# Get the pid from the pidfile
		try:
			pf = file(self.pidfile,'r')
			pid = int(pf.read().strip())
			pf.close()
		except IOError:
			pid = None

		if not pid:
			message = "pidfile %s does not exist. Daemon not running?\n"
			sys.stderr.write(message % self.pidfile)
			return # not an error in a restart

		# Try killing the daemon process
		try:
			message = "Stopped process \n"
			syslog.syslog(syslog.INFO,message)
			sys.stderr.write(message)

			while 1:
				os.kill(pid, SIGTERM)
				time.sleep(0.1)

		except OSError, err:
			if err.errno == 3:
				if os.path.exists(self.pidfile):
					os.remove(self.pidfile)
			else:
				print str(err)
				sys.exit(1)

	def restart(self):
		"""
		Restart the daemon
		"""
		self.stop()
		self.start()

	def run(self):
		"""
		You should override this method when you subclass Daemon. It will be called after the process has been
		daemonized by start() or restart().
		"""
<commit_msg>syslog.LOG_INFO<commit_after>#!/usr/bin/env python

# Based on previous work by
# Charles Menguy (see: http://stackoverflow.com/questions/10217067/implementing-a-full-python-unix-style-daemon-process)
# and Sander Marechal (see: http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/)

# Adapted by M.Hendrix [2015]

import sys, os, time, atexit, syslog
from signal import SIGTERM

class Daemon:
	"""
	A generic daemon class.

	Usage: subclass the Daemon class and override the run() method
	"""
	def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
		self.stdin = stdin
		self.stdout = stdout
		self.stderr = stderr
		self.pidfile = pidfile

	def daemonize(self):
		"""
		do the UNIX double-fork magic, see Stevens' "Advanced
		Programming in the UNIX Environment" for details (ISBN 0201563177)
		http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
		"""
		try:
			pid = os.fork()
			if pid > 0:
				# exit first parent
				sys.exit(0)
		except OSError, e:
			sys.stderr.write("fork no.1 failed: %d (%s)\n" % (e.errno, e.strerror))
			sys.exit(1)

		# decouple from parent environment
		os.chdir("/")
		os.setsid()
		os.umask(0)

		# do second fork
		try:
			pid = os.fork()
			if pid > 0:
				# exit from second parent
				sys.exit(0)
		except OSError, e:
			sys.stderr.write("fork no.2 failed: %d (%s)\n" % (e.errno, e.strerror))
			sys.exit(1)

		# redirect standard file descriptors
		sys.stdout.flush()
		sys.stderr.flush()
		si = file(self.stdin, 'r')
		so = file(self.stdout, 'a+')
		se = file(self.stderr, 'a+', 0)
		os.dup2(si.fileno(), sys.stdin.fileno())
		os.dup2(so.fileno(), sys.stdout.fileno())
		os.dup2(se.fileno(), sys.stderr.fileno())

		# write pidfile
		atexit.register(self.delpid)
		pid = str(os.getpid())
		file(self.pidfile,'w+').write("%s\n" % pid)

	def delpid(self):
		os.remove(self.pidfile)

	def start(self):
		"""
		Start the daemon
		"""
		# Check for a pidfile to see if the daemon already runs
		try:
			pf = file(self.pidfile,'r')
			pid = int(pf.read().strip())
			pf.close()
		except IOError:
			pid = None

		if pid:
			message = "pidfile %s already exist. Daemon already running?\n"
			sys.stderr.write(message % self.pidfile)
			sys.exit(1)

		# Start the daemon
		message = "Started process \n"
		sys.stderr.write(message)
		syslog.syslog(syslog.LOG_INFO,message)
		self.daemonize()
		self.run()

	def stop(self):
		"""
		Stop the daemon
		"""
		# Get the pid from the pidfile
		try:
			pf = file(self.pidfile,'r')
			pid = int(pf.read().strip())
			pf.close()
		except IOError:
			pid = None

		if not pid:
			message = "pidfile %s does not exist. Daemon not running?\n"
			sys.stderr.write(message % self.pidfile)
			return # not an error in a restart

		# Try killing the daemon process
		try:
			message = "Stopped process \n"
			syslog.syslog(syslog.LOG_INFO,message)
			sys.stderr.write(message)

			while 1:
				os.kill(pid, SIGTERM)
				time.sleep(0.1)

		except OSError, err:
			if err.errno == 3:
				if os.path.exists(self.pidfile):
					os.remove(self.pidfile)
			else:
				print str(err)
				sys.exit(1)

	def restart(self):
		"""
		Restart the daemon
		"""
		self.stop()
		self.start()

	def run(self):
		"""
		You should override this method when you subclass Daemon. It will be called after the process has been
		daemonized by start() or restart().
		"""
<|endoftext|>


<commit_before>from collections import namedtuple

Network = namedtuple('Network', [
    'network_name',
    'network_shortname',
    'pubkeyhash',
    'wif_prefix',
    'scripthash',
    'magicbytes',
    'tx_timestamp',
    'denomination',
])

networks = (
    # Peercoin mainnet
    Network("Peercoin", "ppc", b'37', b'b7', b'75', b'e6e8e9e5', True, 1000000),
    # Peercoin testnet
    Network("Peercoin-testnet", "tppc", b'6f', b'ef', b'c4', b'cbf2c0ef', True, 1000000),
    # Bitcoin mainnet
    Network("Bitcoin", "btc", b'00', b'80', b'05', b'd9b4bef9', False, 100000000),
    # Bitcoin testnet
    Network("Bitcoin-testnet", "tbtc", b'6f', b'ef', b'c4', b'dab5bffa', False, 100000000)
)

def query(query):
    '''find matching parameter among the networks'''

    for network in networks:
        for field in network:
            if field == query:
                return network
<commit_msg>networks: add naming rules.<commit_after>from collections import namedtuple

Network = namedtuple('Network', [
    'network_name',
    'network_shortname',
    'pubkeyhash',
    'wif_prefix',
    'scripthash',
    'magicbytes',
    'tx_timestamp',
    'denomination',
])

'''
Network name should be capitalized, for testnet append "-testnet".
For abbreviation prefix testnet of the network with "t".
'''

networks = (
    # Peercoin mainnet
    Network("Peercoin", "ppc", b'37', b'b7', b'75', b'e6e8e9e5', True, 1000000),
    # Peercoin testnet
    Network("Peercoin-testnet", "tppc", b'6f', b'ef', b'c4', b'cbf2c0ef', True, 1000000),
    # Bitcoin mainnet
    Network("Bitcoin", "btc", b'00', b'80', b'05', b'd9b4bef9', False, 100000000),
    # Bitcoin testnet
    Network("Bitcoin-testnet", "tbtc", b'6f', b'ef', b'c4', b'dab5bffa', False, 100000000)
)

def query(query):
    '''find matching parameter among the networks'''

    for network in networks:
        for field in network:
            if field == query:
                return network
<|endoftext|>