Giter Club home page Giter Club logo

example-code's People

Contributors

akay7 avatar anancds avatar destaq avatar dym0080 avatar ramalho avatar timgates42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

example-code's Issues

Runtime error in Example A-2

I'm trying to run the Example A-2 (page 689 in the First Edition).
I'm not sure if it's something wrong that I'm doing.. but I read the code 10 times. I'm sure it is the same of the book.

I'm running this code as:

$python example_a_2__generate_array.py

where my Python version is 3.8.5. Then I see this error:

initial sample: 10500000 elements
Traceback (most recent call last):
  File "example_a_2__generate_array.py", line 19, in <module>
    with len(sample) < SAMPLE_LEN:
AttributeError: __enter__

Some reasoning:
I'm not reasigning the open function (as suggested here for one possible cause).

Possible solution:
Maybe line 19 should be replaced by:

if len(sample) < SAMPLE_LEN:

I tried that and got a good looking output:

initial sample: 10500000 elements
complete sample: 10500000 elements
not selected: 500000 samples
  writing not_selected.arr
selected: 10000000 samples
  writing selected.arr

And they seem to have been created correctly:

$ls -la *selected*
-rw-rw-r-- 1 lucas lucas  4000000 mai 13 14:57 not_selected.arr
-rw-rw-r-- 1 lucas lucas 80000000 mai 13 14:57 selected.arr

PS.: Thank you for the book and sharing your knowledge!

A typo in page 559

loop.run_until.complete in the first sentence of Using asyncio.as_completed should be loop.run_until_complete.

ISBN: 978-7-5641-6874-2

How to kill multiprocess parent program?

I run program but can't kill parent proccess.

code:

#-*- coding:utf-8 -*-
from concurrent import futures
import os
import sys
import signal
 
def test(num):
    import time
    
    print os.getpid()
    time.sleep(100)
    return time.ctime(), num

def handle_interrupt(signum, frame):
    print 'CAUGHT SIGINT!!!!!!!!!!!!!!!!!!!'
    sys.exit(1)

def main():

    signal.signal(signal.SIGTERM, handle_interrupt) # kill
    signal.signal(signal.SIGINT, handle_interrupt)  # Ctrl+C
    signal.signal(signal.SIGQUIT, handle_interrupt) # Ctrl+\

    with futures.ProcessPoolExecutor(max_workers=3) as executor:
        print 'id:', id(executor)
        future1 = executor.submit(test,1)
        future2 = executor.submit(test,2)
        print future1
        print future1

        print future1.result()
        print future2.result()


if __name__ == '__main__':
    main()

run and press Ctrl + C can exit program:

python mul_future.py 
id: 140637338392144
<Future at 0x7fe8a35b8ad0 state=running>
<Future at 0x7fe8a35b8ad0 state=running>
2082
2083
^C
CAUGHT SIGINT!!!!!!!!!!!!!!!!!!!
CAUGHT SIGINT!!!!!!!!!!!!!!!!!!!
CAUGHT SIGINT!!!!!!!!!!!!!!!!!!!
CAUGHT SIGINT!!!!!!!!!!!!!!!!!!!

But use kill can't exit program

**session1:**run program:

python mul_future.py 
id: 140243206338128
<Future at 0x7f8cdf41aad0 state=running>
<Future at 0x7f8cdf41aad0 state=running>
2091
2092

**session2:**kill parent proccess:

ps -ef | grep python
root      2087  1910  0 21:32 pts/0    00:00:00 python mul_future.py
root      2091  2087  0 21:32 pts/0    00:00:00 python mul_future.py
root      2092  2087  0 21:32 pts/0    00:00:00 python mul_future.py
root      2093  2087  0 21:32 pts/0    00:00:00 python mul_future.py

kill 2087

ps -ef | grep python
root      2087  1910  0 21:32 pts/0    00:00:00 python mul_future.py
root      2091  2087  0 21:32 pts/0    00:00:00 python mul_future.py
root      2092  2087  0 21:32 pts/0    00:00:00 python mul_future.py
root      2093  2087  0 21:32 pts/0    00:00:00 python mul_future.py

what should i do?

FrozenJSON

example 19-5 FrozenJSON

def __init__(self, mapping):
        self.__data = dict(mapping)  ➊

    def __getattr__(self, name):  ➋
        if hasattr(self.__data, name):
            return getattr(self.__data, name)  ➌
        else:
            return FrozenJSON.build(self.__data[name])  ➍

