Giter Site home page Giter Site logo

democritus-project / d8s-python Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 4.0 184 KB

Democritus functions for working with Python ASTs.

License: GNU Lesser General Public License v3.0

Python 99.21% Dockerfile 0.45% Shell 0.34%
democritus utility python python-asts python-asts-utility ast python-ast abstract-syntax-tree

d8s-python's Introduction

Democritus Python

PyPI CI Lint codecov The Democritus Project uses semver version 2.0.0 The Democritus Project uses black to format code License: LGPL v3

Democritus functions[1] for working with Python data (code and ASTs).

[1] Democritus functions are simple, effective, modular, well-tested, and well-documented Python functions.

We use d8s (pronounced "dee-eights") as an abbreviation for democritus (you can read more about this here).

Installation

pip install d8s-python

Usage

You import the library like:

from d8s_python import *

Once imported, you can use any of the functions listed below.

Functions

  • def python_functions_signatures(
        code_text: str,
        *,
        ignore_private_functions: bool = False,
        ignore_nested_functions: bool = False,
        keep_function_name: bool = False,
    ) -> List[str]:
        """Return the function signatures for all of the functions in the given code_text."""
  • def python_todos(code_text: str, todo_regex: str = 'TODO:.*') -> List[str]:
        """Return all todos in the given code_text that match the given todo_regex."""
  • def python_make_pythonic(name: str) -> str:
        """Make the name pythonic.
    
    (e.g. 'fooBar' => 'foo_bar', 'foo-bar' => 'foo_bar', 'foo bar' => 'foo_bar', 'Foo Bar' => 'foo_bar')."""
  • def python_namespace_has_argument(namespace: argparse.Namespace, argument_name: str) -> bool:
        """."""
  • def python_traceback_prettify(traceback: str) -> str:
        """Return a string with the given traceback pretty-printed."""
  • def python_traceback_pretty_print(traceback: str) -> None:
        """Return a string with the given traceback pretty-printed."""
  • def python_clean(code_text: str) -> str:
        """Clean python code as it is often found in documentation and snippets."""
  • def python_function_blocks(
        code_text: str, *, ignore_private_functions: bool = False, ignore_nested_functions: bool = False
    ) -> List[str]:
        """Find the code (as a string) for every function in the given code_text."""
  • def python_line_count(python_code: str, *, ignore_empty_lines: bool = True) -> int:
        """Return the number of lines in the given function_text."""
  • def python_function_lengths(code_text: str) -> List[int]:
        """Find the lengths of each function in the given code_text."""
  • def python_version() -> str:
        """Return the python version of the current environment."""
  • def python_is_version_2() -> bool:
        """Return whether or not the python version of the current environment is v2.x."""
  • def python_is_version_3() -> bool:
        """Return whether or not the python version of the current environment is v3.x."""
  • def python_files_using_function(function_name: str, search_path: str) -> List[str]:
        """Find where the given function is used in the given search path."""
  • def python_keywords() -> List[str]:
        """Get a list of the python keywords."""
  • def python_object_properties_enumerate(
        python_object: Any, *, run_methods: bool = True, internal_properties: bool = True
    ) -> None:
        """Enumerate and print out the properties of the given object."""
  • def python_copy_deep(python_object: Any) -> Any:
        """Return a deep (complete, recursive) copy of the given python object."""
  • def python_copy_shallow(python_object: Any) -> Any:
        """Return shallow copy of the given python object."""
  • def python_file_names(path: str, *, exclude_tests: bool = False) -> List[str]:
        """Find all python files in the given directory."""
  • def python_fstrings(code_text: str, *, include_braces: bool = False) -> Iterator[str]:
        """Find all of the python formatted string literals in the given text. See https://realpython.com/python-f-strings/ for more details about f-strings."""
  • def python_code_details(code_text: str):
        """Get details about the given code_text. This is a wrapper for `dis.code_info`"""
  • def python_disassemble(code_text: str):
        """Disassemble the python code_text. This is a wrapper for `dis.dis`"""
  • def python_stack_local_data():
        """Get local data in the current python environment."""
  • def python_object_doc_string(python_object: Any) -> Union[str, None]:
        """Get the doc string for the given python object (e.g. module, function, or class)."""
  • def python_object_source_file(python_object: Any) -> str:
        """Get the source file for the given python object (e.g. module, function, or class)."""
  • def python_object_module(python_object: Any) -> str:
        """Get the module for the given python object (e.g. function or class)."""
  • def python_object_source_code(python_object: Any) -> str:
        """Get the source code for the given python object (e.g. module, function, or class)."""
  • def python_object_signature(python_object: Any) -> str:
        """Get the argument signature for the given python object (e.g. module, function, or class)."""
  • def python_sort_type_list_by_name(python_type_list: List[type], **kwargs) -> List[type]:
        """."""
  • def python_type_name(python_type: type) -> str:
        """Return the common name of the given type."""
  • def python_object_type_to_word(python_object: Any) -> str:
        """Convert the given python type to a string."""
  • def python_ast_raise_name(node: ast.Raise) -> Optional[str]:
        """Get the name of the exception raise by the given ast.Raise object."""
  • def python_ast_exception_handler_exceptions_handled(handler: ast.ExceptHandler) -> Optional[Iterable[str]]:
        """Return all of the exceptions handled by the given exception handler."""
  • def python_ast_exception_handler_exceptions_raised(handler: ast.ExceptHandler) -> Optional[Iterable[str]]:
        """Return the exception raised by the given exception handler."""
  • def python_exceptions_handled(code_text: str) -> Iterable[str]:
        """Return a list of all exceptions handled in the given code."""
  • def python_exceptions_raised(code_text: str) -> Iterable[str]:
        """Return a list of all exceptions raised in the given code."""
  • def python_functions_as_import_string(code_text: str, module_name: str) -> str:
        """."""
  • def python_ast_object_line_number(ast_object: object) -> Optional[int]:
        """."""
  • def python_ast_object_line_numbers(ast_object: object) -> Tuple[int, int]:
        """."""
  • def python_ast_objects_of_type(
        code_text_or_ast_object: Union[str, object], ast_type: type, *, recursive_search: bool = True
    ) -> Iterable[object]:
        """Return all of the ast objects of the given ast_type in the code_text_or_ast_object."""
  • def python_ast_objects_not_of_type(code_text_or_ast_object: Union[str, object], ast_type: type) -> Iterable[object]:
        """Return all of the ast objects which are not of the given ast_type in the code_text_or_ast_object."""
  • def python_ast_parse(code_text: str) -> ast.Module:
        """."""
  • def python_ast_function_defs(code_text: str, recursive_search: bool = True) -> Iterable[ast.FunctionDef]:
        """."""
  • def python_function_arguments(function_text: str) -> List[ast.arg]:
        """."""
  • def python_function_argument_names(function_text: str) -> Iterable[str]:
        """."""
  • def python_function_argument_defaults(function_text: str) -> List[str]:
        """."""
  • def python_function_argument_annotations(function_text: str) -> List[str]:
        """."""
  • def python_function_names(
        code_text: str, *, ignore_private_functions: bool = False, ignore_nested_functions: bool = False
    ) -> List[str]:
        """."""
  • def python_function_docstrings(
        code_text: str, *, ignore_private_functions: bool = False, ignore_nested_functions: bool = False
    ) -> List[str]:
        """Get docstrings for all of the functions in the given text."""
  • def python_variable_names(code_text: str) -> List[str]:
        """Get all of the variables names in the code_text."""
  • def python_constants(code_text: str) -> List[str]:
        """Get all constants in the code_text."""

