def walk2dict(folder: Path, topdown: bool=True, onerror: object=None, followlinks: bool=False, showhidden: bool=False, strip: bool=False) -> namedtuple: """Perform full walk, gather full path of all files, based on os.walk with multiple output types & extras. Returns a namedtuple 'walk2dict' with multiple output types: - Nested common dictionary represents folder/file structure of folder. - JSON dumps string of the dictionary, uses uJSON if installed. - collections.OrderedDict() of the dictionary. - types.MappingProxyType() inmmutable of the dictionary.""" ret = [] for path, dirs, files in os.walk(folder, topdown=topdown, onerror=onerror, followlinks=followlinks): if not showhidden: dirs = [_ for _ in dirs if not _.startswith(".")] files = [_ for _ in files if not _.startswith(".")] a = {} if strip: p = path.strip(str(folder) + os.sep) else: p = path if len(p.split(os.sep)) == 1: parent = '' if len(p.split(os.sep)) > 1: parent = os.sep.join(p.split(os.sep)[:-1]) if path == str(folder): parent = 'root' a['path'] = p a['fullpath'] = os.path.abspath(path) a['parent'] = parent a['dirs'] = dirs a['files'] = [] for fyle in files: try: # sometimes os.stat(ff) just fails,breaking all the loop. f = {} ff = path + os.sep + fyle (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(ff) f['name'] = fyle f['mode'] = mode f['ino'] = ino f['dev'] = dev f['nlink'] = nlink f['uid'] = uid f['gid'] = gid f['size'] = size f['atime'] = atime f['mtime'] = mtime f['ctime'] = ctime a['files'].append(f) except Exception: pass ret.append(a) dict_f = ret[0] return namedtuple("walk2dict", "dict json OrderedDict frozendict")( dict_f, dumps(dict_f), OrderedDict(dict_f), frozendict(dict_f))
def currencies(self): """Fetches current currency data from openexchangerates.""" url = (f"{ self.ENDPOINT_CURRENCIES }?" f"{ urlencode({'app_id': self.api_key, 'base': self.base}) }") data = loads(urlopen(url, timeout=self.timeout).read()) return namedtuple("OpenExchangeRates", "dict frozendict namedtuple")( data, frozendict(data), namedtuple("OpenExchangeRates", data.keys())(*data.values()))
def __call__(self) -> ConfigMapping: try: with open(self.path) as f: s = f.read() self.parser(s) return frozendict(self.parser(s)) except FileNotFoundError: return {} except Exception as e: raise ConfigError(f'error parsing config file {self.path}') from e
def _parsed_response(self, response): data = loads(response, parse_int=self.tipe, parse_float=self.tipe)['rates'] if self.local_base: data = self._local_conversion(data, self.local_base) return namedtuple( "OpenExchangeRates", "dict frozendict html namedtuple")( data, frozendict(data), self.html(data), namedtuple("OpenExchangeRates", data.keys())(*data.values()))
def make_json_flat(jsony: dict, delimiter: str="__") -> namedtuple: """Make a JSON from Nested to Flat with an arbitrary delimiter.""" values = {} for item in jsony.keys(): if isinstance(jsony[item], dict): get = make_json_flat(jsony[item], delimiter) for something in get.keys(): values[f"{item}{delimiter}{something}"] = get[something] else: values[item] = jsony[item] return namedtuple("JSONFlat", "dict json frozendict")( values, dumps(values), frozendict(values))
def random_set(fn, size, blocked=(), max_tries=25, args=(), kwargs=frozendict({})): """ Call fn multiple times to generate a set of random unique values with the given size. Optionally, items included in the blocked set will not be included in the output. """ res = set() blocked = set(blocked) while len(res) < size: for _ in range(max_tries): new = fn(*args, **kwargs) if new not in blocked and new not in res: res.add(new) return res
'SWIFTISOCodeField', 'SemVerField', 'SimplePasswordField', 'SmallHexadecimalField', 'UAZipCodeField', 'USSocialSecurityNumberField', 'USZipCodeField', 'UYCIField', 'XMLField', ) ############################################################################## FIELD_TYPES = {"money": "money", "xml": "xml", "tstzrange": "tstzrange"} ISO639_1: dict = frozendict( loads((Path(__file__).parent / "languages-data.json").read_bytes())) ISO4217: dict = frozendict( loads((Path(__file__).parent / "currency-data.json").read_bytes())) ISO3166: dict = frozendict( loads((Path(__file__).parent / "country-data.json").read_bytes())) INT2COUNTRY: dict = frozendict( loads((Path(__file__).parent / "int2country.json").read_bytes())) INT2CURRENCY: dict = frozendict( loads((Path(__file__).parent / "int2currency.json").read_bytes())) ##############################################################################
def timestamp2human(timestamp_on_seconds: int, iso_sep: str = " ") -> namedtuple: """Calculate date & time, with precision from seconds to millenniums.""" # Calculate all time units from timestamp_on_seconds positive integer. minutes, seconds = divmod(int(abs(timestamp_on_seconds)), 60) hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) weeks, days = divmod(days, 7) months, weeks = divmod(weeks, 4) years, months = divmod(months, 12) decades, years = divmod(years, 10) centuries, decades = divmod(decades, 10) millenniums, centuries = divmod(centuries, 10) # Build a namedtuple with all named time units and all its integer values. time_units = namedtuple("AllHumanFriendlyFrequentTimeUnits", ("seconds minutes hours days weeks months " "years decades centuries millenniums"))( seconds, minutes, hours, days, weeks, months, years, decades, centuries, millenniums) # Build a human friendly time string with frequent time units. time_parts, human_time_auto = deque(), None # [] if millenniums: this_time = f"{millenniums} Millennium{'s' if millenniums > 1 else ''}" if not human_time_auto: human_time_auto = this_time time_parts.append(this_time) if centuries: this_time_unit = f"{centuries} Centur{'ies' if centuries > 1 else 'y'}" if not human_time_auto: human_time_auto = this_time_unit time_parts.append(this_time_unit) if decades: this_time_unit = f"{decades} Decade{'s' if decades > 1 else ''}" if not human_time_auto: human_time_auto = this_time_unit time_parts.append(this_time_unit) if years: this_time_unit = f"{years} Year{'s' if years > 1 else ''}" if not human_time_auto: human_time_auto = this_time_unit time_parts.append(this_time_unit) if months: this_time_unit = f"{months} Month{'s' if months > 1 else ''}" if not human_time_auto: human_time_auto = this_time_unit time_parts.append(this_time_unit) if weeks: this_time_unit = f"{weeks} Week{'s' if weeks > 1 else ''}" if not human_time_auto: human_time_auto = this_time_unit time_parts.append(this_time_unit) if days: this_time_unit = f"{days} Day{'s' if days > 1 else ''}" if not human_time_auto: human_time_auto = this_time_unit time_parts.append(this_time_unit) if hours: this_time_unit = f"{hours} Hour{'s' if hours > 1 else ''}" if not human_time_auto: human_time_auto = this_time_unit time_parts.append(this_time_unit) if minutes: this_time_unit = f"{minutes} minute{'s' if minutes > 1 else ''}" if not human_time_auto: human_time_auto = this_time_unit time_parts.append(this_time_unit) if not human_time_auto: human_time_auto = f"{seconds} Second{'s' if seconds > 1 else ''}" time_parts.append(f"{seconds} Second{'s' if seconds > 1 else ''}") # Get a Human string ISO-8601 representation of datetime.datetime with UTC. iso_datetime = datetime.fromtimestamp(timestamp_on_seconds).replace( microsecond=0).astimezone().isoformat(iso_sep) time_dict = { "seconds": seconds, "minutes": minutes, "hours": hours, "days": days, "weeks": weeks, "months": months, "years": years, "decades": decades, "centuries": centuries, "millenniums": millenniums, } return namedtuple("HumanTimes", "human units auto iso dict json inmmutable ")( " ".join(time_parts), time_units, human_time_auto, iso_datetime, time_dict, dumps(time_dict), frozendict(time_dict))
def bytes2human(integer_bytes: int) -> namedtuple: """Calculate Bytes, with precision from Bytes to Yottabytes.""" # Calculate all Byte units from integer_bytes positive integer. kilo, bites = divmod(int(abs(integer_bytes)), 1_024) mega, kilo = divmod(kilo, 1_024) giga, mega = divmod(mega, 1_024) tera, giga = divmod(giga, 1_024) peta, tera = divmod(tera, 1_024) exa, peta = divmod(peta, 1_024) zetta, exa = divmod(exa, 1_024) yotta, zetta = divmod(zetta, 1_024) # Build a namedtuple with all named bytes units and all its integer values. bytes_units = namedtuple("AllHumanFriendlyFrequentByteUnits", "byte kilo mega giga tera peta exa zetta yotta")( bites, kilo, mega, giga, tera, peta, exa, zetta, yotta) # Build a human friendly bytes string with frequent bytes units. bytes_parts, human_bytes_auto = deque(), None # [] if yotta: this_byte_unit = f"{yotta} Yottabyte{'s' if yotta > 1 else ''}" if not human_bytes_auto: human_bytes_auto, bytes_short = this_byte_unit, f"{yotta}Yb" bytes_parts.append(this_byte_unit) if zetta: this_byte_unit = f"{zetta} Zettabyte{'s' if zetta > 1 else ''}" if not human_bytes_auto: human_bytes_auto, bytes_short = this_byte_unit, f"{zetta}Zb" bytes_parts.append(this_byte_unit) if exa: this_byte_unit = f"{exa} Exabyte{'s' if exa > 1 else ''}" if not human_bytes_auto: human_bytes_auto, bytes_short = this_byte_unit, f"{exa}Eb" bytes_parts.append(this_byte_unit) if peta: this_byte_unit = f"{peta} Petabyte{'s' if peta > 1 else ''}" if not human_bytes_auto: human_bytes_auto, bytes_short = this_byte_unit, f"{peta}Pb" bytes_parts.append(this_byte_unit) if tera: this_byte_unit = f"{tera} Terabyte{'s' if tera > 1 else ''}" if not human_bytes_auto: human_bytes_auto, bytes_short = this_byte_unit, f"{tera}Tb" bytes_parts.append(this_byte_unit) if giga: this_byte_unit = f"{giga} Gigabyte{'s' if giga > 1 else ''}" if not human_bytes_auto: human_bytes_auto, bytes_short = this_byte_unit, f"{giga}Gb" bytes_parts.append(this_byte_unit) if mega: this_byte_unit = f"{mega} Megabyte{'s' if mega > 1 else ''}" if not human_bytes_auto: human_bytes_auto, bytes_short = this_byte_unit, f"{mega}Mb" bytes_parts.append(this_byte_unit) if kilo: this_byte_unit = f"{kilo} Kilobyte{'s' if kilo > 1 else ''}" if not human_bytes_auto: human_bytes_auto, bytes_short = this_byte_unit, f"{kilo}Kb" bytes_parts.append(this_byte_unit) if not human_bytes_auto: human_bytes_auto = f"{bites} Byte{'s' if bites > 1 else ''}" bytes_short = f"{bites}b" bytes_parts.append(f"{bites} Byte{'s' if bites > 1 else ''}") bytes_dict = { "byte": bites, "kilo": kilo, "mega": mega, "giga": giga, "tera": tera, "peta": peta, "exa": exa, "zetta": zetta, "yotta": yotta, } return namedtuple("HumanBytes", "human units auto short dict json frozendict")( " ".join(bytes_parts), bytes_units, human_bytes_auto, bytes_short, bytes_dict, dumps(bytes_dict), frozendict(bytes_dict))
NAMED2HEX = frozendict({ 'aqua': '#00ffff', 'black': '#000000', 'blue': '#0000ff', 'fuchsia': '#ff00ff', 'green': '#008000', 'gray': '#808080', 'lime': '#00ff00', 'maroon': '#800000', 'navy': '#000080', 'olive': '#808000', 'purple': '#800080', 'red': '#ff0000', 'silver': '#c0c0c0', 'teal': '#008080', 'white': '#ffffff', 'yellow': '#ffff00', 'orange': '#ffa500', 'aliceblue': '#f0f8ff', 'antiquewhite': '#faebd7', 'aqua': '#00ffff', 'aquamarine': '#7fffd4', 'azure': '#f0ffff', 'beige': '#f5f5dc', 'bisque': '#ffe4c4', 'black': '#000000', 'blanchedalmond': '#ffebcd', 'blue': '#0000ff', 'blueviolet': '#8a2be2', 'brown': '#a52a2a', 'burlywood': '#deb887', 'cadetblue': '#5f9ea0', 'chartreuse': '#7fff00', 'chocolate': '#d2691e', 'coral': '#ff7f50', 'cornflowerblue': '#6495ed', 'cornsilk': '#fff8dc', 'crimson': '#dc143c', 'cyan': '#00ffff', 'darkblue': '#00008b', 'darkcyan': '#008b8b', 'darkgoldenrod': '#b8860b', 'darkgray': '#a9a9a9', 'darkgrey': '#a9a9a9', 'darkgreen': '#006400', 'darkkhaki': '#bdb76b', 'darkmagenta': '#8b008b', 'darkolivegreen': '#556b2f', 'darkorange': '#ff8c00', 'darkorchid': '#9932cc', 'darkred': '#8b0000', 'darksalmon': '#e9967a', 'darkseagreen': '#8fbc8f', 'darkslateblue': '#483d8b', 'darkslategray': '#2f4f4f', 'darkslategrey': '#2f4f4f', 'darkturquoise': '#00ced1', 'darkviolet': '#9400d3', 'deeppink': '#ff1493', 'deepskyblue': '#00bfff', 'dimgray': '#696969', 'dimgrey': '#696969', 'dodgerblue': '#1e90ff', 'firebrick': '#b22222', 'floralwhite': '#fffaf0', 'forestgreen': '#228b22', 'fuchsia': '#ff00ff', 'gainsboro': '#dcdcdc', 'ghostwhite': '#f8f8ff', 'gold': '#ffd700', 'goldenrod': '#daa520', 'gray': '#808080', 'grey': '#808080', 'green': '#008000', 'greenyellow': '#adff2f', 'honeydew': '#f0fff0', 'hotpink': '#ff69b4', 'indianred': '#cd5c5c', 'indigo': '#4b0082', 'ivory': '#fffff0', 'khaki': '#f0e68c', 'lavender': '#e6e6fa', 'lavenderblush': '#fff0f5', 'lawngreen': '#7cfc00', 'lemonchiffon': '#fffacd', 'lightblue': '#add8e6', 'lightcoral': '#f08080', 'lightcyan': '#e0ffff', 'lightgoldenrodyellow': '#fafad2', 'lightgray': '#d3d3d3', 'lightgrey': '#d3d3d3', 'lightgreen': '#90ee90', 'lightpink': '#ffb6c1', 'lightsalmon': '#ffa07a', 'lightseagreen': '#20b2aa', 'lightskyblue': '#87cefa', 'lightslategray': '#778899', 'lightslategrey': '#778899', 'lightsteelblue': '#b0c4de', 'lightyellow': '#ffffe0', 'lime': '#00ff00', 'limegreen': '#32cd32', 'linen': '#faf0e6', 'magenta': '#ff00ff', 'maroon': '#800000', 'mediumaquamarine': '#66cdaa', 'mediumblue': '#0000cd', 'mediumorchid': '#ba55d3', 'mediumpurple': '#9370db', 'mediumseagreen': '#3cb371', 'mediumslateblue': '#7b68ee', 'mediumspringgreen': '#00fa9a', 'mediumturquoise': '#48d1cc', 'mediumvioletred': '#c71585', 'midnightblue': '#191970', 'mintcream': '#f5fffa', 'mint': '#f5fffa', 'mistyrose': '#ffe4e1', 'moccasin': '#ffe4b5', 'navajowhite': '#ffdead', 'navy': '#000080', 'oldlace': '#fdf5e6', 'olive': '#808000', 'olivedrab': '#6b8e23', 'orange': '#ffa500', 'orangered': '#ff4500', 'orchid': '#da70d6', 'palegoldenrod': '#eee8aa', 'palegreen': '#98fb98', 'paleturquoise': '#afeeee', 'palevioletred': '#db7093', 'papayawhip': '#ffefd5', 'peachpuff': '#ffdab9', 'per': '#cd853f', 'pink': '#ffc0cb', 'plum': '#dda0dd', 'powderblue': '#b0e0e6', 'purple': '#800080', 'red': '#ff0000', 'rosybrown': '#bc8f8f', 'royalblue': '#4169e1', 'saddlebrown': '#8b4513', 'salmon': '#fa8072', 'sandybrown': '#f4a460', 'seagreen': '#2e8b57', 'seashell': '#fff5ee', 'sienna': '#a0522d', 'silver': '#c0c0c0', 'skyblue': '#87ceeb', 'slateblue': '#6a5acd', 'slategray': '#708090', 'slategrey': '#708090', 'snow': '#fffafa', 'springgreen': '#00ff7f', 'steelblue': '#4682b4', 'tan': '#d2b48c', 'teal': '#008080', 'thistle': '#d8bfd8', 'tomato': '#ff6347', 'turquoise': '#40e0d0', 'violet': '#ee82ee', 'wheat': '#f5deb3', 'white': '#ffffff', 'whitesmoke': '#f5f5f5', 'yellow': '#ffff00', 'yellowgreen': '#9acd32', 'rebeccapurple': '#663399', })
settings: dict = frozendict({ 'DATABASE': { # 'default': { # Prod. # 'database': 'production', # 'host': '127.0.0.1', # 'port': 5433, # 'user': '******', # 'password': '******', # 'engine': 'PooledPostgresqlDatabase', # 'fields': {'varchar': 'varchar'}, # Other args for DB Driver. # 'register_hstore': False, # }, 'default': { # Development, Local, Testing. 'database': 'testing.db', 'engine': 'SqliteDatabase', }, }, 'STATICS': { 'ROOT_DIR': 'static', # Include the 'static/' directory. 'PACKAGE_DIRS': ['apistar'], # Include the builtin apistar static files }, 'TEMPLATES': { 'ROOT_DIR': 'templates', # Include the 'templates/' directory. 'PACKAGE_DIRS': ['apistar'], # Include the built-in apistar templates. }, 'RENDERERS': (JSONRenderer(), MessagePackRenderer()), # 'RENDERERS': (JSONRenderer(), MessagePackRenderer(), HTMLRenderer()), 'PARSERS': (JSONParser(), MultiPartParser(), MessagePackParser()), # 'PARSERS': (JSONParser(), MultiPartParser()), # Default. 'AUTHENTICATION': (JWTAuthentication(), ), 'JWT': { # Do NOT PUSH your SECRET into version control!. 'SECRET': 's0m3-l0ng-s3cr3t', # Long randomized secret string key. 'LEEWAY': 60, # Seconds of expiration time. }, # https://github.com/audiolion/apistar-jwt#usage 'PERMISSIONS': ( IsAuthenticated(), IsGuestUser(), IsFreeUser(), IsPremiumUser(), IsCompanyUser(), IsAdminUser(), IsTesterUser(), IsStaffUser(), ), 'HUEY': { # https://huey.readthedocs.io 'FILENAME': 'huey_tasks_queue.db', # File for Huey DB. 'NAME': 'apistar', # Tasks Queue Name. "HOST": '127.0.0.1', # Your Redis host. }, 'DONT_WRITE_BYTECODE': True, # No *.PYC })