Skip to content

Commit be4f1f4

Browse files
committed
Added more
1 parent 60b764d commit be4f1f4

17 files changed

+333
-21
lines changed

docs/basic.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Basic
2+
=====
3+
4+
Snippets on basic things.
5+
6+
7+
`ternary conditional operator`_
8+
-------------------------------
9+
10+
::
11+
12+
>>> 'true' if True else 'false'
13+
'true'
14+
>>> 'true' if False else 'false'
15+
'false'
16+
17+
18+
`accessing the index in for loops`_
19+
-----------------------------------
20+
21+
::
22+
23+
for idx, val in enumerate(ints):
24+
print idx, val
25+
26+
27+
.. _ternary conditional operator: http://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator
28+
.. _accessing the index in for loops: http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops

docs/datetime.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Datetime
2+
========
3+
4+
Snippets about datetime.
5+
6+
7+
`converting string into datetime`_
8+
----------------------------------
9+
10+
::
11+
12+
from datetime import datetime
13+
14+
date_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
15+
16+
17+
.. _converting string into datetime: http://stackoverflow.com/questions/466345/converting-string-into-datetime

docs/decorator.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Decorator
44
Snippets about decorators.
55

66

7-
Decorators 101
7+
decorators 101
88
--------------
99

1010
Let's begin with syntactic sugar::
@@ -30,7 +30,7 @@ Let's begin with syntactic sugar::
3030
add = EXPR(ARG)(add)
3131

3232

33-
Good Decorator
33+
good decorator
3434
--------------
3535

3636
We want to run a function for certain times::
@@ -52,7 +52,7 @@ We want to run a function for certain times::
5252
print word
5353

5454

55-
Method Decorator
55+
method decorator
5656
----------------
5757

5858
We need one decorator that makes function to method decorators::

docs/descriptor.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Descriptor
44
Snippets about descriptors.
55

66

7-
Descriptors 101
7+
descriptors 101
88
---------------
99

1010
What is descriptor:
@@ -33,7 +33,7 @@ Demo::
3333
<bound method Demo.hello of <__main__.Demo object at 0x102b285d0>>
3434

3535

36-
Data And Non-data Descriptors
36+
data and non-data descriptors
3737
-----------------------------
3838

3939
If an object defines both ``__get__`` and ``__set__``, it is considered a data
@@ -96,7 +96,7 @@ Now we add ``__set__``::
9696
'demo3'
9797

9898

99-
Better Way To Write Property
99+
better way to write property
100100
----------------------------
101101

102102
Let's just see the code::
@@ -114,7 +114,7 @@ Let's just see the code::
114114
del _get_thing, _set_thing
115115

116116

117-
Cached Property
117+
cached property
118118
---------------
119119

120120
Just grab it from django source::

docs/dict.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Dictionary
2+
==========
3+
4+
Snippets on dict manipulation.
5+
6+
7+
`merge (union) two Python dictionaries`_
8+
----------------------------------------
9+
10+
::
11+
12+
>>> x = {'a':1, 'b': 2}
13+
>>> y = {'b':10, 'c': 11}
14+
>>> z = dict(x.items() + y.items())
15+
>>> z
16+
{'a': 1, 'c': 11, 'b': 10}
17+
18+
19+
`create a dictionary with list comprehension`_
20+
----------------------------------------------
21+
22+
In Python 2.6 (or earlier), use the dict constructor::
23+
24+
d = dict((key, value) for (key, value) in sequence)
25+
26+
In Python 2.7+ or 3, you can just use the dict comprehension syntax directly::
27+
28+
d = {key: value for (key, value) in sequence}
29+
30+
31+
.. _merge (union) two Python dictionaries: http://stackoverflow.com/questions/38987/how-can-i-merge-union-two-python-dictionaries-in-a-single-expression
32+
.. _create a dictionary with list comprehension: http://stackoverflow.com/questions/1747817/python-create-a-dictionary-with-list-comprehension

docs/email.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Email
44
Snippets about email.
55

66

7-
`Sending-email-with-python`_
7+
`sending email with python`_
88
----------------------------
99

1010
I recommend that you use the standard packages email and smtplib together::
@@ -65,7 +65,7 @@ For multiple destinations::
6565
s.quit()
6666

6767

68-
`Sending mail via sendmail from python`_
68+
`sending mail via sendmail from python`_
6969
----------------------------------------
7070

7171
If I want to send mail not via SMTP, but rather via sendmail::
@@ -81,5 +81,5 @@ If I want to send mail not via SMTP, but rather via sendmail::
8181
p.communicate(msg.as_string())
8282

8383

84-
.. _Sending-email-with-python: http://stackoverflow.com/questions/6270782/sending-email-with-python
85-
.. _Sending mail via sendmail from python: http://stackoverflow.com/questions/73781/sending-mail-via-sendmail-from-python
84+
.. _sending email with python: http://stackoverflow.com/questions/6270782/sending-email-with-python
85+
.. _sending mail via sendmail from python: http://stackoverflow.com/questions/73781/sending-mail-via-sendmail-from-python

docs/file.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ File
44
Snippets about files manipulation.
55

66

7-
`Unicode (utf8) reading and writing to files`_
7+
`check if file exists`_
8+
-----------------------
9+
10+
If you need to be sure it's a file::
11+
12+
import os.path
13+
os.path.isfile(filename)
14+
15+
16+
`unicode (utf8) reading and writing to files`_
817
-----------------------------------------------
918

