Exemplo n.º 1
0
    def __init__(self, alpha, beta, x_plot=None, y_plot=None, verbose=False):
        if len(alpha) <> len(beta):
            raise ValueError("The two parameter lists must be of the same length")

        alpha, beta = map(frac, alpha), map(frac, beta)

        n = len(alpha)
        if not(set([E(beta[j]) for j in range(n)]).isdisjoint(set([E(alpha[i]) for i in range(n)]))):
            if verbose:
                print "Warning the sets of eigenvalues are not disjoint, the flat bundle won't be minimal:"
                print alpha, beta
            alpha = map(lambda x: frac(x + random()/2**15), alpha)
            beta = map(lambda x: frac(x + random()/2**15), beta)
            if verbose:
                print "Adding a little bit of noise"
                print alpha, beta

        self._dimension = len(alpha)
        self._alpha = map(frac, alpha)
        self._beta = map(frac, beta)
        self._alpha.sort()
        self._beta.sort()
        self._i_alpha = [alpha.index(a) for a in self._alpha]
        self._i_beta = [beta.index(b) for b in self._beta]
        self.x_plot = x_plot
        self.y_plot = y_plot
Exemplo n.º 2
0
    def __init__(self, alpha, beta, x_plot=None, y_plot=None, verbose=False):
        if len(alpha) <> len(beta):
            raise ValueError(
                "The two parameter lists must be of the same length")

        alpha, beta = map(frac, alpha), map(frac, beta)

        n = len(alpha)
        if not (set([E(beta[j]) for j in range(n)]).isdisjoint(
                set([E(alpha[i]) for i in range(n)]))):
            if verbose:
                print "Warning the sets of eigenvalues are not disjoint, the flat bundle won't be minimal:"
                print alpha, beta
            alpha = map(lambda x: frac(x + random() / 2**15), alpha)
            beta = map(lambda x: frac(x + random() / 2**15), beta)
            if verbose:
                print "Adding a little bit of noise"
                print alpha, beta

        self._dimension = len(alpha)
        self._alpha = map(frac, alpha)
        self._beta = map(frac, beta)
        self._alpha.sort()
        self._beta.sort()
        self._i_alpha = [alpha.index(a) for a in self._alpha]
        self._i_beta = [beta.index(b) for b in self._beta]
        self.x_plot = x_plot
        self.y_plot = y_plot
Exemplo n.º 3
0
def ai_Mandarin1(phase, player, opponents, Ref):   # This version ensures you have 1 gold before buying provinces
	if phase == 'action':
		if 'Mandarin' in player.hand: 
			return('Mandarin')
		else: return(False)
	elif phase == 'buy':
		losing_play = Ref.supply['Province'] == 1 and player.score() + 6 < max( opponent.score() for opponent in opponents )
		if Ref.feasible(player, 'Province') and not losing_play: return('Province') # buy Province
		elif Ref.feasible(player, 'Duchy') and Ref.supply['Province'] <= 5: return('Duchy')  # buy Duchy if there are fewer than 4 estates
		elif Ref.feasible(player, 'Estate') and Ref.supply['Province'] == 1 and len(player.deck) >= 10: return('Estate') # buy Estate if only 1 province and cards in deck
		elif Ref.feasible(player, 'Mandarin') and player.total('Mandarin') == 0: return('Mandarin')
		elif Ref.feasible(player, 'Gold'): return('Gold') # buy Gold
		elif Ref.feasible(player, 'Silver'): return('Silver') # buy Silver
		else: return(False)
	elif phase == 'mandarin': # Here choose a card to put back on top of the deck
		if player.action == 0 and 'Mandarin' in player.hand: return 'Mandarin' 
		elif player.wealth() > 10 and 'Gold' in player.hand: return 'Gold'
		elif player.wealth() >= 10 and 'Silver' in player.hand: return 'Silver'			
		elif player.wealth() >= 9 and 'Copper' in player.hand: return 'Copper'
		elif  'Copper' in player.hand and player.wealth() == 7: return 'Copper'
		elif  'Copper' in player.hand and player.wealth() == 4: return 'Copper'
		elif list(set(Ref.strict_victory_cards) & set(player.hand)) != []:
			union = list(set(Ref.strict_victory_cards) & set(player.hand))
			return union[0]
		elif 'Copper' in player.hand: return 'Copper'
		elif 'Silver' in player.hand: return 'Silver'
		else:
			print "Error in Mandarin1 AI"
			return 'Gold'	
	else: raise IOError	
	def getStateBySimbol(self, state, simbols):
		states = set([state])
		for simbol in simbols: 
			newStates = set([])
			for state in states: 
				try: 
					newStates = newStates | set(self.delta[state][simbol])
				except KeyError: pass
			states = newStates
		return states
Exemplo n.º 5
0
 def getStateBySimbol(self, state, simbols):
     states = set([state])
     for simbol in simbols:
         newStates = set([])
         for state in states:
             try:
                 newStates = newStates | set(self.delta[state][simbol])
             except KeyError:
                 pass
         states = newStates
     return states
 def TotalClassList(self):
     PythonClassList=set()
     pythonFiles=self.AllFileList()
     for filename in pythonFiles:
         classList=self.PythonClassList(filename)
         PythonClassList.update(classList)
     return PythonClassList
 def TotalFuncList(self):
     PythonFuncList=set()
     pythonFiles=self.AllFileList()
     for filename in pythonFiles:
         funcList=self.PythonFuncList(filename)
         PythonFuncList.update(funcList)
     return PythonFuncList
Exemplo n.º 8
0
def NFAtoDFA(N):
    q0 = frozenset(N.q0)
    Q = set([q0])
    unprocessedQ = Q.copy()
    delta = {}
    F = []
    Sigma = N.alphabet()

    while len(unprocessedQ) > 0:
        qSet = unprocessedQ.pop()
        delta[qSet] = {}
        for a in Sigma:
            nextStates = reduce(lambda x, y: x | y,
                                [N.getStateBySimbol(q, a) for q in qSet])
            nextStates = frozenset(nextStates)
            if (nextStates is not frozenset([])):
                delta[qSet][a] = nextStates
            if not nextStates in Q:
                if (nextStates is not frozenset([])):
                    Q.add(nextStates)
                    unprocessedQ.add(nextStates)
    for qSet in Q:
        if len(qSet & N.F) > 0:
            F.append(qSet)
    M = DFA(delta, q0, F)
    return M
Exemplo n.º 9
0
    def add(self, item, **ftuple):
        """Add an item to be matched on a flow tuple filter.
        Flow tuple values may be lists.
        """
        if 'src' in ftuple:
            value = ftuple['src']
            # XXX - handle lists of values
            if not isinstance(value, list):
                value = [ value ]
            for v in value:
                self.src.add(v.net(), v.bcast(), item)
        else:
            self.src_any.add(item)

        if 'dst' in ftuple:
            value = ftuple['dst']
            # XXX - handle lists of values
            if not isinstance(value, list):
                value = [ value ]
            for v in value:
                self.dst.add(v.net(), v.bcast(), item)
        else:
            self.dst_any.add(item)

        for k in self.__keys:
            d = getattr(self, k)
            value = ftuple.get(k, None)
            # XXX - handle lists of values
            if not isinstance(value, list):
                value = [ value ]
            for v in value:
                if v not in d:
                    d[v] = set()
                d[v].add(item)
Exemplo n.º 10
0
def modified(date=None, etag=None):
    try:
        from __builtin__ import set
    except ImportError:
        # for python 2.3
        from sets import Set as set

    env = web.ctx.env

    n = set(
        [x.strip('" ') for x in env.get('HTTP_IF_NONE_MATCH', '').split(',')])
    m = rbweb.net.parsehttpdate(
        env.get('HTTP_IF_MODIFIED_SINCE', '').split(';')[0])
    validate = False
    if etag:
        if '*' in n or etag in n:
            validate = True
    if date and m:
        if date - datetime.timedelta(seconds=1) <= m:
            validate = True

    if date: lastmodified(date)
    if etag: web.header('ETag', '"' + etag + '"')
    if validate:
        raise web.notmodified()
    else:
        return True