self.__data is a dict ,function hasattr(object, name) the first parameter is object,so hasattr(self.__data, name) always return False,so step ➌ never return . am i right?

How to maintain keys in TransformDict?

I see the demo:
'''
>>> d = TransformDict(str.lower)
>>> d['Foo'] = 5
>>> d['foo'] == d['FOO'] == d['Foo'] == 5
True
>>> set(d.keys())
{'Foo'}
'''
but , i dont know the object how to maintain the keys.
thanks

Missing word in a sentence

In my version of the book, in Chapter 9: A Pythonic Object in section classmethod Versus staticmethod there is a general note (raven box) and inside there is a sentence "Maybe the function is closely related even if it never touches the class, so you want to them nearby in the code." I would suspect it is missing a "have" (or something similar) so the sentence than become ", so you want to have them nearby in the code."?

Sorry for posting it here, I know it is not related to the code exactly.

Windows Specific Issue and work-around: Chapter 17: flags2_common.py

First of all - a well written and complete book for intermediate to advanced Python users. It covers a lot of territory and for the most part will work as is on a variety of systems. I did find one bug in chapter 17 that prevents you from moving through the last part of the chapter. The error is in flags2_common.py on Windows 8-10: Not sure if this effects other systems too. The code provided is this...

def main(download_many, default_concur_req, max_concur_req):

    args, cc_list = process_args(default_concur_req)

    actual_req = min(args.max_req, max_concur_req, len(cc_list))

    initial_report(cc_list, actual_req, args.server)

    base_url = SERVERS[args.server]

    t0 = time.time()

    counter = download_many(cc_list, base_url, args.verbose, actual_req)

    assert sum(counter.values()) == len(cc_list), \

        'some downloads are unaccounted for'

    final_report(cc_list, counter, t0)

For windows, this will throw an error: This code works better...

def main(download_many, default_concur_req, max_concur_req):

    args, cc_list = process_args(default_concur_req)

    actual_req = min(args.max_req, max_concur_req, len(cc_list))

    initial_report(cc_list, actual_req, args.server)

    base_url = SERVERS[args.server]

    t0 = time.time()

    counter = download_many(cc_list, base_url, args.verbose, actual_req)

    assert sum(counter.values()) == len(cc_list), 'some downloads are unaccounted for'
    
    final_report(cc_list, counter, t0)

Let me know if you need clarification.

Example 2-21 in book errors out in python 2.7

# Example 2-21. Changing the value of an array item by poking one of its bytes
import array
numbers = array.array('h', [-2, -1, 0, 1, 2])
memv = memoryview(numbers)

Gives me this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-f1be3db9ba34> in <module>()
----> 1 memv = memoryview(numbers)

TypeError: cannot make memory view because object does not have the buffer interface

Based on research I did, this is a known limitation of python 2.7:

An array can only expose its contents via the old-style buffer interface. This limitation does not apply to Python 3, where memoryview objects can be constructed from arrays, too. Array elements may be multi-byte values.

Source: Python Docs

This info might be useful to someone out there. Not sure if it's worth noting in a future revision of the book :)

about the hash() function

In Python3.7.4

When I test to run this:

>>> hash((1.000001))
2305843009025
>>> hash(1.000001)
2305843009025
>>> (1.000001) == 1.000001
True
>>> [1.000001] == 1.000001
False

But why:

>>> frozenset({1.000001}) == 1.000001
False
>>> hash(frozenset({1.000001}))
8757482414179413028

float, tuple and frozenset are all hashable, why frozenset is special

Illegal copy of book found.

Hello sir,

@ramalho I found a GitHub repository with your book downloadable as a .pdf there. I don't know if you allowed that to be put up there, but if not then there's the link: https://github.com/EvanLi/programming-book-3/tree/master/Python

This repository also has dozens of other O'Reilly books for download, not sure if that was intentional but would like to notify you nonetheless. Other than that, I have the paperback book and it is super useful, thank you!

Code comments unreadable on kindle

Here's one big thing: I have been trying to read it on a Kindle Paperwhite (A stretch, I know, for a programming book, but I worked hard to make Atomic Scala work on it because I know for a lot of people, including me, it's the nicest reading tool) and it's gotten to where I can't anymore, because of the way they handle comments -- which are greyed out to the point where they are basically unreadable. I don't know what control you have over this but you should at least give O'Reilly feedback so they know there's a problem. They should be testing on that, and this makes me think they aren't. (via private e-mail to LR)

17-futures/countries/flags_asyncio.py fails to download flags

