Пример #1
0
 def __init__(self, filename=None, verbose=False, input={}, ddir=None, *args, **kw):
     LazyDict.__init__(self, input, *args, **kw)
     filename = filename or 'mainconfig'
     datadir = ddir or getdatadir()
     dir = datadir + os.sep + 'config'
     if datadir not in filename: cfile = dir + os.sep + filename
     else: cfile = filename
     logging.debug("config - filename is %s" % cfile)
     self.jsondb = None
     try: import waveapi ; self.isdb = True
     except ImportError: self.isdb = False
     if not self.comments: self.comments = {}
     try:
         try: self.fromfile(cfile)
         except IOError:
             logging.warn("can't read config from %s" % self.cfile) 
             import waveapi
             from persist import Persist
             self.jsondb = Persist(cfile)
             self.update(self.jsondb.data)
             self.isdb = True
             logging.debug("config - fromdb - %s - %s" % (self.cfile, str(self)))
     except ImportError:
         handle_exception()
         self.isdb = False
     self.cfile = cfile
     self.dir = dir
     self.filename = filename
     self.init()
     if not self.owner: self.owner = []
     if not self.uuid: self.uuid = str(uuid.uuid4())
Пример #2
0
 def __init__(self, datadir):
     Persist.__init__(self,
                      datadir + os.sep + 'fleet' + os.sep + 'fleet.main')
     if not self.data.has_key('names'): self.data['names'] = []
     if not self.data.has_key('types'): self.data['types'] = {}
     self.startok = threading.Event()
     self.bots = []
Пример #3
0
def clear(target):
    """ clear target's outputcache. """
    cache = Persist(getdatadir() + os.sep + 'run' + os.sep + 'outputcache' + os.sep + stripname(target))
    try:
        cache.data['msg'] = []
        cache.save()
    except KeyError: pass
    return []
Пример #4
0
 def __init__(self, ddir=None, filename=None):
     self.datadir = ddir or getdatadir()
     self.filename = filename or 'mainusers'
     self.udir = self.datadir + os.sep + self.filename
     Persist.__init__(self, self.udir)
     if not self.data: self.data = LazyDict()
     self.data.names = self.data.names or {}
     logging.warn("users object created from %s" % self.udir)
Пример #5
0
 def __init__(self, ddir=None, filename=None):
     self.datadir = ddir or getdatadir()
     self.filename = filename or 'mainusers'
     self.udir = self.datadir + os.sep + self.filename
     Persist.__init__(self, self.udir)
     if not self.data: self.data = LazyDict()
     self.data.names = self.data.names or {}
     logging.warn("users object created from %s" % self.udir)
Пример #6
0
 def save(self):
     """ save fleet data and call save on all the bots. """
     Persist.save(self)
     for i in self.bots:
         try:
             i.save()
         except Exception, ex:
             handle_exception()
Пример #7
0
 def todb(self):
     """ save config to database. """
     cp = dict(self)
     del cp['jsondb']
     if not self.jsondb:
         from jsb.lib.persist import Persist
         self.jsondb = Persist(self.cfile)
     self.jsondb.data = cp
     self.jsondb.save()
Пример #8
0
def clear(target):
    """ clear target's outputcache. """
    cache = Persist(getdatadir() + os.sep + 'run' + os.sep + 'outputcache' +
                    os.sep + stripname(target))
    try:
        cache.data['msg'] = []
        cache.save()
    except KeyError:
        pass
    return []
Пример #9
0
 def __init__(self, name, userhosts=[], perms=[], permits=[], status=[], email=[]):
     assert name
     name = stripname(name.lower())
     Persist.__init__(self, getdatadir() + os.sep + 'users' + os.sep + name)
     self.data.datadir = self.data.datadir or getdatadir()
     self.data.name = self.data.name or name
     self.data.userhosts = self.data.userhosts or userhosts
     self.data.perms = self.data.perms or perms
     self.data.permits = self.data.permits or permits
     self.data.status = self.data.status or status
     self.data.email = self.data.email or email
     self.state = UserState(name)
Пример #10
0
def add(target, txtlist):
    """ add list of txt to target entry. """
    logging.warn("outputcache - adding %s lines" % len(txtlist))
    t = []
    for item in txtlist:
        t.append("[%s] %s" % (hourmin(time.time()), item))
    cache = Persist(getdatadir() + os.sep + 'run' + os.sep + 'outputcache' + os.sep + stripname(target))
    d = cache.data
    if not d.has_key('msg'): d['msg'] = []
    d['msg'].extend(t)
    while len(d['msg']) > 10: d['msg'].pop(0)
    cache.save()
Пример #11
0
 def __init__(self, config, port, state=Follower):
     self.persist = Persist(port, reset=False)
     self.state = state(self)
     self.cluster = config["cluster"]
     self.address = config["address"]
     self.commit_index = 0
     self.last_applied = 0
     self.leader = None
     self.transport = None
     self.client_transport = None
     self.state_machine = {}
     self.state_file = "state_" + str(port)
     self.result = {}
Пример #12
0
class Pipeline:
    def __init__(self, modelname):
        self.modelname = modelname
        self.result_file = modelname + '.json'
        self.predictor = Predictor(self.modelname)
        self.persister = Persist('../test-images/results/' + self.result_file)
        self.result = []

    def predict(self, img_path):
        prediction_result = self.predictor.predict(img_path)
        self.result.append(prediction_result)

    def persist(self):
        self.persister.append_to_json(self.result)
Пример #13
0
def add(target, txtlist):
    """ add list of txt to target entry. """
    logging.warn("outputcache - adding %s lines" % len(txtlist))
    t = []
    for item in txtlist:
        t.append("[%s] %s" % (hourmin(time.time()), item))
    cache = Persist(getdatadir() + os.sep + 'run' + os.sep + 'outputcache' +
                    os.sep + stripname(target))
    d = cache.data
    if not d.has_key('msg'): d['msg'] = []
    d['msg'].extend(t)
    while len(d['msg']) > 10:
        d['msg'].pop(0)
    cache.save()
Пример #14
0
 def __init__(self, filename, verbose=False, input={}, ddir=None, nolog=False, *args, **kw):
     assert filename
     LazyDict.__init__(self, input, *args, **kw)
     self.origname = filename
     self.origdir = ddir or getdatadir()
     self.setcfile(ddir, filename)
     self.jsondb = None
     if not self._comments: self._comments = {}
     try:
         import waveapi
         self.isdb = True
         self.isgae = True
     except ImportError:
         self.isgae = False
         self.isdb = False
     dodb = False
     try:
         logging.info("fromfile - %s from %s" % (self.origname, whichmodule(2)))
         self.fromfile(self.cfile)
     except IOError, ex: handle_exception() ; dodb = True
     if dodb or (self.isgae and not "mainconfig" in filename):
         try:
             from persist import Persist
             self.jsondb = Persist(self.cfile)
             if self.jsondb: self.merge(self.jsondb.data)
             logging.warn("fromdb - %s" % self.cfile)
         except ImportError:
             logging.warn("can't read config from %s - %s" % (self.cfile, str(ex))) 
     self.init()
     if self.owner: logging.info("owner is %s" % self.owner)
     if not self.has_key("uuid"): self.setuuid()
     if not self.has_key("cfile"): self.cfile = self.setcfile(self.origdir, self.origname) 
     assert self.cfile
Пример #15
0
 def __init__(self, filename, verbose=False, input={}, ddir=None, *args, **kw):
     assert filename
     LazyDict.__init__(self, input, *args, **kw)
     self.filename = filename or 'mainconfig'
     self.datadir = ddir or getdatadir()
     self.dir = self.datadir + os.sep + 'config'
     self.cfile = self.dir + os.sep + self.filename
     logging.debug("filename is %s" % self.cfile)
     self.jsondb = None
     try: import waveapi ; self.isdb = True
     except ImportError: self.isdb = False
     if not self.comments: self.comments = {}
     try:
         try: self.fromfile(self.cfile)
         except IOError:
             logging.warn("can't read config from %s" % self.cfile) 
             import waveapi
             from persist import Persist
             self.jsondb = Persist(self.cfile)
             self.update(self.jsondb.data)
             self.isdb = True
             logging.debug("fromdb - %s - %s" % (self.cfile, str(self)))
     except ImportError:
         handle_exception()
         self.isdb = False
     self.init()
     self.datadir = ddir or getdatadir()
     self.dir = self.datadir + os.sep + 'config'
     self.cfile = self.dir + os.sep + self.filename
     if not self.owner: self.owner = []
     dosave = False
     if not self.uuid: self.uuid = str(uuid.uuid4()) ; dosave = True
     if not self.cfile: self.cfile = self.dir + os.sep + self.filename ; dosave = True
     if dosave: self.save()
     assert self.cfile
