honey-team / ufpy

Ufpy (Useful Python) - package for Python with some useful features
https://honey-team.github.io/ufpy-website
MIT License
3 stars 3 forks source link

UStr #55

Open bleudev opened 2 months ago

bleudev commented 2 months ago

Create UStr object for simplification of working with strings.

Methods:

Can also fill with multiply characters (inbuild str - doesn't)!

s.center(10, '#@') # '#@#@34#@#@'


- `join(*iterable: Iterable[SupportsStr] | SupportsStr) -> UStr`
```py
# All elements in iterable must support `str()` method!

# If several arguments -> use args as iterable
UStr(',').join(2, 4) # u'2, 4'
# If one -> use this one argument as iterable
UStr(',').join([2, 4]) #'2, 4'

Properties:

Magic methods:

s2 = UStr("Hello, my name is banana") s2 - ["Hello", "banana"] # u', my name is ' s2 - UList(["Hello", "banana"]) # Same


- Comparison by length
```python
UStr("dddd") > UStr("d") # True

Also all methods of str

Thank @mbutsk and @n0n1m for help with creating issue ❤️

mbutsk commented 2 months ago

Subtraction

s = UStr("Hello 3")
s - "3" # Hello
s - UStr("3") # Hello
s - 3 # Hello

s2 = UStr("Hello, my name is banana")
s2 - ["Hello", "banana"] # my name is
s2 - UList(["Hello", "banana"]) # my name is

Comparison by length

UStr("dddd") > UStr("d") # True 
n0n1m commented 2 months ago

Also all methods of str

Suggestion: maybe make UStr a subclass of built-in str?

bleudev commented 2 months ago

Also all methods of str

Suggestion: maybe make UStr a subclass of built-in str?

Good idea, i wanted to do that. And override all methods with changes

n0n1m commented 2 months ago

Also all methods of str

Suggestion: maybe make UStr a subclass of built-in str?

Good idea, i wanted to do that. And override all methods with changes

Why didn't you do that for UDict?

moontr3 commented 2 months ago

Also all methods of str

Suggestion: maybe make UStr a subclass of built-in str?

Good idea, i wanted to do that. And override all methods with changes

But If you want to overwrite all default functions, why even bother making it a string subclass? I mean, as far as I know, that approach worked for UDict, as @n0n1m mentioned.

moontr3 commented 2 months ago

By the way, I have an idea for a function.

UStr.merge() will merge several symbols or strings into one, while stripping or enlarging them to a specified length to easily create progress bars.

There will be an additional class UMergeStr that inherits from UStr, and it will be used to create strings for the merge function.

It will work something like this:

new = UStr.merge([
    UMergeStr("#", 0.25),
    UMergeStr(" ", 0.75)
], target_length=10)
# "###       "

new = UStr.merge([
    UMergeStr("-=", percentage),
    UMergeStr("•", 1), # if the second argument of UMergeStr is an integer that is equal or larger than 1, it will instead be interpreted as a set amount of characters instead of a percentage like above
    UMergeStr(" ", 1-percentage)
], target_length=15)

# percentage == 0.5:
# "-=-=-=-•       "

# percentage == 0.0:
# "•              "

# percentage == 0.8:
# "-=-=-=-=-=-=•  "

I hope you get my point.

n0n1m commented 2 months ago

Also all methods of str

Suggestion: maybe make UStr a subclass of built-in str?

Good idea, i wanted to do that. And override all methods with changes

But If you want to overwrite all default functions, why even bother making it a string subclass? I mean, as far as I know, that approach worked for UDict, as @n0n1m mentioned.

Does he overwrite all of them?

n0n1m commented 2 months ago

In UDict, there are __len__ for example, and if UDict was subclass of dict, you wouldn't need to reimplement it

moontr3 commented 2 months ago

Also all methods of str

Suggestion: maybe make UStr a subclass of built-in str?

Good idea, i wanted to do that. And override all methods with changes

But If you want to overwrite all default functions, why even bother making it a string subclass? I mean, as far as I know, that approach worked for UDict, as @n0n1m mentioned.

Does he overwrite all of them?

Idk, but judging by his response to a suggestion above he does in fact want to overwrite all methods

n0n1m commented 2 months ago

Idk, but judging by his response to a suggestion above he does in fact want to overwrite all methods

Good idea, i wanted to do that. And override all methods with changes

moontr3 commented 2 months ago

Idk, but judging by his response to a suggestion above he does in fact want to overwrite all methods

Good idea, i wanted to do that. And override all methods with changes

That's exactly what I'm talking about

n0n1m commented 2 months ago

Idk, but judging by his response to a suggestion above he does in fact want to overwrite all methods

Good idea, i wanted to do that. And override all methods with changes

That's exactly what I'm talking about

He says he want to overwrite the methods that he changes, but he didn't say he changes all of them

moontr3 commented 2 months ago

Idk, but judging by his response to a suggestion above he does in fact want to overwrite all methods

Good idea, i wanted to do that. And override all methods with changes

That's exactly what I'm talking about

He says he want to overwrite the methods that he changes, but he didn't say he changes all of them

Dang I thought he said he wanted to overwrite all methods and change them

bleudev commented 2 months ago

Also all methods of str

Suggestion: maybe make UStr a subclass of built-in str?

Good idea, i wanted to do that. And override all methods with changes

Why didn't you do that for UDict?

in UDict there are very much methods fron dict with changes. But it'll be great to subclass dict. It'll make using isinstance() function more great (you can use just inbuilt dict class to check for dict and UDict class. I'll make it.