code
stringlengths
52
7.75k
docs
stringlengths
1
5.85k
def strip_accents(s): nfkd = unicodedata.normalize('NFKD', unicode(s)) return u''.join(ch for ch in nfkd if not unicodedata.combining(ch))
Strip accents to prepare for slugification.
def slugify(s): s = strip_accents(s.replace("'", '').lower()) return re.sub('[^a-z0-9]+', ' ', s).strip().replace(' ', '-')
Converts the given string to a URL slug.
def _legacy_status(stat): # 2d0c00002a0000 if stat[:2] == '30' or stat[:2] == '47': # RX1 CT ooo = stat[4:5] # console.log("legstat. " + o); if ooo == '0': return 0 if ooo == '8': return 100 if stat == '7e': return 0 if stat == '7f': ...
Legacy status method from the 'qsmobile.js' library. Pass in the 'val' from &devices or the 'data' received after calling a specific ID.
def decode_qwikcord(packet, channel=1): val = str(packet.get('val', '')) if len(val) != 16: return None if channel == 1: return int(val[6:12], 16) # CTavg return int(val[12:], 16)
Extract the qwikcord current measurements from val (CTavg, CTsum).
def decode_door(packet, channel=1): val = str(packet.get(QSDATA, '')) if len(val) == 6 and val.startswith('46') and channel == 1: return val[-1] == '0' return None
Decode a door sensor.
def decode_imod(packet, channel=1): val = str(packet.get(QSDATA, '')) if len(val) == 8 and val.startswith('4e'): try: _map = ((5, 1), (5, 2), (5, 4), (4, 1), (5, 1), (5, 2))[ channel - 1] return (int(val[_map[0]], 16) & _map[1]) == 0 except IndexError...
Decode an 4 channel imod. May support 6 channels.
def decode_pir(packet, channel=1): val = str(packet.get(QSDATA, '')) if len(val) == 8 and val.startswith('0f') and channel == 1: return int(val[-4:], 16) > 0 return None
Decode a PIR.
def decode_temperature(packet, channel=1): val = str(packet.get(QSDATA, '')) if len(val) == 12 and val.startswith('34') and channel == 1: temperature = int(val[-4:], 16) return round(float((-46.85 + (175.72 * (temperature / pow(2, 16)))))) return None
Decode the temperature.
def decode_humidity(packet, channel=1): val = str(packet.get(QSDATA, '')) if len(val) == 12 and val.startswith('34') and channel == 1: humidity = int(val[4:-4], 16) return round(float(-6 + (125 * (humidity / pow(2, 16))))) return None
Decode the humidity.
def set_value(self, qsid, new): # Set value & encode new to be passed to QSUSB try: dev = self[qsid] except KeyError: raise KeyError("Device {} not found".format(qsid)) if new < 0: new = 0 if new == dev.value: return ...
Set a value.
def update_devices(self, devices): for qspacket in devices: try: qsid = qspacket[QS_ID] except KeyError: _LOGGER.debug("Device without ID: %s", qspacket) continue if qsid not in self: self[qsid] = QSDev...
Update values from response of URL_DEVICES, callback if changed.
def geist_replay(wrapped, instance, args, kwargs): path_parts = [] file_parts = [] if hasattr(wrapped, '__module__'): module = wrapped.__module__ module_file = sys.modules[module].__file__ root, _file = os.path.split(module_file) path_parts.append(root) _file, _...
Wraps a test of other function and injects a Geist GUI which will enable replay (set environment variable GEIST_REPLAY_MODE to 'record' to active record mode.
def reverse_cyk_transforms(root): # type: (Nonterminal) -> Nonterminal root = InverseContextFree.transform_from_chomsky_normal_form(root) root = InverseContextFree.unit_rules_restore(root) root = InverseContextFree.epsilon_rules_restore(root) return root
Reverse transformation made to grammar before CYK. Performs following steps: - transform from chomsky normal form - restore unit rules - restore epsilon rules :param root: Root node of the parsed tree. :return: Restored parsed tree.
def _geom_solve_p_from_mu(mu, b): def p_eq(x, mu, b): x, mu, b = Decimal(x), Decimal(mu), Decimal(b) return ( (x / (1 - x)) - ((b + 1) / (x**-b - 1)) - mu ) # x here is the param raised to the k_agg power, or 1 - p return 1 - optim.brentq(p_eq, 1e-16, 100, args=(mu, b), disp=True)
For the geom_uptrunc, given mu and b, return p. Ref: Harte 2011, Oxford U Press. Eq. 7.50.
def _nbinom_ztrunc_p(mu, k_agg): p_eq = lambda p, mu, k_agg: (k_agg * p) / (1 - (1 + p)**-k_agg) - mu # The upper bound needs to be large. p will increase with increasing mu # and decreasing k_agg p = optim.brentq(p_eq, 1e-10, 1e10, args=(mu, k_agg)) return p
Calculates p parameter for truncated negative binomial Function given in Sampford 1955, equation 4 Note that omega = 1 / 1 + p in Sampford
def _ln_choose(n, k_agg): ''' log binomial coefficient with extended gamma factorials. n and k_agg may be int or array - if both array, must be the same length. ''' gammaln = special.gammaln return gammaln(n + 1) - (gammaln(k_agg + 1) + gammaln(n - k_agg + 1)f _ln_choose(n, k_agg): ''' ...
log binomial coefficient with extended gamma factorials. n and k_agg may be int or array - if both array, must be the same length.
def _solve_k_from_mu(data, k_array, nll, *args): # TODO: See if a root finder like fminbound would work with Decimal used in # logpmf method (will this work with arrays?) nll_array = np.zeros(len(k_array)) for i in range(len(k_array)): nll_array[i] = nll(data, k_array[i], *args) min_...
For given args, return k_agg from searching some k_range. Parameters ---------- data : array k_range : array nll : function args : Returns -------- :float Minimum k_agg
def _trunc_logser_solver(bins, b): if bins == b: p = 0 else: BOUNDS = [0, 1] DIST_FROM_BOUND = 10 ** -15 m = np.array(np.arange(1, np.int(b) + 1)) y = lambda x: np.sum(x ** m / b * bins) - np.sum((x ** m) / m) p = optim.bisect(y, BOUNDS[0] + DIST_FROM_BOUND...
Given bins (S) and b (N) solve for MLE of truncated logseries parameter p Parameters ----------- bins : float Number of bins. Considered S in an ecological context b : float Upper truncation of distribution. Considered N in an ecological context Returns ------- : float ...
def _expon_solve_lam_from_mu(mu, b): def lam_eq(lam, mu, b): # Small offset added to denominator to avoid 0/0 erors lam, mu, b = Decimal(lam), Decimal(mu), Decimal(b) return ( (1 - (lam*b + 1) * np.exp(-lam*b)) / (lam - lam * np.exp(-lam*b) + Decimal(1e-32)) - mu ) ...
For the expon_uptrunc, given mu and b, return lam. Similar to geom_uptrunc
def _make_rank(dist_obj, n, mu, sigma, crit=0.5, upper=10000, xtol=1): qs = (np.arange(1, n + 1) - 0.5) / n rank = np.empty(len(qs)) brute_ppf = lambda val, prob: prob - dist_obj.cdf(val, mu, sigma) qs_less = qs <= crit ind = np.sum(qs_less) # Use ppf if qs are below crit rank[qs_les...
Make rank distribution using both ppf and brute force. Setting crit = 1 is equivalent to just using the ppf Parameters ---------- {0}
def _mean_var(vals, pmf): mean = np.sum(vals * pmf) var = np.sum(vals ** 2 * pmf) - mean ** 2 return mean, var
Calculates the mean and variance from vals and pmf Parameters ---------- vals : ndarray Value range for a distribution pmf : ndarray pmf values corresponding with vals Returns ------- : tuple (mean, variance)
def rank(self, n, *args): return self.ppf((np.arange(1, n+1) - 0.5) / n, *args)
{0}
def rvs_alt(self, *args, **kwargs): l = kwargs.get('l', 1) b = kwargs.get('b', 1e5) size = kwargs.get('size', 1) model_cdf = self.cdf(np.arange(l, b + 1), *args) unif_rands = np.random.random(size) model_rands = np.array([np.where(tx <= model_cdf)[0][0] + l ...
{0}
def fit_mle(self, data, b=None): # Take mean of data as MLE of distribution mean, then calculate p mu = np.mean(data) if not b: b = np.sum(data) p = _geom_solve_p_from_mu_vect(mu, b) # Just return float, not len 1 array if len(np.atleast_1d(p)) == 1:...
%(super)s In addition to data, requires ``b``, the upper limit of the distribution.
def fit_mle(self, data, init_vals=(80, 80)): if len(data) > 1: mu = np.mean(data) var = np.var(data) theta0 = var / mu alpha0 = mu / theta0 else: alpha0 = init_vals[0] theta0 = init_vals[1] def mle(params): ...
%(super)s In addition to data, can take init_vals which allows the user to specify initial values for (alpha, theta) during the optimization.
def fit_mle(self, data, k_array=np.arange(0.1, 100, 0.1)): # todo: check and mention in docstring biases of mle for k_agg data = np.array(data) mu = np.mean(data) return mu, _solve_k_from_mu(data, k_array, nbinom_nll, mu)
%(super)s In addition to data, gives an optional keyword argument k_array containing the values to search for k_agg. A brute force search is then used to find the parameter k_agg.
def translate_args(self, mu, k_agg, return_p=False): if return_p: return nbinom_ztrunc_p(mu, k_agg), k_agg else: return mu, k_agg
%(super)s The keyword argument return_p computes the p values used to define the the truncated negative binomial
def fit_mle(self, data, k_agg0=0.5): mu = np.mean(data) def mle(k): return -np.sum(np.log(self.pmf(data, mu, k))) k = optim.fmin(mle, x0=k_agg0, disp=0) return mu, k[0]
%(super)s In addition to data, gives an optional keyword argument k_agg0 that specifies the initial value of k_agg used in the optimization.
def fit_mle(self, data, b=None): data = np.array(data) length = len(data) if not b: b = np.sum(data) return _trunc_logser_solver(length, b), b
%(super)s b : float The upper bound of the distribution. If None, fixed at sum(data)
def rank(self, n, mu, sigma, crit=.5, upper=10000, xtol=1): return _make_rank(self, n, mu, sigma, crit=crit, upper=upper, xtol=xtol)
%(super)s Additional Parameters ---------------------- {0}
def fit_mle(self, data, b=None): # Take mean of data as MLE of distribution mean, then calculate p mu = np.mean(data) if not b: b = np.sum(data) lam = _expon_solve_lam_from_mu_vect(mu, b) # Just return float, not len 1 array if len(np.atleast_1d(lam)...
%(super)s Additional Parameters ---------------------- b : float The upper limit of the distribution
def fit_mle(self, data, fix_mean=False): if not fix_mean: sigma, _, scale = stats.lognorm.fit(data, floc=0) return np.log(scale), sigma else: mean = np.mean(data) # MLE fxn to be optmimized mle = lambda sigma, x, mean: -1 *\ ...
%(super)s Additional Parameters ---------------------- fix_mean : bool Default False. If True, fixes mean before optimizing sigma
def _pdf_w_mean(self, x, mean, sigma): # Lognorm pmf with mean for optimization mu, sigma = self.translate_args(mean, sigma) return self.logpdf(x, mu, sigma)
Calculates the pdf of a lognormal distribution with parameters mean and sigma Parameters ---------- mean : float or ndarray Mean of the lognormal distribution sigma : float or ndarray Sigma parameter of the lognormal distribution Returns ...
def union_join(left, right, left_as='left', right_as='right'): attrs = {} attrs.update(get_object_attrs(right)) attrs.update(get_object_attrs(left)) attrs[left_as] = left attrs[right_as] = right if isinstance(left, dict) and isinstance(right, dict): return attrs else: jo...
Join function truest to the SQL style join. Merges both objects together in a sum-type, saving references to each parent in ``left`` and ``right`` attributes. >>> Dog = namedtuple('Dog', ['name', 'woof', 'weight']) >>> dog = Dog('gatsby', 'Ruff!', 15) >>> Cat = namedtuple('Cat', ['name', '...
def removeKeyButtonEvent(self, buttons= [] ): for i in range( 0, len(buttons)-1 ): GPIO.remove_event_detect( buttons[i] )
! \~english Remove key button event callbacks @param buttons: an array of button Ids. eg. [ 12,13,15, ...] \~chinese 移除按键事件回调 @param buttons: 按钮ID数组。 例如: [12,13,15,...]
def configKeyButtons( self, enableButtons = [], bounceTime = DEF_BOUNCE_TIME_NORMAL, pullUpDown = GPIO.PUD_UP, event = GPIO.BOTH ): for key in enableButtons: self.setKeyButton( key["id"], key["callback"], bounceTime, pullUpDown, event ) pass
! \~english Config multi key buttons IO and event on same time @param enableButtons: an array of key button configs. eg. <br> [{ "id":BUTTON_ACT_A, "callback": aCallbackFun }, ... ] @param bounceTime: Default set to DEF_BOUNCE_TIME_NORMAL @param pullUpDown: Defau...
def best_match_from_list(item,options,fuzzy=90,fname_match=True,fuzzy_fragment=None,guess=False): '''Returns the best match from :meth:`matches_from_list` or ``None`` if no good matches''' matches = matches_from_list(item,options,fuzzy,fname_match,fuzzy_fragment,guess) if len(matches)>0: return matc...
Returns the best match from :meth:`matches_from_list` or ``None`` if no good matches
def best_item_from_list(item,options,fuzzy=90,fname_match=True,fuzzy_fragment=None,guess=False): '''Returns just the best item, or ``None``''' match = best_match_from_list(item,options,fuzzy,fname_match,fuzzy_fragment,guess) if match: return match[0] return Nonf best_item_from_list(item,options,...
Returns just the best item, or ``None``
async def create_hlk_sw16_connection(port=None, host=None, disconnect_callback=None, reconnect_callback=None, loop=None, logger=None, timeout=None, reconnect_interval=None)...
Create HLK-SW16 Client class.
def _reset_timeout(self): if self._timeout: self._timeout.cancel() self._timeout = self.loop.call_later(self.client.timeout, self.transport.close)
Reset timeout for date keep alive.
def reset_cmd_timeout(self): if self._cmd_timeout: self._cmd_timeout.cancel() self._cmd_timeout = self.loop.call_later(self.client.timeout, self.transport.close)
Reset timeout for command execution.
def _handle_lines(self): while b'\xdd' in self._buffer: linebuf, self._buffer = self._buffer.rsplit(b'\xdd', 1) line = linebuf[-19:] self._buffer += linebuf[:-19] if self._valid_packet(line): self._handle_raw_packet(line) else:...
Assemble incoming data into per-line packets.
def _valid_packet(raw_packet): if raw_packet[0:1] != b'\xcc': return False if len(raw_packet) != 19: return False checksum = 0 for i in range(1, 17): checksum += raw_packet[i] if checksum != raw_packet[18]: return False ...
Validate incoming packet.
def send_packet(self): waiter, packet = self.client.waiters.popleft() self.logger.debug('sending packet: %s', binascii.hexlify(packet)) self.client.active_transaction = waiter self.client.in_transaction = True self.client.active_packet = packet self.reset_cmd_tim...
Write next packet in send queue.
def format_packet(command): frame_header = b"\xaa" verify = b"\x0b" send_delim = b"\xbb" return frame_header + command.ljust(17, b"\x00") + verify + send_delim
Format packet to be sent.
def connection_lost(self, exc): if exc: self.logger.error('disconnected due to error') else: self.logger.info('disconnected because of close/abort.') if self.disconnect_callback: asyncio.ensure_future(self.disconnect_callback(), loop=self.loop)
Log when connection is closed, if needed call callback.
async def setup(self): while True: fut = self.loop.create_connection( lambda: SW16Protocol( self, disconnect_callback=self.handle_disconnect_callback, loop=self.loop, logger=self.logger), host=self.h...
Set up the connection with automatic retry.
def stop(self): self.reconnect = False self.logger.debug("Shutting down.") if self.transport: self.transport.close()
Shut down transport.
async def handle_disconnect_callback(self): self.is_connected = False if self.disconnect_callback: self.disconnect_callback() if self.reconnect: self.logger.debug("Protocol disconnected...reconnecting") await self.setup() self.protocol.res...
Reconnect automatically unless stopping.
def register_status_callback(self, callback, switch): if self.status_callbacks.get(switch, None) is None: self.status_callbacks[switch] = [] self.status_callbacks[switch].append(callback)
Register a callback which will fire when state changes.
def _send(self, packet): fut = self.loop.create_future() self.waiters.append((fut, packet)) if self.waiters and self.in_transaction is False: self.protocol.send_packet() return fut
Add packet to send queue.
async def turn_on(self, switch=None): if switch is not None: switch = codecs.decode(switch.rjust(2, '0'), 'hex') packet = self.protocol.format_packet(b"\x10" + switch + b"\x01") else: packet = self.protocol.format_packet(b"\x0a") states = await self._...
Turn on relay.
async def turn_off(self, switch=None): if switch is not None: switch = codecs.decode(switch.rjust(2, '0'), 'hex') packet = self.protocol.format_packet(b"\x10" + switch + b"\x02") else: packet = self.protocol.format_packet(b"\x0b") states = await self....
Turn off relay.
async def status(self, switch=None): if switch is not None: if self.waiters or self.in_transaction: fut = self.loop.create_future() self.status_waiters.append(fut) states = await fut state = states[switch] else: ...
Get current relay status.
def value(self, item): # type: (Any) -> Any if isinstance(item, weakref.ReferenceType): if item() is None: raise TreeDeletedException() return item() return item
Return value stored in weakref. :param item: Object from which get the value. :return: Value stored in the weakref, otherwise original value. :raise TreeDeletedException: when weakref is already deleted.
def remove_all(self, item): # type: (Any) -> None item = self.ref(item) while list.__contains__(self, item): list.remove(self, item)
Remove all occurrence of the parameter. :param item: Value to delete from the WeakList.
def index(self, item, **kwargs): # type: (Any, dict) -> int return list.index(self, self.ref(item), **kwargs)
Get index of the parameter. :param item: Item for which get the index. :return: Index of the parameter in the WeakList.
def insert(self, index, item): # type: (int, Any) -> None return list.insert(self, index, self.ref(item))
Insert item at the specific index. :param index: Index where to insert the item. :param item: Item to insert.
def sort(self, *, key: Optional[Callable[[Any], Any]] = None, reverse: bool = False) -> None: return list.sort(self, key=self._sort_key(key), reverse=reverse)
Sort _WeakList. :param key: Key by which to sort, default None. :param reverse: True if return reversed WeakList, false by default.
def get_feed(self, datasource_id): info = self.__metadb.one( """ SELECT to_json(ds) as datasource , to_json(fc) as connector , to_json(fct) as connector_type , to_json(ctp) as connector_type_preset , json_build_...
Получение настроек для фида :param datasource_id: идентификатор фида :return: FeedDataSource
def get_data(self, datasource, callback): task = self.__app.worker.current_task media_metadata = datasource.connector_type_preset['preset_data']['media_metadata'] result_data = task['result_data'] tmp_file = NamedTemporaryFile(delete=False, suffix=SOURCE_FORMAT_EXTENSION.get(med...
Сохранение медиафайла :param task: :param media_metadata: :param file_suffix: :param callback: :return:
def datasource_process(self, datasource_id): # TODO Выпилить потом класс используется для другого # TODO без applicationId не выбираются поля сущностей. Подумать на сколько это НЕ нормально response = self.__app.native_api_call('feed', 'datasource/' + datasource_id + '/process?applicati...
deprecated Запускает настроенные обработки в фиде :param datasource_id: uuid
def _delta_dir(): repo = Repo() current_dir = os.getcwd() repo_dir = repo.tree().abspath delta_dir = current_dir.replace(repo_dir, '') if delta_dir: return delta_dir + '/' else: return ''
returns the relative path of the current directory to the git repository. This path will be added the 'filename' path to find the file. It current_dir is the git root, this function returns an empty string. Keyword Arguments: <none> Returns: str -- relative path of the current dir ...
def commit(filename): try: repo = Repo() # gitcmd = repo.git # gitcmd.commit(filename) index = repo.index index.commit("Updated file: {0}".format(filename)) except Exception as e: print("exception while commit: %s" % e.message)
Commit (git) a specified file This method does the same than a :: $ git commit -a "message" Keyword Arguments: :filename: (str) -- name of the file to commit Returns: <nothing>
def add_file_to_repo(filename): try: repo = Repo() index = repo.index index.add([_delta_dir() + filename]) except Exception as e: print("exception while gitadding file: %s" % e.message)
Add a file to the git repo This method does the same than a :: $ git add filename Keyword Arguments: :filename: (str) -- name of the file to commit Returns: <nothing>
def reset_to_last_commit(): try: repo = Repo() gitcmd = repo.git gitcmd.reset(hard=True) except Exception: pass
reset a modified file to his last commit status This method does the same than a :: $ git reset --hard Keyword Arguments: <none> Returns: <nothing>
def commit_history(filename): result = [] repo = Repo() for commit in repo.head.commit.iter_parents(paths=_delta_dir() + filename): result.append({'date': datetime.fromtimestamp(commit.committed_date + commit.committer_tz_offs...
Retrieve the commit history for a given filename. Keyword Arguments: :filename: (str) -- full name of the file Returns: list of dicts -- list of commit if the file is not found, returns an empty list
def read_committed_file(gitref, filename): repo = Repo() commitobj = repo.commit(gitref) blob = commitobj.tree[_delta_dir() + filename] return blob.data_stream.read()
Retrieve the content of a file in an old commit and returns it. Ketword Arguments: :gitref: (str) -- full reference of the git commit :filename: (str) -- name (full path) of the file Returns: str -- content of the file
def get(self, key, _else=None): with self._lock: self.expired() # see if everything expired try: value = self._dict[key].get() return value except KeyError: return _else except ValueError: ...
The method to get an assets value
def set(self, key, value, expires=None, future=None): # assert the values above with self._lock: try: self._dict[key].set(value, expires=expires, future=future) except KeyError: self._dict[key] = moment(value, expires=expires, future=futur...
Set a value
def values(self): self.expired() values = [] for key in self._dict.keys(): try: value = self._dict[key].get() values.append(value) except: continue return values
Will only return the current values
def has_key(self, key): if key in self._dict: try: self[key] return True except ValueError: return False except KeyError: return False return False
Does the key exist? This method will check to see if it has expired too.
def unit_rules_restore(root): # type: (Nonterminal) -> Nonterminal items = Traversing.post_order(root) items = filter(lambda x: isinstance(x, ReducedUnitRule), items) for rule in items: parent_nonterm = rule.from_symbols[0] # type: Nonterminal # restore chain of unit rules ...
Transform parsed tree for grammar with removed unit rules. The unit rules will be returned back to the tree. :param root: Root of the parsed tree. :return: Modified tree.
def remaining_bytes(self, meta=True): pos, self._pos = self._pos, len(self.buffer) return self.buffer[pos:]
Returns the remaining, unread bytes from the buffer.
def decode(self, bytes): self.buffer = bytes self._pos = 0 Packet = identifier.get_packet_from_id(self._read_variunt()) # unknown packets will be None from the identifier if Packet is None: return None packet = Packet() packet.ParseFromStr...
Decodes the packet off the byte string.
def encode(self, packet): id = identifier.get_packet_id(packet) if id is None: raise EncoderException('unknown packet') self._write_variunt(id) self._write(packet.SerializeToString()) return bytes(self.buffer)
Pushes a packet to the writer, encoding it on the internal buffer.
def setup_logging(level): logging.root.setLevel(level) logging.root.addHandler(STREAM_HANDLER)
Setup logger.
def create(self, data): # todo: copy-paste code from representation.validate -> refactor if data is None: return None prototype = {} errors = {} # create and populate the prototype for field_name, field_spec in self.spec.fields.items(): ...
Create object from the given data. The given data may or may not have been validated prior to calling this function. This function will try its best in creating the object. If the resulting object cannot be produced, raises ``ValidationError``. The spec can affect how individual fields...
def serialize(self, entity, request=None): def should_we_insert(value, field_spec): return value not in self.missing or field_spec.required errors = {} ret = {} for field_name, field_spec in self.spec.fields.items(): value = self._get_value_for_seriali...
Serialize entity into dictionary. The spec can affect how individual fields will be serialized by implementing ``serialize()`` for the fields needing customization. :returns: dictionary
def _create_value(self, data, name, spec): field = getattr(self, 'create_' + name, None) if field: # this factory has a special creator function for this field return field(data, name, spec) value = data.get(name) return spec.fields[name].clean(value)
Create the value for a field. :param data: the whole data for the entity (all fields). :param name: name of the initialized field. :param spec: spec for the whole entity.
def _get_serialize_func(self, name, spec): func = getattr(self, 'serialize_' + name, None) if func: # this factory has a special serializer function for this field return func func = getattr(spec.fields[name], 'serialize', None) if func: retur...
Return the function that is used for serialization.
def _get_value_for_serialization(self, data, name, spec): name = self.property_name_map[name] return getattr(data, name, None)
Return the value of the field in entity (or ``None``).
def _create_mappings(self, spec): ret = dict(zip(set(spec.fields), set(spec.fields))) ret.update(dict([(n, s.alias) for n, s in spec.fields.items() if s.alias])) return ret
Create property name map based on aliases.
def all_substrings(s): ''' yields all substrings of a string ''' join = ''.join for i in range(1, len(s) + 1): for sub in window(s, i): yield join(subf all_substrings(s): ''' yields all substrings of a string ''' join = ''.join for i in range(1, len(s) + 1): for sub i...
yields all substrings of a string
def equivalent_release_for_product(self, product): releases = self._default_manager.filter( version__startswith=self.major_version() + '.', channel=self.channel, product=product).order_by('-version') if not getattr(settings, 'DEV', False): releases = releases...
Returns the release for a specified product with the same channel and major version with the highest minor version, or None if no such releases exist
def notes(self, public_only=False): tag_index = dict((tag, i) for i, tag in enumerate(Note.TAGS)) notes = self.note_set.order_by('-sort_num', 'created') if public_only: notes = notes.filter(is_public=True) known_issues = [n for n in notes if n.is_known_issue_for(self...
Retrieve a list of Note instances that should be shown for this release, grouped as either new features or known issues, and sorted first by sort_num highest to lowest and then by created date, which is applied to both groups, and then for new features we also sort by tag in the order sp...
def to_dict(self): data = model_to_dict(self, exclude=['id']) data['title'] = unicode(self) data['slug'] = self.slug data['release_date'] = self.release_date.date().isoformat() data['created'] = self.created.isoformat() data['modified'] = self.modified.isoformat(...
Return a dict all all data about the release
def to_simple_dict(self): return { 'version': self.version, 'product': self.product, 'channel': self.channel, 'is_public': self.is_public, 'slug': self.slug, 'title': unicode(self), }
Return a dict of only the basic data about the release
def playTone(self, freq, reps = 1, delay = 0.1, muteDelay = 0.0): if freq == 0: self.stopTone() self._delay(delay) #sleep(delay) return False if self._pwmPlayer == None: self._initPWMPlayer(freq) for r in range(0,reps): self...
! \~english Play a tone \~chinese 播放音符 \~english @param freq @param reps @param delay >= 0(s) if 0 means do not delay. tone play will be Stop immediately <br> @param muteDelay >= 0(s) If 0 means no pause after playing, play the next note immediately \~chi...
def playToneList(self, playList = None): if playList == None: return False for t in playList: self.playTone(t["freq"], t["reps"], t["delay"], t["muteDelay"]) self.stopTone() return True
! \~english Play tone from a tone list @param playList a array of tones \~chinese 播放音调列表 @param playList: 音调数组 \~english @note <b>playList</b> format:\n \~chinese @note <b>playList</b> 格式:\n \~ <pre> [ ...
def all(self, instance): url = self._url.format(instance=instance) response = requests.get(url, **self._default_request_kwargs) data = self._get_response_data(response) return self._concrete_acl_list(data)
Get all ACLs associated with the instance specified by name. :param str instance: The name of the instance from which to fetch ACLs. :returns: A list of :py:class:`Acl` objects associated with the specified instance. :rtype: list
def create(self, instance, cidr_mask, description, **kwargs): # Build up request data. url = self._url.format(instance=instance) request_data = { 'cidr_mask': cidr_mask, 'description': description } request_data.update(kwargs) # Call to c...
Create an ACL entry for the specified instance. :param str instance: The name of the instance to associate the new ACL entry with. :param str cidr_mask: The IPv4 CIDR mask for the new ACL entry. :param str description: A short description for the new ACL entry. :param collector kwargs: ...
def get(self, instance, acl): base_url = self._url.format(instance=instance) url = '{base}{aclid}/'.format(base=base_url, aclid=acl) response = requests.get(url, **self._default_request_kwargs) data = self._get_response_data(response) return self._concrete_acl(data)
Get an ACL by ID belonging to the instance specified by name. :param str instance: The name of the instance from which to fetch the ACL. :param str acl: The ID of the ACL to fetch. :returns: An :py:class:`Acl` object, or None if ACL does not exist. :rtype: :py:class:`Acl`
def delete(self, instance, acl): base_url = self._url.format(instance=instance) url = '{base}{aclid}/'.format(base=base_url, aclid=acl) response = requests.delete(url, **self._default_request_kwargs) if response.status_code == 200: logger.info('Successfully deleted ...
Delete an ACL by ID belonging to the instance specified by name. :param str instance: The name of the instance on which the ACL exists. :param str acll: The ID of the ACL to delete.
def _concrete_acl(self, acl_doc): if not isinstance(acl_doc, dict): return None # Attempt to instantiate an Acl object with the given dict. try: return Acl(document=acl_doc, acls=self) # If construction fails, log the exception and return None. ...
Concretize an ACL document. :param dict acl_doc: A document describing an ACL entry. Should come from the API. :returns: An :py:class:`Acl`, or None. :rtype: :py:class:`bases.BaseInstance`
def _concrete_acl_list(self, acl_docs): if not acl_docs: return [] return list(filter(None, [self._concrete_acl(acl_doc=doc) for doc in acl_docs]))
Concretize a list of ACL documents. :param list acl_docs: A list of ACL documents. Should come from the API. :returns: A list of :py:class:`ACL` objects. :rtype: list
def _default_request_kwargs(self): defaults = copy.deepcopy(super(Acls, self)._default_request_kwargs) defaults.setdefault('headers', {}).update({ 'X-Auth-Token': self._client.auth._token }) return defaults
The default request keyword arguments to be passed to the requests library.
def _url(self): base_url = self._client._url.rstrip('/') return '{}/instances/{}/acls/{}/'.format(base_url, self.instance_name, self.id)
The URL of this ACL object.
def first(pipe, items=1): ''' first is essentially the next() function except it's second argument determines how many of the first items you want. If items is more than 1 the output is an islice of the generator. If items is 1, the first item is returned ''' pipe = iter(pipe) re...
first is essentially the next() function except it's second argument determines how many of the first items you want. If items is more than 1 the output is an islice of the generator. If items is 1, the first item is returned
def create_state(cls, state, **kwargs): kwargs['_return_http_data_only'] = True if kwargs.get('async'): return cls._create_state_with_http_info(state, **kwargs) else: (data) = cls._create_state_with_http_info(state, **kwargs) return data
Create State Create a new State This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async=True >>> thread = api.create_state(state, async=True) >>> result = thread.get() :param async bool :param State state:...