def __init__(self, node, platform='', cpus=0, memory=0, disk=0):
if node.find('*') < 0:
try:
info = socket.getaddrinfo(node, None)[0]
ip_addr = info[4][0]
if info[0] == socket.AF_INET6:
ip_addr = re.sub(r'^0*', '', ip_addr)
ip_addr = re.sub(r':0*', ':', ip_addr)
ip_addr = re.sub(r'::+', '::', ip_addr)
node = ip_addr
except:
node = ''
if node:
self.ip_rex = node.replace('.', '\\.').replace('*', '.*')
else:
logger.warning('node "%s" is invalid', node)
self.ip_rex = ''
self.platform = platform.lower()
self.cpus = cpus
self.memory = memory
self.disk = disk
python类sub()的实例源码
def _glob_to_re(self, pattern):
"""Translate a shell-like glob pattern to a regular expression.
Return a string containing the regex. Differs from
'fnmatch.translate()' in that '*' does not match "special characters"
(which are platform-specific).
"""
pattern_re = fnmatch.translate(pattern)
# '?' and '*' in the glob pattern become '.' and '.*' in the RE, which
# IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix,
# and by extension they shouldn't match such "special characters" under
# any OS. So change all non-escaped dots in the RE to match any
# character except the special characters (currently: just os.sep).
sep = os.sep
if os.sep == '\\':
# we're using a regex to manipulate a regex, so we need
# to escape the backslash twice
sep = r'\\\\'
escaped = r'\1[^%s]' % sep
pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', escaped, pattern_re)
return pattern_re
def decrypt(hash, tipo):
global word
try:
if(tipo == 0):
url = BeautifulSoup(urllib.urlopen("https://md5.gromweb.com/?md5=" + hash), "html.parser")
else:
url = BeautifulSoup(urllib.urlopen("https://sha1.gromweb.com/?hash=" + hash), "html.parser")
password = url.find("em", {"class": "long-content string"})
password = re.sub(re.compile("<.*?>"), "", str(password)).strip()
if str(password) == "None":
print word+"\t\t\t\t[-] Senha nao encontrada! :-("
else:
print word+"\t\t\t\t[+] Senha encontrada: " + password
except IOError:
decryptwl(hash, tipo)
def safe_text_for_markdown(text):
"""Clean the text using bleach but keep certain Markdown sections.
Markdown code ie ` or ``` combos. For single `, do not allow line breaks between the tag.
Quotes ie '> ' which bleach would clean up.
"""
code_blocks, text = code_blocks_add_markers(text)
# Store quotes next
text = re.sub(r"(^> )", "%%safe_quote_in_start%%", text)
text = re.sub(r"(\n> )", "%%safe_quote_in_new_line%%", text, flags=re.DOTALL)
# Nuke all html, scripts, etc
text = bleach.clean(text or "")
# Return quotes
text = text.replace("%%safe_quote_in_start%%", "> ")
text = text.replace("%%safe_quote_in_new_line%%", "\n> ")
text = code_blocks_restore(code_blocks, text)
return text
def _clean_names(cls):
'''
Remove indices from non-repeated peeker names that don't need them.
When created, all peekers get an index appended to their name to
disambiguate any repeated names. If the name isn't actually repeated,
then the index is removed.
'''
index_re = '\[\d+\]$'
for name, peeker in cls._peekers.items():
if not peeker.name_dup:
new_name = re.sub(index_re, '', name)
if new_name != name:
peeker.trace.name = new_name
cls._peekers[new_name] = cls._peekers.pop(name)
def parse_article(self, subject, *args, **kwargs):
"""
Takes a an article header and returns it's parsed content if it's
successful. Otherwise it returns None.
"""
matched = NZB_SUBJECT_PARSE.match(subject)
if matched is None:
# subject is not parsable
return None
results = {}
# Trim results
if matched.group('desc') is not None:
results['desc'] = re.sub('[\s-]+$', '', matched.group('desc'))
if matched.group('fname') is not None:
results['fname'] = matched.group('fname').strip()
# Support conversion of integers
for _attr in ['index', 'count', 'yindex', 'ycount', 'size']:
if matched.group(_attr) is not None:
results[_attr] = int(matched.group(_attr))
return results
def __fmt_key(self, key):
"""Formats the hash key for more consistent hits; hence fetching the
'Message-ID' key should still be fetched even if the user indexes
with 'message-id'.
"""
def _fmt(_k):
return _k.group(1) + _k.group(2).upper()
if not isinstance(key, basestring):
# Handle invalid key entries types
key = str(key)
key = re.sub(
# Flip -id to ID (short for Identifier)
# Flip -crc to CRC (short for Cyclic Redundancy Check)
r'([_-])((id|crc)([^a-z0-9]|$))',
_fmt,
re.sub(r'(^|\s|[_-])(\S)', _fmt, key.strip().lower()),
flags=re.IGNORECASE,
)
if key in VALID_HEADER_ENTRIES or key.startswith(UNKNOWN_PREFIX):
return key
return UNKNOWN_PREFIX + key
def process_reply(reply, nested=False):
"""
Process a reply so it looks nice:
- if it's from the prototype yang integration, ditch the 'data' root
- convert from list to nested format if requested
- convert quotes to avoid escaping
"""
try:
# @@@ strip 'data' from yang output
reply['result'] = reply['result'][0]['data']
except Exception:
pass
# If required, and query successful, convert the reply['result'] format.
try:
if nested:
reply['result'] = reformat(reply['result'])
except KeyError:
# Fails silently if there is no 'reply['result']' in the reply['result'], this
# means an error occurred.
pass
# @@@ cheesily try to avoid \" everywhere, at cost of valid json
return re.sub(r'\\"', "'", json.dumps(reply))
def handle_misses(self, request, mapper, map_misses):
self.num_misses = len(map_misses)
self.found_values = []
for i, miss in enumerate(map_misses):
progress_indicator(
request,
"Incomplete mapping from cache, calculating remainder [{}/{}]..."
.format(i + 1, self.num_misses))
print('### FETCHING MISS {}/{}: {}'.format(i, self.num_misses, miss))
d = scrape.schema_describe('config ' + miss, request.sdata)
def got_miss_reply(result, key):
result = [re.sub("'", '"', path) for path in result]
if len(result) == 0:
result = None
print('### GOT RESULT ({} remaining, {} saved) {} -> {}'.format(
self.num_misses, len(self.found_values), key, result))
self.found_values.append((key, result))
self.num_misses -= 1
if self.num_misses == 0:
# Got everything!
mapper.populate_cache(self.found_values)
self.attempt_map(request, second_attempt=True)
d.addCallback(got_miss_reply, miss)
def matches(self, testString, parseAll=True):
"""
Method for quick testing of a parser against a test string. Good for simple
inline microtests of sub expressions while building up larger parser.
Parameters:
- testString - to test against this expression for a match
- parseAll - (default=C{True}) - flag to pass to C{L{parseString}} when running tests
Example::
expr = Word(nums)
assert expr.matches("100")
"""
try:
self.parseString(_ustr(testString), parseAll=parseAll)
return True
except ParseBaseException:
return False
def matches(self, testString, parseAll=True):
"""
Method for quick testing of a parser against a test string. Good for simple
inline microtests of sub expressions while building up larger parser.
Parameters:
- testString - to test against this expression for a match
- parseAll - (default=C{True}) - flag to pass to C{L{parseString}} when running tests
Example::
expr = Word(nums)
assert expr.matches("100")
"""
try:
self.parseString(_ustr(testString), parseAll=parseAll)
return True
except ParseBaseException:
return False
def _collect_zipimporter_cache_entries(normalized_path, cache):
"""
Return zipimporter cache entry keys related to a given normalized path.
Alternative path spellings (e.g. those using different character case or
those using alternative path separators) related to the same path are
included. Any sub-path entries are included as well, i.e. those
corresponding to zip archives embedded in other zip archives.
"""
result = []
prefix_len = len(normalized_path)
for p in cache:
np = normalize_path(p)
if (np.startswith(normalized_path) and
np[prefix_len:prefix_len + 1] in (os.sep, '')):
result.append(p)
return result
generate_reaction_templates.py 文件源码
项目:ochem_predict_nn
作者: connorcoley
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def reassign_atom_mapping(transform):
'''This function takes an atom-mapped reaction and reassigns
the atom-mapping labels (numbers) from left to right, once
that transform has been canonicalized.'''
all_labels = re.findall('\:([0-9]+)\]', transform)
# Define list of replacements which matches all_labels *IN ORDER*
replacements = []
replacement_dict = {}
counter = 1
for label in all_labels: # keep in order! this is important
if label not in replacement_dict:
replacement_dict[label] = str(counter)
counter += 1
replacements.append(replacement_dict[label])
# Perform replacements in order
transform_newmaps = re.sub('\:[0-9]+\]',
lambda match: (':' + replacements.pop(0) + ']'),
transform)
return transform_newmaps
def select_overview_subtab(self):
"""
Selects the overview sub tab.
"""
# this is what we would normally do:
# tab_manager.current = 'overview'
# but instead we need to simulate the click on the
# navigation bar children or the associated screen button
# would not have the selected color
overview_bnavigation = self.overview_bnavigation
navigation_bar = overview_bnavigation.children[0]
boxlayout = navigation_bar.children[0]
nav_headers = boxlayout.children
# the overview is the first/last button
overview_nav_header = nav_headers[-1]
overview_nav_header.dispatch('on_press')
def sms():
if len(sys.argv) < 2:
print(help_text)
return
if sys.argv[1] == "send":
if len(sys.argv) < 3:
print("????? ????? ?? ?? ???.")
return
if not re.match(r"[\+98|0]9[0-9]*",sys.argv[2]):
print("????? ???? ??? ?????? ???.tetete")
return
number = sys.argv[2]
if re.match(sys.argv[2], r"^\+98"):
number = re.sub("+98", "0", number)
text = sys.argv[3]
if len(text) > 100:
print("????? ??????? ??? ??? ????? ???? ???.")
return
send_sms(number, text, str(time.time()))
return
if sys.argv[1] == "credits":
get_credits()
return
def expand(chunks, name, version):
"Return the named chunk with any chunk-references recursively expanded."
template, latest = '', -1
for v in range(version+1):
t = chunks.get(name + ' v%d+' % v, '')
if t:
latest = v
template += t
for v in range(version, latest, -1):
t = chunks.get(name + ' v%d' % v, '')
if t:
template += t
break
if not template:
template = chunks[name]
return chunk_ref_pattern.sub(
lambda mo: indent(mo.group(1), expand(chunks, mo.group(2), version)),
template)
def strip_harakat(text):
"""Strip Harakat from arabic word except Shadda.
The striped marks are :
- FATHA, DAMMA, KASRA
- SUKUN
- FATHATAN, DAMMATAN, KASRATAN, , , .
Example:
>>> text = u"?????????????"
>>> stripTashkeel(text)
????????
@param text: arabic text.
@type text: unicode.
@return: return a striped text.
@rtype: unicode.
"""
# if text:
# return re.sub(HARAKAT_PATTERN, u'', text)
# return text
if not text:
return text
elif is_vocalized(text):
for char in HARAKAT:
text = text.replace(char, '')
return text
def strip_lastharaka(text):
"""Strip the last Haraka from arabic word except Shadda.
The striped marks are :
- FATHA, DAMMA, KASRA
- SUKUN
- FATHATAN, DAMMATAN, KASRATAN, , , .
Example:
>>> text = u"?????????????"
>>> stripTashkeel(text)
????????????
@param text: arabic text.
@type text: unicode.
@return: return a striped text.
@rtype: unicode.
"""
if text:
if is_vocalized(text):
return re.sub(LASTHARAKA_PATTERN, u'', text)
return text
def normalize_ligature(text):
"""Normalize Lam Alef ligatures into two letters (LAM and ALEF),
and Tand return a result text.
Some systems present lamAlef ligature as a single letter,
this function convert it into two letters,
The converted letters into LAM and ALEF are :
- LAM_ALEF, LAM_ALEF_HAMZA_ABOVE, LAM_ALEF_HAMZA_BELOW, LAM_ALEF_MADDA_ABOVE
Example:
>>> text = u"????? ???? ???????"
>>> normalizeLigature(text)
????? ???? ???????
@param text: arabic text.
@type text: unicode.
@return: return a converted text.
@rtype: unicode.
"""
if text:
return LIGUATURES_PATTERN.sub(u'%s%s'%(LAM, ALEF), text)
return text
def normalize_hamza(word):
"""Standardize the Hamzat into one form of hamza,
replace Madda by hamza and alef.
Replace the LamAlefs by simplified letters.
Example:
>>> text = u"??? ??? ??????"
>>> normalizeHamza(text)
??? ??? ??????
@param word: arabic text.
@type word: unicode.
@return: return a converted text.
@rtype: unicode.
"""
if word.startswith(ALEF_MADDA):
if len(word)>= 3 and (word[1] not in HARAKAT) and \
(word[2] == SHADDA or len(word) == 3):
word = HAMZA + ALEF + word[1:]
else:
word = HAMZA + HAMZA + word[1:]
# convert all Hamza from into one form
word = word.replace(ALEF_MADDA, HAMZA+HAMZA)
word = HAMZAT_PATTERN.sub(HAMZA, word)
return word
def tokenize(text = u""):
"""
Tokenize text into words
@param text: the input text.
@type text: unicode.
@return: list of words.
@rtype: list.
"""
if text == u'':
return []
else:
#split tokens
mylist = TOKEN_PATTERN.split(text)
# don't remove newline \n
mylist = [TOKEN_REPLACE.sub('',x) for x in mylist if x]
# remove empty substring
mylist = [x for x in mylist if x]
return mylist
def strip_tashkeel(text):
"""Strip vowel from a text and return a result text.
The striped marks are :
- FATHA, DAMMA, KASRA
- SUKUN
- SHADDA
- FATHATAN, DAMMATAN, KASRATAN, , , .
Example:
>>> text=u"?????????????"
>>> strip_tashkeel(text)
???????
@param text: arabic text.
@type text: unicode.
@return: return a striped text.
@rtype: unicode.
"""
return arabconst.HARAKAT_PAT.sub('', text)
#strip tatweel from a text and return a result text
#--------------------------------------
def strip_tatweel(text):
"""
Strip tatweel from a text and return a result text.
Example:
>>> text=u"????????????"
>>> strip_tatweel(text)
???????
@param text: arabic text.
@type text: unicode.
@return: return a striped text.
@rtype: unicode.
"""
return re.sub(u'[%s]' % arabconst.TATWEEL, '', text)
#--------------------------------------
def normalize_hamza(text):
"""Normalize Hamza forms into one form, and return a result text.
The converted letters are :
- The converted lettersinto HAMZA are: WAW_HAMZA,YEH_HAMZA
- The converted lettersinto ALEF are: ALEF_MADDA,
ALEF_HAMZA_ABOVE, ALEF_HAMZA_BELOW ,HAMZA_ABOVE, HAMZA_BELOW
Example:
>>> text=u"?????? ?? ??????"
>>> normalize_hamza(text)
?????? ?? ??????
@param text: arabic text.
@type text: unicode.
@return: return a converted text.
@rtype: unicode.
"""
text = arabconst.ALEFAT_PAT.sub(arabconst.ALEF, text)
return arabconst.HAMZAT_PAT.sub(arabconst.HAMZA, text)
#--------------------------------------
def normalize_lamalef(text):
"""Normalize Lam Alef ligatures into two letters (LAM and ALEF),
and return a result text.
Some systems present lamAlef ligature as a single letter,
this function convert it into two letters,
The converted letters into LAM and ALEF are :
- LAM_ALEF, LAM_ALEF_HAMZA_ABOVE, LAM_ALEF_HAMZA_BELOW,
LAM_ALEF_MADDA_ABOVE
Example:
>>> text=u"????? ???? ???????"
>>> normalize_lamalef(text)
????? ???? ???????
@param text: arabic text.
@type text: unicode.
@return: return a converted text.
@rtype: unicode.
"""
return arabconst.LAMALEFAT_PAT.sub(\
u'%s%s'%(arabconst.LAM, arabconst.ALEF), text)
#--------------------------------------
def matches(self, testString, parseAll=True):
"""
Method for quick testing of a parser against a test string. Good for simple
inline microtests of sub expressions while building up larger parser.
Parameters:
- testString - to test against this expression for a match
- parseAll - (default=C{True}) - flag to pass to C{L{parseString}} when running tests
Example::
expr = Word(nums)
assert expr.matches("100")
"""
try:
self.parseString(_ustr(testString), parseAll=parseAll)
return True
except ParseBaseException:
return False
def matches(self, testString, parseAll=True):
"""
Method for quick testing of a parser against a test string. Good for simple
inline microtests of sub expressions while building up larger parser.
Parameters:
- testString - to test against this expression for a match
- parseAll - (default=C{True}) - flag to pass to C{L{parseString}} when running tests
Example::
expr = Word(nums)
assert expr.matches("100")
"""
try:
self.parseString(_ustr(testString), parseAll=parseAll)
return True
except ParseBaseException:
return False
def _collect_zipimporter_cache_entries(normalized_path, cache):
"""
Return zipimporter cache entry keys related to a given normalized path.
Alternative path spellings (e.g. those using different character case or
those using alternative path separators) related to the same path are
included. Any sub-path entries are included as well, i.e. those
corresponding to zip archives embedded in other zip archives.
"""
result = []
prefix_len = len(normalized_path)
for p in cache:
np = normalize_path(p)
if (np.startswith(normalized_path) and
np[prefix_len:prefix_len + 1] in (os.sep, '')):
result.append(p)
return result
def sanitize_name(name):
"""
Sanitize a given name such that it conforms to unix policy.
Args:
name: the name to sanitize.
Returns:
The sanitized form of name.
"""
if len(name) == 0:
raise Exception("Can not sanitize an empty field.")
sanitized_name = re.sub(r"[^a-z0-9\+-]", "-", name.lower())
if sanitized_name[0] in string.digits:
sanitized_name = "p" + sanitized_name
return sanitized_name
#I will never understand why the shutil functions act
#the way they do...
def addToCart(self):
print '\nADD TO CART -----------------'
session_get = self.user_session.get(self.URL_product_url, headers=self.get_headers)
#print session_get.content
soup = BeautifulSoup(session_get.content, 'lxml')
results = soup.find_all('select', class_='size-select')
#print results
for item in results[0].select('option'):
re_result = re.sub(self.sub_pattern, '', item.string)
#print re_result
matchObj = re.search(r"^%s+$" % self.user_size, re_result)
if matchObj:
self.post_data_addToCart['pid'] = item['value']
self.post_data_addToCart['masterPID'] = item['value'].partition("_")[0]
print self.post_data_addToCart
break
session_post = self.user_session.post(url=self.URL_cart_post_url, headers=self.post_headers, data=self.post_data_addToCart)
print 'Add To Cart Status: ' + str(session_post.status_code)