Пример #16
0
	def __init__(self):
		self.mode = Mode.MENU
		self.running = True
		pygame.init() 
		self.g = Graphics()
		self.algo_file = 'standard.algo'
		self.c = Cube()
		self.s = Stats()
		#self.algo = Imported_Algo(self.c, 'standard.algo')
		self.persist = Persist()
		self.reset_cube()
		self.batch = 0

		self.inputHandler = {
				'0' : self.scramble,
				#'1' : self.to_next_comment,
				'q': self.next_move, #c.rotate([algo.next_move()])
				'[': self.destroy_edges, #c.rotate(Scrambler.gen_edge_destroy().split(' '))
				']' : self.custom_scramble,
				'6': self.show_rules,
				'9' : self.load_std_algo,
				'x' : self.scramble_from_fst_result,
				'z' : self.solve_with_solver,
				'-' : self.list_results,
				's' : self.remove_results,
				'/' : self.load_result_state,
				'a' : self.step_one,
				'o' : self.step_to_end,
				'e' : self.step_slowly,
				}
Пример #17
0
class Stats():
	def __init__(self):
		self.nr_moves = 0
		self.nr_search_steps = 0
		self.current_nr_search_moves = 0
		self.persist = Persist()

	def reset(self):
		self.nr_moves = 0
		self.nr_search_steps = 0
		self.current_nr_search_moves = 0

	def save(self, data):
		chunk = { 'state' : data,
				'nr_moves' : self.nr_moves,
				'nr_search_steps' : self.nr_search_steps }
		self.persist.save(chunk)
Пример #18
0
 def __init__(self,
              name,
              userhosts=[],
              perms=[],
              permits=[],
              status=[],
              email=[]):
     assert name
     name = stripname(name.lower())
     Persist.__init__(self, getdatadir() + os.sep + 'users' + os.sep + name)
     self.data.datadir = self.data.datadir or getdatadir()
     self.data.name = self.data.name or name
     self.data.userhosts = self.data.userhosts or list(userhosts)
     self.data.perms = self.data.perms or list(perms)
     self.data.permits = self.data.permits or list(permits)
     self.data.status = self.data.status or list(status)
     self.data.email = self.data.email or list(email)
     self.state = UserState(name)
Пример #19
0
def get(target):
    """ get output for target. """
    cache = Persist(getdatadir() + os.sep + 'run' + os.sep + 'outputcache' +
                    os.sep + stripname(target))
    try:
        result = cache.data['msg']
        if result: return result
    except KeyError:
        pass
    return []
Пример #20
0
def main_init(exchange, grid_trader, persistence_file):
    # exchange.cancelAllOpen()

    logger.info("Building trade grids")
    grid_trader.build_new_grids()

    logger.info("Issuing trades on created grids")
    grid_trader.issue_trades()

    logger.info("Storing GridTrader to disk.")
    Persist(persistence_file).store(grid_trader)
Пример #21
0
	def run_algo(self,algo_name, scramble,save_last=False):
		self.persist = Persist()
		
		print "Starting run"
		solver = Solver()
		result = solver.solve(algo_name, scramble)
		print "Run complete, result:", result['success'], "total time:", result['time']

		self.persist.save_result(result)
		if save_last:	
			self.persist.save_latest_solve(result)
		return result['success']
Пример #22
0
def set(target, txtlist):
    """ set target entry to list. """
    cache = Persist(getdatadir() + os.sep + 'run' + os.sep + 'outputcache' +
                    os.sep + stripname(target))
    if not cache.data.has_key('msg'): cache.data['msg'] = []
    cache.data['msg'] = txtlist
    cache.save()
Пример #23
0
def setup():
    from feed import CategoryFeed
    from scheduler import Jas1Scheduler, SM2Scheduler
    from triage import CategoryReverseTriage
    from pack import CategoryPack
    from persist import Persist, replay
    ordered, qs = get_questions()
    feed = CategoryFeed(ordered)
    pack = CategoryPack(feed, CategoryReverseTriage(),
                        SM2Scheduler(base_interval=60 * 5), qs,
                        Persist("records/records.txt"))
    replay("records/records.txt", pack, qs)
    save(pack)
Пример #24
0
	def __init__(self):
		self.persist = Persist()
Пример #25
0
 def __init__(self, filename):
     Persist.__init__(self, filename, {})
     self.types = dict((i, type(j)) for i, j in self.data.iteritems())
Пример #26
0
   def __init__(self):
      if DBUS_OK:
         self.dbus_presence = TaskhatDBusPresence(self)

      self.persist = Persist('Default')
      self.window = gtk.Window()
      self.window.connect('delete_event', self.close)
      self.window.set_title('Taskhat')
      self.window.set_icon_name('gtg-panel')
      self.window.realize()

      box1 = gtk.VBox()
      box2 = gtk.HBox()
      box2.set_border_width(4)

      abox = gtk.HBox()
      image = gtk.Image()
      image.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_MENU)
      label = gtk.Label('Add')
      abox.pack_start(image, False, False, 0)
      abox.pack_start(label, False, False, 0)
      abox.show()
      image.show()
      label.show()

      abutton = self.abutton = gtk.ToolButton(abox, 'Add')
      abutton.set_sensitive(False)
      abutton.connect('clicked', self.entry_done)
      abutton.show()

      abox = gtk.HBox()
      image = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_IN)
      abox.pack_start(image, False, False, 0)
      abox.show()
      image.show()
      tool = gtk.ToolButton(abox, 'Tools')

      def documentation(*args):
         dialog = gtk.MessageDialog(parent=self.window, flags=gtk.DIALOG_MODAL)
         dialog.set_title('User Information')
         dialog.set_markup('A priority of <b>%s</b> denotes administrivia.\nA priority of <b>%s</b> denotes an important task.\nA priority of <b>%s</b> is for normal tasks.\nA priority of <b>%s</b> denotes a low priority task.\n\nOver time, Taskhat will learn how you like to classify your tasks.\n\nSee <b>taskhat/config.py</b> for more options.' % (Task.PRIORITY_ADMIN.name, Task.PRIORITY_HIGH.name, Task.PRIORITY_MEDIUM.name, Task.PRIORITY_LOW.name))
         button = gtk.Button(stock=gtk.STOCK_CLOSE)
         def dest(*args):
            dialog.destroy()
         button.connect('clicked', dest)
         button.show()
         dialog.action_area.pack_end(button)
         dialog.show()

      popup_menu = gtk.Menu()

      s = 'Edit Recurring Events'
      x = gtk.MenuItem(s)
      x.connect('activate', self.event_editor)
      x.show()
      popup_menu.append(x)

      s = 'Revert Changes'
      x = gtk.MenuItem(s)
      x.connect('activate', self.backup_restore)
      x.show()
      popup_menu.append(x)

      x = gtk.SeparatorMenuItem()
      x.show()
      popup_menu.append(x)

      s = 'Information'
      x = gtk.MenuItem(s)
      x.connect('activate', documentation)
      x.show()
      popup_menu.append(x)

      s = 'About'
      x = gtk.MenuItem(s)
      x.connect('activate', self.about_menu)
      x.show()
      popup_menu.append(x)

      popup_menu.attach_to_widget(tool, None)
      popup_menu.show()

      def tool_menu(widget):
         popup_menu.popup(None, None, None, 1, 0)

      tool.connect('clicked', tool_menu)

      entry = self.entry = gtk.Entry()
      entry.connect('changed', self.entry_changed)
      entry.connect('activate', self.entry_done)
      entry.set_width_chars(25)
      entry.show()

      scrolled_window = gtk.ScrolledWindow()
      scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
      scrolled_window.show()
      box3 = gtk.VBox()
      box3.show()
      ebox3 = self.ebox3 = gtk.EventBox()
      ebox3.add(box3)
      ebox3.show()
      scrolled_window.add_with_viewport(ebox3)

#      TaskGroup('Overdue', self.window, self.persist, (-TaskDate.FUTURE+1, -1))
      TaskGroup('Today', self.window, self.persist, (None, 0), events=CONFIG['show_recurring_events'])
      TaskGroup('Tomorrow', self.window, self.persist, (1, 1))
