Skip to content

Commit

Permalink
copy support of nod
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Mar 19, 2020
1 parent d4329b7 commit 4e4d1a4
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 14 deletions.
38 changes: 38 additions & 0 deletions .github/publish-to-test-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Publish Python 🐍 distributions 📦 to PyPI and TestPyPI
on: push
jobs:
build-n-publish:
name: Build and publish Python 🐍 distributions 📦 to PyPI and TestPyPI
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@master
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7

- name: Install pep517
run: >-
python -m
pip install
pep517
--user
- name: Build a binary wheel and a source tarball
run: >-
python -m
pep517.build
--source
--binary
--out-dir dist/
.
- name: Publish distribution 📦 to Test PyPI
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.test_pypi_password }}
repository_url: https://test.pypi.org/legacy/

- name: Publish distribution 📦 to PyPI
if: startsWith(github.event.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.pypi_password }}
48 changes: 48 additions & 0 deletions tests/test_nod.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,51 @@ def test_nested():
cfg.ADSA = 203182

print(cfg)


def test_copy():
from copy import deepcopy, copy

nodict = NOD()
nodict.paramA = "str_parameter"
nodict.paramB = 10

b = copy(nodict)
print(b)
assert b.paramB == 10
assert b.paramB == nodict.paramB

a = deepcopy(nodict)
print(a)
assert a.paramB == 10
assert a.paramB == nodict.paramB


def test_distribute_keys_view():

nodict = NOD()
nodict.paramA = 2
nodict.paramB = 10

nodict.paramC = 5
nodict[nodict.keys()] = 4

assert nodict.paramA == 4
assert nodict.paramB == 4
assert nodict.paramC == 4


def test_distribute_slice():

nodict = NOD()
nodict.paramA = 2
nodict.paramB = 10
nodict.paramC = 5
nodict.paramD = 5

nodict[:2] = 4

