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

Move UStack to ustl #41

Merged
merged 6 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 17 additions & 16 deletions examples/ustack.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# UStack
# Stack

## Introduction

UStack is list with possibility to get only top element with useful features. [Wiki](https://en.wikipedia.org/wiki/Stack_(abstract_data_type))
Stack is a list with the possibility to get only the top element with useful features. [Wiki](https://en.wikipedia.org/wiki/Stack_(abstract_data_type))

bleudev marked this conversation as resolved.
Show resolved Hide resolved
Firstly, import `Stack` from `ufpy`

Firstly, import `UStack` from `ufpy`
```python
from ufpy import UStack
from ufpy import Stack
```

## Create UStack
## Create Stack

For creating UStack you should use this code:
For creating Stack you should use this code:
```python
bleudev marked this conversation as resolved.
Show resolved Hide resolved
s = UStack() # blank stack
s = UStack(1, 9, 2) # You can also provide elements as arguments
s = UStack(iterable=[9, 2, 8]) # Or with `iterable` kwarg
s = Stack() # blank stack
s = Stack(1, 9, 2) # You can also provide elements as arguments
s = Stack(iterable=[9, 2, 8]) # Or with `iterable` kwarg
```

## Get / edit / delete top element
Expand Down Expand Up @@ -102,13 +103,13 @@ len(s)

You can check that stack is empty using `is_empty()` method:
```python
s = UStack()
s = Stack()
s.is_empty() # True
```

Also, you can use `if UStack` syntax for checking that stack is not empty
You can also use the `if Stack` syntax to check if the stack is not empty
```python
bleudev marked this conversation as resolved.
Show resolved Hide resolved
s = UStack()
s = Stack()
if s:
print("Stack isn't empty!")
else:
Expand All @@ -120,18 +121,18 @@ else:

You can use `repr()` with stacks. Because of it, you can print stacks:
```python
s = UStack(1, 9, 2)
s = Stack(1, 9, 2)
print(s) # s[1, 9, 2]
```

## Copying of UStack
## Copying of Stack

You can use `copy()` method for copying `UStack`s:
You can use `copy()` method for copying `Stack`s:
```python
s.copy()
```

You can also use `copy.copy()` function for copying `UStack`s:
You can also use `copy.copy()` function for copying `Stack`s:
```python
bleudev marked this conversation as resolved.
Show resolved Hide resolved
from copy import copy

Expand Down
15 changes: 5 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from setuptools import setup
from setuptools import setup, find_packages

from ufpy import __version__

Expand All @@ -9,13 +9,12 @@
'requests>=2.31.0',
]

author = 'bleudev'
author_email = '[email protected]'
bleudev marked this conversation as resolved.
Show resolved Hide resolved
organization_name = 'honey-team'
bleudev marked this conversation as resolved.
Show resolved Hide resolved
author, author_email = 'bleudev', '[email protected]'
project_name = 'ufpy'
github_url = f'https://github.com/{organization_name}/{project_name}'

def __package(name: str) -> str:
return f'{project_name}.{name}'

setup(
name=project_name,
Expand All @@ -27,16 +26,12 @@ def __package(name: str) -> str:
long_description=long_description,
long_description_content_type='text/markdown',
url=github_url,
packages=[
project_name,
__package('typ'),
__package('github'),
],
packages=find_packages(),
classifiers=[
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.12',
],
zip_safe=False,
python_requires=">=3.12",
install_requires=install_requires
)
)
62 changes: 31 additions & 31 deletions tests/test_ustack.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
import unittest
from copy import copy

from ufpy import UStack
from ufpy import Stack


class UStackTestCase(unittest.TestCase):
def test_init(self):
s = UStack(1, 1, 2, 3, 5, 8)
s2 = UStack(*s.elements)
s3 = UStack(iterable=s2.elements)
s = Stack(1, 1, 2, 3, 5, 8)
bleudev marked this conversation as resolved.
Show resolved Hide resolved
s2 = Stack(*s.elements)
s3 = Stack(iterable=s2.elements)

self.assertEqual(s, s2)
self.assertEqual(s, s3)
self.assertEqual(s2, s3)

def test_elements(self):
s = UStack(1, 1, 2, 3, 5, 8)
s = Stack(1, 1, 2, 3, 5, 8)
self.assertEqual(s.elements, [1, 1, 2, 3, 5, 8])

s.elements = 1, 2
self.assertEqual(s, UStack(1, 2))
self.assertEqual(s, Stack(1, 2))

del s.elements
self.assertEqual(s, UStack())
self.assertEqual(s, Stack())

def test_top(self):
s = UStack(1, 1, 2, 3, 5, 8)
s = Stack(1, 1, 2, 3, 5, 8)
self.assertEqual(s.top, 8)

s.top = 10
self.assertEqual(s, UStack(1, 1, 2, 3, 5, 10))
self.assertEqual(s, Stack(1, 1, 2, 3, 5, 10))

del s.top
self.assertEqual(s, UStack(1, 1, 2, 3, 5))
self.assertEqual(s, Stack(1, 1, 2, 3, 5))

def test_pop(self):
s = UStack(1, 1, 2, 3, 5, 8)
s = Stack(1, 1, 2, 3, 5, 8)
self.assertEqual(s.pop(), 8)
self.assertEqual(s, UStack(1, 1, 2, 3, 5))
self.assertEqual(s, Stack(1, 1, 2, 3, 5))