#      TaskGroup('Today &amp; Tomorrow', self.window, self.persist, (None, 1), events=CONFIG['show_recurring_events'])
      TaskGroup('Next few days', self.window, self.persist, (2, 5))
      TaskGroup('Next week', self.window, self.persist, (6, 14))
      TaskGroup('Future', self.window, self.persist, (8, None))
      for group in TaskGroup.groups:
         box3.pack_start(group, False, False)

      ebox3.modify_bg(gtk.STATE_NORMAL, self.window.get_style().base[gtk.STATE_NORMAL])

      status = self.status = gtk.Label(Taskhat.HELP_STRING)
      status.set_alignment(0,.5)
      status.set_ellipsize(pango.ELLIPSIZE_END)
      status.show()
      status.set_sensitive(False)
      box2.pack_start(status, True, True)
      sep = gtk.Label(' ')
      sep.show()
      box2.pack_start(sep, False, False)
      sep = gtk.VSeparator()
      sep.show()
      box2.pack_end(tool, False, False)
      box2.pack_end(sep, False, False)
      box2.pack_end(abutton, False, False)
      tool.show()
      box2.pack_end(entry, False, False)
      box1.pack_start(box2, False, False)
      box1.pack_start(scrolled_window)

      self.window.add(box1)
      self.window.set_size_request(530, 565)

      box2.show()
      box1.show()

      accelgroup = gtk.AccelGroup()
      self.window.add_accel_group(accelgroup)
      action = gtk.Action('Quit', '_Quit me!', 'Quit the Program', gtk.STOCK_QUIT)
      action.connect('activate', self.close)
      actiongroup = gtk.ActionGroup('BasicAction')
      actiongroup.add_action_with_accel(action, None)
      action.set_accel_group(accelgroup)
      menubar = gtk.MenuBar()
      menuitem = action.create_menu_item()
      menubar.append(menuitem)
      box1.pack_start(menubar, False)

      TaskGroup.origin = scrolled_window

      self.persist.restore(self.insert_task, self.update_events)
      self.persist.restore_geometry(self.window)
      persist.load_classifiers()
      self.window.show()
      entry.grab_focus()
      self.window.connect('notify::style', self.update_group_styles)
      self.window.connect('notify::is-active', self.save_geom)
 
      def _callback(data):
         self.clear_all()
         new = filter(lambda t: not t.removed, data.get('tasks', []))
         taskhat_write = data.get('last_taskhat_update', now())
         taskhat_write += datetime.timedelta(seconds=1)
         closed = set()
         for task in new:
            closed.add(repr(task))
         for task in self.persist.tasks:
            if task.last_updated > taskhat_write and repr(task) not in closed:
               new.append(task)
         self.persist.tasks = new
         for task in new:
            self.insert_task(task)
            task.last_updated = taskhat_write
         self.persist.events =\
            filter(lambda e: not e.deleted, data.get('events', []))
         self.persist.sync()
         self.update_events()
         return False

      def callback_gobject_wrapper(data):
         gobject.idle_add(lambda: _callback(data))

      file_api.watch(callback_gobject_wrapper)
Пример #27
0
 def save(self):
     Persist.save(self)
Пример #28
0
def main(
    account,
    cancel_all=False,
    init=False,
    monitor=False,
    balances=False,
    fetch_markets=False,
    set_balances=False,
    status_of='',
):
    command_line_args = locals()

    pretty_args, log_filename = prep_app(account, command_line_args)

    user_configo = configo.User(account)

    persistence_file = persistence_file_name(account)

    exchange = user_configo.make_exchangeo()

    display_session_info(pretty_args, exchange)
    grid_trader = GridTrader(exchange, user_configo, account)

    try:

        if cancel_all:
            exchange.cancelall()

        if init:
            logger.info("Initializing...")

            main_init(exchange, grid_trader, persistence_file)

        if monitor:
            logger.info("Evaluating trade activity since last invocation")
            persistence = Persist(persistence_file)
            grid_trader = persistence.retrieve()
            grid_trader.poll()
            persistence.store(grid_trader)

        if balances:
            logger.info("Getting balances")
            display_balances(exchange)

        if fetch_markets:
            logger.info("Fetching markets")
            m = exchange.fetchMarkets()
            print(m)

        if set_balances:
            logger.info("Setting balances")
            _set_balances(exchange, config_file, config)
            main_init(exchange, grid_trader, persistence_file)

        if status_of:
            logger.info("Getting status of order")
            # get_status_of(status_of)

    except Exception as e:
        error_msg = traceback.format_exc()
        logger.info('Aborting: %s', error_msg)
        grid_trader.notify_admin(error_msg)

    display_session_info(pretty_args, exchange, end=True)
Пример #29
0
class Raft:
    def __init__(self, config, port, state=Follower):
        self.persist = Persist(port, reset=False)
        self.state = state(self)
        self.cluster = config["cluster"]
        self.address = config["address"]
        self.commit_index = 0
        self.last_applied = 0
        self.leader = None
        self.transport = None
        self.client_transport = None
        self.state_machine = {}
        self.state_file = "state_" + str(port)
        self.result = {}

    # Server will change its role from follower, candidate and leader by its state
    def change_state(self, new_state):
        self.state.leave_state()
        self.state = new_state(self)

    # Message received between servers
    def receive_peer_message(self, peer, message):
        self.state.receive_peer_message(peer, message)

    # Message received by client
    def receive_client_message(self, message, transport):
        self.state.receive_client_message(message, transport)

    def send_peer_message(self, peer, message):
        json_msg = json.dumps(message)
        if peer != self.address:
            self.transport.sendto(json_msg.encode(), peer)

    def send_client_message(self, message, transport):
        json_msg = json.dumps(message)
        transport.write(json_msg.encode())

    # Send message from leader to followers
    def broadcast(self, message):
        for peer in self.cluster:
            self.send_peer_message(peer, message)

    # Increase term of server
    def increment_term(self):
        self.persist.increment_term()

    def get_current_term(self):
        return self.persist.get_current_term()

    def set_current_term(self, term):
        self.persist.set_current_term(term)

    # Voting
    def get_vote_for(self):
        return self.persist.get_vote_for()

    def set_vote_for(self, candidate):
        self.persist.set_vote_for(candidate)

    def get_leader(self):
        return self.leader

    def set_leader(self, new_leader):
        self.leader = new_leader

    def get_address(self):
        return self.address

    # get the index of recent log
    def get_last_log_index(self):
        return self.persist.get_last_log_index()

    # get the index of recent term
    def get_last_log_term(self):
        return self.persist.get_last_log_term()

    def get_cluster(self):
        return self.cluster

    def get_commit_index(self):
        return self.commit_index

    def set_commit_index(self, index):
        self.commit_index = index

    # Get the last command that state_machine has finished
    def get_last_applied(self):
        return self.last_applied

    def set_last_applied(self, applied):
        self.last_applied = applied

    # get the recent log term
    def get_log_term(self, index):
        return self.persist.get_log_term(index)

    def append_entries(self, index, entry):
        self.persist.append_entries(index, entry)

    # according to 'commit_index' and 'last_applied' index, apply commands in log entries to state machine.
    def apply_action(self, commit_index):

        logs = self.persist.data["log_manager"].log[self.last_applied:self.
                                                    commit_index]
        index = self.last_applied
        for log in logs:
            if log["command"] == "insert":
                self.state_machine[log["key"]] = log["value"]
                self.result[index] = "OK"
            elif log["command"] == "get":
                if log["key"] in self.state_machine:
                    self.result[index] = self.state_machine[log["key"]]
                else:
                    self.result[index] = "(nil)"
            elif log["command"] == "delete":
                if log["key"] in self.state_machine:
                    del self.state_machine[log["key"]]
                    self.result[index] = "(integer) 1"
                else:
                    self.result[index] = "(integer) 0"
            else:
                logger.error("Client command fatal error")
            index += 1

            self.last_applied += 1
        #write the contents of the state machine to disk, for checking consistency between servers.
        with open(self.state_file, 'w') as f:
            f.write(str(self.state_machine))
Пример #30
0
class Config(LazyDict):

    """ 
        config class is a dict containing json strings. is writable to file 
        and human editable.

    """

    def __init__(self, filename=None, verbose=False, input={}, ddir=None, *args, **kw):
        LazyDict.__init__(self, input, *args, **kw)
        filename = filename or 'mainconfig'
        datadir = ddir or getdatadir()
        dir = datadir + os.sep + 'config'
        if datadir not in filename: cfile = dir + os.sep + filename
        else: cfile = filename
        logging.debug("config - filename is %s" % cfile)
        self.jsondb = None
        try: import waveapi ; self.isdb = True
        except ImportError: self.isdb = False
        if not self.comments: self.comments = {}
        try:
            try: self.fromfile(cfile)
            except IOError:
                logging.warn("can't read config from %s" % self.cfile) 
                import waveapi
                from persist import Persist
                self.jsondb = Persist(cfile)
                self.update(self.jsondb.data)
                self.isdb = True
                logging.debug("config - fromdb - %s - %s" % (self.cfile, str(self)))
        except ImportError:
            handle_exception()
            self.isdb = False
        self.cfile = cfile
        self.dir = dir
        self.filename = filename
        self.init()
        if not self.owner: self.owner = []
        if not self.uuid: self.uuid = str(uuid.uuid4())

    def __deepcopy__(self, a):
        """ accessor function. """
        return Config(input=self)
         
    def __getitem__(self, item):
        """ accessor function. """
        if not self.has_key(item): return None
        else: return dict.__getitem__(self, item)

    def set(self, item, value):
        """ set item to value. """
        dict.__setitem__(self, item, value)

    def fromdb(self):
        """ read config from database. """
        from jsb.lib.persist import Persist
        logging.info("config - fromdb - %s" % self.cfile)
        tmp = Persist(self.cfile)
        self.update(tmp.data)

    def todb(self):
        """ save config to database. """
        cp = dict(self)
        del cp['jsondb']
        if not self.jsondb:
            from jsb.lib.persist import Persist
            self.jsondb = Persist(self.cfile)
        self.jsondb.data = cp
        self.jsondb.save()

    @savelocked
    def fromfile(self, filename):
        """ read config object from filename. """
        curline = ""
        fname = filename
        logging.debug("config - fromfile - %s" % fname)
        if not os.path.exists(fname): return False 
        comment = ""
        for line in open(fname, 'r'):
            curline = line
            curline = curline.strip()
            if curline == "": continue
            if curline.startswith('#'): comment = curline; continue
            if True:
                try:
                    key, value = curline.split('=', 1)
                    kkey = key.strip()
                    self[kkey] = json.loads(unicode(value.strip()))
                    if comment: self.comments[kkey] = comment 
                    comment = ""
                except ValueError: logging.warn("config - skipping line - unable to parse: %s" % line)
        self.cfile = fname
        return

    @savelocked
    def tofile(self, filename=None):
        """ save config object to file. """
        if not filename: filename = self.cfile
        try: from os import mkdir
        except ImportError:
            logging.debug("can't save %s to file .. os.mkdir() not suported" % filename)
            return
        logging.debug("config - saving %s" % filename)
        if filename.startswith(os.sep): d = [os.sep,]
        else: d = []
        for p in filename.split(os.sep)[:-1]:
            if not p: continue
            d.append(p)
            ddir = os.sep.join(d)
            if not os.path.isdir(ddir):
                logging.debug("persist - creating %s dir" % ddir)
                try: os.mkdir(ddir)
                except OSError, ex:
                    logging.warn("persist - not saving - failed to make %s - %s" % (ddir, str(ex)))
                    return
        written = []
        curitem = None
        try:
            configtmp = open(filename + '.tmp', 'w')
            teller = 0
            keywords = self.keys()
            keywords.sort()
            for keyword in keywords:
                value = self[keyword]
                if keyword in written: continue
                if keyword == 'name': continue
                if keyword == 'createdfrom': continue
                if keyword == 'cfile': continue
                if keyword == 'filename': continue
                if keyword == 'dir': continue
                if keyword == 'jsondb': continue
                if keyword == 'isdb': continue
                if keyword == 'optionslist': continue
                if keyword == 'gatekeeper': continue
                if keyword == "comments": continue
                if self.comments and self.comments.has_key(keyword):
                    configtmp.write(self.comments[keyword] + u"\n")
                curitem = keyword
                try: configtmp.write('%s = %s\n' % (keyword, json.dumps(value)))
                except TypeError: logging.error("config - %s - can't serialize %s" % (filename, keyword)) ; continue
                teller += 1
                configtmp.write("\n")
            configtmp.close()
            os.rename(filename + '.tmp', filename)
            return teller

        except Exception, ex:
            handle_exception()
            print "ERROR WRITING %s CONFIG FILE: %s .. %s" % (self.cfile, str(ex), curitem)
