Example #1
0
 def test_contains(self):
     self.assertRaises(TypeError, operator.contains)
     self.assertRaises(TypeError, operator.contains, None, None)
     self.assertTrue(operator.contains(range(4), 2))
     self.assertFalse(operator.contains(range(4), 5))
     self.assertTrue(operator.sequenceIncludes(range(4), 2))
     self.assertFalse(operator.sequenceIncludes(range(4), 5))
Example #2
0
            def evaluate(cond): # Method to evaluate the conditions
                if isinstance(cond,bool):
                    return cond
                left, oper, right = cond
                if not model or not left in model.mgroup.fields:  #check that the field exist
                    return False

                oper = self.OPERAND_MAPPER.get(oper.lower(), oper)
                if oper == '=':
                    res = operator.eq(model[left].get(model),right)
                elif oper == '!=':
                    res = operator.ne(model[left].get(model),right)
                elif oper == '<':
                    res = operator.lt(model[left].get(model),right)
                elif oper == '>':
                    res = operator.gt(model[left].get(model),right)
                elif oper == '<=':
                    res = operator.le(model[left].get(model),right)
                elif oper == '>=':
                    res = operator.ge(model[left].get(model),right)
                elif oper == 'in':
                    res = operator.contains(right, model[left].get(model))
                elif oper == 'not in':
                    res = operator.contains(right, model[left].get(model))
                    res = operator.not_(res)
                return res
Example #3
0
 def test_contains(self):
     self.assertRaises(TypeError, operator.contains)
     self.assertRaises(TypeError, operator.contains, None, None)
     self.assertTrue(operator.contains(list(range(4)), 2))
     self.assertFalse(operator.contains(list(range(4)), 5))
     self.assertTrue(operator.contains(list(range(4)), 2))
     self.assertFalse(operator.contains(list(range(4)), 5))
Example #4
0
    def message(self, args):
        line = []
        # Üldvorming
        # aeg 14*14DIGIT
        if contains(args, 'timestamp'):
            line = [args['timestamp']]
        else:
            line = [time.strftime("%Y%m%d%H%M%S")]
        # Hääle räsi 28*28BASE64-CHAR
        if not contains(args, 'haal_rasi'):
            if contains(args, 'haal'):
                args['haal_rasi'] = ksum.votehash(args['haal'])
        line.append(args['haal_rasi'])

        # omavalitsuse-number 1*10DIGIT
        line.append(str(args['ringkond_omavalitsus']))
        # suhteline-ringkonna-number 1*10DIGIT
        line.append(str(args['ringkond']))
        # omavalitsuse-number 1*10DIGIT
        line.append(str(args['jaoskond_omavalitsus']))
        # suhteline-valimisjaoskonna-number 1*10DIGIT
        line.append(str(args['jaoskond']))

        if args['tyyp'] in [1, 2, 3]:
            #*1valija-andmed =
            #isikukood 11*11DIGIT
            line.append(str(args['isikukood']))
        if args['tyyp'] == 2:
            # pohjus 1*100UTF-8-CHAR
            line.append(args['pohjus'])
        logstring = "\t".join(line) + "\n"

        return logstring
Example #5
0
 def test_contains(self):
     self.failUnlessRaises(TypeError, operator.contains)
     self.failUnlessRaises(TypeError, operator.contains, None, None)
     self.failUnless(operator.contains(range(4), 2))
     self.failIf(operator.contains(range(4), 5))
     self.failUnless(operator.sequenceIncludes(range(4), 2))
     self.failIf(operator.sequenceIncludes(range(4), 5))
def sort_clusters(clusters, tag_index, ground_truth_clusters):
    def measure(cluster):
        tag_match = cluster.tag_accuracy
        cluster_size = cluster.cluster.number_of_sources
        lo = len(cluster.cluster.label_overlap)
        so = len(cluster.cluster.source_overlap)
        wc = cluster.cluster.number_of_words
        return wc + ((so + 1) * 10) + ((lo + 1) * 100) + ((cluster_size + 1) * 1000) + \
            ((tag_match + 1) * 1000000) + ((cluster.ground_truth_best_match + 1) * 10000000)

    measured_clusters = []
    for cluster in clusters:
        measured_cluster = MeasuredCluster()
        measured_cluster.cluster = cluster
        tag_list = []
        for source in cluster.sources:
            tag_list.append(string_to_phrase(tag_index[source].replace('-', ' ')))
        measured_cluster.tag_accuracy = len(common(map(lambda cluster: cluster, tag_list)))

        for y in ground_truth_clusters.keys(): ## iterate through all tags
            if contains(cluster.sources, ground_truth_clusters[y]) and \
                    contains(ground_truth_clusters[y], cluster.sources):  ## and vice versa??
                tag_list = []
                for source in cluster.sources:
                    tag_list.append(string_to_phrase(tag_index[source].replace('-', ' ')))
                tag_list.append(string_to_phrase(y.replace('-', ' ')))
                d = len(common(map(lambda cluster: cluster, tag_list)))
                if d > measured_cluster.ground_truth_best_match:
                    measured_cluster.ground_truth_best_match = d

        measured_clusters.append(measured_cluster)

    measured_clusters.sort(reverse=True, key=measure)
    return measured_clusters
Example #7
0
 def test_contains(self):
     self.assertRaises(TypeError, operator.contains)
     self.assertRaises(TypeError, operator.contains, None, None)
     self.assertTrue(operator.contains(range(4), 2))
     self.assertFalse(operator.contains(range(4), 5))
     with test_support.check_py3k_warnings():
         self.assertTrue(operator.sequenceIncludes(range(4), 2))
         self.assertFalse(operator.sequenceIncludes(range(4), 5))
Example #8
0
def in_(a, b):
    if isinstance(a, (list, tuple)):
        if isinstance(b, (list, tuple)):
            return any(operator.contains(b, x) for x in a)
        else:
            return operator.contains(a, b)
    else:
        return operator.contains(b, a)
Example #9
0
def main(args):
    global OUTPUT_FOLDER_NAME, DOWNLOAD_DIRECTORY, FILE_FORMATS

    url = urlp.urlparse(args.url)

    if not url.scheme:
        print_formatted("Invalid url, exiting...", ERROR)
        return 1

    if args.folder:
        OUTPUT_FOLDER_NAME = args.folder
        DOWNLOAD_DIRECTORY = DOWNLOAD_DIRECTORY = (
            (os.getcwd() if not os.getcwd().endswith("/") else os.getcwd()[:-1]) + "/" + OUTPUT_FOLDER_NAME + "/"
        )

    print_formatted("Downloading images from thread: " + str(args.url).split("/")[-1], DEFAULT)

    if contains(os.listdir(os.getcwd()), OUTPUT_FOLDER_NAME):
        choice = save_choices()
        if choice:
            pass
        else:
            print_formatted("Exiting...", DEFAULT)
            return 1
    else:
        print_formatted("Creating directory " + OUTPUT_FOLDER_NAME + "...", DEFAULT)
        os.makedirs(OUTPUT_FOLDER_NAME)

    print_formatted('Using "\\' + OUTPUT_FOLDER_NAME + '\\" as the output folder', DEFAULT)
    try:
        response = urllib2.urlopen(args.url)
    except:
        print_formatted("Cannot open url, exiting...", ERROR)
        sys.exit(1)

    soup = BeautifulSoup(response)
    image_a_tags = soup.findAll("a", {"class": "fileThumb"})

    total = len(image_a_tags)
    print_formatted("found " + str(total) + " images", DEFAULT)
    for file_format in FILE_FORMATS:
        count = count_extensions(file_format, image_a_tags)
        print "---" + str(count) + " " + str(file_format) + " " + pluralize(count) + " found"

    existing_files = os.listdir(os.getcwd() + "/" + OUTPUT_FOLDER_NAME)
    to_download_files = []
    for img_href in image_a_tags:
        href = img_href["href"]
        if not contains(existing_files, href.split("/")[-1]):
            to_download_files.append(img_href)

    if not to_download_files:
        print_formatted('No new files to download to directory "\\' + OUTPUT_FOLDER_NAME + '\\", exiting...')
        return 0

    download_files(to_download_files)

    print_formatted("Finished downloading images", DEFAULT)
Example #10
0
def discover():
    print textwrap.dedent(
        """
fuzz [discover | test] url OPTIONS

COMMANDS:
  discover  Output a comprehensive, human-readable list of all discovered inputs to the system. Techniques include both crawling and guessing.
  test      Discover all inputs, then attempt a list of exploit vectors on those inputs. Report potential vulnerabilities.

OPTIONS:
  --custom-auth=string     Signal that the fuzzer should use hard-coded authentication for a specific application (e.g. dvwa). Optional.

  Discover options:
    --common-words=file    Newline-delimited file of common words to be used in page guessing and input guessing. Required.

  Test options:
    --vectors=file         Newline-delimited file of common exploits to vulnerabilities. Required.
    --sensitive=file       Newline-delimited file data that should never be leaked. It's assumed that this data is in the application's database (e.g. test data), but is not reported in any response. Required.
    --random=[true|false]  When off, try each input to each page systematically.  When on, choose a random page, then a random input field and test all vectors. Default: false.
    --slow=500             Number of milliseconds considered when a response is considered "slow". Default is 500 milliseconds
        """
    )
    link = raw_input('Please Enter Command: ')
    info = link.split()
    command = info[0].lower()
    url = info[1].lower()
    options = info[2:]
    for x in options:
        if operator.contains(x, '--custom-auth='):
            print ('Custom Authentication: ')
            custAuth(x.split('=')[1])
        if operator.contains(x, '--common-words='):
            print('Searching Common Words: ')
            guessPages(x.split('=')[1], url)
    if operator.eq(command, 'discover'):
        #discover functionality
        print('Discovering Links: ')
        getLinks(url)
        print('Parse URLs: ')
        parseURL(url)
        print('Discovering Cookies: ')
        getCookies(url)
        print('Get Inputs: ')
        getInputs(url)
    else:
        #test functionality
        print ('Discovering All Inputs: ')
        getInputs(url)
        print ('Checking For Sensitive Data: ')
        for x in options:
            if operator.contains(x, '--vectors='):
                print('Looking For Potential Threats In Vectors: ')
                #senseData(url, x.split('=')[1])
            if operator.contains(x, '--sensitive='):
                print('Looking For Potential Sensitive Information: ')
                senseData(url, x.split('=')[1])
        print ('Checking For Sanitized Data: ')
        lackSanitize(url, 'escape_chars.txt')
Example #11
0
 def test_operator(self):
     import operator
     self.assertIs(operator.truth(0), False)
     self.assertIs(operator.truth(1), True)
     self.assertIs(operator.not_(1), False)
     self.assertIs(operator.not_(0), True)
     self.assertIs(operator.contains([], 1), False)
     self.assertIs(operator.contains([1], 1), True)
     self.assertIs(operator.lt(0, 0), False)
     self.assertIs(operator.lt(0, 1), True)
     self.assertIs(operator.is_(True, True), True)
     self.assertIs(operator.is_(True, False), False)
     self.assertIs(operator.is_not(True, True), False)
     self.assertIs(operator.is_not(True, False), True)
