What are __signature__ and __text_signature__ used for in Python 3.4

These attributes are there to enable introspection for Python objects defined in C code. The C-API Argument Clinic provides the data, to assist the inspect module when building Signature objects. Introspection of C-API functions was not supported before. See the internal inspect._signature_fromstr() function on how the __text_signature__ value is used. Currently, the __text_signature__ attribute is … Read more

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

What’s the correct way to clean up after an interrupted event loop?

When you CTRL+C, the event loop gets stopped, so your calls to t.cancel() don’t actually take effect. For the tasks to be cancelled, you need to start the loop back up again. Here’s how you can handle it: import asyncio @asyncio.coroutine def shleepy_time(seconds): print(“Shleeping for {s} seconds…”.format(s=seconds)) yield from asyncio.sleep(seconds) if __name__ == ‘__main__’: loop … Read more

urllib HTTPS request:

Most likely your Python installation or operating system is broken. Python has only support for HTTPS if it was compiled with HTTPS support. However, this should be the default for all sane installations. HTTPS support is only available if the socket module was compiled with SSL support. https://docs.python.org/3/library/http.client.html Please clarify how you installed Python. Official … Read more

How to get text from span tag in BeautifulSoup

You can use a css selector, pulling the span you want using the title text : soup = BeautifulSoup(“””<div class=”systemRequirementsMainBox”> <div class=”systemRequirementsRamContent”> <span title=”000 Plus Minimum RAM Requirement”>1 GB</span> </div>”””, “xml”) print(soup.select_one(“span[title*=RAM]”).text) That finds the span with a title attribute that contains RAM, it is equivalent to saying in python, if “RAM” in span[“title”]. Or … Read more