GithubHelp home page GithubHelp logo

rasbt / python_reference Goto Github PK

View Code? Open in Web Editor NEW
3.8K 235.0 937.0 9 MB

Useful functions, tutorials, and other Python-related things

CSS 0.12% Python 1.29% HTML 3.69% Shell 0.02% Jupyter Notebook 94.88%
python-tips learning-python

python_reference's Issues

Printing database summary

I realize this post is fairly old, however I was wondering if you could please clarify.

It seems to me that in the functions you define in this section, you use the variable c for cursor within the function even though you pass the cursor object to the functions.

Could you please address this?

Bad generated html

On http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/not_so_obvious_python_stuff.ipynb an anchor is closed in wrong place:

Chapter "Only the first clause of generators is evaluated immediately" is highlighted with the code altogether on mouse over.

See screenshot:
http://h.ctf.su/img/tmp.79rYIPXEi7Ua.png

Also chapter "Assigning types to variables as values" is the only chapter title highlighted on mouse over.

Checked on Chrome 34.0.1847.132 and firefox (linux).

PS: Wanted to patch it myself, but .ipynb syntax is mad. Also it seems HTML generated isn't good at all:

...
<a name="variable_types"></p>
</div>
</div>
</div>
...

Workaround for HTML Conversion in Table of Contents

I found a workaround for an issue you describe in tutorials/table_of_contents_ipython.ipynb when converting internal links to an HTML format. Instead of being forced to put the id-anchor tag in a separate cell like you did in your notebook:

image

You can simply place a line break between the id-anchor and whatever text you'd like to include:

image

Which will render properly without actually including any line break visual:

image

More difference between range@py3 and xrange@py2

One thing I keeps telling people about range/xrange difference is that, the range in python3 implement the __contains__ method, which makes it very easy to test if a value is in range.

For example, in Python2, the following code is super slow:

x = 10000000 # a very large number
if x/2 in xrange(x):
    pass

But in Python3, the following code returns immediately:

x = 10000000 # a very large number
if x/2 in range(x):
    pass

To archive the same performance in Python2, one have to write the code like this:

x = 10000000 # a very large number
if 0 <= x/2 <= x-1:
    pass

PyBuilder

I would like to suggest to create a section "development tools".
There are many interesting tools in this area, for example PyBuilder

Slight lambda-in-closures issue

This is great stuff!

In the not-so-obvious python tutorial, one thing caught me eye - you say that the lambda-closure problem doesn't apply to generators, but it actually does. Your example appears to work because each function returned by the generator is going to return the current value of n, which indicates how far along in generator you've gotten. The list example fails because the whole list is evaluated; the generator example "succeeds" because when you call the ith function, n is still only equal to i. To see this in action:

>>> my_gen = (lambda:n for n in range(5))
>>> a = next(my_gen)
>>> a() # a() returns 0 because that's what n is right now
0
>>> a()
0
>>> # Advancing the generator increases n by one
>>> b = next(my_gen)
>>> b() # b returns 1, as expected
1
>>> a() # but now a returns 1, too
1
>>> # calling list(my_gen) will advance the generator to the end
>>> _ = list(my_gen)
>>> a(), b() # now n is 4
(4, 4)

Modifying a list while looping through it

A nice addition to your "not so obvious" notebook would be the modifying a list while looping through it pitfall. For instance, you might be tempted into believing that the following will remove all even values from the list a

>>> a = [1, 2, 3, 4, 5]
>>> for i in a:
...     if not i % 2:
...         a.remove(i)
...
>>> a
[1, 3, 5]

But if you try a different example:

>>> a = [2, 4, 5, 6]
>>> for i in a:
...     if not i % 2:
...         a.remove(i)
...
>>> a
[4, 5]

I'll leave it as an exercise for you to figure out exactly what is going on here.

Dictionaries will protect you from this by raising an exception if they change during iteration, but for lists, you should use a copy (for i in a[:]), or convert the for loop into a while loop.

Great notebook by the way!

%watermark addition.

FYI, IPython.sys_info() can be of help and give you commit hash or IPython and other stuff.

In [1]: import IPython
In [3]: print(IPython.sys_info())
{'commit_hash': b'4aea4a2',
 'commit_source': 'repository',
 'default_encoding': 'UTF-8',
 'ipython_path': '/Users/bussonniermatthias/ipython/IPython',
 'ipython_version': '3.0.0-dev',
 'os_name': 'posix',
 'platform': 'Darwin-11.4.2-x86_64-i386-64bit',
 'sys_executable': '/usr/local/opt/python3/bin/python3.4',
 'sys_platform': 'darwin',
 'sys_version': '3.4.0 (default, Apr  9 2014, 11:57:22) \n'
                '[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)]'}

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.