#internals-and-peps

1 messages · Page 66 of 1

grave jolt
#

!e

n = (lambda r:r.__code__.co_consts[bool(r.__code__.co_consts[0])])(lambda:r)
print(n)
fallen slateBOT
#

@grave jolt :white_check_mark: Your eval job has completed with return code 0.

None
grave jolt
#

or rather

#

!e

print( (lambda r:r(r))(lambda r: r.__code__.co_consts[0]) )
fallen slateBOT
#

@grave jolt :white_check_mark: Your eval job has completed with return code 0.

None
hexed maple
#

can someone give me some beginner tutorials for asyncio

narrow kettle
hexed maple
#

isn't asyncio advance ?

narrow kettle
#

no not at all

hexed maple
#

what really 👀

grave jolt
#

@hexed maple Please read the channel description.

#

This is not a channel for getting help on topics you find advanced, it's a channel for discussing python itself

hexed maple
#

well fine
but u saying " topics you find 'advanced' " is wrong

narrow kettle
#

? async is not an advanced topic, its very simple tbh

hexed maple
#

simple is relative

grave jolt
#

#async-and-concurrency is for discussing concurrent programming, including asyncio. The purpose of this channel is stated in its description.

narrow kettle
#

i posted a link

unkempt rock
#

So, I'm planning on setting up continuous deployment for a raspberry Pi from a GitHub repo. What would be the best way to accomplish this?
I've heard that I should use Webhooks, but I'm not sure if this is the best solution

#

Im planning on running a webserver, discord bots, and any other things I happen to develop

narrow kettle
#

i use github actions and a self hosted runner, thats imo the easiest way

unkempt rock
#

how does your runner work? @narrow kettle

narrow kettle
#

aaaand you spammed in multiple channels

unkempt rock
#

oh yeah, sorry :P

slim island
#

This question doesn't really relate to python which is the main thing for this channel

unkempt rock
gloomy rain
#

@unkempt rock This channel is for indepth discussion of the Python language. If you need help, please check out #❓|how-to-get-help.

fallen slateBOT
#

:incoming_envelope: :ok_hand: applied mute to @unkempt rock until 2020-08-18 15:28 (9 minutes and 59 seconds) (reason: duplicates rule: sent 4 duplicated messages in 10s).

red solar
#

What are complex functions in python? (Or is that question about maths?)

unkempt rock
#

i would say splice/slice is one. as what is complex can determine on the user

#

or did u mean the complex function?

#

as well as lambda

#

and recursion

slim island
#

A complex function is a function from complex numbers to complex numbers. In other words, it is a function that has a subset of the complex numbers as a domain and the complex numbers as a codomain
That's the definition Google gives - I don't see why that wouldn't also fit with Python

brazen jacinth
#

Its definitely math oriented

spark magnet
#

i would say the original question is ambiguous

pseudo cradle
#

Yeah. In math complex refers to functions with imaginary components. Without a mathematical context it just means something with multiple parts/aspects

paper echo
#

this is why i use the word "complicated" when i mean "complicated", and "complex" when i mean "has real and imaginary components"

#

the word "complex" is heavily overloaded in english anyway, depending on pronunciation

grave jolt
#

Well,

#

!zen complex is better than complicated

fallen slateBOT
#
The Zen of Python (line 3):

Complex is better than complicated.

grave jolt
#

it's complicated

paper echo
#

as if i needed even more reasons to disregard the zen of python

paper echo
#

!e ```python
def as_closing_contextmanager(cls):
def enter(self):
return self
def exit(self, *exc_info):
self.close()
enter.qualname = f'{cls.name}.{enter.qualname}'
exit.qualname = f'{cls.name}.{exit.qualname}'
cls.enter = enter
cls.exit = exit
return cls

@as_closing_contextmanager
class Thing:
def close(self):
print('Goodbye!')

thing = Thing()
with thing:
print('Entered.')

fallen slateBOT
#

@paper echo :white_check_mark: Your eval job has completed with return code 0.

001 | Entered.
002 | Goodbye!
paper echo
#

is this stupid or useful

pseudo cradle
#

It's stupid useful

#

Honestly looks legit

paper echo
#

hmm. maybe it just makes more sense to use contextlib.closing at call time though

#

its a fun parlor trick

#

!e ```python
class with_exit_hooks:
def init(self, hooks):
self.hooks = hooks
def call(outer_self, cls):
def enter(self):
return self
def exit(self, *exc_info):
for hookname in outer_self.hooks:
getattr(self, hookname)()
enter.qualname = f'{cls.name}.{enter.qualname}'
exit.qualname = f'{cls.name}.{exit.qualname}'
cls.enter = enter
cls.exit = exit
return cls

@with_exit_hooks(['close1', 'close2'])
class Thing:
def close1(self):
print('Goodbye!')
def close2(self):
print('Good riddance.')

thing = Thing()
with thing:
print('Entered.')

fallen slateBOT
#

@paper echo :white_check_mark: Your eval job has completed with return code 0.

001 | Entered.
002 | Goodbye!
003 | Good riddance.
paper echo
#

@pseudo cradle you have my permission to use it 😛 but not to blame me if it breaks...

#

i hate the whole thing with manually modifying qualname

#

i know its a simple task but i wish there was something in the stdlib for it

fervent pulsar
#

can I get some advice. Is this a good way to code a bubble sort ```l1 = [6,1,3,2,7,9,10,5,4,8]

def bubble(sorted_list):
for i in range(len(l1)-1):
swap = False
for i in range(len(l1)-1):
if sorted_list[i] > sorted_list[i+1]:
sorted_list[i], sorted_list[i+1] = sorted_list[i+1], sorted_list[i]
swap = True
if swap == False:
return sorted_list
return sorted_list

print(bubble(l1))```

feral cedar
#

no it's not

#

you check every single element against every other element, O(n^2) time

fervent pulsar
#

meaning don't break out?

grave jolt
#

@feral cedar Well, it's a bubble sort, so that's unavoidable.

feral cedar
#

yeah but you can get a better O(n^2)

fervent pulsar
#

I'm just starting a course so i like to try and figure out the answers before I watch the video

feral cedar
#

by not checking elements that are in the right spot

fervent pulsar
#

oh its slow?

grave jolt
feral cedar
#

yeah

#

also fix error is right, i can help you in either of those

fervent pulsar
#

okay thank you

pseudo cradle
#

True

paper echo
#
class Foo(AbstractContextManager, AsyncAbstractContextManager):
    close_callbacks: Sequence[Callable[[], Any]]
    aclose_callbacks: Sequence[Union[Awaitable[Any], Callable[[], Awaitable[Any]]]]

    def close(self):
        for callback in self.close_callbacks:
            callback()

    async def aclose(self):
        for callback in self.aclose_callbacks:
            if inspect.iscoroutinefunction(callback)
                await callback()
            elif inspect.isawaitable(callback):
                await callback
            else:
                callback()

    # Use the default __enter__ and __aenter__ provided by AbstractContextManager and AbstractAsyncContextManager

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    async def __aexit__(self, exc_type, exc_value, traceback):
        await self.aclose()

how about this... is this a bad idea?

#

the idea is that the Foo might contain some kind of data stream or other handle to a resource that needs to be closed properly

#

so the caller can attach one of these callbacks to the Foo instance if required

#
response = requests.get('https://example.net/api', stream=True)
foo = Foo()
foo.data_stream = response.iter_lines(decode_unicode=True)
foo.close_callbacks = [response.close]
with foo:
    foo.do_something_important()
# response.close() needs to be called here
boreal umbra
#

I know I asked about this already but did we decide that there is a decorator that returns a given value if the function raises a given error? If not I'll implement it but even then I'm not sure what I'd call it.

#

and then no one would find it on pypi and the problem would continue

pseudo cradle
#

deco erru

#

decoretuerru

boreal umbra
#

why erru?

pseudo cradle
#

sounds fancy

#

I like to have bits of whimsy in my life

boreal umbra
#

I mean the name of the library is important for people to be able to find it. I say this as the one making a library called bratlib

pseudo cradle
#

Please tell me that's a machine learning library that cooks the perfect bratwurst

boreal umbra
#

nope, it's a library for reading brat files

#

brat stands for brat rapid annotation tool

#

I don't really want to make a utils.py file to implement this one decorator

pseudo cradle
#

huh interesting

boreal umbra
#

but the other option is to make a repo for this one decorator and make it a new dependency

peak spoke
#

What is the use case? Exceptions have a different meaning from normal values

boreal umbra
#
    def __lt__(self, other):
        with suppress(AttributeError):
            return self.entity < other.entity
        return NotImplemented
#

I have this all over the place

#

would rather have

#
@exceptreturn(AttributeError, NotImplemented)
def __lt__(self, other):
    return self.entity < other.entity
pseudo cradle
#

Petition to add it to stdlib

boreal umbra
#

I doubt they'd go for it since the cases where you want all erroneous calls to return the exact same object are rare.

#

except, of course, comparison operator implementations

#

also, regarding decorators themselves

#

is the function being decorated an implicit first argument for the actual decorator callable?

peak spoke
#

The decorator is implicity called with the function

#

(only with the function)

boreal umbra
#
exceptnotimp = exceptreturn(AttributeError, NotImplemented)

@exceptnotimp
def __lt__(...):
    ...
#

this would be even more dry

peak spoke
#

think they feel the same readability wise, maybe with kwargs

