Skip to content

Instantly share code, notes, and snippets.

@vitalizzare
vitalizzare / get.py
Last active November 21, 2020 06:12
A safe get by key function with the default parameter (decorators in python)
from safe import safe
def get(default=None):
'''Return a function f(struct, key) to obtain the value
struct[key] or default if key is out of range.
'''
return safe(default)(lambda struct, key: struct[key])
@vitalizzare
vitalizzare / webdev_online_resources.md
Created November 21, 2020 07:44 — forked from yudistira19/webdev_online_resources.md
Online Resources For Web Developers (No Downloading)
@vitalizzare
vitalizzare / StrictList
Created December 1, 2020 03:35
How to constrain Python using its advanced options
# What if there was an option
# to disable the ability for lists
# to respond to index -1?
from collections import UserList
class StrictList(UserList):
'''List that does not allow index < 0'''
def __getitem__(self, index):
@vitalizzare
vitalizzare / __init__.py
Created December 27, 2020 01:24
Split module into multiple files
from .social_media_post import SocialMediaPost
from .social_media_post_publisher import SocialMediaPostPublisher
# This is used by 'from socsocial_media_post import *'
# As far as there is nothing else here and in app.py
# we import the classes explicitely, this part can be ommited
__all__ = [
'SocialMediaPost',
'SocialMediaPostPublisher'
]
@vitalizzare
vitalizzare / users_moderating.py
Created December 28, 2020 23:58
Single Responsibility Principle
'''
https://twitter.com/jangiacomelli/status/1343676834085543936
'''
from datetime import datetime, timedelta
import logging
class User:
def __init__(self, username):
@vitalizzare
vitalizzare / new_stack.py
Created January 5, 2021 19:55
Stack implementation
# New Stack Implementation
import logging
class Stack:
def __init__(self):
self._items = []
def push(self, item):
self._items.append(item)
@vitalizzare
vitalizzare / first_last_inner_items_of_sequence.py
Created January 10, 2021 19:50
How can we tell if we are at the beginning, at the end, or within a generated sequence in python?
'''
From the @NumFOCUS telethon
title: Z for zip
author: @dontusethiscode
date: December 19, 2020
python version: >= 3.8
video: https://youtu.be/gzbmLeaM8gs?t=15648
edited by: @vitalizzare
date: January 10, 2021
@vitalizzare
vitalizzare / structured_type_example.py
Created April 27, 2021 16:20
Example of using np.dtype to structure data
var_space = np.dtype([
('name', 'S5'), # 5 bytes for the name
('age', 'i1'), # 1 byte for the age
('weight', 'f4') # 4 bytes for the weight value
]) # 10 bytes in total to describe 1 person
def init_persons(n:int) -> np.ndarray:
'''Get initial values for n persons.
Return numpy.ndarray of dtype var_space'''
@vitalizzare
vitalizzare / winetoxin_simple_calculator.py
Last active May 25, 2021 22:01
An alternative Winetoxin's Simple Calculator
# https://replit.com/@winetoxin/Winetoxins-Simple-Calculator
from operator import add, sub, mul, truediv
operations = {"+": add, "-": sub, "*": mul, "/": truediv}
intro = "Welcome to the Winetoxin's Python Project!!!"
decoration = '-+-'
border = decoration * (len(intro)//len(decoration)) + decoration[:len(intro)%len(decoration)]
@vitalizzare
vitalizzare / art.py
Last active May 25, 2021 22:35
"Guess a Number" game using #Python, started by @angelwhocodes
logo = """
____ _ _ _ _
/ ___|_ _ ___ ___ ___ __ _ | \ | |_ _ _ __ ___ | |__ ___ _ __| |
| | _| | | |/ _ \/ __/ __| / _` | | \| | | | | '_ ` _ \| '_ \ / _ \ '__| |
| |_| | |_| | __/\__ \__ \ | (_| | | |\ | |_| | | | | | | |_) | __/ | |_|
\____|\__,_|\___||___/___/ \__,_| |_| \_|\__,_|_| |_| |_|_.__/ \___|_| (_)
"""