Exemplo n.º 11
0
    def subscribe(self, name, event, callback):
        """Subscribe to a handler's event.

        Arguments:
        name     -- name of handler (or match pattern)
        event    -- name of event published by handler
        callback -- callback to be invoked
        """
        if '*' in name:
            found = False
            for x in fnmatch.filter(Handler._subclasses.iterkeys(), name):
                if event in Handler._subclasses[x].events:
                    self.subscribe(x, event, callback)
                    found = True
            if not found:
                raise RuntimeError, 'no matching handlers found'
            return
        if name not in handlers:
            if name != self.name:
                # XXX - auto-instantiate handler
                handlers[name] = Handler._subclasses[name]()
            else:
                handlers[name] = self
        pub = handlers[name]
        pub._register(event, callback)
        if pub not in self.subscriptions:
            self.subscriptions[pub] = set()
        self.subscriptions[pub].add((event, callback))
Exemplo n.º 12
0
def modified(date=None, etag=None):    
    try:
        from __builtin__ import set
    except ImportError:
        # for python 2.3
        from sets import Set as set
        
    env=web.ctx.env

    n = set([x.strip('" ') for x in env.get('HTTP_IF_NONE_MATCH', '').split(',')])
    m = rbweb.net.parsehttpdate(env.get('HTTP_IF_MODIFIED_SINCE', '').split(';')[0])
    validate = False
    if etag:
        if '*' in n or etag in n:
            validate = True
    if date and m:
        if date-datetime.timedelta(seconds=1) <= m:
            validate = True
    
    if date: lastmodified(date)
    if etag: web.header('ETag', '"' + etag + '"')
    if validate:
        raise web.notmodified()
    else:
        return True
Exemplo n.º 13
0
    def add(self, item, **ftuple):
        """Add an item to be matched on a flow tuple filter.
        Flow tuple values may be lists.
        """
        if 'src' in ftuple:
            value = ftuple['src']
            # XXX - handle lists of values
            if not isinstance(value, list):
                value = [value]
            for v in value:
                self.src.add(v.net(), v.bcast(), item)
        else:
            self.src_any.add(item)

        if 'dst' in ftuple:
            value = ftuple['dst']
            # XXX - handle lists of values
            if not isinstance(value, list):
                value = [value]
            for v in value:
                self.dst.add(v.net(), v.bcast(), item)
        else:
            self.dst_any.add(item)

        for k in self.__keys:
            d = getattr(self, k)
            value = ftuple.get(k, None)
            # XXX - handle lists of values
            if not isinstance(value, list):
                value = [value]
            for v in value:
                if v not in d:
                    d[v] = set()
                d[v].add(item)
def NFAtoDFA(N):
	q0 = frozenset(N.q0)
	Q = set([q0])
	unprocessedQ = Q.copy()
	delta = {}
	F = []
	Sigma = N.alphabet()
	
	while len(unprocessedQ) > 0:
		qSet = unprocessedQ.pop()
		delta[qSet] = {}
		for a in Sigma: 
		    nextStates = reduce(lambda x,y: x | y, [N.getStateBySimbol(q,a) for q in qSet])
		    nextStates = frozenset(nextStates)
		    if (nextStates is not frozenset([])):
		        delta[qSet][a] = nextStates
		    if not nextStates in Q: 
		        if (nextStates is not frozenset([])):
		            Q.add(nextStates)
		            unprocessedQ.add(nextStates)
	for qSet in Q: 
		if len(qSet & N.F) > 0: 
			F.append(qSet)
	M = DFA(delta, q0, F)
	return M
Exemplo n.º 15
0
def function_to_run(review):
    ret_list = []
    for _, row in review.iterrows():
        _scores_ = {}
        del row['_id']
        tags = row['final']
        if len(set(tags.keys())) > 0:
            for key in tags.keys():
                _scores_[key] = {}
                word = key.split(" ")
                for category in categories.keys():
                    cat = categories[category]
                    try:
                        _scores_[key][category] = sum(
                            word_vectors.n_similarity(word, cat))
                    except Exception as e:
                        break
                        pass

        row['score'] = _scores_
        ret_list.append(row)
        if len(ret_list) > 10000:
            df = pd.DataFrame(ret_list)
            to_mongo_db(df, 'yelp_review_scored_pair_all_not_final')
            print("Written to DB", len(ret_list), 'time from start',
                  (time.time() - start_time))
            ret_list = []

    df = pd.DataFrame(ret_list)
    to_mongo_db(df, 'yelp_review_scored_pair_all_not_final')
    print("Written to DB", len(ret_list), 'time from start',
          (time.time() - start_time))
Exemplo n.º 16
0
 def __init__(self, file):
     fileRead = open(file, 'r')
     self.delta = dict()
     definitions = fileRead.read().replace(' ',
                                           '').replace('\n', '').replace(
                                               '\t', '').split('.')
     del definitions[-1]
     for definition in definitions:
         if (definition.split(':')[0][0] == 'T'):
             transitionsString = definition.replace(
                 ':=',
                 '=').split('T:')[1].replace('},',
                                             '*').replace('}',
                                                          '*').split('*')
             for transition in transitionsString:
                 if (transition != ''):
                     transition = transition.split('={')
                     state = transition[0][0]
                     simbol = transition[0][2]
                     targets = set(transition[1].split(','))
                     deltaAux = dict([(simbol, targets)])
                     if self.delta.has_key(state):
                         self.delta[state][simbol] = targets
                     else:
                         self.delta[state] = deltaAux
         elif (definition.split(':')[0][0] == 'I'):
             self.q0 = definition.split(':')[1].split(',')[0]
         elif (definition.split(':')[0][0] == 'F'):
             self.F = definition.split(':')[1].split(',')
     fileRead.close()
Exemplo n.º 17
0
def _handle_adjust(func, p0, adjust, noadjust):
    if adjust is None and noadjust is None:
        return slice(None)
    Np = len(p0)
    all_index = range(Np)
    para, kwpara, varargs, varkw, defaults = getVarNames(func)
    names = para
    Npara = len(para)
    if Npara < Np:
        names += kwpara[:Np - Npara]
    if isinstance(adjust, slice):
        adjust = all_index(adjust)
    if isinstance(noadjust, slice):
        adjust = all_index(noadjust)
    if adjust is None:
        adjust = all_index
    if noadjust is None:
        noadjust = []
    #s = set() # This fails when running under pyHegel (not on import).
    s = __builtin__.set()
    # cleanup adjust. Remove duplicates, handle named variables.
    for a in adjust:
        if isinstance(a, basestring):
            s.add(names.index(a) -
                  1)  # -1 to remove the x parameter of f(x, p1, ...)
        else:
            s.add(a)
    # Now cleanup noadjust
    sna = __builtin__.set()
    for na in noadjust:
        if isinstance(na, basestring):
            sna.add(names.index(na) - 1)
        else:
            sna.add(na)
    #Remove noadjust from adjust set
    s = s - sna
    adj = list(s)
    adj.sort()
    return adj
Exemplo n.º 18
0
def get_users_for_business(business_id):
    user_list = []
    reviews = mongo_connection.db.yelp_reviews.find(
        {'business_id': business_id}, {"user_id": 1})
    tips = mongo_connection.db.yelp_tips.find({'business_id': business_id},
                                              {"user_id": 1})

    for user in reviews:
        user_list.append(user['user_id'])
    for user in tips:
        user_list.append(user['user_id'])

    user_list = list(set(user_list))
    return user_list
Exemplo n.º 19
0
def modified(date=None, etag=None):
    """
    Checks to see if the page has been modified since the version in the
    requester's cache.
    
    When you publish pages, you can include `Last-Modified` and `ETag`
    with the date the page was last modified and an opaque token for
    the particular version, respectively. When readers reload the page, 
    the browser sends along the modification date and etag value for
    the version it has in its cache. If the page hasn't changed, 
    the server can just return `304 Not Modified` and not have to 
    send the whole page again.
    
    This function takes the last-modified date `date` and the ETag `etag`
    and checks the headers to see if they match. If they do, it returns 
    `True`, or otherwise it raises NotModified error. It also sets 
    `Last-Modified` and `ETag` output headers.
    """
    try:
        from __builtin__ import set
    except ImportError:
        # for python 2.3
        from sets import Set as set

    n = set([
        x.strip('" ')
        for x in web.ctx.env.get('HTTP_IF_NONE_MATCH', '').split(',')
    ])
    m = net.parsehttpdate(
        web.ctx.env.get('HTTP_IF_MODIFIED_SINCE', '').split(';')[0])
    validate = False
    if etag:
        if '*' in n or etag in n:
            validate = True
    if date and m:
        # we subtract a second because
        # HTTP dates don't have sub-second precision
        if date - datetime.timedelta(seconds=1) <= m:
            validate = True

    if date: lastmodified(date)
    if etag: web.header('ETag', '"' + etag + '"')
    if validate:
        raise web.notmodified()
    else:
        return True
