diff --git a/README.md b/README.md index 7f506de..6f612cb 100644 --- a/README.md +++ b/README.md @@ -544,68 +544,65 @@ from random import random, randint, choice # Also: shuffle, gauss, triang Combinatorics ------------- -* **Every function returns an iterator.** -* **If you want to print the iterator, you need to pass it to the list() function first!** - ```python import itertools as it ``` ```python ->>> it.product([0, 1], repeat=3) +>>> list(it.product([0, 1], repeat=3)) [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] ``` ```python ->>> it.product('abc', 'abc') # a b c -[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x - ('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x - ('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x +>>> list(it.product('abc', 'abc')) # a b c +[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x + ('b', 'a'), ('b', 'b'), ('b', 'c'), # b x x x + ('c', 'a'), ('c', 'b'), ('c', 'c')] # c x x x ``` ```python ->>> it.combinations('abc', 2) # a b c -[('a', 'b'), ('a', 'c'), # a . x x - ('b', 'c')] # b . . x +>>> list(it.combinations('abc', 2)) # a b c +[('a', 'b'), ('a', 'c'), # a . x x + ('b', 'c')] # b . . x ``` ```python ->>> it.combinations_with_replacement('abc', 2) # a b c -[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x - ('b', 'b'), ('b', 'c'), # b . x x - ('c', 'c')] # c . . x +>>> list(it.combinations_with_replacement('abc', 2)) # a b c +[('a', 'a'), ('a', 'b'), ('a', 'c'), # a x x x + ('b', 'b'), ('b', 'c'), # b . x x + ('c', 'c')] # c . . x ``` ```python ->>> it.permutations('abc', 2) # a b c -[('a', 'b'), ('a', 'c'), # a . x x - ('b', 'a'), ('b', 'c'), # b x . x - ('c', 'a'), ('c', 'b')] # c x x . +>>> list(it.permutations('abc', 2)) # a b c +[('a', 'b'), ('a', 'c'), # a . x x + ('b', 'a'), ('b', 'c'), # b x . x + ('c', 'a'), ('c', 'b')] # c x x . ``` Datetime -------- -* **Module 'datetime' provides 'date' ``, 'time' ``, 'datetime' `
` and 'timedelta' `` classes. All are immutable and hashable.** -* **Time and datetime objects can be 'aware' ``, meaning they have defined timezone, or 'naive' ``, meaning they don't.** -* **If object is naive, it is presumed to be in the system's timezone.** +**Provides 'date', 'time', 'datetime' and 'timedelta' classes. All are immutable and hashable.** ```python -from datetime import date, time, datetime, timedelta -from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary +# pip3 install python-dateutil +from datetime import date, time, datetime, timedelta, timezone +from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary ``` -### Constructors ```python = date(year, month, day) # Only accepts valid dates from 1 to 9999 AD. = time(hour=0, minute=0, second=0) # Also: `microsecond=0, tzinfo=None, fold=0`.
= datetime(year, month, day, hour=0) # Also: `minute=0, second=0, microsecond=0, …`. = timedelta(weeks=0, days=0, hours=0) # Also: `minutes=0, seconds=0, microseconds=0`. ``` -* **Use `'.weekday()'` to get the day of the week as an int, with Monday being 0.** +* **Aware `` time and datetime objects have defined timezone, while naive `` don't.** +* **If object is naive, it is presumed to be in the system's timezone.** * **`'fold=1'` means the second pass in case of time jumping back for one hour.** * **Timedelta normalizes arguments to ±days, seconds (< 86 400) and microseconds (< 1M).** +* **Use `'.weekday()'` to get the day of the week as an int, with Monday being 0.** * **`' = resolve_imaginary()'` fixes DTs that fall into the missing hour.** ### Now @@ -618,12 +615,14 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary ### Timezone ```python - = UTC # UTC timezone. London without DST. + = timezone.utc # London without daylight saving time. + = timezone() # Timezone with fixed offset from UTC. = tzlocal() # Local timezone. Also gettz(). = gettz('/') # 'Continent/City_Name' timezone or None. =
.astimezone() # Datetime, converted to the passed timezone. = .replace(tzinfo=) # Unconverted object with a new timezone. ``` +* **Standard library's zoneinfo.ZoneInfo() can be used instead of gettz() on Python 3.9 and later. It requires 'tzdata' package on Windows.** ### Encode ```python @@ -651,7 +650,8 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary >>> dt.strftime("%A, %dth of %B '%y, %I:%M%p %Z") "Thursday, 14th of May '15, 11:39PM UTC+02:00" ``` -* **Format code `'%z'` accepts `'±HH[:]MM'` and returns `'±HHMM'` (or `''` if datetime is naive).** +* **`'%z'` accepts `'±HH[:]MM'` and returns `'±HHMM'` or empty string if datetime is naive.** +* **`'%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.** * **For abbreviated weekday and month use `'%a'` and `'%b'`.** ### Arithmetics @@ -659,7 +659,7 @@ from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary = ± # Returned datetime can fall into missing hour. = - # Returns the difference. Ignores time jumps. = - # Ignores time jumps if they share tzinfo object. - = * # Also: = abs() and = ±% . + = * # Also: = abs() and = ±% . = / # How many weeks/years there are in TD. Also //. ``` diff --git a/index.html b/index.html index 7762f4a..4112724 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -489,55 +489,49 @@ Point(x=1, y=2 <int> = ~<int> # Not. Also -<int> - 1. -

