#internals-and-peps

1 messages · Page 92 of 1

desert peak
#

I suppose given all the nested tuples it doesn't make much sense

#

but I've never seen a lense used, so I'm unsure what problem you're solving there

#

if asked, I'd give the lens return an annotation of Tuple[Union[Tuple[int, ...], int]]

grave jolt
#

I was making an animation library that used immutable values, which proved to be very good for spreading the animation rendering across threads/processes

desert peak
#

now that I think about it more, those tuples can be infinitely nested hm?

#

how would a strictly typed lang solve that same problem?

grave jolt
#

The issue is that first has the type Lens[Tuple[A, C], Tuple[B, C], A, B] (it changes an A to a B inside of Tuple[A, C]). And that annotation is illegal -- for no good reason IMO

#

It's possible in Haskell

#

(for example)

flat gazelle
#

why doesn't that work?

grave jolt
#

that's just illegal

#

You can only have polymorphic functions, not values

desert peak
#

@flat gazelle comin' in with the typing lib fix 😂

grave jolt
#

The answer is basically: "No, this isn't allowed by PEP 484. "

#

I'm not sure why he's so worried about being compliant with PEPs, since typecheckers are already mutually incompatible

flat gazelle
#

would Callable[[None], Lens[tuple[A, C], tuple[B, C], A, B]]] work

grave jolt
#

maybe it's somehow fixed in the new version, let me see in a minute

#

btw, if you do

def f(a: A) -> A:
    return a

g: ? = f

you can't put anything in place of g that would preserve the type

flat gazelle
#

I guess no polymorphic values. Shame, but understandable.

regal mesa
#

hello, could you suggest which is the best tool for static code analysis for python (despite pylint which is already in place in our project) ?

mint forge
#

mypy?

grave jolt
regal mesa
#