assert nodict.paramA == 4
assert nodict.paramB == 4
assert nodict.paramC == 4
assert nodict.paramD == 5
2 changes: 2 additions & 0 deletions warg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@author: cnheider
"""
__all__ = ["PROJECT_APP_PATH", "PROJECT_NAME", "PROJECT_VERSION", "get_version"]


def dist_is_editable(dist):
Expand Down Expand Up @@ -95,3 +96,4 @@ def get_version(append_time=DEVELOP):
from .decorators import *
from .auto_dict import *
from .metas import *
from .bases import *
10 changes: 10 additions & 0 deletions warg/bases/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = "Christian Heider Nielsen"
__doc__ = r"""
Created on 17/03/2020
"""

from .property_settings import *
87 changes: 87 additions & 0 deletions warg/bases/property_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = "Christian Heider Nielsen"
__doc__ = r"""
Created on 17/03/2020
"""
__all__ = ["PropertySettings"]

from typing import Dict, Mapping

from warg import NOD


class PropertySettings:
def __init__(self, **kwargs):
for k, v in kwargs.items():
self.__setattr__(k, v)

for setting in dir(self):
if not setting.startswith("_"):
try:
self.__getattr__(setting)
except KeyError:
self.__setattr__(setting, None)

def __clear__(self):
for setting in dir(self):
if not setting.startswith("_"):
self.__setattr__(setting, None)

def __setattr__(self, key, value):
assert not key.startswith("_"), f"{key} is not allowed"
self.__getattr__(key)
super().__setattr__(key, value)

def __getattr__(self, item):
assert not item.startswith("_"), f"{item} is not allowed"
try:
return super().__getattribute__(item)
except AttributeError as a:
a = type(a)(str(a) + f", available settings {self}")
raise a

def __str__(self) -> str:
return self.__repr__()

def __contains__(self, item):
return hasattr(self, item)

def __repr__(self) -> str:
settings_dict = {}
for setting in dir(self):
if not setting.startswith("_"):
try:
settings_dict[setting] = self.__getattr__(setting)
except KeyError:
settings_dict[setting] = None

return str(settings_dict)

def __iter__(self):
available_settings = []
for setting in dir(self):
if not setting.startswith("_"):
available_settings.append(setting)

return iter(available_settings)

def __to_dict__(self) -> dict:
return self.__crystallise__().as_dict()

def __crystallise__(self) -> NOD:
return NOD({k: getattr(self, k) for k in self})

def __from_mapping__(self, mapping: Mapping) -> None:
for k, v in mapping.items():
setattr(self, k, v)

def __from_dict__(self, dict: Dict) -> None:
self.__from_mapping__(dict)


if __name__ == "__main__":
a = PropertySettings()
assert not "h" in a
58 changes: 49 additions & 9 deletions warg/named_ordered_dictionary.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from typing import Any, ItemsView, Iterable, KeysView, List, MutableMapping, Tuple, TypeVar, ValuesView, Type
from typing import (
Any,
ItemsView,
Iterable,
KeysView,
List,
MutableMapping,
Tuple,
Type,
TypeVar,
ValuesView,
Sized,
)

import sorcery
from sorcery.core import node_name
Expand Down Expand Up @@ -106,7 +118,6 @@ class NamedOrderedDictionary(MutableMapping):

def __init__(self, *args, **kwargs) -> None:
# super().__init__(**kwargs)

if len(args) == 1 and isinstance(args[0], dict):
args_dict = args[0]
else:
Expand Down Expand Up @@ -186,16 +197,21 @@ def nod_of(frame_info, *args, **kwargs) -> T:
return nod

def __getattr__(self, item) -> Any:
if item == "__deepcopy__":
return super().__getattribute__(item)
return self.__dict__[item]

def __len__(self) -> int:
return len(self.__dict__)

def __setattr__(self, key, value) -> None:
if key in LOCALS:
raise IllegalAttributeKey(key, type=NamedOrderedDictionary.__name__)
raise IllegalAttributeKey(key, type=NamedOrderedDictionary)

self.__dict__[key] = value
if key == "__dict__":
super().__setattr__(key, value)
else:
self.__dict__[key] = value

def __getitem__(self, key) -> Any:
if isinstance(key, slice):
Expand All @@ -209,13 +225,26 @@ def __getitem__(self, key) -> Any:
def __setitem__(self, key, value) -> None:
if isinstance(key, slice):
keys = list(self.__dict__.keys())[key]
for a, v in zip(keys, value):
self.__dict__[a] = v
if isinstance(value, Sized):
assert len(keys) == len(
value
), f"number of keys {len(keys)} are not equal values {len(value)}"
for a, v in zip(keys, value):
self.__dict__[a] = v
else:
for a in keys:
self.__dict__[a] = value
elif isinstance(key, KeysView):
# assert set(self.__dict__.keys()).issuperset(key)
assert len(key) == len(value)
for a, v in zip(key, value):
self.__dict__[a] = v
# assert isinstance(value,Sized), f'values must be of type Sized, was {type(value)},' \
# f' distribution is not supported'
if isinstance(value, Sized):
assert len(key) == len(value), f"number of keys {len(key)} are not equal values {len(value)}"
for a, v in zip(key, value):
self.__dict__[a] = v
else:
for a in key:
self.__dict__[a] = value
else:
self.__dict__[key] = value

Expand Down Expand Up @@ -330,3 +359,14 @@ def __getstate__(self) -> dict:
nodict.paramB = 10
assert nodict.paramA == "str_parameter"
assert nodict.paramB == 10
from copy import deepcopy, copy

b = copy(nodict)
print(b)
assert b.paramB == 10
assert b.paramB == nodict.paramB

a = deepcopy(nodict)
print(a)
assert a.paramB == 10
assert a.paramB == nodict.paramB
10 changes: 5 additions & 5 deletions warg/pooled_queue_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ def call(self, *args, **kwargs):
task = Square()

processor = PooledQueueProcessor(task, [2], fill_at_construction=True, max_queue_size=100)
for LATEST_GPU_STATS, _ in zip(processor, range(30)):
print(LATEST_GPU_STATS)
for GPU_STATS, _ in zip(processor, range(30)):
print(GPU_STATS)

processor.blocking = True
processor.args = [4]
time.sleep(3)
for LATEST_GPU_STATS in processor:
print(LATEST_GPU_STATS)
if LATEST_GPU_STATS == 8:
for GPU_STATS in processor:
print(GPU_STATS)
if GPU_STATS == 8:
break

0 comments on commit 4e4d1a4

Please sign in to comment.