boreal umbra
#
def except_return(alternative, *exceptions):

    def wrap(func):

        def _wrap(*args, **kwargs):
            try:
                return func(*args, **kwargs)
            except exceptions:
                return alternative

        return _wrap

    return wrap
#

feels weird being three defs deep

paper echo
#

@boreal umbra you can use a callable class instead

class except_return:
    def __init__(self, alternative, *exc_types):
        self.alternative = alternative
        self.exceptions = exc_types

    def __call__(self, fn):
        @wraps(fn)
        def _fn(*args, **kwargs):
            try:
                return fn(*args, **kwargs)
            except self.exc_types:
                return self.alternative
        return _fn
boreal umbra
#

@paper echo oh cool I'll try that

#

also I had my first lecture for my data science course today

#

are you proud of me? lemon_hyperpleased

paper echo
#

nice

#

looks like you have already mastered the art of yak shaving, so you'll do great 😛

wild harness
#

python allows you to use only one CPU thread at a time right?

#

like multi-threading is not really a thing on python

peak spoke
#

Threads running python code will only actively be able to use one core, but you can utilize multiprocessing.

wild harness
#

yeah

#

so why do we need thread.acquire() and thread.release()

#

if they will use only one core

#

ohh

#

ok, so they will use only one core but might function asynchronously

peak spoke
#

Because they're still threads that can switch on arbitrary instructions

wild harness
#

like different threads might interleave

#

but isn't threading traditionally single core only?

#

doesn"t multi_threading refer to using multiple threads on only one CPU core

peak spoke
#

different threads, os threads can be seen as processes that share their memory with the parent thread, cpu threads are the actual things running in parallel which are seen as cores by the os

wild harness
#

i see

#

but

#

in reality the processes are actually being paused and resumed on the same thread right

#

its not 'true parallelism' ?

#

idk

#

what is a use case for multi-threading?

#

people keep saying use it for I/O Bound tasks but i dont get it

peak spoke
#

Leaning bit out of topic for this channel, the CPU threads are true parallelism, so you can't actually run more than that at a point in time on the CPU. OS threads are scheduled by the OS to be executed by priority etc. since cpus are fast that can also be seen as parallel from the outside. In python however you get a lock for threads that must be acquired by threads to do most operations so usually you only have one os thread running your code.
You can use them for IO because the thread can just pause its execution while it's waiting for a resource to become available. Python also provides async coroutines which run in one thread and are managed by an event loop to run concurrently, where the event loop gets control at points you allow it to, to simplify the code flow a bit and prevent most race conditions that can come from threading

wild harness
#

i see

#

i understood a bit, but not entirely

#

might go back and review some basic stuff about threads

pseudo cradle
#

@boreal umbra Attending a lecture or giving one?

boreal umbra
#

@pseudo cradle attending lol

pseudo cradle
#

Oh, I was about to be super impressed

boreal umbra
#

being a helper on this server is the closest I've come to having a teaching position

pseudo cradle
#

Fair enough

boreal umbra
#

that being said my department was starting a Python class at one point and I demanded that they make me the instructor

#

they did not make me the instructor.

lean cliff
#

@boreal umbra

#

why did you put @

#

@tranquil roostps.(fn)

boreal umbra
#

you're asking why @wraps(fn) is in that code? Because I didn't do that, salt rock lamp did.

#

though I also don't know why I do most of the things that I do.

tired epoch
#

they did not make me the instructor.
@boreal umbra politics ?🙃

boreal umbra
#

what about politics?

#

I'm an undergraduate so from their perspective the idea of making me an instructor for anything is laughable.

tired epoch
#

well maybe u can be instructor for extra help classes

feral cedar
#

that does make sense

boreal umbra
#

that being said I doubt any of the professors in my department who don't do AI or data science actually use Python, so I'm probably more qualified to teach it than the person they actually picked.

#

I lay awake at night worried she used camel case and for i in range(len(...)): even though they only offered the class for one semester a year and a half ago.

tired epoch
#

y dont u ask them for skill test so its fair for everyone

boreal umbra
#

everything is wrong with it

#

I just used that and camel case as examples of shibboleths of C/Java programmers teaching Python.

raven ridge
#

Using a loop over indexes unnecessarily is definitely a sign of not knowing Python well

boreal umbra
#

There are of course limited cases where you want the indices and the indices only

#

well, I can only think of one, but who knows what others there might be

#

one of my coworkers at my cafe job is pursuing a CS-like degree at another institution and he had to learn Python for one class. He said that he hated how for loops are for-each loops.

#

I don't really get that objection.

raven ridge
#

I think that's an "I don't use the language enough to remember its syntax" problem.

wild harness
#

when do you use pool in mp

boreal umbra
#

@raven ridge he also said that he felt like when he wrote in Python, he "didn't feel like [he] was programming"

#

since the language is slightly more abstract than Java, I guess.

#

I only say "slightly" because I can't really think of an area where Python is more abstract other than that variables are untyped

#

that doesn't make typing go away, it just means that you're not allocating specific amounts of memory when you create a variable. I think.

raven ridge
#

@raven ridge he also said that he felt like when he wrote in Python, he "didn't feel like [he] was programming"
@boreal umbra That's code for "it's not real programming unless it's hard to read".

boreal umbra
#

I agree

raven ridge
#

Show him the internals of a major library, let him be impressed by how complicated it gets, heh

boreal umbra
#

his wife is immunocompromised so I haven't seen him since like February

#

at one point he took a class in embedded C and told me that he didn't even have access to malloc and free

#

and he said that was his favorite programming that he's done

#

so shrug

raven ridge
#

Programming in resource constrained environments can be fun, but so is actually building novel or useful things, which is way easier in a high level language with rich libraries.

wild harness
#

python object oriented paradigm is so nasty honestly

#

makes me barf

feral cedar
#

is it not the same as other languages oop?

pseudo cradle
#

Programming in resource constrained environments is like a videogame tbh

#

Beat level X with only Y this in Z time!

boreal umbra
#

Programming in resource constrained environments can be fun, but so is actually building novel or useful things, which is way easier in a high level language with rich libraries.
@raven ridge wow shots fired af

gleaming rover
#

novel in different ways, I think?

boreal umbra
#

python object oriented paradigm is so nasty honestly
@wild harness what do you dislike about it?

gleaming rover
#

I personally found emulating object orientation in C pretty fun

wild harness
#

self

gleaming rover
#

what about self

wild harness
#

and __new__

#

and __init__

#

i mean there's nothing wrong about it, but it isn't nearly as direct as classes in C++

boreal umbra
#

well, part of the point of self is that you can actually give any function to a class

wild harness
#

i know

teal yacht
#

I mean, other than having a separate initializer and constructor, these are extremely standard in oop

wild harness
#

i'm talking more about the syntax i guess

#

of how classes are defined and work

#

also no private variables (?)

boreal umbra
#

not having private variables gives you more flexibility

teal yacht
#

not really, unless by that you mean that it lets you shoot yourself in the foot

feral cedar
#

being able to make variables private lets you have more control over the state

boreal umbra
#

I think that's strawmanning the concept of flexibility

teal yacht
#

how tho ? it's exposing internals to the user

#

internals which you really aren't supposed to ever touch

boreal umbra
#

you can already indicate which internals you don't think they should mess with. if they want to figure out what it's for and have a use case that leverages that, why not let them?

wild harness
#

you can already indicate which internals you don't think they should mess with. if they want to figure out what it's for and have a use case that leverages that, why not let them?
@boreal umbra how do you indicate?

boreal umbra
#

a leading underscore

teal yacht
#

because I think programmers should be treated as small children trying to put forks in outlets

wild harness
#

yeah but that's weak

gleaming rover
#

honestly I can see plus points for both approaches

feral cedar
#

both work pretty well

wild harness
#

also the fact that you can define class variables from outside the class constructor in pytho

boreal umbra
#

you don't have to trust people if that's not your prerogative, but I wouldn't want that baked into the language.

gleaming rover
#

internals which you really aren't supposed to ever touch
@teal yacht but you might have a reason to; that's the point

wild harness
#

you don't have to trust people if that's not your prerogative, but I wouldn't want that baked into the language.
@boreal umbra that's why you have public

gleaming rover
#

so mark them as non-public and be done with it

teal yacht
#

@teal yacht but you might have a reason to; that's the point
@gleaming rover I'd say it's more common for that reason to be bad than not

wild harness
#

ya

#

i agree

gleaming rover
#

in terms of frequency, probably.

wild harness
#

but i also agree there is merit to both public and private which is why you should have pubic and private so if you want public you can just keep everythin gpublic

gleaming rover
#

but well...it is consonant with the design philosophy of the language

raven ridge
#

mocking is much easier in Python than in any other language I've ever used - the duck typing lets you trivially substitute in a mock without needing to define an interface or subclass from the concrete type or anything, and the lack of privates means that your tests are able to patch private instance variables.

gleaming rover
#

which lends itself well to screwing around with lots of fun stuff

#

so I'm cool with it

teal yacht
#

Like, I really don't think the whole PEP8 idea that "programmers are consenting adults, let them be" is healthy in any way, we have simple ways to prevent errors in programming for close to 0 cost, so why not use these

gleaming rover
#

that's what code inspection is for

#

it is kind of a convention over configuration thing

boreal umbra
#

@teal yacht it's not at zero cost though

gleaming rover
#

but if you follow "leading underscore for non-public", you can tell @ compile time

teal yacht
#

the vast majority of languages do completely fine with encapsulation, it's p much close to 0 cost

raven ridge
#

