def append(self, item): if isinstance(item, Collection): self.items.append(item) if item._usage is not None: self._add_to_attr(item._usage._name_.lower(), item) elif isinstance(item, UsagePage): if not [ usage_type for usage_type in item.usage_types if usage_type in self._usage_types ]: raise ValueError() collection = Collection(usage=item) self.items.append(collection) self._add_to_attr(item._usage._name_.lower(), collection) elif isinstance(item, Report): if len(item.usages) > 0: for usage in item.usages: self._add_to_attr( usage._name_.lower(), property(fget=_partial(item.__getitem__, item.usages.index(usage)), fset=_partial(item.__setitem__, item.usages.index(usage)))) self.items.append(item) else: raise ValueError("usage type is not UsagePage or Report")
def ae_code_replace(node): #print('ae_code_replace: ' + node) ctl = cmds.setParent(q=True) + '|codeEd' cmds.scrollField(ctl, e=True, tx=getCode(node), cc=_partial(_ae_setCode, ctl, node)) cmds.scriptJob(p=ctl, rp=True, ac=(node + '.cd', _partial(_ae_codeChange, ctl, node)))
def __init__(self, *combinants, iterop, asiter = False, **kwargs): self.combinants, self.iterop = combinants, iterop regkw = dict(iterop = iterop) if asiter: self.iter_fn = _partial(iterop, *combinants) regkw['asiter'] = asiter else: self.iter_fn = _partial(iterop, combinants) super().__init__(**kwargs) self.register_argskwargs(*combinants, iterop = iterop) # pylint: disable=E1101
def __init__(self, *args, **kwargs): slc, start, stop, step, startinf, stopinf = self.analyse_range_args( *args) if isinstance(step, str): self.iter_fn = _partial(rand_float_range, start, stop, step) self.iterlen = _special.infint elif not startinf: self.iter_fn = _partial(real_range, start, stop, step) if not stopinf: self.iterlen = int(abs(stop - start) // step) + 1 super().__init__(slc, start, stop, step, startinf, stopinf, **kwargs)
def ae_code_new(node): #print('ae_code_new: ' + node) if not cmds.uiTemplate('Exprespy', ex=True): cmds.uiTemplate('Exprespy') cmds.setUITemplate('Exprespy', pushTemplate=True) ctl = cmds.scrollField('codeEd', h=500) cmds.setUITemplate(popTemplate=True) cmds.scrollField(ctl, e=True, tx=getCode(node), cc=_partial(_ae_setCode, ctl, node)) cmds.scriptJob(p=ctl, rp=True, ac=(node + '.cd', _partial(_ae_codeChange, ctl, node)))
def __init__(self, source, selection, **kwargs): if isinstance(selection, tuple): selection = list(selection) selection = type(self)[selection] # pylint: disable=E1136 seltyp = selection.typ if issubclass(seltyp, bool): self.iter_fn = _partial(_itertools.compress, source, selection) self.iterlen = measure_boolean_selection(source, selection) elif issubclass(seltyp, int): self.iter_fn = _partial(selinds_iter, source, selection) self.iterlen = selection.iterlen else: raise TypeError( f"Only integral or boolean selections accepted, not {seltyp}") super().__init__(source, selection, **kwargs)
def _define_dict_for_write(self): map_ = { # 'DevEnbl': _partial(self._set_simple, 'DevEnbl'), # 'EVGDevEnbl': _partial(self._set_simple, 'EVGDevEnbl'), # 'FoutDevEnbl': _partial(self._set_simple, 'FoutDevEnbl'), 'State': _partial(self._set_simple, 'State'), 'Src': self._set_source, 'Duration': self._set_duration, 'Polarity': _partial(self._set_simple, 'Polarity'), 'NrPulses': self._set_nrpulses, 'Delay': _partial(self._set_delay, raw=False), 'DelayRaw': _partial(self._set_delay, raw=True), 'RFDelayType': _partial(self._set_simple, 'RFDelayType'), } return map_
def partial(func: Callable, *args, **kwargs): from functools import partial as _partial return _partial(func, *args, **kwargs) # ---------------------------------------------------------------------------------------------------------------------------------------- #
def get_all_modules(): """ Search all modules on database sorted by order :return: tuple of Module """ lazy_all_modules = _partial(tuple, _Module.objects.order_by('order')) return _cache.get_or_set('ALL_MODULES', lazy_all_modules, _settings.CACHE_TTL)
def partial( func: Callable[..., _ReturnType], *args: Any, **kwargs: Any, ) -> Callable[..., _ReturnType]: """ Typed partial application. It is just a ``functools.partial`` wrapper with better typing support. We use a custom ``mypy`` plugin to make sure types are correct. Otherwise, it is currently impossible to properly type this function. .. code:: python >>> from returns.curry import partial >>> def sum_two_numbers(first: int, second: int) -> int: ... return first + second >>> sum_with_ten = partial(sum_two_numbers, 10) >>> assert sum_with_ten(2) == 12 >>> assert sum_with_ten(-5) == 5 See also: https://docs.python.org/3/library/functools.html#functools.partial """ return _partial(func, *args, **kwargs)
def norm_func(self, *args): # reads in some function and returns its # area-normalized version (as a function object) inst_func = _partial(func, self) ToT = _quad(inst_func, ave_range[0], ave_range[1], args=args[1:])[0] return func(self, *args) / ToT
def total_frame(self, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ): """ Return a matrix of the most recent values: date_time: (True/False) attempt to retrieve a TOSDB_DateTime object labels: (True/False) pull the item and topic labels with the values data_str_max: the maximum length of string data returned label_str_max: the maximum length of label strings returned if labels and date_time are True: returns-> dict of namedtuple of 2tuple if labels is True: returns -> dict of namedtuple if date_time is True: returns -> list of 2tuple else returns-> list """ p = _partial(self.topic_frame, date_time=date_time, labels=labels, data_str_max=data_str_max, label_str_max=label_str_max) if labels: return {x: p(x) for x in self.items()} else: return [p(x) for x in self.items()]
class Slice(_Dimension.Incision): # __slots__ = 'start', 'stop', 'step' def __init__(self, dim, arg0, arg1=None, arg2=None, /, **kwargs): _, start, stop, step = unpack_slice(arg0, arg1, arg2) dimlen = dim.iterlen (start, stop, step), self.iterlen = \ process_index_slice(start, stop, step, dimlen) self.start, self.stop = start, stop if step > 0: self.iter_fn = _partial(_itertools.islice, dim, start, stop, step) else: start = dimlen if start is None else start if start >= _special.inf: raise ValueError("Cannot reverse-slice from infinity.") abstep = abs(step) stop = 0 if stop is None else stop revstop, revstart = stop + abstep - 1, start + 1 content = list(_itertools.islice(dim, revstop, revstart, abstep)) content.reverse() self.iter_fn = content.__iter__ step = None if step == 1 else step self.step = step super().__init__(dim, (start, stop, step), **kwargs)
def total_frame(self, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ): """ Return ALL of the block's most recent values: total_frame(self, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ) date_time :: bool :: include TOSDB_DateTime objects labels :: bool :: include item and topic labels data_str_max :: int :: maximum length of string data returned label_str_max :: int :: maximum length of label strings returned if labels == date_time == True: returns -> dict of namedtuple of 2-tuple** elif labels == True: returns -> dict of namedtuple** elif date_time == True: returns -> list of list of 2-tuple** else: returns -> list of list** **data are ALL of type str (frame can contain topics of different type) throws TOSDB_DataTimeError, TOSDB_CLibError """ p = _partial(self.topic_frame, date_time=date_time, labels=labels, data_str_max=data_str_max, label_str_max=label_str_max) if labels: return {x: p(x) for x in self._items} else: return list(map(p, self._items))
def iter_data(self, rawtokens): """ Generator: for each line, yields a tuple of the decoded tokens """ stop_token = self.KEYWORD_PREFIX + self.END_OF_DATA_NAME empty = self.EMPTY comment_prefix = self.comment_prefix escape_char = self.escape_char str_startswith = str.startswith import re decode_re = re.escape(escape_char) + r"(?:([0123456789abcdefABCDEF]{2})|('')|(.{,2}))" def replacer(match): hex_match, empty_match, bad_match = match.group(1,2,3) if hex_match is not None: assert empty_match is bad_match is None return chr(int(hex_match, 16)) if empty_match is not None: assert hex_match is bad_match is None return empty assert bad_match is not None assert hex_match is empty_match is None raise ValueError("invalid escaping in %s" % (repr(match.string),)) decode = _partial(re.compile(decode_re).sub, replacer) for tokens in rawtokens: assert tokens token0 = tokens[0] if str_startswith(token0, comment_prefix): continue if token0 == stop_token: raise self.StopDataIteration decoded_tokens = tuple(token if escape_char not in token else decode(token) for token in tokens) yield decoded_tokens
def _define_dict_for_read(self): map_ = { 'State': _partial(self._get_simple, 'State'), 'Duration': _partial(self._get_duration_pulses, ''), 'Polarity': _partial(self._get_simple, 'Polarity'), 'NrPulses': _partial(self._get_duration_pulses, ''), 'Delay': _partial(self._get_delay, 'Delay'), 'DelayRaw': _partial(self._get_delay, 'Delay'), 'Src': _partial(self._process_source, ''), 'RFDelayType': _partial(self._get_simple, 'RFDelayType'), 'Status': _partial(self._get_status, ''), } return map_
def simplecurry(*args, **kwargs): r""" >>> from __future__ import print_function >>> show = print |simplecurry(1, 2, sep='/') # show is print with arguments 1, 2 and keyword argument sep='.' >>> show(3) 1/2/3 """ return postfix(lambda function: _partial(function, *args, **kwargs))
class Transform(Derived): def __init__(self, operator, /, *operands, **kwargs): self.operands, self.operator = operands, operator isdim = tuple(isinstance(op, Dimension) for op in operands) nisdim = isdim.count(True) if all(isdim): self.iter_fn = _partial(map, operator, *operands) elif not nisdim: raise ValueError("No dims in input!") else: if nisdim == 1: self.iterlen = operands[isdim.index(True)].iterlen getops = lambda: (op if isinstance(op, Dimension) else _repeat(op) for op in operands) self.iter_fn = _partial(map, operator, *getops()) super().__init__(operator, *operands, **kwargs)
def unique(itms, equiv): """ unique items from a list, according to binary comparison `equiv` """ uniq_itms = [] for itm in itms: if not any(map(_partial(equiv, itm), uniq_itms)): uniq_itms.append(itm) return tuple(uniq_itms)
def _download_image_thread(image_list, param_tuple): # unpack to ensure dst dir (create if missing) before starting threads user, file_name_rule, dst_dir_rule, size, replace = param_tuple dst_dir = dst_dir_from_rule(dst_dir_rule, user) # after checking, pack params again and make thread pool param_tuple = (user, file_name_rule, dst_dir, size, replace) executor = _Pool(max_workers=_cfg.getint('download', 'max_thread')) download_func = _partial(_download_image, param_tuple=param_tuple) return [res for res in executor.map(download_func, image_list)]
def _maybe_register(executable, **kw): if _which(executable): return _partial(register_terminal, **kw) else: def wrapper(fn, *args, **kwargs): return fn return wrapper
async def handle(self) -> None: quit_signal = _Signal() token = self.console.register_event_handler( _partial(self._event_callback, quit_signal=quit_signal)) await quit_signal.wait() self.console.unregister_event_handler(token)
def get_map2writepvs(self): """Get the database.""" dbase = self.get_database() map2write = dict() for pvname in dbase: prop = self._get_prop_name(pvname) if _PVName.is_write_pv(pvname): map2write[pvname] = _partial(self.write, prop) return map2write
def _process_tokens_updating_info(self, connector, auth_info, affordances): input_ = auth_info.tokens def processing_logmessage(insecure): message = 'processing {} --> {}'\ .format(input_ if insecure else input_.keys(), connector) if affordances.outputs != ((),): message += ' --> {}'.format(affordances.outputs) return message self.logger.cond((_logging.INSECURE, _partial(processing_logmessage, insecure=True)), (_logging.DEBUG, _partial(processing_logmessage, insecure=False)), ) new_auth_info = connector.process_tokens(input=input_, affordances=affordances) def processed_logmessage(insecure): message_ = 'processed {} --> {}'\ .format(input_ if insecure else input_.keys(), connector) if affordances.outputs != ((),): message_ += ' --> {}'.format(new_auth_info.tokens if insecure else new_auth_info.tokens.keys()) if new_auth_info.verified: message_ += ', {}'.format('accepted' if new_auth_info.accepted else 'rejected') return message_ self.logger.cond((_logging.INSECURE, _partial(processed_logmessage, insecure=True)), (_logging.DEBUG, _partial(processed_logmessage, insecure=False)), ) if new_auth_info.realm is None: new_auth_info.realm = auth_info.realm new_auth_info.provisions &= auth_info.provisions if not new_auth_info.algorithm: new_auth_info.algorithm = auth_info.algorithm return new_auth_info
def partial(function, *args, **kwargs): ''' add __get__ to python's partial implementation ''' function = _partial(function, *args, **kwargs) # if this function is used as a property it will bind self as the first # argument in *args, otherwise it can be used as a normal function def bind(*args, **kwargs): return function(*args, **kwargs) return bind
def curry(func, n=EMPTY): if n is EMPTY: n = func.__code__.co_argcount if n <= 1: return func elif n == 2: return lambda x: lambda y: func(x, y) else: return lambda x: curry(_partial(func, x), n - 1)
def _normalize_output(conversion): # as in snapshot rev 4, the tolerance should be 1e-11 normalize = _partial(round, ndigits=11 - 1) @_wraps(conversion) def normalized(*args, **kwargs): color = conversion(*args, **kwargs) return tuple(normalize(c) for c in color) return normalized
def get_map2readpvs(self): """Get the database.""" dbase = self.get_database() map2readpvs = dict() for pvname in dbase: if _PVName.is_cte_pv(pvname) or _PVName.is_cmd_pv(pvname): continue prop = self._get_prop_name(pvname) map2readpvs[pvname] = _partial( self.read, prop, is_sp=_PVName.is_sp_pv(pvname)) return map2readpvs
def __init__(self, func, *args, position=None, **kwargs): if position and args: raise ValueError("Cannot use specified position arguments" + " with non-specified position arguments" + " at same time.") if position: self._position = position self._func = func else: self._position = False self._func = _partial(func, *args, **kwargs) self._kwargs = kwargs
def init(dllpath=None, root="C:\\", bypass_check=False): """ Initialize the underlying tos-databridge DLL dllpath: string of the exact path of the DLL root: string of the directory to start walking/searching to find the DLL """ global _dll rel = set() if not bypass_check and dllpath is None and root == "C:\\": if abort_init_after_warn(): return False def _remove_older_versions(): nonlocal rel getver = lambda x: _search(_REGEX_VER_SFFX, x).group().strip('-') vers = tuple(zip(map(getver, rel), rel)) vers_max = max(vers)[0].split('.')[0] mtup = tuple(( x[0].split('.')[1],x[1]) \ for x in vers if x[0].split('.')[0] == vers_max ) mtup_max = max(mtup)[0] rel = set(x[1] for x in mtup if x[0] == mtup_max) try: if dllpath is None: matcher = _partial(_match, _REGEX_DLL_NAME) # regex match function for nfile in map(matcher, _listdir(_curdir)): if nfile: # try the current dir first rel.add(_curdir + _sep + nfile.string) if not rel: # no luck, walk the dir tree for root, dirs, files in _walk(root): for file in map(matcher, files): if file: rel.add(root + _sep + file.string) if not rel: # if still nothing throw raise TOSDB_InitError(" could not locate DLL") if len(rel) > 1: # only use the most recent version(s) _remove_older_versions() # most recently updated d = dict(zip(map(lambda x: _stat(x).st_mtime, rel), rel)) rec = max(d) dllpath = d[rec] _dll = _WinDLL(dllpath) print("+ Using Module ", dllpath) print("+ Last Update ", _asctime(_localtime(_stat(dllpath).st_mtime))) if connect(): print("+ Succesfully Connected to Service \ Engine") else: print("- Failed to Connect to Service \ Engine") return True # indicate the lib was loaded (but not if connect succeeded) except TOSDB_Error: raise except Exception as e: raise TOSDB_InitError("unable to initialize library", e)
def append(self, item): if isinstance(item, Collection): self.items.append(item) if item._usage is not None: self._add_to_attr(item._usage._name_.lower(), item) elif isinstance(item, UsagePage): if not [usage_type for usage_type in item.usage_types if usage_type in self._usage_types]: raise ValueError() collection = Collection(usage=item) self.items.append(collection) self._add_to_attr(item._usage._name_.lower(), collection) elif isinstance(item, Report): if len(item.usages)>0: for usage in item.usages: self._add_to_attr(usage._name_.lower(), property( fget=_partial(item.__getitem__, item.usages.index(usage)), fset=_partial(item.__setitem__, item.usages.index(usage)) )) self.items.append(item) else: raise ValueError("usage type is not UsagePage or Report")
def wrapper_execWork(*args, **kwargs): # Separates positional arguments into 2 lists: length==1, and length >1 args = list(args) args_len_one = [] args_iter = [] for arg in args: length, new_arg = find_length(arg) if length == 1: args_len_one.append(new_arg) else: args_iter.append(new_arg) del args # Separates kwargs into 2 lists: length==1, and length >1 kwargs_len_one = dict() kwargs_iter = dict() for key, arg in kwargs.items(): length, new_arg = find_length(arg) if length == 1: kwargs_len_one.update({key: new_arg}) else: kwargs_iter.update({key: new_arg}) del kwargs # new function with all args of length 1 inputted new_func = _partial(func, *args_len_one, **kwargs_len_one) pos_len = len(args_iter) # multi-threading with _PoolExecutor(max_workers=max_workers) as executor: try: dict_keys, dict_values = zip(*kwargs_iter.items()) dict_len = len(dict_keys) def process_args(args): new_args = args[:-dict_len] new_kwargs = { key: args[i] for i, key in enumerate(dict_keys, pos_len) } return new_args, new_kwargs fs = [(executor.submit(new_func, *args, **kwargs)) for args, kwargs in map(process_args, zip(*args_iter, *dict_values))] except ValueError: # no iterable kwargs fs = [ executor.submit(new_func, *args) for args in zip(*args_iter) ] return [fut.result() for fut in fs] if return_val else None
def _action(*args, **kwargs): def _inner(app_factory, hostname=None, port=None, server='simple'): from inyoka.core.api import ctx from inyoka.utils.urls import get_host_port_mapping, make_full_domain _hostname, _port = get_host_port_mapping(make_full_domain())[:-1] if hostname is None: hostname = _hostname if port is None: port = _port app = app_factory() def _simple(): from werkzeug.serving import run_simple run_simple(hostname, port, app, threaded=False, processes=1, use_reloader=True, use_debugger=False) def _eventlet(): from eventlet import api, wsgi wsgi.server(api.tcp_listener((hostname, port)), app) def _cherrypy(): from cherrypy.wsgiserver import CherryPyWSGIServer server = CherryPyWSGIServer((hostname, port), app, server_name=ctx.cfg['base_domain_name'], request_queue_size=500) server.start() def _tornado(): from tornado import httpserver, ioloop, wsgi container = wsgi.WSGIContainer(app) http_server = httpserver.HTTPServer(container) http_server.listen(port, hostname) ioloop.IOLoop.instance().start() def _gevent(): from gevent import monkey; monkey.patch_all() from gevent.wsgi import WSGIServer WSGIServer((hostname, port), app).serve_forever() mapping = { 'simple': _simple, 'eventlet': _eventlet, 'cherrypy': _cherrypy, 'tornado': _tornado, 'gevent': _gevent, } # run actually the server mapping[server]() return _partial(_inner, *args, **kwargs)
def partial(*args, **kwargs): """ copy.deepcopy balks at copying anonymous functions. This overrides the default behavior of functools.partial to make deepcopy return the function itself, rather than attempting to copy it. """ func = _partial(*args, **kwargs) def _partial_deepcopy(memo={}): return func func.__deepcopy__ = _partial_deepcopy return func
def number_of_timezones(df): """ Find the developers with reviews submitted from the most different timezones. """ by_developer = df.groupby(['owner._account_id']) tz_count = by_developer['owner.tz'].aggregate(lambda x: len(set(x))) tz_count.sort('owner.tz', ascending=False, inplace=True) return Result( tz_count.to_frame(), plot=_partial(tz_count.head().plot, kind='bar') )
def __getattr__(self, name): if name.startswith('__'): return self.__getattribute__(name) elif name in { 'Tag', 'Doc', 'Text', 'CSS', 'JS', 'defaults', 'fix_attribute', 'get_local_variable_from_caller' }: return globals()[name] # Everything else is treated as HTML Tag. name = fix_attribute(name) if name.lower() in defaults.void_tags: tag = _partial(VoidTag, name) else: if name in defaults.deprecated_tags: _warnings.warn( 'The {!r} tag is deprecated. ' 'See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element' ) if name not in defaults.tags: _warnings.warn('Unknown tag {!r}.'.format(name)) tag = _partial(Tag, name) return tag
def make_class_safe(cls=None, *, to_wrap=None, logger=None): if cls is None: return _partial(make_class_safe, to_wrap=to_wrap, logger=logger) if to_wrap is None: to_wrap = ['__call__'] for f_name in to_wrap: setattr(cls, f_name, make_callback_safe(getattr(cls, f_name), logger=logger)) return cls
def make_callback_safe(func=None, *, logger=None): """ If the wrapped func raises any exceptions, log them but continue. This is intended to ensure that any failures in non-critical callbacks do not interrupt data acquisition. It should *not* be applied to any critical callbacks, such as ones that perform data-saving, but is well suited to callbacks that perform non-critical streaming visualization or data processing. To debug the issue causing a failure, it can be convenient to turn this off and let the failures raise. To do this, set the environment variable ``BLUESKY_DEBUG_CALLBACKS=1``. Parameters ---------- func: callable logger: logging.Logger, optional Examples -------- Decorate a callback to make sure it will not interrupt data acquisition if it fails. >>> @make_callback_safe ... def callback(name, doc): ... ... """ if func is None: return _partial(make_callback_safe, logger=logger) @_wraps(func) def inner(*args, **kwargs): try: return func(*args, **kwargs) except Exception: debug_mode = os.environ.get("BLUESKY_DEBUG_CALLBACKS", False) if logger is not None: if debug_mode: msg = f"Exception in {func}" else: msg = (f"An exception raised in the callback {func} " "is being suppressed to not interrupt plan " "execution. To investigate try setting the " "BLUESKY_DEBUG_CALLBACKS env to '1'") logger.exception(msg) if debug_mode: raise return inner
def _write_graphicals(self, iterable): # asserts that tokens from iterable are graphical; writes them # with spaces to output stream GRAPHICAL = self.GRAPHICAL SPACE = self.SPACE write_space = _partial(self._write_string, SPACE) write_string = self._write_string # XXX optimize by inlining functionality of _write_string self._ensure_whitespace(SPACE) for graphical_token in iterable: assert frozenset(graphical_token) <= GRAPHICAL write_string(graphical_token) write_space()
def partial(f, *args, **kwargs): """ A workalike for `functools.partial` that actually (recursively) creates `PartialPlus` objects via `as_partialplus`. Parameters ---------- f : callable Function whose evaluation to defer. Notes ----- Remaining positional and keyword arguments are passed along to `f`, as `functools.partial`. """ return as_partialplus(_partial(f, *args, **kwargs))
def init(dllpath = None, root = "C:\\", bypass_check=False): """ Initialize the underlying tos-databridge DLL dllpath: string of the exact path of the DLL root: string of the directory to start walking/searching to find the DLL """ global _dll rel = set() if not bypass_check and dllpath is None and root == "C:\\": if abort_init_after_warn(): return try: if dllpath is None: matcher = _partial( _match, _REGEX_DLL_NAME) # regex match function for nfile in map( matcher, _listdir( _curdir )): if nfile: # try the current dir first rel.add( _curdir+ _sep + nfile.string ) if not rel: for root,dirs, files in _walk(root): # no luck, walk the dir tree for file in map( matcher, files): if file: rel.add( root + _sep + file.string ) if not rel: # if still nothing throw raise TOSDB_Error(" could not locate DLL") if len(rel) > 1: # only use the most recent version(s) ver = _compile('-[\d]{1,2}.[\d]{1,2}-') vers = tuple( zip( map( lambda x: _search(ver,x).group().strip('-'), rel), rel) ) vers_max = max(vers)[0].split('.')[0] mtup = tuple( (x[0].split('.')[1],x[1]) for x in vers if x[0].split('.')[0] == vers_max) mtup_max = max(mtup)[0] rel = set( x[1] for x in mtup if x[0] == mtup_max ) # find the most recently updated d = dict( zip(map( lambda x : _stat(x).st_mtime, rel), rel ) ) rec = max(d) dllpath = d[ rec ] _dll = _WinDLL( dllpath ) print( "+ Using Module ", dllpath ) print( "+ Last Update ", _asctime(_localtime(_stat(dllpath).st_mtime))) if connect(): print("+ Succesfully Connected to Service \ Engine") else: print("- Failed to Connect to Service \ Engine") return True # indicate the lib was loaded except Exception as e: raise TOSDB_CLibError( "unable to initialize library", e )
def hash(self, algorithm=hashlib.md5, return_hex=True): """ Compute the hash of this file and return it, as a hexidecimal string. The default algorithm is md5. An alternate constructor from hashlib can be passed as the algorithm parameter; file.hash(hashlib.sha1) would, for example, compute the SHA-1 hash instead. If return_hex is False (it defaults to True), the hash object itself will be returned instead of the return value of its hexdigest() method. One can use this to access the binary hash instead. """ hasher = algorithm() with self.open("rb") as f: for block in iter(_partial(f.read, 10), ""): hasher.update(block) if return_hex: hasher = hasher.hexdigest() return hasher
def total_frame(self, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ): """ Return a matrix of the most recent values: date_time: (True/False) attempt to retrieve a TOSDB_DateTime object labels: (True/False) pull the item and topic labels with the values data_str_max: the maximum length of string data returned label_str_max: the maximum length of label strings returned if labels and date_time are True: returns-> dict of namedtuple of 2tuple if labels is True: returns -> dict of namedtuple if date_time is True: returns -> list of 2tuple else returns-> list """ p = _partial(self.topic_frame, date_time=date_time, labels=labels, data_str_max=data_str_max, label_str_max=label_str_max) if labels: return {x : p(x) for x in self.items()} else: return [p(x) for x in self.items()]
def _evaluate(p, instantiate_call=None, bindings=None): """ Evaluate a nested tree of functools.partial objects, used for deferred evaluation. Parameters ---------- p : object If `p` is a partial, or a subclass of partial, it is expanded recursively. Otherwise, return. instantiate_call : callable, optional Rather than call `p.func` directly, instead call `instantiate_call(p.func, ...)` bindings : dict, optional A dictionary mapping `Node` objects to values to use in their stead. Used to cache objects already evaluated. Returns ------- q : object The result of evaluating `p` if `p` was a partial instance, or else `p` itself. Notes ----- For large graphs this recursive implementation may hit the recursion limit and be kind of slow. TODO: write an iterative version. """ instantiate_call = ((lambda f, *args, **kwargs: f(*args, **kwargs)) if instantiate_call is None else instantiate_call) bindings = {} if bindings is None else bindings # If we've encountered this exact partial node before, # short-circuit the evaluation of this branch and return # the pre-computed value. if p in bindings: return bindings[p] if isinstance(p, Literal): bindings[p] = p.value return bindings[p] recurse = _partial(_evaluate, instantiate_call=instantiate_call, bindings=bindings) # When evaluating an expression of the form # `list(...)[item]` # only evaluate the element(s) of the list that we need. if p.func == operator.getitem and is_indexable(p): return _handle_indexing(p, instantiate_call, bindings, recurse) args = [recurse(arg) for arg in p.args] kw = (dict((kw, recurse(val)) for kw, val in p.keywords.iteritems()) if p.keywords else {}) if is_variable_node(p): assert 'name' in p.keywords name = kw['name'] try: return bindings[name] except KeyError: raise KeyError("variable with name '%s' not bound" % name) # bindings the evaluated value (for subsequent calls that # will look at this bindings dictionary) and return. bindings[p] = instantiate_call(p.func, *args, **kw) return bindings[p]
from functools import partial as _partial from itertools import chain as _chain from .termformatconstants import FG_COLORS, BG_COLORS, STYLES from .formatstring import fmtstr for att in _chain(FG_COLORS, ('on_'+x for x in BG_COLORS), STYLES): locals()[att] = _partial(fmtstr, style=att) plain = _partial(fmtstr) if __name__ == '__main__': import doctest doctest.testmod() print((blue('adf'))) print((blue(on_red('ad')))) print((blue('asdf') + on_red('adsf'))) print(((blue('asdf') + on_red('adsf'))[3:7])) f = blue('hey there') + on_red(' Tom!') print(f) f[1:3] = 'ot' print((repr(f))) print(f) f = on_blue(red('stuff')) print((repr(f))) print((repr(str(f)))) print(f) print(((f + '!')[0:6] + '?'))
def _run(args, use_devel_projects=False): if args.projects: names = args.projects else: names = True include_deps = args.include_deps or args.only_deps if args.extras == _EXTRAS_ARGVALUE_ALL: extras = True elif args.extras == _EXTRAS_ARGVALUE_NONE: extras = False else: extras = {} for dist_extras_str in extras: dist, dist_extras_str = dist_extras_str.split(':', 1) dist_extras = dist_extras_str.split(',') extras[dist] = dist_extras update_cmd = args.update_cmd if args.path is None and use_devel_projects: env_searchpaths = _pkg.devel_searchpaths() searchpaths = _pkg.devel_searchpaths(names, require=args.require, include_deps=include_deps, distinction=args.distinction, extras=extras, update_cmd=update_cmd, logger=_logger) update_cmd = None else: env_searchpaths = args.path searchpaths = env_searchpaths actions = [] if args.print_: actions.append(_partial(print_project, format=args.format, dist_format=args.dist_format)) if args.exec_cmd is not None: actions.append(_partial(exec_project, args.exec_cmd, format=args.format, dist_format=args.dist_format)) if not actions: actions.append(_partial(print_project, format=args.format, dist_format=args.dist_format)) projects = _pkg.find_projects(names, require=args.require, include_deps=include_deps, searchpaths=searchpaths, dists_relpaths=args.dists_relpaths, distinction=args.distinction, extras=extras, update_cmd=update_cmd, logger=_logger) if args.sort == 'deps': if update_cmd: searchpaths_set = set(searchpaths) for path in env_searchpaths: if path not in searchpaths_set: _pkg.update_metadata_at(path, cmd=update_cmd, logger=_logger) env_projects = _pkg.find_projects(searchpaths=env_searchpaths, dists_relpaths=args.dists_relpaths, distinction=args.distinction) env = _pkg.DistEnv(_pkg.projects_dists(env_projects)) projects = _pkg.projects_sorted_by_requirements\ (projects, include_deps=include_deps, extras=extras, env=env, logger=_logger) if args.reverse: projects = reversed(projects) for project in projects: if args.only_deps and project.name in names: continue for action in actions: action(project)
d = dict() d.update(data) return req._replace(method="POST", data=d) def nonce(length=8): """Generate pseudorandom number.""" return ''.join([str(randint(0, 9)) for i in range(length)]) def timestamp(): """Get seconds since epoch (UTC).""" return int(time()) urlquote = _partial(urlquote, safe='') TemporaryCredentials = _namedtuple("TemporaryCredentials", ("token", "secret")) TokenCredentials = _namedtuple("TokenCredentials", ("token", "secret")) #Applying oauth parameters to request def req_apply_oauth_header(req: Request, oauth_params) -> Request: x = ",".join(['{0}="{1}"'.format(urlquote(str(k)), urlquote(str(v))) for k, v in oauth_params.items()]) return apply_headers_to_req(req, {"Authorization": "OAuth {0}".format(x)}) def req_apply_oauth_data(req: Request, oauth_params) -> Request:
def __get__(self, obj, objtype=None): if objtype is None: objtype = type(obj) return _partial(self.func, objtype, obj)
def _row_node_from_sql_func(self, table_iri): if table_iri in self._orm_bnode_tables: return _partial(self._row_bnode_from_sql, table_iri) else: return _partial(self._row_iri_from_sql, table_iri)
_get_handler(level=level, file=_os.path.join(UserLogPath, file), fmt='[{levelname:<8}] {asctime}.{msecs:0>3.0f}: <{pathname}:{lineno}> {message}', datefmt='%Y-%m-%d %H:%M:%S')) if scr_log: handlers.append( _get_handler(level=scr_level, file=None, fmt='[{levelname:<8}] <{filename}:{lineno}> {message}')) _logging.basicConfig(level=_logging.DEBUG, handlers=handlers) info('Start the app') info('Process ID: {}'.format(_os.getpid())) info('App config: {}'.format(C.to_dict())) message = _logging.log debug = _logging.debug verbose = _partial(message, LEVEL_VERBOSE) info = _logging.info warning = _logging.warning error = _logging.error critical = _logging.critical @_cm def msg_block(msg, level=LEVEL_INFO, log_time=True): if log_time: start_time = _time() message(level, '{}... '.format(msg)) yield # noinspection PyUnboundLocalVariable message(level, '{} done{}.'.format(msg, ', time: {:.4f}s'.format(_time() - start_time) if log_time else ''))
def _build_gui(self): """ Removes all existing sliders and rebuilds them based on the colormap. """ # remove all widgets (should destroy all children too) self._central_widget.deleteLater() # remove all references to other controls self._sliders = [] self._buttons_top_color = [] self._buttons_bottom_color = [] self._checkboxes = [] self._buttons_plus = [] self._buttons_minus = [] self._color_dialogs_top = [] self._color_dialogs_bottom = [] # create the new central widget self._central_widget = _qt.QWidget() self._window.setCentralWidget(self._central_widget) # layout for main widget self._layout = _qt.QGridLayout(self._central_widget) self._central_widget.setLayout(self._layout) # add the list of cmaps self._combobox_cmaps = _qt.QComboBox(self._central_widget) self._combobox_cmaps.setEditable(True) self._load_cmap_list() # add the save and delete buttons self._button_save = _qt.QPushButton("Save", self._central_widget) self._button_delete = _qt.QPushButton("Delete", self._central_widget) self._button_save.setFixedWidth(70) self._button_delete.setFixedWidth(70) # layouts self._layout.addWidget(self._combobox_cmaps, 1,1, 1,3, _qtcore.Qt.Alignment(0)) self._layout.addWidget(self._button_save, 1,5, 1,1, _qtcore.Qt.Alignment(1)) self._layout.addWidget(self._button_delete, 1,6, 1,2, _qtcore.Qt.Alignment(1)) # actions self._combobox_cmaps.currentIndexChanged.connect(self._signal_load) self._button_save .clicked.connect(self._button_save_clicked) self._button_delete.clicked.connect(self._button_delete_clicked) # ensmallen the window self._window.resize(10,10) # now create a control set for each color point for n in range(len(self._colorpoint_list)): c1 = self._colorpoint_list[n][1] c2 = self._colorpoint_list[n][2] # create a top-color button self._buttons_top_color.append(_qt.QPushButton(self._central_widget)) self._buttons_top_color[-1].setStyleSheet("background-color: rgb("+str(int(c2[0]*255))+","+str(int(c2[1]*255))+","+str(int(c2[2]*255))+"); border-radius: 3px;") # create a bottom-color button self._buttons_bottom_color.append(_qt.QPushButton(self._central_widget)) self._buttons_bottom_color[-1].setStyleSheet("background-color: rgb("+str(int(c1[0]*255))+","+str(int(c1[1]*255))+","+str(int(c1[2]*255))+"); border-radius: 3px;") # create color dialogs self._color_dialogs_top.append(_qt.QColorDialog(self._central_widget)) self._color_dialogs_top[-1].setCurrentColor(self._buttons_top_color[-1].palette().color(1)) self._color_dialogs_bottom.append(_qt.QColorDialog(self._central_widget)) self._color_dialogs_bottom[-1].setCurrentColor(self._buttons_top_color[-1].palette().color(1)) # create link checkboxes self._checkboxes.append(_qt.QCheckBox(self._central_widget)) self._checkboxes[-1].setChecked(c1==c2) # create a slider self._sliders.append(_qt.QSlider(self._central_widget)) self._sliders[-1].setOrientation(_qtcore.Qt.Horizontal) self._sliders[-1].setMaximum(1000) self._sliders[-1].setValue(int(self._colorpoint_list[n][0]*1000)) self._sliders[-1].setFixedWidth(250) # create + and - buttons self._buttons_plus.append(_qt.QPushButton(self._central_widget)) self._buttons_plus[-1].setText("+") self._buttons_plus[-1].setFixedWidth(25) self._buttons_minus.append(_qt.QPushButton(self._central_widget)) self._buttons_minus[-1].setText("-") self._buttons_minus[-1].setFixedWidth(25) # layout self._layout.addWidget(self._buttons_bottom_color[-1], n+3,1, _qtcore.Qt.AlignCenter) self._layout.addWidget(self._checkboxes[-1], n+3,2, 1,1, _qtcore.Qt.AlignCenter) self._layout.addWidget(self._buttons_top_color[-1], n+3,3, _qtcore.Qt.AlignCenter) self._layout.addWidget(self._sliders[-1], n+3,4, 1,2, _qtcore.Qt.AlignCenter) self._layout.setColumnStretch(5,100) self._layout.addWidget(self._buttons_minus[-1], n+3,7, _qtcore.Qt.AlignCenter) self._layout.addWidget(self._buttons_plus[-1], n+3,6, _qtcore.Qt.AlignCenter) # connect the buttons and slider actions to the calls self._buttons_bottom_color[-1] .clicked.connect(_partial(self._color_button_clicked, n, 0)) self._buttons_top_color[-1] .clicked.connect(_partial(self._color_button_clicked, n, 1)) self._color_dialogs_bottom[-1].currentColorChanged.connect(_partial(self._color_dialog_changed, n, 0)) self._color_dialogs_top[-1] .currentColorChanged.connect(_partial(self._color_dialog_changed, n, 1)) self._buttons_plus[-1] .clicked.connect(_partial(self._button_plus_clicked, n)) self._buttons_minus[-1] .clicked.connect(_partial(self._button_minus_clicked, n)) self._sliders[-1] .valueChanged.connect(_partial(self._slider_changed, n)) # disable the appropriate sliders self._sliders[0] .setDisabled(True) self._sliders[-1].setDisabled(True)
def _apply_async(self): try: return self.type.apply_async except KeyError: return _partial(current_app.send_task, self["task"])
def _apply_async(self): try: return self.type.apply_async except KeyError: return _partial((self._app or current_app).send_task, self['task'])
def init(dllpath=None, root="C:\\", bypass_check=False): """ Initialize the underlying tos-databridge DLL dllpath: string of the exact path of the DLL root: string of the directory to start walking/searching to find the DLL """ global _dll, _dll_depend1 rel = set() if not bypass_check and dllpath is None and root == "C:\\": if abort_init_after_warn(): return False def _remove_older_versions(): nonlocal rel getver = lambda x: _search(_REGEX_VER_SFFX,x).group().strip('-') vers = tuple(zip(map(getver, rel), rel)) vers_max = max(vers)[0].split('.')[0] mtup = tuple(( x[0].split('.')[1],x[1]) \ for x in vers if x[0].split('.')[0] == vers_max ) mtup_max = max(mtup)[0] rel = set(x[1] for x in mtup if x[0] == mtup_max) def _get_depends1_dll_path(dllpath): d = _path.dirname(dllpath) return d + "/" + DLL_DEPENDS1_NAME + "-" + SYS_ARCH_TYPE + ".dll" try: if dllpath is None: matcher = _partial(_match, _REGEX_DLL_NAME) for nfile in map(matcher, _listdir(_curdir)): if nfile: # try the current dir first rel.add(_curdir+ _sep + nfile.string) if not rel: # no luck, walk the dir tree for root, dirs, files in _walk(root): for file in map(matcher, files): if file: rel.add(root + _sep + file.string) if not rel: # if still nothing throw raise TOSDB_InitError(" could not locate DLL") if len(rel) > 1: # only use the most recent version(s) _remove_older_versions() # most recently updated d = dict(zip(map(lambda x: _stat(x).st_mtime, rel), rel)) rec = max(d) dllpath = d[rec] dllpath_depends1 = _get_depends1_dll_path(dllpath) _dll_depend1 = _CDLL(dllpath_depends1) _dll = _CDLL(dllpath) print("+ Using Module(s) ", dllpath) print(" ", dllpath_depends1) print("+ Last Update ", _asctime(_localtime(_stat(dllpath).st_mtime))) if connect(): print("+ Succesfully Connected to Service \ Engine") else: print("- Failed to Connect to Service \ Engine") return True # indicate the lib was loaded (but not if connect succeeded) except TOSDB_Error: raise except Exception as e: raise TOSDB_InitError("unable to initialize library", e)
from __future__ import print_function from pycparser import c_ast, preprocess_file, parse_file as _parse_file from codeschematics.parsers.parser_data import ParserData try: from pycparserext.ext_c_parser import GnuCParser except ImportError: import warnings warnings.warn("pycparserext not found, falling back to pycparser (likely to fail)", ImportWarning) parse_file = _parse_file else: from functools import partial as _partial parser = GnuCParser() parse_file = _partial(_parse_file, parser=parser) # Basically parse_file should dynamically use GnuCParser when available, or # silently fallback if not class CTraverser(c_ast.NodeVisitor): top_level = '__file__' # The fake name for containing-function of # top level function calls def __init__(self): self._data = ParserData(self.top_level) def visit_FuncDef(self, node): # For now, we assume that function name is unique (i.e. no overloading), # so ignore param types and return type self._data.parse_func(node.decl.name, self.generic_visit, node)
parser.error("Expected 1 argument, got %d" % len(operands)) imm24 = parser.parse_immediate(operands[0], checked=True) limit = 1<<24 if imm24 < 0 or imm24 >= limit: parser.error("Immediate value should be between 0 and %d, got: %d" % (limit - 1, imm24)) return _swi_format.encode({"Cond": condition, "Imm24" : imm24}) # Install data-processing instructions _dpi_instructions = [("and", 0), ("eor", 1), ("sub", 2), ("rsb", 3), ("add", 4), ("adc", 5), ("sbc", 6), ("rsc", 7), ("orr", 12), ("bic", 14)] for (_name, _opcode) in _dpi_instructions: for _i in range(len(_conditions)): _fullname = _name + _conditions[_i] _instructions[_fullname] = _partial(_parse_dpi, _opcode, _i, 0) _instructions[_fullname + "s"] = _partial(_parse_dpi, _opcode, _i, 1) # Install move instructions _move_instructions = [("mov", 13), ("mvn", 15)] for (_name, _opcode) in _move_instructions: for _i in range(len(_conditions)): _fullname = _name + _conditions[_i] _instructions[_fullname] = _partial(_parse_move, _opcode, _i, 0) _instructions[_fullname + "s"] = _partial(_parse_move, _opcode, _i, 1) # Install test instructions _cond_instructions = [("tst", 8), ("teq", 9), ("cmp", 10), ("cmn", 11)] for (_name, _opcode) in _cond_instructions: for _i in range(len(_conditions)):
def format_table_name(compiler, name, schema): quote = functools._partial(compiler.preparer.quote, force=None) if schema: return quote_dotted(schema, quote) + "." + quote(name) else: return quote(name)
def _apply_async(self): try: return self.type.apply_async except KeyError: return _partial(self.app.send_task, self['task'])
from functools import partial as _partial import numpy as _np from .base import Result def _duration(groupby, at_least, df): df = df[df['status'] == 'MERGED'] df['review_duration.hours'] = df['review_duration'] / 60 / 60 by_developer = df.groupby(groupby) speed = by_developer['review_duration.hours'].aggregate([ _np.mean, _np.std, len]) speed = speed[speed['len'] > at_least] speed.sort('mean', ascending=True, inplace=True) return Result(speed) developer = _partial(_duration, ['owner._account_id'], 10) project = _partial(_duration, ['project'], 100) project_group = _partial(_duration, ['project.group'], 0) submitted_hour = _partial(_duration, ['created.hour'], 0) submitted_hour_local = _partial(_duration, ['created.local.hour'], 0)