python-3.4

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

萝らか妹 提交于 2019-11-27 05:35:10
问题 I have an event loop that runs some co-routines as part of a command line tool. The user may interrupt the tool with the usual Ctrl + C , at which point I want to clean up properly after the interrupted event loop. Here's what I tried. 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 = asyncio.get_event_loop() # Side note: Apparently, async() will be deprecated

What are __signature__ and __text_signature__ used for in Python 3.4

不羁岁月 提交于 2019-11-27 04:52:26
If one does dir() on some builtin callables (class constructors, methods, etc) on CPython 3.4, one finds out that many of them often have a special attribute called __text_signature__ , for example: >>> object.__text_signature__ '()' >>> int.__text_signature__ >>> # was None However the documentation for this is nonexistent. Furthermore, googling for the attribute name suggests that there is also another possible special attribute __signature__ , though I did not find any built-in functions that would have it. I do know they are related to the function argument signature, but nothing beyond

Python3: ImportError: No module named '_ctypes' when using Value from module multiprocessing

三世轮回 提交于 2019-11-27 02:37:51
I am using Ubuntu and have installed Python 2.7.5 and 3.4.0. In Python 2.7.5 I am able to successfully assign a variable x = Value('i', 2) , but not in 3.4.0. I am getting: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.4/multiprocessing/context.py", line 132, in Value from .sharedctypes import Value File "/usr/local/lib/python3.4/multiprocessing/sharedctypes.py", line 10, in < module> import ctypes File "/usr/local/lib/python3.4/ctypes/__init__.py", line 7, in <module> from _ctypes import Union, Structure, Array ImportError: No module named

print binary tree level by level in python

最后都变了- 提交于 2019-11-27 02:26:11
问题 I want to print my binary tree in the following manner: 10 6 12 5 7 11 13 I have written code for insertion of nodes but can't able to write for printing the tree. so please help on this . My code is : class Node: def __init__(self,data): self.data=data self.left=None self.right=None self.parent=None class binarytree: def __init__(self): self.root=None self.size=0 def insert(self,data): if self.root==None: self.root=Node(data) else: current=self.root while 1: if data < current.data: if

Making a collatz program automate the boring stuff

南笙酒味 提交于 2019-11-27 01:53:36
问题 I'm trying to write a Collatz program using the guidelines from a project found at the end of chapter 3 of Automate the Boring Stuff with Python. I'm using python 3.4.0 . Following is the project outline: Write a function named collatz() that has one parameter named number. If the number is even, then collatz() should print number // 2 and return this value. If the number is odd, then collatz() should print and return 3 * number + 1 . Then write a program that lets the user type in an integer

enum - getting value of enum on string conversion

南楼画角 提交于 2019-11-26 22:44:28
问题 I have following enum defined from enum import Enum class D(Enum): x = 1 y = 2 print(D.x) now the printed value is D.x instead I wanted the enum's value to be print 1 Hhat can be done to achieve this functionality? 回答1: You are printing the enum object . Use the .value attribute if you wanted just to print that: print(D.x.value) See the Programmatic access to enumeration members and their attributes section: If you have an enum member and need its name or value: >>> >>> member = Color.red >>>

How can I use functools.singledispatch with instance methods?

非 Y 不嫁゛ 提交于 2019-11-26 21:56:34
Python 3.4 added the ability to define function overloading with static methods. This is essentially the example from the documentation: from functools import singledispatch class TestClass(object): @singledispatch def test_method(arg, verbose=False): if verbose: print("Let me just say,", end=" ") print(arg) @test_method.register(int) def _(arg): print("Strength in numbers, eh?", end=" ") print(arg) @test_method.register(list) def _(arg): print("Enumerate this:") for i, elem in enumerate(arg): print(i, elem) if __name__ == '__main__': TestClass.test_method(55555) TestClass.test_method([33, 22,

How do I unescape a unicode escaped string in python?

北战南征 提交于 2019-11-26 21:41:07
问题 I have a unicode escaped string: > str = 'blah\\x2Ddude' I want to convert this string into the unicode unescaped version 'blah-dude' How do I do this? 回答1: Encode it to bytes (using whatever codec, utf-8 probably works) then decode it using unicode-escape : s = 'blah\\x2Ddude' s.encode().decode('unicode-escape') Out[133]: 'blah-dude' 来源: https://stackoverflow.com/questions/22601291/how-do-i-unescape-a-unicode-escaped-string-in-python

Why is one class variable not defined in list comprehension but another is?

主宰稳场 提交于 2019-11-26 21:04:29
I just read the answer to this question: Accessing class variables from a list comprehension in the class definition It helps me to understand why the following code results in NameError: name 'x' is not defined : class A: x = 1 data = [0, 1, 2, 3] new_data = [i + x for i in data] print(new_data) The NameError occurs because x is not defined in the special scope for list comprehension. But I am unable to understand why the following code works without any error. class A: x = 1 data = [0, 1, 2, 3] new_data = [i for i in data] print(new_data) I get the output [0, 1, 2, 3] . But I was expecting

Python enum, when and where to use?

偶尔善良 提交于 2019-11-26 18:55:16
Python 3.4.0 introduced enum , I've read the doc but still don't know the usage of it. From my perspective, enum is an extended namedtuple type, which may not be true. So these are what I want to know about enum: When and where to use enum? Why do we need enum? what are the advatages? What exactly is enum? Ethan Furman 1. When and where to use enums? When you have a variable that takes one of a limited set of possible values. For example, the days of the week: class Weekday(Enum): MONDAY = 1 TUESDAY = 2 WEDNESDAY = 3 THURSDAY = 4 FRIDAY = 5 SATURDAY = 6 SUNDAY = 7 2. Why do we need enum? What