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

UDict + cmp_generator #3

Merged
merged 30 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
58762fb
UDict part 1
bleudev May 22, 2024
5c72f5b
UDict compare and cmp_generator
bleudev May 22, 2024
c0fb0fa
i_generator + Comparable
bleudev May 22, 2024
e9fb271
math operations + iter
bleudev May 23, 2024
dbb5628
`__getitem__()`
bleudev May 24, 2024
8e850f9
Update `.gitignore` and realise `__setitem__()`
bleudev May 24, 2024
62e72d6
Fix `__setitem__()`
bleudev May 24, 2024
118a0d3
`__reversed__()`, `__nonzero__()`, `__delitem__()`, move all protocol…
bleudev May 24, 2024
c252161
`__getattr__()`
bleudev May 24, 2024
24c4ae7
Revert get/set/del attr methods.
bleudev May 24, 2024
2c6d3ff
Many changes
bleudev May 24, 2024
e301717
Final UDict
bleudev May 25, 2024
b6fc729
Add tests
bleudev May 25, 2024
c9b0c75
Merge 'main' into 'useful-dict'
bleudev May 25, 2024
e3760d8
Adding examples part 1
bleudev May 25, 2024
ceead0b
get() and tests for get()
bleudev May 28, 2024
5b2db39
add __invert__() (__invert__ = __reversed__) and change __neg__
bleudev May 28, 2024
78c3ee4
Example for `get()`
bleudev May 28, 2024
f051010
Merge remote-tracking branch 'refs/remotes/origin/main' into useful-dict
bleudev May 28, 2024
d400623
Delete .md
bleudev May 28, 2024
085effe
final tests + fix math operations
bleudev May 28, 2024
e91f7ff
fix __repr__ and fix get items (IndexError is raising when key == 0)
bleudev May 28, 2024
6185757
add get.value param
bleudev May 29, 2024
86f36c5
Big update
bleudev May 31, 2024
dd73b40
Examples + fix tests
bleudev Jun 1, 2024
a7026f4
Rename `test_bool` to `test_nonzero`
bleudev Jun 1, 2024
69af1b1
Create `is_empty()` method
bleudev Jun 1, 2024
6bf2ae2
Final examples + update test_contains!
bleudev Jun 1, 2024
ce7dcb3
default kwarg
bleudev Jun 2, 2024
ab1ad1e
Fix default value of `default` kwarg
bleudev Jun 2, 2024
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
Empty file removed examples/.md
Empty file.
303 changes: 303 additions & 0 deletions examples/udict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
# UDict

## Introduction

UDict is class which is simplifying working with Python dicts.
It has many methods, properties, magic methods.

Firstly, import `UDict` from `ufpy`
```python
from ufpy import UDict
```

## Create UDict

For creating UDict you should use this code:
```python
d = UDict({'id': 2, 'content': 'hello, world'})
# for string keys you can use this way:
d = UDict(id=2, content='hello, world')
```

You can also define `default` value for items when you use item's getter:
```python
d = UDict(id=2, content='hello, world', default=0)
```

## Get items

For getting items you should use the way you use in lists and dicts:
use `UDict[key]` syntax:
```python
d['id'] # 2
```

You can also use index of key or slice of indexes of keys:

> [!CAUTION]
> In this class first index is 1

```python
d[1] # 2
d[:] # u{'id': 2, 'content': 'hello, world'} (UDict object)
```

You can also use `get()` method.
You can get item by its key using `key` kwarg,
you can get item by its index using `index` kwarg,
and you get item key by its value using `value` kwarg.

Also you can define `default` only for this calling of `get()` using `default` kwarg.

Example:
```python
d = UDict({2: 3, 1: 4}, default=None)
d[2] # 3
d.get(index=2) # 4
d.get(key=1) # also 4
d.get(value=3) # 2

d.get(key=3) # None
d.get(key=3, default='null') # 'null'
```

## Set items

For setting items you should use the way you use in lists and dicts:
use `UDict[key] = value` syntax:
```python
d['id'] = 3
```

Also, you can use indexes and slices when you are setting items:
```python
d[1] = 2
d[2:6:2] = 8
d[:3] = 1, 2, 3
```

## Delete items

For deleting items you should use the way you use in lists and dicts:
use `del UDict[key]` syntax:
```python
del d['id']
```

Also, you can use indexes and slices:
```python
del d[1]
del d[:] # all UDict will become empty
```

## Get length of dict

You can get length of dict using inbuilt `len()` function

```python
d = UDict(hello=1, hi=2)
len(d) # 2
```

## Iterate dict: `keys`, `values`, `items` properties

You can iterate dict using `for key, value in UDict` syntax.