Пример #31
0
 def save(self):
     """ save fleet data and call save on all the bots. """
     Persist.save(self)
     for i in self.bots:
         try: i.save()
         except Exception, ex: handle_exception()
Пример #32
0
def search(queryString):
  indexed_data = Persist("indexed_data").load()
  urls = Persist('url_list').load()
  pg_values = Persist('page_rank').load()

  vectorizer = Persist("vectorizer").load()
  queryVector = vectorizer.transform([queryString])[0]
  scores = indexed_data.dot(queryVector.T)

  index = []
  relevant_data = []
  page_rank = []
  for e,s in enumerate(scores):
    if s > 0.03:
      index.append(e)
      page_rank.append(pg_values[urls[e]])
  print 'score done'
  data = indexed_data[index].todense()
  print 'relavent document size',data.shape

  label_raw = reader.loadJL('/tmp2/ir_labeled_data.jl')
  ldata = [util.get_searchable(item) for item in label_raw];
  label = [item['level'] for item in label_raw]
  seed = ['','','']
  for i in xrange(len(label)):
    ldata[i] = replace(ldata[i])
    if label[i] == 'Advanced':
      seed[2] += ldata[i]
    elif label[i] == 'Intermediate':
      seed[1] += ldata[i]
    elif label[i] == 'Beginner':
      seed[0] += ldata[i]
    else:
      print label[i],'Invalid......'
    if i %100==0:
      print label[i]

  seed = vectorizer.transform(seed)
  seed = seed.todense()

  kmeans = KMeans(n_clusters=3,init=seed)

  kmeans.fit(data)
  label = kmeans.predict(data)
  print 'KMeans done'

  data = scipy.sparse.csr_matrix(data)
  subdata = []
  sub_origin_index = [[],[],[]]
  sub_query_index = [[],[],[]]

  level = [[],[],[]]

  for i in xrange(data.shape[0]):
    sub_query_index[label[i]].append(i)
    sub_origin_index[label[i]].append(index[i])

    ################################################################
    # use page rank
    ################################################################

    level[label[i]].append((index[i],page_rank[i]))

  for i in xrange(3):
    subdata = data[sub_query_index[i]]
    print 'relavent # of',i,':',len(sub_query_index[i])
    result = subdata.dot(queryVector.T)

    ##############################################################
    #for e,r in enumerate(result):
    #  level[i].append((sub_origin_index[i][e],r))
    ##############################################################

    level[i] = sorted(level[i],key=lambda x:-x[1])
    print 'tutorial level',i+1
    for j in xrange(min(10,len(level[i]))):
      print 'document',level[i][j][0],urls[level[i][j][0]],level[i][j][1]

  exit(1)
  scores = [[],[],[]]

  for i in xrange(3):
    scores[i] = 1
    scores[i] = sorted(scores, key=lambda x:-x[1])

  return scores
Пример #33
0
class Solver():
	def __init__(self):
		self.persist = Persist()
		pass

	def solve(self, algo_name, scramble):
		c = Cube()	
		
		dbalgo = self.persist.get_algo(algo_name)

		algo = Imported_Algo(c, dbalgo)

		move_cnt = 0	
		solution = []
		print scramble
		for move in scramble.split(' '):
			c.rotate([move])

		done = False
		success = False
		t0 = time.time()
		while True:

			fst = True
			while len(algo.queued_moves) > 0 or fst:
				fst = False
				move = algo.next_move()
				if move == 'fail':
					done = True

				if move != None and move != ' ' and move != '' and move != 'fail':
					move_cnt += 1
					c.rotate([move])
					solution.append(move)
					
			if len(algo.algo_steps) == 0:
				done = True
				success = True

			else:	
				step = algo.algo_steps[0]

				if step['type'] == 'comment':
					print "Comment:", step['value']
					if step['value'] == "done":
							
						done = True
						success = True
			
			if done:
				break
			#next step in algo		
			res = algo.parse_algo()

		result = {'scramble' : scramble,
			'move_cnt' : move_cnt,
			'success' : success,
			'rules' : algo.rules,
			'moves' : solution,
			'name' : algo_name,
			'date' : datetime.now(),
			'time' : time.time() - t0,
			'search_cnt' : algo.search_cnt,
			'rnd' : random.randrange(0,1000000000000)}
	
		return result
Пример #34
0
 def __init__(self, filename):
     Persist.__init__(self, filename)
     if not self.data:
         self.data = {}