#Combinatorics

    -
  • Every function returns an iterator.
  • -
  • If you want to print the iterator, you need to pass it to the list() function first!
  • -
import itertools as it
+

#Combinatorics

import itertools as it
 
- -
>>> it.product([0, 1], repeat=3)
+
>>> list(it.product([0, 1], repeat=3))
 [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
  (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
 
-
>>> it.product('abc', 'abc')                     #   a  b  c
-[('a', 'a'), ('a', 'b'), ('a', 'c'),             # a x  x  x
- ('b', 'a'), ('b', 'b'), ('b', 'c'),             # b x  x  x
- ('c', 'a'), ('c', 'b'), ('c', 'c')]             # c x  x  x
+
>>> list(it.product('abc', 'abc'))                    #   a  b  c
+[('a', 'a'), ('a', 'b'), ('a', 'c'),                  # a x  x  x
+ ('b', 'a'), ('b', 'b'), ('b', 'c'),                  # b x  x  x
+ ('c', 'a'), ('c', 'b'), ('c', 'c')]                  # c x  x  x
 
-
>>> it.combinations('abc', 2)                    #   a  b  c
-[('a', 'b'), ('a', 'c'),                         # a .  x  x
- ('b', 'c')]                                     # b .  .  x
+
>>> list(it.combinations('abc', 2))                   #   a  b  c
+[('a', 'b'), ('a', 'c'),                              # a .  x  x
+ ('b', 'c')]                                          # b .  .  x
 
-
>>> it.combinations_with_replacement('abc', 2)   #   a  b  c
-[('a', 'a'), ('a', 'b'), ('a', 'c'),             # a x  x  x
- ('b', 'b'), ('b', 'c'),                         # b .  x  x
- ('c', 'c')]                                     # c .  .  x
+
>>> list(it.combinations_with_replacement('abc', 2))  #   a  b  c
+[('a', 'a'), ('a', 'b'), ('a', 'c'),                  # a x  x  x
+ ('b', 'b'), ('b', 'c'),                              # b .  x  x
+ ('c', 'c')]                                          # c .  .  x
 
-
>>> it.permutations('abc', 2)                    #   a  b  c
-[('a', 'b'), ('a', 'c'),                         # a .  x  x
- ('b', 'a'), ('b', 'c'),                         # b x  .  x
- ('c', 'a'), ('c', 'b')]                         # c x  x  .
+
>>> list(it.permutations('abc', 2))                   #   a  b  c
+[('a', 'b'), ('a', 'c'),                              # a .  x  x
+ ('b', 'a'), ('b', 'c'),                              # b x  .  x
+ ('c', 'a'), ('c', 'b')]                              # c x  x  .
 
-

#Datetime

    -
  • Module 'datetime' provides 'date' <D>, 'time' <T>, 'datetime' <DT> and 'timedelta' <TD> classes. All are immutable and hashable.
  • -
  • Time and datetime objects can be 'aware' <a>, meaning they have defined timezone, or 'naive' <n>, meaning they don't.
  • -
  • If object is naive, it is presumed to be in the system's timezone.
  • -
from datetime import date, time, datetime, timedelta
-from dateutil.tz import UTC, tzlocal, gettz, datetime_exists, resolve_imaginary
+

#Datetime

Provides 'date', 'time', 'datetime' and 'timedelta' classes. All are immutable and hashable.

# pip3 install python-dateutil
+from datetime import date, time, datetime, timedelta, timezone
+from dateutil.tz import tzlocal, gettz, datetime_exists, resolve_imaginary
 
-

Constructors

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

Now

<D/DTn>  = D/DT.today()                     # Current local date or naive datetime.
@@ -548,13 +542,17 @@ Point(x=1, y=2
 
  • To extract time use '<DTn>.time()', '<DTa>.time()' or '<DTa>.timetz()'.
-

Timezone

<tzinfo> = UTC                              # UTC timezone. London without DST.
+

Timezone

<tzinfo> = timezone.utc                     # London without daylight saving time.
+<tzinfo> = timezone(<timedelta>)            # Timezone with fixed offset from UTC.
 <tzinfo> = tzlocal()                        # Local timezone. Also gettz().
 <tzinfo> = gettz('<Continent>/<City>')      # 'Continent/City_Name' timezone or None.
 <DTa>    = <DT>.astimezone(<tzinfo>)        # Datetime, converted to the passed timezone.
 <Ta/DTa> = <T/DT>.replace(tzinfo=<tzinfo>)  # Unconverted object with a new timezone.
 
+
    +
  • Standard library's zoneinfo.ZoneInfo() can be used instead of gettz() on Python 3.9 and later. It requires 'tzdata' package on Windows.
  • +

Encode

<D/T/DT> = D/T/DT.fromisoformat('<iso>')    # Object from ISO string. Raises ValueError.
 <DT>     = DT.strptime(<str>, '<format>')   # Datetime from str, according to format.
 <D/DTn>  = D/DT.fromordinal(<int>)          # D/DTn from days since the Gregorian NYE 1.
@@ -579,13 +577,14 @@ Point(x=1, y=2
 
    -
  • Format code '%z' accepts '±HH[:]MM' and returns '±HHMM' (or '' if datetime is naive).
  • +
  • '%z' accepts '±HH[:]MM' and returns '±HHMM' or empty string if datetime is naive.
  • +
  • '%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.
  • For abbreviated weekday and month use '%a' and '%b'.

Arithmetics

<D/DT>   = <D/DT>  ± <TD>                   # Returned datetime can fall into missing hour.
 <TD>     = <D/DTn> - <D/DTn>                # Returns the difference. Ignores time jumps.
 <TD>     = <DTa>   - <DTa>                  # Ignores time jumps if they share tzinfo object.
-<TD>     = <TD>    * <real>                 # Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.
+<TD>     = <TD>    * <int/float>            # Also: <TD> = abs(<TD>) and <TD> = <TD> ±% <TD>.
 <float>  = <TD>    / <TD>                   # How many weeks/years there are in TD. Also //.
 
@@ -2933,7 +2932,7 @@ $ pyinstaller script.py --add-data '<path>:.'