If Python gave privates in the way that C++ or Java does, I wouldn't use it. If it allowed enforcing that a single leading underscore makes something package private and gave a warning when it was accessed by something outside the package - I could get behind that...

boreal umbra
#

why would you need a warning though?

gleaming rover
#

the vast majority of languages do completely fine with encapsulation, it's p much close to 0 cost
@teal yacht the vast majority of languages are not as unashamedly dynamic and free of restrictions as Python

#

If Python gave privates in the way that C++ or Java does, I wouldn't use it. If it allowed enforcing that a single leading underscore makes something package private and gave a warning when it was accessed by something outside the package - I could get behind that...
@raven ridge isn't that what third-party tools are for

#

like why do you want a runtime warning

#

the only case in which that would trigger when a compile-time one wouldn't is dynamic attribute access, I suppose

raven ridge
#

why would you need a warning though?
@boreal umbra to tell people who may not realize the conventions that they're operating out of contract and there are no guarantees about the behavior because of it.

gleaming rover
#

sounds like unneeded overhead

boreal umbra
#

I guess wanting to educate people about the convention is fair but I think it's a pretty widely-known convention

gleaming rover
#

that's basically an additional check on each attribute access?

raven ridge
#

yeah - probably not worth the extra cost.

boreal umbra
#

I've actually been thinking that it might be cool to have an alternative Python implementation for learning

gleaming rover
#

alternative how

raven ridge
#

the only time I ever really wish I had access to private attributes in Python is when dealing with C extensions. I often want to do only the necessary part in C and do everything that could be done in Python in Python, by wrapping extension types in Python objects that help manage them. But, that often means that the extension type is fragile, and could crash the interpreter if misused - I'd be nice to have the ability to enforce more safety around that, even if that was a relatively expensive runtime check.

gleaming rover
#

the only time I ever really wish I had access to private attributes in Python is when dealing with C extensions. I often want to do only the necessary part in C and do everything that could be done in Python in Python, by wrapping extension types in Python objects that help manage them. But, that often means that the extension type is fragile, and could crash the interpreter if misused - I'd be nice to have the ability to enforce more safety around that, even if that was a relatively expensive runtime check.
@raven ridge yeah, I feel the same way too

boreal umbra
#

I'll take a look at that. But someone once suggested to me that cpython intentionally throttle poorly written code to penalize people who write it.

gleaming rover
#

that sounds like a pretty good joke

#

"why is my code so much slower than yours?"

boreal umbra
#

and an implementation that tells you when code could be written differently would be more ideal

gleaming rover
#

"you used for i in range(len(iterable)) instead of for element in iterable when you didn't need the index"

boreal umbra
#

I really hate range(len(...)) so I'd support a Python implementation that installs malware on your computer if you use it.

pallid meteor
#

Just for i, _ in enumerate(...)

#

The truly enlightened way

gleaming rover
#

why not for i in ([id(obj) for obj in iterable].index(id(obj)) for obj in iterable)

patch 0.1: changed outer list to generator expression

brave badger
#

oh no

topaz horizon
#

ok thankyou

boreal umbra
#

I'm sure styx already knows this, but for i, _ in enumerate(...) is better in part because not all iterables have a length, but that syntax is guaranteed to work.

topaz horizon
#

uhm... I can't get you..

gleaming rover
#

I'm sure styx already knows this, but for i, _ in enumerate(...) is better in part because not all iterables have a length, but that syntax is guaranteed to work.
@boreal umbra I disagree

pallid meteor
#

