Metadata-Version: 2.2
Name: stock_catcher
Version: 0.0.1
Summary: Read the latest stock news
Author-email: Pengfei <liu.pengfei@hotmail.fr>
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright [yyyy] [name of copyright owner]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
        
Project-URL: repository, https://github.com/pengfei99/py-packaging
Project-URL: documentation, https://github.com/pengfei99/py-packaging
Keywords: cac40,stock,finance
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pandas
Requires-Dist: yfinance
Requires-Dist: tomli; python_version < "3.13"
Provides-Extra: build
Requires-Dist: build; extra == "build"
Requires-Dist: twine; extra == "build"
Provides-Extra: dev
Requires-Dist: black; extra == "dev"
Requires-Dist: bumpver; extra == "dev"
Requires-Dist: isort; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Requires-Dist: pytest; extra == "dev"

# py-packaging

This repo shows how to package, build python app and publication on pypi (The python packaging index).

In this tutorial, you’ll learn how to:

- Prepare your Python package for publication
- Handle versioning of your package
- Build your package and upload it to PyPI
- Understand and use different build systems

The python packaging standard is **reviewed** by `Python Enhancement Proposals (PEPs)`, and **implemented** by the 
`Python Packaging Authority (PyPA)` working group.

The most important documents that define how Python packaging works are the following PEPs:

- [PEP 427](https://peps.python.org/pep-0427/) describes how wheels should be packaged.
- [PEP 440](https://peps.python.org/pep-0440/) describes how version numbers should be parsed.
- [PEP 508](https://peps.python.org/pep-0508/) describes how dependencies should be specified.
- [PEP 517](https://peps.python.org/pep-0517/) describes how a build backend should work.
- [PEP 518](https://peps.python.org/pep-0518/) describes how a build system should be specified.
- [PEP 621](https://peps.python.org/pep-0621/) describes how project metadata should be written.
- [PEP 660](https://peps.python.org/pep-0660/) describes how editable installs should be performed.

## 1. Prepare your Python package

There is no strict rule on how to organize the python package source. But we recommend you to use the below
organization.

```text
my_package/
│-- src/my_package/           # Package source code
│   │-- __init__.py       # Makes this a package
|   |-- __main__.py       # entry point of the package when run python -m my_package
│   │-- module1.py        # First module
│   │-- module2.py        # Second module
│   │-- subpackage1/       # A subpackage (optional)
│   │   │-- __init__.py
│   │   │-- submodule.py
│
│-- tests/                # Unit tests
│   │-- test_module1.py   # Tests for module1
│   │-- test_module2.py   # Tests for module2
│
│-- docs/                 # Documentation (optional)
│   │-- index.rst         # Main documentation file
│
│-- examples/             # Example scripts (optional)
│   │-- example_usage.py
│
|-- data/
|   |-- input.csv          # test input data
|   |-- output.parquet     # output example
|
│-- pyproject.toml        # Build system and dependency management (recommended)
│-- requirements.txt      # Dependencies list
│-- README.md             # Project overview and usage
│-- LICENSE               # License file
│-- .gitignore            # Git ignore file

```

> The s`rc/-` structure is recommended, it provides several advantages as your project grows. 
> You can read this [page](https://hynek.me/articles/testing-packaging/) for more information.
> You can also read this [article](https://realpython.com/python-application-layouts/) to learn different file layouts for
> different types of python application

### 1.1 src/my_package/ 

This folder contains all the source code. You can have simple modules (e.g. module1.py, etc.) or subpackages(subpackage1/, etc.)


The **__init__.py** defines what gets imported when import my_package is used.
The **__main__.py** is the entry point of the package. It will be executed when user run `python -m my_package`.

### 1.2 tests/

Contains unit tests using unittest or pytest.

### 1.3 docs/

It contains the documentation of the package. The format can be Sphinx documentation generated by the docstring or Markdown files.

### 1.4 examples/

Helps users understand how to use the package.

### 1.5 data/

The sample input data, or required data to run the package correctly.

###  1.6 Build System & Dependencies

pyproject.toml: Preferred way to define dependencies and package metadata.

requirements.txt: Lists dependencies for easy installation.

### 1.7 Metadata & Automation

README.md: Project documentation.
LICENSE: Defines the package’s license.
.gitignore: Specifies files to exclude from version control.


## 2. A real world example


Now we will use a real world project to simulate how to build a python package.

### 2.1 Configuration

You can find the `config.toml` in `src/stock_catcher`. We store the file name which contains 
the French CAC 40 stock ticker.

```toml
[stock]
cac_40="CAC40_2024.csv"
```
TOML is a configuration file format that has gained popularity lately. Python uses it for the pyproject.toml file that 
you’ll learn about later. To dive deeper into TOML, check out this [tutorial](https://realpython.com/python-toml/#read-toml-documents-with-tomli-and-tomllib).
`tomllib` library has been added to python standard library since python 3.11. So you should be able to read `toml`
easily in python.

TOML files contain `key-value pairs` separated into sections, or tables. Our config file contains only one 
section, `stock`, with one key, `cac_40`.


### 2.2 __init__.py

The `__init__.py` file represents the root of your package. It is executed `when the package is imported for the first 
time in a Python session.`

It should usually be kept quite simple, but it’s a good place to put package constants, documentation, and so on.

In our example, we use `importlib.resources` to get the path of `config.toml` based on the package. This is especially 
helpful when you publish your package to PyPI and don’t have full control over where your package is installed and 
how it’s used. Resource files might even end up inside zip archives.

```python

# Standard library imports
from importlib import resources

print("Executing __init__.py")

# Using try except ensures that you use tomllib if it’s available and that you fall back to tomli if it’s not.
try:
    import tomllib
except ModuleNotFoundError:
    # Third party imports
    import tomli as tomllib


# Version of the package
# Specifying __version__ is `strictly speaking` not necessary
# The importlib.metadata machinery uses project metadata to find version numbers. 
__version__ = "0.0.1"
# However, it’s still a useful convention to follow, and tools like Setuptools and Flit can use it to automatically


# Read the CAC40 stock ticker file path from the config file of package stock-catcher
_cfg = tomllib.loads(resources.read_text("stock_catcher", "config.toml"))

CAC40 = _cfg["stock"]["cac_40"]
```


### 2.3 __main__.py

The `__main__.py` is a special file inside a Python package that makes the `package executable using python -m package_name`.

Unlike `__init__.py`, it will not be executed during import. It will be only executed when `python -m my_package`
(run package as a script) is called

### 2.4 catcher.py

This module contains some functions which can get stock information and filter them based on certain user defined 
condition.

The below import is interesting. Because it will not work b default, because `stock_catcher` package is not recognized
in the current python env.

```python
from stock_catcher import CAC40
```

To install the `stock_catcher` package in current python virtual env. you can use the below command

```shell
# install the package in editable mode, any change will be updated into the python virtual env
pip install -e /path/to/package

# in our case
pip install -e /home/pliu/git/py-packaging/src/stock_catcher

# or you can go the folder, and call the command on current folder
cd /home/pliu/git/py-packaging
 python -m pip install -e .
```

> To do the pip install, it requires you to have a valid pyproject.toml.
> 
## 3. How to use a package

The most common way to call a python module in a package is call the python file of the module.
Below is an example
```shell
# if the module contains a main method, the main method will be called
python cacher.py

```

You can also use the -m option, it runs a script by specifying its module name instead of the filename:

```shell
# if the module contains a main method, the main method will be called
python -m cacher
```

## 4. Prepare the package for publication

### 4.1 Choose a catchy package name

All packages on PyPI need to have **unique names**. There are now several hundred thousand packages on PyPI, so 
chances are that your favorite name is already taken.

We will try to use the name `stock_catcher`, if it's already be taken, then we will add some prefix. For example
we can go `pf_stock_catcher`.

> The package name will be used to install the package(pip install stock_catcher).

**The PyPI package name does not need to match the package name**. For example, you can named the package as `stock_catcher`
in PyPI, after installation, the package can be called `catcher` for import(import catcher). 

The best practice is to keep them the same to avoid naming conflict. Because in one python environment, you cannot
have two packages which have the same name.


### 4.2 Configure a build system for your package

A **build system** is responsible for creating the actual files that you’ll upload to PyPI, typically in the [wheel](https://realpython.com/python-wheels/) 
or the [source distribution (sdist)](https://packaging.python.org/en/latest/specifications/source-distribution-format/) 
format. For a long time, this was done by [distutils](https://docs.python.org/3.11/library/distutils.html) or 
[setuptools](https://setuptools.pypa.io/). However, [PEP 517](https://peps.python.org/pep-0517/) and [PEP 518](https://peps.python.org/pep-0518/) 
introduced a way to specify `custom build systems` (e.g. Poetry, flit).

In this tutorial, we only show how to use **setuptools** as our `build system`. The main difference of these build systems
is how you configure your package and build command to build and upload the package. 


#### 4.2.1 The pyproject.toml

The `pyproject.toml` file specify the build system of the package. You can specify setuptools by adding the following 
lines to pyproject.toml:

```toml
[build-system]
requires      = ["setuptools>=61.0.0", "wheel"]
build-backend = "setuptools.build_meta"
```

The `minimal project information` that you must include in your `pyproject.toml` is the following:

- **name**: specifies the name of your package as it will appear on PyPI.
- **version**: sets the current version of your package.

All other attributes are optional, but we recommend you to provide as much information as possible.

- **classifiers**: describes your project using a list of classifiers. You should use these as they make your project more searchable.
- **dependencies**: lists any dependencies your package has to third-party libraries. `stock_cacher` depends on pandas
yfinance, and tomli.
- **project.urls**: adds links that you can use to present additional information about your package to your users. 
                  You can include several links here.
- **project.scripts**: creates command-line scripts that call functions within your package. 
          In our case, the stock_catcher command will call the main() within the `stock_catcher.__main__` module.
         
There are three ways to config the entry point of a package:
- **project.scripts**
- **project.gui-scripts**: specify GUI applications entry point
- **project.entry-points**: specify [plugins](https://packaging.python.org/en/latest/guides/creating-and-discovering-plugins/#using-package-metadata) entry point

