Browse Source

Readme

pull/1/head
Jure Šorn 7 years ago
parent
commit
11efefd2cd
1 changed files with 24 additions and 27 deletions
  1. 51
      README.md

51
README.md

@ -39,7 +39,7 @@ dict(zip(keys, values)) # Initiates a dict from two lists.
{k: v for k, v in <dict>.iteritems() if k in <list>} # Filters a dict by keys. {k: v for k, v in <dict>.iteritems() if k in <list>} # Filters a dict by keys.
``` ```
#### Counter
### Counter
```python ```python
>>> from collections import Counter >>> from collections import Counter
>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red'] >>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
@ -56,8 +56,8 @@ Set
<set>.union(<set>) <set>.union(<set>)
<set>.intersection(<set>) <set>.intersection(<set>)
<set>.difference(<set>) <set>.difference(<set>)
frozenset() # Is hashable and can be used as a key in dictionary.
``` ```
#### Frozenset — Is hashable and can be used as a key in dictionary.
Range Range
@ -67,7 +67,7 @@ range(to_exclusive)
range(from_inclusive, to_exclusive) range(from_inclusive, to_exclusive)
range(from_inclusive, to_exclusive, step_size) range(from_inclusive, to_exclusive, step_size)
``` ```
##### Negative _step_size_ can be used for backward range.
* Negative `step_size` can be used for backward range.
Enumerate Enumerate
--------- ---------
@ -89,12 +89,12 @@ TestResults(filed=1, attempted=2)
Iterator Iterator
-------- --------
#### Reads input until it reaches an empty line.
#### Reads input until it reaches an empty line:
```python ```python
for line in iter(input, ''): for line in iter(input, ''):
... ...
``` ```
Use _partial_ from functools if function needs arguments.
* Use `partial` from functools if function needs arguments:
#### Skips first element. #### Skips first element.
```python ```python
@ -212,13 +212,12 @@ args = (1, 2)
kwargs = {'x': 3, 'y': 4, 'z': 5} kwargs = {'x': 3, 'y': 4, 'z': 5}
func(*args, **kwargs) func(*args, **kwargs)
``` ```
#### Is the same as
#### Is the same as:
```python ```python
func(1, 2, x=3, y=4, z=5) func(1, 2, x=3, y=4, z=5)
``` ```
#### "*" is the splat operator, that takes a list as input, and expands it into
actual positional arguments in the function call.
* "*" is the splat operator, that takes a list as input, and expands it into actual positional arguments in the function call.
Inline Inline
@ -234,14 +233,13 @@ lambda <argument1>, <argument2>: <return_value>
[i+1 for i in range(10)] # [1, 2, ..., 10] [i+1 for i in range(10)] # [1, 2, ..., 10]
[i for i in range(10) if i>5] # [6, 7, ..., 9] [i for i in range(10) if i>5] # [6, 7, ..., 9]
{i: i*2 for i in range(10)} # {0: 0, 1: 2, ..., 9: 18} {i: i*2 for i in range(10)} # {0: 0, 1: 2, ..., 9: 18}
(x+5 for x in range(0, 10)) # (5, 6, ..., 14)
(x+5 for x in range(0, 10)) # (5, 6, ..., 14) -> Generator
``` ```
##### Last one creates a generator.
```python ```python
[i+j for i in range(10) for j in range(10)] [i+j for i in range(10) for j in range(10)]
``` ```
#### Is the same as
#### Is the same as:
```python ```python
out = [] out = []
for i in range(10): for i in range(10):
@ -273,12 +271,11 @@ def multiply_closure(x):
def wrapped(y): def wrapped(y):
return x * y return x * y
return wrapped return wrapped
```
```python
multiply_by_3 = multiply_closure(3) multiply_by_3 = multiply_closure(3)
``` ```
#### or
#### Or:
```python ```python
from functools import partial from functools import partial
partial(<function>, <argument>) partial(<function>, <argument>)
@ -292,7 +289,7 @@ def function_that_gets_passed_to_closure():
pass pass
``` ```
#### Debugger example
#### Debugger example:
```python ```python
from functools import wraps from functools import wraps
@ -327,9 +324,9 @@ import enum
class <enum_name>(enum.Enum): class <enum_name>(enum.Enum):
<name1> = <value1> <name1> = <value1>
<name2> = <value2> <name2> = <value2>
<name3> = enum.auto() # Can be used for automatic indexing.
... ...
``` ```
##### Also "<name3> = enum.auto()" can be used for automatic indexing.
```python ```python
<enum_name>.<name> # == <enum> <enum_name>.<name> # == <enum>
@ -540,7 +537,7 @@ islice([1, 2, 3], 1, None)
``` ```
### 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
@ -561,25 +558,25 @@ Introspection and Metaprograming
>>> b = B() >>> b = B()
``` ```
#### Getattr
### Getattr
```python ```python
>>> getattr(b, 'a') >>> getattr(b, 'a')
'sdfsd' 'sdfsd'
``` ```
##### Is the same as
#### Is the same as:
```python ```python
B.__getattribute__(b, 'a') B.__getattribute__(b, 'a')
``` ```
#### Hasattr
### Hasattr
```python ```python
>>> hasattr(b, 'c') >>> hasattr(b, 'c')
False False
``` ```
#### Setattr
### Setattr
```python ```python
>>> setattr(b, 'c', 10) >>> setattr(b, 'c', 10)
``` ```
@ -603,7 +600,7 @@ def my_meta_class(name, parents, attrs):
... ...
return type(name, parents, attrs) return type(name, parents, attrs)
``` ```
##### or
#### Or:
```python ```python
class MyMetaClass(type): class MyMetaClass(type):
def __new__(klass, name, parents, attrs): def __new__(klass, name, parents, attrs):
@ -698,19 +695,19 @@ def draw(screen):
pass pass
``` ```
##### Gets char from int
#### Gets char from int:
```python ```python
chr(<int>) chr(<int>)
``` ```
Profile Profile
------- -------
#### Times execution of the passed code.
#### Times execution of the passed code:
```python ```python
import timeit import timeit
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000) timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
``` ```
#### Generates a PNG image of call graph and highlights the bottlenecks.
#### Generates a PNG image of call graph and highlights the bottlenecks:
```python ```python
import pycallgraph import pycallgraph
graph = pycallgraph.output.GraphvizOutput() graph = pycallgraph.output.GraphvizOutput()
@ -718,7 +715,7 @@ graph.output_file = get_file_name()
with pycallgraph.PyCallGraph(output=graph): with pycallgraph.PyCallGraph(output=graph):
<code_to_be_profiled> <code_to_be_profiled>
``` ```
##### Utility code for unique PNG filenames
#### Utility code for unique PNG filenames:
```python ```python
def get_file_name(): def get_file_name():
return "{}-{}.png".format("profile", get_current_datetime_string()) return "{}-{}.png".format("profile", get_current_datetime_string())
@ -733,7 +730,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:
```python ```python
import wave, struct import wave, struct
frames = [struct.pack("%dh"%(1), int((a-0.5)*60000)) for a in <list>] frames = [struct.pack("%dh"%(1), int((a-0.5)*60000)) for a in <list>]

Loading…
Cancel
Save