1019
It is easier to use the open method from the codecs module::
@@ -14,4 +23,15 @@ It is easier to use the open method from the codecs module::
1423
>>> f.read()
1524

1625

17-
.. _Unicode (utf8) reading and writing to files: http://stackoverflow.com/questions/491921/unicode-utf8-reading-and-writing-to-files-in-python
26+
`extracting extension from filename`_
27+
-------------------------------------
28+
29+
::
30+
31+
import os
32+
filename, ext = os.path.splitext('/path/to/somefile.ext')
33+
34+
35+
.. _check if file exists: http://stackoverflow.com/questions/82831/how-do-i-check-if-a-file-exists-using-python
36+
.. _unicode (utf8) reading and writing to files: http://stackoverflow.com/questions/491921/unicode-utf8-reading-and-writing-to-files-in-python
37+
.. _extracting extension from filename: http://stackoverflow.com/questions/541390/extracting-extension-from-filename-in-python

docs/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ Python-Snippets is a basket of Python snippets.
77
.. toctree::
88
:maxdepth: 2
99

10+
basic
11+
string
12+
list
13+
dict
1014
decorator
1115
iterator
1216
descriptor
1317
with
1418
method
1519
metaclass
20+
performance
21+
io
1622
file
1723
email
24+
datetime
25+
subprocess
1826

1927

2028
Contribute

docs/io.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Input and Ouput
2+
===============
3+
4+
Snippets about input and output.
5+
6+
7+
`read from stdin`_
8+
------------------
9+
10+
::
11+
12+
import sys
13+
14+
for line in sys.stdin:
15+
print line
16+
17+
18+
`print to stderr`_
19+
------------------
20+
21+
::
22+
23+
print >> sys.stderr, 'spam'
24+
25+
26+
.. _read from stdin: http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python
27+
.. _print to stderr: http://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python

docs/iterator.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Iterator And Generator
44
Snippets about iterators and generators.
55

66

7-
`iterator - The Python yield keyword explained`_
7+
`iterator - the Python yield keyword explained`_
88
------------------------------------------------
99

1010
Iterables::

docs/list.rst

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
List
2+
====
3+
4+
Snippets on list manipulation.
5+
6+
7+
`check if a list is empty`_
8+
---------------------------
9+
10+
::
11+
12+
if not a:
13+
print "List is empty"
14+
15+
16+
`append vs extend`_
17+
-------------------
18+
19+
append::
20+
21+
x = [1, 2, 3]
22+
x.append([4, 5])
23+
# [1, 2, 3, [4, 5]]
24+
25+
extend::
26+
27+
x = [1, 2, 3]
28+
x.extend([4, 5])
29+
# [1, 2, 3, 4, 5]
30+
31+
32+
`split a list into evenly sized chunks`_
33+
----------------------------------------
34+
35+
Here's a generator that yields the chunks you want::
36+
37+
def chunks(l, n):
38+
""" Yield successive n-sized chunks from l.
39+
"""
40+
for i in xrange(0, len(l), n):
41+
yield l[i:i+n]
42+
43+
44+
`finding the index of an item`_
45+
-------------------------------
46+
47+
::
48+
49+
>>> ["foo","bar","baz"].index('bar')
50+
1
51+
52+
53+
`sort a list of dictionaries by values of the dictionary`_
54+
----------------------------------------------------------
55+
56+
::
57+
58+
newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])
59+
60+
61+
`randomly select an item from a list`_
62+
--------------------------------------
63+
64+
::
65+
66+
import random
67+
68+
foo = ['a', 'b', 'c', 'd', 'e']
69+
random.choice(foo)
70+
71+
72+
.. _check if a list is empty: http://stackoverflow.com/questions/53513/best-way-to-check-if-a-list-is-empty
73+
.. _append vs extend: http://stackoverflow.com/questions/252703/python-append-vs-extend
74+
.. _split a list into evenly sized chunks: http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python
75+
.. _finding the index of an item: http://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python
76+
.. _sort a list of dictionaries by values of the dictionary: http://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary-in-python
77+
.. _randomly select an item from a list: http://stackoverflow.com/questions/306400/how-do-i-randomly-select-an-item-from-a-list-using-python

docs/metaclass.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ Metaclass
44
Snippets about metaclasses.
55

66

7-
Metaclasses 101
7+
metaclasses 101
88
---------------
99

1010
In Python, everything is one object. Take CPython for example, we have
1111
PyObject, everything else is inherited from it, and each PyObject has one type
1212
attribute, PyTypeObject, actually PyTypeObject is one PyObject by itself too,
1313
so PyTypeObject has one type attribute, PyTypeType, PyTypeType has type
1414
attribute too, which is PyTypeType itself. So come back to Python, we have
15-
object, each object has one type, and the type of ``type`` is ``type``.
15+
object, each object has one type, and the type of ``type`` is ``type``.
16+
17+
You can also have a look on this stack overflow question
18+
`What is a metaclass in Python <http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python>`_.
1619

1720
Ok, in Python, everything is simple, so you can just treat metaclass as the
1821
class that creates a class. This is useful for post-processing classes, and

docs/method.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Method
44
Snippets about method.
55

66

7-
Method 101
7+
method 101
88
----------
99

1010
Instancemethods take ``self`` argument, classmethods take ``cls`` argument,

0 commit comments

Comments
 (0)