Exemplo n.º 20
0
def modified(date=None, etag=None):
    """
    Checks to see if the page has been modified since the version in the
    requester's cache.

    When you publish pages, you can include `Last-Modified` and `ETag`
    with the date the page was last modified and an opaque token for
    the particular version, respectively. When readers reload the page,
    the browser sends along the modification date and etag value for
    the version it has in its cache. If the page hasn't changed,
    the server can just return `304 Not Modified` and not have to
    send the whole page again.

    This function takes the last-modified date `date` and the ETag `etag`
    and checks the headers to see if they match. If they do, it returns
    `True`, or otherwise it raises NotModified error. It also sets
    `Last-Modified` and `ETag` output headers.
    """
    try:
        from __builtin__ import set
    except ImportError:
        # for python 2.3
        from sets import Set as set

    n = set([x.strip('" ') for x in web.ctx.env.get('HTTP_IF_NONE_MATCH', '').split(',')])
    m = net.parsehttpdate(web.ctx.env.get('HTTP_IF_MODIFIED_SINCE', '').split(';')[0])
    validate = False
    if etag:
        if '*' in n or etag in n:
            validate = True
    if date and m:
        # we subtract a second because
        # HTTP dates don't have sub-second precision
        if date - datetime.timedelta(seconds=1) <= m:
            validate = True

    if date:
        lastmodified(date)
    if etag:
        web.header('ETag', '"' + etag + '"')
    if validate:
        raise web.notmodified()
    else:
        return True
Exemplo n.º 21
0
def getPropertyValueListByPropertyTypeHandler(handlerName):
    propertyTypeHandler = propertyApi.getPropertyHandlerIdByPropertyHandlerName(handlerName)
    if propertyTypeHandler is not None:
        handlerPropertyTypes = propertyApi.getPropertyTypesByHandlerId(propertyTypeHandler.data['id'])
    else:
        return

    propertyValues = []
    for propertyType in handlerPropertyTypes:
        propertyTypeValues = propertyApi.getPropertyValuesByPropertyTypeId(propertyType.data['id'])
        propertyValues = propertyValues + propertyTypeValues

    attachements = []
    for propertyValue in propertyValues:
        appendNonEmptyValue(attachements,propertyValue.data['value'])
        for propertyValueHistory in propertyValue.data['propertyValueHistory']:
            appendNonEmptyValue(attachements, propertyValueHistory.value)
    # Remove duplicates and return list
    return list(set(attachements))
Exemplo n.º 22
0
def get_user_friends(user_list):
    users = mongo_connection.db.yelp_users.find(
        {'user_id': {
            "$in": user_list
        }}, {
            'friends': 1,
            'user_id': 1
        })
    edges = []
    for user in users:
        friends = user['friends']
        user_id = user['user_id']
        for friend in friends:
            edges.append((user_id, friend))
            edges.append((friend, user_id))

    edges = list(set(edges))

    return edges
 def FormattedClassList(self,silent=True):
     PythonClassList=self.TotalClassList()
     formattedClassList=set()
     for filename in self.LyxFileList():
         if not silent:
             print(filename)
         with open(filename,'r') as f:
             lines=f.readlines()
         for lineindex in range(len(lines)):
             if lines[lineindex] == '\\begin_inset Flex PythonClass\n':
                 try:
                     classname=lines[lineindex+5].strip()
                     if classname in PythonClassList:
                         if not silent:
                             print('FOUND ONE: '+classname)
                         formattedClassList.add(classname)
                     else:
                         if not silent:
                             print('class not in list: '+classname)
                 except:
                     pass
     return formattedClassList
Exemplo n.º 24
0
    def __init__(self, service, *args, **kwargs):
        super(PrivacyPolicyForm, self).__init__(*args, **kwargs)
        self.service = service
        unq = set()
        for sett in service.inputs.all():
            for type in sett.types.all():
                unq.add(type)
        for type in service.output.types.all():
            unq.add(type)
        #removing the ones that are currently added
        for rule in service.service_privacy_policy_rule_set.all():
            unq.remove(rule.dataType)
        choices = []

        for type in unq:
            choices.append((type, str(type)))
        print(choices)
        self.fields['type'].choices = choices
        try:
            self.fields['type'].initial = self.instance.dataType.name
        except:
            pass
Exemplo n.º 25
0
    def to_python(self, value):
        striped = value.strip()
        if striped[0] != '{' or striped[-1] != '}':
            raise ValidationError(('set should start with { and end with }'))
        striped = striped[1:-1]
        splited = striped.split(',')
        ret = set()
        if len(splited) == 0 or (len(splited) == 1 and not splited[0].strip()):
            if self.required:
                raise ValidationError(('set can not be empty'))
            else:
                return ret

        for val in splited:
            striped = val.strip()
            if not striped:
                continue
            if re.match('[a-zA-Z0-9_]\w*',striped) is None:
                raise ValidationError(('invalid type name: "%(name)s"'),
                                          params={'name': striped}
                )
            ret.add(striped)
        return ret
 def __init__(self, file):
     fileRead = open(file, 'r')
     self.delta = dict()
     definitions = fileRead.read().replace(' ', '').replace('\n', '').replace('\t', '').split('.')
     del definitions[-1]
     for definition in definitions:
         if (definition.split(':')[0][0] == 'T'):
             transitionsString = definition.replace(':=','=').split('T:')[1].replace('},','*').replace('}','*').split('*')
             for transition in transitionsString:
                 if (transition != ''):
                     transition = transition.split('={')
                     state = transition[0][0]
                     simbol = transition[0][2]
                     targets = set(transition[1].split(','))
                     deltaAux = dict([(simbol,targets)])
                     if self.delta.has_key(state):
                         self.delta[state][simbol] = targets
                     else:
                         self.delta[state] = deltaAux
         elif (definition.split(':')[0][0] == 'I'):
             self.q0 = definition.split(':')[1].split(',')[0]
         elif (definition.split(':')[0][0] == 'F'):
             self.F = definition.split(':')[1].split(',')
     fileRead.close()