Development

๐Ÿ‘‹ ย If you want to get involved in this project, we have some short, helpful guides below:

If you have any questions or there is anything we did not cover, please raise an issue and we'll be happy to help.

Credits

This package was created with Cookiecutter and Floyd Hightower's Python project template.

d8s-python's People

Contributors

dependabot[bot] avatar fhightower avatar kshru9 avatar mcallesen avatar

Stargazers

 avatar

Watchers

 avatar  avatar

d8s-python's Issues

Can i get more information about this project

I want to colaborate on this project, but i dont really understand it.
There are no documentation about it, you should think about creating one...
Also, d8s-python file functions are so messy....
I hope to receive a reply soon.

Add "ignore_nested_functions" flag to python_function_... functions

What?

A boolean flag named ignore_nested_functions which defaults to False.

This should be applied to at least the python_function_names and python_function_docstrings functions.

Here are some tests which should pass:

s = '''def f(a):
    def sq(i):
        return i * i
    return sq(a)

assert python_function_names(s) == ['f', 'sq']

assert python_function_names(s, ignore_nested_functions=True) == ['f']

Why?

There are times (in the case of a decorator, for example, where it is helpful to ignore nested functions).

How?

Not sure yet...

Update "python_file_names" function to have "recursive" boolean argument

I don't have time right now to scope this out entirely, but I would like to create a python_files function that lets a user find python files in a given directory. The function should be very similar to the python_file_names function and should take a recursive kwarg as well as a return_file_paths kwarg which determines whether file names or file paths are returned.

In the future, I can update this issue to have the text below:


HELP WANTED ๐Ÿ‘‹ : If you'd like to take this challenge on, please let me know! Even if you're new to Python and/or Github, this is a great place to start and I'd be happy to help walk you through this challenge as much as you need - don't hesitate to ask.

This particular issue will give you some exposure to updating a function's arguments, using type hints in Python, and the Pathlib library.


What?

We should update the python_file_names function to have a boolean recursive argument that lets the user specify whether or not he/she would like to search for Python files in the given directory recursively or not. The function signature is currently:

python_file_names(path: str, *, exclude_tests: bool = False) -> List[str]:

but should be updated to:

python_file_names(path: str, *, exclude_tests: bool = False, recursive: bool = False) -> List[str]:

Why?

How?

Additional thoughts

Update the "python_file_names" function to use pathlib globing

HELP WANTED ๐Ÿ‘‹ : If you'd like to take this challenge on, please let me know! Even if you're new to Python and/or Github, this is a great place to start and I'd be happy to help walk you through this challenge as much as you need - don't hesitate to ask.

This particular issue will give you some exposure to update a function to use the Pathlib library.


We should update the python_data.python_file_names function to use the Path.glob function.

Write functions to list all imports in a given code string

I would like a python_imports function to list all packages (and, if present, functions/variables) being imported:

s = '''from democritus_dates import date_parse, date_now
import requests

