def set_function(bot: Bot, update: Update):
    print(TAG(), "called set: ", update.message)
    chat_id = update.message.chat_id
    args = update.message.text.split(" ")
    if len(args) < 2:
        bot.send_message(chat_id=chat_id,
                         text="Invalid message. Command pattern is:")
        bot.send_message(chat_id=chat_id, text="/set name URL [CSS SELECTOR]")
        return

    name, url = args[1], args[2]
    watcher = Watcher(name, url, update)
    if len(args) > 3:
        watcher.selector = " ".join(args[3:])
        watcher.type = Selector.CSS
    else:
        watcher.type = Selector.NONE
    ok = watcher_manager.add_watcher(chat_id, watcher)
    if ok:
        bot.send_message(
            chat_id=chat_id,
            text="Notifier {0} correctly created! (SELECTOR: '{1}' ({2}))".
            format(watcher.name, watcher.selector, watcher.type))
        print("{0}: watcher {1} created.".format(chat_id, name))
    else:
        bot.send_message(
            chat_id=chat_id,
            text="Notifier {0} already exists. Please delete it".format(name))

    flush()
    def run_simulation(self):
        start_time = time.time()
        self.start_nodes()
        start_time = time.time() - start_time
        print 'Starting network with {0} nodes took: {1} secods'\
              .format(len(self.nodes),start_time)
  
        watcher_monitor = WatcherMonitor(self.limit)
        #Launch a thread which watches the output of the engines
        watcher_thread = Watcher(self.process_dict,self.config,watcher_monitor,self.persistent_table_dict)
        watcher_thread.start()
        
        #Do pre inputs in parallel
        self.input_pre()
       
        #Set the topology
        self.set_topology()

        self.start_evaluations()

        #Do post inputs
        self.input_post()

        time.sleep(2) #MAY NOT be necessary
        while not watcher_monitor.converged() and\
              not watcher_monitor.hit_limit():
            pass

        if watcher_monitor.hit_limit():
            print "HIT LIMIT"
        else:
            print "CONVERGENCE REACHED"
               
        self.node_states = watcher_thread.stop()
        self.lost_list, self.nr_received, self.nr_sent = lamport_transformation(self.node_states)

        f = open('output','w')
        for node in self.node_states:
            f.write("Instance {0} states".format(node))
            f.write("================================")
            for state in self.node_states[node]:
                f.write(state.__str__())
        f.close()

        self.make_xml()
        f = open(self.output,'w')
        f.write(self.result)
        f.close()
示例#3
0
    def __init__(self, api):
        self.quotes = []

        self.api = api
        self.watcher = Watcher(api)
        self.images = None

        self.init_paths()
        self.load_quotes()
        self.load_image_store()
示例#4
0
    def __init__(self, options):
        """Konstruktor

        options ist ein dictionary, das bisher nur die Parameter
        root_directory und main_priority gesetzt haben braucht. Das
        Verhalten für refresh gehört aber ebenfalls hier hinein."""
        self.__sub_indexers = generateSubindexers(options)
        self.__storage = Storage(options)
        options["cache"]["storage"] = self.__storage
        self.__cache = Cache(options) # self.__storage, self.__sub_indexers)
        options["watcher"]["root_directory"] = options["root_directory"]
        self.__watcher = Watcher(options)
