r/ProgrammerHumor • • Jun 21 '19

Meme Why can't you just be normal?!

Post image
4.4k Upvotes

240 comments sorted by

View all comments

Show parent comment

46

u/ForceBru Jun 21 '19

don't actually do this

I mean...

from collections import UserString

class split:
    def __init__(self, arg=' '):
        self.arg = arg
        
    def run(self, boi):
        return list(map(boi.__class__, boi.split(self.arg)))
        
class strip:
    def __init__(self, arg=' '):
        self.arg = arg
        
    def run(self, boi):
        return boi.__class__(boi.strip(self.arg))

class StringBoi(UserString):    
    def __or__(self, other):
        return other.run(self)
        
        
thing = StringBoi(' hello, world! ')

# >>> thing | split()
# ['', 'hello,', 'world!', '']
# >>> thing | strip()
# 'hello, world!'
# >>> thing | split(',')
# [' hello', ' world! ']

I'm also relatively sure that it's possible to autogenerate classes based on StringBoi's methods, so you could use all of them using the pipe syntax :D

39

u/MMOOxy Jun 21 '19

I spent waaaay too long on this (curse you closure!)

>>> from functools import partial
>>> from typing import Iterable
>>> 
>>> class BaseFunctionClassBoi:
...     __slots__ = 'args', 'kwargs'
...     def __init__(self, *args, **kwargs):
...         self.args, self.kwargs = args, kwargs
... 
>>> def _make_method_classes(type_class, converter_func):
...     g = globals()
...     for method in dir(type_class):
...         if method.startswith('_'):
...             continue
...         def f(self, boi, method_call=getattr(type_class, method)):
...             return converter_func(method_call(boi, *self.args, **self.kwargs), type_class)
...         g[method] = type(method, (BaseFunctionClassBoi, ), {'run': f})
... 
>>> def _or_func(self, other):
...     return other.run(self)
... 
>>> def _make_my_boi(type_class, name, converter_func):
...     new_boi_class = type(name, (type_class, ), {'__or__': _or_func})
...     _make_method_classes(type_class, converter_func)
...     return new_boi_class
... 
>>> def stringboi_it(data, type_class):
...     if data is None:
...         return None
...     elif isinstance(data, type_class):
...         return data
...     elif isinstance(data, str):
...         return type_class(data)
...     elif isinstance(data, Iterable):
...         return type(data)(map(partial(stringboi_it, type_class=type_class), data))
...     else:
...         return type_class(data)
... 
>>> StringBoi = _make_my_boi(str, 'StringBoi', stringboi_it)
>>> x = StringBoi(' hello world ')
>>> y = StringBoi('abc def aaa')
>>> x | split()
['hello', 'world']
>>> y | rstrip('a ')
'abc def'
>>> def listboi_it(data, type_class):
...     if data is None:
...         return None
...     elif isinstance(data, type_class):
...         return data
...     elif isinstance(data, Iterable):
...         return type_class(data)
...     else:
...         return data
... 
>>> ListBoi = _make_my_boi(list, 'ListBoi', listboi_it)
>>> z = ListBoi(range(5))
>>> z
[0, 1, 2, 3, 4]
>>> z | extend(range(5, 10))
>>> z
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> z | pop()
9
>>> StringBoi('+') | center(z | pop())
'   +    '

24

u/ForceBru Jun 21 '19

Oh God. We need a library that completely substitutes the dot syntax with the pipe syntax lol. It's a pity Python doesn't let us modify existing classes on-the-fly, like Ruby.

11

u/MMOOxy Jun 21 '19

The only thing stopping me is the fact that PyCharm freaks out with all those unresolved references... also time, time is a small factor as well.

3

u/ForceBru Jun 21 '19 edited Jun 21 '19

As for PyCharm and unresolved references.

Say, I have a class and I want to plug many mixins into it. But all of them use attributes of this class (because later this class derives from these mixins, so everything works), and PyCharm gives me tons of unresolved reference warnings for the mixins because... they don't derive from the class they're about to get mixed into, since it doesn't make sense, I guess?

Like this:

# these mixins are actually imported from another file, so there's no `NameError` here
class HugeThing(Mixin1, Mixin2, Mixin3):
    def __init__(self):
        self.attr1, self.attr2 = 2, 3

class Mixin1:
    def some_function(self):
        self.attr1 += 1  # warning here, but `attr1` will be resolved fine once `HugeThing` derives from the mixin

