Skip to content

Commit

Permalink
Final examples + update test_contains!
Browse files Browse the repository at this point in the history
  • Loading branch information
bleudev committed Jun 1, 2024
1 parent 69af1b1 commit 6bf2ae2
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
171 changes: 171 additions & 0 deletions examples/udict.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,174 @@ 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}
```
4 changes: 4 additions & 0 deletions tests/test_udict.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ def test_contains(self):
d = UDict(hello=1, hi=2)
self.assertTrue('hello' in d)
self.assertTrue('hi' in d)

self.assertTrue(('hello', 1) in d)
self.assertFalse(('hello', 2) in d)

self.assertTrue(('hi', 2) in d)
self.assertFalse(('hi', 1) in d)

def test_str_and_repr(self):
d = {'hello': 1, 'hi': 2}
Expand Down

0 comments on commit 6bf2ae2

Please sign in to comment.