print(date_parse('2 days from now'))
'''

python_package_imports(s) => # {'democritus_dates': ['date_parse', 'date_now'], 'requests': []}

Function Signature

The function should accept a string argument and return a dict with package names as keys and with a list containing each explicitly imported function/variable name for each package (see the example above).

The annotated signature should look something like:

def python_package_imports(code: str) -> Dict[str, List[str]]:
    ...

code execution backdoor

We discovered a potential code execution backdoor in version 0.1.0 of the project, the backdoor is the democritus-strings package. Attackers can upload democritus-strings packages containing arbitrary malicious code. For the safety of this project, the democritus-strings package has been uploaded by us.

image

The democritus-strings package can be successfully installed using pip install d8s-python==0.1.0

image

Suggestion: remove version 0.1.0 of this project in PyPI

Write function to find number of lines in python code

HELP WANTED ๐Ÿ‘‹ : If you'd like to take this challenge on, please let me know! Even if you're new to Python and/or Github, this is a great place to start and I'd be happy to help walk you through this challenge as much as you need - don't hesitate to ask.

This particular issue will give you some exposure to writing a simple function and writing tests for that function.


What?

I would like a function to find the number of lines in Python code.

I envision the signature of such a function looking like:

def python_line_count(python_code: str, *, ignore_empty_lines: bool = True) -> int:

Why?

Counting lines of code is a common use-case.

How?

This function can be modeled on the python_function_line_count function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.