本文整理匯總了Python中inspect.isgenerator方法的典型用法代碼示例。如果您正苦於以下問題:Python inspect.isgenerator方法的具體用法?Python inspect.isgenerator怎麽用?Python inspect.isgenerator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類inspect
的用法示例。
在下文中一共展示了inspect.isgenerator方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: is_closable_iterator
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def is_closable_iterator(obj):
"""Detect if the given object is both closable and iterator."""
# Not an iterator.
if not is_iterator(obj):
return False
# A generator - the easiest thing to deal with.
import inspect
if inspect.isgenerator(obj):
return True
# A custom iterator. Look for a close method...
if not (hasattr(obj, 'close') and callable(obj.close)):
return False
# ... which doesn't require any arguments.
try:
inspect.getcallargs(obj.close)
except TypeError:
return False
else:
return True
示例2: run_sync
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def run_sync(func: Callable[..., Any]) -> Callable[..., Coroutine[Any, None, None]]:
"""Ensure that the sync function is run within the event loop.
If the *func* is not a coroutine it will be wrapped such that
it runs in the default executor (use loop.set_default_executor
to change). This ensures that synchronous functions do not
block the event loop.
"""
@wraps(func)
async def _wrapper(*args: Any, **kwargs: Any) -> Any:
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(
None, copy_context().run, partial(func, *args, **kwargs)
)
if isgenerator(result):
return run_sync_iterable(result) # type: ignore
else:
return result
_wrapper._quart_async_wrapper = True # type: ignore
return _wrapper
示例3: store_fields
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def store_fields(index, node, query_fields, ids, query_result):
if inspect.isgenerator(query_result):
warnings.warn('Data loading functions should not return generators',
DeprecationWarning)
query_result = list(query_result)
_check_store_fields(node, query_fields, ids, query_result)
names = [f.index_key for f in query_fields]
if node.name is not None:
assert ids is not None
node_idx = index[node.name]
for i, row in zip(ids, query_result):
node_idx[i].update(zip(names, row))
else:
assert ids is None
index.root.update(zip(names, query_result))
示例4: atleast_list
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def atleast_list(a):
"""
Promote an object to a list if not a list or generator.
Parameters
----------
a: object
any object you want to at least be a list with one element
Returns
-------
list or generator:
untounched if :code:`a` was a generator or list, otherwise :code:`[a]`.
Examples
--------
>>> a = 1.
>>> atleast_list(a)
[1.0]
>>> a = [1.]
>>> atleast_list(a)
[1.0]
"""
return a if isinstance(a, list) or isgenerator(a) else [a]
示例5: atleast_tuple
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def atleast_tuple(a):
"""
Promote an object to a tuple if not a tuple or generator.
Parameters
----------
a: object
any object you want to at least be a tuple with one element
Returns
-------
tuple or generator:
untounched if :code:`a` was a generator or tuple, otherwise
:code:`(a,)`.
Examples
--------
>>> a = 1.
>>> atleast_tuple(a)
(1.0,)
>>> a = (1.,)
>>> atleast_tuple(a)
(1.0,)
"""
return a if isinstance(a, tuple) or isgenerator(a) else (a,)
示例6: test_excluding_predicates
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def test_excluding_predicates(self):
self.istest(inspect.isbuiltin, 'sys.exit')
self.istest(inspect.isbuiltin, '[].append')
self.istest(inspect.iscode, 'mod.spam.func_code')
self.istest(inspect.isframe, 'tb.tb_frame')
self.istest(inspect.isfunction, 'mod.spam')
self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
self.istest(inspect.ismethod, 'git.argue')
self.istest(inspect.ismodule, 'mod')
self.istest(inspect.istraceback, 'tb')
self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
self.istest(inspect.isgenerator, '(x for x in xrange(2))')
self.istest(inspect.isgeneratorfunction, 'generator_function_example')
if hasattr(types, 'GetSetDescriptorType'):
self.istest(inspect.isgetsetdescriptor,
'type(tb.tb_frame).f_locals')
else:
self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
if hasattr(types, 'MemberDescriptorType'):
self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
else:
self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
示例7: append
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def append(self, row):
"""
:param row: iterable containing values to append
:type row: iterable
"""
if (not isgenerator(row) and
not isinstance(row, (list, tuple, range))
):
self._invalid_row(row)
self._max_row += 1
if self.writer is None:
self.writer = self._write_header()
next(self.writer)
try:
self.writer.send(row)
except StopIteration:
self._already_saved()
示例8: test_iscoroutine
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def test_iscoroutine(self):
gen_coro = gen_coroutine_function_example(1)
coro = coroutine_function_example(1)
self.assertFalse(
inspect.iscoroutinefunction(gen_coroutine_function_example))
self.assertFalse(inspect.iscoroutine(gen_coro))
self.assertTrue(
inspect.isgeneratorfunction(gen_coroutine_function_example))
self.assertTrue(inspect.isgenerator(gen_coro))
self.assertTrue(
inspect.iscoroutinefunction(coroutine_function_example))
self.assertTrue(inspect.iscoroutine(coro))
self.assertFalse(
inspect.isgeneratorfunction(coroutine_function_example))
self.assertFalse(inspect.isgenerator(coro))
coro.close(); gen_coro.close() # silence warnings
示例9: cleaned_uid_set
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def cleaned_uid_set(uid_set: str or [str] or iter) -> str:
"""
Prepare set of uid for use in commands: delete/copy/move/seen
uid_set may be:
str, that is comma separated uids
Iterable, that contains str uids
Generator with "fetch" name, implicitly gets all non-empty uids
"""
if type(uid_set) is str:
uid_set = uid_set.split(',')
if inspect.isgenerator(uid_set) and getattr(uid_set, '__name__', None) == 'fetch':
uid_set = tuple(msg.uid for msg in uid_set if msg.uid)
try:
uid_set_iter = iter(uid_set)
except TypeError:
raise ValueError('Wrong uid type: "{}"'.format(type(uid_set)))
for uid in uid_set_iter:
if type(uid) is not str:
raise ValueError('uid "{}" is not string'.format(str(uid)))
if not uid.strip().isdigit():
raise ValueError('Wrong uid: "{}"'.format(uid))
return ','.join((i.strip() for i in uid_set))
示例10: default
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat() + 'Z'
if hasattr(obj, "to_json"):
return self.default(obj.to_json())
elif hasattr(obj, "__dict__"):
d = dict(
(key, value)
for key, value in inspect.getmembers(obj)
if not key.startswith("_")
and not inspect.isabstract(value)
and not inspect.isbuiltin(value)
and not inspect.isfunction(value)
and not inspect.isgenerator(value)
and not inspect.isgeneratorfunction(value)
and not inspect.ismethod(value)
and not inspect.ismethoddescriptor(value)
and not inspect.isroutine(value)
)
return self.default(d)
return obj
示例11: cached_property
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def cached_property(prop):
"""
A replacement for the property decorator that will only compute the
attribute's value on the first call and serve a cached copy from
then on.
"""
def cache_wrapper(self):
if not hasattr(self, "_cache"):
self._cache = {}
if prop.__name__ not in self._cache:
return_value = prop(self)
if isgenerator(return_value):
return_value = tuple(return_value)
self._cache[prop.__name__] = return_value
return self._cache[prop.__name__]
return property(cache_wrapper)
示例12: is_closable_iterator
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def is_closable_iterator(obj):
# Not an iterator.
if not is_iterator(obj):
return False
# A generator - the easiest thing to deal with.
import inspect
if inspect.isgenerator(obj):
return True
# A custom iterator. Look for a close method...
if not (hasattr(obj, 'close') and callable(obj.close)):
return False
# ... which doesn't require any arguments.
try:
inspect.getcallargs(obj.close)
except TypeError:
return False
else:
return True
示例13: run
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def run(self, context):
try:
method = self._get_client_method(self._get_client(context))
result = method(**self._kwargs_for_run)
if inspect.isgenerator(result):
return [v for v in result]
return result
except Exception as e:
# Print the traceback for the last exception so that we can see
# where the issue comes from.
LOG.warning(traceback.format_exc())
raise exc.ActionException(
"%s.%s failed: %s" %
(self.__class__.__name__, self.client_method_name, str(e))
)
示例14: allValuesToInts
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def allValuesToInts(sequenceOrVal):
if isinstance(sequenceOrVal, HArrayVal):
sequenceOrVal = sequenceOrVal.val
if isinstance(sequenceOrVal, (Value, Bits3val)):
return valToInt(sequenceOrVal)
elif not sequenceOrVal:
return sequenceOrVal
elif (isinstance(sequenceOrVal, (list, tuple, deque))
or isgenerator(sequenceOrVal)):
seq = []
for i in sequenceOrVal:
seq.append(allValuesToInts(i))
if isinstance(sequenceOrVal, tuple):
return tuple(seq)
return seq
else:
return sequenceOrVal
示例15: test_get_all
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isgenerator [as 別名]
def test_get_all():
mock_adapter = {}
mock_adapter["prefix"] = PREFIX
adapter = requests_mock.Adapter()
mock_adapter["adapter"] = adapter
client = Socrata(DOMAIN, APPTOKEN, session_adapter=mock_adapter)
setup_mock(adapter, "GET", "bike_counts_page_1.json", 200, query="$offset=0")
setup_mock(adapter, "GET", "bike_counts_page_2.json", 200, query="$offset=1000")
response = client.get_all(DATASET_IDENTIFIER)
assert inspect.isgenerator(response)
data = list(response)
assert len(data) == 1001
assert data[0]["date"] == "2016-09-21T15:45:00.000"
assert data[-1]["date"] == "2016-10-02T01:45:00.000"
client.close()