晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
| DIR:/opt/cloudlinux/venv/lib64/python3.11/site-packages/astroid/brain/ |
| Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/astroid/brain/brain_random.py |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
from __future__ import annotations
import random
from astroid import helpers
from astroid.context import InferenceContext
from astroid.exceptions import UseInferenceDefault
from astroid.inference_tip import inference_tip
from astroid.manager import AstroidManager
from astroid.nodes.node_classes import (
Attribute,
Call,
Const,
EvaluatedObject,
List,
Name,
Set,
Tuple,
)
ACCEPTED_ITERABLES_FOR_SAMPLE = (List, Set, Tuple)
def _clone_node_with_lineno(node, parent, lineno):
if isinstance(node, EvaluatedObject):
node = node.original
cls = node.__class__
other_fields = node._other_fields
_astroid_fields = node._astroid_fields
init_params = {"lineno": lineno, "col_offset": node.col_offset, "parent": parent}
postinit_params = {param: getattr(node, param) for param in _astroid_fields}
if other_fields:
init_params.update({param: getattr(node, param) for param in other_fields})
new_node = cls(**init_params)
if hasattr(node, "postinit") and _astroid_fields:
new_node.postinit(**postinit_params)
return new_node
def infer_random_sample(node, context: InferenceContext | None = None):
if len(node.args) != 2:
raise UseInferenceDefault
inferred_length = helpers.safe_infer(node.args[1], context=context)
if not isinstance(inferred_length, Const):
raise UseInferenceDefault
if not isinstance(inferred_length.value, int):
raise UseInferenceDefault
inferred_sequence = helpers.safe_infer(node.args[0], context=context)
if not inferred_sequence:
raise UseInferenceDefault
if not isinstance(inferred_sequence, ACCEPTED_ITERABLES_FOR_SAMPLE):
raise UseInferenceDefault
if inferred_length.value > len(inferred_sequence.elts):
# In this case, this will raise a ValueError
raise UseInferenceDefault
try:
elts = random.sample(inferred_sequence.elts, inferred_length.value)
except ValueError as exc:
raise UseInferenceDefault from exc
new_node = List(lineno=node.lineno, col_offset=node.col_offset, parent=node.scope())
new_elts = [
_clone_node_with_lineno(elt, parent=new_node, lineno=new_node.lineno)
for elt in elts
]
new_node.postinit(new_elts)
return iter((new_node,))
def _looks_like_random_sample(node) -> bool:
func = node.func
if isinstance(func, Attribute):
return func.attrname == "sample"
if isinstance(func, Name):
return func.name == "sample"
return False
AstroidManager().register_transform(
Call, inference_tip(infer_random_sample), _looks_like_random_sample
)
|