Tbh i just really hate how range(len( looks, but TIL

gleaming rover
#
class I:
    def __iter__(self):
        raise TypeError
        
    def __getitem__(self):
        return 0
    
    def __len__(self):
        return 1
#

clearly range(len(I()) works and enumerate(I()) does not

#

let us agree on the former as the one true option

boreal umbra
#

wow

pallid meteor
#

Hold up thats illegal

boreal umbra
#

I just got owned

gleaming rover
#

as a wise man once said, the best kind of right is technically right

boreal umbra
#

though goes __getitem__ work when the signature doesn't allow for a value?

charred wagon
#

As far as I'm concerned that's not an iterable lemon_grumpy

gleaming rover
#

oh wups I forgot my bad

brave badger
#

How about making every Sequence in Python lazy so range(len(...)) is not viable

boreal umbra
#

what is Sequence?

gleaming rover
#

strings, unfortunately

#

🥴

teal yacht
#

a Reversible Collection with __getitem__

brave badger
#

Enforce the iterator protocol for everything hyperlemon

teal yacht
#

I can't wait for python5

gleaming rover
#

okay one thing I would REALLY like

boreal umbra
#

all my dataclasses have to be iterators?

#

god damn it

gleaming rover
#

immutable data

#

(can anyone weigh in on the optimisation possibilities?)

brave badger
#

Persistent data structures 👀

gleaming rover
#

like

#

namedtuples without being namedtuples

#

okay, something a bit more practical

#

would anyone like to see Mapping destructuring?

teal yacht
#

You can have persistent DSs in python

gleaming rover
#

e.g.:

>>> d = {'a': 1, 'b': 2}
>>> {a, b} = d
>>> a
1
>>> b
2
teal yacht
#

see pyrsistent

gleaming rover
#

it's like one of the 2-3 things I treasure about JS over Python (syntactically)

brave badger
#

Speaking of destructuring, were there any criticisms about implementing the __match__ protocol for PEP 622?

teal yacht
#

yes, the protocol has been removed

brave badger
#

Oh wow

pseudo cradle
#

Nice

teal yacht
#

unless future changes, it won't be part of the pep

brave badger
#

Guess I'll have to reread the spec again

boreal umbra
#

hopefully they'll remove everything from the pep

teal yacht
#

It's much better without __match__

wild harness
#

why is multiprocessing slow with more num_workers

gleaming rover
#

hopefully logging will become snake case in Python 4

#

overhead of serialisation/deserialisation...?

brave badger
#

lemon_fingerguns_shades

import logging
logging.get_logger = logging.getLogger
boreal umbra
#

@wild harness depends on a lot of things but managing more cores requires more work

brave badger
#

I jest

wild harness
#

@wild harness depends on a lot of things but managing more cores requires more work
@boreal umbra shouldn't multiprocessing be faster?

raven ridge
#

would anyone like to see Mapping destructuring?
@gleaming rover I'd be much more into this than I am into the match protocol...

gleaming rover
#

@gleaming rover I'd be much more into this than I am into the match protocol...
@raven ridge RIGHT?

pseudo cradle
#

I would not mind mapping destructuring either

#

That's three people four, we can update the lang now, right

#

I'll leave the typo in

boreal umbra
#

multiprocessing isn't guaranteed to be faster.

raven ridge
#

yes, the protocol has been removed
The protocol was very badly broken. The initial version of the PEP missed that it would be impossible to ever add __match_args__ to a class that wasn't originally defined as having one, because it would be a backwards incompatible change.

teal yacht
#

but unlike JS, we don't use dicts everywhere

gleaming rover
#

it's useful in web dev

pseudo cradle
#

What's wrong with just breaking everything

#

jsons everywhere

raven ridge
#

the original proposal for the match protocol was that if a class Foo didn't have __match_args__, then Foo(x) would match any instance of Foo and assign it to x, but if it did have __match_args__ that would match its first argument against x

#

meaning you'd never be able to add __match_args__ to a library class that has been released, since you wouldn't know if they were already doing a case Foo(x) somewhere.

pseudo cradle
#

Ah

boreal umbra
#

Would case become a new key word or what?

teal yacht
#

yes, but a soft one

#

as in you can do still do match = 3 or case = 3

boreal umbra
#

Interesting

#

Are there other soft keywords?

#

I'm trying to think of one but I'm tired.

raven ridge
#

async was one, but isn't anymore...

teal yacht
#

nope, it's the first introduced since the new parser

#

async definitely wasn't a soft keyword

#

it broke half the python ecosystem

boreal umbra
#

Is async now a full keyword?

raven ridge
#

it started as a soft keyword and became a full keyword, right?

teal yacht
#

maybe actually, I just remember python3.5 wrecking havoc

boreal umbra
#

I've never played with async but I thought they were full kehwords if you import asyncio, and are otherwise not keywords at all.

raven ridge
boreal umbra
#

Wow

#

That's not backwards compatible

#

!!!

teal yacht
#

ah wait no you're right, now i remember telling people to downgrade to 3.5

raven ridge
#

indeed. the worst break I can remember, other than the obvious 2-to-3 one.

teal yacht
#

with discord py

#

we should have made a macro for this, it was asked at least 20 times a day

boreal umbra
#

Now that one guy who uses async as a variable has to change that.

raven ridge
#

it was a pretty common parameter name, unfortunately... async=True to request an operation be done in a non-blocking manner.

teal yacht
#

watch code break in 5 years when typing removes all builtins

boreal umbra
#

Can't they just keep it in there?

teal yacht
#

they could but they won't

boreal umbra
#

Did they announce that already?

raven ridge
#

we'll see. who knows who'll be running the show in 5 years.

boreal umbra
#

Not Guido

teal yacht
#

The deprecated functionality will be removed from the typing module in the first Python version released 5 years after the release of Python 3.9.0.

boreal umbra
#

Interesting

#

Guess I'll have to be ready in five years

#

I use type hints a lot for anything that isn't a one off.

raven ridge
#

that one's a much easier change to make, honestly.

teal yacht
#

yeah it should be fine

raven ridge
#

async was tough because people were using it as parameter names, which means it's part of the public API.

#

since any parameter name can be passed by kwarg.

boreal umbra
#

I came on the scene right before 3.8

#

Shrug

raven ridge
#

my first Python version was probably 2.3, plus or minus 0.1...

boreal umbra
#

Wow

#

You belong in a python user museum

#

I have a vague idea of what got implemented from 3.5 onwards because of what versions my uni uses where

raven ridge
#

I learned it a bit in high school - never got too much past the writing simple games stage. Probably never anything more advanced than hangman. 🙂

#

came back to it much later to learn it as an actual programming language, once I had a few others under my belt.

boreal umbra
#

Actually we were stuck with 3.4 for a while

pseudo cradle
#

Yeah, I did python as a one off a while back, then came back to it in earnest after I bullshitted my way into a python job

half wolf
#

I think 2.6 was the latest when I started my professional python career

#

We might have been on 2.5 or 2.4 at work though. We're better at keeping pace in my team now :)

paper echo
#

I never actually used python 2 professionally

#

I learned 3 in school and my first data science job was using 3.4 in production

gleaming rover
#

wow

#

I started with 3.5 or 3.6, I think

#

I don't think I have ever used a print statement outside of legacy code

paper echo
#

i never have either fwiw

#

i think i even used 3.3 in school

#

in hindsight it was actually very bold of my prof to start us on 3.3 and not 2.7

#

but also very wise

#

he was an excellent teacher

#

he drew lots of pictures on the whiteboard and encouraged students who had laptops to open idle and follow along with his lecture

#

i never had any confusion about scoping or lists or mutation like a lot of beginners who ask questions here seem to, he did a great job teaching us the language

paper echo
#

if i monkey patch a class with a new method, is it possible to also patch the class's annotations to reflect the new method?

#

wait.. classes dont even have __annotations__

#

how does that even work?

#
class Thing:
    x: int
    y: float

where do these annotations get stored, and how do you access/modify them?

#

ah, __annotations__ only gets created if the class actually has annotations, otherwise the attribute is missing entirely

#

but functions arent included in that

#

so seems like there's no additional patching required

deft pagoda
#

functions have their own __annotations__, i assume same for methods

cloud crypt
#

methods are functions in a class wrapped into MethodType, so yeah

mortal hare
unkempt rock
#

@mortal hare use machine learning to teach it about your house and bring you beer

feral cedar
#

might not have enough data in a lifetime to do that

pseudo cradle
#

It delivers beer to a location within a 10 mile radius of you with 75% accuracy

feral cedar
#

"fetch me a beer"
robot: leaves house and travels 9 miles east

narrow kettle
#

If you have an if True: will cpython optimize that away

pseudo cradle
#

What if you have an if False:

narrow kettle
#

Either one

#

That’s a pretty trivial optimization that most other Lang’s will do

#

Does cpython do it

spice pecan
#

It does optimize it away

#
>>> def func():
...     if True:
...             print('yes')
...     if False:
...             print('no')
...     else:
...             print('maybe')
... 
>>> dis(func)
  3           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('yes')
              4 CALL_FUNCTION            1
              6 POP_TOP

  7           8 LOAD_GLOBAL              0 (print)
             10 LOAD_CONST               2 ('maybe')
             12 CALL_FUNCTION            1
             14 POP_TOP
             16 LOAD_CONST               0 (None)
             18 RETURN_VALUE```
narrow kettle
#

Gotcha, i figured it would

spice pecan
#

same goes for other literals

#

and expressions that can be pre-calculated, like 10 % 2

mortal hare
#

@mortal hare use machine learning to teach it about your house and bring you beer
@unkempt rock It already can do that

#

but without ml

spice pecan
#

although storing them in a name removes the optimization immediately

>>> def func():
...     x = 10  # Advanced stuff, can't take guesses anymore
...     if x:
...         print('yes')
... 
>>> dis(func)
  2           0 LOAD_CONST               1 (10)
              2 STORE_FAST               0 (x)

  3           4 LOAD_FAST                0 (x)
              6 POP_JUMP_IF_FALSE       16

  4           8 LOAD_GLOBAL              0 (print)
             10 LOAD_CONST               2 ('yes')
             12 CALL_FUNCTION            1
             14 POP_TOP
        >>   16 LOAD_CONST               0 (None)
             18 RETURN_VALUE```
idle isle
#

Could anyone help me out? im trying to make a menu but struggling a bit with userinput

spice pecan
#

This isn't the right channel for that

elder frigate
#

Ok thanks

long forge
#

Could anyone help me out? im trying to make a menu but struggling a bit with userinput
@idle isle Did you get it?

rigid nimbus
#

Does anyone know what the code for this would need to be??
Implement a function that accepts an string of 10 integers.

Have the function return those numbers as a String in the form of a phone number.

Use the following format for the phone number:
(XXX) XXX-XXXX

feral cedar
gleaming rover
#

when using classes as decorators, do you use camel case or snake case?

teal yacht
#

still pascal case

gleaming rover
#

any particular reasoning

feral cedar
#

looks better, probably

north root
#

you would usually instantiate the class before using it as a decorator anyway

gleaming rover
#

I'm using __init__ for parametrisation

teal yacht
#

It's still a class, I prefer to use case for denoting the implementation, not the use

raven ridge
#

the standard library uses snake case for decorators that are classes.

#

wait, that one actually is a function... let me find a different example...

north root
#

a lot of the stdlib has the wrong naming convention

paper echo
#

i use snake case for class-based decorators

#

because they "act like" functions

zenith topaz
#

A lot of the stdlib used to be 3rd party libs, and remained their own style when they joined the stdlib instead of turning everything into pep8.

raven ridge
#

int, dict, str, etc are all lowercase, too.

teal yacht
#

map, range, enumerate etc are also types

raven ridge
#

because they "act like" functions
@paper echo This is what I go with, too. If it acts like a function, and is meant to be used like a function, but happens to be implemented as a class, I use snake case.

#

implementation details shouldn't drive the name 🙂

#

the purpose of the naming conventions is to help readers understand the code better, and calling it Enumerate instead of enumerate wouldn't help with that.

gleaming rover
#

@paper echo This is what I go with, too. If it acts like a function, and is meant to be used like a function, but happens to be implemented as a class, I use snake case.
@raven ridge this was my thought process

#

It's still a class, I prefer to use case for denoting the implementation, not the use
@teal yacht I feel differently

#

like imagine you originally implemented your decorator with a function and now you want to use a class; would you rename it just for that reason?

raven ridge
#

breaking your public interface in the process.

gleaming rover
#

^

pseudo cradle
#

I don't think pep8 utilizes camelCase anywhere

#

I think it's all snake

raven ridge
#

it utilizes CapitalizedWords, which is a name I've never heard anywhere else

gleaming rover
#

upper camel case for classes, I suppose

pseudo cradle
#

Oh, huh

gleaming rover
#

Wiki apparently calls it "Pascal Case"

raven ridge
#

and which people usually consider a special case of camel case.

gleaming rover
#

and "dromedary case" (which is pretty amusing) for lower camel case

pseudo cradle
#

Is that just for classes? I could have sworn that all things like funcs/methods/vars were all snake

gleaming rover
#

yes

pseudo cradle
#

k

gleaming rover
#

upper camel case classes, upper snake case "constants" and lower snake case everything else

#

oh, lower camel case for logging 🙂

raven ridge
#

eh, lower came case for a handful of legacy libraries, against everything pep-8 says. 🙂

teal yacht
#

important to note that it's mostly because pep8 didn't exist back then

pseudo cradle
#

pep8 does say "disclaimer: use camelcase for legacy reasons"

teal yacht
#

mostly <-> entirely ?

gleaming rover
#

and logging was based off some Java library, I believe

raven ridge
#

log4j

#

some things were absorbed into the standard library after starting their lives as non-pep-8 libraries that became big and influential, I believe - so in those cases backwards compatibility for the non-compliant names was the reason.

peak spoke
#

The only problem with naming I have is datetime with the shared module and class name, otherwise not following the guide is just... meh

raven ridge
#

that one's a constant source of confusion.

gleaming rover
#

the lack of word separators is mildly irksome IMO

#

but still better than camel case

#

not sure why it irritates me so much in Python

raven ridge
#

module 'datetime' has no attribute 'today'
...
type object 'datetime.datetime' has no attribute 'datetime'
☹️

paper echo
#

not only is datetime.datetime confusing, but datetime.datetime.now is a classmethod and not a top-level function

#

datetime.DateTime would solve all this

peak spoke
#

Would like to see that changed, but guess that's not happening anytime soon

paper echo
#

why change it? just create an alias

#

in datetime.py:

DateTime = datetime

it's that easy

gleaming rover
#

the core of programming is making tedious things simple, and easy things hard.

paper echo
#

and easy things hard.
GWseremePeepoThink

gleaming rover
#

like we were saying...why write code that solves a problem you have now when you can try to write code that may solve a problem some time down the road? 🙂

raven ridge
#

there's a term called "the pit of success". The idea behind it is that too many people design libraries where using them correctly is like climbing a hill - they put lots of obstacles in the way of doing things the right way. Instead you should design things in a way where people fall into doing things the right way accidentally, because it's less friction than doing it wrong.

gleaming rover
#

related to the principle of least surprise

paper echo
#

i like that

peak spoke
raven ridge
#

the str vs unicode handling in Python 2 was the worst example of making it easy to do the wrong thing. Any byte string could be coerced to a Unicode string, and it would work fine as long as it contained only ASCII bytes. Any Unicode string could be coerced to a byte string, and it would work fine as long as it contained only ASCII codepoints.

#

in practice this made it very easy to write code that works on your machine with your test data, and fails on someone elses, or as soon as someone runs your code with a real person's name, or any sort of user supplied input.

paper echo
#

Please don't do this. We need stable APIs. Trying to switch the entire
community to use CapWord APIs for something as commonly used as datetime
sounds like wasting a lot of cycles with no reason except the mythical
"PEP 8 conformance". As I said, it's a pity we didn't change this at the
3.0 point, but I think going forward we should try to be more committed
to slow change. Additions of new functionality are of course fine. But
renamings (even if the old names remain available) are just noise.

gleaming rover
#

all the more it should be switched if it's commonly used

paper echo
#

maybe without a BDFL at the helm we can actually have community input...

#

indeed @gleaming rover

gleaming rover
#

datetime.datetime 🤔

paper echo
#

i guess someone could write a PEP for it

gleaming rover
#

although honestly I'm fine with now being a classmethod

swift imp
#

Where is that quote from

gleaming rover
#

wait, is it?

paper echo
#

@swift imp from the rejected patch that numerlor linked

gleaming rover
#

yeah it is to access the constructor I guess

swift imp
#

What's the big deal

paper echo
#

so now i have 2 peps i want to see that dont exist yet: function composition with @ and adding CapWords aliases to datetime and time

pseudo cradle
#

yeah datetime.datetime irks me

gleaming rover
#

do we really need a datetime and a time, incidentally?

pseudo cradle
#

Well

paper echo
#

@gleaming rover time has things like sleep and perf_counter so yeah i'd say so

pseudo cradle
#

time has stuff like time.sleep

gleaming rover
#

like separate libraries

teal yacht
#

i'd love a function composition operator but i doubt it'll ever get into the language

paper echo
#

^

peak spoke
#

Yes those should remain separate

paper echo
#

@teal yacht

def compose(f, g):
    def _fg(*args, **kwargs):
        return f(g(*args, **kwargs))

imagine if you could write f @ g for this

gleaming rover
#

not sure about the way they're separated but yeah that's reasonable

swift imp
#

Why can't you just do func1(func2)?

gleaming rover
#

but what if you have a callable matrix?

teal yacht
#

that's not composition @swift imp

#

that's application

paper echo
#

@gleaming rover well a class can only have one implementation of __matmul__

gleaming rover
#

precisely

paper echo
#

and __matmul__ beats __rmatmul__

pseudo cradle
#

rip numpy

teal yacht
#

I wrote a small wrapper that defines @ so you can decorate functions but it's dumb

paper echo
#

so, f @ callable_matrix is function composition

#

and callable_matrix @ f is matrix multiplication

swift imp
#

__matmul__ is the biggest meme of a builtin

paper echo
#

i mean its pretty nice if you are a numpy user

teal yacht
#

it's the middleground between adding and not adding numpy to the stdlib

swift imp
#

You know how rare matrix multiplication is?

teal yacht
#

with numpy it's not

gleaming rover
#

so, f @ callable_matrix is function composition
@paper echo but what if I want the reverse order?

swift imp
#

No even in numpy it's pretty rare

teal yacht
#

it's really not

gleaming rover
#

I'd say it depends on what you do

paper echo
#

@gleaming rover tough, because that's how __matmul__ and __rmatmul__ work

#

what if you have conflicting implementations of +? same thing

gleaming rover
#

@gleaming rover tough, because that's how __matmul__ and __rmatmul__ work
@paper echo PEP denied 😢

swift imp
#

Most people doing ML or DL arnt implementing their own algorithms

teal yacht
swift imp
#

They're using scipy and sklearn

gleaming rover
#

I would like it if we had more operators

paper echo
#

@gleaming rover this has nothing to do with @ and everything to do with how operators work in general

swift imp
#

And just calling methods/functions

teal yacht
#

it's more common than |, << , -= etc

paper echo
#

@gleaming rover its inherently asymmetric by design

gleaming rover
#

yeah, I know

#

I was kind of kidding about callable matrices

#

but still...

#

are there any free symbols left?

#

! and $ aren't used, right

swift imp
#

They should have just implemented .*

pseudo cradle
#

@swift imp I'm implementing my own algorithms in addition to using those

teal yacht
#

I mean, operators don't have to be one character

pseudo cradle
#

#!

teal yacht
#

.* would have broken the numpy api

#

unless you mean for something else

pseudo cradle
#

.* works in matlab

gleaming rover
#

! shouldnt be used, its negation..
@unkempt rock in other languages...

paper echo
#

@swift imp dont forget that python is also used in academia quite a lot now

#

not just ds/ml industry

#

a lot of people using numpy in a lot of fields

teal yacht
#

yeah, .* in matlab is * in numpy

gleaming rover
#

not really a reason not to use it IMO

pseudo cradle
#

That threw me off a little bit

#

when I first learned numpy

raven ridge
#

the ` and <> operators were removed from Python.

swift imp
#

I'm saying * is wrong too

raven ridge
#

so that's two more that could be added back 😄

gleaming rover
#

@gleaming rover Yes, but python shouldnt be confusing
@unkempt rock why would it be confusing?

pseudo cradle
#

What do you mean there's no callable method *

gleaming rover
#

if you're coming from another language you know that syntax differs between languages

#

so are and and or confusing?

#

because most languages use & and |?

pseudo cradle
#

Heh or C

swift imp
#

is and == is another bad decision imo

raven ridge
#

most use && and ||

pseudo cradle
#

&& and ||

teal yacht
#

I mean, sure, but numpy was built in python, there's no way around it @swift imp

pseudo cradle
#

beat me to it

raven ridge
#

which does make & and | confusing, honestly.

teal yacht
#

and element wise multiplication is extremely common

gleaming rover
#

& and | are bitwise, right

pseudo cradle
#

Well, in C & is an address operand

gleaming rover
#

and && and || are logical

raven ridge
#

&& vs & and || vs | isn't easy to explain to people.

swift imp
#

@gm yes

teal yacht
#

so making * matmul and having element wise mul as a method would have been a mistake

raven ridge
#

Well, in C & is an address operand
@pseudo cradle and bitwise arithmetic.

gleaming rover
#

so making * matmul and having element wise mul as a method would have been a mistake
@teal yacht agree

pseudo cradle
#

Ah, right

gleaming rover
#

like honestly you know in Python not is used for negation

#

which frees up !

swift imp
#

I think the biggest issue is that there arnt even matrix objects

pseudo cradle
#

matmul as a dunder confused me

swift imp
#

What? @gleaming rover !=?

pseudo cradle
#

not using it but it's existence

teal yacht
#

we don't really need a matrix object

gleaming rover
#

okay, fair enough

#

but I meant unary logical negation

#

like as a binary operator

swift imp
#

I don't see why matrix multiplication couldn't have just been a method

gleaming rover
#

syntactic sugar

#

I suppose?

teal yacht
#

the rationale is explained in the pep, it's comfy

raven ridge
#

methods don't generalize to arbitrary types.

gleaming rover
#

it looks nicer in chains of expressions

swift imp
#

It's not communititive anyway

teal yacht
#

not every operator is commutative, that's not an argument

gleaming rover
#

^

#

neither is %

teal yacht
#

neither is -

#

commutative operators are the exception, not the rule

raven ridge
#

the entire advantage of custom operators - the only reason for operators over methods beyond syntactic sugar - is that they can work on arbitrary pairs of types that don't have prior knowledge of each other.

#

that's something you can't do with methods.

swift imp
#

It's just such a rare operation and also no communtative that I don't see why it couldn't be a method instead of a Dunder

gleaming rover
#

honestly this matrix multiplication debacle

#

could have been solved by defining matrix multiplication as the Hadamard product instead of what we have now

#

🤔

swift imp
#

Dot with circle?

teal yacht
#

but * is already the hadamard product

swift imp
#

Yeah hadamard is element wise

teal yacht
#

changing that would have broken every existing numpy code

gleaming rover
#

I'm kidding

teal yacht
#

and hadamard product is vastly more common than matmul

gleaming rover
#

hence the 🤔

teal yacht
#

ah, wasn't obvious to me, mb i guess

gleaming rover
#

I should probably have used some other emoji

#

sarcasm is so hard to convey

#

but anyway yes, I agree

#

elementwise operations are far more common

#

IME @ least

teal yacht
#

no, no, they are

gleaming rover
#

you know one nice thing about Scala

swift imp
#

They are...

gleaming rover
#

it allows you to define arbitrary operators

#

you end up with some kind of symbol soup

teal yacht
#
  op  stdlib  scikit-learn  nipy  combined
====  ======  ============  ====  ========
   =    2969          5536  4932      3376 / 10,000 SLOC
   -     218           444   496       261
   +     224           201   348       231
  ==     177           248   334       196
   *     156           284   465       192
   %     121           114   107       119
  **      59           111   118        68
  !=      40            56    74        44
   /      18           121   183        41
   >      29            70   110        39
  +=      34            61    67        39
   <      32            62    76        38
  >=      19            17    17        18
  <=      18            27    12        18
 dot ***** 0 ********** 99 ** 74 ****** 16```
pseudo cradle
#

Delicious

swift imp
#

Eh, ARB operators seems bad idea

pseudo cradle
#

smybol soup

gleaming rover
#

leftVar <$@< rightVar

#

kind of looks like a futuristic gun attack

pseudo cradle
#

Looks like a rocket ship

teal yacht
#

it puts a bar to entry in the language, which is not something wanted by the python community as a whole

#

that's one of the reason people are scared of haskell

gleaming rover
#

I think arbitrary operators are a bad idea in Python

#

but not in general

teal yacht
#

because many common operations are operators, so you have no way to know what they do at first glance

gleaming rover
#

that's one of the reason people are scared of haskell
@teal yacht I can't imagine why

#

after all, a monad is just a monoid in the category of endofunctors

#

🙂

teal yacht
#

memes aside, I think it's not a problem if we ignore beginners

gleaming rover
#

it doesn't help that many people who say "Haskell is easy" tend to unload a ton of category theory on you

swift imp
#

You mean you don't like a language that's based on a field of mathematics that like 99% of the human population never learns? That even a lot of math majors don't learn?

teal yacht
#

these are the worst

pseudo cradle
#

I think every field will have people who are like "X is so easy"

#

Because they learned it

gleaming rover
#

any explanation that begins with "let us assume" or "given that" has instantly shut out a ton of people

swift imp
#

No man category theory is freaking abstract even for a mathematician

teal yacht
#

But tbh, a huge majority of people that I've noticed say "haskell is too hard" tried it for 1h or were just put off by the syntax being different

gleaming rover
#

yeah

#

I mean

#

it's hard to convince some people to use map/filter (or comprehensions) already

#

functional paradigm is great but

teal yacht
#

yay for CS education being textbook java oop

gleaming rover
#

I guess since I didn't come from a CS background

#

I don't really have preconceptions in that regard

raven ridge
#

I don't get this "just make it a method" line of argument. + needs to be an operator; we cannot have an add method because we need radd as well, and we need a way to signal to the interpreter that it may need to try both.

swift imp
#

I personally don't like map/filter

unkempt rock
#

what is the use of \r
in file handling

pseudo cradle
#

I mean, there's such thing as something having a steep learning curve or a high barrier to entry, even if it evens out after that

gleaming rover
#

depends, what OS are you on

#

I personally don't like map/filter
@swift imp the ideas?

unkempt rock
#

pls help me

teal yacht
#

Labrador <: Dog <: Animal <: Living amirite

gleaming rover
#

like in Python I generally avoid map/filter because I prefer comprehensions

swift imp
#

Same reason as Guido

raven ridge
gleaming rover
#

but the concept is nice

swift imp
#

Comps are clearer

unkempt rock
#

what is the use of \r
in file handling

#

\r = ?

gleaming rover
#

@unkempt rock think you wanna post that in a help channel

unkempt rock
#

ok

swift imp
#

I want multiple dispatching

gleaming rover
#

like Julia?

swift imp
#

Yeah

#

And like F#

teal yacht
#

F# has multiple dispatch now ?

#

Is it like super new ?

paper echo
#

imo the reason map and filter are yucky in python is largely because lambdas are yucky

#

and also because typically you need to call list() at the end or something

#

which sometimes is great and other times isnt great

#

i've done things like

def lmap(fn, *iterables):
    return list(map(fn, *iterables))

and id probably be happy to import a library full of functions like this

#
from itertools import chain

def lmap(fn, *iterables):
    return list(map(fn, *iterables))

def lfilter(fn, x):
    return list(filter(fn, x))

def lflatten(fn, x):
    return list(chain.from_iterable(x))
#

etc.

pseudo cradle
#

doesn't seem too unreasonable

cloud crypt
#

Yeah, generally, you would unpack it, but in many cases you would not. Besides, you don't want map() accidentally constructing a huge list. Also, some people might not need the entire result of map(), just some of its elements on front; while this can be better achieved with generating a full list, you can still do it fine with iterators.

wide thistle
#

hi huys my name is pranay and i am new to git and git hub , and i am confused that what is snapshots , can anyone explain me?

gloomy rain
#

@wide thistle This channel is for indepth discussion of the Python language. If you need help, please refer to #❓|how-to-get-help.

safe linden
#

so... I'm doing this refactor and I THINK I just came across someone using bare exceptions as control statements in a for loop and wanted a second set of eyes just to
a) see this insanity that I had never even thought of, and
b) ensure that is indeed what it is before I potentially rip out a bunch of needed exceptions

#
for band in sorted(self.key()):
    try:
        full = np.vstack((full, self.dct[band]))
    except:
        full = np.array(self.dct[band])
teal yacht
#

I have a few things to say about it, but I'm busy, but you can except NameError if that's what you meant

safe linden
#

no, like, i think it is using try/except instead of if/else

#

full is undefined to clarify

#

so it would be defined within the except

#

and then it would loop back up to the try statement to finish out the vstack

#

I think is what this does

gloomy rain
#

There are a few cases where using a try/except can be valid for normal control flow, but none of those cases include using a bare except with no type specification, I think.

safe linden
#

tbh I had never heard of try/except as a control flow

#

part of why this looks so crazy to me

teal yacht
#

look up eafp vs lbyl

gloomy rain
#

@safe linden Because the except has no type specification, it's hard to know for sure what it's supposed to do.

#

Most likely it's only looking to catch one particular exception.

#

But the person who wrote it was too lazy to add it.

teal yacht
#

as in

if s.isnumeric():
  x = int(s)
else:
  x = 0```vs```py
try:
  x = int(s)
except ValueError:
  x = 0```
gloomy rain
#

@safe linden But yeah, that's pretty ugly.

safe linden
#

I guess my confusion is, even if x = int(s) works, if there was a pre existing uncaught value error, couldn't that lead to some crazy runtime fun?

gloomy rain
#

There are various ways you could write that code to be more intuitive.

safe linden
#

my plan is to just replace this with if else logic

teal yacht
#

I'm not sure what condition your code checks

#

shape errors ?

safe linden
#

I think there are a lot of other places this has been done too because I've been filling in bare excepts for a few thousand lines now and it just clicked for me what these are

gloomy rain
#
for i, band in enumerate(sorted(self.key())):
    if i == 0:
        full = np.array(self.dct[band])
    else:
        full = np.vstack((full, self.dct[band]))
#

This feels like a big improvement to me.

#

Because you can see clearly what the intention is.

safe linden
#

yeah, I'm thinking going that route

teal yacht
#

oh and the other thing i wanted to say

#

do not use vstack like this, it's awful in terms of performance

safe linden
#

@teal yacht I meant in the example you posted, wouldn't any ValueError exception that has been thrown potentially reassign x?

teal yacht
#

use a python list, and cast to np.array at the end

safe linden
#

even if your try worked as intended?

teal yacht
#

yes it would, hence why it's important to throw sensible errors

#

there's also finally: to avoid that issue

#
try:
  x = int(s)
except ValueError:
  x = 0
finally:
  somecodethatmaythrowvalueerror()
  print(x * 2)```
gloomy rain
#

@safe linden The contents of the try block only contains an int() call, though.

#

There's pretty much only one possible way ValueError gets thrown.

safe linden
#

yeah, I was trying to understand this in the idea of if it existed as a snippet of a larger function

teal yacht
#
>>> t(t1, setup=setup, number=100)
8.820794968996779
>>> t(t2, setup=setup, number=100)
0.016266275008092634```vstack vs list.append
#
setup = """import numpy as np
x = np.random.randint(100, size=(100, 100))"""

t1 = """
for i in x:
  try:
    y = np.vstack([y, i])
  except NameError:
    y = np.array(i)"""

t2 = """
y = []
for i in x:
  y.append(i)
y = np.array(y)"""

from timeit import timeit as t```
gloomy rain
#

This is what I mean by try/except being valid for normal control flow only in certain special cases.

#

Like when you wrap an int() call and nothing else in it.

safe linden
#

I mean, it is certainly very unique and with clever throws could probably lead to some interesting code behavior

#

whether interesting is what is intended however...

gloomy rain
#

You also generally don't want to be clever. With the int() example, it's still very obvious what's happening.

#

Unlike in your original example.

teal yacht
#

I'm sorry my ocd is kicking in, but please acknowledge my comment on np.vstack vs list.append

safe linden
#

yeah, it's honestly been a trip. I'm just a student but I'm pretty much doing software architecture and development for a lab and it has been fascinating learning how little I know

#

your comment is acknowledged, though tbh I haven't really processed it XD

#

trying to show alternatives to vstack?

gloomy rain
#

@safe linden That sounds like good experience.

#

Most people don't get to practice taking charge of software design for a real project so early.

teal yacht
#

showing that you shouldn't use vstack in this example really

#

it's about 800x slower on the machine i benchmarked it for a small 100x100 array

safe linden
#

Yeah, it's been really crazy because I thought I wrote good lines that were readable and didn't throw error and that's what programming was

#

I am realizing very quickly that is the least significant part

gloomy rain
#

Well, it's not unimportant to write readable code...

safe linden
#

as I try and take a code base written by 6 different people across a decade and turn it into something with extensibility in mind

#

oh, i meant that say a given line or file would be readable

#

but the package would have no structure to it

#

just whatever I cludged together at the time

gloomy rain
#

Well, sure, there are other important aspects as well.

safe linden
#

and I am also learning what I thought was readable was not nearly as good as could be

#

like my mind was blown at the idea of indenting before operators rather than after

#

like it seems simple, but the literary convention is after so I always did that

gloomy rain
#

That's generally how I prefer it, but opinions differ on that.

safe linden
#

but when I started doing it before, it reads like a balance sheet and is beautiful

gloomy rain
#

An advantage of indenting after operators is that you can get for example boolean expressions to line up better.

safe linden
#

also didn't mean to ignore you Lgneous, that is a fascinating point

gloomy rain
#
if (x == y and
    a == b and
    f == g):
safe linden
#

interesting

#

thoughts on indenting after for boolean expressions but before for arithematic ones?

gloomy rain
#

But if a given clause is long, it might be harder to see what the operator actually is if it's at the end

safe linden
#

or is that just inconsistent

gloomy rain
#

So I tend to prefer indenting before the operator

#

I think I usually stick to one for all operators, but I'm not dogmatic about it

#

At work we allow both

safe linden
#

very fair

#

also, curious for your thoughts. I've been basing a lot of the style I'm trying to use on PEP 8

#

it seems like it is mostly good ideas though I don't really have enough experience to recognize any bad ones

gloomy rain
#

We use flake8 to lint our code, we basically follow PEP8 with a few exceptions where we feel like it's too restrictive to be convenient.

safe linden
#

where do you feel it is too restrictive? line length?

gloomy rain
#

Well, what we just talked about is one thing. PEP8 states line breaks should go before the operator, but we don't want to enforce that.

#

And line length, yes.

#

PEP8 says 79 characters, which I think is pretty silly these days.

#

So we go with 119.

safe linden
#

oh interesting, I saw them talk about 99, curious why 119, and also if there is a reason these go in intervals of 20

gloomy rain
#

Because monitors are big these days, and 119 fits easily.

#

I don't think there's any 20-interval standard. I haven't really heard of anyone using 99 either.

#

It was 120 at my last job too.

safe linden
#

hmm, I may have simply mis remembered the PEP 8 documentation

gloomy rain
#

PEP8 says 79, anyway.

grave jolt
#

I prefer a shorter line length (80-90) because for me it's simply hard to read lines that span my entire monitor, especially if I have two files open side by side

safe linden
#

oh, I had it right, they recommend extending to 99 if you do extend it. But it sounds like there is no particular rhyme or reason to any of it past what works

peak spoke
#

99/100 is pretty common; I find 120 a bit too long for logic but things like strings really can use longer limits

gloomy rain
#

I tend to prefer longer identifiers, that might be part of it.

#

I really hate cryptic abbreviated identifiers.

safe linden
#

there certainly is a benefit to be had with nice full words

gloomy rain
#

But avoiding them will increase the average line lenght somewhat.

grave jolt
#

what's not clear in PMOVSXBW or wcsxfrm

safe linden
#

well thanks again, I think I should get back to the grindstone

#

oh

#

so there is a bunch of random commented out lines of code that still lingers

#

I think maybe from before the project used git

#

Recommendations on just ripping it out or putting it somewhere?

gloomy rain
#

@safe linden Getting rid of those as soon as possible is a good principle, I think.

visual shadow
#

Save one state of the current code with them as is

#

If you're using git, then great. Then add a commit that gets rid of it all

gloomy rain
#

They get increasingly outdated and less valuable as time goes on. If you really want to keep it for later, create a branch for it or something.

undone hare
#

If it is working as it is right now, those lines are probably useless now

safe linden
#

the original package will exist as is, with all this in it. I'm creating an entirely new distro from the old code

gloomy rain
#

Though I think if you need to do that, in 99% of cases you will just forget about it and never use it.

visual shadow
#

Ahh

undone hare
#

If it is an half implemented feature, maybe you should move them to a new branch and comment them out?

visual shadow
#

Then you'll probably not need them at all

gloomy rain
#

A lot of linters I've used have a warning to remove commented out code.

visual shadow
#

Now if only someone taught me how to use git.. :(

safe linden
#

I can see that. it honestly feels a little unecessary with correct git usage

#

you and me both Darr XD

gloomy rain
#

@visual shadow Git gud. 😄

visual shadow
#

😆

safe linden
#

omg, I'm stealing that

grave jolt
#

Speaking of git, is there some 'meta-git' tool that tracks changes in the git history? Because I can screw up my local git history, right?

visual shadow
#

I guess you could make a git out of the git history 😂

gloomy rain
#

@grave jolt Not really. Nothing builtin anyway.

safe linden
#

if you are pushing to a remote branch, wouldn't that sort of do it?

gloomy rain
#

You can recover some changes from the reflog, but it's temporary.

visual shadow
#

I'm not entirely sure what that would do to one's sanity

safe linden
#

or do I completely misunderstand the question

visual shadow
#

I think the latter. Basically you can falsify git history by using rebase or something along those lines.

gloomy rain
#

You can rewrite the git history with certain commands.

visual shadow
#

I forget, since i never use git much. (and I know I should. I really do)

gloomy rain
#

Such as rebase and amending commits.

safe linden
#

Darr, what really did it for me was using an editor with strong git integration if that helps

#

though I don't really know it tbh

#

but it is helping

gloomy rain
#

I guess what @grave jolt wants is a way to have a record of history rewrites.

grave jolt
#

yes

gloomy rain
#

That sounds like the path to madness to me though.

#

Regular git histories are already complex enough.

visual shadow
#

It does. I felt like I was closest to learning git when that happened. My issue is, we have a crazy amount of tape around what softwares we can use, and we keep switching permissions on a per project basis

gloomy rain
#

Probably better to just learn to use good principles when managing your branches.

safe linden
#

I mean, hypothetically, all of this stuff exists in files that could be versioned...with git...

gloomy rain
#

If you just never use history-rewriting commands, you can't really lose anything.

visual shadow
#

For some reason, each ide that integrates nicely with git and git itself is not on our pre-approved list yet.

grave jolt
#

I guess the best possible principle is not rewriting history at all 🙂

visual shadow
#

It's such a hassle to try getting the permissions cleared that we end up just doing work without version control

safe linden
#

wait, vscode isn't approved where you work?

gloomy rain
#

@safe linden History rewrites are destructive. You can lose information permanently that way.

visual shadow
#

(cue 15 copies of the same file with v1-15 added at the end)

gloomy rain
#

@grave jolt I would say that you should avoid it except for well-established and well-understood workflows.

visual shadow
#

Sort of. It's approved in our local environment, but each project we work on a new Vdi

#

So.. Yeah... Sigh.

gloomy rain
#

Amending a local commit is pretty safe, because you're only changing the previous commit.

#

Interactive rebasing a local branch that you haven't pushed is also quite difficult to mess up, and can be very useful.

#

It's when you are rewriting a branch that someone else is accessing or writing to that things can get really hairy.

#

So like, never ever do it to a mainline branch.

teal yacht
#

man I'm curious to see what the approved tools list looks like

safe linden
#

probably emacs

teal yacht
#

generally emacs is not allowed in those restraint environments

#

packages are super common and could be malicious

gloomy rain
#

@visual shadow

It's such a hassle to try getting the permissions cleared that we end up just doing work without version control
lemon_scared

visual shadow
#

Yeppp. Rip

safe linden
#

sorry, meant emacs as a cheap stab at humor

teal yacht
#

ngl I'd quit if I ended up in a workplace without vc

gloomy rain
#

That would definitely be a dealbreaker, yes.

safe linden
#

you could probably tell management that this is increasing project timelines drastically...

gloomy rain
#

I mean, my current workplace is not great, but at least we're not on that level.

teal yacht
grave jolt
#

I think we are drifting into off-topic

visual shadow
#

Ah you're right, my bad

gloomy rain
#

I kind of forgot what channel this was for a second there.

visual shadow
#

I didn't even realise it 😅

grave jolt
#

I'm the big bad moderator now

safe linden
#

Can't run a discord of a few thousand without some of those

Are informational PEP like things mostly a python thing? I don't remember such clear style conventions when I was in the depths of C++

gloomy rain
#

I don't think every language has a single widely adopted standardized style convention.

#

But some probably do.

#

Some have multiple competing ones.

visual shadow
#

I believe so actually, or well, it's more formalized and well advertised. Other languages has like mailing lists that never come out for wider consumption

#

Or if they do come out, they're pushed to websites that have such horrid layouts it's really difficult to read.

safe linden
#

Also this may verge on off topic but it is pep related. Should I stop pushing for every person I know who is learning python for the first time to read the PEP 8 documentation?

#

Cause I feel like I am part of a cult a little bit at times tbh

visual shadow
#

Ease them into it perhaps, as opposed to pushing it from day 1

#

Let the question of code style come up organically.

teal yacht
#

I think styles and other opinionated things are one of the few areas where arguments of authority are good

#

I'd rather say "we'll use pep because the big bois said so", than having 340982304823 conventions in place

visual shadow
#

What I personally suggest is letting them code for 6 months in peace, and then turning on their code style analysis in their editor secretly one day. Watch them squirm as the entire file gets highlighted. Every single line.

teal yacht
#

see the state of C

gloomy rain
#

@safe linden Everyone should learn to follow style conventions eventually. But it's good teaching practice not to overwhelm your students with too much information all at once.

visual shadow
#

People pick it up pretty quick after that in my limited experience

safe linden
#

I guess my issue is I know the uni I am at will never touch on programming as an employable skill. It uses it as a tool to illustrate CS concepts

#

so things like style aren't even mentioned

#

like the people I talk to aren't aware style is a thing, so the conversation won't come up organically

#

similarly, versioning and architecture also get the boot

gloomy rain
#

You could certainly introduce the concepts to your peers if you get the chance

#

Even if you don't force them to follow it religiously

visual shadow
#

Tough to say. There's some merit to actually experiencing the problems that show up if you don't follow code styles, but honestly we usually pick these things up on the job. If you try to teach things that people don't see a use for, it may be tough to get the message across. Gentle nudges at most.

teal yacht
#

just silently add a github action that rejects non flake8 compliant code

gloomy rain
#

Worse case scenario, you'll get a rude awakening when you actually start working, but it's nice to be familiar with these things ahead of time.

#

Probably makes a better impression in technical interviews too.

visual shadow
#

Thats a very good point honestly, didn't even think of that angle

gloomy rain
#

@teal yacht That's not a bad idea at all.

visual shadow
#

Well.. If you have github that is..

#

coughs

gloomy rain
#

You can do the same thing with any CI system.

#

lol

safe linden
#

That's valid. I didn't appreciate the significance of most of this until doing a refactor. It's been interesting because at a university, you have a lot of self taught programmers who have never been in industry

visual shadow
#

In the industry too honestly, the same statement applies when you start

gloomy rain
#

By interesting, I guess you mean horrible.

safe linden
#

so a lot of the wisdom you all just kind of know hasn't crossed over, it's been one of the biggest benefits of being on this discord just to get bits of that insight

gloomy rain
#

@safe linden I know, I went to uni once too.

#

Some of the information filtered through the student community as time went on, though.

#

Like, some people learned stuff on their own and taught others.

#

Even if we never had classes on all of it.

safe linden
#

Yeah, I am certainly getting that vibe. And am trying to bring some of that light back to the cave. But I don't know enough to ensure I'm bringing back good ideas XD

#

Thus why I love the PEPs

#

cause python went and really tried to put a lot of the good ideas in one clearly labelled place

visual shadow
#

As long as you can convey uncertainty when you're actually uncertain, I think you'll only be doing good.

#

Sometimes the trick is to just start the discussion, people are quite capable of figuring things out if you can just reveal what we don't know, you know?

safe linden
#

hmmm. interesting. I'll think on that. That organic approach you both were recomending.

Thanks again to all of you. I swear I learn an entire masterclass of information every time I come to this discord.

gloomy rain
#

Good luck

half wolf
#

I've lived with linters on my projects. I've turned them off. It's not a good idea imo. A warning in PRs is fine but to fail builds on a missing newline is obnoxious and stupid imo.

gloomy rain
#

It's easy to prevent letting a missing newline get to the CI with the right tooling.

#

And I much prefer spending some extra time cleaning up my PRs than having to read code that's all over the place.

peak spoke
#

imo having to fix erroneous typehints and styling that someone introduced in the file you're editing is worse

teal yacht
#

that's why you accompany the linter of a formatter

#

that way actual convention mistakes do not pass, while simpler formatting ones get fixed

boreal umbra
#

Does the linter check unit tests?

gloomy rain
#

You mean if you usually apply the linter to test code?

boreal umbra
#

The most recent unit tests I've written use a lot of mocks so those obviously won't be the right type

undone hare
#

That'd be nice to have a linter/tool to test the quality of a test

gloomy rain
#

@undone hare Mutation testing kind of does that. A tool randomly modifies a piece of production code under test and then checks if that causes any test to fail. If not, it tells you what the change was, so you can add a test to cover it.

teal yacht
#

there's code coverage for that too

#

lets you quickly see untested functionalities

gloomy rain
#

Code coverage is good, but mutation testing goes deeper.

teal yacht
#

yeah i didn't mean "too" as in it does the same thing, i meant that's another tool to use

gloomy rain
#

Sure

undone hare
#

Ah that's interesting

sacred tinsel
#

@boreal umbra I'm not sure about various type checkers like mypy but if you specify the spec for your unittest.mock.Mock instance, it will correctly pretend to be of the specified type, e.g.:

>>> from unittest.mock import Mock
>>> 
>>> isinstance(Mock(), int)  # Without a 'spec'
False
>>> isinstance(Mock(int), int)  # This pretends to be an int
True
boreal umbra
#

@sacred tinsel I'll have to look into that. I used simple namespace for the most recent test.

The first library I wrote tests for was already written by someone else, so nothing needed to be mocked.

sacred tinsel
#

I have no idea what SimpleNamespace is lol

#

but I'm reading about it now

boreal umbra
#

It's kind of like a dict that uses tokens and the dot operator.

#

I think

undone hare
#

It is the thing that when you google "Python dict using attribute" you never find even if that's exactly it

teal yacht
#

I mean, you do lose every other dict functionalities

undone hare
#

Well yes

peak spoke
#

It's a very nice utility so you don't have to do class Name(object): pass just for attributes, but it's not really a proper attr dict if you need anything else from it

real stump
#

I've had someone ask me how

a, b = b, a

works internally. Turns out, if you do dis.dis and get the bytecode, it pushes a and b to a stack, and rotates it

pearl river
#

oh dear

#

hmm, that's actually not what I'd expect, must be a specific optimization

#

I'd expect it to evaluate the right part (a tuple of values) and then do deconstructive assignment to the values in the left part.

spice pecan
#

CPython optimizes that away when you swap two names around, yeah

real stump
#

yeah i thought it would do that too, since there's only one assignment at a time

half wolf
#

@gloomy rain and @undone hare talking about mutation testing, I'm the author or mutmut :)

gloomy rain
#

@half wolf You presented at PyCon Sweden, right?

half wolf
#

Yes I did!

gloomy rain
#

Oh, cool, I was there

#

Good talk

half wolf
#

Glad to hear. I had to go for a pretty basic talk given most people don't know anything about it.

gloomy rain
#

I played around a bit with Cosmic Ray earlier, so I was somewhat familiar with the concept at least

half wolf
#

Hopefully after it didn't require rabbitmq to even run :)

gloomy rain
#

Oh, I have no memory of that being a requirement so probably not, haha

#

(why? lol)

half wolf
#

I couldn't even install it when I first tried it heh. My public shaming has made it tolerable (imo barely).

gloomy rain
#

I seem to have some faint memory of it being quite a hassle.

cloud crypt
#

you know that feeling when
everything in python has nice and cozy snake_case names
and there is just logging library

somber bone
#

@oblique raven I take it by changing the nameservers from Namecheap's default to Cloudflare's also the ability to redirect any traffic to my website gets disabled, correct?

oblique raven
#

A lot of the options that namecheap provide are disabled when you use cloudflare

#

But

#

You can also do most of the same things, and more using cloudflare

somber bone
#

Is what I said though true?

oblique raven
#

I believe so

pseudo cradle
#

Wow

#

I didn't even have to ping

pseudo cradle
#

So I know there's some back and forth between camelCase and snake_case.. but what if we COMBINED THEM.
A variable named, for instance, heartRate_BPM

#

Interesting or a sin?

#

or is heart_rate_bpm better

somber halo
#

the latter

#

as a matter of being in a consistent style.

#

also, imagine if you have a class named heartRateBpm

#

although in languages like Java acronyms shouldn't be in all caps, according to PEP8 the correct version would be heartRateBPM

grave jolt
#

what about inverse camel case

#

when the first letter is small and the rest are capitalized

#

lIKEtHIS

#

or maybe just the last letter of each word is capitalized

#

likEthiS

#

It has so many wonderful ideas, like an integer type that cannot take a value from 1 to 71

pseudo cradle
#

hah

gleaming rover
#

you know that feeling when
everything in python has nice and cozy snake_case names
and there is just logging library
@cloud crypt someone summoned me?

vague obsidian
#

Hello! My computer vision program, it finds a face and puts a box around it, how could I make it so when a face is detected it runs a code?

safe linden
#

so if I am reading PEP 8 correctly, it says function names should be lower_case and also file names should be lower_case?

#

I feel like I have to be wrong since it is already leading to some imports that look unclear

#

though the alternatives of UPPER_CASE and MixedCase would also cause problems

unkempt rock
#

i was working with tkinter trying to make a GUI that could automate making directories and open my IDE pycharm. I kept using the .destroy() function to close the window after it opened pycharm. it would just keep saying not responding, so i used the command line and did
os.system('taskkill /f /IM python.exe')
which will force quit the application. Now I can't open python applications anymore. Like i can't double click them. They all force close instantly. I've tried to repair it from control panel in programs, i've shut down my pc and rebooted. I've shut down all command processes in case something is still running, but nothing works. And now i can't use any of the python programs i made on my computer without manually running them through pycharm or through the command prompt.

gleaming rover
#

so if I am reading PEP 8 correctly, it says function names should be lower_case and also file names should be lower_case?
@safe linden why do you find that problematic?

#

i was working with tkinter trying to make a GUI that could automate making directories and open my IDE pycharm. I kept using the .destroy() function to close the window after it opened pycharm. it would just keep saying not responding, so i used the command line and did
os.system('taskkill /f /IM python.exe')
which will force quit the application. Now I can't open python applications anymore. Like i can't double click them. They all force close instantly. I've tried to repair it from control panel in programs, i've shut down my pc and rebooted. I've shut down all command processes in case something is still running, but nothing works. And now i can't use any of the python programs i made on my computer without manually running them through pycharm or through the command prompt.
@unkempt rock try a help channel #❓|how-to-get-help

unkempt rock
#

ok i did

safe linden
#

from PEP8
"Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability."
"Function names should be lowercase, with words separated by underscores as necessary to improve readability."

#

I am reading that as the naming conventions for functions and modules are identical with functions preferred as longer and more descriptive?

north root
#

yeah, but not too long either

#

the function's docstring would go more in depth about itself