Subclass `pathlib.Path` fails

You can subclass the concrete implementation, so this works: class Path(type(pathlib.Path())): Here’s what I did with this: import pathlib class Path(type(pathlib.Path())): def open(self, mode=”r”, buffering=-1, encoding=None, errors=None, newline=None): if encoding is None and ‘b’ not in mode: encoding = ‘utf-8’ return super().open(mode, buffering, encoding, errors, newline) Path(‘/tmp/a.txt’).write_text(“я”)

How to properly create and run concurrent tasks using python’s asyncio module?

Yes, any coroutine that’s running inside your event loop will block other coroutines and tasks from running, unless it Calls another coroutine using yield from or await (if using Python 3.5+). Returns. This is because asyncio is single-threaded; the only way for the event loop to run is for no other coroutine to be actively … Read more

print binary tree level by level in python

Here’s my attempt, using recursion, and keeping track of the size of each node and the size of children. class BstNode: def __init__(self, key): self.key = key self.right = None self.left = None def insert(self, key): if self.key == key: return elif self.key < key: if self.right is None: self.right = BstNode(key) else: self.right.insert(key) else: … Read more

Python 3.4 and 2.7: Cannot install numpy package for python 3.4

You have not installed the Python 3 development package. Install python3.4-dev: apt-get install python3.4-dev The main package never includes the development headers; Debian (and by extension Ubuntu) package policy is to put those into a separate -dev package. To install numpy however, you need these files to be able to compile the extension.

How to check if a directory contains files using Python 3

Adding to @Jon Clements’ pathlib answer, I wanted to check if the folder is empty with pathlib but without creating a set: from pathlib import Path # shorter version from @vogdb is_empty = not any(Path(‘some/path/here’).iterdir()) # similar but unnecessary complex is_empty = not bool(sorted(Path(‘some/path/here’).rglob(‘*’))) vogdb method attempts to iterate over all files in the given … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)