Ejemplo n.º 1
0
class AssertPrints(object):
    """Context manager for testing that code prints certain text.
    
    Examples
    --------
    >>> with AssertPrints("abc", suppress=False):
    ...     print("abcd")
    ...     print("def")
    ... 
    abcd
    def
    """
    def __init__(self, s, channel='stdout', suppress=True):
        self.s = s
        self.channel = channel
        self.suppress = suppress
    
    def __enter__(self):
        self.orig_stream = getattr(sys, self.channel)
        self.buffer = MyStringIO()
        self.tee = Tee(self.buffer, channel=self.channel)
        setattr(sys, self.channel, self.buffer if self.suppress else self.tee)
    
    def __exit__(self, etype, value, traceback):
        self.tee.flush()
        setattr(sys, self.channel, self.orig_stream)
        printed = self.buffer.getvalue()
        assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed)
        return False
Ejemplo n.º 2
0
class AssertPrints(object):
    """Context manager for testing that code prints certain text.
    
    Examples
    --------
    >>> with AssertPrints("abc", suppress=False):
    ...     print "abcd"
    ...     print "def"
    ... 
    abcd
    def
    """
    def __init__(self, s, channel='stdout', suppress=True):
        self.s = s
        if isinstance(self.s, py3compat.string_types):
            self.s = [self.s]
        self.channel = channel
        self.suppress = suppress
    
    def __enter__(self):
        self.orig_stream = getattr(sys, self.channel)
        self.buffer = MyStringIO()
        self.tee = Tee(self.buffer, channel=self.channel)
        setattr(sys, self.channel, self.buffer if self.suppress else self.tee)
    
    def __exit__(self, etype, value, traceback):
        if value is not None:
            # If an error was raised, don't check anything else
            return False
        self.tee.flush()
        setattr(sys, self.channel, self.orig_stream)
        printed = self.buffer.getvalue()
        for s in self.s:
            assert s in printed, notprinted_msg.format(s, self.channel, printed)
        return False
Ejemplo n.º 3
0
def test_tee_simple():
    "Very simple check with stdout only"
    chan = StringIO()
    text = 'Hello'
    tee = Tee(chan, channel='stdout')
    print(text, file=chan)
    nt.assert_equal(chan.getvalue(), text + "\n")
Ejemplo n.º 4
0
    def tchan(self, channel, check='close'):
        trap = StringIO()
        chan = StringIO()
        text = 'Hello'

        std_ori = getattr(sys, channel)
        setattr(sys, channel, trap)

        tee = Tee(chan, channel=channel)
        print(text, end='', file=chan)
        setattr(sys, channel, std_ori)
        trap_val = trap.getvalue()
        nt.assert_equal(chan.getvalue(), text)
        if check == 'close':
            tee.close()
        else:
            del tee
Ejemplo n.º 5
0
    def tchan(self, channel, check='close'):
        trap = StringIO()
        chan = StringIO()
        text = 'Hello'
        
        std_ori = getattr(sys, channel)
        setattr(sys, channel, trap)

        tee = Tee(chan, channel=channel)
        print(text, end='', file=chan)
        setattr(sys, channel, std_ori)
        trap_val = trap.getvalue()
        nt.assert_equals(chan.getvalue(), text)
        if check=='close':
            tee.close()
        else:
            del tee
Ejemplo n.º 6
0
class AssertPrints(object):
    """Context manager for testing that code prints certain text.

    Examples
    --------
    >>> with AssertPrints("abc", suppress=False):
    ...     print("abcd")
    ...     print("def")
    ...
    abcd
    def
    """
    def __init__(self, s, channel='stdout', suppress=True):
        self.s = s
        if isinstance(self.s, (str, _re_type)):
            self.s = [self.s]
        self.channel = channel
        self.suppress = suppress

    def __enter__(self):
        self.orig_stream = getattr(sys, self.channel)
        self.buffer = MyStringIO()
        self.tee = Tee(self.buffer, channel=self.channel)
        setattr(sys, self.channel, self.buffer if self.suppress else self.tee)

    def __exit__(self, etype, value, traceback):
        __tracebackhide__ = True

        try:
            if value is not None:
                # If an error was raised, don't check anything else
                return False
            self.tee.flush()
            setattr(sys, self.channel, self.orig_stream)
            printed = self.buffer.getvalue()
            for s in self.s:
                if isinstance(s, _re_type):
                    assert s.search(printed), notprinted_msg.format(
                        s.pattern, self.channel, printed)
                else:
                    assert s in printed, notprinted_msg.format(
                        s, self.channel, printed)
            return False
        finally:
            self.tee.close()
