python
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| python [2021/08/02 17:57] – [Cheat sheet] utedass | python [2022/12/05 16:26] (current) – utedass | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| * pyserial -- Comport | * pyserial -- Comport | ||
| * matplotlib -- Matlabliknande plotverktyg | * matplotlib -- Matlabliknande plotverktyg | ||
| - | * numpy -- Matlabliknande matte | + | * [[python:numpy]] -- Matlabliknande matte |
| * pandas -- Importera och exportera till olika filformat, ex CSV, tydligen också för att behandla data | * pandas -- Importera och exportera till olika filformat, ex CSV, tydligen också för att behandla data | ||
| Line 18: | Line 18: | ||
| * Hur man läser syntaxguide i pythons dokumentation | * Hur man läser syntaxguide i pythons dokumentation | ||
| * Hur man använder context managers | * Hur man använder context managers | ||
| + | * Vad är generators, hur fungerar yield | ||
| + | * Vem fan kom på syntaxen '' | ||
| + | * ^ Verkar vara [[https:// | ||
| ====== Cheat sheet ====== | ====== Cheat sheet ====== | ||
| Line 28: | Line 30: | ||
| populated_tuple = (1, 2, " | populated_tuple = (1, 2, " | ||
| singleton_tuple = (3,) | singleton_tuple = (3,) | ||
| + | print(populated_tuple[2]) # Prints Kalle | ||
| # Ranges are also immutable | # Ranges are also immutable | ||
| Line 55: | Line 58: | ||
| all_keys = list(my_dict) | all_keys = list(my_dict) | ||
| leet = my_dict[' | leet = my_dict[' | ||
| + | |||
| + | # Dictionaries can be unpacked and used as arguments to a function | ||
| + | dude = {a: 4, b: 5} | ||
| + | function_that_takes_a_and_b(**dude) | ||
| # Set types must contain hashable objects. They are not ordered and cannot be indexed | # Set types must contain hashable objects. They are not ordered and cannot be indexed | ||
| Line 82: | Line 89: | ||
| <code python> | <code python> | ||
| + | |||
| + | # Name mangling for double underscore and less than one trailing underscores | ||
| + | class Foo(object): | ||
| + | yo = None # => foo_object.yo | ||
| + | _yo = None # => foo_object._yo | ||
| + | __yo = None # => foo_object._Foo__yo | ||
| + | __yo__ = None # => foo_object.__yo__ | ||
| + | |||
| + | |||
| # To copy an object | # To copy an object | ||
| import copy | import copy | ||
| Line 88: | Line 104: | ||
| - | ====== | + | ====== |
| <code python> | <code python> | ||
| Line 133: | Line 149: | ||
| ====== Exceptions ====== | ====== Exceptions ====== | ||
| [[https:// | [[https:// | ||
| + | |||
| + | [[https:// | ||
| <code python> | <code python> | ||
| Line 156: | Line 174: | ||
| raise ValueError(" | raise ValueError(" | ||
| </ | </ | ||
| + | |||
| + | ====== Decorators ====== | ||
| + | Syntactic sugar based on functions that return function objects. | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | ===== Basics ===== | ||
| + | |||
| + | <code python> | ||
| + | # Given this function, which is called a decorator function | ||
| + | def my_decorator(func): | ||
| + | def wrapper(): | ||
| + | print(" | ||
| + | func() | ||
| + | return wrapper | ||
| + | |||
| + | # Then one could write something like this | ||
| + | def boring(): | ||
| + | print(" | ||
| + | | ||
| + | pimped_boring = my_decorator(boring) | ||
| + | pimped_boring() | ||
| + | |||
| + | # Or one could overwrite the boring function name | ||
| + | def boring(): | ||
| + | print(" | ||
| + | boring = my_decorator(boring) | ||
| + | |||
| + | # which is equivalent to write this short-cut syntax | ||
| + | @my_decorator | ||
| + | def boring(): | ||
| + | print(" | ||
| + | </ | ||
| + | |||
| + | ===== Chaining ===== | ||
| + | |||
| + | <code python> | ||
| + | # Given the two decorators | ||
| + | def star(func): | ||
| + | def inner(*args, | ||
| + | print(" | ||
| + | func(*args, **kwargs) | ||
| + | print(" | ||
| + | return inner | ||
| + | |||
| + | |||
| + | def percent(func): | ||
| + | def inner(*args, | ||
| + | print(" | ||
| + | func(*args, **kwargs) | ||
| + | print(" | ||
| + | return inner | ||
| + | | ||
| + | # Then | ||
| + | @star | ||
| + | @percent | ||
| + | def printer(msg): | ||
| + | print(msg) | ||
| + | | ||
| + | # is equivalent to | ||
| + | def printer(msg): | ||
| + | print(msg) | ||
| + | printer = star(percent(printer)) | ||
| + | </ | ||
| + | ===== Property decorator and setter decorators ===== | ||
| + | [[https:// | ||
| + | |||
| + | <code python> | ||
| + | class MyClass(object): | ||
| + | def __init__(self): | ||
| + | self._my_property_value = None # Placeholder variable, so getter function works | ||
| + | self.my_property = 123 # Will call the proper setter function | ||
| + | | ||
| + | @property | ||
| + | def my_property(self): | ||
| + | return self._my_property_value | ||
| + | | ||
| + | @my_property.setter | ||
| + | def my_property(self, | ||
| + | if value < 0: | ||
| + | self._my_property_value = -value * 10 | ||
| + | else: | ||
| + | self._my_property_value = value/10 | ||
| + | | ||
| + | my_instance = MyClass() | ||
| + | my_instance.my_property = -30 | ||
| + | print(my_instance.my_property) # Prints 300 | ||
| + | </ | ||
| + | |||
| + | |||
| + | ====== tkinter ====== | ||
| + | [[https:// | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | [[https:// | ||
| + | |||
python.1627927068.txt.gz · Last modified: 2022/09/12 00:30 (external edit)