Exemplo n.º 27
0
    def tradefind_display(self,
                          qtype,
                          fromt,
                          to,
                          set=None,
                          age=24,
                          cashonhand=10000000.00,
                          minprofit=100000,
                          size=10000,
                          startat=0,
                          limit=50,
                          newsearch="0",
                          sort="jprofit",
                          prefer_sec="0"):

        session = EVCstate()
        t = display.template('tradefind_display.tmpl', session)
        db = evec_func.db_con()
        newsearch = int(newsearch)
        if 'trade_results' not in session:
            newsearch = 1

        cur = db.cursor()

        limit = int(limit)
        prefer_sec = int(prefer_sec)
        recalc_route = False

        if sort not in ['jprofit', 'sprofit', 'profit', 'jumps']:
            return

        if set:
            session['tf_age'] = age
            session['tf_minprofit'] = minprofit
            session['tf_size'] = size
            session['tf_limit'] = limit
            session['tf_sort'] = sort
            session['tf_prefer_sec'] = prefer_sec
            newsearch = 1
        else:
            if 'tf_age' in session:
                age = session['tf_age']
            if 'tf_minprofit' in session:
                minprofit = session['tf_minprofit']
            if 'tf_size' in session:
                size = session['tf_size']
            if 'tf_limit' in session:
                limit = session['tf_limit']
            if 'tf_sort' in session:
                sort = session['tf_sort']
            if 'tf_prefer_sec' in session:
                prefer_sec = session['tf_prefer_sec']
            else:
                prefer_sec = 1

        age_t = str(int(age)) + " hours"

        size = str(int(size))
        cashonhand = str(double(cashonhand))

        sql_profit_size = """    (t.price - f.price)* min(""" + size + """, min(t.volremain,f.volremain) * types.size)/types.size """
        sql_profit_jumps = """   '1' """
        #sql_cash = """ f.price <= $(cashonhand)s """

        sql_sec_limit = """ """
        if prefer_sec:
            sql_sec_limit = """ fsys.security > 0.4 AND tsys.security > 0.4 AND """

        cur_f = db.cursor()
        cur_t = db.cursor()

        #####
        # Warning: This is easily the worst piece of code I have ever written.
        # I really am sorry.
        #####

        # Pure suck query
        if qtype == "Systems" and newsearch:

            cur.execute(
                """SELECT types.typeid,types.typename,fs.systemid,ts.systemid,fs.stationname,ts.stationname,t.price - f.price AS pricediff,
            min(t.volremain,f.volremain),
            (t.price - f.price)* min(t.volremain,f.volremain) AS profit,
            """ + sql_profit_size + """ AS profit_size,
            """ + sql_profit_jumps + """ AS profit_jumps,
            t.price,f.price,
            t.volremain,f.volremain

            FROM
            types, current_market AS f, current_market AS t, stations AS fs, stations AS ts, systems AS fsys, systems AS tsys
            WHERE
            """ + sql_sec_limit + """
            ts.systemid = tsys.systemid AND fs.systemid = fsys.systemid AND
            f.minvolume <= 1 AND t.minvolume <= 1 AND
            f.bid = 0 AND t.bid = 1 AND f.systemid = %(fromt)s AND t.systemid = %(to)s AND t.typeid = f.typeid AND t.stationid = ts.stationid AND
            f.stationid = fs.stationid AND
            age(f.reportedtime) < %(age)s AND age(t.reportedtime) < %(age)s AND
            """ + sql_profit_size + """ >=	%(minprofit)s AND

            types.size <= """ + size + """ AND
            t.typeid = types.typeid
            AND f.typeid = types.typeid
            AND f.price < t.price""", {
                    'fromt': fromt,
                    'to': to,
                    'age': age_t,
                    'minprofit': minprofit,
                })

            cur_f.execute(
                "SELECT systemid,(systemname || ' / ')  || regionname FROM systems,regions WHERE systems.regionid = regions.regionid AND systemid = %s ORDER BY systemname",
                [fromt])
            cur_t.execute(
                "SELECT systemid,(systemname || ' / ')  || regionname FROM systems,regions WHERE systems.regionid = regions.regionid AND systemid = %s ORDER BY systemname",
                [to])

        elif qtype == "Regions" and newsearch:
            cur.execute(
                """SELECT types.typeid,types.typename,fs.systemid,ts.systemid,fs.stationname,ts.stationname,t.price - f.price AS pricediff,
            min(t.volremain,f.volremain),
            (t.price - f.price)*min(t.volremain,f.volremain) AS profit,


            """ + sql_profit_size + """ AS profit_size,
            """ + sql_profit_jumps + """ AS profit_jumps,


            t.price,f.price, t.volremain, f.volremain

            FROM types, current_market AS f, current_market AS t, stations AS fs, stations AS ts, systems AS fsys, systems AS tsys
            WHERE
            """ + sql_sec_limit + """
            ts.systemid = tsys.systemid AND fs.systemid = fsys.systemid AND
            f.minvolume <= 1 AND t.minvolume <= 1 AND
            f.bid = 0 AND t.bid = 1 AND f.regionid = %(fromt)s AND t.regionid = %(to)s AND t.typeid = f.typeid AND t.stationid = ts.stationid AND
            f.stationid = fs.stationid AND
            age(f.reportedtime) < %(age)s AND age(t.reportedtime) < %(age)s AND
            """ + sql_profit_size + """ >=	%(minprofit)s AND

            types.size <= """ + size + """ AND
            t.typeid = types.typeid
            AND f.typeid = types.typeid
            AND f.price < t.price""", {
                    'fromt': fromt,
                    'to': to,
                    'age': age_t,
                    'minprofit': minprofit,
                })
            cur_f.execute(
                "SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname",
                [fromt])
            cur_t.execute(
                "SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname",
                [to])

        elif qtype == "SystemToRegion" and newsearch:
            cur.execute(
                """SELECT types.typeid,types.typename,fs.systemid,ts.systemid,fs.stationname,ts.stationname,t.price - f.price AS pricediff,
            min(t.volremain,f.volremain),
            (t.price - f.price)*min(t.volremain,f.volremain) AS profit,


            """ + sql_profit_size + """ AS profit_size,
            """ + sql_profit_jumps + """ AS profit_jumps,


            t.price,f.price, t.volremain, f.volremain

            FROM types, current_market AS f, current_market AS t, stations AS fs, stations AS ts, systems AS fsys, systems AS tsys
            WHERE
            """ + sql_sec_limit + """
            ts.systemid = tsys.systemid AND fs.systemid = fsys.systemid AND
            f.minvolume <= 1 AND t.minvolume <= 1 AND
            f.bid = 0 AND t.bid = 1 AND f.systemid = %(fromt)s AND t.regionid = %(to)s AND t.typeid = f.typeid AND t.stationid = ts.stationid AND
            f.stationid = fs.stationid AND
            age(f.reportedtime) < %(age)s AND age(t.reportedtime) < %(age)s AND
            """ + sql_profit_size + """ >=	%(minprofit)s AND


            types.size <= """ + size + """ AND
            t.typeid = types.typeid
            AND f.typeid = types.typeid
            AND f.price < t.price""", {
                    'fromt': fromt,
                    'to': to,
                    'age': age_t,
                    'minprofit': minprofit,
                })
            cur_f.execute(
                "SELECT systemid,(systemname || ' / ')  || regionname FROM systems,regions WHERE systems.regionid = regions.regionid AND systemid = %s ORDER BY systemname",
                [fromt])
            cur_t.execute(
                "SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname",
                [to])

        trades = []
        time_net = 0.0
        import __builtin__
        from_set = __builtin__.set()
        from_map = {}
        from_to_map = {}
        error_log = []

        fromcache = 0
        if newsearch:

            r = cur.fetchone()
            while r:
                row = SorterDict('sortby')
                row['typeid'] = r[0]
                row['typename'] = r[1]
                fr = int(r[2])
                to2 = int(r[3])
                row['fr'] = int(r[2])
                row['to2'] = int(r[3])
                row['fromstation'] = r[4]
                row['tostation'] = r[5]
                row['pricediff'] = r[6]
                row['tradeable'] = format_long(long(r[7]))
                row['profit'] = format_price(float(r[8]))
                row['profit_num'] = float(r[8])
                row['profit_size'] = float(r[9])
                from_set.add(fr)

                row['tprice'] = format_price(float(r[11]))
                row['fprice'] = format_price(float(r[12]))
                row['tvol'] = format_long(long(r[13]))
                row['fvol'] = format_long(long(r[14]))
                trades.append(row)

                # Add to from_to_map - map source
                if fr in from_to_map:
                    from_to_map[fr].add(row['to2'])
                else:
                    from_to_map[fr] = __builtin__.set()
                    from_to_map[fr].add(row['to2'])

                if fr in from_map:
                    from_map[fr].append(row)
                else:
                    from_map[fr] = []
                    from_map[fr].append(row)

                r = cur.fetchone()

            # Now we try to compute distance
            for compsys in from_set:

                distance_map = {}
                distance = 0
                time_net_ = time.time()

                for tosystem in from_to_map[compsys]:
                    load = urllib.urlopen(
                        'http://127.0.0.1:8081/api/distance/from/' +
                        str(int(compsys)) + '/to/' + str(int(tosystem)))
                    s = load.read()
                    load.close()
                    distance_map[tosystem] = json.loads(s)['distance']

                time_net += time.time() - time_net_

                for row in from_map[compsys]:
                    if row['fr'] == compsys:

                        if compsys == row['to2']:
                            distance = 0
                        else:
                            distance = distance_map[row['to2']]

                        ps = row['profit_size']
                        row['profit_size'] = format_price(ps)

                        if sort == "jprofit":
                            row['sortby'] = ps / (max(0, distance) + 1)
                            t.sort_nice = "Profit per jump"
                        elif sort == "sprofit":
                            row['sortby'] = ps
                            t.sort_nice = "Profit per trip"
                        elif sort == "profit":
                            row['sortby'] = row['profit_num']
                            t.sort_nice = "Profit"
                        elif sort == "jumps":
                            t.sort_nice = "Distance"
                            row['sortby'] = distance
                            row.reverse = True

                        row['profit_jumps'] = format_price(
                            float(ps / (1 + max(0, distance))))
                        row['distance'] = distance

            time_sort = time.time()
            trades.sort(reverse=True)
            time_sort = time.time() - time_sort

            #session['trade_results'] = trades

        else:
            trades = session['trade_results']
            fromcache = 1

        trade_num = len(trades)
        trades = trades[int(startat):int(startat) + int(limit)]

        t.fromcache = fromcache
        t.num_trades = trade_num
        t.trades = trades
        t.fromt = fromt
        t.to = to
        t.qtype = qtype
        t.age = age
        t.minprofit = minprofit
        t.size = size
        t.startat = startat
        t.limit = limit
        t.sort = sort
        t.prefer_sec = prefer_sec
        t.fromname = cur_f.fetchone()[1]
        t.toname = cur_t.fetchone()[1]

        t.page = int((int(startat) + 1) / limit) + 1
        t.next = int(startat) + limit

        t.prev = max(0, int(startat) - limit)

        db.close()

        session.save()

        return t.respond(
        ) + "Pathfinder time: %0.3fs, Sort time: %0.3fs, recompute set was %d elements long<br>" % (
            time_net, time_sort, len(from_set))
