def getOfflineMediaList(self, folderName=False, title=False, contentType=7):
mediaFiles = []
for r1 in re.finditer('\{(.*?)\"spaces\"\:' , entryS, re.DOTALL):
entry = r1.group(1)
media = self.getMediaPackage(entry, folderName=folderName, contentType=contentType, fanart=folderFanart, icon=folderIcon)
if media is not None:
mediaFiles.append(media)
return mediaFiles
##
# retrieve a list of videos, using playback type stream
# parameters: prompt for video quality (optional), cache type (optional)
# returns: list of videos
##
python类finditer()的实例源码
def get_occurences(self, pattern, overlap=False):
"""Return position of the input pattern in the sequence
::
>>> from sequana import Sequence
>>> s = Sequence('ACGTTTTACGT')
>>> s.get_occurences("ACGT")
[0, 7]
"""
if overlap is False:
res = [m.start() for m in re.finditer(pattern, self.sequence)]
elif overlap is True:
res = [m.start() for m in re.finditer('(?=%s)'%pattern, self.sequence)]
return res
# reverse find-all without overlaps, you can combine positive and
# negative lookahead into an expression like this:
#res = [m.start() for m in re.finditer('(?=%s)(?!.{1,%d}%s)' % (search,
# len(pattern)-1, pattern), 'ttt')]
def find_cute(url):
# ??????
r = requests.get(url)
# ??r.encoding
encoding = re.search('content="text/html;\s*charset=(.*?)"', r.text).group(1)
r.encoding = encoding
# print(r.text)
finds = re.finditer(r'<p>\s*([^>]*?)\s*\n', r.text)
i = random.randint(0, sum(1 for _ in finds))
start = 0
finds = re.finditer(r'<p>\s*([^>]*?)\s*\n', r.text)
for f in finds:
if start == i:
print(f.group(1))
break
start += 1
def _proc_gnusparse_00(self, next, pax_headers, buf):
"""Process a GNU tar extended sparse header, version 0.0.
"""
offsets = []
for match in re.finditer(br"\d+ GNU.sparse.offset=(\d+)\n", buf):
offsets.append(int(match.group(1)))
numbytes = []
for match in re.finditer(br"\d+ GNU.sparse.numbytes=(\d+)\n", buf):
numbytes.append(int(match.group(1)))
next.sparse = list(zip(offsets, numbytes))
def _proc_gnusparse_00(self, next, pax_headers, buf):
"""Process a GNU tar extended sparse header, version 0.0.
"""
offsets = []
for match in re.finditer(br"\d+ GNU.sparse.offset=(\d+)\n", buf):
offsets.append(int(match.group(1)))
numbytes = []
for match in re.finditer(br"\d+ GNU.sparse.numbytes=(\d+)\n", buf):
numbytes.append(int(match.group(1)))
next.sparse = list(zip(offsets, numbytes))
def _proc_gnusparse_00(self, next, pax_headers, buf):
"""Process a GNU tar extended sparse header, version 0.0.
"""
offsets = []
for match in re.finditer(br"\d+ GNU.sparse.offset=(\d+)\n", buf):
offsets.append(int(match.group(1)))
numbytes = []
for match in re.finditer(br"\d+ GNU.sparse.numbytes=(\d+)\n", buf):
numbytes.append(int(match.group(1)))
next.sparse = list(zip(offsets, numbytes))
def get_selects_from_text(content):
sqls = []
select_keyword = '@Select\s*\('
for m in re.finditer(select_keyword, content):
rparen_pos = MybatisInlineSqlExtractor.find_right_paren_pos(content[m.end():])
if rparen_pos < 0:
continue
sqls.append(SQL('', eval(content[m.end():m.end() + rparen_pos].replace('\r', '').replace('\n', '')).strip()))
return sqls
def get_properties_dict(self, properties_str):
if isinstance(properties_str, dict):
return properties_str
properties = collections.OrderedDict()
pattern = re.compile('(\S+?)\s*"(.*?)"')
for m in re.finditer(pattern, properties_str):
key = m.group(1)
value = m.group(2)
properties[key] = value
return properties
def get_unambiguous_regions(reference_path):
'''Calculate regions corresponding to unambiguous bases'''
chrom_map = {}
for chrom, seq in open_reference(reference_path).items():
regions = [(m.start(), m.end()) for m in re.finditer('[acgtACGT]+', seq[:])]
chrom_map[chrom] = Regions(regions=regions)
return chrom_map
def tokenize_sents(string):
"""
Tokenize input text to sentences.
:param string: Text to tokenize
:type string: str or unicode
:return: sentences
:rtype: list of strings
"""
string = six.text_type(string)
spans = []
for match in re.finditer('[^\s]+', string):
spans.append(match)
spans_count = len(spans)
rez = []
off = 0
for i in range(spans_count):
tok = string[spans[i].start():spans[i].end()]
if i == spans_count - 1:
rez.append(string[off:spans[i].end()])
elif tok[-1] in ['.', '!', '?', '…', '»']:
tok1 = tok[re.search('[.!?…»]', tok).start()-1]
next_tok = string[spans[i + 1].start():spans[i + 1].end()]
if (next_tok[0].isupper()
and not tok1.isupper()
and not (tok[-1] != '.'
or tok1[0] == '('
or tok in ABBRS)):
rez.append(string[off:spans[i].end()])
off = spans[i + 1].start()
return rez
def register_options(self):
# type: () -> None
"""Parse options from text like this:
# Uncrustify 0.63
#
# General options
#
newlines { Auto, LF, CR, CRLF }
The type of line endings
input_tab_size Number
The original size of tabs in the input
indent_align_string { False, True }
Whether to indent strings broken by '\' so that they line up
"""
exeresult = run_executable(self.exe, ['--show-config'], cache=self.cache)
options = []
text = unistr(exeresult.stdout)
for m in re.finditer(r'^(\w+)\s+(.*?)\s*$', text, re.MULTILINE):
optionname, optiondesc = m.group(1), m.group(2)
if optiondesc.startswith('{'):
optiontype = 'Enum'
configs = optiondesc[1:-1].strip().split(', ')
configs = [c.lower() for c in configs]
else:
optiontype = optiondesc
configs = []
options.append(option_make(optionname, optiontype, configs))
self.styledefinition = styledef_make(options)
def __handle_tostring(self):
for match in re.finditer('(\d+)\[t\+o\+S\+t\+r\+i\+n\+g\](\d+)', self.js):
repl = to_base(match.group(1), match.group(2))
self.js = self.js.replace(match.group(0), repl)
def __get_attribs(element):
attribs = {}
for match in re.finditer('''\s+(?P<key>[^=]+)=\s*(?:(?P<delim>["'])(?P<value1>.*?)(?P=delim)|(?P<value2>[^"'][^>\s]*))''', element):
match = match.groupdict()
value1 = match.get('value1')
value2 = match.get('value2')
value = value1 if value1 is not None else value2
if value is None: continue
attribs[match['key'].lower().strip()] = value
return attribs
def album(URL):
track_list=[]
if (URL.find('?index=')>0):
all_track_nr=((html.count('?index='))//2)-1
a1=URL[:URL.find('?index=')]
current_track_no=int(URL[len(a1)+len('?index='):])
ID=a1[a1.find('/album/')+len('/album/'):]
track_list.append('%s'%current_track_no)
elif (URL.find('?start')>0):
all_track_nr=((html.count('?index='))//2)-1
a1=URL[:URL.find('?start')]
current_track_no=int(URL[len(a1)+len('?start'):])
ID=a1[a1.find('/album/')+len('/album/'):]
track_list.append('%s'%current_track_no)
else:
all_track_nr=(html.count('?index='))//2
a1=URL
current_track_no='null'
ID=a1[a1.find('/album/')+len('/album/'):]
track_list.append('%s'%current_track_no)
i=0
b=html[html.find('<span class="song_name">'):html.rfind('<span class="song_name">')]
b_len=len('<span class="song_name">')
iter=re.finditer(r'<span class="song_name">', b)
indices=[m.start(0) for m in iter]
while i<all_track_nr:
track_list.append('%s?index=%d'%(a1,i))
d=(b[indices[i]:].find('</span>'))
track_name=b[indices[i]+b_len:indices[i]+d]
track_list.append(track_name)
i+=1
return(track_list)
def album(URL):
track_list=[]
if (URL.find('?index=')>0):
all_track_nr=((html.count('?index='))//2)-1
a1=URL[:URL.find('?index=')]
current_track_no=int(URL[len(a1)+len('?index='):])
ID=a1[a1.find('/album/')+len('/album/'):]
track_list.append('%s'%current_track_no)
elif (URL.find('?start')>0):
all_track_nr=((html.count('?index='))//2)-1
a1=URL[:URL.find('?start')]
current_track_no=int(URL[len(a1)+len('?start'):])
ID=a1[a1.find('/album/')+len('/album/'):]
track_list.append('%s'%current_track_no)
else:
all_track_nr=(html.count('?index='))//2
a1=URL
current_track_no='null'
ID=a1[a1.find('/album/')+len('/album/'):]
track_list.append('%s'%current_track_no)
i=0
b=html[html.find('<span class="song_name">'):html.rfind('<span class="song_name">')]
b_len=len('<span class="song_name">')
iter=re.finditer(r'<span class="song_name">', b)
indices=[m.start(0) for m in iter]
while i<all_track_nr:
track_list.append('%s?index=%d'%(a1,i))
d=(b[indices[i]:].find('</span>'))
track_name=b[indices[i]+b_len:indices[i]+d]
track_list.append(track_name)
i+=1
return(track_list)
def legacy_format_json(original):
# save state
states = []
text = original
# save position for double-quoted texts
for i, pos in enumerate(re.finditer('"', text)):
# pos.start() is a double-quote
p = pos.start() + 1
if i % 2 == 0:
nxt = text.find('"', p)
states.append((p, text[p:nxt]))
# replace all weired characters in text
while text.find(',,') > -1:
text = text.replace(',,', ',null,')
while text.find('[,') > -1:
text = text.replace('[,', '[null,')
# recover state
for i, pos in enumerate(re.finditer('"', text)):
p = pos.start() + 1
if i % 2 == 0:
j = int(i / 2)
nxt = text.find('"', p)
# replacing a portion of a string
# use slicing to extract those parts of the original string to be kept
text = text[:p] + states[j][1] + text[nxt:]
converted = json.loads(text)
return converted
def make_RefCmap(fasta_file, enz=None, min_len=20, min_nsite=5, path=None):
name = fasta_file.rsplit('.',1)[0].split('/')[-1]
index = 0
enzymes = {'BspQI':'GCTCTTC',
'BbvCI':'CCTCAGC',
'Bsml':'GAATGC',
'BsrDI':'GCAATG',
'bseCI':'ATCGAT',
'BssSI':'CACGAG'}
try:
cmap_file='%s/%s_%s.cmap'%(path,name,enz)
forwards = enzymes[enz]
reverse = str(Seq(forwards).reverse_complement())
with open (cmap_file,'a') as ref_cmap:
ref_cmap.write('# CMAP File Version:\t0.1\n')
ref_cmap.write('# Label Channels:\t1\n')
ref_cmap.write('# Nickase Recognition Site 1:\t%s\n'%forwards)
ref_cmap.write('# Enzyme1:\tNt.%s\n'%enz)
ref_cmap.write('# Number of Consensus Nanomaps:\tN/A\n')
ref_cmap.write('#h CMapId\tContigLength\tNumSites\tSiteID\tLabelChannel\tPosition\tStdDev\tCoverage\tOccurrence\n')
ref_cmap.write('#f int\tfloat\tint\tint\tint\tfloat\tfloat\tint\tint\n')
for seqs in SeqIO.parse(fasta_file,'fasta'):
seq = str(seqs.seq.upper())
seq_len = len(seq)
index+=1
if seq_len >= min_len*1000:
nsites = len(re.findall('%s|%s'%(forwards,reverse),seq))
if nsites >=min_nsite:
j=1
for o in re.finditer('%s|%s'%(forwards,reverse),seq):
ref_cmap.write('%s\t%.1f\t%d\t%d\t1\t%.1f\t1.0\t1\t1\n'%(index,seq_len,nsites,j,o.start()+1))
j+=1
ref_cmap.write('%s\t%.1f\t%d\t%d\t0\t%.1f\t0.0\t1\t0\n'%(index,seq_len,nsites,j,seq_len))
except:
pass
def formatString(self, text, stats, not_found_replacement = None):
#try:
values = stats['values']
for m in re.finditer("{{([gc]:)?([^}:]*)((:d)|(:1f)|:(\d+)|:(\d+)\.(\d+)f|(:\+d)|(:\+1f))?}}", text):
g, g1, key, g2, sg1, sg2, sg3, sg4a, sg4b, sg5, sg6 = m.group(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
if not key in values:
if not_found_replacement is None:
if d: LOG_NOTE('No key in values of %s (%s)' % (stats.get('_type', 'unknown'), key))
else:
text = text.replace('%s' % g, not_found_replacement)
elif g1 is None:
if g2 is None:
text = text.replace('{{%s}}' % key, self.applyMacros(values[key]))
elif sg1:
text = text.replace('{{%s:d}}' % key, self.applyMacros(values[key], 0))
elif sg2:
text = text.replace('{{%s:1f}}' % key, self.applyMacros(values[key], 1))
elif sg3:
xx = int(sg3)
text = text.replace('{{%s:%d}}' % (key, xx), self.applyMacros2(values[key], xx))
elif sg4a:
xx, yy = int(sg4a), int(sg4b)
text = text.replace('{{%s:%d.%df}}' % (key, xx, yy), self.applyMacros2(values[key], xx, yy))
elif sg5:
text = text.replace('{{%s:+d}}' % key, self.applyMacros(values[key], 0, '+'))
elif sg6:
text = text.replace('{{%s:+1f}}' % key, self.applyMacros(values[key], 1, '+'))
elif g1=="g:":
text = text.replace('{{g:%s}}' % key, stats['gradient'][key])
elif g1=="c:":
text = text.replace('{{c:%s}}' % key, stats['palette'][key])
#except:
# LOG_CURRENT_EXCEPTION()
#finally:
return text
def __call__(self, text):
for match in re.finditer(self.regexp, text):
name = match.lastgroup
value = match.group(0)
span = match.span()
rule = self.mapping[name]
token = rule(value, span)
yield token
def load(self, model_name='main'):
checkpoint_dir = os.path.join(self.checkpoint_dir, self.model_dir)
ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
self.saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name))
counter = int(next(re.finditer("(\d+)(?!.*\d)", ckpt_name)).group(0))
return True, counter
else:
print("Failed to find a checkpoint")
return False, 0