#internals-and-peps

1 messages · Page 126 of 1

deft pagoda
#

i have some tight numpy loops where i'm saving these slices, i was slightly surprised that slice(None) wasn't always the same object

prime estuary
#

Slices currently can't be constant folded - there's an open bug on the topic, the issue is that they aren't hashable right now, so you can't put them in a set and so they'd need special handling to do the deduplication.

#

And letting them be hashable would be bad since then mydict[:4] = ... would be misleadingly valid.

prime estuary
#

Yep, would be a disaster if that worked. There's a PR that instead uses (slice, start, stop, step) tuples during compile, which just needs to be reviewed and merged.

paper echo
#

i wouldn't say a "disaster" but i would not at all look forward to the help channel questions if they made slices hashable without checking that case

prime estuary
#

Indeed.

paper echo
#

unrelated, but i feel like it's a shame that Generator.close doesn't actually return the return value after handling the GeneratorExit:

from typing import TypeVar
from collections.abc import Generator

A = TypeVar('A')

def pushme() -> Generator[A | None, A, A | None]:
    print('Starting.')
    value: None | A = None
    while True:
        try:
            value = yield value
        except GeneratorExit:
            print('Goodbye!')
            return value
        else:
            print(f'{value=}')

def main():
    try:
        gen = pushme()
        gen.send(None)
        gen.send('hello')
        gen.send(1234)
        gen.send(['a',9])
        # This just returns None :(
        result = gen.close()
    except StopIteration as exc:
        # This never happens :(
        print(exc)

if __name__ == '__main__':
    main()
#

that'd be much nicer than relying on StopIteration to get a return value in general

deft pagoda
#

i'd probably enjoy using slices as keys to a dict

#

sounds useful for some undiscovered thing

#

though you can work around it by storing tuples and unpacking them into slices

#

it's sort of weird that they aren't hashable, because they're read only:

In [61]: a
Out[61]: slice(1, 2, 3)

In [62]: a.start = 4
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-62-1dead70f4ac7> in <module>
----> 1 a.start = 4

AttributeError: readonly attribute
unkempt rock
#
In [1]: slice([])
Out[1]: slice(None, [], None)
#

they store any object

deft pagoda
#

so do tuples

#
In [63]: (0, [], 0)
Out[63]: (0, [], 0)
prime estuary
#

I think this is why, so you don't have the weird confusion where this special syntax has functionality on lists, but on dicts it behaves like any other key.

deft pagoda
#

well, you can make your own mutable mapping that accepts slices

prime estuary
#

Of course, it'd definitely be bad though if regular dicts would accept it.

deft pagoda
#

i don't really agree

#

but i don't feel that strongly about it either

prime estuary
#

Well, should mydict[1:4] = ['a', 'b', 'c'] assign a single key, or three?

#

Either way it's rather inconsistent.

deft pagoda
#

a single key for a dict

#

why is it inconsistent

#

mappings and sequences behave differently

#

i don't even know what three keys 1:4 would be assigning to for a dict

prime estuary
#

They do yes, but not too differently - someobj[x] = y assigns y to the x position, and for sequences someobj[x:y] = z assigns in bulk.

#

It could be treated as mydict[1] = 'a', mydict[2] = 'b', mydict[3] = 'c', consistent with sequences.

deft pagoda
#

for python lists my_list[2:6] = 5 doesn't work --- right side needs to be iterable

deft pagoda
prime estuary
#

They do have many operations dealing with iterables, fromkeys for instance.

deft pagoda
#

yes, they're both iterable, but an iterable isn't a sequence

#

but anyways, i just am not that opposed to it, if others are, thats fine

prime estuary
#

I agree it shouldn't really be an operation that's valid, because it would be confusing that this syntax that normally changes the behaviour of the indexing operation to be in bulk suddenly does not.

deft pagoda
#

i think it's weird that some getitems take slices and others don't

#

that's inconsistent

prime estuary
#

Well if they don't use the functionality, they'll always immediately error right now.

unkempt rock
#

HELLO

fresh harness
valid rose
#

deletes a couple of folders

fresh harness
#

Yeah

obsidian kestrel
#

"To opimized system we are deleting cache files and unnecessary files."

Well, I can say with some authority that cleaning up cache and deleting files almost never actually improves Windows system performance or stability. And you can actually clear that stuff up safely (to reclaim space or remove personal data) using built in tools like the Cleanup Tool

fresh harness
#

Any suggestions please

fresh harness
obsidian kestrel
#

If you had a need to do this programatically I'd recommend Powershell or something similar, but for 90% of cases I'd just say leave it alone.

#

Like "Registry Cleaners" and "Driver Optomizers" they very rarely do anything but cause harm

fresh harness
#

Yeah

#

But accessing some one Registry is not right

valid rose
#

and even more typically these performance boosters are usually malware

obsidian kestrel
#

☝️

fresh harness
obsidian kestrel
#

Is this your project?

fresh harness
#

Yes

#

I will be really grateful if you can help

obsidian kestrel
#

I'm sorry to say I feel it's completely unnecessary, but it's also not very clear about what it's actually doing and it's not something that really needs to be a service (let alone a python service).

Is there a reason, other than performance, why you would want to do this?

#

Like I said, if there is a need to do this programatically (like cache clearing and restarting services on some number of application servers) than there is a use case? But perhaps Python is not the best language for it.

fresh harness
#

Yeah thats true that only for learning purpose

obsidian kestrel
#

Fair enough. If you're learning how to automate tasks and publish code that's definitely valid. I don't want to poo poo your project. But I wouldn't recommend anyone actually use something like this for the task as defined.

fresh harness
#

Thank you for your valuable feedback Barmp

obsidian kestrel
#

Are there things you could add on to this that would help as well? Listing services running outside of system folders for instance can provide valuable insight to what is running on a system and may be helpful for improving performance if that is the stated goal 🙂

#

