rasbt / python_reference Goto Github PK
View Code? Open in Web Editor NEWUseful functions, tutorials, and other Python-related things
Useful functions, tutorials, and other Python-related things
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?
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>
...
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:
You can simply place a line break between the id-anchor and whatever text you'd like to include:
Which will render properly without actually including any line break visual:
In the readme file, there is a hyperlink for the IPython notebook for Sorting Algorithms, but it's pointing to "404: Not Found".
Thanks.
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
https://github.com/rasbt/python_reference/blob/master/not_so_obvious_python_stuff.ipynb#L1118
this function and the next one are iterating over wrong parameter - listcompr instead of generator or generator_yield
I would like to suggest to create a section "development tools".
There are many interesting tools in this area, for example PyBuilder
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)
Thanks for this very helpful intro. Hope this isn't just an annoying nitpick. The sentence:
"As we go up in Python's L -> E -> G -> B hierarchy, the function a_func() finds len() already in the global scope first before it attempts"
is missing an ending such as "to search the Built-in namespace."
I was referred to your tutorial by MITx 6.00.1x Intro to CS & Programming Using Python
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!
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)]'}
Do you know that header cell are made for that and will give anchor if you use them ?
In the following README:
https://github.com/rasbt/python_reference/tree/master/tutorials/sqlite3_howto
the link in the line "Download the script: create_new_db.py" appears to be broken, it refers to https://raw.githubusercontent.com/rasbt/python_reference/master/tutorials/code/create_new_db.py which no longer exists.
The link URL should probably be replaced by
https://github.com/rasbt/python_reference/blob/master/tutorials/sqlite3_howto/code/create_new_db.py
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.