Skip to content

Instantly share code, notes, and snippets.

View nenetto's full-sized avatar
🦊
Keep learning & 🐝 kind

nenetto nenetto

🦊
Keep learning & 🐝 kind
View GitHub Profile
@nenetto
nenetto / screen_fix.sh
Last active November 30, 2021 16:01
Add 16:9 resolution to a VMWare Ubuntu machine
xrandr --newmode "1920x1080" 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync
xrandr --addmode Virtual1 1920x1080
xrandr --output Virtual1 --mode 1920x1080
@nenetto
nenetto / launch.json
Created December 14, 2021 09:25
Launch configuration for Python in Visual Studio Code
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run Python current file",
"type": "python",
"request": "launch",
### Keybase proof
I hereby claim:
* I am nenetto on github.
* I am nenetto (https://keybase.io/nenetto) on keybase.
* I have a public key whose fingerprint is 47B4 35B3 E57D FD46 77FE 4DE9 F885 7F5D C440 4967
To claim this, I am signing this object:
@nenetto
nenetto / bytes_str_unicode.py
Created September 26, 2023 06:29
Python bytes, string and unicode
def to_str(bytes_or_str):
"""Transform to string UTF-8"""
if isinstance(bytes_or_str, bytes):
value = bytes_or_str.decode(‘utf-8’)
else:
value = bytes_or_str
return value # Instance of str
def to_bytes(bytes_or_str):
"""Transform to bytestring"""
@nenetto
nenetto / classmethod.py
Last active September 26, 2023 06:40
Example of classmethod for class polymorphism and different constructors
# Python only supports a single constructor per class, the __init__ method.
# Use @classmethod to define alternative constructors for your classes.
# Use class method polymorphism to provide generic ways to build and connect concrete subclasses.
class GenericInputData(object):
def read(self):
raise NotImplementedError
@nenetto
nenetto / super_example.py
Created September 26, 2023 06:43
Class super for initialization
class MyBaseClass:
def __init__(self, value):
self._value = value
class Explicit(MyBaseClass):
def __init__(self, value):
super(__class__, self).__init__(value * 2)
class Implicit(MyBaseClass):
def __init__(self, value):
@nenetto
nenetto / mixin_example.py
Created September 26, 2023 06:53
Mix-in Classes
# Avoid using multiple inheritance if mix-in classes can achieve the same outcome.
# Use pluggable behaviors at the instance level to provide per-class customization when mix-in classes may require it.
# Compose mix-ins to create complex functionality from simple behaviors.
class ToDictMixin(object):
"""This class add the ability to serialize an object to a dictionary
Takes each element of self.__dict__ and saves it
"""
@nenetto
nenetto / property_example.py
Created September 26, 2023 06:57
Properties example
class FixedResistance(Resistor):
@property
def ohms(self):
return self._ohms
@ohms.setter
def ohms(self, ohms):
if hasattr(self, ‘_ohms’):
raise AttributeError(“Can’t set attribute”)
@nenetto
nenetto / descriptors.py
Created September 26, 2023 07:19
Descriptors
# Descriptor protocol
# Special objects with that show magic functionality when they are the type of
# other's class attribute.
## Methods
def __get__(self, obj, type = None) -> object:
"""
self: descriptor itself
obj: object the descriptor is attached (as attribute) to
@nenetto
nenetto / metaclass.py
Created September 26, 2023 07:26
Metaclasses Python
# Rarely used, metaclasses can be used for validation of other classes
class ValidatePolygon(type):
def __new__(meta, name, bases, class_dict):
# Don’t validate the abstract Polygon class
if bases != (object,):
if class_dict[‘sides’] < 3:
raise ValueError(‘Polygons need 3+ sides’)
return type.__new__(meta, name, bases, class_dict)