Listing services running as non-default or non-system users as well.
If you can identify browser extensions that suck up enormous amounts of memory (I'm not sure how visible this is from the system side) it would be very beneficial for an end user - but not really useful on the server side.

Who is your intended audience?

fresh harness
#

Windows user

obsidian kestrel
#

Windows user with Python installed? Who have need of a non system utility to clear cache files.

fresh harness
#

We are also planning to develop UI for non python user

obsidian kestrel
#

(not trying to badger you, just want to be clear on the audience)

fresh harness
#

Naah thats completely ok to get true feedback

obsidian kestrel
#

Makes sense, a UI would really help for general users. I feel like showing the running processes / services and how much memory or CPU time they use would be more valuable to them than just clearing cache; I assume the cache clearing is step 1 of a larger goal.

fresh harness
#

Right

obsidian kestrel
#

Just personal experience on that, but I've been a Windows admin for like 20 years so I have seen a LOT of dirty windows in my time. 😄

fresh harness
#

Yeah

#

but honestly clearing 2-3 folder really helps at some levels

#

But trying to improve it more

obsidian kestrel
#

clearing cache only helps if there are processes or services using a stale cache.

#

It's more a problem on the service side, rather than user.. Such as working with DNS cache

fresh harness
#

That's true for sure

#

Are you python developer ?

obsidian kestrel
#

Speaking of, DNS is a huge issue sometimes and measuring the responsiveness of a users DNS server (then giving them a better alternative) is usually the biggest "speed up" you can give a system.

#

No, definitely not a dev. I literally just browsed around this discord briefly tonight because I've been meaning to learn more Python by building a web app

#

(I am fairly passionate about DNS)

fresh harness
#

Sounds good

valid rose
#

im not sure this is the right place to ask, but what's the regex for a valid python identifier?

#

i want to match alll identifiers given some code

fallen slateBOT
#
ast

Source code: Lib/ast.py

The ast module helps Python applications to process trees of the Python abstract syntax grammar. The abstract syntax itself might change with each Python release; this module helps to find out programmatically what the current grammar looks like.

An abstract syntax tree can be generated by passing ast.PyCF_ONLY_AST as a flag to the compile() built-in function, or using the parse() helper provided in this module. The result will be a tree of objects whose classes all inherit from ast.AST. An abstract syntax tree can be compiled into a Python code object using the built-in compile() function.

paper echo
#

!e ```python
source = """
def f(x):
return x + 5
y = 10
result = f(y)
"""

import ast
tree = ast.parse(source)
all_names = list({
node.id
for node
in ast.walk(tree)
if isinstance(node, ast.Name)
})

print(all_names)

fallen slateBOT
#

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

['f', 'result', 'y', 'x']
valid rose
#

👍 this works

main ginkgo
#

think tokenize is a better module for handling this

true ridge
paper echo
#

good point

true ridge
#

since FunctionDef.name is just a regular string. You could probably use tokenize to find out all NAMEs and discard keywords

#

that would give you the set of names used inside a code

main ginkgo
#

tokenize + keyword module for discarding keywords

paper echo
#

!e ```python
source = """
def f(x):
return x + 5
y = 10
result = f(y)
"""

import io, keyword, tokenize
line_generator = io.BytesIO(source.encode('utf-8')).readline
all_names = list({
tok
for typ, tok, *_
in tokenize.tokenize(line_generator)
if typ == tokenize.NAME and not keyword.iskeyword(tok)
})

print(all_names)

fallen slateBOT
#

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

['result', 'x', 'y', 'f']
paper echo
main ginkgo
#

The match and case keywords are soft keywords, i.e. they are not reserved words in other grammatical contexts (including at the start of a line if there is no colon where expected). This implies that they are recognized as keywords when part of a match statement or case block only, and are allowed to be used in all other contexts as variable or argument names.
from https://www.python.org/dev/peps/pep-0634/#the-match-statement

paper echo
#

interesting

#

is that because of the new parser?

main ginkgo
#

async/await were soft keywords in the past (<3.7) before the new parser

visual shadow
sonic citrus
#

Is there any naming convention for filenames

flat gazelle
#

modules should be snake_case

sonic citrus
flat gazelle
jovial flame
grave jolt
#

Maybe it does make sense if you make a file per class, but in Python people usually don't make a file per class

#

@sonic citrus ^

sonic citrus
#

Yeah ik just wanted a good example to show the coworker

#

I sent the screenshot and link and he’s already changed it 🙂

grave jolt
#

although I guess it makes sense if a file contains a single use case/transaction script/etc.

#

I think PEP 8 on its own isn't a good enough argument to change some practice

#

if your team's internal style guide is to make a file per class, then naming files like classes might not be that bad

sonic citrus
#

We don’t really have that strict guidelines

#

Dw we’ll figure it out

grave jolt
#

One thing I personally dislike from PEP 8 is using implicit booleanness, like if users: instead of if users == []:

sacred tinsel
#

I think it'd be annoying to have a module named SomeClass that contains a SomeClass definition since if you import it it's not obvious whether it is the module or the class from the name

grave jolt
#

the example that comes to mind is PIL.Image

#

it 100% looks like it's an Image class inside the PIL module!

paper echo
halcyon trail
#

Well, datetime has this issue

#

And it's awful

#

For most classes nowadays I do from imports to keep annotations concise

#

from pathlib import Path

#

Etc

#

But for datetime, I always do import datetime

#

At least that way datetime.datetime in the code is unambiguous

lament sinew
#

from datetime import datetime as dt, timedelta as td. Watch new joiners in your company cry.

paper echo
lament sinew
#

That'd break the convention of camel case reserved just for python-implemented types.

#

not sure it's a very important one, but stdlib cares enough to follow it strictly.

#

that i know of, i'm sure someone will gotcha me with an exception

paper echo
#

so classes defined in C extensions are expected to be allsmashedtogethercase?

lament sinew
#

yeh

#

that's why deque but Counter

sonic citrus
paper echo
grave jolt
#

yeah, Range and such as well

#

functools.Partial, itertools.Product 🤔

peak spoke
#

afaik the only convention like that is for builtin names apart from the exception types

paper echo
#

that is, semantically they are supposed to be functions, even if technically they're classes

grave jolt
#

if my class is supposed to look like a function, should I name it in snake_case?

paper echo
#

that's what i personally do

deft pagoda
#

i've seen that idiom plenty

paper echo
#

that, or partial really should be a function that returns a Partial which is callable

deft pagoda
#

mostly with context managers

paper echo
#

and decorators

deft pagoda
#

yeah

paper echo
fallen slateBOT
#

Lib/functools.py line 276

class partial:```
fallen slateBOT
#

Lib/functools.py lines 341 to 344

try:
    from _functools import partial
except ImportError:
    pass```
paper echo
#

oh good catch

grave jolt
#

I guess the reason it's a class if for picklability

#

apart from being easier to implement in C

paper echo
fallen slateBOT
#

Lib/functools.py lines 287 to 290

if hasattr(func, "func"):
    args = func.args + args
    keywords = {**func.keywords, **keywords}
    func = func.func```
grave jolt
#

WTF/minute counter intensifies

paper echo
#

oooh, this allows you to invoke partial on another partial and not end up with multiple layers deep of extra function calls

#

that's a really nice optimization

grave jolt
#

ah

paper echo
#

(yet another argument for making this a function that returns a Partial)

grave jolt
#

should the Partial type be exported?

#

then there'll be two ways of creating a partial

paper echo
#

probably not, or you export it and say "this is plumbing, use partial() instead for most cases"

grave jolt
#

if it won't, then you won't be easily able to do isinstance(x, Partial) (which, I think, Starlette is doing)

#

maybe it could be _Partial

paper echo
#

i'm ok with exporting it if only for isinstance and type annotations

paper echo
#
class Partial:
    def __init__(self, func, /, args, kwargs):
        self.func = func
        self.args = args
        self.kwargs = kwargs

    def __call__(self, *args, **kwargs):
        return self.func(*self.args, *args, **self.kwargs, **kwargs)

    def concatenate(self, other):
        if not isinstance(other, Partial(func=func):
            raise TypeError('Can only concatenate Partial instances together.')
        return Partial(
            self.func,
            self.args + other.args,
            self.kwargs | other.kwargs
        )

    def extend(self, *args, **kwargs):
        return self.concatenate(Partial(self.func, args, kwargs))


def partial(func, /, *args, **kwargs):
    if isinstance(func, Partial):
        return func.extend(args=args, kwargs=kwargs)
    else:
        return Partial(func, args, kwargs)
#

as a bonus, Partial is now a monoid, for any given func, with the concatenate operation (for whatever that's worth)

#

side note: i tried writing this with pattern matching and it felt incredibly unpythonic and was hideously ugly

#

it's a cool feature, but i have a feeling it isn't going to get much adoption

grave jolt
#

yeah I'm not sure it's very useful outside of tree walking and such

#

maybe a third-party library solution could be enough, but idk

paper echo
#

at least not without my "in-line" protocols 😎 like Protocol("Foo", a=int, b=str)

#

i also wonder why runtime_checkable is a decorator and not something like a subclass of Protocol (or a type parameter like class Protocol(runtime_checkable=True): ...)

#

(is there a good reason why all protocols shouldn't be runtime-checkable by default?)

paper echo
grave jolt
#

how do you pronounce hy?

paper echo
#

i pronounce it like the english "hi"

#

or like "py" as in "python" (or "pie" as in the food)

#

i can't imagine how else it'd be pronounced

halcyon trail
#

@lament sinew the snake case for classes is only supposed to be for built-ins

#

datetime is not a builtin, or at least, I don't think that's what they mean by built-in

#

deque, defaultdict, afaics, are examples of the python standard library just not following its own guidelines, and then it being difficult to change for backwards compatibilty reasons

#

and datetime.datetime (though it's not discussed as often) would be an example of this too

#

so just using those aliases from day 1 isn't a bad idea. The problem is that it's pretty hard to enforce it.

cloud crypt
#

in the first match PEP, there was a proposal to add @sealed decorator to typing, which would allow for exhaustiveness checks, and, technically, give ability to write ADTs

grave jolt
static bluff
#

So what we pronounce as 'High' (hi) they would pronounce as 'hee'

static bluff
#

But thats the only thing I can think of

paper echo
#

in most variants of american english, the i in "hi" is a diphthong anyway

#

i believe the IPA pronunciation is /haɪ/

#

so that's how i pronounce Hy

lament sinew
grave jolt
#

🍿 👀

lament sinew
#

Asking how to pronounce hy in japanese is like asking how to pronounce in english

#

inb4 ot i get it

paper echo
#

given that it's not really an english word either, i think that's a reasonable question

lament sinew
#

but it spells a valid english word, so you can pronounce it

#

hy is not a valid japanese word because y does not appear by itself in japanese

#

in the standard romanization anyways

paper echo
#

isn't there a japanese writing system for foreign words?

lament sinew
#

yes

paper echo
#

or can you still not represent something like /haɪ/ in that system?

lament sinew
#

you can, but that would make the pronounciation unambiguous

paper echo
#

i think what skywalker was getting at is that a lot of languages don't have diphthongs like english, so e.g. a romance language speaker might also find that word awkward to pronounce, regardless of how it's spelled

grave jolt
#

I was just wondering if the name is some reference, so it's pronounced in some fancy way

paper echo
#

i'm not sure why it's "h", but i think it's meant to be analogous to "py" as in "python"

lament sinew
paper echo
#

yeah of course, people don't necessarily just pick the closest "native" vowel and substitute it

#

but if there isn't a close native monophthong or diphthong then it might be awkward

#

i've watched enough japanese tv and movies to have a general sense of what vowel sounds they have, but i wouldn't know beyond that 😛

#

english is a funny international standard, the pronunciation is already so detached from the spelling that it kind of makes it good for people with a wide variety of accents

halcyon trail
#

when Hebrew is Romanized, our vowel which is close to the hard e sound, always gets mapped to "i"

#

so Nir is not pronounced rhyming with fir, as you would expect. But rather, rhyming with beer.

#

Pretty silly.

#

Spelling it Neer would have saved me a lot of repetition in my life

cosmic juniper
#

i know

cloud crypt
#

I think it could be ```python
@sealed
class Option(Generic[T]):
...

class Some(Option[T]):
...

but what about Null (None) variant?```

grave jolt
cloud crypt
#

haha, smart

grave jolt
#

but yeah, it's a bit awkward

cloud crypt
#

indeed, since you need to somehow annotated T for Nothing

cloud crypt
#

well, NoReturn exist, which is deleted later (in mypy, at least), but it's still awkward

gleaming rover
#

and create a public instance to use

cloud crypt
#

hm?

gleaming rover
#

like class _Nothing…

#

and then Nothing: _Nothing[NoReturn] = _Nothing()

#

can you type annotate __new__?

grave jolt
#

why annotate it with NoReturn?

gleaming rover
#

and the empty side has none

grave jolt
#

maybe just not make Nothing generic?

gleaming rover
#

if you can then yeah that would make sense

grave jolt
#

yes you can

gleaming rover
#

that would be better then

#

isn’t it typical for the equivalent of None to be parametrised with the bottom type

cloud crypt
#

well ```python
from typing import Generic, TypeVar

T = TypeVar("T")

class Option(Generic[T]):
...

class Some(Option[T]):
...

class Null(Option):
...

mypy --strict

type.py:12: error: Missing type parameters for generic type "Option"```

#

I wonder what does it internally use (since we don't give any actual thing for T of Option)

tidal marten
#

Does autocompleter like Jedi keep track of changes in a given file or does it just parse the file all over again?

#

It would be a hassle to manipulate the parsed tree if you have to change every lineno and offset details, no?

hasty swift
#

this is probably the wrong channel for that, nvm 🙂

potent fractal
#

can i use a youtube video and follow the tutorial an make something in python

stuck valley
#

maybe not the right channel for that

true ridge
tidal marten
#

Hmm, I need to read up more on that. But I'm still wondering, what if you have like tens or hundreds of thousands of lines of codes. Manipulating like half of the tree wouldn't be very efficient, maybe worse than parsing the whole thing over.

analog lava
#

I wanted to ask in terms of performance how a list and a dict handle getting values.

lets say on a list of 18 values, if I tell python "give me the value at index 12", does it go through 12 sections of the list to get me a value? that would be slow on huge lists right?

What about a dict? A dict with 18 keys, and I tell python "give me the value with key 'a' ", would that be slower or faster than the example above?

peak spoke
#

no, when looking up the value through an index it fetches the value directly through the index, regardless of its position

charred pilot
#

it would be faster than the dict, because it doesn't have to hash anything

analog lava
#

a dict is slower then?

charred pilot
#

a dict is slower in this situation, it is much faster at other things

#

although complexity-wise, in this situation they're the same

analog lava
#

what are situations a dict would be better, where it's more performatic?

grave jolt
#

For example, when they are strings

paper echo
#

It's worth noting that in some languages, arrays are implemented as tables (like a dict) with consecutive integer keys

#

I think in general it's less valuable to worry about performance than about using data structures that are semantically meaningful

#

Are you representing a finite sequence? Use a list. Are you representing a record of some kind? Use a tuple. Are you representing a look up table or mapping? Use a dict. Etc

#

I think one of the great design elements of python was the selection of built-in core data structures

#

Especially in python 3 with the unicode/string and bytes distinction

static bluff
#

I've got a fun one for you guys

#

And this is all just spitballing mind you

#
import psutil
import multiprocessing

class Process(multiprocessing.Process, psutil.Process):
  ...
#

Psutil's process class is a tad more useful than the multiprocessing version. The former carries with it a lot of meta information and utility methods for working with processing, while the latter is used to actually start a new process

#

How would you go about building this class?

analog lava
#

I see thanks fellas.

analog lava
paper echo
#

then you can dispatch accordingly. you definitely do not want to inherit from two complicated and unrelated classes that are not meant to work with each other at all

paper echo
static bluff
#

Nice

#

Thanks Salt!

#

So basically — composition wold be preferred over inheritance. I had a feeling that'd be the case.

static bluff
#

Can I get some help rewriting this?

#
# Pyscript is an XML-Element based framework. Every graphical object in Pyscript — from HTML and SVG
# graphics most commonly used, to low-level wxPython widgets used in windows, to WebGL objects used 
# for 3D rendering — all exist as part of a streamlined and unified element based API.
#

I'm trying to succinctly explain that: the HTML and SVG elements rendered in a window have equivalent proxy classes in the framework, and that wxPython's widgets have also been rolled into this API such that they all appear to be part of the same system

paper echo
#

Pyscript is a GUI framework based on wxPython. In Pyscript, you define your interface interface in XHTML, with support for SVG elements. Every XML element in the interface definition corresponds to a Pyscript object. To render the UI, the Pyscript framework converts these objects to low-level wxPython widgets.

static bluff
#

Thanks Salt!

#

While you were writing I whipped this up

#
# Pyscript is an XML-Element based composite framework. This is to say that it that combines the API
# of numerous frameworks into a unified system, and that system is built to walk and talk exactly
# like elements would in a normal web-app development environment.
#

Also, how do you format text like you did?

paper echo
#

> Hello

#

Hello

paper echo
static bluff
#

Pyscript is an XML-Element GUI toolset, designed to resemble the front-end development experience. Interfaces are written in XHTML and converted at runtime to HTML/SVG elements, and wxPython widget or WebGL 3D objects.

static bluff
#

Thanks for the help Salt, I don't want you to think I wasn't taking your comments into consideration. I did my best to integrate them

halcyon trail
#

the annoying thing about composition in a case like that is that you end up writing a ton of boilerplate to expose that API, or else you have a getter at which point you don't have much encapsulation really, and more verbosity.
I hope more languages start to think about delegation.

grave jolt
#

!e

def delegate(to, methods):
    def decorator(cls):
        for entry in methods:
            if isinstance(entry, str):
                entry = entry, entry
            source, dest = entry
            
            def getter(self, source=source):  # closure gotcha
                delegate = getattr(self, to)
                return getattr(delegate, source)
            setattr(cls, dest, property(getter))
        return cls
    return decorator

@delegate("items", ["__iter__", "__len__", "__getitem__"])
@delegate("label", ["__str__"])
class ListWithLabel:
    def __init__(self, items, label):
        self.items = items
        self.label = label

foo = ListWithLabel([1, 2, 3, 4, 5], "bar")
print(foo)
print(len(foo))
fallen slateBOT
#

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

001 | bar
002 | 5
grave jolt
#

well, this works for reads

halcyon trail
#

I guess so, I'm sure there's going to be some pitfalls with a toy example

#

if it can really be done well it should be in the stdlib, IMHO, it would be really useful

verbal escarp
#

hey @paper echo, just to let you know, we're making good progress with the pip-auto-install mechanism, i'm also working on replacing the nested dict ad-hoc registry with an actual sqlite db 🙂

#

it's also exposed to the user, so users can do use.registry(<sql>) to check installed stuff in their own code

worn spindle
#

Could anyone suggest ; Best way to handle large datasets(Json) passed to post api in drf endpoint

verbal escarp
#

now i'm really itching to translate that version stuff to a db table, but i'm not getting anywhere 😄

verbal escarp
#

i was wondering if there was any interest in package/import-metadata

#

like for instance how often a package was imported, last time

summer hedge
shy anvil
#

Hello,

#

i had a question i wanna create a software for Multiple Time Frame Analysis for a graph. anyone know something for how i can find some information about that?

heavy epoch
static bluff
#

Today is just not my day

paper echo
paper echo
paper echo
static bluff
#

#coder-life

paper echo
#

"documentation driven development"

#

pro: it forces you to at least flesh out your ideas before starting
con: you have to know when to stop writing docs and start writing code. but imo that's better than getting through an entire project and realizing you have 0 docs

static bluff
#

Well, I think I've finally come up with a mostly complete mental map of what the framework is going to look like

#

What I'm trying to do now is create a skeleton of the framework — just empty classes — and write out some decent docstrings and comments. Than I can post it on github and call it "Pyscript 0.1.0" with a timeline for future development

paper echo
#

that's a good way to do it

static bluff
#

So far, its kicking my ass XD

paper echo
#

basically a DSL for your notes

#

you'll be happy you did it

static bluff
#

I think my brain might be fried though O.O It was a very hot day today

#

I went blackberry picking

#

Okay. I've gone around and around in a circle and just ended up back at the beginning

#

Ima try this one more time. This time at least it has a little flair

#

This module is the entry point to the Pyscript Graphics Library, and contains every public facing class in its API as well as numerous flags, constants, enumerations, and other such material.

Pyscript is web graphics in Python. Easy to use, powerful, attractive, and very well documented; web graphics are king. Pyscript is web graphics rendered in a python-based web browser, controlled using a pure-python API, and embedded in an operating system's native windowing system.

Real web graphics, native look and feel, pure Python. That's the theory anyway.

worn spindle
halcyon trail
#

@paper echo yes, I mentioned you can simply provide a getter for the other process

#

It's bad for encapsulation

#

Not the end of the world but it's only done that way because it's easier

#

If delegation were easier that would be the default in a situation like this

static bluff
shrewd narwhal
#

is

#

is this Advanced Discussion?

quartz sapphire
#

Actually

#

Python does support putting ; at the end of lines

#

But

#

It makes no differnce

verbal escarp
#

also, sqlite isn't as old as it feels like

#

the story behind it is quite remarkable really

#

i'm guessing pip is older than sqlite

lament sinew
#

no way

#

i remember when pip came out, it was 2010+

#

maybe a bit before

#

i didn't use it for a long time because it couldn't install packages with c dependencies

visual shadow
olive marsh
#

Is there anything in ctype for (keywords) int, float, list....

stuck valley
#

How can I change readonly attributes?

flat gazelle
#

depends on how exactly they are implemented. Generally object.__setattr__(obj, 'readonlyattr', new_value) will work

stuck valley
#

thanks

#

and if not?

flat gazelle
#

This is more a topic for #esoteric-python, but at some point you may have to go into C level memory manipulation

deft pagoda
#

advertisement, no

verbal escarp
lunar trail
#

!mute 622343143064469524 Let's not spam advertisements here please

fallen slateBOT
#

:incoming_envelope: :ok_hand: applied mute to @worldly walrus until <t:1628871759:f> (59 minutes and 59 seconds).

fleet ice
#

Match case are useless change my mind

verbal escarp
#

😉

native flame
#

well that should change their mind

charred pilot
#

match case does things that aren't currently possible (without hacks)

grave jolt
verbal escarp
#

well, i learned to work around it with dicts and dispatch via a kinda ugly construct

#

but lambdas in dicts are restricted to a single line, so match/case is a major step forwards from that

#

i worked around that limitation with multi-line generators, but i'm not sure how readable those are

#

if/elif on the other hand can become equally unwieldy very quickly

#

i'd say dispatching in general could be smoother in python

#

and if match/case helps in that regard, it's a big win

#

although, i wonder if a dict-"workaround" might be faster than case/match

#

hmm.. could pattern matching be applied to xml parsing?

#

xml parsing is one of the most unpleasant things i came across recently

charred pilot
#

pistol operator

paper echo
grave jolt
#

yeah it seems like it's a very niche thing now

flat gazelle
#

it's an answer to multiple similar function signatures, sth which java etc. would do with multiple dispatch

#

optional arguments don't cover all cases, and it is quite common to see 6 lines of argument checking logic followed by 2 of what the function actually does

#

this way the argument checking logic is at least not a weird stateful mess

verbal escarp
raven ridge
nova iris
#
>>> super(Foo, A).__init_subclass__
<built-in method __init_subclass__ of type object at 0x00000166465A7D00>
>>> object.__init_subclass__
<built-in method __init_subclass__ of type object at 0x00007FFC2D84BE00>

okay this is messed up lmao, i have two classes Foo and A, the latter is a subclass of the former. does anyone know what's the difference between these two? from what i can gather:

the first one is a super object bounded to A - which is supposed to represent object, the first superclass in the MRO defining the method - accessing __init_subclass__, also bounded to A (even though it doesn't say for some reason)
the second one is just __init_subclass__ of object, which raises an error if anything is passed in.

what's the difference?

raven ridge
#

Isn't the first one a bound method and the second unbound?

nova iris
#

lol, i remember the good old days when i thought dunder methods with two words or more were deep magic i would never care about

#

welp, here i am with __init_subclass__, deep magic indeed

#

OH I UNDERSTAND NOW

#

INIT SUBCLASS IS ALSO A CLASS METHOD

#

SO WHEN NO ARGUMENTS ARE PASSED THE FIRST POSARG IS STILL THE CLASS, WHICH CAN BE THE SUBCLASS OR THE SUPERCLASS ITSELF IF IT IS INVOKED DIRECTLY FROM THE SUPERCLASS

#

which is why, when object.__init_subclass__() is invoked, it's essentially passing the class object as the first posarg, but this method does nothing anyways

raven ridge
#

sounds right, yep.

nova iris
#

and super(Foo, A).__init_subclass__ is just passing in A as the first posarg

#

so the difference is the class being bounded - one's object, the other is A

#

godlygeek is typing (intermittently lol)...

raven ridge
#

and super(Foo, A).__init_subclass__ is just passing in A as the first posarg
No, it's passing the class after Foo in the A MRO as the first pos arg

#

which is object

nova iris
raven ridge
#

since the MRO that you've described is (A, Foo, object)

#

A isn't bound, object is bound

nova iris
raven ridge
#

yep. The second argument is used to figure out which MRO to search in, and the first argument is where to start the search from - or after, really.

nova iris
# raven ridge yep. The second argument is used to figure out which MRO to search in, and the f...

so then if that's the case, why does __init_subclass__ work at all? consider if we had four classes in total:```py
class A(object):
def init_subclass(cls, /, *, a, **kwargs):
super().init_subclass(**kwargs)
cls.a = a

class B(A, a=1):
def init_subclass(cls, /, *, b, **kwargs):
super().init_subclass(**kwargs)
cls.b = b

class C(B, a=2, b=3): pass
then C would have the MRO `(C, B, A, object)` py

continued from first codeblock

print(C.a) # => 2
print(C.b) # => 3
```but if super here worked like you said, super() in the C class would translate to super(__class__, cls) which would in turn be super(B, C). so if A is passed into __init_subclass__ instead, why do we still see C.a being printed out without an error (C.b would work, but in this case C.a seemed to be defined within the __init_subclass__)?

raven ridge
#

what's this = yeet? Where did you define yeet?

nova iris
#

so it has to be C that's bounded, right?

#

__self__ shows this as well, and i think __self__ is the convention for storing bounded objects (like in methods)

raven ridge
#

you're right - the thing that's bound is the second argument to super()

nova iris
#

ah, okay, so i guess we had a little mix-up lol

#

super is quite weird sometimes

#

there's even the weird unbound version (super(cls)) which is about to get deprecated

raven ridge
#

In your example, there's no super() in the C class body. There's one in the B class body, where super().__init_subclass__(**kwargs) is equivalent to A.__init_subclass__.__func__(cls, **kwargs), and there's one in the A class body, where super().__init_subclass__(**kwargs) is equivalent to object.__init_subclass__.__func__(cls, **kwargs)

nova iris
#

C's just implicitly calling __init_subclass__ from B

raven ridge
#

right.

#

and B is calling A's implementation of __init_subclass__ on the cls it was passed, and A is calling object's implementation of __init_subclass__ on the cls it was passed.

nova iris
#

thanks a lot @raven ridge, we figured it out!

#

i'll go participate in the ractf now

verbal escarp
#

oh goodness, i'm glad i never had to deal with this kind of mess before

#

recursive subclassing.. oh man

unkempt rock
#

in python 3.8, how does future totally change the syntax of the language with just an import here?```py
from future import annotations
def f(x: list[str]):
pass

#

in 3.8 that kind of type hinting is normally invalid and you get Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'type' object is not subscriptable

#

I guess it's effectively adding __getitem__ to list or something but I'm surprised that an import can fix that pithink

peak spoke
#

It makes the typehint a forward ref, it's still invalid on that ver

#

You just postpone when the error will happen as the annotation is not evaluated until necessary

PS > py -3.8
>>> from __future__ import annotations
>>> def f(a: list[str]): ...
...
>>> import typing
>>> typing.get_type_hints(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\miso1\AppData\Local\Programs\Python\Python38\lib\typing.py", line 1264, in get_type_hints
    value = _eval_type(value, globalns, localns)
  File "C:\Users\miso1\AppData\Local\Programs\Python\Python38\lib\typing.py", line 270, in _eval_type
    return t._evaluate(globalns, localns)
  File "C:\Users\miso1\AppData\Local\Programs\Python\Python38\lib\typing.py", line 518, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
TypeError: 'type' object is not subscriptable
unkempt rock
#

oh, interesting

raven ridge
#

it's interpreter magic that changes the syntax of the language.

unkempt rock
#

OhImLearning indeed

#

But what if you import halfway through the code, is the syntax changed from then onward rooHmm

raven ridge
#

previous __future__ imports did things like add the with statement to the language, and change the behavior of 1 / 2 from returning 0 to 0.5, and allowing the yield statement

peak spoke
#

future imports have to be the first thing in your module apart from things like comments and docstrings

raven ridge
#

A future statement must appear near the top of the module. The only lines that can appear before a future statement are:

  • the module docstring (if any),
  • comments,
  • blank lines, and
  • other future statements.
unkempt rock
#

!e Ah, I see. It does complain. ```py
print(1)
from future import annotations
print(2)

fallen slateBOT
#

@unkempt rock :x: Your eval job has completed with return code 1.

001 |   File "<string>", line 2
002 | SyntaxError: from __future__ imports must occur at the beginning of the file
unkempt rock
#

though I'm surprised my linter isn't complaining pithink

#

but thanks guys peepoHappy I've never played around with __future__ much

prime estuary
#

The intent is to allow you to update your code gradually - a feature is released in version X, but only if the future import is used. So you can update your code for that version and add the import, then when it's enabled by default the future statement does nothing.

#

By the way, if you ever get sick of Python's indentation, try the secret braces future import :)

verbal escarp
#

no chance 😉

#

on the other hand, this is a good thing to have handy

#

and annotations is nice to have, yeah

#

although i'm curious how they're going to fix the postponed mess

#

any words on that yet?

prime estuary
#

The planned switch to having annotations be the default was reverted, and it's a complicated issue to figure out what semantics Python should have, how to accomodate all the different use cases and how to do it efficiently.

grave jolt
#

I think there's a somewhat fundamental challenge of combining Python's statement-based syntax and annotations which are declarative

prime estuary
#

Yeah, I think it's sorta exposed a more general issue which we also see with import cycles, where we want stuff to be interdependent but can't.

#

Maybe someone can come up with a brilliant solution that fixes the core issue.

quasi hound
#

Yeah glad to see it

#

Not being able to do something as simple as

class Person:
    def __init__(self, friends: List[Person]):
        pass

Was pretty stupid

grave jolt
#

well, you can do "List[Person]", and type checkers will understand that

#

the issue with lazy annotations arises when libraries inspect the annotations

#

maybe it's not really a problem with annotations, but

stuck valley
#

portal from #esoteric-python to remind you that you can redefine __annotations__ for kicks

spark magnet
verbal escarp
#

after reading the whole mail thread, i'll say that i'm clearly in the "typing should be a DSL" camp

#

which also has been discussed in that thread

#

so no, i wouldn't say "it had to work in the context of python semantics" per se

grave jolt
#

well, it was initially designed to support runtime inspection

spark magnet
verbal escarp
#

they were also introduced under the premise that you could put anything in there and only tooling would need to bother about them

spark magnet
#

yes. so how do you change that without breaking things?

verbal escarp
#

well, the whole runtime-inspection usecase was mostly a happy accident

grave jolt
#

I'm surprised nobody created a typescript-in-python, where annotations don't exist at runtime

spark magnet
grave jolt
verbal escarp
spark magnet
#

"By itself, Python does not attach any particular meaning or significance to annotations. Left to its own, Python simply makes these expressions available as described in Accessing Function Annotations below."

#

I can't explain how FastAPI got overlooked.

verbal escarp
spark magnet
#

"Function annotations are nothing more than a way of associating arbitrary Python expressions with various parts of a function at compile-time."

verbal escarp
#

type checking, IDEs, generic functions, foreign-language bridges, db mapping.. that's mostly static

spark magnet
#

yes, but the PEP is pretty clear that annotations are Python expressions, and that they are available at runtime.

verbal escarp
#

i don't see anything that resembles fastapi in those usecases

spark magnet
#

i don't understand the point you are making.

#

FastAPI uses annotations for type checking and adaptation. Both are listed in the use cases part of the pep.

verbal escarp
#

i mean that the whole notion was towards static typing, not runtime

spark magnet
#

the word static appears zero times in the pep

#

and the pep explains how to access the annotations at runtime

#

are we looking at the same document?

grave jolt
#

I think the PEP clearly says the annotations are supposed to be inspected, like with DB mapping or RPC parameters

verbal escarp
#

Let IDEs show what types a function expects and returns ([17]) <- how would you do that if not by a static analysis?

spark magnet
#

it doesn't preclude static analysis, but it doesn't make sense to say that the whole notion was static analysis

#

btw, an IDE can do that by importing the module and accessing __annotations__

verbal escarp
#

those use cases already are conflicting

spark magnet
#

how do they conflict?

grave jolt
#

I don't think those are conflicting, some languages use static types for things like JSON serialization.

verbal escarp
#

they conflict because if you have an expression that relies on dynamic context, you can't have it available during static analysis

spark magnet
#

exactly. and that is the problem they are currently trying to work out.

verbal escarp
#

which is also what i was trying to say earlier

#

that we might need two different kinds of annotation, clearly distinct

#

to fully satisfy both sides

spark magnet
#

that would be really confusing

verbal escarp
#

not more than having three or four different ways of string interpolation 😄

spark magnet
#

it would be if you expected to have one type annotation serve both purposes, and had to add two to get both uses.

grave jolt
#

having confusing things doesn't mean that we should make more confusing things

verbal escarp
#

say, you rely on runtime checks and you want to signal to any static checker that there's no point in trying - you could put it in as a str

spark magnet
#

but i want my FastAPI types to also be used by mypy

verbal escarp
#

what if mypy generates code objects that are loaded alongside the code if available

#

like a preprocessor

spark magnet
#

then mypy is required to keep FastAPI working?

verbal escarp
#

not if fastapi could also understand the DSL.. bleh. no good solution anywhere

spark magnet
#

yup, bleh

grave jolt
#

TypeScript has a slightly different thing: types aren't available at runtime, but you can make compound objects whose types TS can easily infer, e.g. ```ts
import {obj, array, str, tuple, bigInt} from 'some-sort-of-library'

const userSchema = obj({
name: str,
interests: array(str),
workExperience: array(tuple(str, bigInt)),
})
type User = PayloadType<typeof userSchema>
``` and User is inferred as {name: string, interests: string[], workExperience: [string, bigint][]}

#

but it's not really applicable to Python tbh

cyan elbow
#

I keep getting this error while installing python packages

#

Yes I have installed wheel and I'm still getting the same error what is happening It worked fine a couple of days ago now it keeps doing this

stuck valley
#

what if you try uninstalling the offending packages?

cyan elbow
#

I have tried uninstalling but it says warning skipping matplotlib as it is not installed

#

It's still not working it's says failed to build setup.py for matplotlib

stuck valley
#

I've had this same issue before. I'll try and think back to how I solved it.

naive saddle
#

Can you verify your pip installation is up to date?

cyan elbow
#

Yes I just installed vs code an hour ago

naive saddle
#

It probably shouldn't be compiling matplotlib locally

cyan elbow
#

but I got it to work before but now it won't work

static bluff
#

Can I get a proof my dudes?

#

This module is the entry-point to Pyscript's Elements API, which contains every renderable object in the Pyscript framework.

Pyscript is a graphics framework built atop three entirely separate rendering technologies: HTML and SVG web graphics, WebGL for 3D rendering, and wxPython for lower-level window configuration. Each of these technologies is different under the hood, but Pyscript aims to present a unified element-based API which encompasses all three and which resembles the standard front-end DOM interface.

Pyscript defines four categories or 'namespaces' of element, each demanding its own implementation: HTML elements, SVG elements, WebGL objects, and wxPython widgets.

The former three categories are delivered by way of proxy-object; when a window is launched Pyscript creates a clone or 'proxy' each renderable object, and its those proxies which are controlled by the Pyscript API.

As such, this API contains a pythonic implementation of every element in modern HTML5, including SVG elements. Elements depreciated since HTML5 are not included. Most of Javascript's WebGL interface is cloned also. Pyscript refactors the WebGL API into an element-based system both in Javascript and in Python. See pyscript.Elements.WebGL for more information.

Pyscript uses wxPython to draw the windows in which the rest of its graphics are rendered. Many, but by no means all, wxPython widgets are subclassed in Pyscript to enable fine-grained configuration of windows by the user. These classes have been refactored to resemble XML-elements.

#

I'm sure its still much too verbose 😐 But there is a lot of stuff going on which this package

stuck valley
#

What is __build_class__?

quasi hound
#

probably someting to do with metaclasses

prime estuary
fallen slateBOT
#

Python/bltinmodule.c line 103

/* AC: cannot convert yet, waiting for *args support */```
static bluff
prime estuary
#

There's also __import__.

stuck valley
#

Okay - oh lord

cyan elbow
#

It always crashes at building wheel for setup.py

stuck valley
#

tfw undocumented builtin

#

that's pretty epic

#

How can I redefine it?

quasi hound
#

is if isinstance(x, int) preferred over if type(x) is int? not talking about issubclass here btw

prime estuary
#

That's because it's not really part of the language, PyPy could decide to do it elsewhere instead of a function.

grave jolt
prime estuary
#

Isinstance would be preferable so your code accepts subclasses too.

quasi hound
#

k

prime estuary
static bluff
static bluff
#

But I'll walk you through it

prime estuary
#

Translating the __build_class__ func, what it does is figure out the metaclass, call either that or regular type, call the "function" the class body was compiled into, then return the class.

#

It mostly just calls the metaclass's methods, so you could just override those.

stuck valley
#

Just override type?

#

i.e change type's methods?

#

I intend to have something at the start of my program that affects all successive class declarations

prime estuary
#

Okay, maybe we should take a step back, what is it that you're trying to achieve in the first place?

quasi hound
pliant tusk
#

You could just do __builtins__.__build_class__ = ...

stuck valley
quasi hound
#

lambda: 0

stuck valley
#

ah

#

so just a callable

pliant tusk
#

A function that does your stuff

quasi hound
#

or your stuff

stuck valley
#

what inputs is it passed?

quasi hound
#

thought it'd bejust a throwaway function that does nothing

prime estuary
#

Okay Python doesn't really have anything for that, and it did even remove the old Py2 method of setting a __metaclass__ variable.

stuck valley
pliant tusk
prime estuary
#

Really you should have all your classes inherit from a Struct base class or something, then do your thing in there.

stuck valley
#
>>> __builtins__.__build_class__=lambda*_:print(_)
>>> class a:
    def b(self,x):
        print(2)
        return 3
(<function a at 0x0000020174FD13A0>, 'a')
#

thank you

prime estuary
#

You probably really shouldn't override builtins in this way - it's exceptionally magical, there'd be no sign later on in your code that you did it, and so it'd confuse anyone reading the code or if an error occurs in your substituted class.

#

A metaclass= definition is much clearer, or just subclassing a class with a metaclass set.]

stuck valley
cyan elbow
#

I think it's wheel that keeps crashing but I uninstalled wheel but it's still showing the same error

quasi hound
#
f = __builtins__.__build_class__
__builtins__.__build_class__=lambda*_: (print(_), f())[1]
stuck valley
raven ridge
#

Nothing is crashing. It's trying, and failing, to build

#

matplotlib from source

prime estuary
#

You might also be interested in macropy, which gives you the ability to do AST transforms on code.

raven ridge
stuck valley
#

That's also useful. Thanks.

prime estuary
#

The signature of __build_class__ is complicated, you'd want to do *args, **kwargs probably.

raven ridge
stuck valley
#

What arguments does __build_class__ take? Just the arguments to a class creation?

#

like the things in ()

prime estuary
#
def __build_class__(func, name, /, *bases, **kwds): ...
#

*bases and **kwds are what are put in the bases bit for class creation, it tries to pop "metaclass" to find that out.

quasi hound
#

you know what / is right? in this case func and name can only be passed in positionally and not by keyword

stuck valley
#

I don't know what / is, no

quasi hound
#

(took me a while to find out what it was)

raven ridge
#

"positional only arguments"

stuck valley
#

Okay - thanks.

prime estuary
#

name is the class name. func is a new function which is called with a namespace dict as locals, to do the class body.

#

You probably should call the original builtin.

stuck valley
#

the namespace dict is the class argument?

stuck valley
prime estuary
#

That's what type.__prepare__() is for...

#

Can you explain what exactly you want to change?

stuck valley
#

I'm trying to reimplement the python 2 module-level __metaclass__ variable.

prime estuary
#

No, I mean what is your overall objective, not how you want to implement it.

stuck valley
#

That is my overall objective.

#

I want to have something at the start of my files that affects all subsequent class declarations.

prime estuary
#

Why?

stuck valley
#

For kicks.

prime estuary
#

Okay, sure then.

stuck valley
#

(and for obfuscation fun secondarily)

#

I'm digging progressively deeper into Python's internals.

prime estuary
#

Something like this should work I think.

import builtins
old_build = builtins.__build_class__
def new_build(*args, **kwargs):
    func = args[0]
    if 'metaclass' not in kwargs:
        try:
            meta = func.__globals__['__anon_dev_metaclass__']
        except KeyError:
            pass
        else:
            kwargs['metaclass'] = meta
    return old_build(*args, **kwargs)
builtins.__build_class__ = new_build
stuck valley
#

try except has else? that's when no error is thrown, right?

raven ridge
#

Yep.

elder blade
raven ridge
#

3.8, I think

#

Yeah.

stuck valley
#

Okay - the / syntax is interesting

languid plaza
stuck valley
#

shouldn't you use __builtins__?

prime estuary
#

No, __builtins__ is the old name of the module in Python 2.

cyan elbow
#

I don't know what I'm doing wrong

raven ridge
#

If you don't have wheel installed, start by installing wheel

languid plaza
#

I m trying to create a custom syntax for example:

n = 10
for x in condition(x = 1, x <= n, x += 1):
  print(x)
``` This is similar to C's For loop, ik python has range and other iterators and generators but i want to learn the behind the scenes so can some one explain how can i achieve this
static bluff
cyan elbow
#

It also show error 2 no such file or directory but I saved the python file and I have checked it manually and it's there

raven ridge
#

You either need to be set up to compile the module from source, or you need to be set up to install the module from a wheel

#

The latter route is easier, but you need to pip install wheel

cyan elbow
#

Wheel is already installed

#

But it still shows errors when I say pip install matplotlib

#

I also tried it on almost on all python version from 3.9 to 3.7

raven ridge
#

The last error message you showed said that wheel wasn't installed

#

What error are you getting now that it is?

cyan elbow
#

It shows failed to build wheel for matplotlib setup.py

#

And command errored out with one exit status

raven ridge
#

And it no longer says "since package 'wheel' is not installed"?

paper echo
# languid plaza I m trying to create a custom syntax for example: ```py n = 10 for x in conditio...

!eval you would have to patch python itself and rebuild from source. could be nontrivial. however if you don't mind using lambdas then you can do it with plain python:

def condition(initfunc, stopfunc, stepfunc):
    value = initfunc()
    while stopfunc(value):
        yield value
        value = stepfunc(value)

n = 10
for x in condition(lambda: 1, lambda i: i <= n, lambda i: i + 1):
    print(x)

(i hope your actual use case isn't just a reimplementation of range)

fallen slateBOT
#

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

001 | 1
002 | 2
003 | 3
004 | 4
005 | 5
006 | 6
007 | 7
008 | 8
009 | 9
010 | 10
paper echo
#

yet another use case for Hy >:)

cyan elbow
#

It manly dumps out import Io os sys setup tools tokenize sys.argv

raven ridge
#

Those aren't the important part of the error message.

#

!paste the whole output.

fallen slateBOT
#

Pasting large amounts of code

If your code is too long to fit in a codeblock in discord, you can paste your code here:
https://paste.pydis.com/

After pasting your code, save it by clicking the floppy disk icon in the top right, or by typing ctrl + S. After doing that, the URL should change. Copy the URL and post it here so others can see it.

languid plaza
#

but if i need to rebuild what all i need to do / change

paper echo
#

that said, i question the value of C-style loop syntax in general

paper echo
#

as you can see, you might as well just write a while loop

#

or better yet write a generator (or other iterable/iterator class) that does what you want

cyan elbow
#

At the end it says running_build_ext error Microsoft visual c++ 14.0 or greator is req but I already have it installed

paper echo
#

@languid plaza explain your actual use case in a help channel and @ me, i will be happy to provide a solution that doesn't involve heavily modifying the cpython parser

#

@languid plaza to be clear, you are proposing an entirely new kind of syntax. nowhere in python are "unevaluated" expressions captured (except in function and class definitions)

#

therefore you would need not only new syntax but also new bytecode operations, and probably a whole bunch of new internal machinery

cyan elbow
#

And I have tried python -m pip install -U matplotlib it does the exact same thing

paper echo
cyan elbow
#

I'm using visual studio code terminal

paper echo
#

that ain't gonna cut it

#

visual studio build tools stuff is not normally available in your windows shell

cyan elbow
paper echo
#

you need to activate it, either by sourcing a specific .bat script (the exact name changes every version but usually it's some variation on "vcvarsall"), or by using this Developer Command Prompt

cyan elbow
#

I got matplotlib to work before but ever sense I uninstalled anaconda it's been doing this I have tried reinstalling anaconda and all of the packages but it still does the exact same thing

#

Python locations

#

This is what happens when I try to run a hello world python code

#

Sorry for it being a little blurry

paper echo
#

hold on, are you using conda or no?

#

i've heard there are other issues w/ the windows store versions of python specifically

#

i've been advised not to use them

cyan elbow
#

No

#

But again I have used anaconda but I still get the same error

#

It's a little bit different but it always seem to crash when it's a setup.py

grave jolt
cyan elbow
#

I had but the chat goes quick and I don't get a reply

stuck valley
#

Working a bit deeper, I get this:

#
>>> o=__builtins__.__build_class__
>>> x=lambda f,n,*bases,**kwds:print(f,n,bases,kwds)
>>> __builtins__.__build_class__=x
>>> o
<built-in function __build_class__>
>>> x
<function <lambda> at 0x000002F06EB649D0>
>>> class a:
    def b(self,c):
        print(2*c)
        return 3
<function a at 0x000002F06EB64A60> a () {}
#

@prime estuary what is the function a?

prime estuary
#

It's a function containing the contents of the class definition.

stuck valley
#

Contents of a class definition? A function?

prime estuary
#

It's called, with the local scope set to a dictionary you have access to the definition. Then all the functions/vars defined are put into that and it becomes the class namespace.

def a():
    def b(self,c):
        print(2*c)
        return 3
stuck valley
#

It's called with locals() set to a dictionary. What does it return?

prime estuary
#

Nothing, the return value isn't important.

stuck valley
#

None, apparently?

#

Does it take arguments?

#

No.

#

I'm familiar with the disassembler. Thanks?

prime estuary
#

I mean disassemble the function it produces.

stuck valley
#

so the first function passed to the definition?

#
  1           0 LOAD_NAME                0 (__name__)
              2 STORE_NAME               1 (__module__)
              4 LOAD_CONST               0 ('a')
              6 STORE_NAME               2 (__qualname__)

  2           8 LOAD_CONST               1 (<code object b at 0x000002F06EB479D0, file "<pyshell#16>", line 2>)
             10 LOAD_CONST               2 ('a.b')
             12 MAKE_FUNCTION            0
             14 STORE_NAME               3 (b)
             16 LOAD_CONST               3 (None)
             18 RETURN_VALUE

Disassembly of <code object b at 0x000002F06EB479D0, file "<pyshell#16>", line 2>:
  3           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 (2)
              4 LOAD_FAST                1 (c)
              6 BINARY_MULTIPLY
              8 CALL_FUNCTION            1
             10 POP_TOP

  4          12 LOAD_CONST               2 (3)
             14 RETURN_VALUE

I get this

#

trying to remember what each of these does

prime estuary
#

The dis documentation has an explanation for them all.

stuck valley
#

it says that __module__=__name__ for the first line?

prime estuary
#

Yep

stuck valley
#

Coming back to me now

#

__qualname__=a - what's the function of __qualname__?

prime estuary
#

So MyClass.__qualname__ returns the name of the class.

stuck valley
#

ah

prime estuary
#

__name__ is the name of the module, so it copies that to the class's __module__ attr.

stuck valley
#

then a codeobject representing b and a string 'a.b' are loaded onto the stack

prime estuary
#

Yep, and it creates the function.

paper echo
stuck valley
#

What's the function assigned to?

paper echo
#

i also gave you some very specific advice and i suggest you read that documentation and attempt to use the solution that i proposed

stuck valley
#

I understand that a.b is having the codeobject assigned to it

prime estuary
#

MAKE_FUNCTION pops the code object, function name (and in other cases annotations dict, closure cells, etc), builds the function, then puts the func on the stack.

stuck valley
prime estuary
#

STORE_NAME then stores it into the variable b.

#

Yep

stuck valley
#

okay and then it returns None

#

so what am I missing

prime estuary
#

Yep, the interesting bit is the namespace that LOAD_NAME/STORE_NAME are writing to - it's a dict, so now it's got all the contents of the class stored in it.

stuck valley
#

okay

#

following so far

prime estuary
#

That namespace is passed to the metaclass, which then finally puts it in the class when built.

stuck valley
#

So now what happens to that namespace?

prime estuary
#

It gets put in the class object's __dict__.

stuck valley
#

Okay - that's how attributes are accessed, right?

prime estuary
#

Yeah, attribute lookup accesses the pointer to that dict, then looks it up.

stuck valley
#

Okay.

#

so the final namespace of that function is the class

#

What function do the other arguments to __build_class__ serve?

#

I presume the second argument is the name of the variable to assign it to?

prime estuary
#

Well, the keyword arguments are passed along to the metaclass who can do whatever it wants with them. There's the name of the class, and the base classes tuple.

stuck valley
#

this is looking very fun to mess with

#

The usual metaclass is type, right?

raven ridge
#

yes.

stuck valley
#
o=__builtins__.__build_class__
__builtins__.__build_class__=lambda*_:o(exec((lambda:0).__code__.replace(co_consts=())),'')

stick this in the .pythonrc of someone you don't like

#

it looks golfable

stuck valley
prime estuary
#

No, since they're not as complicated, it's just MAKE_FUNCTION, which constructs the object.

stuck valley
#

Damn- was hoping I could mess with def too.

#

Of course, I could change co_consts

prime estuary
#

macropy is what you want, since it gives you the AST, and you could then swap that to something else.

raven ridge
cyan elbow
#

Got matplotlib to work but can't get mayavi to work on anaconda

paper echo
paper echo
paper echo
prime estuary
#

Yeah it does seem unmaintained, unfortunately. I hadn't used it myself but was aware of it.

languid plaza
stuck valley
#

Actually, does Windows support pythonrc?

unkempt rock
#

where do i ask for help for snap!

stuck valley
paper echo
#

i drafted up a quick sketch of some of the typing-related proposals i've floated here recently https://paste.pythondiscord.com/olabofajep.py. would appreciate some preliminary feedback before i put more time into them, hopefully later this month (after i've made a bigger dent in my from-scratch motor type stubs, and my fixes to the pandas type stubs)

unkempt rock
#

coding language snap!

stuck valley
#

This probably isn't the right channel.

#

Or the right server.

#

@unkempt rock

unkempt rock
#

snap is like python

#

idk this is so stupid

#

fcking my uni is making me to use snap for cs10 course

stuck valley
stuck valley
unkempt rock
#

like just let me use python or those real codes and finish this in like 3min

stuck valley
#

We're not on topic here.

stuck valley
#

I'll add that to my box o' tricks

stuck valley
undone hare
#

I feel like no class will have attributes?

stuck valley
#

it segfaults if you make any classes

#

@undone hare

stuck valley
# undone hare What does this do?

!e

__builtins__.__build_class__=lambda*_:(print('OOP is a sin'),exec((lambda:0).__code__.replace(co_consts=())))
print('test')
class foo:
  def bar(self,x):
    print(x)
fallen slateBOT
#

@stuck valley :x: Your eval job has completed with return code 139 (SIGSEGV).

001 | test
002 | OOP is a sin
stuck valley
#

uh I borked idle

undone hare
#

lol

fast ibex
#

What's up

#

🤣

grave jolt
#

A bit of a type theory moment. Suppose that

MysteriousFunction<T> = (int) -> MysteriousFunction<T>

question: Is MysteriousFunction both covariant in T and not covariant in T?

definition: A type A<B> is covariant in B if (X extends Y) => (A<X> extends A<Y>). In case of a function like this, it's covariant if the argument types either don't depend on T or are contravariant in it, and the return type is covariant in T or doesn't depend on it.

a) Suppose that MysteriousFunction<T> is covariant in T.
then:
✅ none of the arguments depend on T
✅ the return type is compatible, we assumed that (X extends Y) => (MysteriousFunction<X> extends MysteriousFunction<Y>)
Therefore MysteriousFunction<T> is covariant in T.
b) Suppose that MysteriousFunction<T> is not covariant in T.
then:
✅ none of the arguments depend on T
❌ but the return type is not covariant, as we assumed
Therefore MysteriousFunction<T> is not covariant in T.