Ejemplo n.º 7
0
    def tchan(self, channel):
        trap = StringIO()
        chan = StringIO()
        text = 'Hello'
        
        std_ori = getattr(sys, channel)
        setattr(sys, channel, trap)

        tee = Tee(chan, channel=channel)

        print(text, end='', file=chan)
        trap_val = trap.getvalue()
        self.assertEqual(chan.getvalue(), text)

        tee.close()

        setattr(sys, channel, std_ori)
        assert getattr(sys, channel) == std_ori
Ejemplo n.º 8
0
def print_redirect(log_path: Union[Path, str], mode="a"):
    try:
        tee = Tee(log_path, mode=mode, channel="stdout")
        yield tee
    finally:
        tee.close()
Ejemplo n.º 9
0
 def __enter__(self):
     self.orig_stream = getattr(sys, self.channel)
     self.buffer = MyStringIO()
     self.tee = Tee(self.buffer, channel=self.channel)
     setattr(sys, self.channel, self.buffer if self.suppress else self.tee)
Ejemplo n.º 10
0
 def __enter__(self):
     self.orig_stream = getattr(sys, self.channel)
     self.buffer = MyStringIO()
     self.tee = Tee(self.buffer, channel=self.channel)
     setattr(sys, self.channel, self.buffer if self.suppress else self.tee)
Ejemplo n.º 11
0
 def _capture_on(self):
     self.orig_stdout = sys.stdout
     self.buffer = StringIO()
     self.tee = Tee(self.buffer, 'stdout')
     sys.stdout = self.buffer
Ejemplo n.º 12
0
class SlackCaster:

    token = attr.ib(repr=False)
    as_user = attr.ib(default=True)

    def __attrs_post_init__(self):
        self.orig_stdout = None
        self.sc = SlackClient(self.token)
        self.channel = None
        self.cell_input = None
        self.tp = ThreadPoolExecutor(1)
        self.history = {}

    def _capture_on(self):
        self.orig_stdout = sys.stdout
        self.buffer = StringIO()
        self.tee = Tee(self.buffer, 'stdout')
        sys.stdout = self.buffer

    def _capture_off(self):
        res = None

        if self.orig_stdout is not None:

            try:
                self.tee.flush()
                sys.stdout = self.orig_stdout
                self.orig_stdout = None

                res = self.buffer.getvalue()
            finally:
                self.tee.close()
                self.tee = None

        return res

    def _call_and_get(self, cmd, seqname, filter_key, result_key, kwargs,
                      value):
        results = self.sc.api_call(cmd, **kwargs)

        if results['ok']:
            for item in results[seqname]:
                if item[filter_key] == value:
                    return item[result_key]

    _get_channel_id = partialmethod(
        _call_and_get,
        cmd='channels.list',
        seqname='channels',
        filter_key='name',
        result_key='id',
        kwargs={'exclude_archived': 1},
    )

    _get_im_id = partialmethod(
        _call_and_get,
        cmd='im.list',
        seqname='ims',
        filter_key='name',
        result_key='id',
        kwargs={},
    )

    _get_user_id = partialmethod(
        _call_and_get,
        cmd='users.list',
        seqname='members',
        filter_key='name',
        result_key='id',
        kwargs={},
    )

    def _format_cell(self, cell):
        cell = ansi_escape.sub('', cell)
        return html.escape(f"```{cell}```", quote=False)

    def _send(self, channel, contents):
        res = self.sc.api_call(
            'chat.postMessage',
            channel=channel,
            text=self._format_cell(contents),
            as_user=self.as_user,
        )

        if not res['ok']:
            raise SlackAPIException(res['error'])

    def _send_cell(self, cell_input=None, cell_output=None):
        if self.channel is None:
            return

        if cell_input is not None:
            self.tp.submit(self._send, self.channel, cell_input)

        if cell_output is not None:
            if len(cell_output) > 0:
                self.tp.submit(self._send, self.channel, cell_output)

    def pre_run_cell(self, info):
        if not info.raw_cell.startswith('%slackcast'):
            self.cell_input = info.raw_cell
            self._capture_on()
        else:
            self.cell_input = None

    def _store_history(self, number, cell_input=None, cell_output=None):
        self.history[number] = (cell_input, cell_output)

    def post_run_cell(self, result):
        cell_output = self._capture_off()

        # Print output to console
        if type(cell_output) == str:
            sys.stdout.write(cell_output)

        # Send input and output cells to slack
        self._store_history(result.execution_count, self.cell_input,
                            cell_output)
        self._send_cell(self.cell_input, cell_output)

    def replay(self, channel, items):
        orig_channel = self.channel
        self.set_channel(channel)

        for idx in items:
            cell_input, cell_output = self.history[idx]
            self._send_cell(cell_input, cell_output)

        self.channel = orig_channel

    def set_channel(self, channel=None):
        # Go silent
        if channel is None:
            self.channel = None
            return

        chan_type = channel[0]
        name = channel[1:]
        channel_id = None

        if chan_type == '#':
            channel_id = self._get_channel_id(value=name)
        if chan_type == '@':
            channel_id = self._get_user_id(value=name)

        if channel_id is None:
            raise SlackcastException('Could not set channel to {channel}.')
        else:
            self.channel = channel_id

    def say(self, line):
        self._send_cell(line)