Exemplo n.º 28
0
    def test_monodromy(self, nsteps_x, nsteps_y, save=False):
        import os.path, csv
        from csv import DictReader, DictWriter
        import sage.all
        from sage.matrix.special import identity_matrix
        from sage.rings.complex_double import CDF
        from sage.symbolic.constants import e

        x_list = lin_space(self._xmin, self._xmax, nsteps_x)
        y_list = lin_space(self._ymin, self._ymax, nsteps_y)

        tab = [[self.section_f(x, y) for y in y_list] for x in x_list]
        fieldnames = [
            'Exp', 'i', 'j', 'x_plot', 'y_plot', 'dist_id', 'dist_ev'
        ]

        f_name = self.f_name(nsteps_x, nsteps_y, -1, -1)

        if os.path.isfile(f_name):
            with open(f_name, 'r') as csvfile:
                reader = csv.DictReader(csvfile, delimiter=',')
                test = 1
                for row in reader:
                    test = 0
                if test:
                    last_i, last_j = 0, 0
                else:
                    last_i, last_j = int(row['i']), int(row['j'])

        else:
            with open(f_name, 'w') as csvfile:
                writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                writer.writeheader()
                last_i, last_j = 0, 0

        with open(f_name, 'a') as csvfile:
            from cmath import exp, pi

            from __builtin__ import set
            from sage.misc.functional import numerical_approx

            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            if not ((last_i == nsteps_x - 1) and (last_i == nsteps_y - 1)):
                for i in range(last_i, nsteps_x):
                    for j in range(last_j + 1 if i == last_i else 0, nsteps_y):
                        print i, j
                        M0, M1, MInfty = tab[i][j].monodromy_matrices()
                        n = tab[i][j]._dimension
                        beta = tab[i][j]._beta
                        dist_id = (M0 * M1 * MInfty -
                                   identity_matrix(CDF, n)).norm()
                        ev = set(MInfty.eigenvalues())
                        d = []
                        for b in beta:
                            val = exp(-2 * 1j * pi * b)
                            c_val = min(ev, key=lambda z: (z - val).norm())
                            d.append((c_val - val).norm())
                            ev.remove(c_val)
                        dist_ev = max(d)
                        print max(dist_ev, dist_id)
                        writer.writerow({
                            'Exp': tab[i][j],
                            'i': i,
                            'j': j,
                            'x_plot': tab[i][j].x_plot,
                            'y_plot': tab[i][j].y_plot,
                            'dist_id': dist_id,
                            'dist_ev': dist_ev
                        })

        return True
Exemplo n.º 29
0
    def test_monodromy(self, nsteps_x, nsteps_y, save=False):
        import os.path, csv
        from csv import DictReader, DictWriter
        import sage.all
        from sage.matrix.special import identity_matrix
        from sage.rings.complex_double import CDF
        from sage.symbolic.constants import e

        x_list = lin_space(self._xmin, self._xmax, nsteps_x)
        y_list = lin_space(self._ymin, self._ymax, nsteps_y)

        tab = [[self.section_f(x,y) for y in y_list] for x in x_list]
        fieldnames = ['Exp', 'i', 'j', 'x_plot', 'y_plot', 'dist_id', 'dist_ev']

        f_name = self.f_name(nsteps_x, nsteps_y, -1, -1)

        if os.path.isfile(f_name) :
            with open(f_name, 'r') as csvfile :
                reader= csv.DictReader(csvfile, delimiter=',')
                test = 1
                for row in reader :
                    test = 0
                if test:
                    last_i, last_j = 0, 0
                else:
                    last_i, last_j = int(row['i']), int(row['j'])

        else :
            with open(f_name, 'w') as csvfile :
                writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                writer.writeheader()
                last_i, last_j = 0, 0

        with open(f_name, 'a') as csvfile :
            from cmath import exp, pi

            from __builtin__ import set
            from sage.misc.functional import numerical_approx

            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            if not ((last_i == nsteps_x-1) and (last_i == nsteps_y-1)):
                for i in range(last_i,nsteps_x):
                    for j in range(last_j+1 if i == last_i else 0, nsteps_y):
                        print i,j
                        M0, M1, MInfty = tab[i][j].monodromy_matrices()
                        n = tab[i][j]._dimension
                        beta = tab[i][j]._beta
                        dist_id = (M0*M1*MInfty - identity_matrix(CDF,n)).norm()
                        ev = set(MInfty.eigenvalues())
                        d = []
                        for b in beta:
                            val = exp(-2*1j*pi*b)
                            c_val = min(ev, key=lambda z: (z-val).norm())
                            d.append((c_val-val).norm())
                            ev.remove(c_val)
                        dist_ev = max(d)
                        print max(dist_ev, dist_id)
                        writer.writerow({'Exp' : tab[i][j], 'i' : i, 'j' : j,
                                         'x_plot' : tab[i][j].x_plot, 'y_plot' : tab[i][j].y_plot,
                                         'dist_id' : dist_id, 'dist_ev' : dist_ev})

        return True
	def __init__(self, delta, q0, F):
		self.delta = delta
		self.q0 = q0
		self.F = set(F)
Exemplo n.º 31
0
 def _register(self, event, callback):
     """Register subscription from another handler."""
     if event not in self.callbacks:
         self.callbacks[event] = set()
     self.callbacks[event].add(callback)
Exemplo n.º 32
0
def __get_all_modules__(path=convert_to_unicode(sys.path[0])):
    '''
        This method provides a generic way to determine all modules in \
        current package or folder. It is useful for "__init__.py" files.

        **path** - Current working directory.

        Examples:

        >>> __get_all_modules__() # doctest: +ELLIPSIS
        [...]

        >>> from boostnode.extension.file import Handler as FileHandler

        >>> location = FileHandler(
        ...     __test_folder__.path + '__get_all_modules__',
        ...     make_directory=True)
        >>> a = FileHandler(location.path + 'a.py')
        >>> a.content = ''
        >>> FileHandler(
        ...     location.path + 'b.pyc', make_directory=True
        ... ) # doctest: +ELLIPSIS
        Object of "Handler" with path "...__get_all_modu...b.pyc..." (type: ...

        >>> __get_all_modules__(__test_folder__.path + '__get_all_modules__')
        ['a']

        >>> a.remove_file()
        True
        >>> __get_all_modules__(__test_folder__.path + '__get_all_modules__')
        []

        >>> __get_all_modules__('')
        ['highPerformanceModification']
    '''
# # python3.5
# #     if not path:
# #         path = os.getcwd()
# #     return builtins.list(builtins.set(builtins.map(
# #         lambda name: name[:name.rfind('.')],
# #         builtins.filter(
# #             lambda name: (
# #                 (name.endswith('.py') or name.endswith('.pyc')) and
# #                 not name.startswith('__init__.') and os.path.isfile(
# #                     '%s%s%s' % (path, os.sep, name))),
# #             os.listdir(
# #                 path[:-(builtins.len(os.path.basename(path)) + 1)] if
# #                 os.path.isfile(path) else path)))))
    if not path:
        path = os.getcwd()
    return builtins.list(builtins.set(builtins.map(
        lambda name: name[:name.rfind('.')],
        builtins.filter(
            lambda name: (
                (name.endswith('.py') or name.endswith('.pyc')) and
                not name.startswith('__init__.') and os.path.isfile(
                    convert_to_string('%s%s%s' % (path, os.sep, name)))),
            builtins.map(
                lambda name: convert_to_unicode(name), os.listdir(
                    path[:-(builtins.len(os.path.basename(path)) + 1)] if
                    os.path.isfile(path) else path))))))
Exemplo n.º 33
0
import os, __builtin__

plugin_list = __builtin__.set()

for module in os.listdir(os.path.dirname(__file__)):
    if module == '__init__.py' or module[-3:] != '.py':
        continue
    __import__(module[:-3], locals(), globals())
    plugin_list.add(module[:-3])