Пример #35
0
class Runner():
	def __init__(self):
		self.persist = Persist()


	def menu(self):
		while True:
			print "Enter command: "
			s = raw_input()
			s = s.split(" ")
			if s[0] == 'help':
				print ""
				print "HELP:"
				print "run algo random/db scramble_type"
				print "add filename"
				print "dump algoname"
				print "list"
				print "show algoname"
				print "rm algoname"
				print "rmstats algoname"
				print "stats algoname"
				print "addsc type count"
				print "listsc type"
				print "branch algoname tries (algotype)"
				print "rmsc (type)"
				print "catrun category scramble_type" 
				print "catstats category"
				print "import filename"
				print "failshow"
				print "removeallresult"
				print "failselect id"
				print ""

			if s[0] == 'run':
				name = s[1]
				mode = s[2]
				scramble_type = ''
				if len (s) >= 4:
					scramble_type = s[3]
				self.run_algo_command(name,mode,scramble_type,scramble_type)

			if s[0] == 'add':
				algo = Algo_Parser.parse_algo(s[1])
				self.persist.add_algo(algo)

			if s[0] == 'list':
				algos = self.persist.list_algos()
				print ""
				print "Available algos:"
				for a in algos:
					name = a['name']
					while len(name) < 12:
						name = name + " "
					print name, 'category:', a['category']
				print ""	
			if s[0] == 'show':
				res = self.persist.get_algo(s[1])
				print ""
				print "Algo ", s[1], "described:"
				for key, value in res.items():
					print key,":",value
					if str(key) == 'steps':
						for item in value:
							print map(str, item)
				print ""

			if s[0] == 'rm':
				self.persist.remove_algo(s[1])

			if s[0] == 'rmstats':
				name = s[1]
				self.persist.remove_results(name)

			if s[0] == 'stats':
				name = s[1]
				self.stats(name)

			if s[0] == 'addsc':
				scramble_type = s[1]
				cnt = int(s[2])
				for i in range(0,cnt):
					self.persist.add_scramble({'scramble_type' : scramble_type,
								'scramble' : Scrambler.gen_scramble()})
			if s[0] == 'listsc':
				scramble_type = 'full'
				if len(s) > 1:
					scramble_type = s[1]
				#for s in self.persist.get_scrambles(scramble_type=scramble_type):
				for scram in self.persist.get_scrambles():
					print scram
			if s[0] == 'rmsc':
				sctype = None
				if len(s) > 1:
					sctype = s[1]
				self.persist.remove_scrambles(scramble_type = sctype)
			if s[0] == 'col':
				print self.persist.db.collection_names()
				#self.persist.db.problem_state.drop()

			if s[0] == 'branch':
				name = s[1]
				scramble_type = 'full'
				cnt = 1
				if len(s) > 2:
					cnt = int(s[2])
				if len(s) > 3:
					scramble_type = s[3]
				self.branch(name, cnt, scramble_type)
			if s[0] == 'catrun':
				if len(s) < 3:
					print "too few arguments"
					return
				category = s[1]
				scramble_type = s[2]
				algos = self.persist.find_algo_category(category)
				for algo in algos:
					if algo['category'] == category:
						print "Running for ", algo['name']
						self.run_algo_command(algo['name'], 'db',0, scramble_type)
			if s[0] == 'catstats':
				category = s[1]
				algos = self.persist.find_algo_category(category)
				print ""
				print "Stats for category", category
				print "============================================="
				for algo in algos:
					self.stats(algo['name'])
				print ""
			if s[0] == 't':
				for rec in self.persist.db.algo.find():
					print rec
					if rec['name'] == 'standard':
						self.persist.db.algo.remove({'name' : rec['name']})
						rec['category'] = 'full'
						self.persist.db.algo.save(rec)

			if s[0] == 'rename':
				algo = self.persist.get_algo(s[1])
				algo['name'] = s[2]
				self.persist.remove_algo(s[1])
				self.persist.add_algo(algo)

			if s[0] == 'setcat':
				algo = self.persist.get_algo(s[1])
				algo['category'] = s[2]
				self.persist.remove_algo(s[1])
				self.persist.add_algo(algo)

			if s[0] == 'dump':
				algo = self.persist.get_algo(s[1])
				algo.pop("_id", None)
				f = open(s[1] + '-dump.algo', 'w')
				json.dump(algo, f)
				f.close()
			
			if s[0] == 'import':
				f = open(s[1], 'r')
				algo = {}
				for line in f:
					split = line[:-1].split('#')
					algo[split[0]] = split[1]
					print line
				f.close()
				self.persist.add_algo(algo)

			if s[0] == 'failshow':
				fails = self.persist.result.find({'success' : False})
				for nr, fail in enumerate(fails):
					if nr >= 10:
						break
					print "============================ ITEM", nr,"==============================="
					print "algoname:",fail['name']
	
			if s[0] == 'failselect':
				chosen_nr = int(s[1])
				fails = self.persist.result.find({'success' : False})
				success = False
				for nr, fail in enumerate(fails):
					if nr == chosen_nr:
						self.persist.save_latest_solve(fail)
						success = True
				if success:
					print "Done"
				else:
					print "Unknown id"

			if s[0] == 'removeallresult':
				self.persist.result.remove()

			if s[0] == 'export':
				algoname = s[1]
				res = self.persist.result.find({'name' : algoname})
				min_cnt = 1000000
				max_cnt = 0
				tot = 0
				result_cnt = 0.0
				out = ""
				fail = 0
				move_cnt_array = []
				for r in res:
					result_cnt += 1
					if not r['success']:
						fail += 1
						continue
					move_cnt = r['move_cnt']
					min_cnt = min(min_cnt, move_cnt)
					max_cnt = max(max_cnt, move_cnt)
					tot += move_cnt
					move_cnt_array.append(move_cnt)

				bar_chart = [0] * (max_cnt/5 + 1)
				for move in move_cnt_array:
					bar_chart[move/5] += 1
				
				fst_label = 0
				while(bar_chart[0] == 0):
					fst_label += 5
					bar_chart = bar_chart[1:]

				print bar_chart
				value = ""
				for b in bar_chart:
					value += str(fst_label) + '-' + str(fst_label + 4) + "," + str(b) + '\n'
					fst_label += 5
				value = value[:-1]
				fout = open('stats.csv','w')
				fout.write(value)
				fout.close()
				print 'total problem instances:', result_cnt
				print 'fail count:', fail
				print 'min:', min_cnt
				print 'max:', max_cnt
				print 'mean:', tot/result_cnt
				print 'successrate:', 1- fail/result_cnt
	def stats(self, name):
		res = self.get_algo_result(name)
		success_cnt = res['success_cnt']
		tot_cnt = res['tot_cnt']
		tot_success_moves = res['tot_success_moves']
		search_cnt = res['search_cnt']

		if success_cnt > 0:
			print "Total stats for ", name,":"
			print "==========================="
			print "Succes %:",(success_cnt)/(tot_cnt+0.0)*100.0, "(",success_cnt,"/",tot_cnt,")"
			print "Avg moves:",tot_success_moves/success_cnt
			print "Search count:", search_cnt
			print "Avg time:", res['avg_time']
			print ""
		else:
			print "No successful data for ", name
	def branch(self, name, cnt, scramble_type):

		for i in range(0, cnt):
			limit = random.uniform(0.1, 0.5)
			keep_limit = random.uniform(0.0, 0.20)
			original = self.persist.get_algo(name)
			print "Original steps:"
			new_name = ''.join(random.choice(string.ascii_lowercase) for x in range(5))
			steps = []
			for line in original['steps']:
				if str(line[0]) == 'set_moves':
					cmds = ""
					#try adding some stuff:
					cmd_list = []
					for move in line[1].split(","):
						if random.random() > keep_limit:
							cmd_list.append(move)#  += "," + move

					cands = 'r,f,l,u,d,b,U,D,R,F,L,B,rp,fp,lp,up,dp,bp,Dp,r2,f2,l2,u2,d2,b2,U2,D2,R2,F2,L2,B2,r U rp,rp U r, b U bp,bp U b,fp U f,F,R,L,B,Fp,Rp,Lp,Bp'
					for move in cands.split(','):
						if random.random() > limit and move not in cmd_list:
							cmd_list.append(move)
					random.shuffle(cmd_list)	
					for c in cmd_list:
						cmds += "," + c

					if len(cmds) > 0:
						steps.append([line[0], cmds[1:]])
				else:
					steps.append(line)

			algo = { 'name' : new_name,
					'category' : original['category'],
					'steps' : steps }
			print 'new_algo:', algo
			self.persist.add_algo(algo)

			#run the algo on type full
			success_rate = self.run_algo_command(new_name,'db',0,scramble_type, break_early=True)
			
			if success_rate < 0.80:#remove everything that fails more than some %
				self.persist.remove_algo(new_name)
				print "removing algo",new_name,"since successrate is too low"
			print "success rate is", success_rate

	def run_algo(self,algo_name, scramble,save_last=False):
		self.persist = Persist()
		
		print "Starting run"
		solver = Solver()
		result = solver.solve(algo_name, scramble)
		print "Run complete, result:", result['success'], "total time:", result['time']

		self.persist.save_result(result)
		if save_last:	
			self.persist.save_latest_solve(result)
		return result['success']

	def run_algo_command(self, name, mode, num_times, scramble_type, break_early=False):
		scrambles = []
		print scramble_type
		if mode == 'random':
			num_times = 1
			for i in range(0,num_times):
				scrambles.append(Scrambler.gen_scramble())
		if mode == 'db':
			tmp = self.persist.get_scrambles( scramble_type)
			for scr in tmp:
				scram = scr['scramble']
				scrambles.append(scram)
		if mode == 'last':
			res = self.persist.get_latest_solve()
			scrambles.append(res['scramble'])
		print "scram len:", len(scrambles)
		success_cnt = 0.0
		fail_cnt = 0.0
		for scram in scrambles:
			if self.run_algo(name, scram, save_last=mode == 'last'):
				success_cnt += 1
			else:
				fail_cnt += 1
			if break_early and fail_cnt / len(scrambles) > 1 - REQ_SUCCESS + 0.01: #epsilon
				return 0
		if success_cnt == 0:
			return 0.0
		success_rate = (success_cnt)/(success_cnt + fail_cnt)
		return success_rate

	def get_algo_result(self, name):
		algos = self.persist.list_results(name=name)
		success_cnt = 0
		fail_cnt = 0
		tot_success_moves = 0
		search_cnt = 0
		tot_time = 0
		for algo in algos:
			if algo['success']:
				success_cnt += 1
				tot_success_moves += algo['move_cnt']
				search_cnt += algo['search_cnt']
				tot_time += algo['time']
			else:
				fail_cnt += 1

		tot_cnt = success_cnt + fail_cnt
		avg_time = 0
		if tot_cnt > 0:
			search_cnt /= tot_cnt
			avg_time = tot_time / tot_cnt


		ret = {'success_cnt' : success_cnt,
				'fail_cnt' : fail_cnt,
				'tot_success_moves' : tot_success_moves,
				'search_cnt' : search_cnt,
				'tot_cnt' : tot_cnt,
				'avg_time' : avg_time}
		return ret