示例#5
0
    def __init__(self, wyman_file):
        self.file_path = wyman_file
        self.watcher_obj = None
        self.poster_obj = None
        self.twitter_user = None
        self.web_hook = None
        self.text_enabled = True
        self.images_enabled = True
        # default is 10 minutes (600 seconds)
        self.check_delay = 600

        with open(self.file_path, "r") as opened:
            try:
                self.config = json.loads(opened.read())
            except json.JSONDecodeError:
                print("Invalid configuration file [{}]!", self.file_path)
                return

        if is_valid_user(self.config["twitter_handle"]):
            self.twitter_user = self.config["twitter_handle"]
        if is_valid_web_hook(self.config["webhook"]):
            self.web_hook = self.config["webhook"]

        self.watcher_obj = Watcher(self.twitter_user)
        self.poster_obj = Poster(self.watcher_obj, self.web_hook)

        if str(self.config["text_enabled"]).lower() == "false":
            self.poster_obj.disable_text()
        if str(self.config["images_enabled"]).lower() == "false":
            self.poster_obj.disable_images()
        if str(self.config["links_enabled"]).lower() == "false":
            self.poster_obj.disable_links()

        try:
            self.check_delay = int(self.config["check_delay"])
        except ValueError:
            print(
                "check_delay is not a number!\ndefaulting to 10 minute delay.")
    def run_simulation(self):
        start_time = time.time()
        self.start_nodes()
        node_start_time = time.time() - start_time
        print 'Starting network with {0} nodes took: {1} secods'\
              .format(len(self.nodes),node_start_time)
  
        watcher_monitor = WatcherMonitor(self.limit)
        #Launch a thread which watches the output of the engines
        watcher_thread = Watcher(self.process_dict,self.config,watcher_monitor,self.pers_table_dict,self.tran_table_dict)
        watcher_thread.start()
        
        #Do pre inputs in parallel
        self.input_pre()
       
        #Set the topology
        if not self.hardcoded_topology:
            self.set_topology()

            self.start_evaluations()

        #Do post inputs
        self.input_post()

        time.sleep(2) #MAY NOT be necessary
        while not watcher_monitor.converged() and\
              not watcher_monitor.hit_limit():
            pass

        if watcher_monitor.hit_limit():
            print "HIT LIMIT"
            self.hit_limit = True
        else:
            print "CONVERGENCE REACHED"
            self.hit_limit = False

        self.evaluations = watcher_monitor.evaluations
        self.node_states = watcher_thread.stop()
        lamport_start = time.time()
        self.lost_list, self.nr_received, self.nr_sent = lamport_transformation(self.node_states)
        lamport_duration = time.time() - lamport_start
        print "Lamport took {0} seconds".format(lamport_duration)
        try:
            percentage_lost = float((self.nr_sent - self.nr_received))/self.nr_sent
        except ZeroDivisionError:
            percentage_lost = 0

        f = open('output','w')
        for node in self.node_states:
            f.write("Instance {0} states".format(node))
            f.write("================================")
            for state in self.node_states[node]:
                f.write(state.__str__())
        f.close()

        write_to_file_start = time.time()
        self.simulation_time = time.time() - start_time
        self.make_xml()
        f = open(self.output,'w')
        f.write(self.result)
        f.close()
        write_duration = time.time() - write_to_file_start
        print "Write to file: {0}s".format(write_duration)

        size_of_file = os.path.getsize(self.output)

        time_taken = time.time() - start_time
        print "Took {0} seconds to run the whole simulation".format(time_taken)
        print start_time
        print time.time()
        

        time_taken = int(time_taken)
        percentage_lost = round(percentage_lost * 100,2)
        f = open('experiment_data', 'a')
        to_write = '{0} & {1} & {2} & {3}\n'.format(self.evaluations, time_taken, self.nr_sent, percentage_lost)
#to_write = 'STATE TRANSITIONS: {0},TOTAL TIME: {6}, SENT MESSAGES: {1}, PERCENTAGE LOST: {2}, LAMPORT TIME: {3}, WRITE TIME: {4}, WRITE SIZE: {5}\n'.format(self.evaluations, self.nr_sent, percentage_lost, lamport_duration, write_duration, size_of_file, time_taken)
        f.write(to_write)
        f.close()
示例#7
0
class Indexer:
    """Indexer-Basisklasse

    Diese Klasse verwaltet die gesammelten Informationen und die Indexer
    für im Dateibaum weiter unten liegende Verzeichnisse und stellt sie
    zur Verfügung."""
    def __init__(self, options):
        """Konstruktor

        options ist ein dictionary, das bisher nur die Parameter
        root_directory und main_priority gesetzt haben braucht. Das
        Verhalten für refresh gehört aber ebenfalls hier hinein."""
        self.__sub_indexers = generateSubindexers(options)
        self.__storage = Storage(options)
        options["cache"]["storage"] = self.__storage
        self.__cache = Cache(options) # self.__storage, self.__sub_indexers)
        options["watcher"]["root_directory"] = options["root_directory"]
        self.__watcher = Watcher(options)

    def __iter__(self):
        """Liefert einen Iterator auf die Sub-Indexer zurück."""
        return iter(self.__sub_indexers)

    def __cmp__(self, other):
        """Vergleichsfunktion, um das Sortieren zu vereinfachen"""
        return cmp(self.__priority, other.__priority)

    def refresh(self):
        res = self.__watcher.collect()
        self.__watcher.reset()

    def could_be(self, query):
        """Unscharfe Suchfunktion

        Diese Funktion soll anhand der Parameter überprüfen, ob eine genaue 
        Übereinstimmung überhaupt möglich ist (ohne die REs zu benutzen).
        Das ganze muss per Rekursion geschehen, um sinnvoll zu sein!
        Die Zwischenergebnisse müssen gecached werden, damit sie in der
        eigentlichen Suche wiederverwendet werden können."""
        return self.__cache.could_be(query)

    def find(self, query):
        return self.__cache.find(query)

    def __refresh_files(filelist):
        """Aktualisierungsfunktion für Dateien

        Aktualisiert alle Dateien in files. files ist ein Dictionary, das
        den Dateinamen (oder die Cachenummer) und die Änderungsart enthält."""
        self.__cache.refresh(filelist)
        for i in filelist:
            self.__handle (i)

    def __refresh_dirs(dirlist):
        """Aktualisierungsfunktion für Verzeichnisse"""
        for i in dirlist["deleted"]:
            del self.__subindexer[i]
        for i in dirlist["created"]:
            options = self.options
            options["root_directory"] = join (self.root_directory, i)
            self.__sub_indexers.append(Indexer(options))
        for i in dirlist["changed"]:
            self.__sub_indexers[i].refresh()

    def __handle(file):
        from mimetypes import guess_type
        type = guess_type(file)[0]
        if type in handler.keys():
            ## Tupel aus Standardelementen und dem Zusatz (ansonsten None)
            self.__storage.replace (file, (mtime, mod, handler[type]))
        else:
            self.__storage.replace (file, (mtime, mod, None))