del module
Exemplo n.º 34
0
 def __init__(self):
     self.src, self.src_any = itree.Itree(), set()
     self.dst, self.dst_any = itree.Itree(), set()
     for k in self.__keys:
         setattr(self, k, {None: set()})
Exemplo n.º 35
0
    def tradefind_display(self, qtype, fromt, to, set = None, age = 24, cashonhand = 10000000.00, minprofit = 100000, size = 10000, startat = 0, limit = 10, newsearch = "0", sort = "jprofit", prefer_sec = "0"):


        mapserver = Pyro.core.getProxyForURI("PYROLOC://localhost:7766/mapserver")


        session = EVCstate()
        t = display.template('tradefind_display.tmpl', session)
        db = evec_func.db_con()
        newsearch = int(newsearch)
        if 'trade_results' not in session:
            newsearch = 1

        cur = db.cursor()

        limit = int(limit)
        prefer_sec = int(prefer_sec)
        recalc_route = False


        if sort not in ['jprofit', 'sprofit', 'profit', 'jumps']:
            return

        if set:
            session['tf_age'] = age
            session['tf_minprofit'] = minprofit
            session['tf_size'] = size
            session['tf_limit'] = limit
            session['tf_sort'] = sort
            session['tf_prefer_sec'] = prefer_sec
            newsearch = 1
        else:
            if 'tf_age' in session:
                age = session['tf_age']
            if 'tf_minprofit' in session:
                minprofit = session['tf_minprofit']
            if 'tf_size' in session:
                size = session['tf_size']
            if 'tf_limit' in session:
                limit = session['tf_limit']
            if 'tf_sort' in session:
                sort = session['tf_sort']
            if 'tf_prefer_sec' in session:
                prefer_sec = session['tf_prefer_sec']
            else:
                prefer_sec = 1


        age_t = str(int(age)) + " hours"

        size = str(int(size))
        cashonhand = str(double(cashonhand))

        sql_profit_size = """    (t.price - f.price)* min(""" + size + """, min(t.volremain,f.volremain) * types.size)/types.size """
        sql_profit_jumps = """   '1' """
        #sql_cash = """ f.price <= $(cashonhand)s """

        sql_sec_limit = """ """
        if prefer_sec:
            sql_sec_limit = """ fsys.security > 0.4 AND tsys.security > 0.4 AND """

        cur_f = db.cursor()
        cur_t = db.cursor()


        # Pure suck query
        if qtype == "Systems" and newsearch:

            cur.execute("""SELECT types.typeid,types.typename,fs.systemid,ts.systemid,fs.stationname,ts.stationname,t.price - f.price AS pricediff,
            min(t.volremain,f.volremain),
            (t.price - f.price)* min(t.volremain,f.volremain) AS profit,
            """ + sql_profit_size + """ AS profit_size,
            """ + sql_profit_jumps + """ AS profit_jumps,
            t.price,f.price,
            t.volremain,f.volremain

            FROM
            types, current_market AS f, current_market AS t, stations AS fs, stations AS ts, systems AS fsys, systems AS tsys
            WHERE
            """ + sql_sec_limit + """
            ts.systemid = tsys.systemid AND fs.systemid = fsys.systemid AND
            f.bid = 0 AND t.bid = 1 AND f.systemid = %(fromt)s AND t.systemid = %(to)s AND t.typeid = f.typeid AND t.stationid = ts.stationid AND
            f.stationid = fs.stationid AND
            age(f.reportedtime) < %(age)s AND age(t.reportedtime) < %(age)s AND
            """ + sql_profit_size + """ >=	%(minprofit)s AND

            types.size <= """ + size + """ AND
            t.typeid = types.typeid
            AND f.typeid = types.typeid
            AND f.price < t.price""",

                        {'fromt':fromt, 'to':to, 'age':age_t, 'minprofit':minprofit,})

            cur_f.execute("SELECT systemid,(systemname || ' / ')  || regionname FROM systems,regions WHERE systems.regionid = regions.regionid AND systemid = %s ORDER BY systemname", [fromt])
            cur_t.execute("SELECT systemid,(systemname || ' / ')  || regionname FROM systems,regions WHERE systems.regionid = regions.regionid AND systemid = %s ORDER BY systemname", [to])


        elif qtype == "Regions" and newsearch:
            cur.execute("""SELECT types.typeid,types.typename,fs.systemid,ts.systemid,fs.stationname,ts.stationname,t.price - f.price AS pricediff,
            min(t.volremain,f.volremain),
            (t.price - f.price)*min(t.volremain,f.volremain) AS profit,


            """+ sql_profit_size + """ AS profit_size,
            """ + sql_profit_jumps + """ AS profit_jumps,


            t.price,f.price, t.volremain, f.volremain

            FROM types, current_market AS f, current_market AS t, stations AS fs, stations AS ts, systems AS fsys, systems AS tsys
            WHERE
            """ + sql_sec_limit + """
            ts.systemid = tsys.systemid AND fs.systemid = fsys.systemid AND
            f.bid = 0 AND t.bid = 1 AND f.regionid = %(fromt)s AND t.regionid = %(to)s AND t.typeid = f.typeid AND t.stationid = ts.stationid AND
            f.stationid = fs.stationid AND
            age(f.reportedtime) < %(age)s AND age(t.reportedtime) < %(age)s AND
            """ + sql_profit_size + """ >=	%(minprofit)s AND

            types.size <= """ + size + """ AND
            t.typeid = types.typeid
            AND f.typeid = types.typeid
            AND f.price < t.price""",
                        {'fromt':fromt, 'to':to, 'age':age_t, 'minprofit':minprofit, })
            cur_f.execute("SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname", [fromt])
            cur_t.execute("SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname", [to])



        elif qtype == "SystemToRegion" and newsearch:
            cur.execute("""SELECT types.typeid,types.typename,fs.systemid,ts.systemid,fs.stationname,ts.stationname,t.price - f.price AS pricediff,
            min(t.volremain,f.volremain),
            (t.price - f.price)*min(t.volremain,f.volremain) AS profit,


            """ + sql_profit_size + """ AS profit_size,
            """ + sql_profit_jumps + """ AS profit_jumps,


            t.price,f.price, t.volremain, f.volremain

            FROM types, current_market AS f, current_market AS t, stations AS fs, stations AS ts, systems AS fsys, systems AS tsys
            WHERE
            """ + sql_sec_limit + """
            ts.systemid = tsys.systemid AND fs.systemid = fsys.systemid AND
            f.bid = 0 AND t.bid = 1 AND f.systemid = %(fromt)s AND t.regionid = %(to)s AND t.typeid = f.typeid AND t.stationid = ts.stationid AND
            f.stationid = fs.stationid AND
            age(f.reportedtime) < %(age)s AND age(t.reportedtime) < %(age)s AND
            """ + sql_profit_size + """ >=	%(minprofit)s AND


            types.size <= """ + size + """ AND
            t.typeid = types.typeid
            AND f.typeid = types.typeid
            AND f.price < t.price""",

                        {'fromt':fromt, 'to':to, 'age':age_t, 'minprofit':minprofit, })
            cur_f.execute("SELECT systemid,(systemname || ' / ')  || regionname FROM systems,regions WHERE systems.regionid = regions.regionid AND systemid = %s ORDER BY systemname", [fromt])
            cur_t.execute("SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname", [to])

        elif qtype == "Global" and newsearch:
            cur.execute("""SELECT types.typeid,types.typename,fs.systemid,ts.systemid,fs.stationname,ts.stationname,t.price - f.price AS pricediff,
            min(t.volremain,f.volremain),
            (t.price - f.price)*min(t.volremain,f.volremain) AS profit,


            """+ sql_profit_size + """ AS profit_size,
            """ + sql_profit_jumps + """ AS profit_jumps,


            t.price,f.price

            FROM types, current_market AS f, current_market AS t, stations AS fs, stations AS ts, systems AS fsys, systems AS tsys
            WHERE
            """ + sql_sec_limit + """
            ts.systemid = tsys.systemid AND fs.systemid = fsys.systemid AND
            f.bid = 0 AND t.bid = 1 AND t.typeid = f.typeid AND t.stationid = ts.stationid AND
            f.stationid = fs.stationid AND
            age(f.reportedtime) < %(age)s AND age(t.reportedtime) < %(age)s AND
            """ + sql_profit_size + """ >=	%(minprofit)s AND

            types.size <= """ + size + """ AND
            t.typeid = types.typeid
            AND f.typeid = types.typeid
            AND f.price < t.price""",
    #    ORDER BY profit_size DESC LIMIT %(limit)s OFFSET %(startat)s""",
                        {'age':age_t, 'minprofit':minprofit})
            cur_f.execute("SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname", [fromt])
            cur_t.execute("SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname", [to])



        trades = []
        time_net = 0.0
        import __builtin__
        from_set = __builtin__.set()
        from_map = {}
        from_to_map = {}
        error_log = []

        fromcache = 0
        if newsearch:

            r = cur.fetchone()
            while r:
                row = SorterDict('sortby')
                row['typeid'] = r[0]
                row['typename'] = r[1]
                fr = int(r[2])
                to2 = int(r[3])
                row['fr'] = int(r[2])
                row['to2'] = int(r[3])
                row['fromstation'] = r[4]
                row['tostation'] = r[5]
                row['pricediff'] = r[6]
                row['tradeable'] = format_long(long(r[7]))
                row['profit'] = format_price(float(r[8]))
                row['profit_size'] = float(r[9])
                from_set.add(fr)






                row['tprice'] = format_price(float(r[11]))
                row['fprice'] = format_price(float(r[12]))
                row['tvol'] = format_long(long(r[13]))
                row['fvol'] = format_long(long(r[14]))
                trades.append(row)

                # Add to from_to_map - map source
                if fr in from_to_map:
                    from_to_map[fr].add(row['to2'])
                else:
                    from_to_map[fr] = __builtin__.set()
                    from_to_map[fr].add(row['to2'])

                if fr in from_map:
                    from_map[fr].append(row)
                else:
                    from_map[fr] = []
                    from_map[fr].append(row)

                r = cur.fetchone()


            # Now we try to compute distance



            for compsys in from_set:

                distance_map = {}
                distance = 0


                time_net_ = time.time()

                distance_map = mapserver.route_comp(compsys, list(from_to_map[compsys]))

                time_net += time.time() - time_net_

                for row in from_map[compsys]:
                    if row['fr'] == compsys:

                        if compsys == row['to2']:
                            distance = 0
                        else:
                            distance = distance_map[row['to2']]

                        ps = row['profit_size']


                        row['profit_size'] = format_price(ps)

                        if sort == "jprofit":
                            row['sortby'] = ps/(max(0,distance)+1)
                            t.sort_nice = "Profit per jump"
                        elif sort == "sprofit":
                            row['sortby'] = ps
                            t.sort_nice = "Profit per trip"
                        elif sort == "profit":
                            row['sortby'] = row['profit']
                            t.sort_nice = "Profit"
                        elif sort == "jumps":
                            t.sort_nice = "Distance"
                            row['sortby'] = distance
                            row.reverse= True

                        row['profit_jumps'] = format_price(float(ps/(1+max(0,distance))))
                        row['distance'] = distance


            time_sort = time.time()
            trades.sort(reverse=True)
            time_sort = time.time() - time_sort

            #session['trade_results'] = trades

        else:
            trades = session['trade_results']
            fromcache = 1

        trade_num = len(trades)
        trades = trades[int(startat):int(startat)+int(limit)]

        t.fromcache = fromcache
        t.num_trades = trade_num
        t.trades = trades
        t.fromt = fromt
        t.to = to
        t.qtype = qtype
        t.age = age
        t.minprofit = minprofit
        t.size = size
        t.startat = startat
        t.limit = limit
        t.sort = sort
        t.prefer_sec = prefer_sec
        t.fromname = cur_f.fetchone()[1]
        t.toname = cur_t.fetchone()[1]

        t.page = int((int(startat) + 1) / limit) + 1
        t.next = int(startat) +limit

        t.prev = max(0,int(startat) - limit)

        db.close()

        session.save()

        return t.respond() +"Pathfinder time: %0.3fs, Sort time: %0.3fs, recompute set was %d elements long<br>" % (time_net, time_sort, len(from_set))