Пример #36
0
class Simulation():
	def __init__(self):
		self.mode = Mode.MENU
		self.running = True
		pygame.init() 
		self.g = Graphics()
		self.algo_file = 'standard.algo'
		self.c = Cube()
		self.s = Stats()
		#self.algo = Imported_Algo(self.c, 'standard.algo')
		self.persist = Persist()
		self.reset_cube()
		self.batch = 0

		self.inputHandler = {
				'0' : self.scramble,
				#'1' : self.to_next_comment,
				'q': self.next_move, #c.rotate([algo.next_move()])
				'[': self.destroy_edges, #c.rotate(Scrambler.gen_edge_destroy().split(' '))
				']' : self.custom_scramble,
				'6': self.show_rules,
				'9' : self.load_std_algo,
				'x' : self.scramble_from_fst_result,
				'z' : self.solve_with_solver,
				'-' : self.list_results,
				's' : self.remove_results,
				'/' : self.load_result_state,
				'a' : self.step_one,
				'o' : self.step_to_end,
				'e' : self.step_slowly,
				}

	def step_slowly(self):
		self.step_to_end(delay=0.1)
	
	def step_to_end(self, delay=0):
		while len(self.move_queue) > 0:
			n = self.move_queue.popleft()
			self.c.rotate([n])
			if delay > 0:
				self.g.draw_cube(self.c)
				time.sleep(delay)
			time.sleep(0.001)

		self.g.draw_cube(self.c)

	def step_one(self):
		if self.move_queue == None or len(self.move_queue) == 0:
			return
		n = self.move_queue.popleft()
		self.c.rotate([n])
		self.g.draw_cube(self.c)
		print "move:", n
		print "new queue:", self.move_queue

	def scramble_from_fst_result(self):
		res = self.persist.get_latest_solve()
		#self.persist.get_result_by_id(
		print "Final rules:"
		for rule in res['rules']:
			print rule['color'], ":", rule
		scram = res['scramble']
		print "Scramble:", scram
		for m in scram.split(' '):
			self.c.rotate([m])
		p = map(str,res['scramble'])
		s = ""
		for i in p:
			s += i + ","
		self.move_queue = deque()
		moves = map(str, res['moves'])
		print "Moves", moves
		for m in moves:
			self.move_queue.append(m)

	def scramble_from_fst_problem(self):
		prob = self.s.persist.get_first_problem()
		self.scramble(map(str, prob['scramble']))


	def increment_batch(self):
		self.scramble()
		self.batch += BATCH_SIZE

	def show_rules(self):
		print self.algo.rules

	def load_result_state(self):
		res = self.persist.list_results()[0]
		moves = res['scramble'].split(" ")
		moves.extend(map(str,res['moves']))
		for m in moves:
			self.c.rotate([m])

	def solve_with_solver(self):
		solver = Solver()
		scramble = Scrambler.gen_scramble()
		result = solver.solve('cen', scramble)
		self.c.rotate(result['scramble'].split(" "))
		for m in result['moves']:
			print m
			self.c.rotate([m])
			self.g.draw_cube(self.c, self.algo, self.s)
			event = pygame.event.wait()
		
		self.persist.save_result(result)
	
	def menu(self):
		while self.mode == Mode.MENU:
			self.g.draw_menu()
			event = pygame.event.wait()

			if event.type == pygame.KEYDOWN:
				self.mode = Mode.SIMULATION
	def recalc(self):
		self.algo.load_state(self.s.persist)

	def custom_scramble(self):
		parity_scramble = "F r r B B U U l U U rp U U r U U F F r F F lp B B r r Fp"
		scramble_3x3 = Scrambler.gen_3x3x3_scramble()
		wr_scramble = "L2 Dp U L R B L R D D U B B Fp U Lp Rp Bp Fp D F Lp B B U U Rp Bp F F"
		scramble = wr_scramble
		self.scramble(scramble)

	def show_queued_moves(self):
		print self.algo.queued_moves

	def next_move(self):
		print 'move:', self.algo.next_move()
		self.c.rotate([self.algo.next_move()])

	def destroy_edges(self):
		self.c.rotate(Scrambler.gen_edge_destroy().split(' '))

	def list_results(self):
		res = self.persist.list_results()
		for r in res:
			print r

	def save_result(self, res):
		self.persist.save_result(res)

	def remove_results(self, all=False):
		self.persist.remove_results(all=True)

	def list_problems(self):
		self.s.persist.list_problems()

	def load_3x3_algo(self):
		self.algo_file = '3x3.algo'
		self.reset_cube()

	def load_edge_algo(self):
		self.algo_file = 'edge.algo'
		self.reset_cube()

	def load_std_algo(self):
		self.algo_file = 'standard.algo'
		self.reset_cube()

	def load_state(self):
		self.algo.load_state(self.s.persist)

	def scramble(self, scramble_seq=None):
		if(scramble_seq == None):
			scramble_seq = Scrambler.gen_scramble().split(' ')
		self.c.rotate(scramble_seq)
		self.c.scramble = scramble_seq 

	def init_menu(self):
		pass

	#Main application loop
	def loop(self):

		prim = False
		w = False

		font = pygame.font.SysFont("monospace", 15)
		run_to_comment = False
		self.reset = False
	
		self.init_menu()
	
		#handle input and draw graphics
		while self.running: 
			s = self.s
			c = self.c
			g = self.g
			#algo = self.algo
			if self.reset:
				print self.reset, " is the reset state"
				self.reset_cube()
				self.reset = False
				self.scramble()
		
			if self.batch > 0 and not pygame.event.peek(pygame.KEYDOWN):
				print algo.rules
				self.to_next_comment()
				continue

			g.draw_cube(c)
			keymap = {}
			event = pygame.event.wait()

			if event.type == pygame.KEYDOWN:
				#handles dvorak
				keymap[event.scancode] = event.unicode
				cmd = event.unicode
				if event.unicode in self.inputHandler:
					self.inputHandler[event.unicode]()
				elif event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
					self.running = False
				elif event.unicode == '?':
					print "Commands: \n", self.inputHandler
				elif event.unicode.upper() in Turns:	
					c.rotate([cmd])
				else:
					print "No mapping for key: ", event.unicode


				#if w:
					#cmd = cmd.upper()
					#cmd += 'w'

				#if(prim):
					#cmd += 'p'

				#if event.unicode == 'y':
					#prim = not prim

				#if event.unicode == 'w':
					#w = not w
			pygame.event.clear()

		pygame.quit()

	def reset_cube(self):
		self.c = Cube()
		self.s.reset()

		steps = deque()
Пример #37
0
 def __init__(self, filename):
     Persist.__init__(self, filename)
     self.types = dict((i, type(j)) for i, j in self.data.iteritems())
Пример #38
0
 def __init__(self, modelname):
     self.modelname = modelname
     self.result_file = modelname + '.json'
     self.predictor = Predictor(self.modelname)
     self.persister = Persist('../test-images/results/' + self.result_file)
     self.result = []
Пример #39
0
 def __init__(self, ddir=None, filename=None):
     self.datadir = ddir or getdatadir()
     self.filename = filename or 'mainusers'
     Persist.__init__(self, self.datadir + os.sep + self.filename)
     if not self.data: self.data = LazyDict()
     self.data.names = self.data.names or {}
Пример #40
0
class Taskhat:

   HELP_STRING = ' Enter a new task:'

   def __init__(self):
      if DBUS_OK:
         self.dbus_presence = TaskhatDBusPresence(self)

      self.persist = Persist('Default')
      self.window = gtk.Window()
      self.window.connect('delete_event', self.close)
      self.window.set_title('Taskhat')
      self.window.set_icon_name('gtg-panel')
      self.window.realize()

      box1 = gtk.VBox()
      box2 = gtk.HBox()
      box2.set_border_width(4)

      abox = gtk.HBox()
      image = gtk.Image()
      image.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_MENU)
      label = gtk.Label('Add')
      abox.pack_start(image, False, False, 0)
      abox.pack_start(label, False, False, 0)
      abox.show()
      image.show()
      label.show()

      abutton = self.abutton = gtk.ToolButton(abox, 'Add')
      abutton.set_sensitive(False)
      abutton.connect('clicked', self.entry_done)
      abutton.show()

      abox = gtk.HBox()
      image = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_IN)
      abox.pack_start(image, False, False, 0)
      abox.show()
      image.show()
      tool = gtk.ToolButton(abox, 'Tools')

      def documentation(*args):
         dialog = gtk.MessageDialog(parent=self.window, flags=gtk.DIALOG_MODAL)
         dialog.set_title('User Information')
         dialog.set_markup('A priority of <b>%s</b> denotes administrivia.\nA priority of <b>%s</b> denotes an important task.\nA priority of <b>%s</b> is for normal tasks.\nA priority of <b>%s</b> denotes a low priority task.\n\nOver time, Taskhat will learn how you like to classify your tasks.\n\nSee <b>taskhat/config.py</b> for more options.' % (Task.PRIORITY_ADMIN.name, Task.PRIORITY_HIGH.name, Task.PRIORITY_MEDIUM.name, Task.PRIORITY_LOW.name))
         button = gtk.Button(stock=gtk.STOCK_CLOSE)
         def dest(*args):
            dialog.destroy()
         button.connect('clicked', dest)
         button.show()
         dialog.action_area.pack_end(button)
         dialog.show()

      popup_menu = gtk.Menu()

      s = 'Edit Recurring Events'
      x = gtk.MenuItem(s)
      x.connect('activate', self.event_editor)
      x.show()
      popup_menu.append(x)

      s = 'Revert Changes'
      x = gtk.MenuItem(s)
      x.connect('activate', self.backup_restore)
      x.show()
      popup_menu.append(x)

      x = gtk.SeparatorMenuItem()
      x.show()
      popup_menu.append(x)

      s = 'Information'
      x = gtk.MenuItem(s)
      x.connect('activate', documentation)
      x.show()
      popup_menu.append(x)

      s = 'About'
      x = gtk.MenuItem(s)
      x.connect('activate', self.about_menu)
      x.show()
      popup_menu.append(x)

      popup_menu.attach_to_widget(tool, None)
      popup_menu.show()

      def tool_menu(widget):
         popup_menu.popup(None, None, None, 1, 0)

      tool.connect('clicked', tool_menu)

      entry = self.entry = gtk.Entry()
      entry.connect('changed', self.entry_changed)
      entry.connect('activate', self.entry_done)
      entry.set_width_chars(25)
      entry.show()

      scrolled_window = gtk.ScrolledWindow()
      scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
      scrolled_window.show()
      box3 = gtk.VBox()
      box3.show()
      ebox3 = self.ebox3 = gtk.EventBox()
      ebox3.add(box3)
      ebox3.show()
      scrolled_window.add_with_viewport(ebox3)