#

(You could imagine that this isn't just a standalone function, but an object like

class Mystery<T>
    (int) -> Mystery<T>
    () -> Sequence<T>
``` which leads to the same properties, but isn't completely useless)
verbal escarp
#

type theory.. where math dropped all pretense to make any sense 😄

#

reminds me of bertrand russell's barber

#

which in turn reminds me of monty python's barber..

#

"i never wanted to be a mathematician - i wanted to be a lumberjack!"

elder blade
#

Can you remind me what covariant, contravariant, and invariant is again?

grave jolt
#

A type is contravariant if Type[Supertype] is a subtype of Type[Subtype], e.g. (Animal) -> str is a subtype of (Cat) -> str

#

A type is invariant if it's neither, for example, list[Cat] and list[Animal] are unrelated

grave jolt
#

No

#

wait

#

yes

#

now there isn't

elder blade
#

A super type is commonly a parent of a subclass right?

grave jolt
#

yeah

#

Well, it's not necessarily a subclass. For example, int is a subtype of Union[int, str]

#

so more like a subset

elder blade
#

When is it the other way around and as such a contravariant type?

grave jolt
#

a function is an example

elder blade
#

When Type[SuperType] is a subtype of Type[SubType]?

grave jolt
# grave jolt a function is an example
NameGetter = Callable[[T], str]

def get_animal_name(animal: Animal) -> str: ...
def get_cat_name(animal: Cat) -> str: ...

get_animal_name is a generalization of get_cat_name, because it fits whenever get_cat_name fits, but also handles other animals.

#

So NameGetter[Animal] is a subtype of NameGetter[Cat]

elder blade
#

The way I see it, a subtype is a more narrow type. Which doesn't fit into what you're explaining 🤔

grave jolt
#

A is subtype of type B if it supports all the operations B supports

#

Cat is a subtype of Animal because it supports all operations an Animal supports, plus perhaps some extras

#

the operation NameGetter[Cat] can perform is get a str object from a Cat object

NameGetter[Animal] is a subtype of NameGetter[Cat] because it can do the operation NameGetter[Cat] does, plus perhaps some extras

#

In other words, if you have some place where you can put a NameGetter[Cat] function:

def print_cat_names(cats: list[Cat], get_name: NameGetter[Cat]):
    ...

you can plug in a NameGetter[Animal] as well

#

There is a related mind-bending pair of statements in logic:

((A => B) => (A => C)) => (B => C)
((B => A) => (C => A)) => (C => B)
elder blade
#

Can you explain to me how the following works then?

  • A is a subclasstype of B
  • list[B] is annotated in a function parameter
  • Why can I not pass list[A]?
grave jolt
#

that's why list is said to be invariant in its type parameter

elder blade
#

Ah I see that makes more sense, is this the case for tuples as well?

grave jolt
#

No, because you can't mutate a tuple

elder blade
#

Because, they're immutable so I guess you shouldn't be able to cause this issue?

elder blade
grave jolt
# elder blade Yeah

You can also think of it like this: a list is a pair of functions: py class list[A]: append(item: A) get_item(index: int) -> A The first one is contravariant in A, the second one is invariant in A. So if you had a B which is a subtype of A,

  1. if list[B] is a subtype of list[A], then (item: B) -> None would be a subtype of (item: A) -> None, which is not possible
  2. if list[A] is a subtype of list[A], then (index: int) -> A would be a subtype of (index: int) -> B, which is not possible
#

and a tuple only has contravariant functions

#

well... not really, it also has __contains__ (in), but it's sort of supposed to work with Any object...

#

so just close your eyes when you look at __contains__

elder blade
#

So (item: A) -> None is a subtype of (item: B) -> None?

grave jolt
#

Yep, like (Animal) -> None is a subtype of (Cat) -> None

#

or, more extreme example, (Any) -> str is a subtype of (int) -> str,

#

because into the left function you can put anything, including an int

#

so it satisfies the interface of the right function

#

but you can't pass (int) -> str where (Any) -> str is expected

elder blade
#

But say that B adds an extra method. Now A does not support all operations that B does. So it is invariant in the scope of (item: ...) -> None?

grave jolt
elder blade
#

If the classes look like this: ```py
class A:
value: str

def __init__(self, val) -> None:
    self.value = val

class B(A):
def merge(self, other: A) -> None:
self.value += other.value


`B` is a subtype of `A`
#

But (item: A) -> None can't be a subtype of (item: B) -> None now?

grave jolt
#

wait, no, I misread

verbal escarp
#

imo we should forget about types and focus on interfaces

grave jolt
#

well, in this context those are kind of synonymous

verbal escarp
#

not really

#

it would turn the whole thing around

grave jolt
#

one interface can be a subtype of another, and the same rules apply

verbal escarp
#

well, let's say a type is a set of methods/attributes

#

in the mathematical sense

#

then one would have to check if the set of all methods/attributes of a given object is a superset of the set of methods/attributes that would define a given type

#

so instead of saying "this is type.. therefore it must have this and that method" one would say "it has those methods, therefore it must be of type.."

flat gazelle
#

it depends on what problem you are modelling

grave jolt
flat gazelle
#

sometimes, you want 2 distinct types which both have the same set of possible values

grave jolt
#

yeah, like Point2D and Point3D are different, but Point3D technically satisfied the interface that Point2D has

elder blade
verbal escarp
#

the methods for Point2D and Point3D have different signatures, therefore they are not the same

flat gazelle
#

they don't

#

or well, methods yes

#

attributes no

grave jolt
#
class Point2D:
    x: int
    y: int

class Point3D:
    x: int
    y: int
    z: int
verbal escarp
#

anywhere where you have (x,y,z) as signature, there's a difference

flat gazelle
#

but you can see why passing a Point3D into a Point2D accepting method would be incorrect

#

even if it is correct under a non-nominal type system

flat gazelle
#

I think a valid solution is the Pony approach of just supporting both

verbal escarp
#

-v 🙂

#

"care to elaborate?"

flat gazelle
#

you have both java style interfaces and go style interfaces

#

so Point2D and Point3D would be traits, but Iterable would be an interface

elder blade
#

Why is this called Pony-approach?

flat gazelle
#

Pony is a programming language

elder blade
#

Ah lmao

#

I see

verbal escarp
flat gazelle
#

traits can be of other traits and classes can be of both traits and interfaces

finite dirge
#

Pls help

lean basin
#

Hey so I am building a content aggregator using Django and Python. On the back end I plan on setting up a scheduling service to run my scraping scripts that will collect the data (in this case news links) and django to handle the website and the templates. However, I have hit a wall. I dont know how to organize my program in a way that makes in scalable. I feel like I only have one shot at this so I dont want to mess the organization up. How should I go about this?

lean basin
#

Well what I worry about is that point where I have scaled and realize I can scale no more because I haven't gone about structuring the project the right way.

verbal escarp
#

i don't think there's any such point if you have a working unit test infrastructure that allows you easy refactoring

grave jolt
#

stares at our bot

verbal escarp
#

<.<

verbal escarp
grave jolt
#

that was a reference to @fallen slate

#

!source

fallen slateBOT
grave jolt
#

nevermind

paper echo
#

@flat gazelle this kind of reminds me of the Clojure approach too

#

Also it's exactly why i want to be able to derive a Protocol from (a subset of) an existing class

#

ProtocolOf[Point3D, 'x', 'y']

#

I also want ParamSpecOf[foo_func]

#

The Clojure spec/select proposal is brilliant here

#

The difference in that language is that spec only deals with keys in tables/mappings, not attributes and methods also

#

It'd be really interesting to be able to further specify the subset of x and y's attributes that you need

#

But there are trade-offs in that the interfaces become fragile, as soon as you need another attribute do you break other peoples code or at least their types

cyan elbow
#

Ok well I have uninstalled vtk wheel and reinstalled pip and changed python version but nothing I do seems to work and it sometimes get stuck on running setup.py install for mayavi ...\

paper echo
#

But I think there could be a lot of benefit to exploring this space in language design

cyan elbow
#

If anyone is wondering why I need the plugins Im trying to experiment with plotting 3D weather radar Data

verbal escarp
unkempt rock
#

Help :/ in currently new to python and a beginner :/

elder blade
unkempt rock
#

:/

#

if i do most of people gonna laugh at me ;(

#

I just want a person to help me teach python :(

elder blade
lofty pilot
#

Hello

unkempt rock
#

k thanks :/

elder blade
sacred yew
#

also post text instead of images

#

@cyan elbow

mystic cargo
#

Hey @cyan elbow, your question is off-topic for this channel. Please read #❓|how-to-get-help which will instruct you on how to claim a help channel.

oblique thistle
#

I have just a code with 1 final error to test

elder blade
#

How does overloads work in stub files?

#

Say I have two ways to use a function, do I still need to have option 1, option 2, and a final function that encapsulates both?

#

Can I have option 1 and have a final function that is option 2?

prime estuary
#

You do yes.

prime estuary
elder blade
#

But in a stub file?

prime estuary
#

In a stub you omit that undecorated one.

#

So in that case just two decorated.

elder blade
#

Alright thanks

#

What order do overloads go in again?

prime estuary
#

First which matches wins.

unkempt rock
#

Why is bytes in the suggested way to type hint a filename? Union[str, bytes, os.PathLike] https://stackoverflow.com/a/53418245 Wouldn't it be weird to have raw bytes that are a path rather than just a string rooThink1

charred wagon
#

Because on unix at least paths don't have to only be strings

raven ridge
#

If you only allow str strings, you'll be unable to cope with non Unicode file names. On Linux for example, you can create a file like:

with open(b"\xC3", "w"):
    pass
unkempt rock
#

oh rooWutClap

raven ridge
#

That file name is not valid UTF-8.

unkempt rock
#

how do you touch a filename like that to make it? or cd into a dir named like that rooThinkingNut

raven ridge
#

Depends on the shell, but for bash or zsh,

mkdir $"\xC3"
cd $"\xC3"
#

That should do the trick, though I can't test right now

unkempt rock
#

hmm, when I do with open(b"\xC3", "w"): in wsl I end with a file that shows as ''$'\303' but it cats properly

raven ridge
#

!e ```py
print(0xC3)
print(0o303)

fallen slateBOT
#

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

001 | 195
002 | 195
raven ridge
#

Same thing.

#

That's just how ls is choosing to render the non Unicode file name

unkempt rock
#

huh, I didn't realize it was octal rescMagic

static bluff
#

I have an object attached to my Element class called the 'Style' class

#

It works just the an element's style attribute in Javascript — a container for every CSS style attached to an element. In Python, the Style class basically just a namespace inside a class filled with hundreds of Attribute objects, descriptors

#

Style looks these values up using both dot and dict notation

#

What kind of object is this style object? A namespace?

#

Its dictionary-like in some respects I suppose

paper echo
#

It's a huge data class

#

No logic, just a bundle of semantically related attributes

#

Not that there's any kind of well established taxonomy of classes anyway

static bluff
#

Rockin

static bluff
#

So, lets say I'm working away on this dictionary that enforces annotations

#

The actual method of enforcement is neither here nor there

#

Some implementation of beartyping's logic in the __setitem__ of the dictionary

#

But for static checking I'd want to inherit typeddict

#

How would I implement this?

#
class EnforcementDict(TypeDict, total=False):

  def __setitem__(self, key: Hashable, value: Any):
     
    SOME_TYPE_ENFORCEMENT_LOGIC(self, key, value)

    super().__setitem__(key, value)
#

How do I get the annotations for values into this?

stuck valley
prime estuary
#

What you could do is something like this:

class EnforcementDict(TypedDict, total=False):
  pass

def enforced_dict_setitem(self, key: Hashable, value: Any):
    SOME_TYPE_ENFORCEMENT_LOGIC(self, key, value)
    dict.__setitem__(self, key, value)
EnforcementDict = type('EnforcementDict', (dict, ), '__setitem__': {enforced_dict_setitem})  # type: ignore

Or in other words replace the type in a dynamic way, so it's not visible to type checkers.

#

typing.TYPE_CHECKING could also work, though I don't know how well that always behaves when replacing stuff.

static bluff
#
Schematic = TypeDict('Schematic', {
  'some-kebab-case-value': int,
})

class EnforcementDict(Schematic, total=False):

  def __setitem__(self, key: Hashable, value: Any):
     
    SOME_TYPE_ENFORCEMENT_LOGIC(self, key, value)

    super().__setitem__(key, value)
#

PyCharm's linter seems to pick up on this just fine, but this doesn't seem right to me. And my library uses keys in kebab case so using the normal syntax wouldn't work — though I guess there's no reason I have to use kebab case, other than its consistent with the rest of the API which for technical reason favors kebab

paper echo
#

This looks fine to me

nova iris
#

place your bets, everyone! what will happen next??

#

and what would happen if it's an integer?

#

first to get it right without searching documentation/testing the code can get bragging rights lemon_swag

nova iris
#

oh god what in the fk have i done

#

just. how. is this even legal.

#

nope, it actually raises an error since the metaclass param of __init_subclass__ isn't specified

#

the metaclass kwarg has priority on actual metaclass specification

#

lol

#

all this time, i thought it had to be a class

#

but any callable will do-

#

trivia: what is type(C)? (could be a trick question 👀)

#

no lol, classes are types (v2.3+, that is)

#

ofc, metaclasses are types

#

you didn't guess MetaB yet lmao (but it's wrong anyways)

#

nope

#

nope

#

actual answer is:

#

TypeError. it's a metaclass conflict, none of the metaclasses specified is the most derived

#

well, the class can't even exist

#

it's an error lol

#

which is why i said it's a trick question

#

lol

#

man, these obscure weird python stuff is fun

#

i don't either :P

#

well, an ABC vs a mcls i do

#

but no idea about protocols

#

oh lmao you're asking me

#

i didn't know it was a question

#

a metaclass creates classes

#

and can customize their behaviour