code
stringlengths
52
7.75k
docs
stringlengths
1
5.85k
def is_waiting_for_input(self): return self.waiting_for and \ not isinstance(self.waiting_for, forking.SwitchOnValue) and \ not is_base_type(self.waiting_for)
could make one step further :return:
def alias(self): if self._alias is None: if self.name in self.aliases_fix: self._alias = self.aliases_fix[self.name] else: self._alias = self.name.lower()\ .replace(' ', '-')\ .re...
If the _alias cache is None, just build the alias from the item name.
def load_configs(self, conf_file): with open(conf_file) as stream: lines = itertools.chain(("[global]",), stream) self._config.read_file(lines) return self._config['global']
Assumes that the config file does not have any sections, so throw it all in global
def remove_quotes(self, configs): for key in configs: value = configs[key] if value[0] == "'" and value[-1] == "'": configs[key] = value[1:-1] return configs
Because some values are wraped in single quotes
def multikey_sort(items, columns): comparers = [ ((itemgetter(col[1:].strip()), -1) if col.startswith('-') else (itemgetter(col.strip()), 1)) for col in columns ] def cmp(a, b): return (a > b) - (a < b) def comparer(left, right): comparer_iter = ( cmp(f...
Source: https://stackoverflow.com/questions/1143671/python-sorting-list-of-dictionaries-by-multiple-keys
def sanitize(string): replace_chars = [ ['\\', '-'], [':', '-'], ['/', '-'], ['?', ''], ['<', ''], ['>', ''], ['`', '`'], ['|', '-'], ['*', '`'], ['"', '\''], ['.', ''], ['&', 'and'] ] for ch in replace_chars: string = string.replace(ch[0], ch[1]) return stri...
Catch and replace invalid path chars [replace, with]
def chunks_of(max_chunk_size, list_to_chunk): for i in range(0, len(list_to_chunk), max_chunk_size): yield list_to_chunk[i:i + max_chunk_size]
Yields the list with a max size of max_chunk_size
def split_into(max_num_chunks, list_to_chunk): max_chunk_size = math.ceil(len(list_to_chunk) / max_num_chunks) return chunks_of(max_chunk_size, list_to_chunk)
Yields the list with a max total size of max_num_chunks
def norm_path(path): # path = os.path.normcase(path) path = os.path.expanduser(path) path = os.path.expandvars(path) path = os.path.normpath(path) return path
:return: Proper path for os with vars expanded out
def create_hashed_path(base_path, name, depth=2): if depth > 16: logger.warning("depth cannot be greater then 16, setting to 16") depth = 16 name_hash = hashlib.md5(str(name).encode('utf-8')).hexdigest() if base_path.endswith(os.path.sep): save_path = base_path else: ...
Create a directory structure using the hashed filename :return: string of the path to save to not including filename/ext
def create_path(path, is_dir=False): path = norm_path(path) path_check = path if not is_dir: path_check = os.path.dirname(path) does_path_exists = os.path.exists(path_check) if does_path_exists: return path try: os.makedirs(path_check) except OSError: ...
Check if path exists, if not create it :param path: path or file to create directory for :param is_dir: pass True if we are passing in a directory, default = False :return: os safe path from `path`
def rate_limited(num_calls=1, every=1.0): frequency = abs(every) / float(num_calls) def decorator(func): """ Extend the behaviour of the following function, forwarding method invocations if the time window hes elapsed. Arguments: func (function): The func...
Source: https://github.com/tomasbasham/ratelimit/tree/0ca5a616fa6d184fa180b9ad0b6fd0cf54c46936 Need to make a few changes that included having num_calls be a float Prevent a method from being called if it was previously called before a time widows has elapsed. Keyword Arguments: num_calls ...
def rate_limited_old(max_per_second): lock = threading.Lock() min_interval = 1.0 / max_per_second def decorate(func): last_time_called = time.perf_counter() @wraps(func) def rate_limited_function(*args, **kwargs): lock.acquire() nonlocal last_time_calle...
Source: https://gist.github.com/gregburek/1441055
def timeit(stat_tracker_func, name): def _timeit(func): def wrapper(*args, **kw): start_time = time.time() result = func(*args, **kw) stop_time = time.time() stat_tracker_func(name, stop_time - start_time) return result return wrapper...
Pass in a function and the name of the stat Will time the function that this is a decorator to and send the `name` as well as the value (in seconds) to `stat_tracker_func` `stat_tracker_func` can be used to either print out the data or save it
def get_proxy_parts(proxy): proxy_parts = {'schema': None, 'user': None, 'password': None, 'host': None, 'port': None, } # Find parts results = re.match(proxy_parts_pattern, proxy) if results: mat...
Take a proxy url and break it up to its parts
def remove_html_tag(input_str='', tag=None): result = input_str if tag is not None: pattern = re.compile('<{tag}[\s\S]+?/{tag}>'.format(tag=tag)) result = re.sub(pattern, '', str(input_str)) return result
Returns a string with the html tag and all its contents from a string
def asodict(self, handlepoints=True, reportpoints=True): out = odict() if handlepoints: for hp in self.handlepoints: out[hp.hpoint] = hp.trace if reportpoints: for rp in self.reportpoints: if not (rp.rpoint in out): ...
Returns an ordered dictionary of handle/report points
def asodict(self, freports=True, handlepoints=True, reportpoints=True): out = odict() if freports: for fr in self.freports: out[fr.num] = {'tstamp' : fr.tstamp, 'report' : fr.asodict(handlepoints, reportpoints)} if h...
Returns an ordered dictionary of feed, and handle/report points
def ip_between(ip, start, finish): if is_IPv4Address(ip) and is_IPv4Address(start) and is_IPv4Address(finish): return IPAddress(ip) in IPRange(start, finish) else: return False
Checks to see if IP is between start and finish
def is_rfc1918(ip): if ip_between(ip, "10.0.0.0", "10.255.255.255"): return True elif ip_between(ip, "172.16.0.0", "172.31.255.255"): return True elif ip_between(ip, "192.168.0.0", "192.168.255.255"): return True else: return False
Checks to see if an IP address is used for local communications within a private network as specified by RFC 1918
def is_reserved(ip): if ip_between(ip, "0.0.0.0", "0.255.255.255"): return True elif ip_between(ip, "10.0.0.0", "10.255.255.255"): return True elif ip_between(ip, "100.64.0.0", "100.127.255.255"): return True elif ip_between(ip, "127.0.0.0", "127.255.255.255"): retur...
Checks to see if an IP address is reserved for special purposes. This includes all of the RFC 1918 addresses as well as other blocks that are reserved by IETF, and IANA for various reasons. https://en.wikipedia.org/wiki/Reserved_IP_addresses
def is_hash(fhash): # Intentionally doing if/else statement for ease of testing and reading if re.match(re_md5, fhash): return True elif re.match(re_sha1, fhash): return True elif re.match(re_sha256, fhash): return True elif re.match(re_sha512, fhash): return Tr...
Returns true for valid hashes, false for invalid.
def ip_to_geojson(ipaddress, name="Point"): geo = ip_to_geo(ipaddress) point = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "name": name }, "geometry": { ...
Generate GeoJSON for given IP address
def ips_to_geojson(ipaddresses): features = [] for ipaddress in ipaddresses: geo = gi.record_by_addr(ipaddress) features.append({ "type": "Feature", "properties": { "name": ipaddress }, "geometry": { "type": ...
Generate GeoJSON for given IP address
def reverse_dns_sna(ipaddress): r = requests.get("http://api.statdns.com/x/%s" % ipaddress) if r.status_code == 200: names = [] for item in r.json()['answer']: name = str(item['rdata']).strip(".") names.append(name) return names elif r.json()['code'] ...
Returns a list of the dns names that point to a given ipaddress using StatDNS API
def vt_ip_check(ip, vt_api): if not is_IPv4Address(ip): return None url = 'https://www.virustotal.com/vtapi/v2/ip-address/report' parameters = {'ip': ip, 'apikey': vt_api} response = requests.get(url, params=parameters) try: return response.json() except ValueError: ...
Checks VirusTotal for occurrences of an IP address
def vt_name_check(domain, vt_api): if not is_fqdn(domain): return None url = 'https://www.virustotal.com/vtapi/v2/domain/report' parameters = {'domain': domain, 'apikey': vt_api} response = requests.get(url, params=parameters) try: return response.json() except ValueError: ...
Checks VirusTotal for occurrences of a domain name
def vt_hash_check(fhash, vt_api): if not is_hash(fhash): return None url = 'https://www.virustotal.com/vtapi/v2/file/report' parameters = {'resource': fhash, 'apikey': vt_api} response = requests.get(url, params=parameters) try: return response.json() except ValueError: ...
Checks VirusTotal for occurrences of a file hash
def ipinfo_ip_check(ip): if not is_IPv4Address(ip): return None response = requests.get('http://ipinfo.io/%s/json' % ip) return response.json()
Checks ipinfo.io for basic WHOIS-type data on an IP address
def ipvoid_check(ip): if not is_IPv4Address(ip): return None return_dict = {} headers = {'User-Agent': useragent} url = 'http://ipvoid.com/scan/%s/' % ip response = requests.get(url, headers=headers) data = BeautifulSoup(response.text) if data.findAll('span', attrs={'class': 'l...
Checks IPVoid.com for info on an IP address
def urlvoid_check(name, api_key): if not is_fqdn(name): return None url = 'http://api.urlvoid.com/api1000/{key}/host/{name}'.format(key=api_key, name=name) response = requests.get(url) tree = ET.fromstring(response.text) if tree.find('./detections/engines'): return [e.text for ...
Checks URLVoid.com for info on a domain
def urlvoid_ip_check(ip): if not is_IPv4Address(ip): return None return_dict = {} headers = {'User-Agent': useragent} url = 'http://urlvoid.com/ip/%s/' % ip response = requests.get(url, headers=headers) data = BeautifulSoup(response.text) h1 = data.findAll('h1')[0].text if ...
Checks URLVoid.com for info on an IP address
def dshield_ip_check(ip): if not is_IPv4Address(ip): return None headers = {'User-Agent': useragent} url = 'https://isc.sans.edu/api/ip/' response = requests.get('{0}{1}?json'.format(url, ip), headers=headers) return response.json()
Checks dshield for info on an IP address
def validate(version, comparison): # match any if not comparison: return True # loop through all available opts = comparison.split(',') expr = re.compile('(==|!=|<=|>=|<|>)(.*)') for opt in opts: try: test, value = expr.match(opt.strip()).groups() ex...
Returns whether or not the version for this plugin satisfies the inputted expression. The expression will follow the dependency declaration rules associated with setuptools in Python. More information can be found at [https://pythonhosted.org/setuptools/setuptools.html#declaring-dependencies] ...
def _full_kind(details): kind = details[u"kind"] if details.get(u"group") is not None: kind += u"." + details[u"group"] return kind
Determine the full kind (including a group if applicable) for some failure details. :see: ``v1.Status.details``
def RemoveEmptyDirectoryTree(path, silent = False, recursion = 0): if not silent and recursion is 0: goodlogging.Log.Info("UTIL", "Starting removal of empty directory tree at: {0}".format(path)) try: os.rmdir(path) except OSError: if not silent: goodlogging.Log.Info("UTIL", "Removal of empty ...
Delete tree of empty directories. Parameters ---------- path : string Path to root of directory tree. silent : boolean [optional: default = False] Turn off log output. recursion : int [optional: default = 0] Indicates level of recursion.
def CheckPathExists(path): i = 0 root, ext = os.path.splitext(path) while os.path.exists(path): i = i + 1 goodlogging.Log.Info("UTIL", "Path {0} already exists".format(path)) path = "{0}_{1}".format(root, i) + ext return path
Check if path exists, if it does add number to path (incrementing until a unique path is found). Parameters ---------- path : string Path of directory to try. Returns ---------- string Path of unique directory.
def StripSpecialCharacters(string, stripAll = False): goodlogging.Log.Info("UTIL", "Stripping any special characters from {0}".format(string), verbosity=goodlogging.Verbosity.MINIMAL) string = string.strip() string = re.sub('[&]', 'and', string) string = re.sub(r'[@#$%^&*{};:,/<>?\\|`~=+±§£]', '', string) ...
Strips special characters, duplicate spaces and post/pre-ceeding spaces. Strip on single spaces, periods, hyphens and underscores is conditional on if stripAll is set Parameters ---------- string : string String to strip special characters from. stripAll : boolean [optional: default = False] ...
def ValidUserResponse(response, validList): if response in validList: return response else: prompt = "Unknown response given - please reenter one of [{0}]: ".format('/'.join(validList)) response = goodlogging.Log.Input("DM", prompt) return ValidUserResponse(response, validList)
Check if user response is in a list of valid entires. If an invalid response is given re-prompt user to enter one of the valid options. Do not proceed until a valid entry is given. Parameters ---------- response : string Response string to check. validList : list A list of valid response...
def GetBestMatch(target, matchList): bestMatchList = [] if len(matchList) > 0: ratioMatch = [] for item in matchList: ratioMatch.append(GetBestStringMatchValue(target, item)) maxRatio = max(ratioMatch) if maxRatio > 0.8: matchIndexList = [i for i, j in enumerate(ratioMatch) if j == ...
Finds the elements of matchList which best match the target string. Note that this searches substrings so "abc" will have a 100% match in both "this is the abc", "abcde" and "abc". The return from this function is a list of potention matches which shared the same highest match score. If any exact match is fou...
def GetBestStringMatchValue(string1, string2): # Ignore case string1 = string1.lower() string2 = string2.lower() # Ignore non-alphanumeric characters string1 = ''.join(i for i in string1 if i.isalnum()) string2 = ''.join(i for i in string2 if i.isalnum()) # Finding best match value between string1 an...
Return the value of the highest matching substrings between two strings. Parameters ---------- string1 : string First string. string2 : string Second string. Returns ---------- int Integer value representing the best match found between string1 and string2.
def WebLookup(url, urlQuery=None, utf8=True): goodlogging.Log.Info("UTIL", "Looking up info from URL:{0} with QUERY:{1})".format(url, urlQuery), verbosity=goodlogging.Verbosity.MINIMAL) response = requests.get(url, params=urlQuery) goodlogging.Log.Info("UTIL", "Full url: {0}".format(response.url), verbosity=g...
Look up webpage at given url with optional query string Parameters ---------- url : string Web url. urlQuery : dictionary [optional: default = None] Parameter to be passed to GET method of requests module utf8 : boolean [optional: default = True] Set response encoding Returns -...
def ArchiveProcessedFile(filePath, archiveDir): targetDir = os.path.join(os.path.dirname(filePath), archiveDir) goodlogging.Log.Info("UTIL", "Moving file to archive directory:") goodlogging.Log.IncreaseIndent() goodlogging.Log.Info("UTIL", "FROM: {0}".format(filePath)) goodlogging.Log.Info("UTIL", "TO: {...
Move file from given file path to archive directory. Note the archive directory is relative to the file path directory. Parameters ---------- filePath : string File path archiveDir : string Name of archive directory
def send(self,text): #print text self.s.write(text) time.sleep(0.001*len(text))
Send a string to the PiLite, can be simple text or a $$$ command
def send_wait(self,text): self.send(text) time.sleep(len(text)*PiLite.COLS_PER_CHAR*self.speed/1000.0)
Send a string to the PiLite, sleep until the message has been displayed (based on an estimate of the speed of the display. Due to the font not being monotype, this will wait too long in most cases
def set_speed(self,speed): self.speed=speed self.send_cmd("SPEED"+str(speed))
Set the display speed. The parameters is the number of milliseconds between each column scrolling off the display
def set_fb_pic(self,pattern): pattern=''.join(pattern.split()) # Remove whitespace pattern=pattern.replace('*','1') pattern=pattern.replace('.','0') fb='' for x in range(14): for y in range(9): fb+=pattern[y*14+x] self.set_fb(fb)
Set the "frame buffer". This allows "nice" string to be sent, because it first removes all whitespace, then transposes so that the X and Y axes are swapped, so what is seen in the file matches what will be seen on the screen. Also '.' and '*' can be used in place of 0 and 1.
def set_fb_random(self): pattern=''.join([random.choice(['0','1']) for i in xrange(14*9)]) self.set_fb(pattern)
Set the "frame buffer" to a random pattern
def set_pixel(self,x,y,state): self.send_cmd("P"+str(x+1)+","+str(y+1)+","+state)
Set pixel at "x,y" to "state" where state can be one of "ON", "OFF" or "TOGGLE"
def display_char(self,x,y,char): self.send_cmd("T"+str(x+1)+","+str(y+1)+","+char)
Display character "char" with its top left at "x,y"
def cli(ctx, amount, index, stage): ctx.obj.say_green('Starting Streaming Pipe') res_pull = ctx.invoke(pull, amount=amount, index=index, stage=stage) res_tra = False if res_pull: # amount to transform can be less (or more) res_tra = ctx.invoke( transform, amount=amount, ...
Pull, Transform, Push,streaming inside a pipe(experimental).
def camelHump(text): # make sure the first letter is upper case output = ''.join([word[0].upper() + word[1:] for word in words(text)]) if output: output = output[0].lower() + output[1:] return output
Converts the inputted text to camel humps by joining all capital letters toegether (The Quick, Brown, Fox.Tail -> TheQuickBrownFoxTail) :param: text <str> text to be changed :return: <str> :usage: |import projex.text |print projex.text.camel...
def capitalize(text): text = nativestring(text) if EXPR_CAPITALS.match(text): return text return text.capitalize()
Capitalizes the word using the normal string capitalization method, however if the word contains only capital letters and numbers, then it will not be affected. :param text | <str> :return <str>
def classname(text): if not text: return text text = camelHump(text) return text[0].upper() + text[1:]
Converts the inputted text to the standard classname format (camel humped with a capital letter to start. :return <str>
def encoded(text, encoding=DEFAULT_ENCODING): # already a string item if type(text) == bytes_type: return text elif type(text) != unicode_type: # convert a QString value if type(text).__name__ == 'QString': if encoding == 'utf-8': return unicode_type...
Encodes the inputted unicode/string variable with the given encoding type. :param text | <variant> encoding | <str> :return <str>
def decoded(text, encoding=DEFAULT_ENCODING): # unicode has already been decoded if type(text) == unicode_type: return text elif type(text) != bytes_type: try: return unicode_type(text) except StandardError: try: text = bytes_type(text) ...
Attempts to decode the inputted unicode/string variable using the given encoding type. If no encoding is provided, then it will attempt to use one of the ones available from the default list. :param text | <variant> encoding | <str> || None :return <unicode>
def nativestring(val, encodings=None): # if it is already a native python string, don't do anything if type(val) in (bytes_type, unicode_type): return val # otherwise, attempt to return a decoded value try: return unicode_type(val) except StandardError: pass try: ...
Converts the inputted value to a native python string-type format. :param val | <variant> encodings | (<str>, ..) || None :sa decoded :return <unicode> || <str>
def joinWords(text, separator=''): text = nativestring(text) output = separator.join(words(text.strip(separator))) # no need to check for bookended items when its an empty string if not separator: return output # look for beginning characters begin = re.match('^\%s+' % separator, ...
Collects all the words from a text and joins them together with the inputted separator. :sa [[#words]] :param text <str> :param separator <str> :return <str> :usage |import projex |print projex.joinWords('This::is.a testTest...
def pluralize(word, count=None, format=u'{word}'): if count == 1: return word elif count is not None: return format.format(word=word, count=count) word = nativestring(word) if inflect_engine: return format.format(word=inflect_engine.plural(word)) all_upper = EXPR_UPPER...
Converts the inputted word to the plural form of it. This method works best if you use the inflect module, as it will just pass along the request to inflect.plural If you do not have that module, then a simpler and less impressive pluralization technique will be used. :sa https://pypi.pyt...
def safe_eval(value): if not isinstance(value, (str, unicode)): return value try: return CONSTANT_EVALS[value] except KeyError: try: return ast.literal_eval(value) except StandardError: return value
Converts the inputted text value to a standard python value (if possible). :param value | <str> || <unicode> :return <variant>
def sectioned(text, sections=1): text = nativestring(text) if not text: return '' count = len(text) / max(1, sections) return ' '.join([text[i:i + count] for i in range(0, len(text), count)])
Splits the inputted text up into sections. :param text | <str> sections | <int> :return <str>
def singularize(word): word = toUtf8(word) if inflect_engine: result = inflect_engine.singular_noun(word) if result is False: return word return result # go through the different plural expressions, searching for the # proper replacement if word.endswith('ie...
Converts the inputted word to the single form of it. This method works best if you use the inflect module, as it will just pass along the request to inflect.singular_noun. If you do not have that module, then a simpler and less impressive singularization technique will be used. :sa https...
def stemmed(text): terms = re.split('\s*', toAscii(text)) output = [] for term in terms: # ignore apostrophe's if term.endswith("'s"): stripped_term = term[:-2] else: stripped_term = term single_term = singularize(stripped_term) if term...
Returns a list of simplified and stemmed down terms for the inputted text. This will remove common terms and words from the search and return only the important root terms. This is useful in searching algorithms. :param text | <str> :return [<str>, ..]
def stripHtml(html, joiner=''): stripper = HTMLStripper() stripper.feed(html.replace('<br>', '\n').replace('<br/>', '\n')) return stripper.text(joiner)
Strips out the HTML tags from the inputted text, returning the basic text. This algorightm was found on [http://stackoverflow.com/questions/753052/strip-html-from-strings-in-python StackOverflow]. :param html | <str> :return <str>
def truncate(text, length=50, ellipsis='...'): text = nativestring(text) return text[:length] + (text[length:] and ellipsis)
Returns a truncated version of the inputted text. :param text | <str> length | <int> ellipsis | <str> :return <str>
def toBytes(text, encoding=DEFAULT_ENCODING): if not text: return text if not isinstance(text, bytes_type): text = text.encode(encoding) return text
Converts the inputted text to base string bytes array. :param text | <variant> :return <str> || <bytes> (python3)
def toUnicode(data, encoding=DEFAULT_ENCODING): if isinstance(data, unicode_type): return data if isinstance(data, bytes_type): return unicode_type(data, encoding=encoding) if hasattr(data, '__iter__'): try: dict(data) except TypeError: pass ...
Converts the inputted data to unicode format. :param data | <str> || <unicode> || <iterable> :return <unicode> || <iterable>
def underscore(text, lower=True): out = joinWords(text, '_') if lower: return out.lower() return out
Splits all the words from the inputted text into being separated by underscores :sa [[#joinWords]] :param text <str> :return <str> :usage |import projex.text |print projex.text.underscore('TheQuick, Brown, Fox')
def xmlindent(elem, level=0, spacer=' '): i = "\n" + level * spacer if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + spacer if not elem.tail or not elem.tail.strip(): elem.tail = i for elem in elem: xmlindent(elem, level +...
Indents the inputted XML element based on the given indent level. :param elem | <xml.etree.Element>
def words(text): stext = nativestring(text) if not stext: return [] # first, split all the alphanumeric characters up phrases = EXPR_PHRASE.findall(stext) # second, split all the camel humped words output = [] for phrase in phrases: output += EXPR_WORD.findall(phrase) ...
Extracts a list of words from the inputted text, parsing out non-alphanumeric characters and splitting camel humps to build the list of words :param text <str> :return <str> :usage |import projex.text |print projex.text.words('TheQuick, TheBro...
def to_json(data): return json.dumps(data, default=lambda x: x.__dict__, sort_keys=True, indent=4)
Return data as a JSON string.
def convert_string(string, chars=None): if chars is None: chars = [',', '.', '-', '/', ':', ' '] for ch in chars: if ch in string: string = string.replace(ch, ' ') return string
Remove certain characters from a string.
def convert_time(time): split_time = time.split() try: # Get rid of period in a.m./p.m. am_pm = split_time[1].replace('.', '') time_str = '{0} {1}'.format(split_time[0], am_pm) except IndexError: return time try: time_obj = datetime.strptime(time_str, '%I:%M ...
Convert a time string into 24-hour time.
def convert_month(date, shorten=True, cable=True): month = date.split()[0].lower() if 'sept' in month: shorten = False if cable else True try: if shorten: month = SHORT_MONTHS[MONTHS.index(month)] else: month = MONTHS[SHORT_MONTHS.index(month)] excep...
Replace month by shortening or lengthening it. :param shorten: Set to True to shorten month name. :param cable: Set to True if category is Cable.
def convert_date(date): date = convert_month(date, shorten=False) clean_string = convert_string(date) return datetime.strptime(clean_string, DATE_FMT.replace('-',''))
Convert string to datetime object.
def date_in_range(date1, date2, range): date_obj1 = convert_date(date1) date_obj2 = convert_date(date2) return (date_obj2 - date_obj1).days <= range
Check if two date objects are within a specific range
def inc_date(date_obj, num, date_fmt): return (date_obj + timedelta(days=num)).strftime(date_fmt)
Increment the date by a certain number and return date object. as the specific string format.
def get_soup(url): html = requests.get(url, stream=True, headers=HEADERS) if html.status_code != 404: return BeautifulSoup(html.content, 'html.parser') else: return None
Request the page and return the soup.
def match_list(query_list, string): # Get rid of 'the' word to ease string matching match = False index = 0 string = ' '.join(filter_stopwords(string)) if not isinstance(query_list, list): query_list = [query_list] while index < len(query_list): query = query_list[index] ...
Return True if all words in a word list are in the string. :param query_list: list of words to match :param string: the word or words to be matched against
def filter_stopwords(phrase): if not isinstance(phrase, list): phrase = phrase.split() stopwords = ['the', 'a', 'in', 'to'] return [word.lower() for word in phrase if word.lower() not in stopwords]
Filter out stop words and return as a list of words
def safe_unicode(string): if not PY3: uni = string.replace(u'\u2019', "'") return uni.encode('utf-8') return string
If Python 2, replace non-ascii characters and return encoded string.
def get_strings(soup, tag): tags = soup.find_all(tag) strings = [s.string for s in tags if s.string] return strings
Get all the string children from an html tag.
def _bld_op(self, op, num, **kwargs): kwargs['other'] = num setattr(self, op, {'mtype': pab, 'kwargs': kwargs})
implements pandas an operator
def _bld_pab_generic(self, funcname, **kwargs): margs = {'mtype': pab, 'kwargs': kwargs} setattr(self, funcname, margs)
implements a generic version of an attribute based pandas function
def _bld_pnab_generic(self, funcname, **kwargs): margs = {'mtype': pnab, 'kwargs': kwargs} setattr(self, funcname, margs)
implement's a generic version of a non-attribute based pandas function
def get(self, request, *args, **kwargs): cart = ShoppingCartProxy(request) return JsonResponse(cart.get_products(onlypublic=request.GET.get('onlypublic', True)))
List all products in the shopping cart
def post(self, request, *args, **kwargs): POST = json.loads(request.body.decode('utf-8')) if 'product_pk' in POST and 'quantity' in POST: cart = ShoppingCartProxy(request) cart.add( product_pk=int(POST['product_pk']), quantity=int(POST['q...
Adds new product to the current shopping cart
def dispatch(self, *args, **kwargs): self.__line_pk = kwargs.get('pk', None) return super(LinesUpdateModalBasket, self).dispatch(*args, **kwargs)
if SalesLineBasketOption.objects.filter(line_budget__pk=self.__line_pk).exists(): self.form_class = LineBasketFormPack self.__is_pack = True else: self.__is_pack = False
def get_form(self, form_class=None): # form_kwargs = super(LineBasketUpdateModal, self).get_form_kwargs(*args, **kwargs) form = super(LinesUpdateModalBasket, self).get_form(form_class) initial = form.initial initial['type_tax'] = self.object.product_final.product.tax.pk initial['...
if self.__is_pack: options = [] lang = get_language_database() for option in SalesLineBasketOption.objects.filter(line_budget__pk=self.__line_pk): initial['packs[{}]'.format(option.product_option.pk)] = option.product_final.pk a = { ...
def register_signal(alias: str, signal: pyqtSignal): if SignalDispatcher.signal_alias_exists(alias): raise SignalDispatcherError('Alias "' + alias + '" for signal already exists!') SignalDispatcher.signals[alias] = signal
Used to register signal at the dispatcher. Note that you can not use alias that already exists. :param alias: Alias of the signal. String. :param signal: Signal itself. Usually pyqtSignal instance. :return:
def register_handler(alias: str, handler: callable): if SignalDispatcher.handlers.get(alias) is None: SignalDispatcher.handlers[alias] = [handler] else: SignalDispatcher.handlers.get(alias).append(handler)
Used to register handler at the dispatcher. :param alias: Signal alias to match handler to. :param handler: Handler. Some callable. :return:
def dispatch(): aliases = SignalDispatcher.signals.keys() for alias in aliases: handlers = SignalDispatcher.handlers.get(alias) signal = SignalDispatcher.signals.get(alias) if signal is None or handlers.__len__() == 0: continue ...
This methods runs the wheel. It is used to connect signal with their handlers, based on the aliases. :return:
def signal_alias_exists(alias: str) -> bool: if SignalDispatcher.signals.get(alias): return True return False
Checks if signal alias exists. :param alias: Signal alias. :return:
def handler_alias_exists(alias: str) -> bool: if SignalDispatcher.handlers.get(alias): return True return False
Checks if handler alisa exists. :param alias: Handler alias. :return:
def get_function_data(minion, jid): redis = Redis(connection_pool=redis_pool) data = redis.get('{0}:{1}'.format(minion, jid)) return Response(response=data, status=200, mimetype="application/json")
AJAX access for loading function/job details.
def get_api_publisher(self, social_user): def _post(**kwargs): api = self.get_api(social_user) from pudb import set_trace; set_trace() # api.group.getInfo('uids'='your_group_id', 'fields'='members_count') #response = api.wall.post(**kwargs) r...
and other https://vk.com/dev.php?method=wall.post
def _get_rev(self, fpath): rev = None try: cmd = ["git", "log", "-n1", "--pretty=format:\"%h\"", fpath] rev = Popen(cmd, stdout=PIPE, stderr=PIPE).communicate()[0] except: pass if not rev: try: cmd...
Get an SCM version number. Try svn and git.
def execute_migrations(self, show_traceback=True): all_migrations = get_pending_migrations(self.path, self.databases) if not len(all_migrations): sys.stdout.write("There are no migrations to apply.\n") for db, migrations in all_migrations.iteritems(): ...
Executes all pending migrations across all capable databases
def handle(self, *args, **options): self.do_list = options.get("do_list") self.do_execute = options.get("do_execute") self.do_create = options.get("do_create") self.do_create_all = options.get("do_create_all") self.do_seed = options.get("do_seed") self.load_initi...
Upgrades the database. Executes SQL scripts that haven't already been applied to the database.
def is_git_directory(path='.'): try: dulwich.repo.Repo.discover(path) except dulwich.errors.NotGitRepository: return False return True
Checks if given directory is a git repository :param path: path to check :return: True if it's a git repo and False otherwise