#      TaskGroup('Overdue', self.window, self.persist, (-TaskDate.FUTURE+1, -1))
      TaskGroup('Today', self.window, self.persist, (None, 0), events=CONFIG['show_recurring_events'])
      TaskGroup('Tomorrow', self.window, self.persist, (1, 1))
#      TaskGroup('Today &amp; Tomorrow', self.window, self.persist, (None, 1), events=CONFIG['show_recurring_events'])
      TaskGroup('Next few days', self.window, self.persist, (2, 5))
      TaskGroup('Next week', self.window, self.persist, (6, 14))
      TaskGroup('Future', self.window, self.persist, (8, None))
      for group in TaskGroup.groups:
         box3.pack_start(group, False, False)

      ebox3.modify_bg(gtk.STATE_NORMAL, self.window.get_style().base[gtk.STATE_NORMAL])

      status = self.status = gtk.Label(Taskhat.HELP_STRING)
      status.set_alignment(0,.5)
      status.set_ellipsize(pango.ELLIPSIZE_END)
      status.show()
      status.set_sensitive(False)
      box2.pack_start(status, True, True)
      sep = gtk.Label(' ')
      sep.show()
      box2.pack_start(sep, False, False)
      sep = gtk.VSeparator()
      sep.show()
      box2.pack_end(tool, False, False)
      box2.pack_end(sep, False, False)
      box2.pack_end(abutton, False, False)
      tool.show()
      box2.pack_end(entry, False, False)
      box1.pack_start(box2, False, False)
      box1.pack_start(scrolled_window)

      self.window.add(box1)
      self.window.set_size_request(530, 565)

      box2.show()
      box1.show()

      accelgroup = gtk.AccelGroup()
      self.window.add_accel_group(accelgroup)
      action = gtk.Action('Quit', '_Quit me!', 'Quit the Program', gtk.STOCK_QUIT)
      action.connect('activate', self.close)
      actiongroup = gtk.ActionGroup('BasicAction')
      actiongroup.add_action_with_accel(action, None)
      action.set_accel_group(accelgroup)
      menubar = gtk.MenuBar()
      menuitem = action.create_menu_item()
      menubar.append(menuitem)
      box1.pack_start(menubar, False)

      TaskGroup.origin = scrolled_window

      self.persist.restore(self.insert_task, self.update_events)
      self.persist.restore_geometry(self.window)
      persist.load_classifiers()
      self.window.show()
      entry.grab_focus()
      self.window.connect('notify::style', self.update_group_styles)
      self.window.connect('notify::is-active', self.save_geom)
 
      def _callback(data):
         self.clear_all()
         new = filter(lambda t: not t.removed, data.get('tasks', []))
         taskhat_write = data.get('last_taskhat_update', now())
         taskhat_write += datetime.timedelta(seconds=1)
         closed = set()
         for task in new:
            closed.add(repr(task))
         for task in self.persist.tasks:
            if task.last_updated > taskhat_write and repr(task) not in closed:
               new.append(task)
         self.persist.tasks = new
         for task in new:
            self.insert_task(task)
            task.last_updated = taskhat_write
         self.persist.events =\
            filter(lambda e: not e.deleted, data.get('events', []))
         self.persist.sync()
         self.update_events()
         return False

      def callback_gobject_wrapper(data):
         gobject.idle_add(lambda: _callback(data))

      file_api.watch(callback_gobject_wrapper)

   def save_geom(self, *args):
      self.persist.save_geometry(self.window.get_position(), self.window.get_size())

   def clear_all(self):
      for g in TaskGroup.groups:
         g.clear()

   def update_events(self):
      for g in TaskGroup.groups:
         g.update(True)

   def event_editor(self, *args):
      EventEditor(self.window, self.persist, self).run()

   def backup_restore(self, *args):
      RestoreFromBackup(self.window, self.persist, self).run()

   def update_group_styles(self, args, x):
      self.ebox3.modify_bg(gtk.STATE_NORMAL, self.window.get_style().base[gtk.STATE_NORMAL])

   def get_active_text(self, combobox):
      model = combobox.get_model()
      active = combobox.get_active()
      if active < 0:
         return None
      return model[active][0]

   def entry_changed(self, widget, data=None):
      text = self.entry.get_text().strip()
      if text:
         self.abutton.set_sensitive(True)
         self.status.set_label(label_from_string(text)[0])
      else:
         self.abutton.set_sensitive(False)
         self.status.set_label(Taskhat.HELP_STRING)

   def insert_task(self, task):
      TaskGroup.groups[0].smart_assign(task)

   def entry_done(self, widget, data=None):
      text = self.entry.get_text().strip()
      if text:
         task = task_from_string(self.entry.get_text())
         self.insert_task(task)
         self.persist.save(task)
         self.entry.set_text('')

   def close(self, widget, data=None):
      if CONFIG['run_in_background']:
         map(TaskGroup.remove_removed, TaskGroup.groups)
         self.entry.set_text('')
         self.window.hide()
         return True
      else:
         gtk.main_quit()

   def about_menu(self, *args):
      dialog = gtk.AboutDialog()
      dialog.set_name('Taskhat')
      dialog.set_version('0.2')
      dialog.set_comments('Taskhat lets you manage tasks and events, over both the short and long term.')
      dialog.set_copyright('Copyright \xc2\xa9 2010 Eric Liang')
      dialog.run()
      dialog.hide()

   def main(self):
      try:
         from ctypes import cdll
         cdll.LoadLibrary('libc.so.6').prctl(15, 'taskhat', 0, 0, 0)
      except:
         pass
      def handler(e_type, e_value, e_trace):
         sys.excepthook = sys.__excepthook__
         msg = str(e_value) + '\n'
         msg += '\n'.join(traceback.format_tb(e_trace))
         msg += '\n' + 'Further exceptions will not be shown.'
         dialog = gtk.MessageDialog(parent=self.window, buttons=gtk.BUTTONS_OK, flags=gtk.DIALOG_MODAL, type=gtk.MESSAGE_ERROR)
         dialog.set_title(str(e_type))
         dialog.set_markup(escape(msg))
         dialog.run()
         dialog.hide()
      sys.excepthook = handler
      gtk.main()
Пример #41
0
 def __init__(self, ddir=None, filename=None):
     self.datadir = ddir or getdatadir()
     self.filename = filename or 'mainusers'
     Persist.__init__(self, self.datadir + os.sep + self.filename)
     if not self.data: self.data = LazyDict()
     self.data.names = self.data.names or {}
