Python string.count() Examples

The following are 30 code examples for showing how to use string.count(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module string , or try the search function .

Example 1
Project: featherduster   Author: nccgroup   File: helpers.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def count_words(candidate_text, common_words=frequency.common_words['english'], case_sensitive=True):
   '''
   Count the instances of common words in the expected plaintext
   language, return the total number of characters matched in each
   word 

   candidate_text - (string) Sample to analyze
   common_words - (list) Sequences expected to appear in the text
   case_sensitive - (bool) Whether or not to match case sensitively
   '''
   score = 0

   for word in common_words:
      if not case_sensitive:
         word = word.lower()
      num_found = candidate_text.count(word)
      if num_found > 0:
         score += num_found * len(word)
      
   return score 
Example 2
Project: MARA_Framework   Author: xtiankisutsa   File: androapi_format.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def translateDescParams(desc_params):
    desc_params = desc_params.replace(" ", "")
    buff = ""

    for elem in desc_params.split(","):
        if elem != "":
            tab = ""
            if "[" in elem:
                tab = "[" * string.count(elem, "[")

                elem = elem[:tab.find("[") - 2]

            if elem not in BASIC_TYPES:
                if elem in ADVANCED_TYPES:
                    buff += tab + ADVANCED_TYPES[elem] + " "
                else:
                    buff += tab + "L" + elem.replace(".", "/") + "; "
            else:
                buff += tab + BASIC_TYPES[elem] + " "

    buff = buff[:-1]
    return buff 
Example 3
Project: MARA_Framework   Author: xtiankisutsa   File: androapi_format.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def translateDescReturn(desc_return):
    buff = ""
    for elem in desc_return.split(" "):
        tab = ""
        if "[" in elem:
            tab = "[" * string.count(elem, "[")
            elem = elem[:tab.find("[") - 2]

        if elem in BASIC_TYPES:
            buff += tab + BASIC_TYPES[elem] + " "
        else:
            if elem in ADVANCED_TYPES:
                buff += tab + ADVANCED_TYPES[elem] + " "
            else:
                if "." in elem:
                    buff += tab + "L" + elem.replace(".", "/") + "; "

    buff = buff[:-1]
    return buff 
Example 4
Project: stdm   Author: gltn   File: xpreformatted.py    License: GNU General Public License v2.0 6 votes vote down vote up
def _getFragWord(frags,maxWidth):
    ''' given a fragment list return a list of lists
        [size, spaces, (f00,w00), ..., (f0n,w0n)]
        each pair f,w represents a style and some string
    '''
    W = []
    n = 0
    s = 0
    for f in frags:
        text = f.text[:]
        W.append((f,text))
        cb = getattr(f,'cbDefn',None)
        if cb:
            _w = getattr(cb,'width',0)
            if hasattr(_w,'normalizedValue'):
                _w._normalizer = maxWidth
        n = n + stringWidth(text, f.fontName, f.fontSize)

        #s = s + _countSpaces(text)
        s = s + string.count(text, ' ') # much faster for many blanks

        #del f.text # we can't do this until we sort out splitting
                    # of paragraphs
    return n, s, W 
Example 5
Project: ircsnapshot   Author: bwall   File: to.gexf.py    License: MIT License 6 votes vote down vote up
def ParseCSVLine(line):
    line = line.strip()
    values = []
    while line.__len__() > 0:
        if string.count(line, ',') > 0:
            if line[0] == '"':
                line = line[1:]
                values.append(line[:string.find(line, '"')])
                line = line[string.find(line, '"') + 1:]
                line = line[1:]
                if line.__len__() == 0:
                    values.append("")
            else:
                values.append(line[:string.find(line, ',')])
                line = line[string.find(line, ',') + 1:]
                if line.__len__() == 0:
                    values.append("")
        else:
            values.append(line)
            line = ""
    return values 
Example 6
Project: pDNS2   Author: bez0r   File: pdns2_query_api.py    License: GNU General Public License v3.0 6 votes vote down vote up
def domain_counter(fqdn):
    ''' counter for domains based on the last two zones '''
    fulldata = []
    for each in fqdn:
        a = count((each),".")
        if a >= 2:
            fulldata.append(each[::-1])
    fulldata.sort()
    domain_trunk = []
    for each in fulldata:
        splitdata = each.split('.')
        # treat the first two as one
        TwoZones = str(splitdata[0])+"."+str(splitdata[1])
        #print TwoZones
        domain_trunk.append(TwoZones)        
    fulldata = []
    for each in domain_trunk:
        fulldata.append(each[::-1])
    return (Counter(fulldata)) 
Example 7
Project: pDNS2   Author: bez0r   File: pdns2_query.py    License: GNU General Public License v3.0 6 votes vote down vote up
def ip_print(sublist):
    query      = r.hget('IP:'+str(sublist), 'name')
    first      = r.hget('IP:'+str(sublist), 'first')
    date       = r.hget('IP:'+str(sublist), 'date')
    count      = r.hget('IP:'+str(sublist), 'count')
    rr_type    = r.hget('IP:'+str(sublist), 'type')
    ttl        = r.hget('IP:'+str(sublist), 'ttl')
    count      = r.hget('IP:'+str(sublist), 'count')
    #dns_client = r.hget(sublist, 'dns_client')
    #dns_server = r.hget(sublist, 'dns_server')
    #nss        = r.hget(sublist, 'nss')

    if first == None:
        first = '00000000'
    if date == None:
        date = '00000000'
    
    # PRINT FIELDS
    print "{0:18} {1:35} {2:9} {3:9} {4:8} {5:8} {6:6}".format(sublist,query,first[:8],date[:8],record_translate(rr_type),ttl,count)
    pass 
Example 8
Project: pDNS2   Author: bez0r   File: pdns2_query.py    License: GNU General Public License v3.0 6 votes vote down vote up
def domain_counter(fqdn):
    ''' counter for domains based on the last two zones '''
    fulldata = []
    for each in fqdn:
        a = count((each),".")
        if a >= 2:
            fulldata.append(each[::-1])
    fulldata.sort()
    domain_trunk = []
    for each in fulldata:
        splitdata = each.split('.')
        # treat the first two as one
        TwoZones = str(splitdata[0])+"."+str(splitdata[1])
        #print TwoZones
        domain_trunk.append(TwoZones)        
    fulldata = []
    for each in domain_trunk:
        fulldata.append(each[::-1])
    return (Counter(fulldata)) 
Example 9
Project: ITWSV   Author: penetrate2hack   File: spider.py    License: MIT License 5 votes vote down vote up
def unique(L):
	noDupli=[]
	[noDupli.append(i) for i in L if not noDupli.count(i)]
	return noDupli 
Example 10
Project: ITWSV   Author: penetrate2hack   File: spider.py    License: MIT License 5 votes vote down vote up
def parseJavaScriptContent(jsContent):
	global database_url, database_ext, dumb_params
	"""
		Parse the content of a JavaScript file
	"""
	for l in jsContent.readlines():
		for e in allowed:
			if l.count('.'+e) > 0:
				# we found an external a call
				if l.count('http://') > 0 and l.count(root) < 1:
					# External link
					et= '.'+e
					b1 = l.find('http://')
					b2 = l.find(et) + len(et)
					database_ext.append(l[b1:b2])
				else:
					# Internal link
					et= '.'+e
					b2 = l.find(et) + len(et)
					b1 = rfindFirstJSChars(l[:b2])+1
					database_url.append(giveGoodURL(l[b1:b2],root))
		# try to get a parameter
		k = l.find('?')
		if k > 0:
			results = l[k:].split('?')
			plop = []
			for a in results:
				plop.append(cleanListDumbParams(regDumbParam.split(a)))
			dumb_params.append(flatten(plop))
		k = l.find('&')
		if k > 0:
			results = l[k:].split('&')
			plop = []
			for a in results:
				plop.append(cleanListDumbParams(regDumbParam.split(a)))
			plop = flatten(plop)
			dumb_params.append(flatten(plop))
	dumb_params = unique(flatten(dumb_params)) 
Example 11
Project: featherduster   Author: nccgroup   File: helpers.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def generate_frequency_table(text,charset):
   '''
   Generate a character frequency table for a given text
   and charset as dict with character or string as key and
   frequency of appearance as value expressed as a decimal
   percentage

   text - A sample of plaintext to analyze for frequency data
   charset - (list of strings) The set of items to count in the plaintext
      such as ['a','b','c', ... 'z','aa','ab','ac', ... 'zz']
   '''
   freq_table = {}
   text_len = 0 
   for char in charset:
      freq_table[char] = 0 
   for char in text:
      if char in charset:
         freq_table[char] += 1
         text_len += 1
   for multigraph in filter(lambda x: len(x)>1,charset):
      freq_table[multigraph] = string.count(text, multigraph)
   # Normalize frequencies with length of text
   for key in freq_table.keys():
      if text_len != 0:
         freq_table[key] /= float(text_len)
      else:
         freq_table[key] = 0 
   return freq_table 
Example 12
Project: spitfire   Author: youtube   File: yappsrt.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def format_error(input, err, scanner):
  """This is a really dumb long function to print error messages nicely."""
  error_message = StringIO.StringIO()
  p = err.pos
  print >> error_message, "error position", p
  # Figure out the line number
  line = count(input[:p], '\n')
  print >> error_message, err.msg, "on line", repr(line+1) + ":"
  # Now try printing part of the line
  text = input[max(p-80, 0):p+80]
  p = p - max(p-80, 0)

  # Strip to the left
  i = rfind(text[:p], '\n')
  j = rfind(text[:p], '\r')
  if i < 0 or (0 <= j < i): i = j
  if 0 <= i < p:
    p = p - i - 1
  text = text[i+1:]

  # Strip to the right
  i = find(text,'\n', p)
  j = find(text,'\r', p)
  if i < 0 or (0 <= j < i):
    i = j
  if i >= 0:
    text = text[:i]

  # Now shorten the text
  while len(text) > 70 and p > 60:
    # Cut off 10 chars
    text = "..." + text[10:]
    p = p - 7

  # Now print the string, along with an indicator
  print >> error_message, '> ', text.replace('\t', ' ').encode(sys.getdefaultencoding())
  print >> error_message, '> ', ' '*p + '^'
  print >> error_message, 'List of nearby tokens:', scanner
  return error_message.getvalue() 
Example 13
Project: spitfire   Author: youtube   File: yappsrt.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def wrap_error_reporter(parser, rule):
  try:
    return getattr(parser, rule)()
  except SyntaxError, e:
    logging.exception('syntax error')
    input = parser._scanner.input
    try:
      error_msg = format_error(input, e, parser._scanner)
    except ImportError:
      error_msg = 'Syntax Error %s on line\n' % (e.msg, 1 + count(input[:e.pos])) 
Example 14
Project: stdm   Author: gltn   File: xpreformatted.py    License: GNU General Public License v2.0 5 votes vote down vote up
def _countSpaces(text):
    return string.count(text, ' ')
##  i = 0
##  s = 0
##  while 1:
##      j = string.find(text,' ',i)
##      if j<0: return s
##      s = s + 1
##      i = j + 1 
Example 15
Project: ircsnapshot   Author: bwall   File: to.gexf.py    License: MIT License 5 votes vote down vote up
def LoadBlocks(path):
    global blocks
    with open(path) as f:
        count = 0
        for line in f:
            count += 1
            if count < 3:
                continue
            blocks.append(ParseCSVLine(line))
    blocks = sorted(blocks, key=lambda it: int(it[0])) 
Example 16
Project: ircsnapshot   Author: bwall   File: to.gexf.py    License: MIT License 5 votes vote down vote up
def LoadLocations(path):
    global locations
    with open(path) as f:
        count = 0
        for line in f:
            count += 1
            if count < 3:
                continue
            (locId, country, region, city, postalCode, latitude, longitude,
            metroCode, areaCode) = ParseCSVLine(line)
            locations[int(locId)] = {"country": country, "region": region,
                "city": city, "postalCode": postalCode, "latitude": latitude,
                "longitude": longitude, "metroCode": metroCode,
                "areaCode": areaCode} 
Example 17
Project: DCC   Author: dieterich-lab   File: genecount.py    License: GNU General Public License v3.0 5 votes vote down vote up
def countmapped(self, string):
        # This function takes the 5th column (a string with mapping information),
        # and return the count of '.' and ','. Which are mapped read count

        rs = string.count(',') + string.count('.')

        return str(rs) 
Example 18
Project: DCC   Author: dieterich-lab   File: genecount.py    License: GNU General Public License v3.0 5 votes vote down vote up
def countspliced(self, string):
        # return the count of '>' and '<'
        rs = string.count('>') + string.count('<')
        return str(rs) 
Example 19
Project: DCC   Author: dieterich-lab   File: genecount.py    License: GNU General Public License v3.0 5 votes vote down vote up
def getreadscount(self, mpileup, countmapped=False):
        # Input a mpileup result, which is a list
        # The count information is in the 5th column
        # count the number of mapped reads represented by ',' and '.' or, spliced reads
        # represented by the number of '>' and '<' in the string

        count = []

        # print "mpileup is a " + str(type(mpileup))

        if type(mpileup) is str:

            mpileupline = mpileup.split('\n')

            for itm in mpileupline:
                tmp = itm.split('\t')

                if countmapped:
                    if len(tmp) == 6:
                        count.append(tmp[0] + '\t' + tmp[1] + '\t' + self.countmapped(tmp[4]))
                else:
                    if len(tmp) == 6:
                        count.append(tmp[0] + '\t' + tmp[1] + '\t' + self.countspliced(tmp[4]))

        elif type(mpileup) is list:

            for itm in mpileup:

                if countmapped:
                    if len(mpileup) == 6:
                        count.append(itm[0] + '\t' + itm[1] + '\t' + self.countmapped(itm[4]))
                else:
                    if len(mpileup) == 6:
                        count.append(itm[0] + '\t' + itm[1] + '\t' + self.countspliced(itm[4]))

        return count 
Example 20
Project: pDNS2   Author: bez0r   File: pdns2_query_api.py    License: GNU General Public License v3.0 5 votes vote down vote up
def header_dom():
    ''' header and linked to domain searched output'''
    print "{0:40} {1:15} {2:9} {3:9} {4:5} {5:5} {6:8} {7:10} {8:8} {9:8} {10:9} {11:12}".format ("Domain","ips","first","date","rr","ttl","count","threat","date","tag","source","dom")
    pass 
Example 21
Project: pDNS2   Author: bez0r   File: pdns2_query_api.py    License: GNU General Public License v3.0 5 votes vote down vote up
def dom_print(sublist):
    ''' print the queried list in a 120 character line'''
    # sublist as passed contains the 'Domain:' with the search criteria
    ips        = r.hget('Domain:'+str(sublist), 'ip')
    first      = r.hget('Domain:'+str(sublist), 'first')
    date       = r.hget('Domain:'+str(sublist), 'date')
    rr_type    = r.hget('Domain:'+str(sublist), 'type')
    ttl        = r.hget('Domain:'+str(sublist), 'ttl')
    count      = r.hget('Domain:'+str(sublist), 'count')
    # access the TDOM keys
    threat    = r.hget('TDOM:'+str(sublist), 'threat')
    evaldate  = r.hget('TDOM:'+str(sublist), 'evaldate')
    tag       = r.hget('TDOM:'+str(sublist), 'tag')
    source    = r.hget('TDOM:'+str(sublist), 'source')   
    # check the subsequent key TIP if tagged as threat
    ips_hit   = r.hget('TIP:'+str(ips), 'threat')
    # fix strings for dates
    if first == None:
        first = '00000000'
    if date == None:
        date = '00000000'

    #FINAL PRINT
    print "{0:40} {1:15} {2:9} {3:9} {4:5} {5:5} {6:9} {7:10} {8:8} {9:8} {10:8} {11:12}".format(sublist,ips,first[:8],date[:8],record_translate(rr_type),ttl,count,threat, evaldate,tag,source,ips_hit)
    # denoting DNS client and server is sometime useful, depeends on the organization
    
    #dns_client = r.hget('Domain:'+str(sublist), 'dns_client')
    #dns_server = r.hget('Domain:'+str(sublist), 'dns_server')
    #print dns_client,",",sublist,",",dns_server,",",dns_server
    pass	


# ===================================== 
Example 22
Project: pDNS2   Author: bez0r   File: pdns2_query_api.py    License: GNU General Public License v3.0 5 votes vote down vote up
def header_ip():
    # header for IP based print information
    print  "{0:18} {1:35} {2:9} {3:9} {4:8} {5:8} {6:6} {7:12} {8:8} {9:12} {10:10} {11:12}".format ("IP","query","first","date","rr","ttl","count","threat","date","tag","source","dom")
    pass 
Example 23
Project: pDNS2   Author: bez0r   File: pdns2_query_api.py    License: GNU General Public License v3.0 5 votes vote down vote up
def ip_print(sublist):
    # split and remove the header before you get here along with sort
    # BASIC DNS / IP PROPERTIES
    query      = r.hget('IP:'+str(sublist), 'name')
    first      = r.hget('IP:'+str(sublist), 'first')
    date       = r.hget('IP:'+str(sublist), 'date')
    count      = r.hget('IP:'+str(sublist), 'count')
    rr_type    = r.hget('IP:'+str(sublist), 'type')
    ttl        = r.hget('IP:'+str(sublist), 'ttl')
    count      = r.hget('IP:'+str(sublist), 'count')
    threat      = r.hget('TIP:'+str(sublist), 'threat')
    evaldate   = r.hget('TIP:'+str(sublist), 'evaldate')
    tag       = r.hget('TIP:'+str(sublist), 'tag')
    source    = r.hget('TIP:'+str(sublist), 'source')
    dom_hit   = r.hget('TDOM:'+str(query), 'threat')

    ## below fields have value in some networks, you decide if you want to dispay it
    #dns_client = r.hget(sublist, 'dns_client')
    #dns_server = r.hget(sublist, 'dns_server')
    #nss        = r.hget(sublist, 'nss')

    
    # fix strings for dates
    if first == None:
        first = '00000000'
    if date == None:
        date = '00000000'
    
    
    # PRINT FIELDS
    print "{0:18} {1:35} {2:9} {3:9} {4:8} {5:8} {6:6} {7:12} {8:8} {9:12} {10:10} {11:12}".format(sublist,query,first[:8],date[:8],record_translate(rr_type),ttl,count,threat,evaldate,tag,source,dom_hit)
    pass 
Example 24
Project: pDNS2   Author: bez0r   File: pdns2_query_api.py    License: GNU General Public License v3.0 5 votes vote down vote up
def ip_flux(self, arg1):
        ''' Search IP space for a count of domains '''
        all_ips = r.keys('IP:'+str(arg1))
        allcount = []
        for each in all_ips:
            acount = r.hget(each, 'name') # working list
            #print acount
            if acount == None:
                pass
            else:
                allcount.append(acount)
        x = Counter(allcount).most_common(50)
        for k in x:
            print u'{0}'.format(k) 
Example 25
Project: pDNS2   Author: bez0r   File: pdns2_query_api.py    License: GNU General Public License v3.0 5 votes vote down vote up
def dom_unanswered(self, arg1,arg2):
        ''' find unaswered name request for searched domain space
        MX record are excluded
        format 'domain.com', min count'''
        all_dom = r.keys('Domain:'+str(arg1))
        print header_dom()
        for each in all_dom:
            work_ip = r.hget(each, 'ip')
            if work_ip == '':
                record = r.hget(each, 'type')
                count = r.hget(each, 'count')
                if record != '0x000f' and record != '0x0006' and int(count) >= arg2:
                    dom_print(each[7:]) 
Example 26
Project: pDNS2   Author: bez0r   File: pdns2_query_api.py    License: GNU General Public License v3.0 5 votes vote down vote up
def count(self, domain):
        ''' count the subdomains within a ROOT domains ('domain.com') '''
        # consider an exclusion list
        # locate mass number of sub domains
        # REVERSE OF IP will resolve only a single domain, not all domains that have the same pairing
        # remember a change over time is more valuable then instant stats
        alldom = r.keys('Domain:'+str(domain))
        # call domain_counter function
        x = domain_counter(alldom).most_common(500)
        for k, v in x:
            print u'{0}: {1}'.format(k, v) 
Example 27
Project: pDNS2   Author: bez0r   File: pdns2_query_api.py    License: GNU General Public License v3.0 5 votes vote down vote up
def help(self):
        print 'pDNS2 commands\n'
        print 'DOMAIN EXAMPLES'
        print 'domain(\'*xstamper.com\') seeks all domains that end with xstamper.com'
        print 'ttl(\'0\')                use a number like 0 or 100 to get all the TTL of a specific value search is based on domain not IP'
        print 'tdom(\'*\')              return by query, tagged domain threats '
        print 'count(\'*\')             return by query,count sub domains for a given domain example: .cn .com .google.com '
        print 'acount(\'*\')            return by query, counts of counts (usage), or \'hits\' for the domains in order, *.google.com or *.com are examples '
        print ''
        print 'IP EXAMPLES'
        print 'ip(\'222.*\')            returns 222.* or anything '
        print 'tip(\'192.168.*\')       return by query, tagged IP addresses'
        print 'local()                  search entire database local resolved IP addresses that resolve to 127.0.0.1 etc. '
        print 'ip_flux(\'*.com\')       return a COUNT of domains in the IP space for each instance of a domain, use with ip_reverse'
        print 'x.ip_reverse(\'*\',\'seattletimes.com\') use with ip_flux, enumerate domains in the IP space'
        print ''
        print 'TAGGING'
        print 'tag_domain(\'ms.justice.cz\')    tag a domain'
        print 'tag_ip(\'194.213.41.92\')        tag an IP address '
        print ''        
        print 'THREAT DETECTION'
        print 'ip_sniff(\'192.168.1.1\')    search the domain space for a specific IP address, different then searching by IP '
        print 'date(\'20130101\')           return all records by date'
        print 'knn_dom(\'*.google.com\',20)  DO NOT USE IN DEV, returns euclidian distance threats by IP address '

        print ''
        print 'ADMINISTRATIVE'
        print 'delete_key(\'Domain:*delete*\') Dangerous command, deletes a key, must use the entire key such as Domain: or IP:'
        print 'raw_record(\'Domain:xalrbngb-0.t.nessus.org\') view the raw record properties (no wildcards) use full key name'
        print 'dom_unanswered(\'*.com\',1000) return unanswered queries by minimual count such as everything in .com with 1000 or more'
        print 'threat(5)                    search threats by provided date (DO NOT USE, work in progress)\n'        
        print 'pDNS2 tracks current state and last known, it is a snapshot of organization perception, not a log.'
        
        #x.rrecord('*') 
Example 28
Project: pDNS2   Author: bez0r   File: pdns2_query.py    License: GNU General Public License v3.0 5 votes vote down vote up
def header_dom():
    ''' header and linked to domain searched output'''
    print "{0:40} {1:15} {2:9} {3:9} {4:5} {5:5} {6:9}".format ("Domain","ips","first","date","rr","ttl","count")
    pass 
Example 29
Project: pDNS2   Author: bez0r   File: pdns2_query.py    License: GNU General Public License v3.0 5 votes vote down vote up
def header_ip():
    # header for IP based print information
    print  "{0:18} {1:35} {2:9} {3:9} {4:8} {5:8} {6:6} ".format ("IP","query","first","date","rr","ttl","count")
    pass 
Example 30
Project: pDNS2   Author: bez0r   File: pdns2_query.py    License: GNU General Public License v3.0 5 votes vote down vote up
def acount(self, domain):
        ''' counts of counts, or 'hits' for the domains in order, *.google.com or *.com are examples '''
        alldom = r.keys('Domain:'+str(domain)) 
        allcount = []
        for each in alldom:
            acount = r.hget(each, 'count')  
            if acount == None:
                pass
            else:
                allcount.append([int(acount),each]) 
        allcount.sort()
        for each in allcount:
            print str(each[0]),str(each[1])