@ -1,4 +1,4 @@
Python and Libraries Cheatsheet
Comprehensive Python Cheatsheet
===============================
===============================
Main
Main
@ -20,6 +20,11 @@ sum(<list>)
sorted_by_second = sorted(< list > , key=lambda tup: tup[1])
sorted_by_second = sorted(< list > , key=lambda tup: tup[1])
```
```
Flatten List
```
[item for sublist in list for item in sublist]
```
Dictionary
Dictionary
----------
----------
```python
```python
@ -35,6 +40,11 @@ Init from two lists
dict(zip(keys, values))
dict(zip(keys, values))
```
```
Filter by keys
```
{k: v for k, v in d.iteritems() if k in [2,3]}
```
Counter
Counter
```
```
>>> from collections import Counter
>>> from collections import Counter
@ -52,7 +62,7 @@ Set
< set > .union(< set > )
< set > .union(< set > )
< set > .intersection(< set > )
< set > .intersection(< set > )
< set > .difference(< set > )
< set > .difference(< set > )
< frozenset > - is hashable can be used as key in dictionary
< frozenset > - Is hashable and can be used as key in dictionary
```
```
Range
Range
@ -514,14 +524,13 @@ islice([1, 2, 3], 1, None)
[2, 3]
[2, 3]
```
```
### Ifilter/imap/izip
### ifilter/imap/izip
Filter, map and zip functions that return generators instead of iterators
Filter, map and zip functions that return generators instead of iterators
Introspection and Metaprograming
Introspection and Metaprograming
--------------------------------
--------------------------------
Inspecting code at rune time and code that generates code.
Inspecting code at runtime and code that generates code.
```python
```python
>>> class B:
>>> class B:
@ -566,6 +575,7 @@ BB = type('B', (), {'a': 'sdfsd', 'b': 123324}
b = BB()
b = BB()
```
```
MetaClass
MetaClass
---------
---------
Classes that creates classes.
Classes that creates classes.
@ -582,6 +592,7 @@ class MyMetaClass(type):
return type.__new__(klass, name, parents, attrs)
return type.__new__(klass, name, parents, attrs)
```
```
Do Stuff
Do Stuff
--------
--------
* Look at the attributes
* Look at the attributes
@ -590,6 +601,7 @@ Do Stuff
* Traverse the parent classes
* Traverse the parent classes
* Change values in the class
* Change values in the class
Metaclass Attr
Metaclass Attr
--------------
--------------
When class is created it checks if it has metaclass defined. If not, it recursively checks if any of his parents has it defined, and eventually comes to type.
When class is created it checks if it has metaclass defined. If not, it recursively checks if any of his parents has it defined, and eventually comes to type.
@ -598,6 +610,7 @@ class BlaBla:
__metaclass__ = Bla
__metaclass__ = Bla
```
```
Eval
Eval
----
----
```
```
@ -633,20 +646,6 @@ def eval_(node):
```
```
Ascii
-----
Get char from int.
```
chr(< int > )
```
Flatten List
------------
```
[item for sublist in a_list for item in sublist]
```
Libraries
Libraries
=========
=========
@ -691,7 +690,10 @@ def draw(screen):
pass
pass
```
```
Get char from int
```
chr(< int > )
```
Profile
Profile
-------
-------
@ -718,7 +720,7 @@ def get_datetime_string(a_datetime):
Audio
Audio
-----
-----
Saves list of floats of size 0 to 1 to a wav file.
Saves list of floats of size 0 to 1 to a WAV file.
```
```
import wave
import wave
import struct
import struct
@ -731,16 +733,6 @@ wf.writeframes(b''.join(frames))
wf.close()
wf.close()
```
```
Coockboock
==========
Dictionary
----------
Filter by keys:
```
{k: v for k, v in d.iteritems() if k in [2,3]}
```
xxxxxxxxxx