Example #12
0
    def apply_filters(self, records, filters):
        """Filter the specified records, using basic iteration.
        """
        operators = {
            COMPARISON.LT: operator.lt,
            COMPARISON.MAX: operator.le,
            COMPARISON.EQ: operator.eq,
            COMPARISON.NOT: operator.ne,
            COMPARISON.MIN: operator.ge,
            COMPARISON.GT: operator.gt,
            COMPARISON.IN: operator.contains,
            COMPARISON.EXCLUDE: lambda x, y: not operator.contains(x, y),
        }

        for record in records:
            matches = True
            for f in filters:
                left = record.get(f.field)
                right = f.value
                if f.operator in (COMPARISON.IN, COMPARISON.EXCLUDE):
                    right = left
                    left = f.value
                matches = matches and operators[f.operator](left, right)
            if matches:
                yield record
Example #13
0
    def __iter__(self):
        """This method is a way to sort and filter an object easily. The
        keyword arguments, order_by and cmp are used for sorting while
        everything else is used by __checker__ for filtering. The order_by
        keyword may be either a string or a function. If it is a string
        then we create a function which gets that key from the entities
        in obj and uses that as the key function in sorting. All other keywords
        are used as described in __checker__. If no keywords are given then
        the object is simply returned.

        :obj: An iterable to filter and sort
        :kwargs: Keyword arguments for filtering and sorting.
        """

        filtered = iter(self.obj)
        for key, value in self.options.items():
            if key == '_':
                filtered = it.ifilter(value, filtered)
            elif callable(value):
                filtered = it.ifilter(self.__callable_filter__(key, value),
                                      filtered)
            elif isinstance(value, (list, set, tuple)):
                func = self.__basic_filter__(key, set(value),
                                             lambda a, b: op.contains(b, a))
                filtered = it.ifilter(func, filtered)
            else:
                func = self.__basic_filter__(key, value, op.eq)
                filtered = it.ifilter(func, filtered)

        return filtered
	def visitInfixOperatorNode( self, node ):	 
		lhs = self._contextUnwrap( node.leftOperand.accept( self ) )
		rhs = self._contextUnwrap( node.rightOperand.accept( self ) )
		
		if node.operator == 'and':
			return bool(lhs) and bool(rhs)
		elif node.operator == 'or':
			return bool(lhs) or bool(rhs)
		elif node.operator == 'in':
			# TODO: deep unwrap
			return operator.contains( self._deepContextUnwrap(rhs),
			                          self._deepContextUnwrap(lhs) )
		else:
			func = {
				'|': operator.or_,
				'&': operator.and_,
				'^': operator.xor,
				'+': operator.add,
				'-': operator.sub,
				'*': operator.mul,
				'/': operator.div,
				'%': operator.mod,
				'<': operator.lt,
				'>': operator.gt,
				'<=': operator.le,
				'>=': operator.ge,
				'==': operator.eq,
				'!=': operator.ne,
				'<<': operator.lshift,
				'>>': operator.rshift,
			}[node.operator]
			return func( lhs, rhs )
Example #15
0
def get_avalide_inavalide_shipping_values_from_lines(shipping_lines):
    """"将输入的数据 进行检查,并返回有效和无效的数据"""
    inavalide_shipping_lines = []
    avalide_shipping_lines = []
    avalide_shipping_out_sid = []

    for ship_line in shipping_lines.split('\n'):
        shipping_values = get_avalide_shipping_line_data(ship_line)
        if shipping_values:
            #去除重复项,并将其放到错误数据中
            if contains(avalide_shipping_out_sid, shipping_values[0]):
                avalide_shipping_out_sid.remove(shipping_values[0])

                for sp_value in avalide_shipping_lines:
                    if sp_value[0] == shipping_values[0]:
                        avalide_shipping_lines.remove(sp_value)
                        inavalide_shipping_lines.append(
                            u'重复项 %s' % str(sp_value))
                        inavalide_shipping_lines.append(
                            u'重复项 %s' % str(shipping_values))
            #如果没有重复项,则添加out_sid到avalide_shipping_out_sid中
            else:
                avalide_shipping_out_sid.append(shipping_values[0])

            avalide_shipping_lines.append(shipping_values)
        else:
            inavalide_shipping_lines.append(ship_line)

    return avalide_shipping_lines, inavalide_shipping_lines
Example #16
0
        def encode(cls, iterable, result):
            '''Given an `iterable` string, send each character in a printable form to `result`.'''

            # construct a transformer that writes characters to result
            escape = internal.utils.character.escape(result); next(escape)

            # send the key prefix
            result.send(cls.prefix)

            # now we can actually process the string
            for ch in iterable:

                # first check if character has an existing key mapping
                if operator.contains(cls.mappings, ch):
                    for ch in operator.getitem(cls.mappings, ch):
                        result.send(ch)

                # otherwise pass it to the regular escape function
                else:
                    escape.send(ch)

                continue

            # submit the suffix and we're good
            result.send(cls.suffix)
            return
Example #17
0
 def apply_filters(self, records, filters):
     """Filter the specified records, using basic iteration.
     """
     operators = {
         COMPARISON.LT: operator.lt,
         COMPARISON.MAX: operator.le,
         COMPARISON.EQ: operator.eq,
         COMPARISON.NOT: operator.ne,
         COMPARISON.MIN: operator.ge,
         COMPARISON.GT: operator.gt,
         COMPARISON.IN: operator.contains,
         COMPARISON.EXCLUDE: lambda x, y: not operator.contains(x, y),
     }
     for record in records:
         matches = True
         for f in filters:
             left = record.get(f.field)
             right = f.value
             if f.operator in (COMPARISON.IN, COMPARISON.EXCLUDE):
                 right = left
                 left = f.value
             else:
                 # Python3 cannot compare None to other value.
                 if left is None:
                     if f.operator in (COMPARISON.GT, COMPARISON.MIN):
                         matches = False
                         continue
                     elif f.operator in (COMPARISON.LT, COMPARISON.MAX):
                         continue  # matches = matches and True
             matches = matches and operators[f.operator](left, right)
         if matches:
             yield record
def filtered_list(l, key, value, op='eq'):
    '''Filter list items based on their value in a specific key/attr.
    '''

    if not l:
        return l
    
    op_map = {
        'ne': operator.ne,
        'eq': operator.eq,
        'le': operator.ge,
        'lt': operator.gt,
        'ge': operator.le,
        'gt': operator.lt,
        '!=': operator.ne,
        '==': operator.eq,
        '<=': operator.ge,
        '<':  operator.gt,
        '>=': operator.le,
        '>':  operator.lt,
        'in': operator.contains,
        'not-in': lambda a, b: not operator.contains(a, b) 
    }
   
    op = op_map.get(str(op).lower(), operator.eq)

    items_are_dicts = operator.isMappingType(l[0])
    if items_are_dicts:
        def pred(x):
            return op(value, x.get(key))
    else:
        def pred(x):
            return op(value, getattr(x, key, None))
    
    return filter(pred, l)
Example #19
0
def get_operator_function(name):
    lookup = {
        'And': and_operator,
        'Or': or_operator,
        'Add': add_operator,
        'In': operator.contains,
        'NotIn': lambda x, y: not operator.contains(x, y),
        'Div': operator.truediv,
        'FloorDiv': operator.floordiv,
        'BitAnd': operator.and_,
        'BitXor': operator.xor,
        'Invert': operator.invert,
        'BitOr': operator.or_,
        'Pow': operator.pow,
        'Is': operator.is_,
        'IsNot': operator.is_not,
        'LShift': operator.lshift,
        'Mod': mod_operator,
        'Mult': operator.mul,
        'USub': operator.neg,
        'Not': operator.not_,
        'UAdd': operator.pos,
        'RShift': operator.rshift,
        'Repeat': operator.repeat,  # special
        'Sub': operator.sub,
        'Lt': comparison(operator.lt),
        'LtE': comparison(operator.le),
        'Eq': operator.eq,
        'NotEq': operator.ne,
        'GtE': comparison(operator.ge),
        'Gt': comparison(operator.gt),
    }
    return lookup.get(name)
Example #20
0
    def contains(self, thing):
        '''
        if `thing` is found in incoming things

        @param thing: some thing
        '''
        with self._context():
            return self._append(contains(self._iterable, thing))
Example #21
0
 def final(self, priority_name):
     base_hostlist = self.base_hostlist
     if priority_name:
         base_hostlist = sorted(
             base_hostlist,
             key=lambda x: contains(x[0], priority_name),
             reverse=True)
     return base_hostlist
Example #22
0
def enabledDomaen(domaen):
    domaens = ["000.net", "00.com", "0000000.net","gbicom.cn"]
    if os.path.exists("domaens.txt"):
        fh = open("domaens.txt","r")
        for line in fh:
            domaens.append(line.replace("\n",""))
        fh.close()
    return operator.contains(domaens, domaen)
Example #23
0
	def add_object(self, name, instance):
		from operator import contains
		from Exceptions import GlobalStoreObjectExistsError
		if contains(self.__object_dictionary.keys(), name): raise GlobalStoreObjectExistsError
		from utils import generate_random_number
		object_id = generate_random_number(map(lambda x: x[1],self.__object_dictionary.values()))
		self.__object_dictionary[name] = instance, object_id
		return object_id
Example #24
0
 def calculateProbs(self):
     
     # Start with P(F) and P(H)
     fluTweets = list()
     healthTweets = list()
     
     # Split tweets into two lists. Flu and not Flu
     data.numFluTweets = 0
     for tweetNum, cat in data.categorization.iteritems():
         data.numFluTweets += cat
         if(cat == 0):
             healthTweets.append(data.tweets[tweetNum])
         else:
             fluTweets.append(data.tweets[tweetNum])
     
     data.numHealthyTweets = data.tweetsStored - data.numFluTweets
     
     data.probOfFlu = float(data.numFluTweets) / float(data.tweetsStored)
     
     print(data.probOfFlu)
     
     # Create a list of all words that occur in the training data
     for tweet in data.tweets:
         words = re.findall(r'\w+', tweet)
         
         for word in words:
             if(not word in data.words):
                 data.words.append(word)
                 
     
     # For each word:
     for word in data.words:
         # Get prob word given flu
         isWordInFluTweets = map(lambda x: operator.contains(x, word)/float(data.numFluTweets), fluTweets)
         probWordGivenFlu = reduce(operator.add, isWordInFluTweets)
         
         # Get prob word given healthy
         isWordInHealthTweets = map(lambda x: operator.contains(x, word)/float(data.numHealthyTweets), healthTweets)
         probWordGivenHealth = reduce(operator.add, isWordInHealthTweets)
         
         # Updata data in data
         data.probabilities[word] = probWordGivenFlu, probWordGivenHealth
         
     
     print("Probabilities calculated!!!")  
Example #25
0
 def p_toggleDso(self, enable):
     jvmArgList = self.getGenericJvmArguments()
     self.p_info("Original value of genericJvmArguments: " + str(jvmArgList))
     if enable:
         for dsoArg in self.dsoArgs:
             if not operator.contains(jvmArgList, dsoArg): jvmArgList.append(dsoArg)
     else:
         jvmArgList = filter(self.p_filterOutDsoArgs, jvmArgList)
     self.p_setGenericJvmArguments(jvmArgList)
     self.p_info("New value of genericJvmArguments: " + str(self.getGenericJvmArguments()))
