Skip to content

Instantly share code, notes, and snippets.

@mahenzon
Created April 20, 2024 18:54
Show Gist options
  • Save mahenzon/a99f1e724264b574a33dfe693890065b to your computer and use it in GitHub Desktop.
Save mahenzon/a99f1e724264b574a33dfe693890065b to your computer and use it in GitHub Desktop.
Please don't use these Python instruments
from collections import namedtuple
from dataclasses import dataclass
# from typing import NamedTuple
# Point = namedtuple("Point", "x, y")
AgeAndWeight = namedtuple(
"AgeAndWeight",
"age, weight",
)
# class Point(NamedTuple):
# x: int
# y: int
@dataclass(slots=True, frozen=True)
class Point:
x: int
y: int
def main():
p1 = Point(1, 2)
print(p1)
# x1, y1 = p1
x1 = p1.x
y1 = p1.y
print(x1, y1)
print(p1.x.bit_count())
p2 = Point("one", "two")
print(p2.x.title())
print(Point.mro())
a_w = AgeAndWeight(20, 87)
print(a_w)
print("age:", a_w.age)
p3 = Point(20, 87)
print("a_w == p3?", a_w == p3)
print((1, 2) == (1, 2))
# print(p3.x)
# p3.x = 10
# print(p3.x)
#
# p3.z = 7
# print(p3.z)
if __name__ == "__main__":
main()
import operator
from operator import attrgetter
from operator import itemgetter
from dataclasses import dataclass, field
from random import randint, random
@dataclass
class Rating:
value: float = field(default_factory=random)
@dataclass
class Person:
name: str
age: int
rating: Rating = field(default_factory=Rating)
def run_operation(op, values):
return list(map(op, *values))
def main():
values = [
(1, 2),
(3, 5),
(7, 9),
(99, 9),
(1, 1),
# (1, 2, 3),
]
numbers = list(map(lambda x: x[0] + x[1], values))
print(numbers)
numbers = list(map(lambda x: sum(x), values))
print(numbers)
numbers = list(map(sum, values))
print(numbers)
numbers = [sum(x) for x in values]
print(numbers)
print(run_operation(lambda a, b: a + b, zip(*values)))
print(run_operation(operator.add, zip(*values)))
print(run_operation(operator.sub, zip(*values)))
print(run_operation(operator.mul, zip(*values)))
print(run_operation(operator.eq, zip(*values)))
numbers = [randint(1, 10) for _ in range(20)]
print(numbers)
target = 7
print(numbers.index(target))
# count = sum(x == target for x in numbers)
count = operator.countOf(numbers, target)
print("Count of", target, ":", count)
first = ("foo", "bar")
second = ("spam", "eggs")
for line in map(operator.concat, first, second):
print(line)
persons = [
Person("Bob", 33),
Person("Alice", 50),
Person("Nick", 42),
]
print("sorted by name")
sorted_by_name = sorted(persons, key=attrgetter("name"))
for p in sorted_by_name:
print(p)
print("sorted by age")
sorted_by_age = sorted(persons, key=attrgetter("age"))
for p in sorted_by_age:
print(p)
pairs = [
("a", 5),
("b", 4),
("c", 7),
("d", 6),
]
print(pairs)
pairs.sort(key=itemgetter(1))
print(pairs)
users = [
{
"id": 1,
"name": "John",
},
{
"id": 2,
"name": "Bob",
"username": "bob",
},
{
"id": 3,
"name": "Bob",
"username": "bob1",
},
]
print(users)
users.sort(key=itemgetter("id"), reverse=True)
print(users)
users.sort(key=itemgetter("name"))
print(users)
users.sort(key=itemgetter("name", "id"))
print(users)
print(itemgetter("name", "id")(users[0]))
print(attrgetter("name", "age")(persons[1]))
print(attrgetter("name", "age", "rating")(persons[1]))
print(attrgetter("name", "age", "rating.value")(persons[1]))
if __name__ == "__main__":
main()
def main():
# res = 2 + 2 * 2
expr = "2 + 2 * 2"
res = eval(expr)
print("Expr", repr(expr), "res:", res)
# expr_list_dir = "os.listdir()"
# dirs = eval(expr_list_dir)
# print(dirs)
#
# expr_rm = 'os.remove("secret.txt")'
# eval(expr_rm)
import_and_rm = "import os; os.remove('secret.txt')"
exec(import_and_rm)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment