dabeaz / python-cookbook Goto Github PK
View Code? Open in Web Editor NEWCode samples from the "Python Cookbook, 3rd Edition", published by O'Reilly & Associates, May, 2013.
Code samples from the "Python Cookbook, 3rd Edition", published by O'Reilly & Associates, May, 2013.
I don't understand the function of flag in example2 how_to_determine_if_a_thread_has_started (12.2). The scripts works fine when I remove the flag part likes below.
import threading
import time
class PeriodicTimer:
def __init__(self, interval):
self._interval = interval
self._cv = threading.Condition()
def start(self):
t = threading.Thread(target=self.run)
t.daemon = True
t.start()
def run(self):
print('running start')
while True:
time.sleep(self._interval)
with self._cv:
self._cv.notify_all()
def wait_fot_tick(self):
with self._cv:
self._cv.wait()
ptimer = PeriodicTimer(5)
ptimer.start()
def countdown(nticks):
print('countdown start')
while nticks > 0:
ptimer.wait_fot_tick()
print('T-minus', nticks)
nticks -= 1
def countup(last):
n = 0
print('counting start')
while n < last:
ptimer.wait_fot_tick()
print('Counting', n)
n += 1
threading.Thread(target=countdown, args=(10,)).start()
threading.Thread(target=countup, args=(5,)).start()
12/how_to_create_a_thread_pool/example1.py
As you said in book, create each Thread in new connection is a bad idea. I want create a ThreadPool to handle a TCP Socket, So I open Python CookBook and look into this demo. But it doesn't work actually.
When I use PyCharm, pool.submit(echo_client, sk, addr)
actually called, but can't echo back
def echo_client(sock, client_addr):
while True:
msg = sock.recv(65536)
if not msg:
break
sock.sendall(msg)
sock.close()
def echo_server(addr):
pool = ThreadPoolExecutor(128)
sock = socket(AF_INET, SOCK_STREAM)
sock.bind(addr)
sock.listen(5)
while True:
client_sock, client_addr = sock.accept()
pool.submit(echo_client, client_sock, client_addr) # this func called, but not truly executed to echo back
echo_server(('',15000))
My TCP client:
def tcpclient():
skt = socket(AF_INET, SOCK_STREAM)
skt.connect(('', 15000))
skt.sendall('Client: To be or not, is a question'.encode())
data = skt.recv(1024)
print('Client recv: ', data.decode())
skt.close()
What's Wrong with it???
Hi, Dabeaz, I found it is very hard to compile with Visual Studio.
I use this command to compile:
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/vcvarsall.bat" amd64 && cl /W4 /EHsc /ZI /MTd /D _UNICODE /Fd:debug/ /Fo:debug/ /IC:/Python3664/include f:\py_c\embed.c /link /LIBPATH:C:/Python3664/libs /out:debug/embed.exe
I have compiled and run it successfully in MacOS with clang.
The code has missed ignore_types arg when calling the method of flatten
recursively in the example about how to flatten a nested sequence.
src/4/how_to_flatten_a_nested_sequence/example.py
from collections import Iterable
def flatten(items, ignore_types=(str, bytes)):
for x in items:
if isinstance(x, Iterable) and not isinstance(x, ignore_types):
yield from flatten(x)
else:
yield x
The corrected code is yield from flatten(x, ignore_types)
.
python-cookbook/src/8/extending_classes_with_mixins/example2.py
that's seem not a solutino for minin class which needs init method, i have checked on python 2.7, this is what i got:
C:\Python27\python.exe D:/dev/python/pyqt/workapce/pyqt/widget/test/test.py
File "D:/dev/python/pyqt/workapce/pyqt/widget/test/test.py", line 2
def init(self, *args, _restrict_key_type, **kwargs):
^
SyntaxError: invalid syntax
should be re.compile(r'"(.*)"')
because with r'...', backslashes will be remained
so the findall should return [],The fix should be to delete the two backslashes.
hi, David
i have just tested
python-cookbook/src/8/lazily_computed_attributes/example1.py
but happened to findout it does not workout like what the book tell, which i meam python cookbook, 3rd.
After a little research, i found that you need to put a object there, like this :
class lazyproperty(object):
but i dont konw why.
please tell me if you know.
PS:
PyCharm 2016.3.1
Build #PY-163.9735.8, built on December 15, 2016
Subscription is active until February 25, 2017
JRE: 1.8.0_111-b14 amd64
JVM: Java HotSpot(TM) 64-Bit Server VM by Oracle Corporation
Python
2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)] on win32
Hi!
In the Chapter 12: communicating between threads "Solution" section (bottom of p493) there is an example in which the Queue.task_done method is used along with Queue.join to wait for all items produced by the "producer" function to be consumed.
However (and unless I am misunderstanding), because the queue has no items in it before the threads start, unless the producer function immediately puts an item (before the q.join() executes in the main thread) into the queue, the queue will immediately join. For example, if you have something like:
from queue import Queue
from threading import Thread
import time
def producer(out_q):
time.sleep(5)
out_q.put('hello')
def consumer(in_q):
h = in_q.get()
print(h + ' world!')
in_q.task_done()
q = Queue()
t1 = Thread(target=producer, args=(q,))
t2 = Thread(target=consumer, args=(q,))
t1.start()
t2.start()
q.join()
print('q joined!')
You will see:
q joined!
hello world!
I guess the fix would be putting in an initial sentinel before starting the threads?
Love the book by the way!
Hi,
In chapter 9.9, I don't understand the below code, especially in __init__()
. It uses wraps(func)(self)
in __init__()
, but it doesn't assign wraps(func)(self)
to self
. Why self.__wrapped__
can get the original function in __call__()
?
import types
from functools import wraps
class Profiled:
def __init__(self, func):
wraps(func)(self)
self.ncalls = 0
def __call__(self, *args, **kwargs):
self.ncalls += 1
return self.__wrapped__(*args, **kwargs)
def __get__(self, instance, cls):
if instance is None:
return self
else:
return types.MethodType(self, instance)
@Profiled
def add(x, y):
return x + y
print(add(1, 2))
print(add(1, 3))
print(add.ncalls)
print(add)
I changed it to self = wraps(func)(self)
, found that it also can work? Who can explain that? I also couldn't understand self
in self = wraps(func)(self)
. What's "self"?
The code is not provided with any kind of license, making it difficult to determine if these snippets can be used in a professional context.
i finally got this working by defining 'record' instead of user_record.
Just wanted to share .. thanks!
on-input-37-fd835976edf3> in ()
----> 1 user_record = name, email, *phone_numbers
NameError: name 'email' is not defined
NameError Traceback (most recent call last)
in ()
----> 1 phone_numbers
NameError: name 'phone_numbers' is not defined
#here where i set to record instead of user_record
In [39]: name, email, *phone_numbers = record
In [40]: name
Out[40]: 'Dave'
In [41]: email
Out[41]: '[email protected]'
In [42]: phone_numbers
Out[42]: ['773-555-1212', '555-555-5555']
In [43]:
Maybe its me, but this code seems to just not work for me:
my tag_stack == path_parts
never fires and so the generator yields nothing?
Is there a typo somewhere here?
Offending XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tv SYSTEM "xmltv.dtd">
<tv generator-info-name="xmltv.co.uk" source-info-name="xmltv.co.uk">
<channel id="f3932e75f691561adbe3b609369e487b">
<display-name>BBC One Lon</display-name>
<icon src="/images/channels/f3932e75f691561adbe3b609369e487b.png"/>
</channel>
<channel id="a3c70f4c25110a9ca84f7c604023ee6c">
<display-name>Dave</display-name>
<icon src="/images/channels/a3c70f4c25110a9ca84f7c604023ee6c.png"/>
</channel>
<programme start="20181005060000 +0100" stop="20181005091500 +0100" channel="f3932e75f691561adbe3b609369e487b">
<title lang="en">Break</title>
<desc lang="en">The latest news, sport, business and weather from the BBC's Breakfast team. Also in HD. [S] Including regional news at 25 and 55 minutes past each hour.</desc>
</programme>
<programme start="20181005130000 +0100" stop="20181005133000 +0100" channel="f3932e75f691561adbe3b609369e487b">
<title lang="en">BBC News at One</title>
<desc lang="en">The latest national and international news stories from the BBC News team, followed by weather. Also in HD. [S]</desc>
</programme>
...
Brad
This one for instance: mapping_names_to_sequence_elements
In example of section 1.2 the first variable with name record must be replaced by user_record
`
user_record = ('Dave', '[email protected]', '773-555-1212', '847-555-1212')
name, email, *phone_numbers = user_record
name
'Dave'
'[email protected]'
phone_numbers
['773-555-1212', '847-555-1212']
`
The demo of the indices
method is called on a different object a
than the previously instanciated one. This could be fixed ny instanciating a
with the following line:
a = slice(5, 50, 2)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.