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

[Code Style] Added pre-commit and code formatter #52

Merged
merged 4 commits into from
Oct 6, 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ test/optional
log
for_debug.txt

other/
other/
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
exclude: |
(?x)^(
other/.+|
doc/.+|
)$
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-added-large-files
# For Python files
- repo: https://github.com/psf/black.git
rev: 23.9.1
hooks:
- id: black
files: (.*\.(py|pyi|bzl)|BUILD|.*\.BUILD|WORKSPACE)$
- repo: https://github.com/pycqa/isort
rev: 5.11.5
hooks:
- id: isort
args: ["--profile", "black"]
18 changes: 17 additions & 1 deletion doc/docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ sidebar_position: 1
pip install neetbox
```

If you want to enable torch-related feature, please try below:

```bash
pip install neetbox[torch]
```

Since NEETBOX is under ~~heavy~~ development, it's better to forcely reinstall the newest version:

```bash
Expand All @@ -36,4 +42,14 @@ __NEETBox contains useless code snippets for Deep Learning Researchers.__ The re
So here is the thing: I'm new to complex Computer Vision projects and I found myself writing duplicate codes somehow, about convolutional neural networking, figure ploting(for paper writing), visualizing, data processing, etc., in different projects. So I'm going to have code snippets for myself and for other beginners who would like to have one. Certainly there are a lot of extraordinarily frameworks with high performance models integrated in their model hub (or stuff like that). However, that's not what I really want. Personally, I would like to have a collection of standalone code snippets which you can easily plug into your code without importing heavy dependency or doing code migration.


I would appreciate it if you would contribute your excellent tiny code snippets. There is no such ~~"Code of conduct"~~ here (lol) as long as the code runs buglessly. Leave your code here with simply a doc related to it would be helpful.
I would appreciate it if you would contribute your excellent tiny code snippets. There is no such ~~"Code of conduct"~~ here (lol) as long as the code runs buglessly. Leave your code here with simply a doc related to it would be helpful.

## Contribution

if you want to make this repository greater again, you should follow the step below to setup your development environment.

1. Make sure you are in the correct python version.
2. Run `poetry install` to setup your environment.
3. Contribute your code and pull request!!!!
4. Ask [VisualDust](mailto://gavin@gong.host) or [PaperCube](mailto://imzhy@hotmail.com) to review your change.
5. Merge!!!!
2 changes: 1 addition & 1 deletion neetbox.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ name = "FakeConfig"

[daemon]
enable = true
allowIpython = true
allowIpython = true
12 changes: 9 additions & 3 deletions neetbox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os

import toml
from neetbox.config import default as default_config, get_module_level_config

from neetbox.config import default as default_config
from neetbox.config import get_module_level_config
from neetbox.config._config import update_with
from neetbox.daemon import _try_attach_daemon
from neetbox.utils.framing import get_frame_module_traceback
Expand Down Expand Up @@ -70,8 +73,11 @@ def init(path=None, load=False, **kwargs) -> bool:
logger.err(f"Failed to load config from {config_file_path}: {e}")
return False

is_in_daemon_process = 'NEETBOX_DAEMON_PROCESS' in os.environ and os.environ['NEETBOX_DAEMON_PROCESS'] == '1'

is_in_daemon_process = (
"NEETBOX_DAEMON_PROCESS" in os.environ
and os.environ["NEETBOX_DAEMON_PROCESS"] == "1"
)
# print('prevent_daemon_loading =', is_in_daemon_process)
if os.path.isfile(config_file_name) and not is_in_daemon_process: # if in a workspace
init(load=True)

7 changes: 4 additions & 3 deletions neetbox/cli/parse.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import argparse
import os
import json
import os

import neetbox
from neetbox.logging.logger import Logger
from neetbox.logging.formatting import LogStyle
from neetbox.daemon._apis import get_status_of
from neetbox.logging.formatting import LogStyle
from neetbox.logging.logger import Logger

_log_style = LogStyle()
_log_style.with_datetime = False
Expand Down
17 changes: 11 additions & 6 deletions neetbox/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@
# URL: https://gong.host
# Date: 20230413

import inspect
from typing import Optional, Union

from neetbox.config._config import DEFAULT_CONFIG as default
from neetbox.config._config import get_current
from neetbox.utils.framing import *
from typing import Union, Optional
import inspect


def get_module_level_config(module=None):
try:
module = module or get_frame_module_traceback(traceback=2).__name__ # try to trace if module not given
if type(module) is not str: # try to trace the belonging module of the given object
module = (
module or get_frame_module_traceback(traceback=2).__name__
) # try to trace if module not given
if (
type(module) is not str
): # try to trace the belonging module of the given object
module = inspect.getmodule(module).__name__
except:
module = '@' # faild to trace, returning all configs
module = "@" # faild to trace, returning all configs
the_config = get_current()
sub_module_names = module.split(".")
if len(sub_module_names):
if sub_module_names[0] == 'neetbox':
if sub_module_names[0] == "neetbox":
# skip 'neetbox'
sub_module_names.pop(0)
for sub_module in sub_module_names:
Expand Down
7 changes: 4 additions & 3 deletions neetbox/config/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
# Date: 20230413

import collections
from neetbox.utils.mvc import patch
from multiprocessing import current_process

from neetbox.utils.mvc import patch

DEFAULT_CONFIG = {
"name": None,
"version": None,
Expand All @@ -30,9 +31,9 @@
"displayName": None,
"uploadInterval": 10,
"mute": True,
"launcher":{
"launcher": {
"port": 20202,
}
},
},
}
WORKSPACE_CONFIG: dict = DEFAULT_CONFIG.copy()
Expand Down
2 changes: 1 addition & 1 deletion neetbox/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from neetbox.core.registry import Registry