thanks everyone. have tested mypy already and it is showing soo many errors in legacy code (where we didn't use types descriptions) omg..

tidal marten
#

I wonder if Python has a mechanism for interfaces. I want to do type hinting that behaves like Go's interface instead of having to do abstract classes.

brave badger
#

typing.Protocol?

tidal marten
#

Hmm, let me check...

#

Oh nice!

#

I assume it works with "magic" methods (like __init__, __call__, etc)

brave badger
#

Work in what sense?

tidal marten
#

Check that there's a __call__ method for instance

#

I know there's Callable, but just for example

brave badger
#

Yep

#

You could actually use them to model better callback types

tidal marten
#

Nice, thank you!

brave badger
#

Speaking of Protocols

#

I actually like them better over subclassing unless I have good reason to do code sharing

wide shuttle
grave jolt
#

yeah

#

with @runtime_checkable

#

But most of the time isinstance isn't really needed

#

(unless you're writing some library)

wide shuttle
#

Yes, it's more meant for situations in which you want your types to be able to checked against ABC protocols in a goose typing kind of way.

grave jolt
#

also more friendly syntax error messages

undone hare
#

Is that for 3.10?

grave jolt
#

yes

undone hare
#

There are some cool things in 3.10

narrow kettle
#

Finally

brave badger
#

I'm trying to remember a type-hinting PEP that basically allowed for extra metadata to be put on types

#

Or is that even a proper PEP? I'm not sure

narrow kettle
#

I wonder what part of LL1 was needed that the old parser couldn’t do

fallen slateBOT
#
**PEP 593 - Flexible function and variable annotations**
Status

Accepted

Python-Version

3.9

Created

26-April-2019

Type

Standards Track

brave badger
#

Ah yeah

#

Was wondering if it's possible to write a mypy extension that does some rigid form of dependent types

undone hare
narrow kettle
#

Gotcha makes sense

undone hare
#

*for the as, not for the with

paper vapor
#

guys how to do this thing

#

like the as is in a blacker speech thing

wide shuttle
#

You can make inline code blocks by wrapping text within backticks: `hello` -> hello

paper vapor
#

test oh wow

#

tahmks

boreal umbra
#

How much overhead does having __del__ add to cpython? I assume when it comes time to destroy an object it first checks to see if __del__ is defined rather than calling it with the possibility that it doesn't do anything. But that would still be a check that has to be performed for every PyObject.

undone hare
#

I don't have any number, but I wouldn't expect much

#

Such behaviors are already used a bit everywhere in the language

#

Take __getattribute__ for example

#

It will be called very often, compared to __del__

#

I think if you put it in context, it is fairly negligeable

deft pagoda
#

overloading getattribute has some real overhead

#

as i recently experienced

swift imp
#

Yeah its one I completely dislike doing and why I am not a big fan of class wrappers

#

even __getattr__ slows stuff down big time.

sharp plover
#

Thought this was interesting, for anyone who doesn't follow Guido on Twitter:

#

Ah, the embed messes up the indentation, sorry lemon_pensive

charred wagon
#

Yeah it's weird. It was discussed here a few days ago.

sharp plover
#

Ah right. Missed that convo.

charred wagon
#

Had me wondering what a legitimate use case of a class definition within a function would be.

#

It's not something I recall seeing done in practice

swift imp
#

Isn't that like the definition of a class factory

charred wagon
#

I don't think so. Don't those return instances rather than creating new types?

#

Technically a class is an instance of type but lets ignore that

pliant tusk
#

doesnt namedtuple do something like that?

raven ridge
#

It's what a class decorator would do

pliant tusk
#

depends on what the decorator does. if its just pulling information from the class definition then that makes sense, otherwise its easier to just modify the passed in class

swift imp
#

I do have some code at work where there is a function that returns new subclasses. However I do it the sane way with types.new_class and it passes the args to __init_subclass__

grave jolt
#

Maybe there's a way with subclassing Scene, but I doubt it's a better solution

charred wagon
#

Good point on the class decorator

grave jolt
charred wagon
#

Yeah unholy is a good way to describe that manim code

#

You gotta do what you gotta do

grave jolt
#

well, I tried to keep it as coherent as possible

paper hatch
brave badger
paper hatch
#

oh boi, moar reading 😴

maiden rivet
#

Hey, i've been trying to use lz4 in python for quite a bit now

#

i have the package installed via pip

#

but when i try to run it, i get this

#

it's odd because the versions binary is in the folder

#
from .version import version as __version__
VERSION = __version__


from ._version import (  # noqa: F401
    library_version_number,
    library_version_string,
)
#

here is what's inside the init

flat gazelle
#

all integers from -3 to 256 are in an internal array and only ever exist once

#

it saves memory. ints are full objects just like everything else, and consider how many ones there are in an average python program

gleaming rover
#

yes

sturdy timber
supple geyser
#

is there any big-o-analysis plugin for vscode?

primal elk
#

You have to do that manually

supple geyser
#

cool, yet it seems very feasible to construct such plugin

native flame
#

sounds way too similar to the Halting problem to be feasible

#

imo

errant apex
#

is there a way to get into open source programming with python? like - i have a decent understanding of python (and dart) and want to contribute, but have no idea what projects i could do some simple things in

timid orbit
#

can someone explain to me why co_nlocals is used in various places if you could also just check PyTuple_GET_SIZE(co_varnames)?

pliant tusk
#

afaik its because its more optimized

#

ceval.c is one of the most heavily optimized parts of the interpreter

timid orbit
#

I guess that makes sense yeah

supple geyser
#

@errant apex dart/flutter front end and python backend, use python with database and make a pretty graph output in dart

errant apex
#

Huh I never thought about using dart outside of flutter actually.

supple geyser
#

oh well, afaik (I've just touched flutter a bit), the big pro of flutter is that it's good for making pretty lightweight 2d omniplatform apps, so go make some more computation heavy thing like ML in python and display it in flutter?

#

Imho fiddling with tkinter is just a waste of time

errant apex
#

i don't like tkinter

#

I'd use flask befrore tkinter

#

but I'm a professional mobile dev and work with flutter

atomic elbow
#

how do you create a URL?

errant apex
#

what do you mean create an url?

fresh bridge
#

So I've been using kivy and want to move it over to my phone... does anyone know how?

whole mulch
#

Which data structure is the Python list?

flat gazelle
#

an automatically growing and shrinking array

whole mulch
#

Dynamic array?

flat gazelle
#

it is an array of pointers to python objects if that's what you are asking

whole mulch
#

ok thx 👍

raven ridge
#

Yeah, it's a dynamic array of pointers to objects.

desert peak
#

is List similar to Rust's Vec? it basically has an internal buffer it manages and a length it keeps track of?

flat gazelle
#

yes

#

though all of its elements are always pointers

#

whereas in rust you can get other types too

desert peak
#

via traits yeah

grave jolt
#

whereas in Python there's always another layer of indirection

desert peak
#

and that's not allowed

desert peak
#

whoa, pypi updated their site css

#

it looks nice now

atomic elbow
distant girder
#

What do you call matrix , 2d array and other stuff

spark magnet
distant girder
#

No?

#

Matrix in python

#

Data types?

spark magnet
#

numpy has arrays

#

they are types

#

though 2d array isn't really a type exactly

distant girder
feral cedar
#

usually you'd make a matrix with a 2d array

#

it's just a list with lists in it

distant girder
#

What do you call those

feral cedar
#

lists?

native flame
#

list with lists in it

grave jolt
#

well, it depends on what you call "type"

distant girder
#

Hmm

grave jolt
#

does [1, 2, 3] have the same type as ["a", "b", "c"]?

#

Technically yes, but it's useful to think of the first one as list[int], and the second one as list[str].

#

And if you want to use type hints, those are "different types".

distant girder
#

What i mean by those is like asking what a data structure is or something alike

feral cedar
#

you're asking what a list is?

distant girder
#

No

#

Matrix and stuff they fall under the group of?

grave jolt
#

A matrix is just a mathematical object

astral gazelle
#

Matrix and what stuff

grave jolt
#

I don't think there's a common name for two-dimensional things

feral cedar
#

data structures?

astral gazelle
#

Containers?

distant girder
#

👀

#

I wanted to find more things like matrix and other things like that

astral gazelle
#

But theres no matrix thing in python

#

The closest you could get is to nest lists

#

Not even numpy uses a matrix class anymore

distant girder
#

How about 2d arrays and stuff

feral cedar
#

usually a matrix is another name for 2d array

#

and, "and stuff" is a bit too vague to answer

spark magnet
#

@distant girder can you say what the "other things" have in common?

#

what characteristics are you looking for?

distant girder
#

Like how would you store them

#

Is it data structure?

spark magnet
#

it is a data structure

distant girder
distant girder
#

the question here is it better to call a train of function until it meets a certain condition or is it better to have a while true and call the function every time it is needed

mint forge
boreal umbra
#

@distant girder you might need to be more specific about what you mean by "train of functions". Can you give examples that are brief enough that they fit in this chat?

boreal umbra
mint forge
#

😕

distant girder
#

i guess 'which software design is more efficient and better?' i guess?

boreal umbra
tidal marten
#

Are dictionary keys unordered?

native flame
#

not any more

unkempt rock
#

Not since python 3.7+, in which they retain insertion order

tidal marten
#

So the order depends on the insertion right?

unkempt rock
#

Yep, (at least in the cpython implementation)

tidal marten
#
names = {}
names['id'] = True
names['owner'] = True
names['description'] = True

# Then later use it like this...
some_data[names.keys()[0]]
#

.keys() are not subscriptable tho

unkempt rock
#

dict_keys objects aren't, but since theyre iterable you can cast it to a list and then index it

tidal marten
#

But that's inefficient

#

Hmm...

unkempt rock
#

Well, in this case you could just use next(iter(names))

tidal marten
#

I still can't access the keys by index though

#

I want to use it to populate a table, so like

some_data[tuple(names.keys())[column_index]]
#

Maybe I should just keep a dictionary for fast checking and a list to retrieve the key

raven ridge
dark temple
#

is there any other way to freeze time other than:
import time
time.sleep(#)

#

I ment for creating a game

#

no, i can show you, just wait a sec

#

thats a part of the code

signal tide
#

Can you open a help channel? This channel isn't the right one for this

dark temple
#

I was just wondering

#

thx

tidal marten
#

Most game I knew just waste CPU cycles to wait for the next frame render lol

novel vector
#

Pardon me cause I cant recall, but I remember reading something about initiating an empty list as a bad practice due to some possible weird issues with that, can anybody tell if its true and what this is all about?

flat gazelle
#

I have never heard of that

tidal marten
#

What should I do if I want to initialize resources (e.g. database connection, data, etc) for each process when using multiprocessing?

#

Do I just need to initialize them all before if __name__ == '__main__'?

undone hare
#

Probably not, since you don’t want to store them as global afaik

tidal marten
#

I was thinking of something like

from multiprocessing import Pool, freeze_support
import some_lib

process_resource = some_lib.get_some_resource()

def do_something(arg):
    process_resource.do_stuff(arg)

if __name__ == '__main__':
    freeze_support()
    with Pool(16) as p:
        p.map(do_something, [... some data])
#

some_lib is just a placeholder, forgot to put the from multiprocessing import Pool, freeze_support

#

You do know how sometimes you just write mockup codes that don't actually run but give a general idea of how they are expected to work, right?

tidal marten
#

Nope, I need some queues

tidal marten
#

Did that person get banned? Lol

primal elk
hasty lily
#

is this a more serious channel to ask questions in?

raven ridge
#

This is a channel for discussions about how the Python language itself functions

novel idol
#

can you ask questions about that?

#

my questions not so much of an error as explaining a behavior I found

feral cedar
#

as long as it fits in the channel topic

lone wagon
#

I’ve read Kite’s description for SystemError

#

And apparently it’s like an internal error

#

Raised when the interpreter finds an internal error, but the situation does not look so serious to cause it to abandon all hope. The associated value is a string indicating what went wrong (in low-level terms).

You should report this to the author or maintainer of your Python interpreter. Be sure to report the version of the Python interpreter (sys.version; it is also printed at the start of an interactive Python session), the exact error message (the exception’s associated value) and if possible the source of the program that triggered the error.

#

Lol

tidal marten
#

Is there any builtin libraries that can be used to check if a file is being used/written?

novel vector
#

You could just wrap around lsof on unix, idk for win

tidal marten
#

It seems that people are recommending using psutil for portability

#

I don't think it would work if the file is being used by root and the script checking for usage is not privileged

spice pecan
#

I guess you could go straight ahead and just try opening the file, catching a permission error

tidal marten
#

I'm trying to create a hot folder on Linux that will move files to other directory and process them there

novel vector
#

Just give them chmod 777 if you can and should work

tidal marten
#

I don't think that would work, my last two tries corrupted some files and crashed the whole system because I was doing it through NFS

#

Maybe I need to use inotify

novel vector
#

Thats totally not the 777s fault lol

tidal marten
#

The problem is not file permissions but race condition

novel vector
#

Sounds complicated, can you explain a bit?

tidal marten
#

I'm creating a hot folder on a Linux VM that would automatically process any files put in there. I'm accessing it through NFS. Ideally, I want to wait for the files to finish writing, then do things with it, and move it to another directory.

#

Currently, I just use systemd to watch the directory but it didn't wait for the files to finish writing.

#

It just crashed my system because it moved the file too fast and triggered some reference check error (probably race condition). NFS is a low-level thing, so that's bad.

novel vector
#

Hmm I don't think that this approach with checking for currently opened files can work, you will have some files opened in the append mode by lots of apps and you will never get them synced that way

#

I guess it depends how deep you want your sync, if its just user files then maybe it could work, but still I dunno how to do it too

raven ridge
#

Even on NFS, rename is atomic. So, write the files into the correct directory but with a .tmp name, then close the file, them rename it to remove the .tmp - and have the process that's picking up new files ignore ones with a .tmp extension

tidal marten
#

Hmm, that can work. But I won't be able to just drop files in the folder.

#

But I won't be able to use systemd and simple bash script for this

unkempt rock
#

Anyone have a recomendation for a good OpenCV course? I found a free one on Udemy and got down using the basic .xml sets that come with the library, but I feel like I will need to make my own data sets for what I want to do.

#

Maybe I just need to use the docs at this point?

elder pine
#

well yeah it would be a good idea to check out the docs too

#

there are some really good tutorials on youtube about OpenCV too

flat gazelle
#

@unkempt rock this server is specific to English, please keep that in mind.

#

@unkempt rock this channel is specifically about python, we have off topic channels such as #ot2-never-nester’s-nightmare if you want to ask about things unrelated to python

boreal umbra
#

!e I thought the loop variable for a for loop had its own special scope rule, but apparently not.

print('before the loop')
for letter in 'abc':
    print(letter)
print('after the loop')
print(letter)
fallen slateBOT
#

@boreal umbra :white_check_mark: Your eval job has completed with return code 0.

001 | before the loop
002 | a
003 | b
004 | c
005 | after the loop
006 | c
gleaming rover
boreal umbra
snow kindle
#

x = lambda value : [k for k, v in MyDict.items() if v == value]

liste_allValue = [(y:=x(v)) for k, v in MyDict.items() if k not in y]

By curiousity why this code is wrong and how I can correct it (I try to learn how use the symbol := in python) ?

#

y is not defined

boreal umbra
gleaming rover
boreal umbra
snow kindle
gleaming rover
snow kindle
#

So I try to understand why

boreal umbra
snow kindle
#

So who can response to me so I can understand or give my docs

#

Please ?

gleaming rover
desert peak
fallen slateBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

snow kindle
raven ridge
#

I've also seen plenty of context managers that do very wrong things if you try to call methods on them before __enter__ - at least as many as I've seen that will do bad things if you call methods on them after __exit__

desert peak
#

anyone know whether there's a good way to type a function as pure in python?

raven ridge
#

there's not AFAIK - typing focuses only on the parameter types and return types.

desert peak
#

ah, that's a little unfortunate

tidal marten
#

I have actually used the variable returned by context manager like this

with context_manager() as ctx:
     ...

if ctx.has_some_condition:
    ...
#

If context manager suddenly have its own special scope, I'll have a lot of broken codes 🤣

deft pagoda
#

i've used this too

tidal marten
#

This one is more concerning though:

# have some items
for item in [1, 2, 3]:
    print('ITEM:', item)

print('LAST ITEM:', item)  # This will work fine

# No items
for item2 in []:
    print('ITEM:', item2)

print('LAST ITEM:', item2)   # This will break
deft pagoda
#

might add a break and an else for those cases

boreal umbra
#

!e

# No items
for item2 in []:
    print('ITEM:', item2)

print('LAST ITEM:', item2)   
fallen slateBOT
#

@boreal umbra :x: Your eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "<string>", line 5, in <module>
003 | NameError: name 'item2' is not defined
boreal umbra
#

😮

deft pagoda
#
for item in maybe_nothing:
    something
    break
else:
    item = None
boreal umbra
deft pagoda
#

when you don't break

raven ridge
#

if the loop exhausts the iterable

#

so, no break and no return and no exception thrown

boreal umbra
raven ridge
#

right

boreal umbra
deft pagoda
#

it's a common idiom when you're looking for something

#

and you break when you find it, and else is for the cases you don't find it

boreal umbra
#

didn't guido say he wish he hadn't added it?

deft pagoda
#

maybe

boreal umbra
#

the else block for for loops?

deft pagoda
#

i like it

boreal umbra
#

he also said he regrets static methods, and I like that that option is there even if I don't use it

raven ridge
#

I use it more than the else block for try, heh

deft pagoda
#

i use the else block in try a lot --- i try to have just the single line i think will error in the try and in rest of the relevant code in the else

raven ridge
#

I use it occasionally, though I find that an early return is usually more appropriate

#

I'm glad it exists, but I don't use it terribly often.

brave badger
#

That's rather nice

raven ridge
#

3.10 is shaping up to have a lot of improved syntax errors, too

#

It should do a much better job of actually explaining what you got wrong in a lot of cases

lunar kestrel
#

Let us say there is a while loop, is there a method that jumps to the next iteration? Not break as it breaks out of the loop but I just want a method that immediatly starts the next iteration of the loop

native flame
#

continue

lunar kestrel
#

k ty

unkempt rock
#

@native flame

lunar kestrel
#

wait how about a double continue?

#

like a for loop in a for loop, and a want to continue for both loops

unkempt rock
#

how do u do that?

native flame
#

boolean flags, most likely 🙃

tidal marten
#

Set a flag?

lunar kestrel
#

ugh

tidal marten
#

This should be in programming 101

lunar kestrel
#

and I don't wanna set a flag

tidal marten
#

Tough luck, lol

lunar kestrel
#

i wanna see if there is a faster way

#

even though if finding the faster way takes more time

#

uk what I mean lmao?

tidal marten
#

You can directly manipulate the iterator?

boreal umbra
#

There's no purpose built way to continue an outer loop, but if you're leveraging functions properly then you almost certainly don't need to to get the desired behavior.

lunar kestrel
#

the iterator woudl just be for i in range(25)

#

like that

#

hwo would u directly manipulate the iterator?

tidal marten
#

Well, it is a tad bit more complicated after I thought about it. I was thinking of advancing the iterator but that's a C++ feature lol

west wind
#

hi need help in opencv terminology to segmentation of lungs image

tidal marten
#

Maybe try visiting computer vision specific channel/server

west wind
#

i tried to but cant able to fetch info similar to that

#

if you know kindly ping the server/channel so i can join there

fallen slateBOT
#

@tidal marten Per Rule 6, your invite link has been removed. If you believe this was a mistake, please let staff know!

Our server rules can be found here: https://pythondiscord.com/pages/rules

tidal marten
#

Uhh... mod?

west wind
#

thanks a lot

odd ether
#

@tidal marten I understand that your intention was good, but in that case you can contact us so that we can repost it or whitelist altogether. Circumventing our filters is not appreciated

tidal marten
#

Yeah, sorry

unkempt rock
#

Does anyone know how to properly configure pycharm to give proper code suggestions for libraries?
I keep getting errors like this
Cannot find reference 'CascadeClassifier' in '__init__.py | __init__.py'

#

I have the interpreter set up properly for my venv and I do get function suggestions for numpy

#

lol

#

I just figured it out

#

before I was just doing import cv2 but when I switched it to from opencv_python import cv2 it worked.

#

The answer came to me as I was trying to formulate my question hahah

#

My environment is now complete, feels good man

#

oof, that broke the import though :/

#

back to the drawing board

#

I feel like the solution has something to do with the __all__ thing in __init__.py but I just didn't understand what was going on there

#

Ok, this is so weird. Why would from opencv_python import cv2 even fix the Cannot find reference 'CascadeClassifier' in '__init__.py | __init__.py' problem?

#

even though its the wrong way to import cv2

visual shadow
# lunar kestrel wait how about a double continue?

This doesn't make sense to me. If you want to continue the outer loop, you're effectively discarding the state of everything inside, yes? So when you continue the outer loop the notion of continuing the inner loop no longer makes sense

rose stream
#

I'm trying to setup mypy in my workspace, for my django project... Should I use the config in the user folder or my workspace?

atomic egret
#

Why were the keywords await and async made builtin, but asyncio is just stdlib, rather than a builtin?

#

wouldn't it make more sense if they already added the keywords to make whole asyncio a direct builtin

spice pecan
#

You can use alternative async managers, not just asyncio

#

I'm assuming that's why, async and await let you work with async in general, asyncio is a particular event loop implementation

undone hare
#

async await are keywords, it is a parsing rule, you can’t import those

grave jolt
#

@atomic egret As 63n6h16 6ukrh4n said, asyncio is not the only event loop provider. There's also trio, for example.
Besides, a module in the stdlib is just a way of structuring the language. sys, threading, iteratools, and os are essential tools, but they're separated into their own modules, because bloating the already huge bulitin namespace doesn't help anyone.

radiant fulcrum
#

well asyncio is a lil more than that

#

it also provides all the coroutine handlers in the first place

grave jolt
#

yeah

radiant fulcrum
#

since they removed the ability to directly set the coroutine wrapper

#

which honestly i think is a lil annoying

peak spoke
#

Doesn't really make sense to export asyncio names to the builtins anyway, the await keyword is important to signify a possible context change which couldn't really be done without one

grave jolt
#

well, it could -- it was done with yield from, but that's just a bit meh.

spice pecan
#

Yeah, coroutines are essentially glorified generators

peak spoke
#

Well that's still a distinct keyword, if it was handled through a func for example it's be weird and not as noticeable

atomic egret
#

yeah, that does make sense, although it is a little weird to have these keywords without any default implementation for them

radiant fulcrum
#

good example is the gevent system

#

asyncio is the default implementation

#

but bearing in mind most languages dont provide a default system where its exported directly like that

#

as far as im aware JS is the only system that does it because the language is built around it

grave jolt
radiant fulcrum
#

I mean yeah

#

but JS was built entirely around the eventloop due to rendering design with browsers

#

JS' eventloop is an amazing example of making a language incredibly performent from the idea of the event loop

lunar kestrel
visual shadow
# lunar kestrel so I am in the inner loop and I want to continue the outer loop
for i in range(10):
    # logic pre loop
    for j in range(10):
        if j % 2 == 0:
            break
        # logic inside loop
    else:
        pass # logic post loop
``` you can build a construct like this instead. using the `for-else` on the inner loop guarantees the loop executes extra logic only if no break was encountered, and you can just break the inner loop when the condition is met.
lunar kestrel
#

waiiiiit for else is a thing?

visual shadow
#

Now having said that, i personally just find the option of using a flag easier to read, but this is an option if you wish to avoid flags. Note that the core theme is, instead of worrying about forcing a continue from inside the loop to outside, we simply switch to executing logic only when condition is never met

lunar kestrel
#

I have been doing python for half a year and I didn't knwo that

#

wow

visual shadow
#

oh yeah. for else is a syntax. the else is executed if and only if there was no break while iterating. (or rather, to be specific, if the iterator gets exhaused without breaking)

lunar kestrel
#

k, tysm! the more u learn 🙂

visual shadow
#

np 🙂

clear niche
#

Need to be able to message some people. I am re-taking python again, and I just need to clarify certain things when I am programming. Thanks

thorny wharf
#

Someone want to hop in a discord call with me and help me develop something

#

Using the Binance API?

clear niche
#

Eh, I can't. Won't be useful.

desert peak
sacred yew
limpid marten
#

@desert peak I actually read this today!

#

Wasn't a big fan of the writing style, but enjoyed the links to the various resources they had.

tepid wedge
#

Heres a question: does calling logging.debug(debugdata) cause any performance issues if the logger isn't listening to debug calls.

raven ridge
#

It has some performance impact.

#

You'd have to profile to find out if the impact is enough to cause issues.

tepid wedge
#

OK perfect I figured, and profiled it but just wanted to make sure it wasn't something else.

raven ridge
#

Every call still needs to access the logger and ask it if it's handling debug logs. That's not super expensive, but it's not free either

tepid wedge
#

Yeah it adds up on a pi with hundreds of times a second XD

boreal umbra
tepid wedge
#

Negligible hahahah

boreal umbra
tepid wedge
#

Yeah look I think going ham and deleting them is gonna be the way, they're not useful. Relics. Relics I say!

boreal umbra
#

I don't think they will be expensive though.

tepid wedge
#

Mmm. We enable/disable debug with a variable anyway so using it in an if is totally doable.

boreal umbra
#

There is actually such a variable built in to the language

tepid wedge
#

Logger.debug?

boreal umbra
#

Namely __debug__

#

It's True by default unless you run the program with the -O flag

tepid wedge
#

Ah. Yeah we look for an args and set logging.debug if we want it. My guess is that sets that value true

boreal umbra
#

The debug value that I just mentioned is part of the language.

#

You actually can't overwrite it

tepid wedge
#

Hmm.

boreal umbra
#

@supple geyser @limpid marten this is not an on-topic conversation for this channel. Please refer to the channel description.

atomic egret
#

I'm working with type hinting and I have a function that takes a python module, is there a module object that I could use to type hint that?

peak spoke
#

There's types.ModuleType

atomic egret
#

and what about a whole package?

peak spoke
#

I don't think python uses a different type for them

atomic egret
#

oh, interesting

#

alright, thanks

neon palm
#

Hello

#

Anyone can help me with Numba?

boreal umbra
boreal umbra
gleaming rover
#

This is strictly a discussion channel. I'm not sure what Numba is but perhaps you could ask in the topical channel that relates to it.
@boreal umbra is baaaaasically Cython for numpy

raven ridge
#

A JIT compiler for certain types of numeric code

unkempt rock
#

s

#

test

swift imp
#

i guess 634 is going to be voted on soon and thats why the mailing list has new drama on it?

boreal umbra
limpid marten
raven ridge
#

hm, I wasn't either - but I've never used it myself.

sly pine
#

In a code :
a = 9
b=9
c = a + b

print("c")

How will the python know the arithmetic operation that you're using ? How will it evaluate?

limpid marten
#

Do you mean print(c)?

raven ridge
sly pine
#

How does it understand?

raven ridge
#

the type defines its own __add__ operator.

#

!e ```py
class A:
def add(self, other):
print(f"adding {other} to {self}")
return NotImplemented

class B:
def radd(self, other):
print(f"reflective adding {self} to {other}")
return NotImplemented

A() + B()

fallen slateBOT
#

@raven ridge :x: Your eval job has completed with return code 1.

001 | adding <__main__.B object at 0x7f9ce6e36fa0> to <__main__.A object at 0x7f9ce6e36fd0>
002 | reflective adding <__main__.B object at 0x7f9ce6e36fa0> to <__main__.A object at 0x7f9ce6e36fd0>
003 | Traceback (most recent call last):
004 |   File "<string>", line 11, in <module>
005 | TypeError: unsupported operand type(s) for +: 'A' and 'B'
sly pine
#

i dont understand , im new to coding

raven ridge
#

Have you learned object oriented programming yet?

grave jolt
#

Like everything else in Python, you can find the implementation somewhere here: https://github.com/python/cpython, but it probably won't be of much value if you're just starting out.

sly pine
#

ok

raven ridge
#

unless you've learned OOP, it won't be possible to explain how + works in terms that make sense to you yet.

#

words like type and object just don't mean something concrete to you yet, and there's no way to explain the behavior without those.

sly pine
#

k

swift imp
#

TIL typical method/signature arguments are Positional OR Keyword and that you can refer to ones, even without a default argument through kwargs.

It's not until 3.8 that we can specify positional only

#

Specifying **kwargs in the signature allows it to accept abitrary extra keyword arguments

#

Mind blown

main flare
#

anyone knows how to read a map file in python.. with open(mapfile,'r') as fp:
This code is giving error

desert peak
#

+ is just a function call - that's pretty easy to understand

#

1 + 2 -> add(1, 2)

#

all of our special symbols are just sugar at the end of the day

wide shuttle
#

But that does not really explain how they work in Python

desert peak
#

it does, tho

wide shuttle
#

The part @raven ridge is referring to is that you can implement the functionality for those operators; it's defined in the types

#

When you're doing something like a + b, Python will first try a.__add__(b) to seek for an implementation in the type of a

desert peak
#

it doesn't take type theory to know that classes have methods

wide shuttle
#

I don't think it was meant much in that way, but to really understand how those operators work in Python, you'll get in touch with implementation special methods rather quickly

desert peak
#

just scrolled up and saw that person is new to coding entirely

wide shuttle
#

And that's the OOP part: The implementation of the operators depends on the special methods defined in Python's data model

desert peak
#

overriding dunder methods is pretty advanced for a newbie, I agree

raven ridge
# desert peak it doesn't take type theory to know that classes have methods

I didn't say it does. But if you don't know what "type" and "method" mean yet, you do need to stop and learn that before someone can explain to you how + works in Python. Every answer that's deeper than "it's built into the language" requires knowing that objects have types, types have methods, and + calls a method of each of the operands' types in turn to ask if they know how to add the two operands together. There's no way I can see to simplify it further than that.

raven ridge
# desert peak `1 + 2` -> `add(1, 2)`

yeah - but that function behaves like: ```py
def add(a, b):
ret = type(a).add(a, b)
if ret is not NotImplemented:
return ret
ret = type(b).radd(b, a)
if ret is not NotImplemented:
return ret
raise TypeError

neon palm
#

im trying to place files and a directory I created.. how would I ?

sacred yew
tidal marten
#

I kind of wish for a statically typed Python

#

Probably a Python front-end for LLVM, lol

boreal umbra
raven ridge
#

But it's definitely a bit weird, and doesn't quite fit modern Pythonic sensibilities.

#

And, worse - there's both the NotImplemented singleton which requests a fallback to the reflected operator, and the NotImplementedError exception class. People mix these up all the time; it's a real footgun in the language.

tidal marten
#

That's why I was not able to raise NotImplemented

#

I thought NotImplemented is just a shorthand for NotImplementedError()

fallen slateBOT
#

:incoming_envelope: :ok_hand: applied mute to @merry pewter until 2021-02-08 06:16 (9 minutes and 58 seconds) (reason: duplicates rule: sent 4 duplicated messages in 10s).

visual shadow
#

Ooh, very cool, never heard about NotImplemented singleton before.

#

As for language design, in general I think it's okay for language designers to use footguns as long as programmers don't have to. I think some of it is unavoidable, and some of it is practical

tidal marten
#

This might not be exactly on topic, but which percentile numbers are useful when comparing thread performance based on thread count?

#

I ran my threaded code at least five times with five different thread count. I included the initial cold run though.

tidal marten
#

What do you mean?

limpid marten
#

This is thread performance in Python?

tidal marten
#

Yeps

#

My machine is 2c/4t so that might explain the sweet spot on 4 threads

#

I did have some mutex locks as well as IO operations

errant apex
#

while on topic of threads

#

what is the point of threads

#

wait i answered my own question while asking it

#

is the point to do things parallel when you need the results at the same time?

#

but then again, if i do some sorting algorithm shenanigans, and i put them into 2 threads, they are gonna take as long as if i put them behind each other, right?

#

if i wanted SPEED i'd use multiprocessing

limpid marten
#

Threads in python should only be used for IO bound tasks.

grave jolt
#

...or for CPU-bound tasks that lift the GIL, like some stuff in numpy and PIL

peak spoke
#

Most io bound tasks are mostly taken care of by async, but it can't handle anything that's blocking. Sometimes with a cpu bound workload you do need the concurrency, but processing parallelism isn't important which is a great use case for threads. async would stall at the cpu bound workload, and multiprocessing would add unnecessary overhead

visual shadow
peak spoke
#

I'd say most thread uses in python now are through out of python gui libs, extensions that can do work without holding the gil and executing blocking tasks while working with async

errant apex
#

but don't processes run on different cores at the same time like
ab
ab
ab
ab
while threading is just ababababababa

visual shadow
#

however, i find myself that in python its mostly multiprocessing or async

errant apex
#

will multiprocessing code even run on non-unix machines

visual shadow
#

i think youre right in case of python specifically. im not a 100% sure though

errant apex
#

iirc there was something like that

visual shadow
#

python has to let the python interpreter manage threads, so i suspect it's the case

peak spoke
#

Without synchronization it may do things like aabb etc., as the threads will request the gil after a certain amount of time (5ms I think) and the thread that's currently holding it will have to release it

errant apex
#

ok just looked it up it only doens't run on mac

peak spoke
#

But yes, it'll be serial in the sense that only one thread will ever be executing your python code no matter how the os manages them

errant apex
#

can i just do

#

!multiprocessing

#

huh

#

how can i bot command call the docs

unkempt rock
#

Ok so pretty much

#

this is a question

#

my code worked fine

#

2 seconds ago

#

i didnt adjust it

#

i just closed the running test

#

and now it wont show the gui

#

and wont show any errors

raven ridge
errant apex
desert peak
#

hey folks, how would you enforce a factory method for a type over its constructor?

#

I need the ctor due to inheritance, but it leaves the class in an invalid state

#

obviously documentation is one way, but is there a way to detect a calling class method and throw an assertion error?

raven ridge
#

Can you elaborate on the "due to inheritance" bit?

desert peak
#
class T(V):
  def __init__(self):
    super().__init__('param')

  @classmethod
  async def create(cls):
    return T()

super dumbed down example

flat gazelle
#

if you omit that ctor, the ctor of the superclass gets called anyway

desert peak
#

my class members require async fn calls and can't be used in the ctor

desert peak
flat gazelle
#

ah

desert peak
#

for reference, I'm extending one of fastapi's oauth2 schemes

#

to work with an internal security implementation

raven ridge
#

Why not:

class T(V):
    def __init__(self):
        raise TypeError("call create")

    @classmethod
    def create(cls):
        obj = object.__new__(cls)
        super().__init__(obj)
        return obj
flat gazelle
#

was about to type that

desert peak
#

because I didn't know that pattern 🙂

flat gazelle
#

will super() work in a classmethod

desert peak
#

thanks a lot @raven ridge

#

I'll test it out

flat gazelle
#

the two arg form definitely will

raven ridge
#

I believe it works

#

But if it doesn't, yeah, two arg.

flat gazelle
#

I would also suggest erroring out __init_subclass__, since extending this class won't work

raven ridge
#

Hm. I think it will!

#

Depending on how you extend it, at least.

desert peak
#

my main goal is that you are forced to use the factory method

#

so my coworkers don't ignore my documentation and do something dumb

raven ridge
#

If you keep the constraint that create must be called, and subclasses create calls the super create, it should work.

#

Though multiple inheritance won't.

flat gazelle
#
class T(V):
    def __init__(self):
        if not getattr(self, '__proper', False):
            raise NotImplementedError('use the factory method make to create an instance of this class')
        super().__init__()

    @classmethod
    def make(cls):
        t = object.__new__(cls)
        t.__proper = True
        t.__init__()
        return t
``` would let you keep a ctor, so subclassing will work even if a ctor is used
#

and missing a supercall to make would also error out

desert peak
#

does super() work in any method call?

#

I thought it was restricted to ctor for some reason

#

only place I ever see it, I guess

flat gazelle
#

it works in any method

raven ridge
#

!e ```py
class A:
pass
class B(A):
def init(self):
raise NotImplementedError("call create()")
@classmethod
def create(cls):
obj = object.new(cls)
super().init(obj)
return obj
class C(B):
@classmethod
def create(cls):
obj = super().create()
obj.attr = 5
return obj
print(C.create().attr)
print(C())

fallen slateBOT
#

@raven ridge :x: Your eval job has completed with return code 1.

001 | 5
002 | Traceback (most recent call last):
003 |   File "<string>", line 18, in <module>
004 |   File "<string>", line 5, in __init__
005 | NotImplementedError: call create()
desert peak
raven ridge
#

subclassing seems to work fine with my approach.

agile briar
#

Hello i need some help iwth the ursina game engine

agile briar
#

k

desert peak
raven ridge
#

what's the name of the class this is in?

desert peak
#

JWTBearer

raven ridge
#

then, try super(JWTBearer, scheme).__init__(scheme, ...)

flat gazelle
#

yeah, it does work if you use create instead of __init__

#

my method should in theory allow the use of __init__ in a subclass

desert peak
#

unexpected arg it tells me

#

I never do any subclassing in python, always lose my way

raven ridge
#

test is better than screenshots - what arg is it saying is unexpected?

desert peak
#

the highlighted

#

let me get a mvp real quick

raven ridge
#

and OAuth2PasswordBearer.__init__ does take tokenUrl as a parameter? Hm.

desert peak
#

yep

raven ridge
#

!e ```py
class A:
def init(self, arg):
self.arg = arg
class B(A):
def init(self):
raise NotImplementedError("call create()")
@classmethod
def create(cls, arg):
obj = object.new(cls)
super().init(obj, arg)
return obj
print(B.create(5).arg)

fallen slateBOT
#

@raven ridge :white_check_mark: Your eval job has completed with return code 0.

5
raven ridge
#

seems like it should work...

#

is this a linter getting confused, or the interpreter itself raising a TypeError?

desert peak
#

about to check. intellij has been flakey in these kind of scenarios

#

nope, get an error at creation

#

oh weird

#

"got multiple values for argument tokenUrl"

raven ridge
#

uh - ok. so it thinks you passed two values for it, probably one positionally and one by keyword?

desert peak
#

yeh

raven ridge
#

What's the signature for OAuth2PasswordBearer.__init__ ?

desert peak
#

I get rid of the first arg and highlighting goes away...

raven ridge
#

ah, yeah, that seems right.

#

the __init__ is already bound to an object by the two-arg super call, so you don't need to pass that argument again.

#

So it should be correct to do ```py
super(JWTBearer, scheme).init(tokenUrl=token_url, auto_error=False)

desert peak
#

ahhh, makes sense

#

I wish super was documented better

#

they literally point at some blog to see its semantics in the docs

#

the other day I was trying to figure out multiple inheritance and gave up

raven ridge
desert peak
#

yep, that's the link in the docs

#

no where in that entire post do they use super with args

raven ridge
#

ah. I don't know of anything that explains it better than that - that's written by one of the CPython core developers.

desert peak
#

I understand super gets the superclass, but I wanted more granular control which is what I understood the arguments to grant

#

just wasn't sure how

raven ridge
#

it doesn't get the superclass, it gets the next class in the MRO

#

which may be a superclass, or may be a sibling class.

desert peak
#

well, yeah, but if you only have one inheritance

#

that's the superclass

raven ridge
#

not necessarily

#

even if you've only got one parent class, if someone derives from you then it's possible that a call to super() from within your code will call a sibling class rather than your superclass.

desert peak
#

that bit's going a bit over my head atm

#

I've been staring at security documentation all day so I'm a little fried 😅

raven ridge
#

e.g., if you've got: py class A: ... class B(A): ... class C(A): ... then a call to super() in either B or C will delegate to A, just like you think it will. But if someone comes along and does: py class D(B, C): ... then calls to super() inside a method that D inherited from B will delegate to C, not to A.

desert peak
#

oh, interesting

#

which I'd consider correct behavior, but yeah, confusing

#

at first glance

raven ridge
#

!e ```py
class A:
def meth(self):
print("A meth")
class B(A):
def meth(self):
print("B meth")
super().meth()
class C(A):
def meth(self):
print("C meth")
super().meth()
class D(B, C):
pass

print("On B:")
B().meth()

print("On C:")
C().meth()

print("On D:")
D().meth()

fallen slateBOT
#

@raven ridge :white_check_mark: Your eval job has completed with return code 0.

001 | On B:
002 | B meth
003 | A meth
004 | On C:
005 | C meth
006 | A meth
007 | On D:
008 | B meth
009 | C meth
010 | A meth
desert peak
#

Isn't the general philosophy one of composition, not inheritance, these days?

raven ridge
#

it's generally more maintainable in most cases, but inheritance, even multiple inheritance, has its uses if planned carefully.

desert peak
#

so in my example, super(JWTBearer is resolving OAuth2PasswordBearer?

raven ridge
#

yep.

desert peak
#

is there a way to specify a concrete class?

#

just calling the class's __init__ with self = scheme?

grave jolt
#

yep

raven ridge
#

super(cls, obj) scans obj.__mro__ until it finds cls, and then returns a proxy to the next class in the MRO after that.

desert peak
#

gotcha gotcha

#

this is making a lot more sense now

grave jolt
#

super is specifically to avoid specifying a concrete class, because you might even not know it

raven ridge
#

so given an object with an MRO of [D, B, C, A], super(B, self) will find C.

desert peak
#

I wish there was a Self alias like Rust has

raven ridge
#

The most confusing thing about super is its name. It sounds like it looks up the superclass. It doesn't. It looks up the next class in the MRO, which is frequently but not always a superclass.

desert peak
#

yes, the source of my frustration this week

#

I'm glad this forum exists, this community is great

#

side-note: why object.__new__?

#

because init will throw?

lone wagon
#

Then again, there’s pluggy

desert peak
#

also: is there a good solution for async destructors?

#

I guess getting the event loop and calling functions that way?

sacred yew
#

make it an async context manager?

#

idt you can rely on any globals existing when destructors are called

desert peak
#

!ot @eternal forge

fallen slateBOT
grave jolt
#

Please don't spam random images in on-topic channels.

#

i.e. PEP 634, PEP 635, PEP 636

desert peak
#

hnnnnng

#

python finally gets its 1990 feature of switches

#

(kidding, of course)

#

oh wow, they might even have them in time for py310

#

that makes me very excited for next year

grave jolt
desert peak
#

ugh, going insane. how do you finalize an async attribute? aclose doesn't do it, not allowed to use await in __del__

#

the event loop seems to already be closed by the time __del__ is called

#

so I'm leaking connections when my app finishes

raven ridge
#

You can't await in __del__ because there's no guarantee about when and where __del__ gets called. It could get called in a thread that doesn't have an event loop, it could get called after the event loop has been closed, it could be called after the __main__ script has finished. Technically it's not guaranteed to be called at all, in fact.

#

there was talk for a while about moving all garbage collection into its own thread - hasn't happened, but it could.

#

sounds like you should probably be counting on a context manager to free up resources for this, rather than __del__. async with, that is.

desert peak
raven ridge
#

Sounds like the session pool should be under the user's control, and they should pass it into you, then.

desert peak
#

the pool exists so I can query my adfs infra for new keys

#

it's entirely internal to the class

raven ridge
#

Then yeah, you'd need to create a new pool on every call.

#

You can do something like keep a reference to the event loop that you were created with and use it for scheduling a callback with call_soon_threadsafe from __del__, maybe. But ew.

desert peak
#

yeah, after more thought, an extra sessionpool every few months isn't impactful enough to engineer a solution around it

swift imp
#

All hail the overlord pattern matching

flat gazelle
grave jolt
#

well, math had everything earlier

#

like lambdas

swift imp
#

I can't if there's going to be a new dunder or not

grave jolt
#

there should be

#

for matching on user classes

#

and namedtuples and such

swift imp
#

__match_args__

#

I saw it's on there for namedtuples and dataclasses but can't find anything about it being an official dunder

desert peak
#

that's the thing with duck typing, it doesn't matter what's official!

swift imp
#

Like theres dunders related to say abc that don't do anything unless you use that meta

desert peak
#

def __iter__, I'm an iterator now!

swift imp
#

That's bc for loops and stuff know to look for it

#

I swore I saw a deferred idea about it

desert peak
#

Q for anyone: does python cache module execution?

#

using python -m app gives me different results from uvicorn app.main:app

#

even though the code is the same

#

going. insane.

#

false flag: intellij is dumb

spark magnet
#

does that mean you solved it?

desert peak
#

I did. IntelliJ left a zombie process that kept the port bound, but rebinding that port didn't give me an error or anything @spark magnet

#

but also didn't update my web server

sacred yew
#

wait

#

"intellij"

#

as in a java ide?

#

@desert peak

boreal umbra
#

Am I seeing that the patma pep was accepted today?

boreal umbra
# grave jolt yep

I haven't looked at very many patma examples but I'm scared that I'm going to be disappointed by all this

desert peak
grave jolt
#

But I guess you can't really form an opinion before you actually use the feature extensively.

desert peak
#

people are insufferable and loud people averse to change usually win those arguments because they're so insufferable

grave jolt
#

well, patma was accepted despite loud people

desert peak
#

yes

#

it almost wasn't, however

grave jolt
#

Maybe I don't find it disturbing because I have used a little bit of Haskell

desert peak
#

if you're familiar with anything that has real pattern matching, it's dreamy

#

not just switch statements

feral cedar
#

in rust, they are very powerful

desert peak
#

hm, my issue still persists

#

I hate intellij

grave jolt
#

You can always try VSCode 🙂

desert peak
#

I don't have pylance at work

#

and the default sucks ass

grave jolt
#

You can use mypy as a type checker as well

desert peak
#

Jedi is at least tolerable, but Pylance was a great jump in experience for me

#

er, Microsoft*

#

Jedi was the crappy one

grave jolt
#

I'm using Pylance

desert peak
#

Yeah, I work at a big place, no plugins unless they go through procurement

grave jolt
#

oh wow

desert peak
#

for whatever reason, the task runner in intellij is ignoring my fastapi dependencies. the plot thickens but honestly, I'll just go back to console debugging instead
Nuking .idea/ from orbit seems to have resolved everything

feral cedar
#

but..but..your workspace configs 😦

desert peak
brave badger
swift imp
#

I really want to see better patmat examples. Reading the peps is too abstract

raven ridge
#

!pep 636

fallen slateBOT
#
**PEP 636 - Structural Pattern Matching: Tutorial**
Status

Final

Python-Version

3.10

Created

12-Sep-2020

Type

Informational

raven ridge
#

Gives some pretty good examples.

#

634 is very abstract, but 636 is pretty concrete.

tidal marten
#

I feel like this is a lot of syntactic sugar

swift imp
#

How long do you think it'll take anaconda to get it's hands in it

#

Conda is still on 3.8

grave jolt
#

I guess there aren't too many examples of how patma could improve code, because library writers wouldn't design stuff so that it looks and feels ugly, so they choose other, less optimal (for the given problem) approaches (like subtype polymorphism instead of a switch).
For example, this function https://github.com/decorator-factory/python-fnl/blob/master/fnl/fnlx.py#L37 and this https://github.com/decorator-factory/python-fnl/blob/21a1a67d20eeb20f3c09dfa7e9de758c4914bd36/fnl/bindings.py#L133
and https://github.com/gurkult/py-gurklang/blob/master/gurklang/prelude.py#L538 (and many functions in that project) could all be improved by pattern matching. Basically, everything that deals with parsing or language evaluation or otherwise interpreting a tree.

tidal marten
#

Oh gosh, DIY pattern matching looks awful

grave jolt
#

yep 🙂

swift imp
#

Man people at work accuse me of writing too complicated of shit at work and then I see that stuff in repos and like ???

grave jolt
#

well, that's a bit of an esoteric bootleg

#

I wouldn't use that in actual production code

tidal marten
#

Yeah

grave jolt
#

How would you write that function without pattern matching, though?

tidal marten
#

Which one?

grave jolt
#

The first one

swift imp
#

People at work want to stick to strictly procedural with lots of type checks, like some weird java or c#

tidal marten
#

Probably with a lot of IFs and isinstance/issubclass

tidal marten
#

Ahh, that's better lol

#

Wouldn't it incur a lot of runtime overhead though?

grave jolt
#

the with thing? Yeah, a few function calls.

swift imp
#

Interesting using of context manager though

#

Honestly I never ever use that

grave jolt
#

yeah, it's a combination of pretty obscure things

tidal marten
#

Maybe instead of using string, you can create some representation classes and evaluate the input against that?

#

Still a lot of overhead though

grave jolt
#

Well, in this case, it's not a very high-load system

#

Although... that probably wouldn't be fixed by patma, lol

tidal marten
#

Idk, I also have some distaste with languages that do weird syntactic stuffs. Probably why I dislike those kind of things.

molten pier
#

What would be a good coding project? Easy to code(as in not to difficult for a beginner), but a cool program

grave jolt
tidal marten
#

The one thing I know is how Angular do dependency injection through the constructor, also some Kotlin and Dart stuffs

#

Anything that is not traditional like C/C++ lol

grave jolt
#

Bash is the language with the most cursed syntax and semantics I've ever seen.

#

yet people are able to use it to do some actual stuff

tidal marten
#

Yeah, I just have to overcome that first "eww" instict

#

Bash doesn't have to be pretty though

grave jolt
#

Well, it just has very confusing control flow constructs IMO

tidal marten
#

My problem with the newer languages mostly stems from the barrier to entry, not from scratch, but if you already have experience with other languages

grave jolt
# grave jolt Well, that's just an issue of familiarity, maybe

Last year I just looked into lots of different languages at least at a very superficial level (the tutorial): C++, Clojure, Rust, Haskell, OCaml, Agda, JavaScript, TypeScript, Elixir. maybe something else. After seeing lots of different, sometimes completely alien, things I'm a bit more open-minded about language features or just how they look.

#

I even liked some parts of JavaScript

swift imp
#

Anyone got good applications of context managers that arnt insane?

#

Or that are useful but not the typical use case

tidal marten
grave jolt
#

@swift imp contextlib.suppress uses exception swallowing (which was a new thing for me) to suppress exceptions.

tidal marten
swift imp
#

I feel like exception handling is a normal use case though

grave jolt
swift imp
#

Not that I don't appreciate your responses

tidal marten
#

Closing resources gracefully

grave jolt
#

Is monkeypatching a non-typical use case for you?

#

Testing libraries use context managers to temporarily patch module or class members.

swift imp
#

That's neat

grave jolt
#

Like here

#

!e

import time
time.sleep(42)
fallen slateBOT
#

@grave jolt :warning: Your eval job timed out or ran out of memory.

[No output]
grave jolt
#

Well, you might have missed that, but the bot uses it to signify that code is being executed.

frigid cairn
#

!e

fallen slateBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

magic hedge
#

Re. people mistaking the topic of this channel, has 'language-discussion' been suggested as an alternate name?

feral cedar
#

i think a variant of that was used before

grave jolt
#

It's hard to make a good name, and it's always a compromise.

#

It was renamed because automated-testing attracted selenium questions about webscraping.

#

(one of the reasons)

magic hedge
#

I got the sense of that. Figured I'd see if that name in particular had come up before.

grave jolt
#

@magic hedge Another example is how it's #async-and-concurrency and not #concurrency-and-parallelism because it just doesn't fit

#

or how it's #c-extensions, even though you can also write extensions in C++, Rust, Forth, Fortran and probably many more languages

raven ridge
#

Forth? 👀

grave jolt
feral cedar
#

Fith?

magic hedge
grave jolt
magic hedge
#

Cheers

raven ridge
#

very little of Python's C API actually requires C. Most of it is a C-callable ABI, which can be used from any language that can call C functions.

magic hedge
#

Could muddy the waters of 'design' by shifting to #language-design

raven ridge
#

a handful of public interfaces actually are macros, which really do require a C compiler - those are the exceptions.

grave jolt
#

In the end, it's just pushing around bytes, right?

magic hedge
#

Back to assembly we go!

raven ridge
#

well, let's see. You need to be able to create a dynamic library (.so or .dylib or .dll or whatever). That dynamic library needs to expose a C-callable symbol named PyInit_<modulename>. That needs to define a structure in a particular memory layout, and call some C functions exposed by the interpreter (which could be found through dlsym or the like)

magic hedge
raven ridge
#

AFAIK CPython supports every version of standard C

#

the first C standard was C89, and lots of programs are still written for C89 compatibility.

grave jolt
#

I think the recent versions now require C99?...

raven ridge
sturdy timber
#

Doesn't trying to get patma into 3.10, just having accepted the PEP today, seem a bit last-minute? From what I can see the first 3.10 beta has it's expected release in May, which seems pretty soon to get everything in by.

#

I guess they do already have an implementation and have been working on it for a while though

undone hare
#

No major implementation can be added in beta, so I don't think we will have it to 3.10

sturdy timber
undone hare
#

Hmmmm

#

I am not sure

radiant fulcrum
#

the whole match statement stuff seems rather rushed

#

not entirely sure what they're doing tbh

visual shadow
#

Adding code that's easy to write and hard to read.

#

I love pep636, it's very well written, and yet it's still hurting my brain trying to imagine walking into a code base blind and seeing pattern matching.

#

I think I'll need to spend some time reading the links fix error shared above when I get a chance.

craggy tree
#

should i use excel i am trying to track my expenses in python from another document

#

im on mac

grave jolt
undone hare
#

I have yet to see the walrus in prod app tbh

grave jolt
#

well, walrus is a rather small addition

#

I could totally live without it, if the time dedicated to it would be spent on patma bikeshedding, or subinterpreters, or something else

unkempt rock
#

From #help-donut , what is going on here?

In [11]: [5, 6] is [5, 6]
Out[11]: False

In [12]: id([5, 6]), id([5, 6])
Out[12]: (4363340736, 4363340736)
undone hare
#

Hmm

peak spoke
#

The address (or object, not sure if there is an optimization) can be reused after the lists are dereferenced

#

the is check needs both alive, id only keeps the current one

undone hare
#

What is the dis for the latter?

peak spoke
#

should be build list and a call to id twice, both of which are completely separate

#

While the first one has to build both lists and then compare

unkempt rock
#

Ah that makes sense

#
In [16]: id(list(range(5, 7))), id(list(range(5, 7)))
Out[16]: (4362631488, 4362631296)

Doesn't really happen when list literals arent used

#

...which leads me to believe its an optimization?

paper echo
#

Also ugh pattern matching

#

I should have said something on the mailing list

mint forge
#

but that returns true for tuples though

paper echo
#

I feel like I missed my chance to speak up

unkempt rock
peak spoke
unkempt rock
#

Which would mean list() doesnt use PyList_New?

undone hare
#

What is the dis for the latter?

boreal umbra
#

!e

from dis import dis
print(dis(lambda: []))
print(dis(lambda: list()))
fallen slateBOT
#

@boreal umbra :white_check_mark: Your eval job has completed with return code 0.

001 |   2           0 BUILD_LIST               0
002 |               2 RETURN_VALUE
003 | None
004 |   3           0 LOAD_GLOBAL              0 (list)
005 |               2 CALL_FUNCTION            0
006 |               4 RETURN_VALUE
007 | None
boreal umbra
undone hare
#

Yeah, one is a BUILD_LIST while the other is a native call

#

I forgot to run it lol

boreal umbra
#

It's just trading one abstraction for another. I don't know if I've learned anything.

undone hare
#

!e

from dis import dis
print(dis("[1, 2] is [1, 2]"))
print(dis("id([1, 2]), id([1, 2])"))```
fallen slateBOT
#

@undone hare :white_check_mark: Your eval job has completed with return code 0.

001 |   1           0 LOAD_CONST               0 (1)
002 |               2 LOAD_CONST               1 (2)
003 |               4 BUILD_LIST               2
004 |               6 LOAD_CONST               0 (1)
005 |               8 LOAD_CONST               1 (2)
006 |              10 BUILD_LIST               2
007 |              12 IS_OP                    0
008 |              14 RETURN_VALUE
009 | None
010 |   1           0 LOAD_NAME                0 (id)
011 |               2 LOAD_CONST               0 (1)
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/wubufesone.txt

undone hare
#

Hmm okay

#

That’s interesting

grave jolt
#

not sure how dis can help understand the behaviour

pliant tusk
#

in cpython there is an internal free lists array. when a new list is made (using the BUILD_LIST instruction) it can use a list from that free list, as the runtime knows the length of the resulting list by the time it gets to there. list(arg) uses PyType_GenericAlloc instead of PyList_New(size), then calls list____init____impl(list_obj, iterable) which handles putting the argument of list(arg) into the new list

#

basically, the reason why list(arg) doesnt use the free lists array is because it initializes the new list before it knows the size of the arg passed in

#

@unkempt rock ^ that is why

true ridge
#

so there is a really high (I'd even say it is certain) that we will have patma in 3.10

#

Isn't that true for just about any new big addition, like async/await or type hints?
These are much much smaller compared to patma. I'd say it is the biggest change in the last decade or so.

undone hare
#

Oh that's cool

grave jolt
#

btw, what's your opinion on patma?

swift imp
#

If I wanted to traverse through code without executing it would ast be the way to do it?

raven ridge
#

Yep.

spark magnet
#

there are other ways depending on what aspect of the code you want

swift imp
#

So basically I have a few dictionaries, who ultimately get merged together into one, but for each dict I would like to know where each key is being populated last prior to final updating.

#

Basically the path through the code isn't completely determinant

#

At least easily

#

So I would like someway of seeing the intermediate steps without having to actively debug

blissful comet
#

Anyone here interested in writing python -> another statically typed language transpiler?

undone hare
#

Except that it is against Teams’ ToS

thick wagon
#

i am

#

me

radiant fulcrum
#

that is already sorta implemented btw

#

It doesnt work for everything though due to python types

#

but if you have purely numerical calculations Numba can jit compile it to C code with LLVMlite

mint forge
topaz rune
#

Oh

#

sorry

#

im really sorry for that

boreal umbra
#

@topaz rune No problem; you can always check out the channel description for each channel before commenting there for the first time.

blissful comet
undone hare
#

That honestly sounds like too much indirection

blissful comet
#

Are you concerned about compile times or dependency on another language ecosystem?

undone hare
#

More indirection means more chances to mess up some little details here and there

#

Rust is totally different from Python

blissful comet
#

Rust is more concrete and more restrictive than python. To make it work python is constrained to a small subset as well

#

I would start from the tests/cases and expected outputs to get a sense

limpid marten
radiant fulcrum
#

Python generally would be alot of pain to transpile to from python

#

Python uses ALOT of callbacks

#

and rust is a language where you generally dont have anywhere near that same amount of callbacks or non at all

tacit hawk
#

can OrderedDict be used as a deque?

desert peak
#

rust makes liberal use of closures (callbacks)

radiant fulcrum
#

Not in the same way python makes heavy use of it

tacit hawk
radiant fulcrum
#

ig the question is why would you want a OrderedDict over a Deque to be used as a deque

orchid pagoda
#

can someone help me my code say some thing is wrong btw its my first code

#

from pyatuogui import *
import pyautogui
import time
import keyboard
import random
import win32api,win32con

#Tile 1 Position: X: 1019 Y: 471 RGB ( 1, 0, 1
#Tile 2 Position: X: 754 Y: 471 RGB: ( 54, 159, 198)
#Tile 3 Position: X: 840 Y: 471 RGB: (164, 221, 255)
#tile 4 Position: X: 934 Y: 471 RGB: (148, 178, 254)

def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(0.01)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)

while keyboard.is_pressed ('q') == False:

    if pyautogui.pixel(1019, 471) [0] == 0:
        click(1019, 471)
        if pyautogui.pixel(754, 471) [0] == 0:
        click(754, 471)
        if pyautogui.pixel(840, 471) [0] == 0:
        click(840, 471)
        if pyautogui.pixel(934, 471) [0] == 0:
        click(934, 471)
fallen slateBOT
#

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

orchid pagoda
#

its a bot i try to make pls help

#

!code

hexed spire
#

Read this, don't press the trash bin

radiant fulcrum
#

mm Ig

#

but that would rely on the keys being hashable which might be a downside

tacit hawk
#

yeah in my use case all keys are hashable

fading cargo
#

Hello everyone

#

Just joined, I'm a python newbie so here to hopefully get better...

tacit hawk
#

I used timeit to test insertion on ordereddict, it took minutes, but in deque it's the search that takes minutes

#

search test

$ python -m timeit -n 500 -s "from collections import deque; q = deque(); [q.append(i) for i in range(1000)]" "[1001 in q for _ in range(1000)]"
500 loops, best of 5: 12.6 msec per loop
$ python -m timeit -n 500 -s "from collections import OrderedDict; q = OrderedDict(); [q.__setitem__(i, None) for i in range(1000)]" "[1001 in q for _ in range(1000)]"
500 loops, best of 5: 49.4 usec per loop
#

insertion test

$ python -m timeit -n 500 -s "from collections import OrderedDict; q = OrderedDict()" "[q.__setitem__(i, None) for i in range(1000)]"
500 loops, best of 5: 187 usec per loop
$ python -m timeit -n 500 -s "from collections import deque; q = deque()" "[q.append(i) for i in range(1000)]"                
500 loops, best of 5: 74.3 usec per loop
raven ridge
#

The insert only being 2.5x slower isn't so bad, honestly

tacit hawk
#

in my use case I do more seach than insert

warm narwhal
#

Which text editor do you use ?

blissful comet
radiant fulcrum
blissful comet
# radiant fulcrum What sort of coverage r we looking at STD lib wise?

It doesn't compile much beyond what's in the tests/cases directory. But if you're looking at examples on rosettacode, a lot of that should be mechanically transpiled between languages.

binit example is from rosettacode.

Coverage of stdlib: the harder problem is translating datetime.datetime.now() or uuid.uuid4() to another language. I'm thinking of some type of a plugin system where stdlib is transformed.

still river
#

that's great

grave jolt
#

Probably the most controversial and debated feature in Python ever.

gleaming rover
#

🥴

raven ridge
#

The stakes on this one are way higher. It's not hard to pretend walrus doesn't exist, but this one will be a lot harder to ignore

gleaming rover
#

(it's a joke)

raven ridge
#

Realistically, that probably wasn't a bad outcome. I think the Python ecosystem is in a better place now.

grave jolt
raven ridge
#

And it's already up to 1.0!

swift imp
#

That's a funny ass plugin

#

I'm still waiting on that keyword indexing

feral cedar
grave jolt
#

I'm sure someone will fork CPython and make match segfault or something