def test_push(self):
s = UStack(1, 1, 2, 3, 5, 8)
self.assertEqual(s.push(2, 1), UStack(1, 1, 2, 3, 5, 8, 2, 1))
self.assertEqual(s, UStack(1, 1, 2, 3, 5, 8, 2, 1))
s = Stack(1, 1, 2, 3, 5, 8)
self.assertEqual(s.push(2, 1), Stack(1, 1, 2, 3, 5, 8, 2, 1))
bleudev marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(s, Stack(1, 1, 2, 3, 5, 8, 2, 1))

def test_remove(self):
s = UStack(1, 1, 2, 3, 5, 8)
self.assertEqual(s.remove(1), UStack(1, 2, 3, 5, 8))
self.assertEqual(s, UStack(1, 2, 3, 5, 8))
s = Stack(1, 1, 2, 3, 5, 8)
self.assertEqual(s.remove(1), Stack(1, 2, 3, 5, 8))
bleudev marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(s, Stack(1, 2, 3, 5, 8))

def test_clear(self):
s = UStack(1, 1, 2, 3, 5, 8)
s2 = UStack(1, 1, 2, 3, 5, 8)
s = Stack(1, 1, 2, 3, 5, 8)
s2 = Stack(1, 1, 2, 3, 5, 8)

del s2.elements

self.assertEqual(s.clear(), UStack())
self.assertEqual(s.clear(), Stack())
self.assertEqual(s, s2)

def test_copy(self):
# with copy
s = UStack(1, 1, 2, 3, 5, 8)
s = Stack(1, 1, 2, 3, 5, 8)
s2 = s.copy()
s3 = copy(s2)

Expand All @@ -79,28 +79,28 @@ def test_copy(self):
self.assertEqual(id(s4), id(s5))

def test_call(self):
s = UStack(1, 1, 2, 3, 5, 8)
s = Stack(1, 1, 2, 3, 5, 8)

def f(i, v):
return v * i

self.assertEqual(s(f), UStack(0, 1, 4, 9, 20, 40))
self.assertEqual(s(f), Stack(0, 1, 4, 9, 20, 40))
bleudev marked this conversation as resolved.
Show resolved Hide resolved

def test_math_operations(self):
s = UStack(1, 1, 2, 3, 5, 8)
self.assertEqual(s + 1, UStack(1, 1, 2, 3, 5, 8, 1))
self.assertEqual(s - 1, UStack(1, 2, 3, 5, 8))
self.assertEqual(s * 2, UStack(2, 2, 4, 6, 10, 16))
self.assertEqual(s / 2, UStack(0.5, 0.5, 1, 1.5, 2.5, 4))
s = Stack(1, 1, 2, 3, 5, 8)
self.assertEqual(s + 1, Stack(1, 1, 2, 3, 5, 8, 1))
self.assertEqual(s - 1, Stack(1, 2, 3, 5, 8))
self.assertEqual(s * 2, Stack(2, 2, 4, 6, 10, 16))
self.assertEqual(s / 2, Stack(0.5, 0.5, 1, 1.5, 2.5, 4))
bleudev marked this conversation as resolved.
Show resolved Hide resolved

def test_len_and_empty(self):
s = UStack(1, 1, 2, 3, 5, 8)
s = Stack(1, 1, 2, 3, 5, 8)
self.assertEqual(len(s), 6)
self.assertFalse(s.is_empty())
self.assertTrue(bool(s))

def test_repr(self):
s = UStack(1, 1, 2, 3, 5, 8)
s = Stack(1, 1, 2, 3, 5, 8)
self.assertEqual(repr(s), 's[1, 1, 2, 3, 5, 8]')


Expand Down
27 changes: 25 additions & 2 deletions ufpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
__version__ = '0.1.2'
# Deprecated
def __deprecated(deprecated_name: str, x, start_version: str, end_version: str):
"""This is a decorator which can be used to mark functions
bleudev marked this conversation as resolved.
Show resolved Hide resolved
as deprecated. It will result in a warning being emitted
when the function is used."""
from functools import wraps
from warnings import warn, simplefilter

@wraps(x)
def new_func(*args, **kwargs):
simplefilter('always', DeprecationWarning) # turn off filter
warn(f"{deprecated_name} is deprecated in {start_version} and will be deleted in {end_version}. "
f"Use {x.__name__} instead.",
category=DeprecationWarning,
stacklevel=2)
simplefilter('default', DeprecationWarning) # reset filter
return x(*args, **kwargs)
return new_func


__version__ = '0.2-dev'
from ufpy.cmp import *
from ufpy.math_op import *
from ufpy.udict import *
from ufpy.ustack import *
from ufpy.utils import *

# Typing package
__typ_version__ = '0.1'
from ufpy.typ import *

# Ustl package
__ustl_version = '0.1'
from ufpy.ustl import *
UStack = __deprecated("UStack", Stack, '0.2', '0.5')

bleudev marked this conversation as resolved.
Show resolved Hide resolved
# Path package
__path_version__ = '0.1'
from ufpy.path import *
Expand Down
1 change: 1 addition & 0 deletions ufpy/ustl/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ufpy.ustl.stack import *
Loading
Loading