Python types.GeneratorType() Examples
The following are 30
code examples of types.GeneratorType().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
types
, or try the search function
.

Example #1
Source File: __init__.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 7 votes |
def is_iterator(obj): """Detect if the object provided implements the iterator protocol. (i.e. like a generator). This will return False for objects which are iterable, but not iterators themselves. """ from types import GeneratorType if isinstance(obj, GeneratorType): return True elif not hasattr(obj, '__iter__'): return False else: # Types which implement the protocol must return themselves when # invoking 'iter' upon them. return iter(obj) is obj
Example #2
Source File: decorators.py From certidude with MIT License | 6 votes |
def default(self, obj): from certidude.user import User if isinstance(obj, ipaddress._IPAddressBase): return str(obj) if isinstance(obj, set): return tuple(obj) if isinstance(obj, datetime): return obj.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z" if isinstance(obj, date): return obj.strftime("%Y-%m-%d") if isinstance(obj, timedelta): return obj.total_seconds() if isinstance(obj, types.GeneratorType): return tuple(obj) if isinstance(obj, User): return dict(name=obj.name, given_name=obj.given_name, surname=obj.surname, mail=obj.mail) return json.JSONEncoder.default(self, obj)
Example #3
Source File: inspect.py From jawfish with MIT License | 6 votes |
def isgenerator(object): """Return true if the object is a generator. Generator objects provide these attributes: __iter__ defined to support iteration over container close raises a new GeneratorExit exception inside the generator to terminate the iteration gi_code code object gi_frame frame object or possibly None once the generator has been exhausted gi_running set to 1 when generator is executing, 0 otherwise next return the next item from the container send resumes the generator and "sends" a value that becomes the result of the current yield-expression throw used to raise an exception inside the generator""" return isinstance(object, types.GeneratorType)
Example #4
Source File: utils.py From plugin.video.kmediatorrent with GNU General Public License v3.0 | 6 votes |
def ensure_fanart(fn): """Makes sure that if the listitem doesn't have a fanart, we properly set one.""" from functools import wraps @wraps(fn) def _fn(*a, **kwds): import os import types from kmediatorrent import plugin items = fn(*a, **kwds) if items is None: return if isinstance(items, types.GeneratorType): items = list(items) for item in items: properties = item.setdefault("properties", {}) if not properties.get("fanart_image"): properties["fanart_image"] = plugin.addon.getAddonInfo("fanart") return items return _fn
Example #5
Source File: caching.py From plugin.video.kmediatorrent with GNU General Public License v3.0 | 6 votes |
def cached_route(*args, **kwargs): from functools import wraps def cached(fn): @wraps(fn) def _fn(*a, **kwds): import hashlib basename = "kmediatorrent.route.%s" % hashlib.sha1(plugin.request.path).hexdigest() with shelf(basename, ttl=kwargs.get("ttl") or 0) as result: if not result.get("value"): ret = fn(*a, **kwds) import types if isinstance(ret, types.GeneratorType): ret = list(ret) result["value"] = ret if kwargs.get("content_type"): plugin.set_content(kwargs.get("content_type")) return result["value"] return _fn if len(args) == 1 and callable(args[0]): return cached(args[0]) return cached
Example #6
Source File: util.py From browserscope with Apache License 2.0 | 6 votes |
def is_generator(obj): """Return true if the object is generator or generator function. Generator function objects provides same attributes as functions. See isfunction.__doc__ for attributes listing. Adapted from Python 2.6. Args: obj: an object to test. Returns: true if the object is generator function. """ if isinstance(obj, types.GeneratorType): return True CO_GENERATOR = 0x20 return bool(((inspect.isfunction(obj) or inspect.ismethod(obj)) and obj.func_code.co_flags & CO_GENERATOR))
Example #7
Source File: test_io.py From imageio-ffmpeg with BSD 2-Clause "Simplified" License | 6 votes |
def test_reading1(): # Calling returns a generator gen = imageio_ffmpeg.read_frames(test_file1) assert isinstance(gen, types.GeneratorType) # First yield is a meta dict meta = gen.__next__() assert isinstance(meta, dict) for key in ("size", "fps", "duration"): assert key in meta # Read frames framesize = meta["size"][0] * meta["size"][1] * 3 assert framesize == 1280 * 720 * 3 count = 0 for frame in gen: assert isinstance(frame, bytes) and len(frame) == framesize count += 1 assert count == 280
Example #8
Source File: test_utils.py From aboleth with Apache License 2.0 | 6 votes |
def test_batch(): """Test the batch feed dict generator.""" X = np.arange(100) fd = {'X': X} data = ab.batch(fd, batch_size=10, n_iter=10) # Make sure this is a generator assert isinstance(data, GeneratorType) # Make sure we get a dict back of a length we expect d = next(data) assert isinstance(d, dict) assert 'X' in d assert len(d['X']) == 10 # Test we get all of X back in one sweep of the data accum = list(d['X']) for ds in data: assert len(ds['X']) == 10 accum.extend(list(ds['X'])) assert len(accum) == len(X) assert set(X) == set(accum)
Example #9
Source File: test_utils.py From aboleth with Apache License 2.0 | 6 votes |
def test_batch_predict(): """Test the batch prediction feed dict generator.""" X = np.arange(100) fd = {'X': X} data = ab.batch_prediction(fd, batch_size=10) # Make sure this is a generator assert isinstance(data, GeneratorType) # Make sure we get a dict back of a length we expect with correct indices for ind, d in data: assert isinstance(d, dict) assert 'X' in d assert len(d['X']) == 10 assert all(X[ind] == d['X'])
Example #10
Source File: clx_legacy.py From pycomm3 with MIT License | 6 votes |
def read_tag(self, *tags): """ read tag from a connected plc Possible combination can be passed to this method: - ('Counts') a single tag name - (['ControlWord']) a list with one tag or many - (['parts', 'ControlWord', 'Counts']) At the moment there is not a strong validation for the argument passed. The user should verify the correctness of the format passed. :return: None is returned in case of error otherwise the tag list is returned """ if not self._forward_open(): self.__log.warning("Target did not connected. read_tag will not be executed.") raise DataError("Target did not connected. read_tag will not be executed.") if len(tags) == 1: if isinstance(tags[0], (list, tuple, GeneratorType)): return self._read_tag_multi(tags[0]) else: return self._read_tag_single(tags[0]) else: return self._read_tag_multi(tags)
Example #11
Source File: inspect.py From meddle with MIT License | 6 votes |
def isgenerator(object): """Return true if the object is a generator. Generator objects provide these attributes: __iter__ defined to support interation over container close raises a new GeneratorExit exception inside the generator to terminate the iteration gi_code code object gi_frame frame object or possibly None once the generator has been exhausted gi_running set to 1 when generator is executing, 0 otherwise next return the next item from the container send resumes the generator and "sends" a value that becomes the result of the current yield-expression throw used to raise an exception inside the generator""" return isinstance(object, types.GeneratorType)
Example #12
Source File: beam.py From sentry-python with BSD 2-Clause "Simplified" License | 6 votes |
def _wrap_task_call(func): # type: (F) -> F """ Wrap task call with a try catch to get exceptions. Pass the client on to raise_exception so it can get rebinded. """ client = Hub.current.client @wraps(func) def _inner(*args, **kwargs): # type: (*Any, **Any) -> Any try: gen = func(*args, **kwargs) except Exception: raise_exception(client) if not isinstance(gen, types.GeneratorType): return gen return _wrap_generator_call(gen, client) setattr(_inner, USED_FUNC, True) return _inner # type: ignore
Example #13
Source File: util.py From pyinfra with MIT License | 6 votes |
def unroll_generators(generator): ''' Take a generator and unroll any sub-generators recursively. This is essentially a Python 2 way of doing `yield from` in Python 3 (given iterating the entire thing). ''' # Ensure we have a generator (prevents ccommands returning lists) if not isinstance(generator, GeneratorType): raise TypeError('{0} is not a generator'.format(generator)) items = [] for item in generator: if isinstance(item, GeneratorType): items.extend(unroll_generators(item)) else: items.append(item) return items
Example #14
Source File: inventory.py From pyinfra with MIT License | 6 votes |
def _is_inventory_group(key, value): ''' Verify that a module-level variable (key = value) is a valid inventory group. ''' if ( key.startswith('_') or not isinstance(value, (list, tuple, GeneratorType)) ): return False # If the group is a tuple of (hosts, data), check the hosts if isinstance(value, tuple): value = value[0] # Expand any generators of hosts if isinstance(value, GeneratorType): value = list(value) return all( isinstance(item, ALLOWED_HOST_TYPES) for item in value )
Example #15
Source File: inspect.py From ironpython2 with Apache License 2.0 | 6 votes |
def isgenerator(object): """Return true if the object is a generator. Generator objects provide these attributes: __iter__ defined to support iteration over container close raises a new GeneratorExit exception inside the generator to terminate the iteration gi_code code object gi_frame frame object or possibly None once the generator has been exhausted gi_running set to 1 when generator is executing, 0 otherwise next return the next item from the container send resumes the generator and "sends" a value that becomes the result of the current yield-expression throw used to raise an exception inside the generator""" return isinstance(object, types.GeneratorType)
Example #16
Source File: cache.py From aumfor with GNU General Public License v3.0 | 6 votes |
def _find_generators(self, item): """ A recursive function to flatten generators into lists """ try: result = [] # Make sure dicts aren't flattened to lists if isinstance(item, dict): result = {} for i in item: result[self._find_generators(i)] = self._find_generators(item[i]) return result # Since NoneObjects and strings are both iterable, treat them specially if isinstance(item, obj.NoneObject) or isinstance(item, str): return item if isinstance(item, types.GeneratorType): raise CacheContainsGenerator for x in iter(item): flat_x = self._find_generators(x) result.append(flat_x) return result except TypeError: return item
Example #17
Source File: util.py From locality-sensitive-hashing with MIT License | 6 votes |
def is_generator(obj): """Return true if the object is generator or generator function. Generator function objects provides same attributes as functions. See isfunction.__doc__ for attributes listing. Adapted from Python 2.6. Args: obj: an object to test. Returns: true if the object is generator function. """ if isinstance(obj, types.GeneratorType): return True CO_GENERATOR = 0x20 return bool(((inspect.isfunction(obj) or inspect.ismethod(obj)) and obj.func_code.co_flags & CO_GENERATOR))
Example #18
Source File: test_common.py From koku with GNU Affero General Public License v3.0 | 6 votes |
def test_date_range(self): """Test that a date range generator is returned.""" start_date = "2020-01-01" end_date = "2020-02-29" date_generator = common_utils.date_range(start_date, end_date) start_date = parser.parse(start_date) end_date = parser.parse(end_date) self.assertIsInstance(date_generator, types.GeneratorType) first_date = next(date_generator) self.assertEqual(first_date, start_date.date()) for day in date_generator: self.assertIsInstance(day, date) self.assertGreater(day, start_date.date()) self.assertLessEqual(day, end_date.date()) self.assertEqual(day, end_date.date())
Example #19
Source File: test_common.py From koku with GNU Affero General Public License v3.0 | 6 votes |
def test_date_range_pair(self): """Test that start and end dates are returned by this generator.""" start_date = "2020-01-01" end_date = "2020-02-29" step = 3 date_generator = common_utils.date_range_pair(start_date, end_date, step=step) start_date = parser.parse(start_date) end_date = parser.parse(end_date) self.assertIsInstance(date_generator, types.GeneratorType) first_start, first_end = next(date_generator) self.assertEqual(first_start, start_date.date()) self.assertEqual(first_end, start_date.date() + timedelta(days=step)) for start, end in date_generator: self.assertIsInstance(start, date) self.assertIsInstance(end, date) self.assertGreater(start, start_date.date()) self.assertLessEqual(end, end_date.date()) self.assertEqual(end, end_date.date())
Example #20
Source File: _Simulation.py From myhdl with GNU Lesser General Public License v2.1 | 5 votes |
def _makeWaiters(arglist): waiters = [] ids = set() cosims = [] for arg in arglist: if isinstance(arg, GeneratorType): waiters.append(_inferWaiter(arg)) elif isinstance(arg, _Instantiator): waiters.append(arg.waiter) elif isinstance(arg, Cosimulation): cosims.append(arg) waiters.append(_SignalTupleWaiter(arg._waiter())) elif isinstance(arg, _Waiter): waiters.append(arg) elif arg == True: pass else: raise SimulationError(_error.ArgType, str(type(arg))) if id(arg) in ids: raise SimulationError(_error.DuplicatedArg) ids.add(id(arg)) # add waiters for shadow signals for sig in _signals: if hasattr(sig, '_waiter'): waiters.append(sig._waiter) return waiters, cosims
Example #21
Source File: _toVerilog.py From myhdl with GNU Lesser General Public License v2.1 | 5 votes |
def _checkArgs(arglist): for arg in arglist: if not isinstance(arg, (GeneratorType, _Instantiator, _UserVerilogCode)): raise ToVerilogError(_error.ArgType, arg)
Example #22
Source File: _toVHDL.py From myhdl with GNU Lesser General Public License v2.1 | 5 votes |
def _checkArgs(arglist): for arg in arglist: if not isinstance(arg, (GeneratorType, _Instantiator, _UserVhdlCode)): raise ToVHDLError(_error.ArgType, arg)
Example #23
Source File: test_inferWaiter.py From myhdl with GNU Lesser General Public License v2.1 | 5 votes |
def bench(self, genFunc, waiterType): a, b, c, d, r, s = [Signal(intbv(0)) for i in range(6)] gen_inst_r = genFunc(a, b, c, d, r) if not isinstance(gen_inst_r, GeneratorType): # decorator type gen_inst_r = gen_inst_r.gen assert type(_inferWaiter(gen_inst_r)) == waiterType gen_inst_s = genFunc(a, b, c, d, s) if not isinstance(gen_inst_s, GeneratorType): # decorator type gen_inst_s = gen_inst_s.gen def stimulus(): for i in range(1000): yield delay(randrange(1, 10)) if randrange(2): a.next = randrange(32) if randrange(2): b.next = randrange(32) c.next = randrange(2) d.next = randrange(2) raise StopSimulation def check(): while 1: yield a, b, c, r, s assert r == s return gen_inst_r, _Waiter(gen_inst_s), _Waiter(stimulus()), _Waiter(check())
Example #24
Source File: module.py From pyfilter with MIT License | 5 votes |
def __init__(self, *args: torch.Tensor): if len(args) == 0: self._cont = tuple() else: self._cont = tuple(args) if not isinstance(args[0], GeneratorType) else tuple(*args)
Example #25
Source File: driver.py From zun with Apache License 2.0 | 5 votes |
def get_archive(self, context, container, path): with docker_utils.docker_client() as docker: try: stream, stat = docker.get_archive( container.container_id, path) if isinstance(stream, types.GeneratorType): filedata = ''.encode("latin-1").join(stream) else: filedata = stream.read() return filedata, stat except errors.APIError as api_error: if is_not_found(api_error): raise exception.Invalid(_("%s") % str(api_error)) raise
Example #26
Source File: driver.py From zun with Apache License 2.0 | 5 votes |
def upload_image_data(self, context, img_id, data): """Upload an image.""" LOG.debug('Uploading an image to glance %s', img_id) try: if isinstance(data, types.GeneratorType): # NOTE(kiennt): In Docker-py 3.1.0, get_image # returns generator - related bugs [1]. # These lines makes image_data readable. # [1] https://bugs.launchpad.net/zun/+bug/1753080 data = ''.encode("latin-1").join(data) data = io.BytesIO(data) return utils.upload_image_data(context, img_id, data) except Exception as e: raise exception.ZunException(str(e))
Example #27
Source File: utils.py From Telethon with MIT License | 5 votes |
def is_list_like(obj): """ Returns `True` if the given object looks like a list. Checking ``if hasattr(obj, '__iter__')`` and ignoring ``str/bytes`` is not enough. Things like ``open()`` are also iterable (and probably many other things), so just support the commonly known list-like objects. """ return isinstance(obj, (list, tuple, set, dict, GeneratorType))
Example #28
Source File: sandbox.py From misp42splunk with GNU Lesser General Public License v3.0 | 5 votes |
def is_internal_attribute(obj, attr): """Test if the attribute given is an internal python attribute. For example this function returns `True` for the `func_code` attribute of python objects. This is useful if the environment method :meth:`~SandboxedEnvironment.is_safe_attribute` is overridden. >>> from jinja2.sandbox import is_internal_attribute >>> is_internal_attribute(str, "mro") True >>> is_internal_attribute(str, "upper") False """ if isinstance(obj, types.FunctionType): if attr in UNSAFE_FUNCTION_ATTRIBUTES: return True elif isinstance(obj, types.MethodType): if attr in UNSAFE_FUNCTION_ATTRIBUTES or \ attr in UNSAFE_METHOD_ATTRIBUTES: return True elif isinstance(obj, type): if attr == 'mro': return True elif isinstance(obj, (types.CodeType, types.TracebackType, types.FrameType)): return True elif isinstance(obj, types.GeneratorType): if attr in UNSAFE_GENERATOR_ATTRIBUTES: return True elif hasattr(types, 'CoroutineType') and isinstance(obj, types.CoroutineType): if attr in UNSAFE_COROUTINE_ATTRIBUTES: return True elif hasattr(types, 'AsyncGeneratorType') and isinstance(obj, types.AsyncGeneratorType): if attr in UNSAFE_ASYNC_GENERATOR_ATTRIBUTES: return True return attr.startswith('__')
Example #29
Source File: testing.py From tornado-zh with MIT License | 5 votes |
def __call__(self, *args, **kwargs): result = self.orig_method(*args, **kwargs) if isinstance(result, GeneratorType) or iscoroutine(result): raise TypeError("Generator and coroutine test methods should be" " decorated with tornado.testing.gen_test") elif result is not None: raise ValueError("Return value from test method ignored: %r" % result)
Example #30
Source File: testing.py From tornado-zh with MIT License | 5 votes |
def __call__(self, *args, **kwargs): result = self.orig_method(*args, **kwargs) if isinstance(result, GeneratorType) or iscoroutine(result): raise TypeError("Generator and coroutine test methods should be" " decorated with tornado.testing.gen_test") elif result is not None: raise ValueError("Return value from test method ignored: %r" % result)