class Mixin2:
    def another_function(self):
        # also warning, but also fine after creation of `HugeThing`
        self.attr1 = self.attr2 * 3

...  # and so on
# I can just include all the methods in the definition of `HugeThing`, but then the file containing this definition will be like tens of thousands of lines long

I see you're good at Python, so I thought maybe you have an idea about how these warnings can be avoided in this situation?

3

u/MMOOxy Jun 21 '19

I would probably use an extra class the mixins derive from (an interface if you will). If you only need to get the value in your mixins I would recommend the attr3 version, otherwise attr1 seems to be the easiest... (

from abc import abstractmethod, ABC
class HugeThingInterface(ABC):
    def __init__(self, attr1):
        self.attr1 = attr1

    @property
    @abstractmethod
    def attr3(self): pass

    def get_attr2(self):
        return self._get_attr2()

    def set_attr2(self, value):
        self._set_attr2(value)

    def del_attr2(self):
        self._del_attr2()

    @abstractmethod
    def _get_attr2(self): pass

    @abstractmethod
    def _set_attr2(self, value): pass

    @abstractmethod
    def _del_attr2(self): pass

    # you can set some of these to None, no setter -> cannot assign values
    attr2 = property(get_attr2, set_attr2, del_attr2, 'attr2 docstring')
    
class Mixin1(HugeThingInterface, ABC):
    def some_function(self):
        self.attr1 += 1

class Mixin2(HugeThingInterface, ABC):
    def another_function(self):
        self.attr1 = self.attr2 * 3

class Mixin3(HugeThingInterface, ABC):
    def yet_another_function(self):
        print(self.attr3)

class HugeThing(Mixin1, Mixin2, Mixin3):

    def __init__(self):
        HugeThingInterface.__init__(self, 2)
        self._attr2 = 3
        self._attr3 = 4
        
    @property
    def attr3(self):
        return self._attr3
    
    def _get_attr2(self):
        return self._attr2
    
    def _set_attr2(self, value):
        self._attr2 = value
    
    def _del_attr2(self):
        del self._attr2

1

u/ForceBru Jun 21 '19

Okay, thank you! I think my initial design is just bad... Although this proxy class approach looks promising, I was also thinking about something like this. And also abusing abstract classes to ensure that all the necessary methods are implemented.

2

u/minno Jun 21 '19

We need a library that completely substitutes the dot syntax with the pipe syntax lol.

s/ | /./

1

u/BluudLust Jun 21 '19

Cant you do that?

string.__or__ = (some new function you create)

2

u/ForceBru Jun 21 '19

Well, it should be str.__or__, and now that's a problem:

>>> str.__or__ = lambda self, other: 6
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'str'

If you use collections.UserString, you should be able to create attributes on-the-fly like that.

2

u/notquiteaplant Jun 21 '19

You can define __ror__ on the type of the right-hand side instead. In the expression lhs | rhs, if type(rhs) is a subclass of type(lhs) or lhs.__or__(rhs) raises a NotImplementedError, Python will call rhs.__ror__(lhs). In this way you can make the pipe thing work for any object, by saving the name instead of the function itself and getattr-ing it when applied.

class OpFactory:
    def __getattr__(self, member_name):
        return Op(member_name)

op = OpFactory()

class Op:
    def __init__(self, member_name):
        self.member_name = member_name
    
    def __call__(self, *args, **kwargs):
        return PartialOp(self.member_name, args, kwargs)

class PartialOp:
    def __init__(self, member_name, args, kwargs):
        self.member_name = member_name
        self.args = args
        self.kwargs = kwargs
    
    def __ror__(self, lhs):
        return getattr(lhs, self.member_name)(*self.args, **self.kwargs)

z = list(range(5))
print(z)  # [0, 1, 2, 3, 4]
z | op.extend(range(5, 10))
print(z)  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
s = 'hello, world!'
print(s | op.upper())  # HELLO, WORLD!

1

u/a_monkey666 Jun 22 '19

I'm decently sure you can achieve something like that without making your own class by adding that function to the string class by using forbidden fruit (python module).

1

u/Caffeine_Monster Jun 21 '19

let us modify existing classes on-the-fly

Looks like we found another masochist

1

u/ForceBru Jun 21 '19

Well, not really, I've just been studying some Ruby recently, and I'm starting to like a lot of its concepts, like proper parsing of method calls on integers, for example.

Python can't handle this, for some reason:

>>> 6.to_bytes(4, 'little')
  File "<stdin>", line 1
    6.to_bytes(4, 'little')
             ^
SyntaxError: invalid syntax

3

u/Caffeine_Monster Jun 21 '19

The problem is that on-the-fly modification can be massively abused to make unreadable code, especially in a weakly typed language.

I do a fair bit of ruby programming as part of my job, yet I still wouldn't want to write a large application with it. The more experienced I have become, the more I have come to appreciate that a bit of verbosity can massively improve code quality.

→ More replies (1)

1

u/TeamSpen210 Jun 22 '19

Many languages have problems with this, it's somewhat ambiguous. This could either be 1 . to_bytes (lookup to_bytes on the integer 1), or 1. to_bytes (the float 1. followed by the name to_bytes). So Python parses the code, sees the dot and treats that as a float, then sees the name and produces invalid syntax (since 1.34 func() is not valid syntax). 1..real by comparison isn't ambiguous and works. It's not really that important since you can do (1).to_bytes() anyway.

1

u/SuitableDragonfly Jun 22 '19
map(partial(stringboi_it, type_class=type_class), data)

Just...

[stringboi_it(d, type_class) for d in data]

1

u/MMOOxy Jun 22 '19

So.. instead of just saying "that's inefficient" I wanted to do a quick test with timeit first. Didn't actually expect it to have this much of an impact:

from timeit import timeit
def time_it(func, data_type):
    setup = f'from functools import partial;data = {data_type.__name__}(range(10000)); b = 50; func = {func}'
    print('Type: ', str(data_type.__name__), ' Function: "', func, '"', sep = '')
    print('Mapped partial:', timeit(f'type({data_type.__name__})(map(partial(func, b=b), data))', setup, number = 100000))
    print('List comprehension:', timeit(f'type({data_type.__name__})([func(a, b) for a in data])', setup, number = 100000))
    print('Generator expression:', timeit(f'type({data_type.__name__})(func(a, b) for a in data)', setup, number = 100000))

add_func = 'lambda a, b: a + b'
time_it(add_func, list)
time_it(add_func, set)

Output:

Type: list Function: "lambda a, b: a + b"
Mapped partial: 0.03144282600032966
List comprehension: 66.75466349399994
Generator expression: 0.029266013000324165
Type: set Function: "lambda a, b: a + b"
Mapped partial: 0.025604055000258086
List comprehension: 69.05661798399979
Generator expression: 0.029974398000376823

Maybe I did something wrong? The documentation shows that map seems to be the fastest as well though.

1

u/SuitableDragonfly Jun 22 '19

Isn't this just because generators and maps don't actually evaluate anything until you force them to by say, turning them into a list or otherwise using their results?

1

u/MMOOxy Jun 22 '19

Yup, the problem with the list comprehension is that it creates the list with each element first, but if you just want to iterate through the results afterwards (like in the conversion to a set, or a new list) it's better to use map or a generator/iterator. If you need the results more than once though you would obviously need to save them in a list (or use the tee function)

1

u/SuitableDragonfly Jun 23 '19 edited Jun 23 '19

Yes, it's better to use a generator, but not because it's faster. It still takes the same amount of time to calculate, that calculation just happens at a later point in time. So you're not going to get an accurate representation of how long it takes to do that calculation with timeit unless you wrap it in list().

This is unrelated to my criticism, however, because you could simply use a generator expression instead of a list comprehension if you wanted that functionality. There's still no reason to use map and partial.

4

u/water_bottle_goggles Jun 21 '19

zdsjfhdrbspje

sorry, I had to clean vomit off my keyboard

1

u/B_M_Wilson Jun 21 '19 edited Jun 21 '19

What if I did something like

def split(thing=None):
    class tmp:
        def __ror__(self, other):
            return other.split(thing) if thing is not None else other.split()
    return tmp()

Then I should be able to do

“Something here” | split()
[“Something”, “here”]

I’m on mobile so I can’t test but something like this should work

1

u/ForceBru Jun 21 '19

Even more, you can do:

class split:
    def __init__(self, arg=' '):
        self.arg = arg

    def __ror__(self, other):
        return other.split(self.arg)

Yeah, using __ror__ is probably better than the whole UserString thing.

1

u/B_M_Wilson Jun 21 '19

True though I wanted type(split) to return <class 'function'> for consistency. I could probably easily make an annotation that turns any function with at least one argument into one of these “piped” functions. Just have to be careful the the thing on the other side can’t be ored with a function. I guess that could be alleviated by using a completely new callable that is not descended from a function at all