```python
d = UDict(hello=1, hi=2)
for key, value in d:
print(key, value)

# out:
#
# hello 1
# hi 2
```

Also, you can iterate `items` property for this result

```python
for key, value in d.items:
...
```

If you want iterate only all keys or values, use `keys` or `values` properties

```python
for key in d.keys:
...

for value in d.values:
...
```

## Check that dict is empty or not empty

You can use `is_empty()` method to check that UDict is empty:
```python
d = UDict()
print(d.is_empty()) # True

d['hello'] = 'world'
print(d.is_empty()) # False
```

You also can use `if UDict` or `bool(UDict)` syntax:
```python
d = UDict()
print(bool(d)) # False

d['hello'] = 'world'
print(bool(d)) # True

if d:
print('True!')

# out: True!
```

## Check that key or item in dict

You can check that key in dict:
```python
d = UDict(hi=1, hello=2)

print('hi' in d) # True
print('hii' in d) # False
```

You can also check that item in dict:
```python
d = UDict(hi=1, hello=2)

print(('hi', 1) in d) # True
print(('hi', 11) in d) # False
```

## Convert to `str` type and using in `print`

### Print dict

`print` uses `repr()` therefore `UDict` supports `repr()`

```python
d = UDict(hi=1, hello=2)
print(d) # u{'hi': 1, 'hello': 2}
print(repr(d)) # u{'hi': 1, 'hello': 2}
```

### Convert to str

You can use inbuilt `str()` class for converting `UDict` to `str` type

```python
d = UDict(hi=1, hello=2)
print(str(d)) # {'hi': 1, 'hello': 2}
```

## Comparing dicts

You can compare `UDict`s using inbuilt compare operators
(`==`, `!=`, `>`, `>=`, `<`, `<=`):

> [!NOTE]
> When you use equal and not equal compare operator, dicts are comparing by its items and length,
> but in other compare operators dicts are comparing only by its length.
>
> For example, `d > d2 -> len(d) > len(d2)`, etc.

```python
d = UDict(hi=1, hello=2)
d2 = UDict(hi=1, hello=2)
d3 = UDict(hi=1, hello=2, world=3)

print(d == d2) # True
print(d != d2) # False
print(d < d3) # True
print(d <= d3) # True
print(d3 > d) # True
print(d3 >= d) # True
```

## Math operations

You can use inbuilt math operators (`+`, `-`, `*`, `/`, `+=`, `-=`, `*=`, `/=`)
with `UDict`s:

> [!NOTE]
> When you use sum and sub math operators (`+`, `-`) dicts are summing or subtracting, but
> when you use other math operators dict will be multiplying or dividing by integer or dict.
>
>When dict it works like this:
>
> d * {'hello': 2, 'hi': 0.5}
>
> d['hello'] * 2
>
> d['hi'] * 0.5

```python
d = UDict(hi=1, hello=2)
print(d + {'world': 3}) # u{'hi': 1, 'hello': 2, 'world': 3}
print(d - {'hello': 2}) # u{'hi': 1}

print(d * 2) # u{'hi': 2, 'hello': 4}
print(d * {'hi': 2}) # u{'hi': 2, 'hello': 2}

print(d / 2) # u{'hi': 0.5, 'hello': 1}
print(d / {'hi': 2}) # u{'hi': 0.5, 'hello': 2}
```

## Negative dict

You can use unary minus with dicts:
```python
d = UDict(hi=1, hello=2)
print(-d) # u{'hi': -1, 'hello': -2}
```

## Reverse dict

You can reverse dict using `reverse()` or `reversed()` method.
> [!CAUTION]
> When you use `reverse()`, dict updates in contrast to `reversed()`. Be careful!
>
> ```python
> # reverse()
> d = UDict(b=1, a=2)
> print(d.reverse()) # u{'a': 2, 'b': 1}
> print(d) # u{'a': 2, 'b': 1}
>
> # reversed()
> d = UDict(b=1, a=2)
> print(d.reversed()) # u{'a': 2, 'b': 1}
> print(d) # u{'b': 1, 'a': 2}
> ```

```python
d.reverse()
d.reversed()
```

Also, you can use `~` operator and `reversed()` class. They are equivalents of `UDict.reversed()`:
```python
~d
reversed(d)
```

## Sort dict

You can use `sort()` and `sorted()` methods for sorting UDict.
`sort()` is updating dict, `sorted()` - isn't.

```python
# sort()
d = UDict(b=1, a=2)
print(d.sort()) # u{'a': 2, 'b': 1}
print(d) # u{'a': 2, 'b': 1}

# sorted()
d = UDict(b=1, a=2)
print(d.sorted()) # u{'a': 2, 'b': 1}
print(d) # u{'b': 1, 'a': 2}
```
Loading
Loading