Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some docs #21

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,52 @@ stmdency, **sta**tement depen**dency** is a Python library for extracting depend
python -m pip install --upgrade stmdency
```

## Usage

Let's say we have a Python script named `test.py` with the following content:

```python
a = 1
b = 2

def bar():
b = a + 3
print(a, b)

def foo():
bar(b)
```

We want to extract function `foo` and all its dependencies. `stmdency` can do this for us:

```python
from stmdency.extractor import Extractor

with open("test.py", "r") as f:
source = f.read()
extractor = Extractor(source)
print(extractor.get_code("foo"))
```

The output will be:

```python
a = 1

def bar():
b = a + 3
print(a, b)

b = 2

def foo():
bar(b)
```

## Documentation

The documentation host on read the doc and is available at [https://stmdency.readthedocs.io](https://stmdency.readthedocs.io).
The documentation host read the doc and is available at [https://stmdency.readthedocs.io](https://stmdency.readthedocs.io).

## Who is using stmdency?

- [dolphinscheduler-sdk-python](https://github.com/apache/dolphinscheduler-sdk-python): Python API to manage Dolphinscheduler workflow by code, aka PyDolphinscheduler.
114 changes: 96 additions & 18 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Stmdency's Documentation
========================

Stmdency, **ST** ate **M** ents depen **DENCY**, a tool handling python statements' dependencies, can
extract dependencies from python statements according to the given statement identifier.
extract dependencies from Python statements according to the given statement identifier.

Installation
------------
Expand All @@ -16,12 +16,25 @@ Stmdency can be installed from PyPI using pip:

.. code-block:: bash

python -m pip install stmdency
python -m pip install --upgrade stmdency

Usage
-----

Stmdency can be used as a python module(as a command line tool will comming soon).
Stmdency can be used as a Python module(as a command line tool will coming soon).

Extract Variable Dependencies
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Let's say we have a python code like below, we define a variable ``a`` and ``b`` and ``b`` depends on ``a``:

.. code-block:: python

a = 1
b = a + 2

Now we want to extract variable ``b`` and all of its dependencies, and we want to make sure our extracted code
can be executed. Stmdency can help us to do this:

.. code-block:: python

Expand All @@ -31,13 +44,40 @@ Stmdency can be used as a python module(as a command line tool will comming soon
a = 1
b = a + 2
"""

extractor = Extractor(source=statement)
print(extractor.get_code("b"))
# a = 1
#
# b = a + 2
result = extractor.get_code("b")
print(result)

The result will be:

.. code-block:: python

a = 1

b = a + 2


or for function name:
Extract Function Dependencies
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Stmdency not only can extract variable dependencies, but also can extract function dependencies. Suppose we
have the script below.

.. code-block:: python

a = 1
b = 2

def bar():
b = a + 3
print(a, b)

def foo():
bar(b)

Now we want to extract function ``foo`` and all of its dependencies, and we want to make our extracted code
runnable.

.. code-block:: python

Expand All @@ -54,16 +94,54 @@ or for function name:
"""
extractor = Extractor(source=statement)
print(extractor.get_code("foo"))
# a = 1
#
# b = 2
#
# def bar():
# b = a + 3
# print(a, b)
#
# def foo():
# bar(b)

The result will be:

.. code-block:: python

a = 1

b = 2

def bar():
b = a + 3
print(a, b)

def foo():
bar(b)

Python Code in File
~~~~~~~~~~~~~~~~~~~

As you can see, we use a string to represent the Python code in the above examples. But in most cases, our code
is in a file. Stmdency can also handle this situation.

First, we need to create a file named ``test.py`` and write the code below into it:

.. code-block:: bash

cat <<EOF > test.py
a = 1
b = 2

def bar():
b = a + 3
print(a, b)

def foo():
bar(b)
EOF

Then we can use the code below to extract function ``foo`` and all of its dependencies:

.. code-block:: python

from stmdency.extractor import Extractor

with open("test.py", "r") as f:
source = f.read()
extractor = Extractor(source)
print(extractor.get_code("foo"))

Indices and tables
==================
Expand Down