def generate_attribute(max_depth=None, ctx=ast.Load()): value = generate_expression(max_depth=max_depth - 1) attr = generate_variable_name() return ast.Attribute(value, attr, ctx)
def _generate_keyword(max_depth=None, starred=False): argname = generate_variable_name() if not starred else None value = generate_expression(max_depth=max_depth - 1) return ast.keyword(arg=argname, value=value)
def _generate_except_handler(max_depth=None): type = generate_variable_or_tuple() name = random.choice([generate_variable_name(), None]) body = generate_block(max_depth=max_depth) return ast.ExceptHandler(type, name, body)
def generate_variable(max_depth=None, ctx=ast.Load()): name = generate_variable_name() return ast.Name(name, ctx)
def _generate_module_name(): num_parts = random.randrange(1, 4) names = [generate_variable_name() for _ in range(num_parts)] return '.'.join(names)
def _generate_alias(): name = generate_variable_name() asname = random.choice([generate_variable_name(), None]) return ast.alias(name, asname)
def generate_nonlocal(max_depth=None): num_names = random.choice([1, 1, 1, 1, 1, 2, 2, 3]) names = [generate_variable_name() for _ in range(num_names)] return ast.Nonlocal(names)