Exemplo n.º 36
0
    def tradefind_display(
        self,
        qtype,
        fromt,
        to,
        set=None,
        age=24,
        cashonhand=10000000.00,
        minprofit=100000,
        size=10000,
        startat=0,
        limit=50,
        newsearch="0",
        sort="jprofit",
        prefer_sec="0",
    ):

        session = EVCstate()
        t = display.template("tradefind_display.tmpl", session)
        db = evec_func.db_con()
        newsearch = int(newsearch)
        if "trade_results" not in session:
            newsearch = 1

        cur = db.cursor()

        limit = int(limit)
        prefer_sec = int(prefer_sec)
        recalc_route = False

        if sort not in ["jprofit", "sprofit", "profit", "jumps"]:
            return

        if set:
            session["tf_age"] = age
            session["tf_minprofit"] = minprofit
            session["tf_size"] = size
            session["tf_limit"] = limit
            session["tf_sort"] = sort
            session["tf_prefer_sec"] = prefer_sec
            newsearch = 1
        else:
            if "tf_age" in session:
                age = session["tf_age"]
            if "tf_minprofit" in session:
                minprofit = session["tf_minprofit"]
            if "tf_size" in session:
                size = session["tf_size"]
            if "tf_limit" in session:
                limit = session["tf_limit"]
            if "tf_sort" in session:
                sort = session["tf_sort"]
            if "tf_prefer_sec" in session:
                prefer_sec = session["tf_prefer_sec"]
            else:
                prefer_sec = 1

        age_t = str(int(age)) + " hours"

        size = str(int(size))
        cashonhand = str(double(cashonhand))

        sql_profit_size = (
            """    (t.price - f.price)* min(""" + size + """, min(t.volremain,f.volremain) * types.size)/types.size """
        )
        sql_profit_jumps = """   '1' """
        # sql_cash = """ f.price <= $(cashonhand)s """

        sql_sec_limit = """ """
        if prefer_sec:
            sql_sec_limit = """ fsys.security > 0.4 AND tsys.security > 0.4 AND """

        cur_f = db.cursor()
        cur_t = db.cursor()

        #####
        # Warning: This is easily the worst piece of code I have ever written.
        # I really am sorry.
        #####

        # Pure suck query
        if qtype == "Systems" and newsearch:

            cur.execute(
                """SELECT types.typeid,types.typename,fs.systemid,ts.systemid,fs.stationname,ts.stationname,t.price - f.price AS pricediff,
            min(t.volremain,f.volremain),
            (t.price - f.price)* min(t.volremain,f.volremain) AS profit,
            """
                + sql_profit_size
                + """ AS profit_size,
            """
                + sql_profit_jumps
                + """ AS profit_jumps,
            t.price,f.price,
            t.volremain,f.volremain

            FROM
            types, current_market AS f, current_market AS t, stations AS fs, stations AS ts, systems AS fsys, systems AS tsys
            WHERE
            """
                + sql_sec_limit
                + """
            ts.systemid = tsys.systemid AND fs.systemid = fsys.systemid AND
            f.minvolume <= 1 AND t.minvolume <= 1 AND
            f.bid = 0 AND t.bid = 1 AND f.systemid = %(fromt)s AND t.systemid = %(to)s AND t.typeid = f.typeid AND t.stationid = ts.stationid AND
            f.stationid = fs.stationid AND
            age(f.reportedtime) < %(age)s AND age(t.reportedtime) < %(age)s AND
            """
                + sql_profit_size
                + """ >=	%(minprofit)s AND

            types.size <= """
                + size
                + """ AND
            t.typeid = types.typeid
            AND f.typeid = types.typeid
            AND f.price < t.price""",
                {"fromt": fromt, "to": to, "age": age_t, "minprofit": minprofit},
            )

            cur_f.execute(
                "SELECT systemid,(systemname || ' / ')  || regionname FROM systems,regions WHERE systems.regionid = regions.regionid AND systemid = %s ORDER BY systemname",
                [fromt],
            )
            cur_t.execute(
                "SELECT systemid,(systemname || ' / ')  || regionname FROM systems,regions WHERE systems.regionid = regions.regionid AND systemid = %s ORDER BY systemname",
                [to],
            )

        elif qtype == "Regions" and newsearch:
            cur.execute(
                """SELECT types.typeid,types.typename,fs.systemid,ts.systemid,fs.stationname,ts.stationname,t.price - f.price AS pricediff,
            min(t.volremain,f.volremain),
            (t.price - f.price)*min(t.volremain,f.volremain) AS profit,


            """
                + sql_profit_size
                + """ AS profit_size,
            """
                + sql_profit_jumps
                + """ AS profit_jumps,


            t.price,f.price, t.volremain, f.volremain

            FROM types, current_market AS f, current_market AS t, stations AS fs, stations AS ts, systems AS fsys, systems AS tsys
            WHERE
            """
                + sql_sec_limit
                + """
            ts.systemid = tsys.systemid AND fs.systemid = fsys.systemid AND
            f.minvolume <= 1 AND t.minvolume <= 1 AND
            f.bid = 0 AND t.bid = 1 AND f.regionid = %(fromt)s AND t.regionid = %(to)s AND t.typeid = f.typeid AND t.stationid = ts.stationid AND
            f.stationid = fs.stationid AND
            age(f.reportedtime) < %(age)s AND age(t.reportedtime) < %(age)s AND
            """
                + sql_profit_size
                + """ >=	%(minprofit)s AND

            types.size <= """
                + size
                + """ AND
            t.typeid = types.typeid
            AND f.typeid = types.typeid
            AND f.price < t.price""",
                {"fromt": fromt, "to": to, "age": age_t, "minprofit": minprofit},
            )
            cur_f.execute("SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname", [fromt])
            cur_t.execute("SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname", [to])

        elif qtype == "SystemToRegion" and newsearch:
            cur.execute(
                """SELECT types.typeid,types.typename,fs.systemid,ts.systemid,fs.stationname,ts.stationname,t.price - f.price AS pricediff,
            min(t.volremain,f.volremain),
            (t.price - f.price)*min(t.volremain,f.volremain) AS profit,


            """
                + sql_profit_size
                + """ AS profit_size,
            """
                + sql_profit_jumps
                + """ AS profit_jumps,


            t.price,f.price, t.volremain, f.volremain

            FROM types, current_market AS f, current_market AS t, stations AS fs, stations AS ts, systems AS fsys, systems AS tsys
            WHERE
            """
                + sql_sec_limit
                + """
            ts.systemid = tsys.systemid AND fs.systemid = fsys.systemid AND
            f.minvolume <= 1 AND t.minvolume <= 1 AND
            f.bid = 0 AND t.bid = 1 AND f.systemid = %(fromt)s AND t.regionid = %(to)s AND t.typeid = f.typeid AND t.stationid = ts.stationid AND
            f.stationid = fs.stationid AND
            age(f.reportedtime) < %(age)s AND age(t.reportedtime) < %(age)s AND
            """
                + sql_profit_size
                + """ >=	%(minprofit)s AND


            types.size <= """
                + size
                + """ AND
            t.typeid = types.typeid
            AND f.typeid = types.typeid
            AND f.price < t.price""",
                {"fromt": fromt, "to": to, "age": age_t, "minprofit": minprofit},
            )
            cur_f.execute(
                "SELECT systemid,(systemname || ' / ')  || regionname FROM systems,regions WHERE systems.regionid = regions.regionid AND systemid = %s ORDER BY systemname",
                [fromt],
            )
            cur_t.execute("SELECT regionid,regionname FROM regions WHERE regionid = %s ORDER BY regionname", [to])

        trades = []
        time_net = 0.0
        import __builtin__

        from_set = __builtin__.set()
        from_map = {}
        from_to_map = {}
        error_log = []

        fromcache = 0
        if newsearch:

            r = cur.fetchone()
            while r:
                row = SorterDict("sortby")
                row["typeid"] = r[0]
                row["typename"] = r[1]
                fr = int(r[2])
                to2 = int(r[3])
                row["fr"] = int(r[2])
                row["to2"] = int(r[3])
                row["fromstation"] = r[4]
                row["tostation"] = r[5]
                row["pricediff"] = format_price(r[6])
                row["tradeable"] = format_long(r[7])
                row["profit"] = format_price(r[8])
                row["profit_num"] = float(r[8])
                row["profit_size"] = float(r[9])
                from_set.add(fr)

                row["tprice"] = format_price(r[11])
                row["fprice"] = format_price(r[12])
                row["tvol"] = format_long(r[13])
                row["fvol"] = format_long(r[14])
                trades.append(row)

                # Add to from_to_map - map source
                if fr in from_to_map:
                    from_to_map[fr].add(row["to2"])
                else:
                    from_to_map[fr] = __builtin__.set()
                    from_to_map[fr].add(row["to2"])

                if fr in from_map:
                    from_map[fr].append(row)
                else:
                    from_map[fr] = []
                    from_map[fr].append(row)

                r = cur.fetchone()

            # Now we try to compute distance
            for compsys in from_set:

                distance_map = {}
                distance = 0
                time_net_ = time.time()

                for tosystem in from_to_map[compsys]:
                    load = urllib.urlopen(
                        "http://127.0.0.1:8081/api/distance/from/" + str(int(compsys)) + "/to/" + str(int(tosystem))
                    )
                    s = load.read()
                    load.close()
                    distance_map[tosystem] = json.loads(s)["distance"]

                time_net += time.time() - time_net_

                for row in from_map[compsys]:
                    if row["fr"] == compsys:

                        if compsys == row["to2"]:
                            distance = 0
                        else:
                            distance = distance_map[row["to2"]]

                        ps = row["profit_size"]
                        row["profit_size"] = format_price(ps)

                        if sort == "jprofit":
                            row["sortby"] = ps / (max(0, distance) + 1)
                            t.sort_nice = "Profit per jump"
                        elif sort == "sprofit":
                            row["sortby"] = ps
                            t.sort_nice = "Profit per trip"
                        elif sort == "profit":
                            row["sortby"] = row["profit_num"]
                            t.sort_nice = "Profit"
                        elif sort == "jumps":
                            t.sort_nice = "Distance"
                            row["sortby"] = distance
                            row.reverse = True

                        row["profit_jumps"] = format_price(ps / (1 + max(0, distance)))
                        row["distance"] = distance

            time_sort = time.time()
            trades.sort(reverse=True)
            time_sort = time.time() - time_sort

            # session['trade_results'] = trades

        else:
            trades = session["trade_results"]
            fromcache = 1

        trade_num = len(trades)
        trades = trades[int(startat) : int(startat) + int(limit)]

        t.fromcache = fromcache
        t.num_trades = trade_num
        t.trades = trades
        t.fromt = fromt
        t.to = to
        t.qtype = qtype
        t.age = age
        t.minprofit = minprofit
        t.size = size
        t.startat = startat
        t.limit = limit
        t.sort = sort
        t.prefer_sec = prefer_sec
        t.fromname = cur_f.fetchone()[1]
        t.toname = cur_t.fetchone()[1]

        t.page = int((int(startat) + 1) / limit) + 1
        t.next = int(startat) + limit

        t.prev = max(0, int(startat) - limit)

        db.close()

        session.save()

        return t.respond() + "Pathfinder time: %0.3fs, Sort time: %0.3fs, recompute set was %d elements long<br>" % (
            time_net,
            time_sort,
            len(from_set),
        )