__all__ = ["Registry"]
__all__ = ["Registry"]
10 changes: 5 additions & 5 deletions neetbox/core/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
# URL: https://gong.host
# Date: 20230413

from neetbox.logging import logger
from neetbox.utils.format import *
from typing import Optional, Union, Sequence
import json
import functools
import json
from typing import Any, Dict, List, Optional, Sequence, Union

from neetbox.logging import logger
from neetbox.utils.format import *


class _RegEndpoint:
def __init__(self, what, tags=None):
Expand Down Expand Up @@ -196,5 +196,5 @@ def __str__(self) -> str:
separators=(",", ":"),
default=str,
)

__repr__ = __str__
13 changes: 7 additions & 6 deletions neetbox/daemon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
# URL: https://gong.host
# Date: 20230414

import json
import os
import platform
import subprocess
import time

from neetbox.daemon._daemon_client import connect_daemon
from neetbox.daemon.daemonable_process import DaemonableProcess
from neetbox.logging import logger
from neetbox.pipeline import listen, watch
from neetbox.utils import pkg
from neetbox.pipeline import watch, listen
import subprocess
import platform
import time
import os
import json


def __attach_daemon(daemon_config):
Expand Down
19 changes: 13 additions & 6 deletions neetbox/daemon/_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,33 @@
# Date: 20230414


from neetbox.daemon._local_http_client import _local_http_client
from neetbox.utils import pkg
from neetbox.utils.framing import get_frame_module_traceback
from neetbox.daemon._local_http_client import _local_http_client

module_name = get_frame_module_traceback().__name__
assert pkg.is_installed('httpx', try_install_if_not=True), f"{module_name} requires httpx which is not installed"
assert pkg.is_installed(
"httpx", try_install_if_not=True
), f"{module_name} requires httpx which is not installed"
import json
import time

import httpx

from neetbox.config import get_module_level_config
from neetbox.logging import logger
import time
import json

logger = logger("NEETBOX DAEMON API")

__cfg = get_module_level_config()
daemon_address = f"{__cfg['server']}:{__cfg['port']}"
base_addr = f"http://{daemon_address}"



def get_status_of(name=None):
name = name or ""
api_addr = f"{base_addr}/status"
logger.info(f"Fetching from {api_addr}")
r = _local_http_client.get(api_addr)
_data = r.json()
return _data
return _data
15 changes: 10 additions & 5 deletions neetbox/daemon/_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@

from neetbox.utils import pkg
from neetbox.utils.framing import get_frame_module_traceback

module_name = get_frame_module_traceback().__name__
assert pkg.is_installed('flask', try_install_if_not=True), f"{module_name} requires flask which is not installed"
from flask import Flask, json, abort, request
from neetbox.config import get_module_level_config
from threading import Thread
import time
assert pkg.is_installed(
"flask", try_install_if_not=True
), f"{module_name} requires flask which is not installed"
import sys
import time
from threading import Thread

from flask import Flask, abort, json, request

from neetbox.config import get_module_level_config

_STAT_POOL = {}
__DAEMON_SHUTDOWN_IF_NO_UPLOAD_TIMEOUT_SEC = 60 * 60
Expand Down
9 changes: 5 additions & 4 deletions neetbox/daemon/_daemon_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# URL: https://gong.host
# Date: 20230414

from neetbox.pipeline._signal_and_slot import _update_value_dict
from neetbox.logging import logger
from neetbox.config import get_module_level_config
from threading import Thread
import json
import time
from threading import Thread

from neetbox.config import get_module_level_config
from neetbox.daemon._local_http_client import _local_http_client
from neetbox.logging import logger
from neetbox.pipeline._signal_and_slot import _update_value_dict

__TIME_UNIT_SEC = 0.1

Expand Down
23 changes: 12 additions & 11 deletions neetbox/daemon/_daemon_launcher.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
print('========= Daemon Launcher =========')
print("========= Daemon Launcher =========")

import argparse
import json
import os
import sys

from neetbox.daemon._daemon import daemon_process

# sys.stdout=open(r'D:\Projects\ML\neetbox\logdir\daemon.log', 'a+')

import json
import argparse

from neetbox.daemon._daemon import daemon_process
# from neetbox.daemon._local_http_client import _local_http_client


def run():
if len(sys.argv) <= 1:
print('_daemon_launcher: Warning: empty daemon_config')
print("_daemon_launcher: Warning: empty daemon_config")
daemon_config = {}
else:
ap = argparse.ArgumentParser()
ap.add_argument('--config')
ap.add_argument("--config")
args = ap.parse_args()
daemon_config = json.loads(args.config)
print('Daemon launcher started with config:', daemon_config)
print("Daemon launcher started with config:", daemon_config)
daemon_process(daemon_config)


print('_daemon_launcher is starting with __name__ =',
__name__, ' and args =', sys.argv)

print("_daemon_launcher is starting with __name__ =", __name__, " and args =", sys.argv)

run()
print('_daemon_launcher: exiting')
print("_daemon_launcher: exiting")
Loading