def __init__(self, commoditiesIdNm={}, commoditiesIdCode={}): # self.countriesIdNm = countriesIdNm if len(countriesIdNm) else None self.commoditiesIdNm = commoditiesIdNm if len(commoditiesIdNm) else None self.commoditiesIdCode = commoditiesIdCode if len(commoditiesIdCode) else None ########## stop words self.commStopWords = { 0: reCompile("partner|independ|executive|director|trade|negotiator"\ +"|role|auditor|declar|represent|manager|offer|engage|long|time"\ +"|underwriter|party|integrated|business|sponsor|joint|role"\ +"|co-lead|agents|assignment|arranger|plaintiff|lender|engineer"\ +"|player|dealer|private|rich|corporate|communications|email"\ +"|advisor|arranger|regulation|estimator|consultant|up", reIGNORECASE ) } ########## Commodities if self.commoditiesIdNm is not None: comPat = '' for commid, names in self.commoditiesIdNm.items(): namespat = '\W|\W'.join( names ) comPat = comPat + f"|(?P<com{commid}>\\W{namespat}\\W)" comPat = comPat.strip('|') comCodePat = '' for commid, commcode in self.commoditiesIdCode.items(): comCodePat = comCodePat + f"|(?P<com{commid}>\\W{commcode}\\W)" comCodePat = comCodePat.strip('|') self.comCodePatCompiled = reCompile(comCodePat) self.comPatCompiled = reCompile(comPat, reIGNORECASE) Span.set_extension('commodities', getter=self.Commodities, force=True) Doc.set_extension('commodities', default=[], force=True)
def __call__(self, parser, namespace, values, option_string = None): key, auth = values if reCompile('[0-9A-Z]{8}-[0-9A-Z]{4}').match(key): # New reservation if not reCompile('[0-9a-f]{1,64}').match(auth): raise ValueError("invalid (new) key auth") elif reCompile('[0-9A-Z]{8}').match(key): # Existing reservation if not reCompile("[0-9a-f]{32}").match(auth): raise ValueError("invalid (existing) key auth") else: raise ValueError("invalid key") setattr(namespace, self.dest, values)
def compareOutput(self, gotLines, expectedLines): self.assertTrue(gotLines, 'We could not collect any output.') index = 0 for expectedLine in expectedLines: expectedPattern = reCompile(expectedLine) foundLine = False lastIndex = index while True: try: line = gotLines[index] except IndexError: break index += 1 if expectedPattern.match(line): foundLine = True break if not foundLine: break else: expectedLine = False nextOutput = '\n'.join(gotLines[lastIndex:][:3]) fullOutput = '\n'.join(gotLines) msg = (f'Expected line not found:\n{expectedLine}\n\n' f'Next three lines were:\n{nextOutput}\n\n' f'Complete output was:\n{fullOutput}\n') self.assertTrue(foundLine, msg)
def waitForAppServer(self, pattern, wait=5): """Check that the appserver output contains the specified pattern. Returns True or False depending on whether the pattern was seen. """ start = time.time() comp = reCompile(pattern) lines = [] found = False while 1: try: line = self._queue.get_nowait() except Empty: line = None if line is None: now = time.time() if now - start > wait: break time.sleep(0.2) else: if len(lines) > 9: del lines[0] lines.append(line) if comp.search(line): found = True break self._actualAppServerOutput = ''.join(lines) return found
def compileMatchPatterns(): patterns = { '0001': '[DR,3][SR1,3](000[SR2,3]|1[imm5,5])', '0101': '[DR,3][SR1,3](000[SR2,3]|1[imm5,5])', '0000': '[n,1][z,1][p,1][PCoffset9,9]', '1100': '000111000000|000[BaseR,3]000000', '0100': '1[PCoffset11,11]|000[BaseR,3]000000', '0010': '[DR,3][PCoffset9,9]', '1010': '[DR,3][PCoffset9,9]', '0110': '[DR,3][BaseR,3][offset6,6]', '1110': '[DR,3][PCoffset9,9]', '1001': '[DR,3][SR,3]111111', '1000': '00000000000000', '0011': '[SR,3][PCoffset9,9]', '1011': '[SR,3][PCoffset9,9]', '0111': '[SR,3][BaseR,3][offset,6]', '1111': '0000[trapvect8,8]' } for opcode, opformat in patterns.items(): opPattern = sub( r'(\[(\S+?),(\d{1,2})\])', lambda i: f'(?P<{i.group(2)}>\d' + '{' + i.group(3) + '})', opcode + opformat) patterns[opcode] = reCompile(opPattern) return patterns
def __call__(self, parser, namespace, values, option_string=None): m = reCompile( '^https://book.passkey.com/reg/([0-9A-Z]{8}-[0-9A-Z]{4})/([0-9a-f]{1,64})$' ).match(values) if m: setattr(namespace, self.dest, m.groups()) else: raise ArgumentError(self, "invalid passkey url: '%s'" % values)
def __call__(self, parser, namespace, values, option_string = None): for pattern in ( '^https://book.passkey.com/reg/([0-9A-Z]{8}-[0-9A-Z]{4})/([0-9a-f]{1,64})$', '^https://book.passkey.com/event/[0-9]+/owner/[0-9]+/r/([0-9A-Z]{8})/([0-9a-f]{32})', ): m = reCompile(pattern).match(values) if m: setattr(namespace, self.dest, m.groups()) return raise ArgumentError(self, "invalid passkey url: '%s'" % values)
def load(self): """Reads key value pairs into self, provided the file exists. """ if not self.configfile: return r = reCompile('^[a-zA-Z]+[a-zA-Z0-9_]*$') with open(self.configfile, 'rb') as fp: line = fp.readline() while line: key, _, value = line.partition(' ') key, value = key.strip(), value.strip() if r.match(key): self.__dict__[key] = value or True line = fp.readline()
class CSVdumpable(CSVfileable): NEEDS_HEADER = True HEADER_COMMENT = '''\ %s.csv for "Dark Tower: All Hail" LARP. %s !!! DO NOT EDIT !!! Generated at %s ''' INSTANCES = None FILE_NAME_PATTERN = '%s.csv' TITLE = None GENERATOR = None HEADER_TITLE = None LIST_PATTERN = reCompile(r'(?<!{){%(.*?)}') @classmethod def getInstances(cls): return cls.INSTANCES.itervalues() if isinstance(cls.INSTANCES, dict) else cls.INSTANCES @classmethod def getFileName(cls): return getFileName(cls.FILE_NAME_PATTERN % cls.TITLE) @classmethod def getHeaderComment(cls): ret = cls.HEADER_COMMENT return ret % (cls.TITLE, cls.HEADER_TITLE + (('\n\nGenerated by %s.py' % cls.GENERATOR) if cls.GENERATOR else ''), currentTime()) if ret else None @classmethod def fillTemplate(cls, templateFileName, target, params = ()): with open(getFileName(templateFileName), 'rb') as f: template = f.read() result = cls.LIST_PATTERN.sub(lambda m: '\n'.join((m.group(1) % v.__dict__) for v in cls.INSTANCES.itervalues()), template) result = result % params with open(getFileName(target), 'wb') as f: f.write(result)
class GoogleTableEntry(CSVable): CSV_FIELDS = ('rID', 'rName', 'level', 'timeout', 'doganAmount', 'eName', 'eType', 'ePriority', 'contents') REASON_PATTERN = reCompile(r'[A-Z][A-Z0-9_]*|[A-Z][a-zA-Z]*') EMOTION_PATTERN = reCompile(r'[A-Z][A-Z0-9_]*') LEVELS = OrderedDict({'NONE': 0, 'NEAR': 1, 'MEDIUM': 2, 'FAR': 3}) ETYPES = OrderedDict({'SINGLE': 0, 'REPEAT': 1}) def processFromGoogleCSV(self): """Process the objects after it was loaded from CSV exported from Google Docs spreadsheet.""" # rName self.rName = self.rName.strip() if not self.rName: return try: self.rID = int(self.rID) except ValueError: assert False, "rID is not a number for reason %s: %r" % ( self.rName, self.rID) assert REASON_ID_START <= self.rID <= REASON_ID_END, "Incorrect rID for reason %s: %r, expected %s <= rID < %d" % ( self.rName, self.rID, REASON_ID_START, REASON_ID_END) assert self.rID not in (r.rID for r in Reason.INSTANCES.itervalues() ), "Duplicate rID: %s" % self.rID try: self.rName = self.rName.encode('ascii') except UnicodeError: assert False, "Reason name is not ASCII: %r" % self.rName if self.rName != '-': assert self.REASON_PATTERN.match( self.rName ), "Reason name is not in PROPER_FORMAT_WITH_DIGITS: %s" % self.rName assert self.rName not in Reason.INSTANCES, "Duplicate reason name: %s" % self.rName # level try: self.level = self.LEVELS[self.level.upper()] except KeyError: assert False, "Incorrect level for reason %s: %r, expected '%s'" % ( self.rName, self.level, '\' or \''.join(self.LEVELS)) # timeout try: self.timeout = int(self.timeout or 0) except ValueError: assert False, "Timeout is not a number for reason %s: %r" % ( self.rName, self.timeout) assert 0 <= self.timeout <= 999 or self.timeout == -1, "Bad timeout for reason %s: %r" % ( self.rName, self.timeout) # doganAmount try: self.doganAmount = int(self.doganAmount or 0) except ValueError: assert False, "Dogan amount is not a number for reason %s: %r" % ( self.rName, self.doganAmount) assert -3 <= self.doganAmount <= 10, "Bad dogan amount for reason %s: %r" % ( self.rName, self.doganAmount) # eName try: self.eName = self.eName.strip().encode('ascii') except UnicodeError: assert False, "Emotion name is not ASCII for reason %s: %r" % ( self.rName, self.eName) assert not self.eName or self.EMOTION_PATTERN.match( self.eName ), "Emotion name is not in PROPER_FORMAT_WITH_DIGITS for reason %s: %s" % ( self.rName, self.eName) if self.eName: # eType try: self.eType = self.ETYPES[self.eType.upper()] except KeyError: assert False, "Incorrect eType for reason %s: %r, expected '%s'" % ( self.rName, self.eType, '\' or \''.join(self.ETYPES)) # ePriority try: self.ePriority = int(self.ePriority or 0) except ValueError: assert False, "Priority is not a number for reason %s, emotion %s: %r" % ( self.rName, self.eName, self.ePriority) # isPlayer self.isPlayer = int(u'игроки' in self.contents) # pylint: disable=E1101 # ePriority consistency emotion = Emotion.INSTANCES.get(self.eName) if emotion: assert emotion.eType == self.eType, "Non-consistent type for emotion %s: %d and %d" % ( self.eName, emotion.eType, self.eType) assert emotion.ePriority == self.ePriority, "Non-consistent priority for emotion %s: %d and %d" % ( self.eName, emotion.ePriority, self.ePriority) elif self.eName: emotion = Emotion.addEmotion( Emotion(self.eName, self.eType, self.ePriority, self.isPlayer)) else: emotion = None Reason.addReason( Reason(self.rID, self.rName, self.level, self.timeout, self.doganAmount, self.eName)) @classmethod def loadFromGoogleDocs(cls, dumpCSV=False, dumpCSV1251=False): """Loads the reasons and emotions from the Google Docs spreadsheet.""" print "Fetching data from Google Docs..." Reason.INSTANCES.clear() Emotion.INSTANCES.clear() for attempt in xrange(1, 3): try: if attempt == 1: # load from URL googleDocID = open( getFileName(GOOGLE_DOC_ID_FILE_NAME)).read().strip() url = CSV_URL % googleDocID data = urlopen(url).read() else: # attempt == 2: # load from cached copy data = open(getFileName(DUMP_FILE_NAME), 'rb').read() for self in CSVObjectReader(data.splitlines(), cls, True, 'utf-8'): self.processFromGoogleCSV() break except Exception, e: if attempt == 1: print format_exc() print "ERROR fetching data: %s, using cached version" % e else: raise if attempt == 1: # pylint: disable=W0631 if dumpCSV: with open( dumpCSV if dumpCSV is not True else getFileName(DUMP_FILE_NAME), 'wb') as f: f.write(data) if dumpCSV1251: with open( dumpCSV1251 if dumpCSV1251 is not True else getFileName(DUMP_FILE_NAME_1251), 'wb') as f: f.write(data.decode('utf-8').encode(ENCODING))
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with "Open Annotation Service"; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # ## end license ## from optparse import OptionParser, Option from sys import exit from re import compile as reCompile VALIDNAMES=reCompile(r'^[A-Za-z0-9]+$') alloptions = dict((o.dest, o) for o in [ Option('-n', '--name', help="Service name, characters allowed A-Za-z0-9", dest="name"), Option('-p', '--port', type='int', help="Default port number", dest="port"), Option('', '--stateDir', help="Directory to write state.", dest="stateDir"), Option('', '--solrDataDir', help="Directory for solr data.", dest="solrDataDir"), Option('-c', '--configFile', help="Config file.", dest="configFile"), Option('', '--storeLocation', help="Directory for OWLIM data.", dest="storeLocation"), Option('', '--updatePort', type='int', help="Port number of Search Service update", dest="updatePort"), ]) applicationProfiles = { 'search': dict( options=['configFile'], mandatoryKeys=['configFile']),
return list(chain.from_iterable(xs)) class NoValue(Enum): def __repr__(self): return "<%s.%s>" % (self.__class__.__name__, self.name) def strtoi(s: str) -> Optional[int]: try: return int(s) except ValueError as e: return None def filterOutNones(xs: Sequence[Optional[T]]) -> Sequence[T]: return cast(Sequence[T], filter(lambda x: x is not None, xs)) TEXTDECODE_RE = reCompile(r"^(.*)_([0-9]+)_(.*)$") def textdecode(s: str) -> str: while True: match = TEXTDECODE_RE.match(s) if match is None: break [begin, char, end] = match.groups() s = begin + chr(int(char)) + end return s
def __call__(self, parser, namespace, values, option_string = None): m = reCompile('^https://book.passkey.com/reg/([0-9A-Z]{8}-[0-9A-Z]{4})/([0-9a-f]{64})$').match(values) if m: setattr(namespace, self.dest, m.groups()) else: raise ArgumentError(self, "invalid passkey url: '%s'" % values)
class WikiRider(object): """Wikiruns maker""" WIKI_REGEX = reCompile(r"https://.*\.wikipedia\.org/wiki/.[^:#]{3,}$") WIKI_PAGE_REGEX = reCompile(r"^/wiki/.[^:#]+$") HREF_REGEX = reCompile(r"^/wiki/.*") def __init__(self, starting_url, depth): """WikiRider constructor Parameters ---------- starting_url : str Url to any wikipedia web article, starting point for the wikirun depth : int Quantity of webpages to visit """ self.depth = depth self.depth_counter = 0 self.next_url = starting_url self.base_url = starting_url.split('/wiki/')[0] self.visited_urls = [] self.possible_urls = [] self.html_source = None def run(self): """Do a run across wikipedia articles Yields ------ WikiRider Yield this instance for each time it visits a new webpage """ if self.depth_counter < self.depth: self.visited_urls.append(self.next_url) self._scrape_html_source() yield self self._search_urls() self._set_destination() for rider_state in self.run(): yield self def _scrape_html_source(self): """Scrape html soup from next url""" try: self.html_source = Bs(req.get(self.next_url).content, 'lxml') except req.RequestException: self.printer.print_connection_error() return None def _search_urls(self): """Look for possible urls""" self.possible_urls = [] for a in self.html_source.find_all('a', href=self.HREF_REGEX): if (a.text and WikiRider.valid_url(a['href']) and a['href'] not in self.next_url): valid = True for visited_url in self.visited_urls: if a['href'] in visited_url: valid = False break if valid: self.possible_urls.append(a['href']) def _set_destination(self): """Randomly choose next url to travel""" if not self.possible_urls: self.depth_counter = self.depth else: next_url_tail = rchoice(self.possible_urls) while next_url_tail in self.next_url: next_url_tail = rchoice(self.possible_urls) self.depth_counter += 1 self.next_url = self.base_url + next_url_tail @staticmethod def valid_url(url): if "Main_Page" in url: return False return (WikiRider.WIKI_REGEX.match(url) is not None or WikiRider.WIKI_PAGE_REGEX.match(url) is not None)
def checkCinemaAvailability(self, cinemaLink, movieName): ''' Notifies if a show is available in your requested cinema ''' cinemaDetails = self.ss.get(cinemaLink) if not (cinemaDetails.url == cinemaLink): print( "Counters haven't opened for specified date yet, retrying...") return False assert cinemaDetails.status_code == 200 cinemaSoup = BeautifulSoup(cinemaDetails.content, 'html5lib') # get movie name jsonRespOfMovies = self.getUrlDataJSON(movieName) # return the first hit belonging to movieName data = {} for movieInfo in jsonRespOfMovies['hits']: if movieInfo.get('TYPE_NAME') == "Movies": data = movieInfo break movieName = data.get('TITLE', movieName) scripts = cinemaSoup.find_all("script") jsonMoviePattern = reCompile( "^\s*try\s+{\s+var\s+API\s+=\s+JSON.parse\(\"(.*)\"\);") jsonMovieFormats = {} for script in scripts: jsonMovieFormats = jsonMoviePattern.match(str(script.string)) if jsonMovieFormats: jsonMovieFormats = jsonMovieFormats.groups()[0] # now remove double slashes jsonMovieFormats = jsonMovieFormats.replace("\\", "") # now convert to json jsonMovieFormats = loads(jsonMovieFormats) break # now see if your format is available found = False if jsonMovieFormats['BookMyShow']['Event']: for event in jsonMovieFormats['BookMyShow']['Event']: if event.get('EventTitle') == movieName: for eventFormat in event['ChildEvents']: if self.format is None: # movie is available in any format found = True break elif eventFormat['EventDimension'] == self.format: # we found our format found = True break # string to print format if available formatAvailable = "" if self.format is not None: formatAvailable = " in " + self.format if found: # Movie tickets are now available print("HURRAY! Movie tickets are now available" + formatAvailable) self.notification( "Hurray!", "Tickets for " + movieName + " at " + self.title + " are now available" + formatAvailable) self.soundAlarm() return True elif jsonMovieFormats['BookMyShow']['Event']: # The requires format isn't available or the movie is yet to be released availableFormats = [ eventFormat['EventDimension'] for eventFormat in event['ChildEvents'] for event in jsonMovieFormats['BookMyShow']['Event'] if event['EventTitle'] == movieName ] if availableFormats: print("The available format(s) : " + (", ".join(availableFormats))) print("Movie is not available in requested " + self.format + " format, will retry...") return False else: print("Movie tickets aren't available yet, retrying...") return False else: print("Movie tickets aren't available yet, retrying...") return False
if '' in tmp: tmp.remove('') a.append(len(tmp)) sentence1 += tmp for t in sentence1: if t == '': continue cnt, pre = 0, False for i in t: if i.lower() in ['a', 'e', 'i', 'o', 'u'] and pre is False: pre = True cnt += 1 else: pre = False b.append(cnt) answer = 0.39*(sum(a)/len(a))+11.8*(sum(b)/len(b))-15.59 return round(answer, 2) # 다른 사람의 풀이 from re import compile as reCompile SENTENCE = reCompile(r'[.!?]') SYLLABLE = reCompile(r'(?i)[aeiou]+') def count(string, pattern): return len(pattern.findall(string)) def flesch_kincaid1(text): nWords = text.count(' ') + 1 return round(0.39 * nWords / count(text, SENTENCE) + 11.8 * count(text, SYLLABLE) / nWords - 15.59, 2) # print(flesch_kincaid("The turtle is leaving."), 3.67) # print(flesch_kincaid("A good book is hard to find."), -1.06) # print(flesch_kincaid("To be or not to be. That is the question."), -0.66) print(flesch_kincaid("Oh no! The lemming is falling."), 1.31)
def type_regex(arg): try: return reCompile(arg, RE_IGNORECASE) except Exception, e: raise ArgumentTypeError("invalid regex '%s': %s" % (arg, e))
class CharacterCSVable(CSVdumpable): CSV_FIELDS = ('rID', 'shortName', 'isManni', 'dogan', 'kaTet', 'isDead', 'hasMusic') KEY_FUNCTION = lambda character: character.shortName JOINRPG_FIELDS = dict( (('shortName', u'Имя на браслете'), ('isManni', u'Мэнни?'), ('dogan', u'Внутренний Доган'), ('kaTet', u'Связи ка-тета'), ('isDead', u'Мёртв?'), ('hasMusic', u'Музыка прислана?'))) DOGAN = OrderedDict( ((u'Служба Алому Королю', -2), (u'Алое Колебание', -1), (u'Нейтралитет', 0), (u'Белое Колебание', 1), (u'Путь Белизны', 2))) NO = 0 UNCHECKED = 1 CHECKED = 2 HAS_MUSIC = OrderedDict((('', NO), (u'да, не проверена', UNCHECKED), (u'да, проверена', CHECKED))) SEPARATORS = reCompile('[,; ]*') KA_TET_SEP = ':' INSTANCES = OrderedDict() TITLE = 'Characters' HEADER_TITLE = '''# Characters table for ArmLet initialization. # # Generated, updated from JoinRPG and used by CharacterProcessor.py # to track persistent unique character Reason IDs.''' RIDS = set() SHORT_NAMES = set() def getKaTet(self): return set(self.kaTet.split(self.KA_TET_SEP)) if self.kaTet else set() def setKaTet(self, kaCharacters): self.kaTet = self.KA_TET_SEP.join(sorted(kaCharacters)) def processNames(self): """Process and validate shortName.""" self.shortName = (self.shortName or '').strip() if not self.shortName: raise CharacterError("Character short name is empty") try: self.shortName = self.shortName.encode('ascii') except UnicodeError: assert False, "Character short name is not ASCII: %r" % self.shortName assert self.shortName.isalnum( ), "Character short name is not alphanumeric: %s" % self.shortName assert self.shortName[:1].isupper( ), "Character short name doesn't start with a capital letter: %s" % self.shortName def validate(self): """Validate fields other than shortName.""" assert self.isManni in (0, 1), "Bas isManni value: %d" % self.isManni assert self.dogan in self.DOGAN.itervalues( ), "Bad dogan value: %s, expected %s" % (self.dogan, '/'.join( str(v) for v in self.DOGAN.itervalues())) assert not self.kaTet or all( name.isalpha() for name in self.getKaTet()), "Bad ka-tet value: %s" % self.kaTet assert self.isDead in (0, 1), "Bas isDead value: %d" % self.isDead assert self.hasMusic in self.HAS_MUSIC.itervalues( ), "Bad hasMusic value: %s, expected %s" % (self.dogan, '/'.join( str(v) for v in self.HAS_MUSIC.itervalues())) def validateLinks(self): assert self.rID not in self.RIDS, "Duplicate character ID %s" % self.rID self.RIDS.add(self.rID) assert self.shortName.lower( ) not in self.SHORT_NAMES, "Duplicate character short name %r" % self.shortName self.SHORT_NAMES.add(self.shortName.lower()) kaTet = self.getKaTet() if kaTet: assert not kaTet or self.shortName in kaTet, "Character is not in one's own ka-tet: %s" % self.shortName assert len( kaTet ) > 1, "Character is the only member in one's ka-tet: %s" % self.shortName for kaTetName in kaTet: assert kaTetName in self.INSTANCES, "Unknown ka-tet member of %s: %s" % ( self.shortName, kaTetName) assert kaTet == self.INSTANCES[kaTetName].getKaTet( ), "Ka-tets for %s and %s are different" % (self.shortName, kaTetName) def integrate(self): """Add the character to the list of characters.""" assert CHARACTER_ID_START <= self.rID <= CHARACTER_ID_END, "Bad character ID value: %d" % self.rID self.INSTANCES[self.shortName] = self def processFromCharactersCSV(self): """Process the object after it was loaded from CSV file.""" self.processNames() self.rID = int(self.rID) self.isManni = int(self.isManni) self.dogan = int(self.dogan) self.isDead = int(self.isDead) self.hasMusic = int(self.hasMusic) self.validate() self.integrate() def fromJoinRPG(self, jCharacter): """Construct the object from informated loaded from JoinRPG.""" for field in self._getFields(): try: setattr(self, field, getattr(jCharacter, field)) except AttributeError: try: setattr(self, field, jCharacter.fieldValues[self.JOINRPG_FIELDS[field]]) except KeyError: pass self.processNames() self.rID = None self.isManni = int(bool((self.isManni or '').strip())) self.isDead = int(bool((self.isDead or '').strip())) try: self.dogan = self.DOGAN[self.dogan.strip()] # pylint: disable=E1101 except KeyError: assert False, "%s: unknown dogan setting: %r" % (self.shortName, self.dogan) self.setKaTet( name for name in (name.strip() for name in self.SEPARATORS.split(self.kaTet or '')) if name) try: self.hasMusic = self.HAS_MUSIC[self.hasMusic or ''] except KeyError: assert False, "%s: unknown hasMusic setting: %r" % (self.shortName, self.hasMusic) self.validate() return self @classmethod def validateAllCharacters(cls): """Check validity of the whole set of characters.""" assert len(cls.INSTANCES) <= len( CHARACTER_IDS), "Too many characters in %s file: %d" % ( CHARACTERS_CSV, len(cls.INSTANCES)) cls.INSTANCES = OrderedDict( sorted(cls.INSTANCES.iteritems(), key=lambda (_shortName, character): character.rID)) assert tuple( character.rID for character in cls.INSTANCES.itervalues()) == tuple( xrange(CHARACTER_ID_START, CHARACTER_ID_START + len(cls.INSTANCES))), "Damaged rIDs in %s file: %s" % ( CHARACTERS_CSV, tuple(character.rID for character in cls.INSTANCES.itervalues())) cls.RIDS.clear() cls.SHORT_NAMES.clear() for character in cls.INSTANCES.itervalues(): character.validateLinks() for character in cls.INSTANCES.itervalues(): character.kaTetIDs = ','.join( sorted( str(cls.INSTANCES[k].rID) for k in character.getKaTet() if k != character.shortName)) @classmethod def updateFromJoinRPG(cls): """Update loaded characters set with data from a JoinRPG database.""" print "Fetching data from JoinRPG.ru..." try: jCharacters = getAllCharacters(GAME_ID, cacheData=True, cacheAuth=True) print "Loaded %d character entities" % len(jCharacters) nLoaded = nChanged = nAdded = nSkipped = 0 for jCharacter in jCharacters: try: character = CharacterCSVable().fromJoinRPG(jCharacter) except CharacterError, e: nSkipped += 1 continue nLoaded += 1 oldCharacter = cls.INSTANCES.get(character.shortName) if oldCharacter: character.rID = oldCharacter.rID # makes comparison work if character == oldCharacter: continue nChanged += 1 else: # new character character.rID = max( c.rID for c in cls.INSTANCES.itervalues() ) + 1 if cls.INSTANCES else CHARACTER_ID_START assert character.rID in CHARACTER_IDS, "Character ID range is full, can't add new character: %d" % character.rID nAdded += 1 character.integrate() cls.validateAllCharacters() if nSkipped: print "Skipped %d characters" % nSkipped if nLoaded: print "Loaded %d valid characters" % nLoaded else: print "No valid characters found" if nAdded or nChanged: if nAdded: print "Added %d characters" % nAdded if nChanged: print "Changed %d characters" % nChanged print "Updating %s..." % CHARACTERS_CSV cls.dumpCSV() print "Saved characters: %d" % len(cls.INSTANCES) else: print "No changes detected" except Exception, e: print format_exc() print "ERROR fetching data: %s, using cached version" % unicode(e)
#!/usr/bin/python3 from itertools import chain from re import compile as reCompile from subprocess import Popen, PIPE, STDOUT from sys import exit as sysExit from typing import Callable, Dict, List, Optional, Sequence LIST_PACKAGE_PATTERN = reCompile(r'(?m)^(?P<package>[^/]+)/(?P<date>[^ ]+) (?P<version>[^ ]+) (?P<arch>[^ ]+) (?P<status>\[installed,local\])$') PURGE_PACKAGE_PATTERN = reCompile(r'(?ms).*The following packages will be REMOVED:\n(?P<packages>(?: [^\n]*\n)+)') PACKAGE_PATTERN = reCompile(r'[^\s*]+') PURGE_EXCLUDE_PATTERN = reCompile(r'Note, selecting|is not installed, so not removed') def purgeFilter(line: str) -> Optional[str]: if line.endswith(' disk space will be freed.\n'): return line + 'Do you want to continue? [Y/n] ' # Add the question that gets suppressed by subprocess buffering return line def runProcess(args: Sequence[str], lineFilter: Optional[Callable[[str], Optional[str]]] = None, printOut: bool = True, expectedReturnCode: Optional[int] = 0) -> str: print('$', ' '.join(args)) subProcess = Popen(args, stdout = PIPE, stderr = STDOUT, bufsize = 0) if lineFilter: output: List[str] = [] assert subProcess.stdout is not None for byteLine in subProcess.stdout: line = lineFilter(byteLine.decode()) if line is None: continue output.append(line)
SOURCE_DIR = 'src' ARMLET_DIR = 'armlet' MUSIC_DIR = 'personal' COMMON_MUSIC_DIR = 'common' ERROR_DIR = 'errors' OTHER_DIR = 'other' COMMON_DIR = '_COMMON' INI_FILE = 'settings.ini' INI_CONTENT = open('settings_ini.tpl').read().replace('\r\n', '\n').replace( '\n', '\r\n') CHARACTER_CSV_PATTERN = reCompile(r'character.*\.csv') SEPARATOR = '-' NEW_FORMAT = 'wav' MAX_FILE_NAME = 64 NEW_EXTENSION = '.%s' % NEW_FORMAT DEFAULT_TARGET_DIR = 'processed' MUSIC_MARK = 'DTAH_Music_Here' RESULT_MARKS = {True: 'music_errors', False: 'music_ok'}
def type_regex(arg): try: return reCompile(arg, RE_IGNORECASE) except Exception as e: raise ArgumentTypeError("invalid regex '%s': %s" % (arg, e))
import re def to_camel_case(str): return re.sub(r'(_|-)([a-zA-Z])', upper_repl , str) def upper_repl(match): return "" + match.group(2).upper() # no regex def to_camel_case(text): words = text.replace('_', '-').split('-') return words[0] + ''.join([x.title() for x in words[1:]]) # other regex from re import compile as reCompile PATTERN = reCompile(r'(?i)[-_]([a-z])') def to_camel_case(text): return PATTERN.sub(lambda m: m.group(1).upper(), text)
from copy import deepcopy from re import compile as reCompile from tf import eval as tfeval # type: ignore from typing import Mapping, NamedTuple, Optional, Sequence, Set from spells import DamType from tfutils import tfprint from utils import strtoi MAGE = reCompile("Mage \((?P<subguilds>.+)\)") MAGE_TYPES = reCompile("^(?P<type>.+?)(?: \[(?P<lvl>\d+)\])?$") GUILD_WITH_LEVELS = reCompile( "(?P<guild>.+) \((?P<lvl>\d+)(?:/(?P<maxlvl>\d+))?\)") GUILD_ORDER = [ "Disciple", # magical "Riftwalker", "Channellers", "Psionicist", "Mage", "Asphyxiation", "Acid", "Cold", "Electricity", "Fire", "Magical", "Poison", "Inner circle", # civilized "Merchant",
#!/usr/bin/python3 from re import compile as reCompile from subprocess import Popen, PIPE, STDOUT from sys import exit as sysExit from typing import Callable, List, Optional, Sequence, Tuple VERSION_SPLIT_PATTERN = reCompile(r'[.-]') VERSION_PATTERN_STR = r'(?P<version>\d+\.\d+\.\d+-\d+)(?:-(?P<variation>[a-z]+))?' KERNEL_PATTERN = reCompile( rf'(?P<line>[a-z][a-z]\s+linux-[a-z]+-(?:[a-z]+-)?{VERSION_PATTERN_STR}\s+.*)' ) UNAME_R_PATTERN = reCompile(VERSION_PATTERN_STR) PURGE_EXCLUDE_PATTERN = reCompile( r'Note, selecting|is not installed, so not removed') PURGE_FILTER_PATTERN = reCompile(r'.* disk space will be (?:freed|used).\n$') def versionTuple(version: str) -> Tuple[int, ...]: return tuple(int(v) for v in VERSION_SPLIT_PATTERN.split(version)) def purgeFilter(line: str) -> Optional[str]: if PURGE_EXCLUDE_PATTERN.search(line): return None if PURGE_FILTER_PATTERN.match(line): return line + 'Do you want to continue? [Y/n] ' # Add the question that gets suppressed by subprocess buffering
dest.append(tuple(['email'] + values)) parser = ArgumentParser() parser.add_argument('--surname', '--lastname', action = SurnameAction, help = SUPPRESS) parser.add_argument('--guests', type = int, default = 1, help = 'number of guests') parser.add_argument('--children', type = int, default = 0, help = 'number of children') parser.add_argument('--rooms', type = int, default = 1, help = 'number of rooms') group = parser.add_mutually_exclusive_group() group.add_argument('--checkin', type = type_day, metavar = 'YYYY-MM-DD', default = startDay.strftime('%Y-%m-%d'), help = 'check in') group.add_argument('--wednesday', dest = 'checkin', action = 'store_const', const = (startDay - timedelta(1)).strftime('%Y-%m-%d'), help = 'check in on Wednesday') parser.add_argument('--checkout', type = type_day, metavar = 'YYYY-MM-DD', default = (startDay + timedelta(3)).strftime('%Y-%m-%d'), help = 'check out') group = parser.add_mutually_exclusive_group() group.add_argument('--max-distance', type = type_distance, metavar = 'BLOCKS', help = "max hotel distance that triggers an alert (or 'connected' to require skywalk hotels)") group.add_argument('--connected', dest = 'max_distance', action = 'store_const', const = 'connected', help = 'shorthand for --max-distance connected') parser.add_argument('--budget', type = float, metavar = 'PRICE', default = '99999', help = 'max total rate (not counting taxes/fees) that triggers an alert') parser.add_argument('--hotel-regex', type = type_regex, metavar = 'PATTERN', default = reCompile('.*'), help = 'regular expression to match hotel name against') parser.add_argument('--room-regex', type = type_regex, metavar = 'PATTERN', default = reCompile('.*'), help = 'regular expression to match room against') parser.add_argument('--show-all', action = 'store_true', help = 'show all rooms, even if miles away (these rooms never trigger alerts)') group = parser.add_mutually_exclusive_group() group.add_argument('--delay', type = int, default = 1, metavar = 'MINS', help = 'search every MINS minute(s)') group.add_argument('--once', action = 'store_true', help = 'search once and exit') parser.add_argument('--test', action = 'store_true', dest = 'test', help = 'trigger every specified alert and exit') group = parser.add_argument_group('required arguments') # Both of these set 'key'; only one of them is required group.add_argument('--key', action = KeyAction, nargs = 2, metavar = ('KEY', 'AUTH'), help = 'key (see the README for more information)') group.add_argument('--url', action = PasskeyUrlAction, dest = 'key', help = 'passkey URL containing your key') group = parser.add_argument_group('alerts') group.add_argument('--popup', dest = 'alerts', action = 'append_const', const = ('popup',), help = 'show a dialog box') group.add_argument('--cmd', dest = 'alerts', action = 'append', type = lambda arg: ('cmd', arg), metavar = 'CMD', help = 'run the specified command, passing each hotel name as an argument')
# A part of RadioChronicle project # # by Vasily Zakharov ([email protected]) # https://github.com/jolaf/radiochronicle # # Version 0.1 # from datetime import datetime, timedelta from re import compile as reCompile from sys import argv from typing import Sequence from wave import open as waveOpen SECOND = timedelta(seconds=1) FILENAME_PATTERN = reCompile(r'^(?:.*?[/\\])?(RC-.*?).wav$') WAV_FILENAME_FORMAT = 'RC-%Y%m%d-%H%M%S.wav' DISPLAY_TIME_FORMAT = '%a %H:%M:%S' SRT_TIME_FORMAT = '%H:%M:%S,%f' WEEKDAYS = ('ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС') def displayFormat(d: datetime) -> str: """Returns a string representation for a datetime in format `ДД HH:MM:SS`.""" return d.strftime(DISPLAY_TIME_FORMAT.replace('%a', WEEKDAYS[d.weekday()])) def srtFormat(d: datetime) -> str: """Returns a string representation for a datetime in format `HH:MM:SS,fff`.
dest.append(tuple(['email'] + values)) parser = ArgumentParser() parser.add_argument('--surname', '--lastname', action = SurnameAction, help = SUPPRESS) parser.add_argument('--guests', type = int, default = 1, help = 'number of guests') parser.add_argument('--children', type = int, default = 0, help = 'number of children') parser.add_argument('--rooms', type = int, default = 1, help = 'number of rooms') group = parser.add_mutually_exclusive_group() group.add_argument('--checkin', type = type_day, metavar = 'YYYY-MM-DD', default = startDay.strftime('%Y-%m-%d'), help = 'check in') group.add_argument('--wednesday', dest = 'checkin', action = 'store_const', const = (startDay - timedelta(1)).strftime('%Y-%m-%d'), help = 'check in on Wednesday') parser.add_argument('--checkout', type = type_day, metavar = 'YYYY-MM-DD', default = (startDay + timedelta(3)).strftime('%Y-%m-%d'), help = 'check out') group = parser.add_mutually_exclusive_group() group.add_argument('--max-distance', type = type_distance, metavar = 'BLOCKS', help = "max hotel distance that triggers an alert (or 'connected' to require skywalk hotels)") group.add_argument('--connected', dest = 'max_distance', action = 'store_const', const = 'connected', help = 'shorthand for --max-distance connected') parser.add_argument('--budget', type = float, metavar = 'PRICE', default = '99999', help = 'max total rate (not counting taxes/fees) that triggers an alert') parser.add_argument('--hotel-regex', type = type_regex, metavar = 'PATTERN', default = reCompile('.*'), help = 'regular expression to match hotel name against') parser.add_argument('--room-regex', type = type_regex, metavar = 'PATTERN', default = reCompile('.*'), help = 'regular expression to match room against') parser.add_argument('--show-all', action = 'store_true', help = 'show all rooms, even if miles away (these rooms never trigger alerts)') group = parser.add_mutually_exclusive_group() group.add_argument('--delay', type = int, default = 1, metavar = 'MINS', help = 'search every MINS minute(s)') group.add_argument('--once', action = 'store_true', help = 'search once and exit') parser.add_argument('--test', action = 'store_true', dest = 'test', help = 'trigger every specified alert and exit') group = parser.add_argument_group('required arguments') # Both of these set 'key'; only one of them is required group.add_argument('--key', nargs = 2, metavar = ('KEY', 'AUTH'), help = 'key (see the README for more information)') group.add_argument('--url', action = PasskeyUrlAction, dest = 'key', help = 'passkey URL containing your key') group = parser.add_argument_group('alerts') group.add_argument('--popup', dest = 'alerts', action = 'append_const', const = ('popup',), help = 'show a dialog box') group.add_argument('--cmd', dest = 'alerts', action = 'append', type = lambda arg: ('cmd', arg), metavar = 'CMD', help = 'run the specified command, passing each hotel name as an argument')
# TG-UserBot is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # TG-UserBot is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with TG-UserBot. If not, see <https://www.gnu.org/licenses/>. from re import compile as reCompile regexp = reCompile(r"(\d+)(w|d|h|m|s)?") adminregexp = reCompile(r"\d+(?:w|d|h|m|s)?") async def amount_to_secs(amount: tuple) -> int: """Resolves one unit to total seconds. Args: amount (``int``, ``str``): Tuple where str is the unit. Returns: ``int``: Total seconds of the unit on success. Example:
def get_options(args=None): parser = DiscordArgParse() parser.add_argument( '--guests', type=int, default=1, help='number of guests') parser.add_argument( '--children', type=int, default=0, help='number of children') parser.add_argument( '--rooms', type=int, default=1, help='number of rooms') group = parser.add_mutually_exclusive_group() group.add_argument( '--checkin', type=type_day, metavar='YYYY-MM-DD', default=startDay.strftime('%Y-%m-%d'), help='check in') group.add_argument( '--wednesday', dest='checkin', action='store_const', const=(startDay - timedelta(1)).strftime('%Y-%m-%d'), help='check in on Wednesday') parser.add_argument( '--checkout', type=type_day, metavar='YYYY-MM-DD', default=(startDay + timedelta(3)).strftime('%Y-%m-%d'), help='check out') group = parser.add_mutually_exclusive_group() group.add_argument( '--max-distance', type=type_distance, metavar='BLOCKS', help="max hotel distance that triggers an alert (or 'connected' to require skywalk hotels)") group.add_argument( '--connected', dest='max_distance', action='store_const', const='connected', help='shorthand for --max-distance connected') parser.add_argument( '--budget', type=float, metavar='PRICE', default='99999', help='max total rate (not counting taxes/fees) that triggers an alert') parser.add_argument( '--hotel-regex', type=type_regex, metavar='PATTERN', default=reCompile('.*'), help='regular expression to match hotel name against') parser.add_argument( '--room-regex', type=type_regex, metavar='PATTERN', default=reCompile('.*'), help='regular expression to match room against') parser.add_argument( '--show-all', action='store_true', help='show all rooms, even if miles away (these rooms never trigger alerts)') parser.add_argument( '--ssl-insecure', action='store_false', dest='ssl_cert_verify', help=SUPPRESS) group = parser.add_mutually_exclusive_group() group.add_argument( '--delay', type=int, default=1, metavar='MINS', help='search every MINS minute(s)') group.add_argument( '--once', action='store_true', help='search once and exit') parser.add_argument( '--test', action='store_true', dest='test', help='trigger every specified alert and exit') group = parser.add_argument_group('required arguments') group.add_argument( '--key', required=True, help='key (see the README for more information)') group = parser.add_argument_group('alerts') group.add_argument( '--popup', dest='alerts', action='append_const', const=('popup',), help='show a dialog box') group.add_argument( '--cmd', dest='alerts', action='append', type=lambda arg: ('cmd', arg), metavar='CMD', help='run the specified command, passing each hotel name as an argument') group.add_argument( '--browser', dest='alerts', action='append_const', const=('browser',), help='open the Passkey website in the default browser') group.add_argument( '--email', dest='alerts', action=EmailAction, nargs=3, metavar=('HOST', 'FROM', 'TO'), help='send an e-mail') options = parser.parse_args(args) return options
from os import rename, chmod from hashlib import md5 from stat import S_IRUSR, S_IWUSR from re import compile as reCompile USER_RW = S_IRUSR | S_IWUSR def md5Hash(data): return md5(data).hexdigest() def saltedPasswordHasher(salt): return lambda data: md5Hash(data + salt) def simplePasswordTest(passwd): return bool(passwd.strip()) VALIDNAME=reCompile(r'^[\w@\-\.]+$') def usernameTest(username): return bool(VALIDNAME.match(username)) class PasswordFile(object): def __init__(self, filename, hashPassword, passwordTest=simplePasswordTest, usernameTest=usernameTest): self._hashPassword = hashPassword self._passwordTest = passwordTest self._usernameTest = usernameTest self._filename = filename self._users = {}
from stat import S_IRUSR, S_IWUSR from re import compile as reCompile from random import choice from string import digits as _SALT_1, ascii_letters as _SALT_2 from meresco.components.json import JsonDict from warnings import warn USER_RW = S_IRUSR | S_IWUSR def md5Hash(data): return md5(data).hexdigest() def simplePasswordTest(passwd): return bool(passwd.strip()) VALIDNAME=reCompile(r'^[\w@\-\.]+$') def usernameTest(username): return bool(VALIDNAME.match(username)) def randomString(length=5): return ''.join(choice(_SALT_1 + _SALT_2) for i in xrange(length)) class FileStorage(object): def store(self, id_, data): with open(id_+'~', 'w') as f: f.write(data) rename(id_+'~', id_) chmod(id_, USER_RW) def retrieve(self, id_): try: return open(id_).read()
) group.add_argument('--connected', dest='max_distance', action='store_const', const='connected', help='shorthand for --max-distance connected') parser.add_argument( '--budget', type=float, metavar='PRICE', default='99999', help='max total rate (not counting taxes/fees) that triggers an alert') parser.add_argument('--hotel-regex', type=type_regex, metavar='PATTERN', default=reCompile('.*'), help='regular expression to match hotel name against') parser.add_argument('--room-regex', type=type_regex, metavar='PATTERN', default=reCompile('.*'), help='regular expression to match room against') parser.add_argument( '--show-all', action='store_true', help='show all rooms, even if miles away (these rooms never trigger alerts)' ) parser.add_argument('--ssl-insecure', action='store_false', dest='ssl_cert_verify', help=SUPPRESS)
str(text)) if w not in ('', '-', '_')]) if not text: return text if not text[0].isupper(): return camel[:1].lower() + camel[1:] return camel # others def to_camel_case(text): return text[0] + ''.join([w[0].upper() + w[1:] for w in text.replace("_", "-").split("-")])[1:] if text else '' import re def to_camel_case(text): return reduce(lambda p, n: p + n[0].upper() + n[1:], re.split('[-_]', text)) from re import compile as reCompile PATTERN = reCompile(r'(?i)[-_]([a-z])') def to_camel_case(text): return PATTERN.sub(lambda m: m.group(1).upper(), text) import re def to_camel_case(text): words = re.split(r'[_\-]', text) return ''.join([words[0]] + [word.capitalize() for word in words[1:]])
OPCODEs = ['ADD', 'AND', 'JMP', 'JSR', 'JSRR', 'LDI', 'LDR', 'LD', 'LEA', 'NOT', 'RET', 'RTI', 'STI', 'STR', 'ST', 'TRAP'] # Branch opcodes OPCODEs += ['BRnzp', 'BRnz', 'BRnp', 'BRzp', 'BRn', 'BRz', 'BRp', 'BR'] # Trap Alias OPCODEs += ['HALT', 'IN', 'OUT', 'GETC', 'PUTS'] # Real opcodes realOPCODEs = OPCODEs.copy() # Assembler directives OPCODEs += ['.ORIG', '.END', '.FILL', '.BLKW', '.STRINGZ'] # Regex pattern for matching OPCODE opCodePattern = '|'.join(OPCODEs).replace('.', '\\.') opCodePatternEx = ' |'.join(OPCODEs).replace('.', '\\.') # Regex pattern for parse assembly line. pattern = reCompile(f'(^(?!{opCodePatternEx})(?P<LABEL>\S+))?\s*(?P<OPCODE>{opCodePattern})?' + '(\s+(?P<OPERANDS>.*))?') def parseNumber(num: Str) -> Int: assert num[0] in ['x', '#', '-'] or num[0].isdigit() if num.startswith('x'): return int(num[1:], 16) elif num.startswith('0x'): return int(num[2:], 16) elif num.startswith('#'): return int(num[1:]) elif num[0].isdigit() or num[0] == '-': return int(num) def parseOperands(operand: Str, symbolTable, lineNo) -> List[Int]:
# TG-UserBot is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # TG-UserBot is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with TG-UserBot. If not, see <https://www.gnu.org/licenses/>. from re import compile as reCompile regexp = reCompile(r"(\d+)(w|d|h|m|s)?") async def amount_to_secs(amount: tuple) -> int: """Resolves one unit to total seconds. Args: amount (``int``, ``str``): Tuple where str is the unit. Returns: ``int``: Total seconds of the unit on success. Example: >>> await amount_to_secs(("1", "m"))