Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f5f03a

Browse files
committed
Major changes to Combinatorics and Datetime
1 parent d32e026 commit 4f5f03a

File tree

2 files changed

+64
-65
lines changed

2 files changed

+64
-65
lines changed

README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -544,68 +544,65 @@ from random import random, randint, choice # Also: shuffle, gauss, triang
544544

545545
Combinatorics
546546
-------------
547-
* **Every function returns an iterator.**
548-
* **If you want to print the iterator, you need to pass it to the list() function first!**
549-
550547
```python
551548
import itertools as it
552549
```
553550

554551
```python
555-
>>> it.product([0, 1], repeat=3)
552+
>>> list(it.product([0, 1], repeat=3))
556553
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
557554
(1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
558555
```
559556

560557
```python
561-
>>> it.product('abc', 'abc') # a b c
562-
[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x
563-
('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x
564-
('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x
558+
>>> list(it.product('abc', 'abc')) # a b c
559+
[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x
560+
('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x
561+
('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x
565562
```
566563

567564
```python
568-
>>> it.combinations('abc', 2) # a b c
569-
[('a', 'b'), ('a', 'c'), # a . x x
570-
('b', 'c')] # b . . x
565+
>>> list(it.combinations('abc', 2)) # a b c
566+
[('a', 'b'), ('a', 'c'), # a . x x
567+
('b', 'c')] # b . . x
571568
```
572569

573570
```python
574-
>>> it.combinations_with_replacement('abc', 2) # a b c
575-
[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x
576-
('b', 'b'), ('b', 'c'), # b . x x
577-
('c', 'c')] # c . . x
571+
>>> list(it.combinations_with_replacement('abc', 2)) # a b c
572+
[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x
573+
('b', 'b'), ('b', 'c'), # b . x x
574+
('c', 'c')] # c . . x
578575
```
579576

580577
```python
581-
>>> it.permutations('abc', 2) # a b c
582-
[('a', 'b'), ('a', 'c'), # a . x x
583-
('b', 'a'), ('b', 'c'), # b x . x
584-
('c', 'a'), ('c', 'b')] # c x x .
578+
>>> list(it.permutations('abc', 2)) # a b c
579+
[('a', 'b'), ('a', 'c'), # a . x x
580+
('b', 'a'), ('b', 'c'), # b x . x
581+
('c', 'a'), ('c', 'b')] # c x x .
585582
```
586583

587584

588585
Datetime
589586
--------
590-
* **Module 'datetime' provides 'date' `<D>`, 'time' `<T>`, 'datetime' `<DT>` and 'timedelta' `<TD>` classes. All are immutable and hashable.**
591-
* **Time and datetime objects can be 'aware' `<a>`, meaning they have defined timezone, or 'naive' `<n>`, meaning they don't.**
592-
* **If object is naive, it is presumed to be in the system's timezone.**
587+
**Provides 'date', 'time', 'datetime' and 'timedelta' classes. All are immutable and hashable.**
593588

594589
```python
595-
from datetime import date, time, datetime, timedelta
596-
from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary
590+
# pip3 install python-dateutil
591+
from datetime import date, time, datetime, timedelta, timezone
592+
from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary
597593
```
598594

599-
### Constructors
600595
```python
601596
<D> = date(year, month, day) # Only accepts valid dates from 1 to 9999 AD.
602597
<T> = time(hour=0, minute=0, second=0) # Also: `microsecond=0, tzinfo=None, fold=0`.
603598
<DT> = datetime(year, month, day, hour=0) # Also: `minute=0, second=0, microsecond=0, …`.
604599
<TD> = timedelta(weeks=0, days=0, hours=0) # Also: `minutes=0, seconds=0, microseconds=0`.
605600
```
606-
* **Use `'<D/DT>.weekday()'` to get the day of the week as an int, with Monday being 0.**
601+
* **Aware `<a>` time and datetime objects have defined timezone, while naive `<n>` don't.**
602+
* **If object is naive, it is presumed to be in the system's timezone.**
607603
* **`'fold=1'` means the second pass in case of time jumping back for one hour.**
608604
* **Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M).**
605+
* **Use `'<D/DT>.weekday()'` to get the day of the week as an int, with Monday being 0.**
609606
* **`'<DTa> = resolve_imaginary(<DTa>)'` fixes DTs that fall into the missing hour.**
610607

611608
### Now
@@ -618,12 +615,14 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary
618615

619616
### Timezone
620617
```python
621-
<tzinfo> = UTC # UTC timezone. London without DST.
618+
<tzinfo> = timezone.utc # London without daylight saving time.
619+
<tzinfo> = timezone(<timedelta>) # Timezone with fixed offset from UTC.
622620
<tzinfo> = tzlocal() # Local timezone. Also gettz().
623621
<tzinfo> = gettz('<Continent>/<City>') # 'Continent/City_Name' timezone or None.
624622
<DTa> = <DT>.astimezone(<tzinfo>) # Datetime, converted to the passed timezone.
625623
<Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>) # Unconverted object with a new timezone.
626624
```
625+
* **Standard library's zoneinfo.ZoneInfo() can be used instead of gettz() on Python 3.9 and later. It requires 'tzdata' package on Windows.**
627626

628627
### Encode
629628
```python
@@ -651,15 +650,16 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary
651650
>>> dt.strftime("%A, %dth of %B '%y, %I:%M%p %Z")
652651
"Thursday, 14th of May '15, 11:39PM UTC+02:00"
653652
```
654-
* **Format code `'%z'` accepts `'±HH[:]MM'` and returns `'±HHMM'` (or `''` if datetime is naive).**
653+
* **`'%z'` accepts `'±HH[:]MM'` and returns `'±HHMM'` or empty string if datetime is naive.**
654+
* **`'%Z'` accepts `'UTC/GMT'` and local timezone's code and returns timezone's name, `'UTC[±HH:MM]'` if timezone is nameless, or an empty string if datetime is naive.**
655655
* **For abbreviated weekday and month use `'%a'` and `'%b'`.**
656656

657657
### Arithmetics
658658
```python
659659
<D/DT> = <D/DT> ± <TD> # Returned datetime can fall into missing hour.
660660
<TD> = <D/DTn> - <D/DTn> # Returns the difference. Ignores time jumps.
661661
<TD> = <DTa> - <DTa> # Ignores time jumps if they share tzinfo object.
662-
<TD> = <TD> * <real> # Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.
662+
<TD> = <TD> * <int/float> # Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.
663663
<float> = <TD> / <TD> # How many weeks/years there are in TD. Also //.
664664
```
665665

0 commit comments

Comments
 (0)