Example #26
0
 def _all(base, files, exp, negate=truth, expansion=""):
     for e in exp:
         abs_e = base + e + expansion
         # print abs_e, files
         if negate(contains(files, abs_e)):
             continue
         else:
             print "Failed on ", e
             return False
     return True
 def test_operator(self):
     import operator
     self.assertIs(operator.truth(0), False)
     self.assertIs(operator.truth(1), True)
     self.assertIs(operator.isNumberType(None), False)
     self.assertIs(operator.isNumberType(0), True)
     self.assertIs(operator.not_(1), False)
     self.assertIs(operator.not_(0), True)
     self.assertIs(operator.isSequenceType(0), False)
     self.assertIs(operator.isSequenceType([]), True)
     self.assertIs(operator.contains([], 1), False)
     self.assertIs(operator.contains([1], 1), True)
     self.assertIs(operator.isMappingType(1), False)
     self.assertIs(operator.isMappingType({}), True)
     self.assertIs(operator.lt(0, 0), False)
     self.assertIs(operator.lt(0, 1), True)
     self.assertIs(operator.is_(True, True), True)
     self.assertIs(operator.is_(True, False), False)
     self.assertIs(operator.is_not(True, True), False)
     self.assertIs(operator.is_not(True, False), True)
def run_trial(experiment, trial):
    phases = experiment.experiment_data.get('phases', ('outbound', 'return'))
    trial.data['height'] += trial.data['lowest_height_not_afforded']
    show_trial(trial.data)
    results = {'time': datetime.now()}
    for phase in phases:
        results[phase] = RESULTS_BY_CODE[get_input(
            trial_prompt(phase,  max(len(phase_) for phase_ in phases)),
            contains(set(RESULTS_BY_CODE.keys())),
            single_key=True
        )]
    return results
Example #29
0
    def test_updates_on_node_removal(self):
        # Ensure that nodes removed after the holder has been instantiated are
        # removed as attributes on the settings holder.
        self.client.create("/DEBUG", json.dumps(True))
        self.assertTrue(self.holder.DEBUG)  # sanity check

        event = self.get_child_node_event("/DEBUG", comparator=lambda *a: not operator.contains(*a))

        self.client.delete("/DEBUG")
        event.wait(self.TIMEOUT)
        with self.assertRaises(AttributeError):
            self.holder.DEBUG
Example #30
0
 def visit_Compare(self, node):
     if len(node.comparators) > 1:
         raise SyntaxError("mutliple comparison is not supported")
     left = self.visit(node.left)
     right = self.visit(node.comparators[0])
     op = node.ops[0]
     if op.__class__ == ast.In:
         return Import(operator.contains(right, left))
     elif op.__class__ == ast.NotIn:
         return Import(operator.not_)(Import(operator.contains)(right, left))
     else:
         return Import(self.COMPARE_MAP[op.__class__])(left, right)
Example #31
0
		return get_url(uri = "/app/{0}/view/report/{1}".format(quoted(slug(doctype)), quoted(name)))
	else:
		return get_url(uri = "/app/query-report/{0}".format(quoted(name)))

def get_url_to_report_with_filters(name, filters, report_type = None, doctype = None):
	if report_type == "Report Builder":
		return get_url(uri = "/app/{0}/view/report?{1}".format(quoted(doctype), filters))
	else:
		return get_url(uri = "/app/query-report/{0}?{1}".format(quoted(name), filters))

operator_map = {
	# startswith
	"^": lambda a, b: (a or "").startswith(b),

	# in or not in a list
	"in": lambda a, b: operator.contains(b, a),
	"not in": lambda a, b: not operator.contains(b, a),

	# comparison operators
	"=": lambda a, b: operator.eq(a, b),
	"!=": lambda a, b: operator.ne(a, b),
	">": lambda a, b: operator.gt(a, b),
	"<": lambda a, b: operator.lt(a, b),
	">=": lambda a, b: operator.ge(a, b),
	"<=": lambda a, b: operator.le(a, b),
	"not None": lambda a, b: a and True or False,
	"None": lambda a, b: (not a) and True or False
}

def evaluate_filters(doc, filters):
	'''Returns true if doc matches filters'''
Example #32
0
class DummyStorage(HealthcareStorage):
    "In-memory storage. This should only be used for testing."

    _patients = {}
    _patient_ids = {}
    _providers = {}

    _comparison_mapping = {
        comparisons.EQUAL:
        operator.eq,
        # operator.contains reverses the operands
        # http://docs.python.org/2/library/operator.html#operator.contains
        # field_value contains value
        comparisons.LIKE:
        operator.contains,
        # value contains field_value
        comparisons.IN:
        lambda a, b: operator.contains(b, a),
        comparisons.LT:
        operator.lt,
        comparisons.LTE:
        operator.le,
        comparisons.GT:
        operator.gt,
        comparisons.GTE:
        operator.ge,
    }

    def _lookup_to_filter(self, lookup):
        def filter_func(item):
            field, operator, value = lookup
            comparison_func = self._comparison_mapping[operator]
            field_value = item.get(field)
            if field_value is None:
                return False
            return comparison_func(field_value, value)

        return filter_func

    def _build_source_id(self, source_id, source_name):
        return '{0}-{1}'.format(source_id, source_name)

    def get_patient(self, id, source=None):
        "Retrieve a patient record by ID."
        patient = None
        if source:
            uid = self._build_source_id(id, source)
            patient_id = self._patient_ids.get(uid)
            if patient_id:
                patient = self._patients.get(patient_id)
        else:
            patient = self._patients.get(id)
        return patient

    def create_patient(self, data):
        "Create a patient record."
        uid = uuid.uuid4().int
        data['created_date'] = now()
        data['updated_date'] = now()
        if 'status' not in data:
            data['status'] = 'A'
        self._patients[uid] = data
        data['id'] = uid
        return data

    def update_patient(self, id, data):
        "Update a patient record by ID."
        if id in self._patients:
            data['updated_date'] = now()
            self._patients[id].update(data)
            return True
        return False

    def delete_patient(self, id):
        "Delete a patient record by ID."
        if id in self._patients:
            del self._patients[id]
            return True
        return False

    def filter_patients(self, *lookups):
        "Find patient records matching the given lookups."
        filters = map(self._lookup_to_filter, lookups)
        return filter(lambda t: all(f(t) for f in filters),
                      self._patients.values())

    def link_patient(self, id, source_id, source_name):
        "Associated a source/id pair with this patient."
        uid = self._build_source_id(source_id, source_name)
        if uid in self._patient_ids:
            return False
        if id not in self._patients:
            return False
        self._patient_ids[uid] = id
        return True

    def unlink_patient(self, id, source_id, source_name):
        "Remove association of a source/id pair with this patient."
        uid = self._build_source_id(source_id, source_name)
        if uid in self._patient_ids:
            del self._patient_ids[uid]
            return True
        return False

    def get_provider(self, id):
        "Retrieve a provider record by ID."
        return self._providers.get(id)

    def create_provider(self, data):
        "Create a provider record."
        uid = uuid.uuid4().int
        data['created_date'] = now()
        data['updated_date'] = now()
        if 'status' not in data:
            data['status'] = 'A'
        self._providers[uid] = data
        data['id'] = uid
        return data

    def update_provider(self, id, data):
        "Update a provider record by ID."
        if id in self._providers:
            data['updated_date'] = now()
            self._providers[id].update(data)
            return True
        return False

    def delete_provider(self, id):
        "Delete a provider record by ID."
        if id in self._providers:
            del self._providers[id]
            return True
        return False

    def filter_providers(self, *lookups):
        "Find provider records matching the given lookups."
        filters = map(self._lookup_to_filter, lookups)
        return filter(lambda t: all(f(t) for f in filters),
                      self._providers.values())
Example #33
0
class CompositeDecider(xapian.MatchDecider):
    # operators map
    op_map = {
        'exact': operator.eq,
        'iexact': i(operator.eq),
        'startswith': startswith,
        'istartswith': i(startswith),
        'endswith': endswith,
        'iendswith': i(endswith),
        'contains': operator.contains,
        'icontains': i(operator.contains),
        'regex': regex,
        'iregex': iregex,
        'in': lambda a, b: operator.contains(b, a),
        'gt': operator.gt,
        'gte': operator.ge,
        'lt': operator.lt,
        'lte': operator.le,
    }

    def __init__(self, model, tags, filter, exclude):
        xapian.MatchDecider.__init__(self)

        self._model = model
        self._tags = tags
        self._values_map = dict([(t.prefix, t.number) for t in tags])
        self._filter = filter
        self._exclude = exclude

    def __call__(self, document):
        if self._filter and not self._do_x(self._filter, document):
            return False

        if self._exclude and self._do_x(self._exclude, document):
            return False

        return True

    def get_tag(self, index):
        for tag in self._tags:
            if tag.number == index:
                return tag
        raise ValueError("No tag with number '%s'" % index)

    def _do_x(self, field, document):
        for child in field.children:
            if isinstance(child, X):
                result = self._do_x(child, document)
            else:
                result = self._do_field(child[0], child[1], document)

            if (result and field.connector == 'OR')\
                or (not result and field.connector == 'AND'):
                break

        if field.negated:
            return not result
        else:
            return result

    def _do_field(self, lookup, value, document):
        if '__' in lookup:
            field, op = lookup.split('__', 1)
        else:
            field, op = lookup, 'exact'

        if op not in self.op_map:
            raise ValueError("Unknown lookup operator '%s'" % op)

        op = self.op_map[op]

        doc_value = document.get_value(self._values_map[field])
        # mod
        #convert = curry(
        #self.get_tag(self._values_map[field]).convert,
        #model=self._model
        #)
        convert = self.get_tag(self._values_map[field]).convert

        if isinstance(value, (list, tuple)):
            value = map(convert, value)
        else:
            value = convert(value)

        operands = [
            doc_value,
            value,
        ]

        return reduce(op, operands)