Пример #42
0
class Config(LazyDict):

    """ 
        config class is a dict containing json strings. is writable to file 
        and human editable.

    """

    def __init__(self, filename, verbose=False, input={}, ddir=None, nolog=False, *args, **kw):
        assert filename
        LazyDict.__init__(self, input, *args, **kw)
        self.origname = filename
        self.origdir = ddir or getdatadir()
        self.setcfile(ddir, filename)
        self.jsondb = None
        if not self._comments: self._comments = {}
        try:
            import waveapi
            self.isdb = True
            self.isgae = True
        except ImportError:
            self.isgae = False
            self.isdb = False
        dodb = False
        try:
            logging.info("fromfile - %s from %s" % (self.origname, whichmodule(2)))
            self.fromfile(self.cfile)
        except IOError, ex: handle_exception() ; dodb = True
        if dodb or (self.isgae and not "mainconfig" in filename):
            try:
                from persist import Persist
                self.jsondb = Persist(self.cfile)
                if self.jsondb: self.merge(self.jsondb.data)
                logging.warn("fromdb - %s" % self.cfile)
            except ImportError:
                logging.warn("can't read config from %s - %s" % (self.cfile, str(ex))) 
        self.init()
        if self.owner: logging.info("owner is %s" % self.owner)
        if not self.has_key("uuid"): self.setuuid()
        if not self.has_key("cfile"): self.cfile = self.setcfile(self.origdir, self.origname) 
        assert self.cfile

    def setcfile(self, ddir, filename):
        self.filename = filename or 'mainconfig'
        self.datadir = ddir or getdatadir()
        self.dir = self.datadir + os.sep + 'config'
        self.cfile = self.dir + os.sep + filename

    def setuuid(self, save=True):
        logging.debug("setting uuid")
        self.uuid = str(uuid.uuid4())
        if save: self.save()

    def __deepcopy__(self, a):
        """ accessor function. """
        cfg = Config(self.filename, input=self, nolog=True)
        return cfg

    def __getitem__(self, item):
        """ accessor function. """
        if not self.has_key(item): return None
        else: return LazyDict.__getitem__(self, item)

    def merge(self, cfg):
        """ merge in another cfg. """
        self.update(cfg)

    def set(self, item, value):
        """ set item to value. """
        LazyDict.__setitem__(self, item, value)

    def fromdb(self):
        """ read config from database. """
        from jsb.lib.persist import Persist
        tmp = Persist(self.cfile)
        logging.debug("fromdb - %s - %s" % (self.cfile, tmp.data.tojson()))
        self.update(tmp.data)

    def todb(self):
        """ save config to database. """
        cp = dict(self)
        del cp['jsondb']
        if not self.jsondb:
            from jsb.lib.persist import Persist
            self.jsondb = Persist(self.cfile)
        self.jsondb.data = cp
        self.jsondb.save()

    def fromfile(self, filename=None):
        """ read config object from filename. """
        curline = ""
        fname = filename or self.cfile
        if not fname: raise Exception(" %s - %s" % (self.cfile, self.dump()))
        if not os.path.exists(fname): logging.warn("config file %s doesn't exist yet" % fname) ; return False 
        comment = ""
        for line in open(fname, 'r'):
            curline = line
            curline = curline.strip()
            if curline == "": continue
            if curline.startswith('#'): comment = curline; continue
            if True:
                try:
                    key, value = curline.split('=', 1)
                    kkey = key.strip()
                    self[kkey] = json.loads(unicode(value.strip()))
                    if comment: self._comments[kkey] = comment 
                    comment = ""
                except ValueError: logging.error("skipping line - unable to parse: %s" % line)
        #self.cfile = fname
        return

    def tofile(self, filename=None, stdout=False):
        """ save config object to file. """
        if not filename: filename = self.cfile
        if not filename: raise Exception("no cfile found  - %s" % whichmodule(3))
        if self.isgae: logging.warn("can't save config file %s on GAE" % filename) ; return
        logging.warn("saving %s" % filename)
        if filename.startswith(os.sep): d = [os.sep,]
        else: d = []
        for p in filename.split(os.sep)[:-1]:
            if not p: continue
            d.append(p)
            ddir = os.sep.join(d)
            if not os.path.isdir(ddir):
                logging.debug("persist - creating %s dir" % ddir)
                try: os.mkdir(ddir)
                except OSError, ex:
                    logging.error("persist - not saving - failed to make %s - %s" % (ddir, str(ex)))
                    return
        written = []
        curitem = None
        later = []
        try:
            if stdout: configtmp = sys.stdout
            else: configtmp = open(filename + '.tmp', 'w')
            configtmp.write('# ===========================================================\n#\n')
            configtmp.write("# JSONBOT CONFIGURATION FILE - %s\n" % filename)
            configtmp.write("#\n")
            configtmp.write('# last changed on %s\n#\n' % time.ctime(time.time()))
            configtmp.write("# This file contains configration data for the JSONBOT.\n")
            configtmp.write('# Variables are defined by "name = json value" pairs.\n')
            configtmp.write('# Make sure to use " in strings.\n#\n')
            configtmp.write('# The bot can edit this file!.\n#\n')
            configtmp.write('# ===========================================================\n\n')
            teller = 0
            keywords = self.keys()
            keywords.sort()
            for keyword in keywords:
                value = self[keyword]
                if keyword in written: continue
                if keyword in ['isgae', 'origdir', 'origname', 'issaved', 'blacklist', 'whitelist', 'followlist', 'uuid', 'whitelist', 'datadir', 'name', 'createdfrom', 'cfile', 'filename', 'dir', 'isdb']: later.append(keyword) ; continue
                if keyword == 'jsondb': continue
                if keyword == 'optionslist': continue
                if keyword == 'gatekeeper': continue
                if keyword == "_comments": continue
                if self._comments and self._comments.has_key(keyword):
                    configtmp.write(self._comments[keyword] + u"\n")
                curitem = keyword
                try: configtmp.write('%s = %s\n' % (keyword, json.dumps(value)))
                except TypeError: logging.error("%s - can't serialize %s" % (filename, keyword)) ; continue
                teller += 1
                #configtmp.write("\n")
            configtmp.write('\n\n# ============================================================\n#\n')
            configtmp.write("# bot generated stuff.\n#\n")
            configtmp.write('# ============================================================\n\n')
            for keyword in later:
                if self._comments and self._comments.has_key(keyword):
                    configtmp.write(self._comments[keyword] + u"\n")
                curitem = keyword
                value = self[keyword]
                try: configtmp.write(keyword + " = " + json.dumps(value) + "\n")
                except TypeError: logging.error("%s - can't serialize %s" % (filename, keyword)) ; continue
                teller += 1
                #configtmp.write("\n")
            if not "mainconfig" in filename and self._comments:
                try:
                    configtmp.write('\n\n# ============================================================\n#\n')
                    configtmp.write("# possible other config variables.\n#\n")
                    configtmp.write('# ============================================================\n\n')
                    items = self._comments.keys()
                    keys = self.keys()
                    do = []
                    for var in items:
                        if var not in keys: do.append(var)
                    do.sort()
                    for var in do:
                         configtmp.write(u"# %s -=- %s\n" % (var, self._comments[var]))
                    configtmp.write("\n\n")
                except Exception, ex: handle_exception()
            else: configtmp.write("\n\n# jsonbot can run multiple bots at once. see %s/config/fleet for their configurations.\n\n" % self.origdir)
            if not stdout: 
                configtmp.close()
                os.rename(filename + '.tmp', filename)
            return teller

        except Exception, ex:
            handle_exception()
            logging.error("ERROR WRITING %s CONFIG FILE: %s .. %s" % (self.cfile, str(ex), curitem))
Пример #43
0
	def __init__(self):
		self.nr_moves = 0
		self.nr_search_steps = 0
		self.current_nr_search_moves = 0
		self.persist = Persist()
Пример #44
0
 def fromdb(self):
     """ read config from database. """
     from jsb.lib.persist import Persist
     logging.info("config - fromdb - %s" % self.cfile)
     tmp = Persist(self.cfile)
     self.update(tmp.data)
Пример #45
0
import discord
from functools import lru_cache
import asyncio
import asyncpg
import os
from botcommand import BotCommand
from credentials import Credentials
from persist import Persist
persist = Persist()
client = discord.Client()
token = os.environ['token']
credentials = Credentials()
credentials.uri = os.environ['DATABASE_URL']
from store.dblayer import DuplicateError
from store.postgres import PGWrapper
db = PGWrapper(credentials)


@client.event
async def on_message(message):
    if message.content.startswith("^register"):
        await register_command(message)
    if message.content.startswith("^count"):
        await get_count(message)
    if message.content.startswith("^top10"):
        await top10(message)
    elif message.content.startswith('[') and message.content[-1] == ']':
        await respond_to_command(message)
    elif message.content.startswith('^randomguess'):
        await random_guess(message)
    elif message.content.startswith('^random'):
Пример #46
0
def set(target, txtlist):
    """ set target entry to list. """
    cache = Persist(getdatadir() + os.sep + 'run' + os.sep + 'outputcache' + os.sep + stripname(target))
    if not cache.data.has_key('msg'): cache.data['msg'] = []
    cache.data['msg'] = txtlist
    cache.save()
Пример #47
0
 def __init__(self, datadir):
     Persist.__init__(self, datadir + os.sep + 'fleet' + os.sep + 'fleet.main')
     if not self.data.has_key('names'): self.data['names'] = []
     if not self.data.has_key('types'): self.data['types'] = {}
     self.startok = threading.Event()
     self.bots = []
Пример #48
0
    been checked in before, and provides the opportunity to insert
    the call and name for that person from the list.
    
    See https://github.com/fledder/ham-net-scribe for more info
    or to contribute.
"""

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import (Qt, pyqtSignal, QObject, QEvent)

from persist import Persist
from dataStructures import Station, Script, StationList
from customWidgets import *

p = Persist()

PHONETIC_STYLESHEET = '''
    background-color: rgb(0, 0, 0, 255);
    font-family: "Consolas";
    font-size: 24px;
    font-style: bold;
    color: rgb(128, 255, 255, 255);
    qproperty-alignment: AlignCenter;
    '''


class MainFormWidget(QWidget):
    """
    Class for the main window. 
    """