INSTRUCTION
stringlengths
1
8.43k
RESPONSE
stringlengths
75
104k
Loads config checking CLI arguments for a config file
def load_config_from_cli(config: GoodConf, argv: List[str]) -> List[str]: """Loads config, checking CLI arguments for a config file""" # Monkey patch Django's command parser from django.core.management.base import BaseCommand original_parser = BaseCommand.create_parser def patched_parser(self, prog_name, subcommand): parser = original_parser(self, prog_name, subcommand) argparser_add_argument(parser, config) return parser BaseCommand.create_parser = patched_parser try: parser = argparse.ArgumentParser(add_help=False) argparser_add_argument(parser, config) config_arg, default_args = parser.parse_known_args(argv) config.load(config_arg.config) yield default_args finally: # Put that create_parser back where it came from or so help me! BaseCommand.create_parser = original_parser
Load s config then runs Django s execute_from_command_line
def execute_from_command_line_with_config(config: GoodConf, argv: List[str]): """Load's config then runs Django's execute_from_command_line""" with load_config_from_cli(config, argv) as args: from django.core.management import execute_from_command_line execute_from_command_line(args)
Adds argument for config to existing argparser
def argparser_add_argument(parser: argparse.ArgumentParser, config: GoodConf): """Adds argument for config to existing argparser""" help = "Config file." if config.file_env_var: help += (" Can also be configured via the " "environment variable: {}".format(config.file_env_var)) if config.default_files: help += (" Defaults to the first file that exists from " "[{}].".format(', '.join(config.default_files))) parser.add_argument('-C', '--config', metavar='FILE', help=help)
Given a file path parse it based on its extension ( YAML or JSON ) and return the values as a Python dictionary. JSON is the default if an extension can t be determined.
def _load_config(path: str) -> dict: """ Given a file path, parse it based on its extension (YAML or JSON) and return the values as a Python dictionary. JSON is the default if an extension can't be determined. """ __, ext = os.path.splitext(path) if ext in ['.yaml', '.yml']: import ruamel.yaml loader = ruamel.yaml.safe_load else: loader = json.load with open(path) as f: config = loader(f) return config
Find config file and set values
def load(self, filename: str = None): """Find config file and set values""" if filename: self.config_file = _find_file(filename) else: if self.file_env_var and self.file_env_var in os.environ: self.config_file = _find_file(os.environ[self.file_env_var]) if not self.config_file: for filename in self.default_files: self.config_file = _find_file(filename, require=False) if self.config_file: break if self.config_file: config = _load_config(self.config_file) log.info("Loading config from %s", self.config_file) else: config = {} log.info("No config file specified. " "Loading with environment variables.") self.set_values(config)
Dumps initial config in YAML
def generate_yaml(cls, **override): """ Dumps initial config in YAML """ import ruamel.yaml yaml = ruamel.yaml.YAML() yaml_str = StringIO() yaml.dump(cls.get_initial(**override), stream=yaml_str) yaml_str.seek(0) dict_from_yaml = yaml.load(yaml_str) if cls.__doc__: dict_from_yaml.yaml_set_start_comment( '\n' + cls.__doc__ + '\n\n') for k in dict_from_yaml.keys(): if cls._values[k].help: dict_from_yaml.yaml_set_comment_before_after_key( k, before='\n' + cls._values[k].help) yaml_str = StringIO() yaml.dump(dict_from_yaml, yaml_str) yaml_str.seek(0) return yaml_str.read()
Documents values in markdown
def generate_markdown(cls): """ Documents values in markdown """ lines = [] if cls.__doc__: lines.extend(['# {}'.format(cls.__doc__), '']) for k, v in cls._values.items(): lines.append('* **{}** '.format(k)) if v.required: lines[-1] = lines[-1] + '_REQUIRED_ ' if v.help: lines.append(' {} '.format(v.help)) lines.append(' type: `{}` '.format(v.cast_as.__name__)) if v.default is not None: lines.append(' default: `{}` '.format(v.default)) return '\n'.join(lines)
converts string to type requested by cast_as
def cast(self, val: str): """converts string to type requested by `cast_as`""" try: return getattr(self, 'cast_as_{}'.format( self.cast_as.__name__.lower()))(val) except AttributeError: return self.cast_as(val)
Returns all dates from first to last included.
def list_dates_between(first_date, last_date): """Returns all dates from first to last included.""" return [first_date + timedelta(days=n) for n in range(1 + (last_date - first_date).days)]
Fast %Y - %m - %d parsing.
def parse_date(s): """Fast %Y-%m-%d parsing.""" try: return datetime.date(int(s[:4]), int(s[5:7]), int(s[8:10])) except ValueError: # other accepted format used in one-day data set return datetime.datetime.strptime(s, '%d %B %Y').date()
To be subclassed if alternate methods of loading data.
def load_file(self, currency_file): """To be subclassed if alternate methods of loading data. """ if currency_file.startswith(('http://', 'https://')): content = urlopen(currency_file).read() else: with open(currency_file, 'rb') as f: content = f.read() if currency_file.endswith('.zip'): self.load_lines(get_lines_from_zip(content)) else: self.load_lines(content.decode('utf-8').splitlines())
Fill missing rates of a currency with the closest available ones.
def _set_missing_to_none(self, currency): """Fill missing rates of a currency with the closest available ones.""" rates = self._rates[currency] first_date, last_date = self.bounds[currency] for date in list_dates_between(first_date, last_date): if date not in rates: rates[date] = None if self.verbose: missing = len([r for r in itervalues(rates) if r is None]) if missing: print('{0}: {1} missing rates from {2} to {3} ({4} days)'.format( currency, missing, first_date, last_date, 1 + (last_date - first_date).days))
Fill missing rates of a currency.
def _compute_missing_rates(self, currency): """Fill missing rates of a currency. This is done by linear interpolation of the two closest available rates. :param str currency: The currency to fill missing rates for. """ rates = self._rates[currency] # tmp will store the closest rates forward and backward tmp = defaultdict(lambda: [None, None]) for date in sorted(rates): rate = rates[date] if rate is not None: closest_rate = rate dist = 0 else: dist += 1 tmp[date][0] = closest_rate, dist for date in sorted(rates, reverse=True): rate = rates[date] if rate is not None: closest_rate = rate dist = 0 else: dist += 1 tmp[date][1] = closest_rate, dist for date in sorted(tmp): (r0, d0), (r1, d1) = tmp[date] rates[date] = (r0 * d1 + r1 * d0) / (d0 + d1) if self.verbose: print(('{0}: filling {1} missing rate using {2} ({3}d old) and ' '{4} ({5}d later)').format(currency, date, r0, d0, r1, d1))
Get a rate for a given currency and date.
def _get_rate(self, currency, date): """Get a rate for a given currency and date. :type date: datetime.date >>> from datetime import date >>> c = CurrencyConverter() >>> c._get_rate('USD', date=date(2014, 3, 28)) 1.375... >>> c._get_rate('BGN', date=date(2010, 11, 21)) Traceback (most recent call last): RateNotFoundError: BGN has no rate for 2010-11-21 """ if currency == self.ref_currency: return 1.0 if date not in self._rates[currency]: first_date, last_date = self.bounds[currency] if not self.fallback_on_wrong_date: raise RateNotFoundError('{0} not in {1} bounds {2}/{3}'.format( date, currency, first_date, last_date)) if date < first_date: fallback_date = first_date elif date > last_date: fallback_date = last_date else: raise AssertionError('Should never happen, bug in the code!') if self.verbose: print(r'/!\ {0} not in {1} bounds {2}/{3}, falling back to {4}'.format( date, currency, first_date, last_date, fallback_date)) date = fallback_date rate = self._rates[currency][date] if rate is None: raise RateNotFoundError('{0} has no rate for {1}'.format(currency, date)) return rate
Convert amount from a currency to another one.
def convert(self, amount, currency, new_currency='EUR', date=None): """Convert amount from a currency to another one. :param float amount: The amount of `currency` to convert. :param str currency: The currency to convert from. :param str new_currency: The currency to convert to. :param datetime.date date: Use the conversion rate of this date. If this is not given, the most recent rate is used. :return: The value of `amount` in `new_currency`. :rtype: float >>> from datetime import date >>> c = CurrencyConverter() >>> c.convert(100, 'EUR', 'USD', date=date(2014, 3, 28)) 137.5... >>> c.convert(100, 'USD', date=date(2014, 3, 28)) 72.67... >>> c.convert(100, 'BGN', date=date(2010, 11, 21)) Traceback (most recent call last): RateNotFoundError: BGN has no rate for 2010-11-21 """ for c in currency, new_currency: if c not in self.currencies: raise ValueError('{0} is not a supported currency'.format(c)) if date is None: date = self.bounds[currency].last_date else: try: date = date.date() # fallback if input was a datetime object except AttributeError: pass r0 = self._get_rate(currency, date) r1 = self._get_rate(new_currency, date) return float(amount) / r0 * r1
Group iterable by n elements.
def grouper(iterable, n, fillvalue=None): """Group iterable by n elements. >>> for t in grouper('abcdefg', 3, fillvalue='x'): ... print(''.join(t)) abc def gxx """ return list(zip_longest(*[iter(iterable)] * n, fillvalue=fillvalue))
Animate given frame for set number of iterations.
def animate(frames, interval, name, iterations=2): """Animate given frame for set number of iterations. Parameters ---------- frames : list Frames for animating interval : float Interval between two frames name : str Name of animation iterations : int, optional Number of loops for animations """ for i in range(iterations): for frame in frames: frame = get_coded_text(frame) output = "\r{0} {1}".format(frame, name) sys.stdout.write(output) sys.stdout.write(CLEAR_LINE) sys.stdout.flush() time.sleep(0.001 * interval)
Convert Cnf object ot Dimacs cnf string cnf: Cnf object In the converted Cnf there will be only numbers for variable names. The conversion guarantees that the variables will be numbered alphabetically.
def tostring(self, cnf): """Convert Cnf object ot Dimacs cnf string cnf: Cnf object In the converted Cnf there will be only numbers for variable names. The conversion guarantees that the variables will be numbered alphabetically. """ self.varname_dict = {} self.varobj_dict = {} varis = set() for d in cnf.dis: for v in d: varis.add(v.name) ret = "p cnf %d %d" % (len(varis), len(cnf.dis)) varis = dict(list(zip(sorted(list(varis)),list(map(str,list(range(1,len(varis)+1))))))) for v in varis: vo = Variable(v) self.varname_dict[vo] = varis[v] self.varobj_dict[varis[v]] = vo for d in cnf.dis: ret += "\n" vnamelist = [] for v in d: vnamelist.append(("-" if v.inverted else "") + varis[v.name]) ret += " ".join(vnamelist) + " 0" return ret
I just found a remarkably large bug in my SAT solver and found an interesting solution. Remove all b | - b ( - b | b ) & ( b | - a ) & ( - b | a ) & ( a | - a ) becomes ( b | - a ) & ( - b | a )
def reduceCnf(cnf): """ I just found a remarkably large bug in my SAT solver and found an interesting solution. Remove all b | -b (-b | b) & (b | -a) & (-b | a) & (a | -a) becomes (b | -a) & (-b | a) Remove all (-e) & (-e) (-e | a) & (-e | a) & (-e | a) & (-e | a) becomes (-e | a) (-b | b | c) becomes nothing, not (c) """ output = Cnf() for x in cnf.dis: dont_add = False for y in x: for z in x: if z == -y: dont_add = True break if dont_add: break if dont_add: continue # TODO: Is this necessary anymore? Probably not. Do statistical analysis. if x not in output.dis: output.dis |= frozenset([x]) return output
[ DEPRECATED ] Load the polynomial series for name and return it.
def load(self, name): """[DEPRECATED] Load the polynomial series for `name` and return it.""" s = self.sets.get(name) if s is None: self.sets[name] = s = np.load(self.path('jpl-%s.npy' % name)) return s
[ DEPRECATED ] Compute the position of name at time tdb [ + tdb2 ].
def position(self, name, tdb, tdb2=0.0): """[DEPRECATED] Compute the position of `name` at time ``tdb [+ tdb2]``. The position is returned as a NumPy array ``[x y z]``. The barycentric dynamical time `tdb` argument should be a float. If there are many dates you want computed, then make `tdb` an array, which is more efficient than calling this method multiple times; the return value will be a two-dimensional array giving a row of values for each coordinate. For extra precision, the time can be split into two floats; a popular choice is to use `tdb` for the integer or half-integer date, and `tdb2` to hold the remaining fraction. Consult the `names` attribute of this ephemeris for the values of `name` it supports, such as ``'mars'`` or ``'earthmoon'``. """ bundle = self.compute_bundle(name, tdb, tdb2) return self.position_from_bundle(bundle)
[ DEPRECATED ] Compute the position and velocity of name at tdb [ + tdb2 ].
def position_and_velocity(self, name, tdb, tdb2=0.0): """[DEPRECATED] Compute the position and velocity of `name` at ``tdb [+ tdb2]``. The position and velocity are returned in a 2-tuple:: ([x y z], [xdot ydot zdot]) The barycentric dynamical time `tdb` argument should be a float. If there are many dates you want computed, then make `tdb` an array, which is more efficient than calling this method multiple times; the return values will be two-dimensional arrays giving a row of values for each coordinate. For extra precision, the time can be split into two floats; a popular choice is to use `tdb` for the integer or half-integer date, and `tdb2` to hold the remaining fraction. Consult the `names` attribute of this ephemeris for the values of `name` it supports, such as ``'mars'`` or ``'earthmoon'``. """ bundle = self.compute_bundle(name, tdb, tdb2) position = self.position_from_bundle(bundle) velocity = self.velocity_from_bundle(bundle) return position, velocity
[ DEPRECATED ] Legacy routine that concatenates position and velocity vectors.
def compute(self, name, tdb): """[DEPRECATED] Legacy routine that concatenates position and velocity vectors. This routine is deprecated. Use the methods `position()` and `position_and_velocity()` instead. This method follows the same calling convention, but incurs extra copy operations in order to return a single NumPy array:: [x y z xdot ydot zdot] """ bundle = self.compute_bundle(name, tdb, 0.0) position = self.position_from_bundle(bundle) velocity = self.velocity_from_bundle(bundle) return np.concatenate((position, velocity))
[ DEPRECATED ] Return a tuple of coefficients and parameters for tdb.
def compute_bundle(self, name, tdb, tdb2=0.0): """[DEPRECATED] Return a tuple of coefficients and parameters for `tdb`. The return value is a tuple that bundles together the coefficients and other Chebyshev intermediate values that are needed for the computation of either the position or velocity. The bundle can then be passed to either `position_from_bundle()` or `velocity_from_bundle()` to finish the computation. See the package-level documentation for details; most users will simply call `position()` or `position_and_velocity()` instead. The barycentric dynamical time `tdb` argument should be a float. If there are many dates you want computed, then make `tdb` an array, which is more efficient than calling this method multiple times; the return values will be arrays providing a value for each time in `tdb`. For extra precision, the time can be split into two floats; a popular choice is to use `tdb` for the integer or half-integer date, and `tdb2` to hold the remaining fraction. Consult the `names` attribute of this ephemeris for the values of `name` it supports, such as ``'mars'`` or ``'earthmoon'``. """ input_was_scalar = getattr(tdb, 'shape', ()) == () if input_was_scalar: tdb = np.array((tdb,)) # no need to deal with tdb2; numpy broadcast will add fine below. coefficient_sets = self.load(name) number_of_sets, axis_count, coefficient_count = coefficient_sets.shape jalpha, jomega = self.jalpha, self.jomega days_per_set = (jomega - jalpha) / number_of_sets # to keep precision, first subtract, then add index, offset = divmod((tdb - jalpha) + tdb2, days_per_set) index = index.astype(int) if (index < 0).any() or (number_of_sets < index).any(): raise DateError('ephemeris %s only covers dates %.1f through %.1f' % (self.name, jalpha, jomega)) omegas = (index == number_of_sets) index[omegas] -= 1 offset[omegas] += days_per_set coefficients = np.rollaxis(coefficient_sets[index], 1) # Chebyshev recurrence: T = np.empty((coefficient_count, len(index))) T[0] = 1.0 T[1] = t1 = 2.0 * offset / days_per_set - 1.0 twot1 = t1 + t1 for i in range(2, coefficient_count): T[i] = twot1 * T[i-1] - T[i-2] bundle = coefficients, days_per_set, T, twot1 return bundle
[ DEPRECATED ] Return position given the coefficient_bundle () return value.
def position_from_bundle(self, bundle): """[DEPRECATED] Return position, given the `coefficient_bundle()` return value.""" coefficients, days_per_set, T, twot1 = bundle return (T.T * coefficients).sum(axis=2)
[ DEPRECATED ] Return velocity given the coefficient_bundle () return value.
def velocity_from_bundle(self, bundle): """[DEPRECATED] Return velocity, given the `coefficient_bundle()` return value.""" coefficients, days_per_set, T, twot1 = bundle coefficient_count = coefficients.shape[2] # Chebyshev derivative: dT = np.empty_like(T) dT[0] = 0.0 dT[1] = 1.0 dT[2] = twot1 + twot1 for i in range(3, coefficient_count): dT[i] = twot1 * dT[i-1] - dT[i-2] + T[i-1] + T[i-1] dT *= 2.0 dT /= days_per_set return (dT.T * coefficients).sum(axis=2)
Return record n as 1 024 bytes ; records are indexed from 1.
def read_record(self, n): """Return record `n` as 1,024 bytes; records are indexed from 1.""" self.file.seek(n * K - K) return self.file.read(K)
Write data to file record n ; records are indexed from 1.
def write_record(self, n, data): """Write `data` to file record `n`; records are indexed from 1.""" self.file.seek(n * K - K) return self.file.write(data)
Return a memory - map of the elements start through end.
def map_words(self, start, end): """Return a memory-map of the elements `start` through `end`. The memory map will offer the 8-byte double-precision floats ("elements") in the file from index `start` through to the index `end`, inclusive, both counting the first float as element 1. Memory maps must begin on a page boundary, so `skip` returns the number of extra bytes at the beginning of the return value. """ i, j = 8 * start - 8, 8 * end try: fileno = self.file.fileno() except (AttributeError, io.UnsupportedOperation): fileno = None if fileno is None: skip = 0 self.file.seek(i) m = self.file.read(j - i) else: skip = i % mmap.ALLOCATIONGRANULARITY r = mmap.ACCESS_READ m = mmap.mmap(fileno, length=j-i+skip, access=r, offset=i-skip) if sys.version_info > (3,): m = memoryview(m) # so further slicing can return views return m, skip
Return the text inside the comment area of the file.
def comments(self): """Return the text inside the comment area of the file.""" record_numbers = range(2, self.fward) if not record_numbers: return '' data = b''.join(self.read_record(n)[0:1000] for n in record_numbers) try: return data[:data.find(b'\4')].decode('ascii').replace('\0', '\n') except IndexError: raise ValueError('DAF file comment area is missing its EOT byte') except UnicodeDecodeError: raise ValueError('DAF file comment area is not ASCII text')
Return floats from start to end inclusive indexed from 1.
def read_array(self, start, end): """Return floats from `start` to `end` inclusive, indexed from 1. The entire range of floats is immediately read into memory from the file, making this efficient for small sequences of floats whose values are all needed immediately. """ f = self.file f.seek(8 * (start - 1)) length = 1 + end - start data = f.read(8 * length) return ndarray(length, self.endian + 'd', data)
Return floats from start to end inclusive indexed from 1.
def map_array(self, start, end): """Return floats from `start` to `end` inclusive, indexed from 1. Instead of pausing to load all of the floats into RAM, this routine creates a memory map which will load data from the file only as it is accessed, and then will let it expire back out to disk later. This is very efficient for large data sets to which you need random access. """ if self._array is None: self._map, skip = self.map_words(1, self.free - 1) assert skip == 0 self._array = ndarray(self.free - 1, self.endian + 'd', self._map) return self._array[start - 1 : end]
Yield ( record_number n_summaries record_data ) for each record.
def summary_records(self): """Yield (record_number, n_summaries, record_data) for each record. Readers will only use the second two values in each tuple. Writers can update the record using the `record_number`. """ record_number = self.fward unpack = self.summary_control_struct.unpack while record_number: data = self.read_record(record_number) next_number, previous_number, n_summaries = unpack(data[:24]) yield record_number, n_summaries, data record_number = int(next_number)
Yield ( name ( value value... )) for each summary in the file.
def summaries(self): """Yield (name, (value, value, ...)) for each summary in the file.""" length = self.summary_length step = self.summary_step for record_number, n_summaries, summary_data in self.summary_records(): name_data = self.read_record(record_number + 1) for i in range(0, int(n_summaries) * step, step): j = self.summary_control_struct.size + i name = name_data[i:i+step].strip() data = summary_data[j:j+length] values = self.summary_struct.unpack(data) yield name, values
Add a new array to the DAF file.
def add_array(self, name, values, array): """Add a new array to the DAF file. The summary will be initialized with the `name` and `values`, and will have its start word and end word fields set to point to where the `array` of floats has been appended to the file. """ f = self.file scs = self.summary_control_struct record_number = self.bward data = bytearray(self.read_record(record_number)) next_record, previous_record, n_summaries = scs.unpack(data[:24]) if n_summaries < self.summaries_per_record: summary_record = record_number name_record = summary_record + 1 data[:24] = scs.pack(next_record, previous_record, n_summaries + 1) self.write_record(summary_record, data) else: summary_record = ((self.free - 1) * 8 + 1023) // 1024 + 1 name_record = summary_record + 1 free_record = summary_record + 2 n_summaries = 0 data[:24] = scs.pack(summary_record, previous_record, n_summaries) self.write_record(record_number, data) summaries = scs.pack(0, record_number, 1).ljust(1024, b'\0') names = b'\0' * 1024 self.write_record(summary_record, summaries) self.write_record(name_record, names) self.bward = summary_record self.free = (free_record - 1) * 1024 // 8 + 1 start_word = self.free f.seek((start_word - 1) * 8) array = numpy_array(array) # TODO: force correct endian f.write(array.view()) end_word = f.tell() // 8 self.free = end_word + 1 self.write_file_record() values = values[:self.nd + self.ni - 2] + (start_word, end_word) base = 1024 * (summary_record - 1) offset = int(n_summaries) * self.summary_step f.seek(base + scs.size + offset) f.write(self.summary_struct.pack(*values)) f.seek(base + 1024 + offset) f.write(name[:self.summary_length].ljust(self.summary_step, b' '))
Close this SPK file.
def close(self): """Close this SPK file.""" self.daf.file.close() for segment in self.segments: if hasattr(segment, '_data'): del segment._data self.daf._array = None self.daf._map = None
Return a textual description of the segment.
def describe(self, verbose=True): """Return a textual description of the segment.""" center = titlecase(target_names.get(self.center, 'Unknown center')) target = titlecase(target_names.get(self.target, 'Unknown target')) text = ('{0.start_jd:.2f}..{0.end_jd:.2f} {1} ({0.center})' ' -> {2} ({0.target})'.format(self, center, target)) if verbose: text += ('\n frame={0.frame} data_type={0.data_type} source={1}' .format(self, self.source.decode('ascii'))) return text
Compute the component values for the time tdb plus tdb2.
def compute(self, tdb, tdb2=0.0): """Compute the component values for the time `tdb` plus `tdb2`.""" for position in self.generate(tdb, tdb2): return position
Close this file.
def close(self): """Close this file.""" self.daf.file.close() for segment in self.segments: if hasattr(segment, '_data'): del segment._data
Return a textual description of the segment.
def describe(self, verbose=True): """Return a textual description of the segment.""" body = titlecase(target_names.get(self.body, 'Unknown body')) text = ('{0.start_jd:.2f}..{0.end_jd:.2f} frame={0.frame}' ' {1} ({0.body})'.format(self, body)) if verbose: text += ('\n data_type={0.data_type} source={1}' .format(self, self.source.decode('ascii'))) return text
Map the coefficients into memory using a NumPy array.
def _load(self): """Map the coefficients into memory using a NumPy array. """ if self.data_type == 2: component_count = 3 else: raise ValueError('only binary PCK data type 2 is supported') init, intlen, rsize, n = self.daf.read_array(self.end_i - 3, self.end_i) initial_epoch = jd(init) interval_length = intlen / S_PER_DAY coefficient_count = int(rsize - 2) // component_count coefficients = self.daf.map_array(self.start_i, self.end_i - 4) coefficients.shape = (int(n), int(rsize)) coefficients = coefficients[:,2:] # ignore MID and RADIUS elements coefficients.shape = (int(n), component_count, coefficient_count) coefficients = rollaxis(coefficients, 1) return initial_epoch, interval_length, coefficients
Generate angles and derivatives for time tdb plus tdb2.
def compute(self, tdb, tdb2, derivative=True): """Generate angles and derivatives for time `tdb` plus `tdb2`. If ``derivative`` is true, return a tuple containing both the angle and its derivative; otherwise simply return the angles. """ scalar = not getattr(tdb, 'shape', 0) and not getattr(tdb2, 'shape', 0) if scalar: tdb = array((tdb,)) data = self._data if data is None: self._data = data = self._load() initial_epoch, interval_length, coefficients = data component_count, n, coefficient_count = coefficients.shape # Subtracting tdb before adding tdb2 affords greater precision. index, offset = divmod((tdb - initial_epoch) + tdb2, interval_length) index = index.astype(int) if (index < 0).any() or (index > n).any(): final_epoch = initial_epoch + interval_length * n raise ValueError('segment only covers dates %.1f through %.1f' % (initial_epoch, final_epoch)) omegas = (index == n) index[omegas] -= 1 offset[omegas] += interval_length coefficients = coefficients[:,index] # Chebyshev polynomial. T = empty((coefficient_count, len(index))) T[0] = 1.0 T[1] = t1 = 2.0 * offset / interval_length - 1.0 twot1 = t1 + t1 for i in range(2, coefficient_count): T[i] = twot1 * T[i-1] - T[i-2] components = (T.T * coefficients).sum(axis=2) if scalar: components = components[:,0] if not derivative: return components # Chebyshev differentiation. dT = empty_like(T) dT[0] = 0.0 dT[1] = 1.0 if coefficient_count > 2: dT[2] = twot1 + twot1 for i in range(3, coefficient_count): dT[i] = twot1 * dT[i-1] - dT[i-2] + T[i-1] + T[i-1] dT *= 2.0 dT /= interval_length rates = (dT.T * coefficients).sum(axis=2) if scalar: rates = rates[:,0] return components, rates
Show system notification with duration t ( ms )
def notify(msg, msg_type=0, t=None): "Show system notification with duration t (ms)" if platform.system() == 'Darwin': command = notify_command_osx(msg, msg_type, t) else: command = notify_command_linux(msg, t) os.system(command.encode('utf-8'))
批量获取音乐的地址
def geturls_new_api(song_ids): """ 批量获取音乐的地址 """ br_to_quality = {128000: 'MD 128k', 320000: 'HD 320k'} alters = NetEase().songs_detail_new_api(song_ids) urls = [alter['url'] for alter in alters] return urls
Visit a function call.
def visit_Call(self, node): """ Visit a function call. We expect every logging statement and string format to be a function call. """ # CASE 1: We're in a logging statement if self.within_logging_statement(): if self.within_logging_argument() and self.is_format_call(node): self.violations.append((node, STRING_FORMAT_VIOLATION)) super(LoggingVisitor, self).generic_visit(node) return logging_level = self.detect_logging_level(node) if logging_level and self.current_logging_level is None: self.current_logging_level = logging_level # CASE 2: We're in some other statement if logging_level is None: super(LoggingVisitor, self).generic_visit(node) return # CASE 3: We're entering a new logging statement self.current_logging_call = node if logging_level == "warn": self.violations.append((node, WARN_VIOLATION)) self.check_exc_info(node) for index, child in enumerate(iter_child_nodes(node)): if index == 1: self.current_logging_argument = child if index >= 1: self.check_exception_arg(child) if index > 1 and isinstance(child, keyword) and child.arg == "extra": self.current_extra_keyword = child super(LoggingVisitor, self).visit(child) self.current_logging_argument = None self.current_extra_keyword = None self.current_logging_call = None self.current_logging_level = None
Process binary operations while processing the first logging argument.
def visit_BinOp(self, node): """ Process binary operations while processing the first logging argument. """ if self.within_logging_statement() and self.within_logging_argument(): # handle percent format if isinstance(node.op, Mod): self.violations.append((node, PERCENT_FORMAT_VIOLATION)) # handle string concat if isinstance(node.op, Add): self.violations.append((node, STRING_CONCAT_VIOLATION)) super(LoggingVisitor, self).generic_visit(node)
Process dict arguments.
def visit_Dict(self, node): """ Process dict arguments. """ if self.should_check_whitelist(node): for key in node.keys: if key.s in self.whitelist or key.s.startswith("debug_"): continue self.violations.append((self.current_logging_call, WHITELIST_VIOLATION.format(key.s))) if self.should_check_extra_exception(node): for value in node.values: self.check_exception_arg(value) super(LoggingVisitor, self).generic_visit(node)
Process f - string arguments.
def visit_JoinedStr(self, node): """ Process f-string arguments. """ if version_info >= (3, 6): if self.within_logging_statement(): if any(isinstance(i, FormattedValue) for i in node.values): if self.within_logging_argument(): self.violations.append((node, FSTRING_VIOLATION)) super(LoggingVisitor, self).generic_visit(node)
Process keyword arguments.
def visit_keyword(self, node): """ Process keyword arguments. """ if self.should_check_whitelist(node): if node.arg not in self.whitelist and not node.arg.startswith("debug_"): self.violations.append((self.current_logging_call, WHITELIST_VIOLATION.format(node.arg))) if self.should_check_extra_exception(node): self.check_exception_arg(node.value) super(LoggingVisitor, self).generic_visit(node)
Process except blocks.
def visit_ExceptHandler(self, node): """ Process except blocks. """ name = self.get_except_handler_name(node) if not name: super(LoggingVisitor, self).generic_visit(node) return self.current_except_names.append(name) super(LoggingVisitor, self).generic_visit(node) self.current_except_names.pop()
Heuristic to decide whether an AST Call is a logging call.
def detect_logging_level(self, node): """ Heuristic to decide whether an AST Call is a logging call. """ try: if self.get_id_attr(node.func.value) == "warnings": return None # NB: We could also look at the argument signature or the target attribute if node.func.attr in LOGGING_LEVELS: return node.func.attr except AttributeError: pass return None
Helper to get the exception name from an ExceptHandler node in both py2 and py3.
def get_except_handler_name(self, node): """ Helper to get the exception name from an ExceptHandler node in both py2 and py3. """ name = node.name if not name: return None if version_info < (3,): return name.id return name
Check if value has id attribute and return it.
def get_id_attr(self, value): """Check if value has id attribute and return it. :param value: The value to get id from. :return: The value.id. """ if not hasattr(value, "id") and hasattr(value, "value"): value = value.value return value.id
Checks if the node is a bare exception name from an except block.
def is_bare_exception(self, node): """ Checks if the node is a bare exception name from an except block. """ return isinstance(node, Name) and node.id in self.current_except_names
Checks if the node is the expression str ( e ) or unicode ( e ) where e is an exception name from an except block
def is_str_exception(self, node): """ Checks if the node is the expression str(e) or unicode(e), where e is an exception name from an except block """ return ( isinstance(node, Call) and isinstance(node.func, Name) and node.func.id in ('str', 'unicode') and node.args and self.is_bare_exception(node.args[0]) )
Reports a violation if exc_info keyword is used with logging. error or logging. exception.
def check_exc_info(self, node): """ Reports a violation if exc_info keyword is used with logging.error or logging.exception. """ if self.current_logging_level not in ('error', 'exception'): return for kw in node.keywords: if kw.arg == 'exc_info': if self.current_logging_level == 'error': violation = ERROR_EXC_INFO_VIOLATION else: violation = REDUNDANT_EXC_INFO_VIOLATION self.violations.append((node, violation))
Delete file from database only if needed.
def delete_file_if_needed(instance, filefield_name): """Delete file from database only if needed. When editing and the filefield is a new file, deletes the previous file (if any) from the database. Call this function immediately BEFORE saving the instance. """ if instance.pk: model_class = type(instance) # Check if there is a file for the instance in the database if model_class.objects.filter(pk=instance.pk).exclude( **{'%s__isnull' % filefield_name: True} ).exclude( **{'%s__exact' % filefield_name: ''} ).exists(): old_file = getattr( model_class.objects.only(filefield_name).get(pk=instance.pk), filefield_name ) else: old_file = None # If there is a file, delete it if needed if old_file: # When editing and NOT changing the file, # old_file.name == getattr(instance, filefield_name) # returns True. In this case, the file must NOT be deleted. # If the file IS being changed, the comparison returns False. # In this case, the old file MUST be deleted. if (old_file.name == getattr(instance, filefield_name)) is False: DatabaseFileStorage().delete(old_file.name)
Delete the file ( if any ) from the database.
def delete_file(instance, filefield_name): """Delete the file (if any) from the database. Call this function immediately AFTER deleting the instance. """ file_instance = getattr(instance, filefield_name) if file_instance: DatabaseFileStorage().delete(file_instance.name)
Edit the download - link inner text.
def db_file_widget(cls): """Edit the download-link inner text.""" def get_link_display(url): unquoted = unquote(url.split('%2F')[-1]) if sys.version_info.major == 2: # python 2 from django.utils.encoding import force_unicode unquoted = force_unicode(unquoted) return escape(unquoted) def get_template_substitution_values(self, value): # Used by Django < 1.11 subst = super(cls, self).get_template_substitution_values(value) subst['initial'] = get_link_display(value.url) return subst setattr(cls, 'get_template_substitution_values', get_template_substitution_values) def get_context(self, name, value, attrs): context = super(cls, self).get_context(name, value, attrs) if value and hasattr(value, 'url'): context['widget']['display'] = get_link_display(value.url) return context setattr(cls, 'get_context', get_context) return cls
Returns the freshly rendered content for the template and context described by the PDFResponse.
def rendered_content(self): """Returns the freshly rendered content for the template and context described by the PDFResponse. This *does not* set the final content of the response. To set the response content, you must either call render(), or set the content explicitly using the value of this property. """ cmd_options = self.cmd_options.copy() return render_pdf_from_template( self.resolve_template(self.template_name), self.resolve_template(self.header_template), self.resolve_template(self.footer_template), context=self.resolve_context(self.context_data), request=self._request, cmd_options=cmd_options, cover_template=self.resolve_template(self.cover_template) )
Returns a PDF response with a template rendered with the given context.
def render_to_response(self, context, **response_kwargs): """ Returns a PDF response with a template rendered with the given context. """ filename = response_kwargs.pop('filename', None) cmd_options = response_kwargs.pop('cmd_options', None) if issubclass(self.response_class, PDFTemplateResponse): if filename is None: filename = self.get_filename() if cmd_options is None: cmd_options = self.get_cmd_options() return super(PDFTemplateView, self).render_to_response( context=context, filename=filename, show_content_in_browser=self.show_content_in_browser, header_template=self.header_template, footer_template=self.footer_template, cmd_options=cmd_options, cover_template=self.cover_template, **response_kwargs ) else: return super(PDFTemplateView, self).render_to_response( context=context, **response_kwargs )
Converts options into a list of command - line arguments. Skip arguments where no value is provided For flag - type ( No argument ) variables pass only the name and only then if the value is True
def _options_to_args(**options): """ Converts ``options`` into a list of command-line arguments. Skip arguments where no value is provided For flag-type (No argument) variables, pass only the name and only then if the value is True """ flags = [] for name in sorted(options): value = options[name] formatted_flag = '--%s' % name if len(name) > 1 else '-%s' % name formatted_flag = formatted_flag.replace('_', '-') accepts_no_arguments = formatted_flag in NO_ARGUMENT_OPTIONS if value is None or (value is False and accepts_no_arguments): continue flags.append(formatted_flag) if accepts_no_arguments: continue flags.append(six.text_type(value)) return flags
Converts html to PDF using http:// wkhtmltopdf. org/.
def wkhtmltopdf(pages, output=None, **kwargs): """ Converts html to PDF using http://wkhtmltopdf.org/. pages: List of file paths or URLs of the html to be converted. output: Optional output file path. If None, the output is returned. **kwargs: Passed to wkhtmltopdf via _extra_args() (See https://github.com/antialize/wkhtmltopdf/blob/master/README_WKHTMLTOPDF for acceptable args.) Kwargs is passed through as arguments. e.g.: {'footer_html': 'http://example.com/foot.html'} becomes '--footer-html http://example.com/foot.html' Where there is no value passed, use True. e.g.: {'disable_javascript': True} becomes: '--disable-javascript' To disable a default option, use None. e.g: {'quiet': None'} becomes: '' example usage: wkhtmltopdf(pages=['/tmp/example.html'], dpi=300, orientation='Landscape', disable_javascript=True) """ if isinstance(pages, six.string_types): # Support a single page. pages = [pages] if output is None: # Standard output. output = '-' has_cover = kwargs.pop('has_cover', False) # Default options: options = getattr(settings, 'WKHTMLTOPDF_CMD_OPTIONS', None) if options is None: options = {'quiet': True} else: options = copy(options) options.update(kwargs) # Force --encoding utf8 unless the user has explicitly overridden this. options.setdefault('encoding', 'utf8') env = getattr(settings, 'WKHTMLTOPDF_ENV', None) if env is not None: env = dict(os.environ, **env) cmd = 'WKHTMLTOPDF_CMD' cmd = getattr(settings, cmd, os.environ.get(cmd, 'wkhtmltopdf')) # Adding 'cover' option to add cover_file to the pdf to generate. if has_cover: pages.insert(0, 'cover') ck_args = list(chain(shlex.split(cmd), _options_to_args(**options), list(pages), [output])) ck_kwargs = {'env': env} # Handling of fileno() attr. based on https://github.com/GrahamDumpleton/mod_wsgi/issues/85 try: i = sys.stderr.fileno() ck_kwargs['stderr'] = sys.stderr except (AttributeError, IOError): # can't call fileno() on mod_wsgi stderr object pass return check_output(ck_args, **ck_kwargs)
Given a unicode string will do its dandiest to give you back a valid ascii charset string you can use in say http headers and the like.
def http_quote(string): """ Given a unicode string, will do its dandiest to give you back a valid ascii charset string you can use in, say, http headers and the like. """ if isinstance(string, six.text_type): try: import unidecode except ImportError: pass else: string = unidecode.unidecode(string) string = string.encode('ascii', 'replace') # Wrap in double-quotes for ; , and the like string = string.replace(b'\\', b'\\\\').replace(b'"', b'\\"') return '"{0!s}"'.format(string.decode())
Convert all MEDIA files into a file:// URL paths in order to correctly get it displayed in PDFs.
def make_absolute_paths(content): """Convert all MEDIA files into a file://URL paths in order to correctly get it displayed in PDFs.""" overrides = [ { 'root': settings.MEDIA_ROOT, 'url': settings.MEDIA_URL, }, { 'root': settings.STATIC_ROOT, 'url': settings.STATIC_URL, } ] has_scheme = re.compile(r'^[^:/]+://') for x in overrides: if not x['url'] or has_scheme.match(x['url']): continue if not x['root'].endswith('/'): x['root'] += '/' occur_pattern = '''["|']({0}.*?)["|']''' occurences = re.findall(occur_pattern.format(x['url']), content) occurences = list(set(occurences)) # Remove dups for occur in occurences: content = content.replace(occur, pathname2fileurl(x['root']) + occur[len(x['url']):]) return content
If text is matched with pattern return variable names specified ( % { pattern: variable name } ) in pattern and their corresponding values. If not matched return None. custom patterns can be passed in by custom_patterns ( pattern name pattern regular expression pair ) or custom_patterns_dir.
def match(self, text): """If text is matched with pattern, return variable names specified(%{pattern:variable name}) in pattern and their corresponding values.If not matched, return None. custom patterns can be passed in by custom_patterns(pattern name, pattern regular expression pair) or custom_patterns_dir. """ match_obj = None if self.fullmatch: match_obj = self.regex_obj.fullmatch(text) else: match_obj = self.regex_obj.search(text) if match_obj == None: return None matches = match_obj.groupdict() for key,match in matches.items(): try: if self.type_mapper[key] == 'int': matches[key] = int(match) if self.type_mapper[key] == 'float': matches[key] = float(match) except (TypeError, KeyError) as e: pass return matches
Sets defaults for class Meta declarations.
def configure(module=None, prefix='MONGODB_', **kwargs): """Sets defaults for ``class Meta`` declarations. Arguments can either be extracted from a `module` (in that case all attributes starting from `prefix` are used): >>> import foo >>> configure(foo) or passed explicictly as keyword arguments: >>> configure(database='foo') .. warning:: Current implementation is by no means thread-safe -- use it wisely. """ if module is not None and isinstance(module, types.ModuleType): # Search module for MONGODB_* attributes and converting them # to _Options' values, ex: MONGODB_PORT ==> port. attrs = ((attr.replace(prefix, '').lower(), value) for attr, value in vars(module).items() if attr.startswith(prefix)) _Options._configure(**dict(attrs)) elif kwargs: _Options._configure(**kwargs)
Updates class - level defaults for: class: _Options container.
def _configure(cls, **defaults): """Updates class-level defaults for :class:`_Options` container.""" for attr in defaults: setattr(cls, attr, defaults[attr])
Converts a given string from CamelCase to under_score.
def to_underscore(string): """Converts a given string from CamelCase to under_score. >>> to_underscore('FooBar') 'foo_bar' """ new_string = re.sub(r'([A-Z]+)([A-Z][a-z])', r'\1_\2', string) new_string = re.sub(r'([a-z\d])([A-Z])', r'\1_\2', new_string) return new_string.lower()
Builds all indices listed in model s Meta class.
def auto_index(mcs): """Builds all indices, listed in model's Meta class. >>> class SomeModel(Model) ... class Meta: ... indices = ( ... Index('foo'), ... ) .. note:: this will result in calls to :meth:`pymongo.collection.Collection.ensure_index` method at import time, so import all your models up front. """ for index in mcs._meta.indices: index.ensure(mcs.collection)
Same as: meth: pymongo. collection. Collection. find except it returns the right document class.
def find(self, *args, **kwargs): """Same as :meth:`pymongo.collection.Collection.find`, except it returns the right document class. """ return Cursor(self, *args, wrap=self.document_class, **kwargs)
Same as: meth: pymongo. collection. Collection. find_one except it returns the right document class.
def find_one(self, *args, **kwargs): """Same as :meth:`pymongo.collection.Collection.find_one`, except it returns the right document class. """ data = super(Collection, self).find_one(*args, **kwargs) if data: return self.document_class(data) return None
Load and parse a. csv file
def parse_file(self, file_path, currency) -> List[PriceModel]: """ Load and parse a .csv file """ # load file # read csv into memory? contents = self.load_file(file_path) prices = [] # parse price elements for line in contents: price = self.parse_line(line) assert isinstance(price, PriceModel) price.currency = currency prices.append(price) return prices
Loads the content of the text file
def load_file(self, file_path) -> List[str]: """ Loads the content of the text file """ content = [] content = read_lines_from_file(file_path) return content
Parse a CSV line into a price element
def parse_line(self, line: str) -> PriceModel: """ Parse a CSV line into a price element """ line = line.rstrip() parts = line.split(',') result = PriceModel() # symbol result.symbol = self.translate_symbol(parts[0]) # value result.value = Decimal(parts[1]) # date date_str = parts[2] date_str = date_str.replace('"', '') date_parts = date_str.split('/') year_str = date_parts[2] month_str = date_parts[1] day_str = date_parts[0] logging.debug(f"parsing {date_parts} into date") result.datetime = datetime(int(year_str), int(month_str), int(day_str)) return result
translate the incoming symbol into locally - used
def translate_symbol(self, in_symbol: str) -> str: """ translate the incoming symbol into locally-used """ # read all mappings from the db if not self.symbol_maps: self.__load_symbol_maps() # translate the incoming symbol result = self.symbol_maps[in_symbol] if in_symbol in self.symbol_maps else in_symbol return result
Loads all symbol maps from db
def __load_symbol_maps(self): """ Loads all symbol maps from db """ repo = SymbolMapRepository(self.__get_session()) all_maps = repo.get_all() self.symbol_maps = {} for item in all_maps: self.symbol_maps[item.in_symbol] = item.out_symbol
Reuses the same db session
def __get_session(self): """ Reuses the same db session """ if not self.session: self.session = dal.get_default_session() return self.session
Add individual price
def add(symbol: str, date, value, currency: str): """ Add individual price """ symbol = symbol.upper() currency = currency.upper() app = PriceDbApplication() price = PriceModel() # security = SecuritySymbol("", "") price.symbol.parse(symbol) # price.symbol.mnemonic = price.symbol.mnemonic.upper() # date_str = f"{date}" # date_format = "%Y-%m-%d" # if time: # date_str = f"{date_str}T{time}" # date_format += "T%H:%M:%S" # datum.from_iso_date_string(date) # price.datetime = datetime.strptime(date_str, date_format) price.datum.from_iso_date_string(date) price.value = Decimal(value) price.currency = currency app.add_price(price) app.save() click.echo("Price added.")
Import prices from CSV file
def import_csv(filepath: str, currency: str): """ Import prices from CSV file """ logger.debug(f"currency = {currency}") # auto-convert to uppercase. currency = currency.upper() app = PriceDbApplication() app.logger = logger app.import_prices(filepath, currency)
displays last price for symbol if provided
def last(symbol: str): """ displays last price, for symbol if provided """ app = PriceDbApplication() # convert to uppercase if symbol: symbol = symbol.upper() # extract namespace sec_symbol = SecuritySymbol("", "") sec_symbol.parse(symbol) latest = app.get_latest_price(sec_symbol) assert isinstance(latest, PriceModel) print(f"{latest}") else: # Show the latest prices available for all securities. latest = app.get_latest_prices() for price in latest: print(f"{price}")
Display all prices
def list_prices(date, currency, last): """ Display all prices """ app = PriceDbApplication() app.logger = logger if last: # fetch only the last prices prices = app.get_latest_prices() else: prices = app.get_prices(date, currency) for price in prices: print(price) print(f"{len(prices)} records found.")
Download the latest prices
def download(ctx, help: bool, symbol: str, namespace: str, agent: str, currency: str): """ Download the latest prices """ if help: click.echo(ctx.get_help()) ctx.exit() app = PriceDbApplication() app.logger = logger if currency: currency = currency.strip() currency = currency.upper() # Otherwise download the prices for securities listed in the database. app.download_prices(currency=currency, agent=agent, symbol=symbol, namespace=namespace)
Delete old prices leaving just the last.
def prune(symbol: str, all: str): """ Delete old prices, leaving just the last. """ app = PriceDbApplication() app.logger = logger count = 0 if symbol is not None: sec_symbol = SecuritySymbol("", "") sec_symbol.parse(symbol) deleted = app.prune(sec_symbol) if deleted: count = 1 else: count = app.prune_all() print(f"Removed {count} old price entries.")
Return the default session. The path is read from the default config.
def get_default_session(): """ Return the default session. The path is read from the default config. """ from .config import Config, ConfigKeys db_path = Config().get(ConfigKeys.price_database) if not db_path: raise ValueError("Price database not set in the configuration file!") return get_session(db_path)
Creates a symbol mapping
def add_map(incoming, outgoing): """ Creates a symbol mapping """ db_path = Config().get(ConfigKeys.pricedb_path) session = get_session(db_path) new_map = SymbolMap() new_map.in_symbol = incoming new_map.out_symbol = outgoing session.add(new_map) session.commit() click.echo("Record saved.")
Displays all symbol maps
def list_maps(): """ Displays all symbol maps """ db_path = Config().get(ConfigKeys.price_database) session = get_session(db_path) maps = session.query(SymbolMap).all() for item in maps: click.echo(item)
Finds the map by in - symbol
def get_by_id(self, symbol: str) -> SymbolMap: """ Finds the map by in-symbol """ return self.query.filter(SymbolMap.in_symbol == symbol).first()
Read text lines from a file
def read_lines_from_file(file_path: str) -> List[str]: """ Read text lines from a file """ # check if the file exists? with open(file_path) as csv_file: content = csv_file.readlines() return content
Map the price entity
def map_entity(self, entity: dal.Price) -> PriceModel: """ Map the price entity """ if not entity: return None result = PriceModel() result.currency = entity.currency # date/time dt_string = entity.date format_string = "%Y-%m-%d" if entity.time: dt_string += f"T{entity.time}" format_string += "T%H:%M:%S" price_datetime = datetime.strptime(dt_string, format_string) result.datum = Datum() result.datum.from_datetime(price_datetime) assert isinstance(result.datum, Datum) #result.namespace = entity.namespace #result.symbol = entity.symbol result.symbol = SecuritySymbol(entity.namespace, entity.symbol) # Value value = Decimal(entity.value) / Decimal(entity.denom) result.value = Decimal(value) return result
Parse into the Price entity ready for saving
def map_model(self, model: PriceModel) -> Price: """ Parse into the Price entity, ready for saving """ # assert isinstance(model, PriceModel) assert isinstance(model.symbol, SecuritySymbol) assert isinstance(model.datum, Datum) entity = Price() # Format date as ISO string date_iso = f"{model.datum.value.year}-{model.datum.value.month:02d}-{model.datum.value.day:02d}" entity.date = date_iso entity.time = f"{model.datum.value.hour:02d}:{model.datum.value.minute:02d}:{model.datum.value.second:02d}" # Symbol # properly mapped symbols have a namespace, except for the US markets # TODO check this with .csv import if model.symbol.namespace: entity.namespace = model.symbol.namespace.upper() entity.symbol = model.symbol.mnemonic.upper() assert isinstance(model.value, Decimal) # Find number of decimal places dec_places = abs(model.value.as_tuple().exponent) entity.denom = 10 ** dec_places # Price value entity.value = int(model.value * entity.denom) # Currency entity.currency = model.currency.upper() # self.logger.debug(f"{entity}") return entity
Read the config file
def __read_config(self, file_path: str): """ Read the config file """ if not os.path.exists(file_path): raise FileNotFoundError(f"File path not found: {file_path}") # check if file exists if not os.path.isfile(file_path): self.logger.error(f"file not found: {file_path}") raise FileNotFoundError(f"configuration file not found {file_path}") self.config.read(file_path)
gets the default config path from resources
def __get_config_template_path(self) -> str: """ gets the default config path from resources """ filename = resource_filename( Requirement.parse(package_name), template_path + config_filename) return filename
Copy the config template into user s directory
def __create_user_config(self): """ Copy the config template into user's directory """ src_path = self.__get_config_template_path() src = os.path.abspath(src_path) if not os.path.exists(src): message = f"Config template not found {src}" self.logger.error(message) raise FileNotFoundError(message) dst = os.path.abspath(self.get_config_path()) shutil.copyfile(src, dst) if not os.path.exists(dst): raise FileNotFoundError("Config file could not be copied to user dir!")
Returns the path where the active config file is expected. This is the user s profile folder.
def get_config_path(self) -> str: """ Returns the path where the active config file is expected. This is the user's profile folder. """ dst_dir = self.__get_user_path() dst = dst_dir + "/" + config_filename return dst
Reads the contents of the config file
def get_contents(self) -> str: """ Reads the contents of the config file """ content = None # with open(file_path) as cfg_file: # contents = cfg_file.read() # Dump the current contents into an in-memory file. in_memory = io.StringIO("") self.config.write(in_memory) in_memory.seek(0) content = in_memory.read() # log(DEBUG, "config content: %s", content) in_memory.close() return content
Sets a value in config
def set(self, option: ConfigKeys, value): """ Sets a value in config """ assert isinstance(option, ConfigKeys) # As currently we only have 1 section. section = SECTION self.config.set(section, option.name, value) self.save()
Retrieves a config value
def get(self, option: ConfigKeys): """ Retrieves a config value """ assert isinstance(option, ConfigKeys) # Currently only one section is used section = SECTION return self.config.get(section, option.name)
Save the config file
def save(self): """ Save the config file """ file_path = self.get_config_path() contents = self.get_contents() with open(file_path, mode='w') as cfg_file: cfg_file.write(contents)
Splits the symbol into namespace symbol tuple
def parse(self, symbol: str) -> (str, str): """ Splits the symbol into namespace, symbol tuple """ symbol_parts = symbol.split(":") namespace = None mnemonic = symbol if len(symbol_parts) > 1: namespace = symbol_parts[0] mnemonic = symbol_parts[1] self.namespace = namespace self.mnemonic = mnemonic return namespace, mnemonic