Example #34
0
    METRIC_STRING_LIMIT=
    4096,  # Trim string value of metric to 4096 unicode characters

    # For trigger and metrics
    CONDITIONS_NUMBERIC={'gt', 'gte', 'lt', 'lte', 'eq', 'neq'},
    CONDITIONS_STRINGS={'contains', 'ncontains', 'exact'},
    CONDITIONS_BOOLEAN={'isTrue', 'isFalse'},
    CONDITIONS_CMP_FUNCTIONS={
        'gt': operator.gt,
        'gte': operator.ge,
        'lt': operator.lt,
        'lte': operator.le,
        'eq': operator.eq,
        'neq': operator.ne,
        'contains': operator.contains,
        'ncontains': lambda x, y: not operator.contains(x, y),
        'exact': operator.eq,
        'isTrue': lambda x, y: x is True,
        'isFalse': lambda x, y: x is False,
    },
    SHARD_CONFIG={
        "retentionPolicy": "60d",
        "shardDuration": "7d",
        "regex": "/.*/",
        "replicationFactor": 1,
        "split": 1
    },
    LOGGING_SHARD_CONFIG={
        "retentionPolicy": "7d",
        "shardDuration": "1d",
        "regex": "/.*/",
Example #35
0
from collections import OrderedDict
res = list(OrderedDict.fromkeys(test_list))
print ("The new list is : " + str(res))


print "Number of values greater than k in list"
k=4
res = filter(lambda x:x>k, test_list)
print ("the Number of items greater than K in the list :" + str(len(res)))

print "find substr in string"
test_str ="GeeksforGeeks"

res = test_str.find("eek")
res2 = test_str.index("eek")
import operator
res3 = operator.contains(test_str, "eek")
print res
print res2
print res3


print "intresection of 2 lists"
# initializing lists
test_list1 = [ [1, 2], [3, 4], [5, 6] ]
test_list2 = [ [3, 4], [5, 7], [1, 2] ]
# printing both lists
print ("The original list 1 : " + str(test_list1))
print ("The original list 2 : " + str(test_list2))

Example #36
0
 def __item_match_partial__(cls, x, y):
     ''' '''
     return contains(y, x)
Example #37
0
print("{:*^30}".format("math模块"))
import math
print(abs(-10))  #取绝对值
print(math.floor(4.65))  #返回数字的下舍整数
print(math.ceil(4.65))  #返回数字的上舍整数

print("{:*^30}".format("operator模块"))
import operator
print(operator.eq(10, 20))  #等于True,不等于False
print(operator.ne(10, 20))  #不等于
print(operator.lt(10, 20))  #小于
print(operator.le(10, 20))  #小于等于
print(operator.gt(10, 20))  #大于
print(operator.ge(10, 20))  #大于等于

print("contains:", operator.contains("abcde",
                                     "abcd"))  #contains(a,b),如果b in a 则True
print("concat:", operator.concat("ab", "cd"))  #字符串拼接
print("is:", operator.is_("ab", "ab"))  #识别字符串

list2 = {"a": "b", "k": "d"}
operator.setitem(list2, "k", "v")  #索引赋值
print("索引赋值obj[k]=v:{}".format(list2))

operator.delitem(list2, "k")  #删除赋值
print("删除赋值 obj[k]:{}".format(list2))

operator.getitem(list2, "a")  #删除赋值
print("索引 obj[a]:{}".format(operator.getitem(list2, "a")))
Example #38
0
operator.setitem(li, slice(0, 2), ["sachin", "kumar"])
print(" After modify the list :")
for i in range(0, len(li)):
    print(li[i], end=" ")

print("\r")

# use of getitems with slice
ass = operator.getitem(li, slice(0, 2))
print(" The use of getitems :\t", ass)

# use of delitems
operator.delitem(li, slice(0, 2))
print(" The use delitem :")
for i in range(0, len(li)):
    print(li[i], end=" ")

print("\r")

#\\\\\\\\\

s1 = "greeksfor"
s2 = "  greek"
print("the use of concatenate method :")
print(operator.concat(s1, s2))

if (operator.contains(s1, s2)):
    print("greekfor conatain greek.")
else:
    print("greekfor doesnot conatain greek ")
Example #39
0
import operator as op

print(op.add(4, 5))
print(op.mul(6, 7))
print(op.contains([1, 2, 3], 4))

x = [1, 2, 3]
f = op.itemgetter(-1)
print(f(x))
Example #40
0
 def in_words(word: str) -> bool:
     return contains(words, word)
Example #41
0
# http://www.lightningsafety.noaa.gov/odds.shtml
# Chance of being struck by lightning in your lifetime.
lightning_strike = Distribution({'Struck by lightning': 1/13500, 'Safe': REST})
# http://news.nationalgeographic.com/2016/02/160209-meteorite-death-india-probability-odds/
# Chance of being killed by meteorite in your lifetime.
meteorite = Distribution({'Killed by meteorite': 1/700000, 'Safe': REST})

# Common filters and maps.
import operator
lt = lambda s: operator.lt(*s)
le = lambda s: operator.le(*s)
eq = equal = equals = lambda s: operator.eq(*s)
ne = not_equal = not_equals = lambda s: operator.ne(*s)
gt = lambda s: operator.gt(*s)
ge = lambda s: operator.ge(*s)
contains = lambda s: operator.contains(*s)

add = sum
sub = lambda s: operator.sub(*s)
difference = lambda s: abs(s[0]-s[1])
from functools import reduce
mul = product = lambda s: reduce(operator.mul, s)

first = lambda s: s[0]
second = lambda s: s[1]
third = lambda s: s[2]
last = lambda s: s[-1]

class Solution(Distribution):
    """
    Uses the Distribution algorithms to model concentration of solutions.
Example #42
0
class OpCode:
    cmp_op = (lt, le, eq, ne, gt, ge, lambda a, b: contains(b, a),
              lambda a, b: not_(contains(b, a)), is_, is_not)  # 支持的比较运算符

    def __init__(self, frame):
        self.frame = frame
        self.stack = frame.stack
        self.co_code = frame.co_code
        self.co_consts = frame.co_consts
        self.co_cellvars = frame.co_cellvars
        self.co_freevars = frame.co_freevars
        self.co_consts = frame.co_consts
        self.co_name = frame.co_name
        self.co_names = frame.co_names
        self.co_nlocals = frame.co_nlocals
        self.co_argcount = frame.co_argcount
        self.global_ = frame.global_
        self.local = frame.local
        self.builtin = frame.builtin
        self.dict = frame.dict
        self.co_varnames = frame.co_varnames
        self.blocks = frame.blocks
        self.if_return = False
        self.opcode_dict = {
            '1': 'POP_TOP',
            '2': 'ROT_TWO',
            '3': 'ROT_THREE',
            '4': 'DUP_TOP',
            '5': 'DUP_TOP_TWO',
            '9': 'NOP',
            '10': 'UNARY_POSITIVE',
            '11': 'UNARY_NEGATIVE',
            '12': 'UNARY_NOT',
            '15': 'UNARY_INVERT',
            '16': 'BINARY_MATRIX_MULTIPLY',
            '17': 'INPLACE_MATRIX_MULTIPLY',
            '19': 'BINARY_POWER',
            '20': 'BINARY_MULTIPLY',
            '22': 'BINARY_MODULO',
            '23': 'BINARY_ADD',
            '24': 'BINARY_SUBTRACT',
            '25': 'BINARY_SUBSCR',
            '26': 'BINARY_FLOOR_DIVIDE',
            '27': 'BINARY_TRUE_DIVIDE',
            '28': 'INPLACE_FLOOR_DIVIDE',
            '29': 'INPLACE_TRUE_DIVIDE',
            '50': 'GET_AITER',
            '51': 'GET_ANEXT',
            '52': 'BEFORE_ASYNC_WITH',
            '55': 'INPLACE_ADD',
            '56': 'INPLACE_SUBTRACT',
            '57': 'INPLACE_MULTIPLY',
            '59': 'INPLACE_MODULO',
            '60': 'STORE_SUBSCR',
            '61': 'DELETE_SUBSCR',
            '62': 'BINARY_LSHIFT',
            '63': 'BINARY_RSHIFT',
            '64': 'BINARY_AND',
            '65': 'BINARY_XOR',
            '66': 'BINARY_OR',
            '67': 'INPLACE_POWER',
            '68': 'GET_ITER',
            '69': 'GET_YIELD_FROM_ITER',
            '70': 'PRINT_EXPR',
            '71': 'LOAD_BUILD_CLASS',
            '72': 'YIELD_FROM',
            '73': 'GET_AWAITABLE',
            '75': 'INPLACE_LSHIFT',
            '76': 'INPLACE_RSHIFT',
            '77': 'INPLACE_AND',
            '78': 'INPLACE_XOR',
            '79': 'INPLACE_OR',
            '80': 'BREAK_LOOP',
            '81': 'WITH_CLEANUP_START',
            '82': 'WITH_CLEANUP_FINISH',
            '83': 'RETURN_VALUE',
            '84': 'IMPORT_STAR',
            '85': 'SETUP_ANNOTATIONS',
            '86': 'YIELD_VALUE',
            '87': 'POP_BLOCK',
            '88': 'END_FINALLY',
            '89': 'POP_EXCEPT',
            '90': 'STORE_NAME',
            '91': 'DELETE_NAME',
            '92': 'UNPACK_SEQUENCE',
            '93': 'FOR_ITER',
            '94': 'UNPACK_EX',
            '95': 'STORE_ATTR',
            '96': 'DELETE_ATTR',
            '97': 'STORE_GLOBAL',
            '98': 'DELETE_GLOBAL',
            '100': 'LOAD_CONST',
            '101': 'LOAD_NAME',
            '102': 'BUILD_TUPLE',
            '103': 'BUILD_LIST',
            '104': 'BUILD_SET',
            '105': 'BUILD_MAP',
            '106': 'LOAD_ATTR',
            '107': 'COMPARE_OP',
            '108': 'IMPORT_NAME',
            '109': 'IMPORT_FROM',
            '110': 'JUMP_FORWARD',
            '111': 'JUMP_IF_FALSE_OR_POP',
            '112': 'JUMP_IF_TRUE_OR_POP',
            '113': 'JUMP_ABSOLUTE',
            '114': 'POP_JUMP_IF_FALSE',
            '115': 'POP_JUMP_IF_TRUE',
            '116': 'LOAD_GLOBAL',
            '119': 'CONTINUE_LOOP',
            '120': 'SETUP_LOOP',
            '121': 'SETUP_EXCEPT',
            '122': 'SETUP_FINALLY',
            '124': 'LOAD_FAST',
            '125': 'STORE_FAST',
            '126': 'DELETE_FAST',
            '127': 'STORE_ANNOTATION',
            '130': 'RAISE_VARARGS',
            '131': 'CALL_FUNCTION',
            '132': 'MAKE_FUNCTION',
            '133': 'BUILD_SLICE',
            '135': 'LOAD_CLOSURE',
            '136': 'LOAD_DEREF',
            '137': 'STORE_DEREF',
            '138': 'DELETE_DEREF',
            '141': 'CALL_FUNCTION_KW',
            '142': 'CALL_FUNCTION_EX',
            '143': 'SETUP_WITH',
            '144': 'EXTENDED_ARG',
            '145': 'LIST_APPEND',
            '146': 'SET_ADD',
            '147': 'MAP_ADD',
            '148': 'LOAD_CLASSDEREF',
            '149': 'BUILD_LIST_UNPACK',
            '150': 'BUILD_MAP_UNPACK',
            '151': 'BUILD_MAP_UNPACK_WITH_CALL',
            '152': 'BUILD_TUPLE_UNPACK',
            '153': 'BUILD_SET_UNPACK',
            '154': 'SETUP_ASYNC_WITH',
            '155': 'FORMAT_VALUE',
            '156': 'BUILD_CONST_KEY_MAP',
            '157': 'BUILD_STRING',
            '158': 'BUILD_TUPLE_UNPACK_WITH_CALL',
            '257': 'EXCEPT_HANDLER'
        }

    def opcode_1(self, oparg):
        """弹出栈顶元素."""
        # define POP_TOP                   1
        self.stack.pop()

    def opcode_2(self, oparg):
        """交换栈顶和第二个的位置."""
        # define ROT_TWO                   2
        top = self.stack.top()
        second = self.stack.second()
        self.stack.set_top(second)
        self.stack.set_second(top)

    def opcode_3(self, oparg):
        """栈顶三个元素之间交换."""
        # define ROT_THREE                 3
        top = self.stack.pop()
        second = self.stack.pop()
        third = self.stack.pop()
        self.stack.push(top)
        self.stack.push(third)
        self.stack.push(second)

    def opcode_4(self, oparg):
        """复制栈顶并压入栈."""
        # define DUP_TOP                   4
        top = self.stack.top()
        self.stack.push(top)

    def opcode_5(self, oparg):
        """复制栈顶两个元素并压入栈, 顺序不变."""
        # define DUP_TOP_TWO               5
        top = self.stack.top()
        second = self.stack.second()
        self.stack.push(second)
        self.stack.push(top)

    def opcode_9(self, oparg):
        # define NOP                       9
        code = "NOP"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_10(self, oparg):

        # define UNARY_POSITIVE           10
        code = "UNARY_POSITIVE"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_11(self, oparg):
        """针对 -a 运算的字节码."""
        # define UNARY_NEGATIVE           11
        value = self.stack.pop()
        res = -value
        self.stack.push(res)

    def opcode_12(self, oparg):
        # define UNARY_NOT                12
        code = "UNARY_NOT"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_15(self, oparg):
        # define UNARY_INVERT             15
        value = self.stack.pop()
        res = ~value
        self.stack.push(res)

    def opcode_16(self, oparg):
        # define BINARY_MATRIX_MULTIPLY   16
        code = "BINARY_MATRIX_MULTIPLY"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_17(self, oparg):
        # define INPLACE_MATRIX_MULTIPLY  17
        code = "INPLACE_MATRIX_MULTIPLY"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_19(self, oparg):
        """乘方运算."""
        # define BINARY_POWER             19
        right = self.stack.pop()
        left = self.stack.pop()
        res = left**right
        self.stack.push(res)

    def opcode_20(self, oparg):
        """乘法运算."""
        # define BINARY_MULTIPLY          20
        rigth = self.stack.pop()
        left = self.stack.pop()
        res = left * rigth
        self.stack.push(res)

    def opcode_22(self, oparg):
        """取模运算."""
        # define BINARY_MODULO            22
        divisor = self.stack.pop()
        dividend = self.stack.pop()
        res = dividend % divisor
        self.stack.push(res)

    def opcode_23(self, oparg):
        """加法运算."""
        # define BINARY_ADD               23
        right = self.stack.pop()
        left = self.stack.pop()
        res = left + right
        self.stack.push(res)

    def opcode_24(self, oparg):
        """减法运算."""
        # define BINARY_SUBTRACT          24
        right = self.stack.pop()
        left = self.stack.pop()
        res = left - right
        self.stack.push(res)

    def opcode_25(self, oparg):
        """如果 index 是数字的话就是按照索引取值,如果是 slice 就是取子序列."""
        # define BINARY_SUBSCR            25
        index = self.stack.pop()
        list_ = self.stack.pop()
        value = list_[index]
        self.stack.push(value)

    def opcode_26(self, oparg):
        """除法运算."""
        # define BINARY_FLOOR_DIVIDE      26
        right = self.stack.pop()
        left = self.stack.pop()
        res = left // right
        self.stack.push(res)

    def opcode_27(self, oparg):
        """真除法运算."""
        # define BINARY_TRUE_DIVIDE       27
        right = self.stack.pop()
        left = self.stack.pop()
        res = left / right
        self.stack.push(res)

    def opcode_28(self, oparg):
        """就地除法运算."""
        # define INPLACE_FLOOR_DIVIDE     28
        right = self.stack.pop()
        left = self.stack.pop()
        left //= right
        self.stack.push(left)

    def opcode_29(self, oparg):
        """就地真除法运算."""
        # define INPLACE_TRUE_DIVIDE      29
        right = self.stack.pop()
        left = self.stack.pop()
        left /= right
        self.stack.push(left)

    def opcode_50(self, oparg):
        # define GET_AITER                50
        code = "GET_AITER"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_51(self, oparg):
        # define GET_ANEXT                51
        code = "GET_ANEXT"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_52(self, oparg):

        # define BEFORE_ASYNC_WITH        52
        code = "BEFORE_ASYNC_WITH"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_55(self, oparg):
        """就地加法运算."""
        # define INPLACE_ADD              55
        right = self.stack.pop()
        left = self.stack.pop()
        left += right
        self.stack.push(left)

    def opcode_56(self, oparg):
        """就地减法运算."""
        # define INPLACE_SUBTRACT         56
        right = self.stack.pop()
        left = self.stack.pop()
        left -= right
        self.stack.push(left)

    def opcode_57(self, oparg):
        """就地乘法运算."""
        # define INPLACE_MULTIPLY         57
        right = self.stack.pop()
        left = self.stack.pop()
        left *= right
        self.stack.push(left)

    def opcode_59(self, oparg):
        """就地取模运算."""
        # define INPLACE_MODULO           59
        right = self.stack.pop()
        left = self.stack.pop()
        left %= right
        self.stack.push(left)

    def opcode_60(self, oparg):
        """将一个序列插入另外一个序列 a[1:2] = [1,2,3,4,5,6]."""
        # define STORE_SUBSCR             60
        s = self.stack.pop()
        x = self.stack.pop()
        other_list = self.stack.pop()
        x[s] = other_list

    def opcode_61(self, oparg):
        # define DELETE_SUBSCR            61
        code = "DELETE_SUBSCR"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_62(self, oparg):
        """左移运算"""
        # define BINARY_LSHIFT            62
        right = self.stack.pop()
        left = self.stack.pop()
        res = left << right
        self.stack.push(res)

    def opcode_63(self, oparg):
        """右移运算"""
        # define BINARY_RSHIFT            63
        rigth = self.stack.pop()
        left = self.stack.pop()
        res = left >> rigth
        self.stack.push(res)

    def opcode_64(self, oparg):
        """与运算"""
        # define BINARY_AND               64
        rigth = self.stack.pop()
        left = self.stack.pop()
        res = left & rigth
        self.stack.push(res)

    def opcode_65(self, oparg):
        """异或运算"""
        # define BINARY_XOR               65
        rigth = self.stack.pop()
        left = self.stack.pop()
        res = left ^ rigth
        self.stack.push(res)

    def opcode_66(self, oparg):
        """或运算"""
        # define BINARY_OR                66
        rigth = self.stack.pop()
        left = self.stack.pop()
        res = left | rigth
        self.stack.push(res)

    def opcode_67(self, oparg):
        """就地乘方运算."""
        # define INPLACE_POWER            67
        right = self.stack.pop()
        left = self.stack.pop()
        left **= right
        self.stack.push(left)

    def opcode_68(self, oparg):
        """获取可迭代对象的迭代器."""
        # define GET_ITER                 68
        a = self.stack.pop()
        it = iter(a)
        self.stack.push(it)

    def opcode_69(self, oparg):
        # define GET_YIELD_FROM_ITER      69
        code = "GET_YIELD_FROM_ITER"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_70(self, oparg):
        # define PRINT_EXPR               70
        code = "PRINT_EXPR"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_71(self, oparg):
        # define LOAD_BUILD_CLASS         71
        # code = "LOAD_BUILD_CLASS"
        # raise RuntimeError("使用到未实现的字节码:"+code)
        print(self.stack)

    def opcode_72(self, oparg):
        # define YIELD_FROM               72
        code = "YIELD_FROM"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_73(self, oparg):
        # define GET_AWAITABLE            73
        code = "GET_AWAITABLE"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_75(self, oparg):
        # define INPLACE_LSHIFT           75
        code = "INPLACE_LSHIFT"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_76(self, oparg):
        # define INPLACE_RSHIFT           76
        code = "INPLACE_RSHIFT"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_77(self, oparg):
        """按位与运算"""
        # define INPLACE_AND              77
        right = self.stack.pop()
        left = self.stack.pop()
        left &= right
        self.stack.push(left)

    def opcode_78(self, oparg):
        # define INPLACE_XOR              78
        code = "INPLACE_XOR"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_79(self, oparg):
        """按位或运算"""
        # define INPLACE_OR               79
        right = self.stack.pop()
        left = self.stack.pop()
        left |= right
        self.stack.push(left)

    def opcode_80(self, oparg):
        """循环结束."""
        # define BREAK_LOOP               80
        self.frame.index = self.forend
        self.stack.pop()

    def opcode_81(self, oparg):

        # define WITH_CLEANUP_START       81
        code = "WITH_CLEANUP_START"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_82(self, oparg):
        # define WITH_CLEANUP_FINISH      82
        code = "WITH_CLEANUP_FINISH"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_83(self, oparg):
        """返回值."""
        # define RETURN_VALUE             83
        value = self.stack.pop()
        index = frames.index(self.frame)
        if (index != 0):
            frame = frames[index - 1]
            frame.stack.push(value)
            self.if_return = True
        else:
            exit(0)

    def opcode_84(self, oparg):
        # define IMPORT_STAR              84
        code = "IMPORT_STAR"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_85(self, oparg):
        # define SETUP_ANNOTATIONS        85
        code = "SETUP_ANNOTATIONS"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_86(self, oparg):
        # define YIELD_VALUE              86
        code = "YIELD_VALUE"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_87(self, oparg):
        """这个我只针对了 for 进行的处理."""
        # define POP_BLOCK                87
        p = self.blocks.pop()
        index = len(self.stack)
        while index > p:
            self.stack.pop()
            index -= 1

    def opcode_88(self, oparg):
        # define END_FINALLY              88
        code = "END_FINALLY"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_89(self, oparg):
        # define POP_EXCEPT               89
        code = "POP_EXCEPT"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_90(self, oparg):
        """给相应对象设置名字."""
        # define STORE_NAME               90
        value = self.stack.pop()
        name = self.co_names[oparg]
        self.local[name] = value

    def opcode_91(self, oparg):
        # define DELETE_NAME              91
        code = "DELETE_NAME"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_92(self, oparg):
        """从堆栈中获取一个可迭代的对象然后对它进行拆包然后倒序压入栈"""
        # define UNPACK_SEQUENCE          92
        sequence = self.stack.pop()
        for item in sequence[::-1]:
            self.stack.push(item)

    def opcode_93(self, oparg):
        # define FOR_ITER                 93
        it = self.stack.top()
        try:
            value = next(it)
            self.stack.push(value)
        except StopIteration:
            self.stack.pop()
            self.frame.index += oparg

    def opcode_94(self, oparg):
        # define UNPACK_EX                94
        code = "UNPACK_EX"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_95(self, oparg):
        # define STORE_ATTR               95
        code = "STORE_ATTR"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_96(self, oparg):
        # define DELETE_ATTR              96
        code = "DELETE_ATTR"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_97(self, oparg):
        # define STORE_GLOBAL             97
        name = self.co_names[oparg]
        value = self.stack.pop()
        self.global_[name] = value

    def opcode_98(self, oparg):
        # define DELETE_GLOBAL            98
        code = "DELETE_GLOBAL"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_100(self, oparg):
        # define LOAD_CONST              100
        value = self.co_consts[oparg]
        self.stack.push(value)

    def opcode_101(self, oparg):
        # define LOAD_NAME               101
        name = self.co_names[oparg]
        value = None
        if name in self.local:
            value = self.local[name]
        if not value:
            if name in self.global_:
                value = self.global_[name]
        if not value:
            if name in self.builtin:
                value = self.builtin[name]
        self.stack.push(value)

    def opcode_102(self, oparg):
        # define BUILD_TUPLE             102
        list_ = []
        for _ in range(oparg):
            a = self.stack.pop()
            list_.append(a)
        res = tuple(list_[::-1])
        self.stack.push(res)

    def opcode_103(self, oparg):
        # define BUILD_LIST              103
        list_ = []
        for i in range(oparg):
            value = self.stack.pop()
            list_.append(value)
        self.stack.push(list_[::-1])

    def opcode_104(self, oparg):
        # define BUILD_SET               104
        code = "BUILD_SET"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_105(self, oparg):
        # define BUILD_MAP               105
        res = {}
        values = []
        keys = []
        for _ in range(oparg):
            values.append(self.stack.pop())
            keys.append(self.stack.pop())
        for key, value in zip(keys[::-1], values[::-1]):
            res[key] = value
        self.stack.push(res)

    def opcode_106(self, oparg):
        """屏蔽所以 LOAD_ATTR 中以 __ 开头的并且不是 __init__ 的所有属性"""
        # define LOAD_ATTR               106
        c = self.stack.pop()
        name = self.co_names[oparg]
        assert not (name.startswith("__") and name != "__init__")
        res = getattr(c, name)
        self.stack.push(res)

    def opcode_107(self, oparg):
        # define COMPARE_OP              107
        right = self.stack.pop()
        left = self.stack.pop()
        cmp_ = self.cmp_op[oparg]
        res = cmp_(left, right)
        self.stack.push(res)

    def opcode_108(self, oparg):
        # define IMPORT_NAME             108
        code = "IMPORT_NAME"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_109(self, oparg):
        # define IMPORT_FROM             109
        code = "IMPORT_FROM"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_110(self, oparg):
        # define JUMP_FORWARD            110
        self.frame.index += oparg

    def opcode_111(self, oparg):
        # define JUMP_IF_FALSE_OR_POP    111
        cond = self.stack.top()
        if cond is True:
            self.stack.pop()
        else:
            self.frame.index = oparg

    def opcode_112(self, oparg):
        # define JUMP_IF_TRUE_OR_POP     112
        cond = self.stack.top()
        if cond is True:
            self.frame.index = oparg
        else:
            self.stack.pop()

    def opcode_113(self, oparg):
        # define JUMP_ABSOLUTE           113
        self.frame.index = oparg

    def opcode_114(self, oparg):
        # define POP_JUMP_IF_FALSE       114
        a = self.stack.pop()
        if a:
            pass
        else:
            self.frame.index = oparg

    def opcode_115(self, oparg):
        # define POP_JUMP_IF_TRUE        115
        a = self.stack.pop()
        if a:
            self.frame.index = oparg

    def opcode_116(self, oparg):
        # define LOAD_GLOBAL             116
        name = self.co_names[oparg]
        if name in self.global_:
            value = self.global_[name]
        else:
            value = self.builtin[name]
        self.stack.push(value)

    def opcode_119(self, oparg):
        # define CONTINUE_LOOP           119
        code = "CONTINUE_LOOP"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_120(self, oparg):
        # define SETUP_LOOP              120
        p = len(self.stack)
        self.blocks.push(p)  # 将当前运行时栈长度压入 blocks 栈
        self.forend = self.frame.index + oparg

    def opcode_121(self, oparg):
        # define SETUP_EXCEPT            121
        code = "SETUP_EXCEPT"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_122(self, oparg):
        # define SETUP_FINALLY           122
        code = "SETUP_FINALLY"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_124(self, oparg):
        # define LOAD_FAST               124
        name = self.co_varnames[oparg]
        try:
            value = self.dict[self.co_varnames[oparg]]
        except KeyError as e:
            raise TypeError("missing " + name)
        self.stack.push(value)

    def opcode_125(self, oparg):
        # define STORE_FAST              125
        name = self.co_varnames[oparg]
        value = self.stack.pop()
        self.dict[name] = value

    def opcode_126(self, oparg):
        # define DELETE_FAST             126
        code = "DELETE_FAST"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_127(self, oparg):
        # define STORE_ANNOTATION        127
        code = "STORE_ANNOTATION"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_130(self, oparg):
        # define RAISE_VARARGS           130
        code = "RAISE_VARARGS"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_131(self, oparg):
        # define CALL_FUNCTION           131
        args = []

        for _ in range(oparg):
            args.append(self.stack.pop())

        func = self.stack.pop()
        if callable(func):
            res = func(*args[::-1])
            self.stack.push(res)
        elif (isinstance(func, Function)):
            frame = new_frame(func)
            if oparg > frame.co_argcount:
                raise TypeError(
                    f"{frame.co_name}() takes {frame.co_argcount} positional \
                        arguments but {oparg} were given")

            if oparg < frame.co_argcount:
                raise TypeError(
                    f"{frame.co_name}() missing {frame.co_argcount - oparg} \
                    required positional arguments: " +
                    ",".join(frame.co_varnames[oparg:]))

            for name, value in zip(func.varnames, args[::-1]):
                frame.dict[name] = value

            frames.append(frame)
            run(frame)
            frames.pop()
        else:
            raise RuntimeError("有些事情没有考虑到")

    def opcode_132(self, oparg):
        # define MAKE_FUNCTION           132
        name = self.stack.pop()
        mycode = self.stack.pop()
        f = Function(name, mycode)
        for i in f.freevars:
            f.dict[i] = self.dict[i]
        self.stack.push(f)

    def opcode_133(self, oparg):
        # define BUILD_SLICE             133
        args = []
        for _ in range(oparg):
            args.append(self.stack.pop())
        value = slice(*args[::-1])
        self.stack.push(value)

    def opcode_135(self, oparg):
        # define LOAD_CLOSURE            135
        key = self.co_cellvars[oparg]
        value = self.dict[key]
        self.stack.push(value)

    def opcode_136(self, oparg):
        # define LOAD_DEREF              136
        cell = self.co_freevars[oparg]
        value = self.dict[cell]
        self.stack.push(value)

    def opcode_137(self, oparg):
        # define STORE_DEREF             137
        value = self.stack.pop()
        key = self.co_cellvars[oparg]
        self.dict[key] = value

    def opcode_138(self, oparg):
        # define DELETE_DEREF            138
        code = "DELETE_DEREF"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_141(self, oparg):
        # define CALL_FUNCTION_KW        141
        code = "CALL_FUNCTION_KW"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_142(self, oparg):
        # define CALL_FUNCTION_EX        142
        code = "CALL_FUNCTION_EX"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_143(self, oparg):
        # define SETUP_WITH              143
        code = "SETUP_WITH"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_144(self, oparg):
        """计算下一条字节码的参数"""
        # define EXTENDED_ARG            144
        oldoparg = oparg
        oparg = self.frame.co_code[self.frame.index + 1]
        oparg |= oldoparg << 8
        self.frame.co_code[self.frame.index + 1] = oparg

    def opcode_145(self, oparg):
        # define LIST_APPEND             145
        v = self.stack.pop()
        list_ = self.stack.peek(oparg)
        list_.append(v)

    def opcode_146(self, oparg):
        # define SET_ADD                 146
        code = "SET_ADD"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_147(self, oparg):
        # define MAP_ADD                 147
        key = self.stack.pop()
        value = self.stack.pop()
        map_ = self.stack.peek(oparg)
        map_[key] = value

    def opcode_148(self, oparg):
        # define LOAD_CLASSDEREF         148
        code = "LOAD_CLASSDEREF"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_149(self, oparg):
        # define BUILD_LIST_UNPACK       149
        code = "BUILD_LIST_UNPACK"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_150(self, oparg):
        # define BUILD_MAP_UNPACK        150
        code = "BUILD_MAP_UNPACK"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_151(self, oparg):
        # define BUILD_MAP_UNPACK_WITH_CALL 151
        code = "BUILD_MAP_UNPACK_WITH_CALL"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_152(self, oparg):
        # define BUILD_TUPLE_UNPACK      152
        code = "BUILD_TUPLE_UNPACK"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_153(self, oparg):
        # define BUILD_SET_UNPACK        153
        code = "BUILD_SET_UNPACK"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_154(self, oparg):
        # define SETUP_ASYNC_WITH        154
        code = "SETUP_ASYNC_WITH"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_155(self, oparg):
        # define FORMAT_VALUE            155
        code = "FORMAT_VALUE"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_156(self, oparg):
        # define BUILD_CONST_KEY_MAP     156
        keys = self.stack.pop()
        values = []
        for _ in range(oparg):
            t = self.stack.pop()
            values.append(t)
        values = values[::-1]
        res = dict(zip(keys, values))
        self.stack.push(res)

    def opcode_157(self, oparg):
        # define BUILD_STRING            157
        code = "BUILD_STRING"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_158(self, oparg):
        # define BUILD_TUPLE_UNPACK_WITH_CALL 158
        code = "BUILD_TUPLE_UNPACK_WITH_CALL"
        raise RuntimeError("使用到未实现的字节码:" + code)

    def opcode_257(self, oparg):
        # define EXCEPT_HANDLER 25
        code = "EXCEPT_HANDLER"
        raise RuntimeError("使用到未实现的字节码:" + code)
Example #43
0
			from startup import get_url
			base_url = get_url()
		except ImportError:
			base_url = get_request_site_address()
	
	if not label: label = name
	
	return """<a href="%(base_url)s/app.html#!Form/%(doctype)s/%(name)s">%(label)s</a>""" % locals()
	
import operator
operator_map = {
	# startswith
	"^": lambda (a, b): (a or "").startswith(b),

	# in or not in a list
	"in": lambda (a, b): operator.contains(b, a),
	"not in": lambda (a, b): not operator.contains(b, a),

	# comparison operators
	"=": lambda (a, b): operator.eq(a, b),
	"!=": lambda (a, b): operator.ne(a, b),
	">": lambda (a, b): operator.gt(a, b),
	"<": lambda (a, b): operator.lt(a, b),
	">=": lambda (a, b): operator.ge(a, b),
	"<=": lambda (a, b): operator.le(a, b),
}

def compare(val1, condition, val2):
	if condition in operator_map:
		return operator_map[condition]((val1, val2))
Example #44
0
 def test_contains(self):
     self.assertRaises(TypeError, operator.contains)
     self.assertRaises(TypeError, operator.contains, None, None)
     self.assertTrue(operator.contains(range(4), 2))
     self.assertFalse(operator.contains(range(4), 5))
Example #45
0
def not_contains(container, element):
    """Antonym of :func:`operator.contains`"""
    return not operator.contains(container, element)
Example #46
0
 def test_contains(self):
     self.failUnless(operator.contains(range(4), 2))
     self.failIf(operator.contains(range(4), 5))
     self.failUnless(operator.sequenceIncludes(range(4), 2))
     self.failIf(operator.sequenceIncludes(range(4), 5))
Example #47
0
 def evaluate(expression):
     return contains(transite(store, expression))
    'is': operator.is_,
    'is_not': operator.is_not,
    '+': operator.add,
    '&': operator.and_,
    '/': operator.truediv,
    '//': operator.floordiv,
    '<<': operator.lshift,
    '%': operator.mod,
    '*': operator.mul,
    '|': operator.or_,
    '**': operator.pow,
    '>>': operator.rshift,
    '-': operator.sub,
    '^': operator.xor,
    'in': operator.contains,
    'not_in': lambda x, y: not operator.contains(x, y),
    'and': operator.and_,
    'or': operator.or_,
}

# shamelessly copied from Cython
compile_time_unary_operators = {
    'not': operator.not_,
    '~': operator.inv,
    '-': operator.neg,
    '+': operator.pos,
}

ast_to_binary_operator = {
    ast.Add: '+',
    ast.Sub: '-',
Example #49
0
COMPARATORS = {
    'count_eq': lambda x, y: safe_length(x) == y,
    'lt': operator.lt,
    'less_than': operator.lt,
    'le': operator.lt,
    'less_than_or_equal': operator.lt,
    'eq': operator.eq,
    'equals': operator.eq,
    'str_eq': lambda x, y: operator.eq(str(x), str(y)),
    'ne': operator.ne,
    'not_equals': operator.ne,
    'ge': operator.ge,
    'greater_than_or_equal': operator.ge,
    'gt': operator.gt,
    'greater_than': operator.gt,
    'contains': lambda x, y: x and operator.contains(x, y),  # is y in x
    'contained_by': lambda x, y: y and operator.contains(y, x),  # is x in y
    'regex': lambda x, y: regex_compare(str(x), str(y)),
    'type': lambda x, y: test_type(x, y)
}
COMPARATORS['length_eq'] = COMPARATORS['count_eq']

# Allow for testing basic types in comparators
TYPES = {
    'null': type(None),
    'none': type(None),
    'number': (int, long, float),
    'int': (int, long),
    'float': float,
    'boolean': bool,
    'string': basestring,
Example #50
0
def open_proto_file(main_file, package_name):
    new_proto_single_file = main_file.replace("raw_protos.proto",
                                              "POGOProtos.Rpc.proto")
    # Add licenses
    head = '/*\n'
    head += '* Copyright 2016-2020 --=FurtiF=-- Co., Ltd.\n'
    head += '*\n'
    head += '* Licensed under the\n'
    head += '*	Educational Community License, Version 2.0 (the "License"); you may\n'
    head += '*	not use this file except in compliance with the License. You may\n'
    head += '*	obtain a copy of the License at\n'
    head += '*\n'
    head += '*	http://www.osedu.org/licenses/ECL-2.0\n'
    head += '*\n'
    head += '*	Unless required by applicable law or agreed to in writing,\n'
    head += '*	software distributed under the License is distributed on an "AS IS"\n'
    head += '*	BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\n'
    head += '*	or implied. See the License for the specific language governing\n'
    head += '*	permissions and limitations under the License.\n'
    head += '*/\n\n'
    head += 'syntax = "proto3";\n'
    head += 'package %s;\n\n' % package_name

    if os.path.exists(new_proto_single_file):
        os.unlink(new_proto_single_file)

    open_for_new = open(new_proto_single_file, 'a')

    if not gen_only:
        # Add options by language
        if java_multiple_files and lang == "java":
            head += 'option java_multiple_files = true;\n\n'
        elif lang == "cpp":
            head += 'option optimize_for = CODE_SIZE;\n\n'

    open_for_new.writelines(head)

    messages = ''
    is_enum = False
    enum_name = ''
    is_one_off = False
    refs = []
    is_ignored = False
    fixed_messages = ''

    with open(main_file, 'r') as proto_file:
        for proto_line in proto_file.readlines():
            if is_ignored and operator.contains(proto_line, "}"):
                is_ignored = False
            if operator.contains(proto_line, "//ignored_"):
                messages += proto_line
                is_ignored = True
                continue
            if is_ignored:
                messages += proto_line
                continue
            if proto_line.startswith("syntax"):
                continue
            if proto_line.startswith("package"):
                continue
            # Ignore file licenses
            if proto_line.startswith("/*"):
                continue
            if proto_line.startswith("*"):
                continue
            if proto_line.startswith("*/"):
                continue
            # ---
            if is_blank(proto_line):
                continue

            if proto_line.startswith("enum"):
                is_enum = True
                enum_name = re.split(r'\s', proto_line)[1].upper()

            # refs
            if operator.contains(proto_line, "// ref:"):
                refs.append(proto_line.replace("// ref:", "").strip())
            if operator.contains(proto_line, "}") and refs:
                refs.pop()

            # OneOf stuff here
            if operator.contains(proto_line, "//oneof"):
                is_one_off = True
            # ---
            if operator.contains(proto_line, "//") and is_one_off:
                if gen_one_off:
                    if operator.contains(proto_line, "none = 0;"):
                        continue
                    proto_line = proto_line.replace("//", "")
                    if not operator.contains(proto_line,
                                             "{") and not operator.contains(
                                                 proto_line, "}"):
                        field = proto_line.split("=")[0].strip()
                        refName = "/".join(refs[-1].split("/")[:-1])
                        proto_line = "#" + refName + "#" + field + "#"
                else:
                    continue
            if is_one_off and operator.contains(proto_line, "}"):
                is_one_off = False

            if is_enum:
                if not proto_line.startswith("enum"):
                    if not proto_line.startswith("}"):
                        e = proto_line.replace(
                            re.split(r'(\W+)', proto_line)[2], enum_name +
                            "_" + re.split(r'(\W+)', proto_line)[2])
                        proto_line = e

            if not is_one_off and gen_one_off and operator.contains(
                    proto_line, "=") and refs:
                split = proto_line.strip().split(" ", 2)
                field = split[1].strip()
                target = "#" + refs[-1] + "#" + field + "#"
                if target in messages:
                    messages = messages.replace(target, "\t" + proto_line)
                    continue

            messages += proto_line

            if not proto_line.startswith("}") and operator.contains(
                    proto_line, "}"):
                messages += "\n"
                is_one_off = False
                is_enum = False

            if proto_line.startswith("}"):
                messages += "\n"
                is_enum = False
                enum_name = ''
                is_one_off = False

    if gen_one_off:
        messagesNew = ""
        lastLine = ""

        refs = []
        is_one_off = False
        skip = False
        setSkipFalse = False
        removeLast = False

        refsCount = {}
        for proto_line in messages.split("\n"):
            if operator.contains(proto_line, "// ref:"):
                refs.append(proto_line.replace("// ref:", "").strip())

            if operator.contains(proto_line, "}") and refs:
                refs.pop()

            if operator.contains(proto_line, "oneof") and refs:
                is_one_off = True
                count = len(messages.split("// ref: " + refs[-1] + "\n")) - 1
                if not refs[-1] in refsCount:
                    refsCount[refs[-1]] = 0
                refsCount[refs[-1]] += 1
                if not count == refsCount[refs[-1]]:
                    skip = True
                    removeLast = True

            if is_one_off and operator.contains(proto_line, "}"):
                is_one_off = False
                setSkipFalse = True

            if not skip and not (
                (operator.contains(proto_line, "// ref:")
                 or operator.contains(proto_line, "//----")) and gen_only):
                if not removeLast:
                    messagesNew += lastLine
                removeLast = False
                lastLine = proto_line + "\n"
            elif skip:
                removeLast = False

            if setSkipFalse:
                skip = False
                setSkipFalse = False

        messages = messagesNew

    message_for_fix = None

    for fix_line in messages.split("\n"):
        # ignore refs
        if fix_line.startswith("message"):
            match = re.split(r'\s', fix_line)
            message_for_fix = match[1]
        elif fix_line.startswith("enum"):
            match = re.split(r'\s', fix_line)
            message_for_fix = match[1]

        ## Check for all bytes refs
        # if operator.contains(fix_line, "\tbytes"):
        #     byte = fix_line.split("=")
        #     byte_fix = byte[0].replace("\t","")[:-1]
        #     print('elif message_for_fix == "' + message_for_fix + '" and operator.contains(fix_line, "' + byte_fix +'"):')
        #     print('\tfix_line = fix_line.replace("bytes", "Good_Proto_Here")')

        # Replace bytes for good proto here by condition
        if message_for_fix == "ClientGameMasterTemplateProto" and operator.contains(
                fix_line, "bytes data"):
            fix_line = fix_line.replace("bytes", "PBMGHIOCLGG")
        # elif message_for_fix == "BackgroundToken" and operator.contains(fix_line, "bytes token"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "BackgroundToken" and operator.contains(fix_line, "bytes iv"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "InventoryItemProto" and operator.contains(fix_line, "bytes deleted_item_key"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        elif message_for_fix == "InventoryItemProto" and operator.contains(
                fix_line, "bytes item"):
            fix_line = fix_line.replace("bytes", "MKBNBGHGGBA")
        # elif message_for_fix == "ProxyRequestProto" and operator.contains(fix_line, "bytes payload"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "ProxyResponseProto" and operator.contains(fix_line, "bytes payload"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "AEECDHCIKCM" and operator.contains(fix_line, "bytes eafbmckfnli"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "ANOMPFPBIPM" and operator.contains(fix_line, "bytes lfcpdhhaefn"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "ANOMPFPBIPM" and operator.contains(fix_line, "bytes lakniimfnlb"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "BBAJDEEEDEF" and operator.contains(fix_line, "bytes cpijocndncp"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "CEHDJEMNBBC" and operator.contains(fix_line, "bytes pjnecadedfi"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "FitnessReportProto" and operator.contains(fix_line, "bytes game_data"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "GADPHKHAIMJ" and operator.contains(fix_line, "bytes pjnecadedfi"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "GPHLJOLGGMF" and operator.contains(fix_line, "bytes jjfjedaecam"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "GPHLJOLGGMF" and operator.contains(fix_line, "bytes edilgaoikmd"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "HAAJDBDDIIJ" and operator.contains(fix_line, "bytes bdlohknpdhl"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "IFPNMHEPBGF" and operator.contains(fix_line, "bytes dcongihbmfb"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "JFJNABPPJLH" and operator.contains(fix_line, "bytes kaiamlgepej"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "JHDOEDMJKHI" and operator.contains(fix_line, "bytes moljgjghggp"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "JMOJFCBHMPO" and operator.contains(fix_line, "bytes bdlohknpdhl"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "LDBLNDNGEEC" and operator.contains(fix_line, "bytes gbddmbdejfj"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "LMJMNIKIMMF" and operator.contains(fix_line, "bytes kaiamlgepej"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "MKOOLBIJEBG" and operator.contains(fix_line, "bytes pjnecadedfi"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "NEJJFPEMFAP" and operator.contains(fix_line, "bytes kaiamlgepej"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "NNFGPFNNGNB" and operator.contains(fix_line, "bytes moljgjghggp"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "PHEGKNLDPFP" and operator.contains(fix_line, "bytes pjnecadedfi"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "PlatformClientApiSettingsProto" and operator.contains(fix_line, "bytes payload"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "RedeemPasscodeResponseProto" and operator.contains(fix_line, "bytes acquired_items_proto"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "AddLoginActionProto" and operator.contains(fix_line, "bytes inner_message"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        elif message_for_fix == "DownloadSettingsResponseProto" and operator.contains(
                fix_line, "bytes values"):
            fix_line = fix_line.replace("bytes", "PGBOCBAKEJG")
        # elif message_for_fix == "FriendDetailsProto" and operator.contains(fix_line, "bytes friend_visible_data"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "FriendDetailsProto" and operator.contains(fix_line, "bytes data_with_me"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "GetFriendsListOutProto" and operator.contains(fix_line, "bytes data_with_me"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "GetFriendsListOutProto" and operator.contains(fix_line, "bytes shared_data"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "GetFriendsListOutProto" and operator.contains(fix_line, "bytes data_from_me"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "GetFriendsListOutProto" and operator.contains(fix_line, "bytes data_to_me"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "PlayerSummaryProto" and operator.contains(fix_line, "bytes public_data"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "TemplateVariable" and operator.contains(fix_line, "bytes byte_value"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "ClientTelemetryRecordProto" and operator.contains(fix_line, "bytes encoded_message"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")
        # elif message_for_fix == "MapTileDataProto" and operator.contains(fix_line, "bytes tile_data"):
        #     fix_line = fix_line.replace("bytes", "Good_Proto_Here")

        fixed_messages += fix_line + "\n"

    messages = fixed_messages[:-1]

    open_for_new.writelines(messages[:-1])
    open_for_new.close()
    add_command_for_new_proto_file(new_proto_single_file)
Example #51
0
                                  " make something too long.")
    return a + b


########################################
# Defaults for the evaluator:

DEFAULT_OPERATORS = {ast.Add: safe_add, ast.Sub: op.sub, ast.Mult: safe_mult,
                     ast.Div: op.truediv, ast.FloorDiv: op.floordiv,
                     ast.Pow: safe_power, ast.Mod: op.mod,
                     ast.Eq: op.eq, ast.NotEq: op.ne,
                     ast.Gt: op.gt, ast.Lt: op.lt,
                     ast.GtE: op.ge, ast.LtE: op.le,
                     ast.Not: op.not_,
                     ast.USub: op.neg, ast.UAdd: op.pos,
                     ast.In: lambda x, y: op.contains(y, x),
                     ast.NotIn: lambda x, y: not op.contains(y, x),
                     ast.Is: lambda x, y: x is y,
                     ast.IsNot: lambda x, y: x is not y,
                     }

DEFAULT_FUNCTIONS = {"rand": random, "randint": random_int,
                     "int": int, "float": float,
                     "str": str if PYTHON3 else unicode}

DEFAULT_NAMES = {"True": True, "False": False, "None": None}

ATTR_INDEX_FALLBACK = True


########################################
Example #52
0
def RelationalExpression(e, ctx):

    expr = e.expr
    other = e.other
    op = e.op

    # because of the way the add-expr production handled operator precedence
    # we sometimes have nothing to do
    if other is None:
        return expr

    ops = dict([('>', lambda x, y: x.__gt__(y)),
                ('<', lambda x, y: x.__lt__(y)), ('=', lambda x, y: x.eq(y)),
                ('!=', lambda x, y: x.neq(y)),
                ('>=', lambda x, y: x.__ge__(y)),
                ('<=', lambda x, y: x.__le__(y)), ('IN', pyop.contains),
                ('NOT IN', lambda x, y: not pyop.contains(x, y))])

    if op in ('IN', 'NOT IN'):

        res = (op == 'NOT IN')

        error = False

        if other == RDF.nil:
            other = []

        for x in other:
            try:
                if x == expr:
                    return Literal(True ^ res)
            except SPARQLError as e:
                error = e
        if not error:
            return Literal(False ^ res)
        else:
            raise error

    if not op in ('=', '!=', 'IN', 'NOT IN'):
        if not isinstance(expr, Literal):
            raise SPARQLError(
                "Compare other than =, != of non-literals is an error: %r" %
                expr)
        if not isinstance(other, Literal):
            raise SPARQLError(
                "Compare other than =, != of non-literals is an error: %r" %
                other)
    else:
        if not isinstance(expr, Node):
            raise SPARQLError('I cannot compare this non-node: %r' % expr)
        if not isinstance(other, Node):
            raise SPARQLError('I cannot compare this non-node: %r' % other)

    if isinstance(expr, Literal) and isinstance(other, Literal):

        if expr.datatype is not None and expr.datatype not in XSD_DTs and other.datatype is not None and other.datatype not in XSD_DTs:
            # in SPARQL for non-XSD DT Literals we can only do =,!=
            if op not in ('=', '!='):
                raise SPARQLError(
                    'Can only do =,!= comparisons of non-XSD Literals')

    try:
        r = ops[op](expr, other)
        if r == NotImplemented:
            raise SPARQLError('Error when comparing')
    except TypeError as te:
        raise SPARQLError(*te.args)
    return Literal(r)
Example #53
0
 elif "chdir " in command:
     dirpath = command.replace("chdir ", "")
     dirpath = dirpath.replace("\n", "")
     ftph.cwd(dirpath)
     print "Directory changed to " + dirpath
     continue
 elif command == "up":
     dir = ftph.pwd()
     temp = dir
     index = len(dir) - 1
     for i in range(index, 0, -1):
         if temp[i] == "/" and i != len(dir):
             ftph.cwd(temp)
             print "One directory back."
             continue
         if (operator.contains(charset, dir[i])):
             temp = temp[:-1]
             if temp == "/":
                 ftph.cwd(temp)
                 print "One directory back."
 elif command == "rename":
     fromname = raw_input("Current file name: ")
     toname = raw_input("To be changed to: ")
     ftph.rename(fromname, toname)
     print "Successfully renamed."
     continue
 elif "delete " in command:
     delfile = command.replace("delete ", "")
     delfile = delfile.replace("\n", "")
     ftph.delete(delfile)
     print "File successfully deleted."
Example #54
0
 def in_usecase(x, y):
     return operator.contains(y, x)
Example #55
0
class Condition():
    '''
    Condition represents a single condition.
    It is modified based on the selector class from Orange package. (in particualr, Orange.classification.rule)
    '''
    OPERATORS = {
        # discrete, nominal variables
        # '==': operator.eq,
        # '=': operator.eq,
        # '!=': operator.ne,
        'in': lambda x, y: operator.contains(y, x),
        # note the reverse order of argument in the contains function

        # continuous variables
        '<=': operator.le,
        '>=': operator.ge,
        '<': operator.lt,
        '>': operator.gt,
    }

    def __init__(self,
                 column,
                 values=None,
                 min_value=None,
                 max_value=None,
                 type='categorical'):
        '''
        differs in the number of arguments as for categorical feature and continuous features
        '''
        self.type = type
        if type == 'categorical':
            # for categorical features
            self.column = column
            # value_1 is a set of possible values
            self.values = values
        elif type == 'continuous':
            # for continuous features
            self.column = column
            self.min = min_value
            self.max = max_value
        else:
            print(
                "critical error. Type of selector unspecified. Must be one of categorical or continuous "
            )

    def filter_instance(self, x):
        """
        Filter a single instance. Returns true or false
        """
        if self.type == 'categorical':
            return Condition.OPERATORS['in'](x[self.column], self.values)
        elif self.type == 'continuous':
            return Condition.OPERATORS['<='](
                x[self.column], self.max) and Condition.OPERATORS['>='](
                    x[self.column], self.min)

    def filter_data(self, X):
        """
        Filter several instances concurrently. Retunrs array of bools
        """

        if self.type == 'categorical':
            # return np.logical_or.reduce(
            # [np.equal(X[:,self.column], v) for v in self.values ]
            # )
            tmp = np.stack(
                [np.equal(X[:, self.column], v) for v in self.values])
            return np.any(tmp, axis=0)
        elif self.type == 'continuous':
            return np.logical_and(
                Condition.OPERATORS['<='](X[:, self.column], self.max),
                Condition.OPERATORS['>='](X[:, self.column], self.min))

    def __deepcopy__(self, memodict={}):
        # copy_object = Condition()
        # copy_object.value = self.value
        if self.type == 'categorical':
            column = self.column
            values = self.values[:]
            return Condition(column=column, values=values, type='categorical')
            # return Condition(column=deepcopy(self.column,memodict),values=deepcopy(self.values,memodict),type='categorical')
        elif self.type == 'continuous':
            return Condition(column=self.column,
                             min_value=self.min,
                             max_value=self.max,
                             type='continuous')
        else:
            print(
                "critical error. deep copy error! __deepcopy__ for Condition ")
            quit()

    def sample(self, batch_size=10):
        if self.type == 'categorical':
            synthetic_column = np.random.choice(self.values, size=batch_size)
            print("all possible values", self.values)
            print("generated values", synthetic_instances)
            print("check this categorical generation!")
            quit()
            return synthetic_instances
        elif self.type == 'continuous':
            synthetic_column = np.random.uniform(low=self.min,
                                                 high=self.max,
                                                 size=batch_size)
            return synthetic_column

    def __eq__(self, other):
        # return self.conditions == other.conditions
        if self.type != other.type:
            raise Exception(
                'two selector should have the same type. The value of s1 was: {0} and the type of s2 was: {1}'
                .format(self.type, other.type))
            return

        if self.type == 'categorical':
            return self.values == other.values
        elif self.type == 'continuous':
            return self.min == other.min and self.max == other.max
        else:
            raise Exception('critical error: unknown selector type {}'.format(
                self.type))
            return
Example #56
0
 def not_in_usecase(x, y):
     return not operator.contains(y, x)
Example #57
0
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
import operator
st = "http://www.burjeel.com/abu-dhabi/dr/"
page = requests.get("http://www.burjeel.com/abu-dhabi/doctors/")
dr1 = []
soup = BeautifulSoup(page.content, 'html.parser')
list1 = ((soup.find_all('h4')))
dr2 = [name1.get_text() for name1 in list1]
dr1 = [name for name in dr2 if name.startswith("Dr.")]
for jj in range(0, len(dr1)):
    dr1[jj] = dr1[jj].lower()
    dr1[jj] = dr1[jj].replace(" ", "-")
    dr1[jj] = dr1[jj].replace(".", "")
    dr1[jj] = dr1[jj][:-1]
    link = st + dr1[jj] + "/"
    page1 = requests.get(link)
    dr11 = []
    soup = BeautifulSoup(page1.content, 'html.parser')
    list11 = ((soup.find_all('h3')))
    dr11 = [name12.get_text() for name12 in list11]
    dr21 = [
        name11 for name11 in dr11 if operator.contains(name11, "Specialist")
        or operator.contains(name11, "Consultant") or operator.contains(
            name11, "General") or operator.contains(name11, "Chief")
    ]
    print(dr1[jj], end=" -> ")
    print(dr21)
Example #58
0
def in_(a, b):
    if isinstance(a, (list, tuple)):
        return any(operator.contains(b, x) for x in a)
    else:
        return operator.contains(b, a)
Example #59
0
print operator.and_(1, 1)

#按位或 即 a|b
print operator.or_(1, 8)
print operator.or_(1, 3)

#按位异或 即 a^b
print operator.xor(1, 8)
print operator.xor(1, 3)

#合并,不过只能用于序列
print operator.concat([1, 2], [3, 4])
print operator.concat(("one", ), ("two", ))

#是否包含,同样是序列
print operator.contains([1, 2, 3], 2)
print operator.contains([1, 2, 3], 0)

#包含位置,同样是序列
print operator.indexOf([1, 2, 3], 2)
#如果没有,则会抛出一个异常
# print operator.indexOf([1,2,3],0)

#包含 同 in
print operator.sequenceIncludes([1, 2, 3], 1)
print operator.sequenceIncludes("123", "1")

#计数,计算某个值在序列中出现的次数
print operator.countOf([1, 2, 1, 3, 1], 1)
#set序列可以去重
print operator.countOf(set([1, 2, 1, 3, 1]), 1)
Example #60
0
 def in_impl(a, b):
     return operator.contains(b, a)