Get Mystery Box with random crypto!

Python etc

Logo of telegram channel pythonetc — Python etc P
Logo of telegram channel pythonetc — Python etc
Channel address: @pythonetc
Categories: Technologies
Language: English
Subscribers: 6.28K
Description from channel

Regular tips about Python and programming in general
Owner — @pushtaev
The current season is run by @orsinium
Tips are appreciated: https://ko-fi.com/pythonetc / https://sobe.ru/na/pythonetc
© CC BY-SA 4.0 — mention if repost

Ratings & Reviews

4.00

2 reviews

Reviews can be left only by registered users. All reviews are moderated by admins.

5 stars

1

4 stars

0

3 stars

1

2 stars

0

1 stars

0


The latest Messages 7

2020-11-17 18:00:06 ipaddress provides capabilities to work with IP addresses and networks (both IPv4 and IPv6).

ip = ipaddress.ip_address('127.0.0.1')
ip.is_private # True
ip.is_loopback # True
ip.is_global # False
ip.is_multicast # False
ip.is_reserved # False
ip.is_unspecified # False
ip.reverse_pointer # '1.0.0.127.in-addr.arpa'

net = ipaddress.ip_network('192.168.0.0/28')
net.is_private # True
net.hostmask # IPv4Address('0.0.0.15')
net.num_addresses # 16
4.2K views15:00
Open / Comment
2020-11-12 18:00:52 PEP-560 (landed in Python 3.7) introduced a new magic method __class_getitem__. it is the same as __getitem__ but for not instancinated class. The main motivation is easier type annotation support for generic collections like List[int] or Type[Dict[str, int]]:

class MyList:
def __getitem__(self, index):
return index + 1

def __class_getitem__(cls, item):
return f"{cls.__name__}[{item.__name__}]"

MyList()[1]
# 2

MyList[int]
# 'MyList[int]'
5.6K views15:00
Open / Comment
2020-11-10 18:00:13 ​​The script pydoc can be used to see documentationand docstrings from the console:

$ pydoc3 functools.reduce | cat
Help on built-in function reduce in functools:

functools.reduce = reduce(...)
reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,
...

Also, you can specify a port with -p flag, and pydoc will serve the HTML documentation browser on the given port:

$ pydoc3 -p 1234
Server ready at http://localhost:1234/
Server commands: [b]rowser, [q]uit
server>
5.0K views15:00
Open / Comment
2020-11-05 18:00:08 __slots__ can be used to save memory. You can use any iterable as __slots__ value, including dict. AND Starting from Python 3.8, you can use dict to specify docstrings for slotted attributes __slots__:

class Channel:
"Telegram channel"
__slots__ = {
'slug': 'short name, without @',
'name': 'user-friendly name',
}
def __init__(self, slug, name):
self.slug = slug
self.name = name

inspect.getdoc(Channel.name)
# 'user-friendly name'

Also, help(Channel) lists docs for all slotted attributes:

class Channel(builtins.object)
| Channel(slug, name)
|
| Telegram channel
|
| Methods defined here:
|
| __init__(self, slug, name)
| Initialize self. See help(type(self)) for accurate signature.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| name
| user-friendly name
|
| slug
| short name, without @
4.8K views15:00
Open / Comment
2020-11-03 18:00:08 As we said, comprehensions compiled into functions. That means, we can take a types.CodeType object for a comprehension, wrap it into types.FunctionType and get a function.

import types

def make():
[x*2 for x in _]

code = make.__code__.co_consts[1]
func = types.FunctionType(code, globals())

# call the function!
func(iter(range(5)))
# [0, 2, 4, 6, 8]
4.4K views15:00
Open / Comment