When running with python 3.6.6, with aiohttp 3.3.2, the 'flags_asyncio.py' script outputs 20 pairs of lines similar to this:
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x10ecbfd68>
followed by 20 tracebacks similar to this:
Task exception was never retrieved
future: <Task finished coro=<download_one() done, defined at flags_asyncio.py:28> exception=TypeError("'_SessionRequestContextManager' object is not iterable",)>
Traceback (most recent call last):
File "flags_asyncio.py", line 30, in download_one
image = yield from get_flag(cc) # <7>
File "flags_asyncio.py", line 23, in get_flag
resp = yield from aiohttp.request('GET', url) # <4>
TypeError: '_SessionRequestContextManager' object is not iterable

18-asyncio/charfinder/http_charfinder.py 'Request' object has no Attribute 'GET'

the 18-asyncio-py3.7 folder doesn't have this file.
but this file doesn't work in aiohttp3.5.4 with Python3.7.2
the Request object has no attribute 'GET', use query instead.

def home(request):
    try:
        query = request.GET.get('query', '').strip()
    except AttributeError:
        query = request.query.get('query', '').strip()
    print('Query: {!r}'.format(query))

11-iface-abc/tombola_runner.py fixture 'cls' not found

my python version is 3.7.6, when I try running tombola_runner.py, it reports to me that: fixture 'cls' not found

the error log is:

file D:\huawei-code\github\example-code\11-iface-abc\tombola_runner.py, line 23
  def test(cls, verbose=False):
E       fixture 'cls' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

AttributeError

18-Python 3.7: module 'asyncio' has no attribute 'run'

listcomp_speed.py will rasise a error in python2.7 and run well in python3.5

the code in listcomp_speed.py will rasise an syntax error:
def clock(label, cmd):
... res = time.repeat(cmd, setup=SETUP, number=TIMES)
... print(label, *('{:3.f}.formart(x) for x in res'))
File "", line 3
print(label, *('{:3.f}.formart(x) for x in res'))
^
SyntaxError: invalid syntax

but it doesn't make sense to me, as the asterisk in print is unpacking, and unpacking is support in print, as
print(*("1","2")) will work well.

and the same code run well in python3.5.

the book did not specify the environment of python.

17-futures-py3.7, toxiproxy settings update necessary

The toxiproxy settings for localhost:8003 (25% error, 50% delay) are as below.

TODO: UPDATE NEXT PARAGRAPH

There is also the XXX script which runs a proxy on port 8003 producing errors in 25% of the responses and a .5 s delay to 50% of the responses. You can also test it with the browser on port 8003, but rememeber that errors are expected.

An update for the necessary settings would help a lot.

ValueError: charset must not be in content_type argument

When running http server given by example-code/18-asyncio/charfinder/http_charfinder.py the error is thrown:
Error handling request Traceback (most recent call last): File "/home/osboxes/.local/lib/python3.5/site-packages/aiohttp/web_protocol.py", line 416, in start resp = yield from self._request_handler(request) File "/home/osboxes/.local/lib/python3.5/site-packages/aiohttp/web.py", line 323, in _handle resp = yield from handler(request) File "/home/osboxes/.local/lib/python3.5/site-packages/aiohttp/web_urldispatcher.py", line 139, in handler_wrapper result = old_handler(*args, **kwargs) File "http_charfinder.py", line 43, in home return web.Response(content_type=CONTENT_TYPE, text=html) File "/home/osboxes/.local/lib/python3.5/site-packages/aiohttp/web_response.py", line 458, in __init__ raise ValueError("charset must not be in content_type " ValueError: charset must not be in content_type argument

My Env: Python 3.5, aiohttp 2.3.2

Removing charset=UTF-8 param from CONTENT_TYPE constant solves the issue.

_abc_registry attribute is not available in python 3.7.0

When I try running the tombola_runner.py in chapter 11, python reports to me that the '_abc_registry' doesn't exist.

I searched in docs, but found that the attribute is not accesible anymore.

The changelog is
"""
bpo-31333: _abc module is added. It is a speedup module with C implementations for various functions and methods in abc. Creating an ABC subclass and calling isinstance or issubclass with an ABC subclass are up to 1.5x faster. In addition, this makes Python start-up up to 10% faster.

Note that the new implementation hides internal registry and caches, previously accessible via private attributes _abc_registry, _abc_cache, and _abc_negative_cache. There are three debugging helper methods that can be used instead _dump_registry, _abc_registry_clear, and _abc_caches_clear.
"""

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.