@@ -544,68 +544,65 @@ from random import random, randint, choice # Also: shuffle, gauss, triang
544
544
545
545
Combinatorics
546
546
-------------
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
-
550
547
``` python
551
548
import itertools as it
552
549
```
553
550
554
551
``` python
555
- >> > it.product([0 , 1 ], repeat = 3 )
552
+ >> > list ( it.product([0 , 1 ], repeat = 3 ) )
556
553
[(0 , 0 , 0 ), (0 , 0 , 1 ), (0 , 1 , 0 ), (0 , 1 , 1 ),
557
554
(1 , 0 , 0 ), (1 , 0 , 1 ), (1 , 1 , 0 ), (1 , 1 , 1 )]
558
555
```
559
556
560
557
``` 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
565
562
```
566
563
567
564
``` 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
571
568
```
572
569
573
570
``` 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
578
575
```
579
576
580
577
``` 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 .
585
582
```
586
583
587
584
588
585
Datetime
589
586
--------
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.**
593
588
594
589
``` 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
597
593
```
598
594
599
- ### Constructors
600
595
``` python
601
596
< D> = date(year, month, day) # Only accepts valid dates from 1 to 9999 AD.
602
597
< T> = time(hour = 0 , minute = 0 , second = 0 ) # Also: `microsecond=0, tzinfo=None, fold=0`.
603
598
< DT > = datetime(year, month, day, hour = 0 ) # Also: `minute=0, second=0, microsecond=0, …`.
604
599
< TD > = timedelta(weeks = 0 , days = 0 , hours = 0 ) # Also: `minutes=0, seconds=0, microseconds=0`.
605
600
```
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.**
607
603
* ** ` 'fold=1' ` means the second pass in case of time jumping back for one hour.**
608
604
* ** 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.**
609
606
* ** ` '<DTa> = resolve_imaginary(<DTa>)' ` fixes DTs that fall into the missing hour.**
610
607
611
608
### Now
@@ -618,12 +615,14 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary
618
615
619
616
### Timezone
620
617
``` 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.
622
620
< tzinfo> = tzlocal() # Local timezone. Also gettz().
623
621
< tzinfo> = gettz(' <Continent>/<City>' ) # 'Continent/City_Name' timezone or None.
624
622
< DTa> = < DT > .astimezone(< tzinfo> ) # Datetime, converted to the passed timezone.
625
623
< Ta/ DTa> = < T/ DT > .replace(tzinfo = < tzinfo> ) # Unconverted object with a new timezone.
626
624
```
625
+ * ** Standard library's zoneinfo.ZoneInfo() can be used instead of gettz() on Python 3.9 and later. It requires 'tzdata' package on Windows.**
627
626
628
627
### Encode
629
628
``` python
@@ -651,15 +650,16 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary
651
650
>> > dt.strftime(" %A, %d th of %B '%y, %I:%M%p %Z" )
652
651
" Thursday, 14th of May '15, 11:39PM UTC+02:00"
653
652
```
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.**
655
655
* ** For abbreviated weekday and month use ` '%a' ` and ` '%b' ` .**
656
656
657
657
### Arithmetics
658
658
``` python
659
659
< D/ DT > = < D/ DT > ± < TD > # Returned datetime can fall into missing hour.
660
660
< TD > = < D/ DTn> - < D/ DTn> # Returns the difference. Ignores time jumps.
661
661
< 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>.
663
663
< float > = < TD > / < TD > # How many weeks/years there are in TD. Also //.
664
664
```
665
665
0 commit comments