Exemplo n.º 37
0
 def alphabet(self):
     sigma = reduce(lambda a, b: set(a) | set(b),
                    [x.keys() for x in self.delta.values()])
     return sigma
Exemplo n.º 38
0
 def clean_set(self):
     sett = self.cleaned_data['set']
     if not set(sett.types.all()).isdisjoint(self.service.output.types.all()):
         raise ValidationError("input set must be disjoint from output set")
     return sett
Exemplo n.º 39
0
 def __init__(self):
     self.src, self.src_any = itree.Itree(), set()
     self.dst, self.dst_any = itree.Itree(), set()
     for k in self.__keys:
         setattr(self, k, { None: set() })
	def alphabet(self):
		sigma = reduce(lambda a,b:set(a) | set(b), [x.keys() for x in self.delta.values()])
		return sigma
Exemplo n.º 41
0
 def __init__(self, delta, q0, F):
     self.delta = delta
     self.q0 = q0
     self.F = set(F)
	def states(self):
		d = set([self.q0]) | set(self.delta.keys()) | reduce(lambda a,b:a | b, reduce(lambda a,b:a + b, [x.values() for x in self.delta.values()]))
		return d
Exemplo n.º 43
0
 def states(self):
     d = set([self.q0]) | set(self.delta.keys()) | reduce(
         lambda a, b: a | b,
         reduce(lambda a, b: a + b,
                [x.values() for x in self.delta.values()]))
     return d
Exemplo n.º 44
0
def as_set(iterable):
    return builtins.set(iterable)