Ejemplo n.º 13
0
    df_for_cluster = util.pre_process(df)

    tree = find_decision_point(df_for_cluster)
    IM_tree = IM_tree.replace(" ", "")

    fig_tree.create_tree(tree, config.silhouette)
    sum_sil = 0.0
    max_row = 0
    for node in config.process_tree:
        if node.sil == None:
            continue
        parent_log_rows = node.parent.log_rows if node.parent != -1 else node.log_rows
        sum_sil += (node.sil * (node.log_rows / parent_log_rows))
        max_row = node.row if node.row > max_row else max_row
    total_sil = sum_sil / (max_row + 1)

    with closing(Tee('log_file.txt', "a", channel="stdout")) as outputstream:
        print("=============================================")
        print(time.strftime("%d/%m/%Y  %H:%M:%S"))
        print("{} \nsilhouette threshold: {}".format(
            config.data_file, config.silhouette_threshold))
        print('IM tree: ', IM_tree)
        print('tree: ', tree)
        print(config.explain_operator)
        print('sil: ', config.silhouette)
        print('total_sil: ', total_sil)

    fig_tree.create_tree_fig()

    # activity_set = set(df['activity'].unique())
    if path.exists(path1+"/States_SL.txt") == False:
        print('', file=open(path1+"/States_SL.txt", "w"))
    if path.exists(path1+"/States_TP.txt") == False:
        print('', file=open(path1+"/States_TP.txt", "w"))

States_Market=path1+dash+"States_Market.txt"
States_Order=path1+dash+"States_Order.txt"
States_Possible=path1+dash+"States_Possible.txt"
States_isOpen=path1+dash+"States_isOpen.txt"
States_SL=path1+dash+"States_SL.txt"
States_TP=path1+dash+"States_TP.txt"

if True:#change from if to while to always loop
 from IPython.utils.io import Tee
 from contextlib import closing
 with closing(Tee("outputfile.txt", "w", channel="stdout")) as outputstream:
  try: 
    api_key = '4-H0Cb9XAKoBfNmCDO-yFlMf' # your api key
    api_secret = 'LfpNu5iTL2UGgdknnbM0Do0Ws4edag8ckF70pb3UJNRkYkVM' # your api secret
    #whenever bitmex_cli is used, 1 api useage instance is tracked by bitmex.  Bitmex only allows 15 api calls per minute
    bitmex_cli = bitmex1(test=False, api_key=api_key, api_secret=api_secret)#this wrapper used due to supporting leverage and the simplicity of creating market/limit orders.  Also  closes orders.
    #creating dataframe that will be used to store candlestick data. 
    df=pd.DataFrame()
    timep= df.append({'time':1}, ignore_index=True)
    timep=timep.iloc[0:0]
    openp= df.append({'open':1}, ignore_index=True)
    openp=openp.iloc[0:0]
    highp= df.append({'high':2}, ignore_index=True)
    highp=highp.iloc[0:0]
    lowp= df.append({'low':3}, ignore_index=True)
    lowp=lowp.iloc[0:0]
Ejemplo n.º 15
0
                        help="Whether to show the GPU utilization on terminal")
    parser.add_argument("--reverse")

    args = parser.parse_args()
    exp_id = args.id
    tot_exps = args.tot_exps
    n_trials = args.trials
    gputil = args.print_gputil

    gpus = tf.config.experimental.list_physical_devices('GPU')

    if not os.path.exists('../terminal_logs'):
        os.makedirs('../terminal_logs')

    with closing(
            Tee(f"../terminal_logs/exp{exp_id}.txt", "w",
                channel="stderr")) as outputstream:
        # with closing(Tee(f"../terminal_logs/exp{exp_id}_new_epochs.txt", "w", channel="stderr")) as outputstream:
        if gputil:
            monitor = Monitor(30)
        # with StdoutTee(f"../terminal_logs/exp{exp_id}.txt"), StderrTee(f"../terminal_logs/exp{exp_id}_err.txt"):
        if gpus:
            try:
                # Currently, memory growth needs to be the same across GPUs
                for gpu in gpus:
                    tf.config.experimental.set_memory_growth(gpu, True)
                logical_gpus = tf.config.experimental.list_logical_devices(
                    'GPU')
                print(len(gpus), "Physical GPUs,", len(logical_gpus),
                      "Logical GPUs")
            except RuntimeError as e:
                # Memory growth must be set before GPUs have been initialized