示例#8
0
 def __init__(self, parent):
     Watcher.__init__(self, parent)
示例#9
0
def Main():
    if len(sys.argv) <= 1:
        print("Argument required: create, watch, buy, sell, stats")
    else:
        if sys.argv[1] == 'create':
            if len(sys.argv) < 3:
                print("Invalid arguments. `python main.py create 5000`")
            else:
                watcher = Watcher(filename='data/portfolio',
                                  hyperparameters=hyperparameters)
                watcher.create(balance=float(sys.argv[2]))

        elif sys.argv[1] == 'watch':
            watcher = Watcher(filename='data/portfolio',
                              hyperparameters=hyperparameters)
            watcher.start()

        elif sys.argv[1] == 'buy':
            # main.py buy 20 AMD 81.26
            if len(sys.argv) < 5:
                print("Invalid arguments. `python main.py buy 20 AMD 81.26`")
            else:
                executor = Executor(filename='data/portfolio',
                                    hyperparameters=hyperparameters)
                print(
                    executor.buy(symbol=sys.argv[3],
                                 count=int(sys.argv[2]),
                                 value=float(sys.argv[4])))

        elif sys.argv[1] == 'sell':
            # main.py sell 20 AMD 81.26
            if len(sys.argv) < 5:
                print("Invalid arguments. `python main.py sell 20 AMD 81.26`")
            else:
                executor = Executor(filename='data/portfolio',
                                    hyperparameters=hyperparameters)
                print(
                    executor.sell(symbol=sys.argv[3],
                                  count=int(sys.argv[2]),
                                  value=float(sys.argv[4])))

        elif sys.argv[1] == 'stats':
            watcher = Watcher(filename='data/portfolio',
                              hyperparameters=hyperparameters)
            watcher.stats()

        elif sys.argv[1] == 'sim':
            from Sim import Sim
            from Analysis import Analysis

            sim = Sim(period='3y',
                      timedelay=100,
                      window=100,
                      timestep=1,
                      budget=5000,
                      stockPicks=5,
                      avoidDowntrends=True,
                      sellAllOnCrash=False,
                      **hyperparameters)
            stats = sim.run()

            analysis = Analysis(stats=stats,
                                positions=sim.portfolio.holdings,
                                prices=sim.downloader.prices)
            analysis.chart('data/best_optimized_3y.png')
            output, advanced_stats = analysis.positionStats()
            print(output)

            g = sim.portfolio.holdings.copy().groupby('label').sum()
            g['profits_pct'] = (g['current_price'] - g['purchase_price']
                                ) / g['purchase_price'] * 100
            print(g)

        elif sys.argv[1] == 'check':
            watcher = Watcher(filename='data/portfolio',
                              hyperparameters=hyperparameters)
            watcher.check(sys.argv[2])

        else:
            print("Unknown command")
示例#10
0
import proxy
from Watcher import Watcher
import time
from random import randint

if __name__ == "__main__":
    while True:
        try:
            watchers_count = int(input("Number of bots: "))
            if watchers_count < 1:
                raise ValueError
            break
        except ValueError as e:
            print("Somár, číslo veď...")

    for _ in range(watchers_count):
        proxy.switchIP()
        watcher = Watcher()
        watcher.start()
        time.sleep(randint(5, 10))
 def __init__(self, parent):
     Watcher.__init__(self, parent)
示例#12
0
from Watcher import Watcher
import os

downloads_path = os.path.expanduser('~/Downloads')
# desktop_path = os.path.expanduser('~/Desktop')

# enable watcher
watcher = Watcher(downloads_path)
watcher.run()



示例#13
0
"""
@warning Watcher.py is not completely reusable right now!
This is a test script for development just to see if things are
indeed working
"""

from Watcher import Watcher
from docutils.core import publish_string


CONTENT_PATH = 'content'
OUTPUT_PATH = 'output'


def make(filename='', content_path='', output_path='', out_format='html'):
    def corresponding_output_path():
        return "{name}.{extension}".format(name=filename.replace(content_path, output_path, 1).split('.')[0],
                                           extension=out_format)

    with open(filename) as f:
        s = publish_string(f.read(), writer_name=out_format)

        with open(corresponding_output_path(), 'w') as w:
            w.write(s)


if __name__ == '__main__':
    rst_watcher = Watcher(CONTENT_PATH, OUTPUT_PATH, func=make)
    rst_watcher.activate_and_serve()