#internals-and-peps

1 messages · Page 133 of 1

peak spoke
#

If you take this for example

import inspect
import sys

def all_kwargs(func):
    locals = sys._getframe().f_back.f_locals
    d = {
        name: locals[name]
        for name, param in inspect.signature(func).parameters.items()
        if (
                param.kind is inspect.Parameter.KEYWORD_ONLY
                or param.kind is inspect.Parameter.VAR_KEYWORD
        )
    }
    d.update(d["kwargs"])
    del d["kwargs"]
    return d

some_list_of_names = ["b", "c"]

def foo(*, a, b, **kwargs):
    return [all_kwargs(foo)[name] for name in some_list_of_names]

foo(a=2, b=3, c=4)

It can easily be avoided and would make more sense outside of the list comp, but the error is not exactly clear

raven ridge
old hearth
pliant tusk
#

!pypi fishhook is also an option

fallen slateBOT
verbal escarp
#

forbiddenfruit and fishhook is so black magic.. i wonder how many chickens had to be sacrificed to make those work xD

boreal umbra
#

Wait no

#

!otn delete python sacrificing chickens to forbiddenfruit

#

!otn a sacrificing chickens to forbiddenfruit

fallen slateBOT
#

:ok_hand: Added sacrificing-chickens-to-forbiddenfruit to the names list.

verbal escarp
#

what names list xD

grave jolt
#

the channels in the off-topic category rotate their names, which are taken from a giant (1342) pool

raven ridge
#

!otn a what names list?

fallen slateBOT
#

:ok_hand: Added what-names-list? to the names list.

white nexus
#

lol i remember this

boreal umbra
#

When creating a library, what should determining the right dependency versions entail? I don't like when libraries try to lock you in to exact versions as this can result in the inelegant scenario of having to clone the library and change the requirements manually if you need a different version that would work.

verbal escarp
#

we still have issues with the auto-install feature, but we're getting there

#

when it's reliable enough you could use it to fix your private dependencies to specific versions you know that work without affecting other dependencies

#

if you have any suggestions regarding private dependencies/versions - maybe auto-upgrading specific dependencies etc. feel free to share 🙂

silver field
verbal escarp
silver field
#

yeah, it's in c++

verbal escarp
#

any practical uses?

silver field
#

no. I just wanted to try to implement some python in c++ for fun.

#

and share it with the python server to see if I can fool them.

#

I mean there are practical uses as it's faster than python (at just this task of mapping, getting input, and splitting) because python is slow, while being the same. But I'm not going to use it.

white nexus
#

uh

#

!e ```py
oof = []
print(all(oof))
print(any(oof))

fallen slateBOT
#

@white nexus :white_check_mark: Your eval job has completed with return code 0.

001 | True
002 | False
white nexus
#

could someone explain 👀

runic musk
#

What do you mean?

flat gazelle
#

@white nexus
There are no True elements in an empty list -> any([]) is False
Every element is True in an empty list (no elements are False)-> all([]) is True

white nexus
#

wait....

#

they are inverses... lol

#

i did not realise how they were lol

#

any is checking for just one True

#

and all is checking for just one False

elder blade
#

any is or

#

all is and

turbid sequoia
#

Could anyone tell me the basics of Python

#

If you can I am really appreciate it

#

Pls

verbal escarp
#

you should try to ask in one of the help channels 🙂

dusty oak
#

this library proves it.

#

just kidding 😆

raven ridge
# boreal umbra When creating a library, what should determining the right dependency versions e...

If the library uses semantic versioning, you would normally require somelib>=2.3,<3.0 or something like that - where "2.3" is the lowest version you've tested with and know works, or the most recent version that introduced a new feature that you rely on, and you want something less than 3.0 because you know 3.0 will contain backwards incompatible changes, so you can't know if your code will work with it or not.

#

That is, you want want any 2.x version, where x >= 3

dusty oak
#

tox could be used for fast test running in multiple environments

#

at least running for different python versions(matrix mode like in github CI) it worked fine

raven ridge
#

If the library doesn't use semantic versioning, then it comes down to knowing the particular versioning scheme it uses, and whether there's any way to predict backwards incompatible changes

verbal escarp
#

in justuse i had the idea of auto-upgrading as long as functions signatures are compatible

#

the versioning wouldn't matter that way

#

you'd basically choose a base-version and then it'd try to install all versions up to the latest one that still has the same signatures as the one you chose

#

the results of the effort could be shared via P2P, similarly to how i'm imagining sharing dependency information

#

so next time someone else would try to auto-upgrade from the same base, they could pull the info from their peers and go to the latest compatible version directly

#

basically pushing python-wide code-by-contract 🙂

#

then you could practically ignore all the versioning but instead go by release date and signature-compatibility

#

easy ^^

grave jolt
#

why does signature similarity imply version compatibility? sounds strange

#
# v1.0
def pi() -> float:
    return 3.14159

def e() -> float:
    return 2.718
``` ```py
# v2.0
def pi() -> float:
    return e()

def e() -> float:
    return 3.0
elder blade
#

Well in this case the typings are wrong

elder blade
#

But if the behaviour changes then you have to be explicit

#

You're only implying that it's version compatible, which is very weak

grave jolt
#

many of them are written on a napkin

#

especially in the case of **kwargs and such

elder blade
#

Oh yeah, I am not disagreeing with you. Who is?

verbal escarp
#

i am not disagreeing, but it's better than what we have now

#

also, we don't really have a language for pre/postconditions in annotations atm, which would make sense for this

#

it's not a guarantee that functions are compatible if signatures match, but it is clear that they are incompatible if signatures mismatch

peak spoke
#

I'd rather always leave upgrading to the user, dependency managers make it fairly easy, both within constraints and to update the constraints to a new ver

verbal escarp
#

i'm not sure if the user knows what they are doing when it comes to upgrading

#

maybe one could inform the user about incompatibilities and leave the decision to the user, yes

#

when pip says "this and that needs version x, upgrade now?" you usually say yes without thinking about the implications..

#

i'm suspecting that there will hardly ever be an auto-upgrade like this for a while since nobody is aware that signature-equality could be a thing

#

maybe for simple bug fixes

grave jolt
#

What's the conventional way of documenting attributes, namely dataclass attributes?

peak spoke
#

class docstring?

grave jolt
#

you mean, like this?

@dataclass(frozen=True)
class Animation(Generic[A]):
    """
    :param duration: How long the animation should last for
    :param projector: Strategy to get a still frame from a progress in [0; 1]
    """
    duration: float
    projector: Projector[A]
#

or a similar style

peak spoke
#

Yes, I think that's mostly what I've encountered.
I'm not sure if it was only a proposal or handled by sphinx but I believe I also saw "docstrings" under the attr names

grave jolt
#

not sure if editors pick up on it

lusty scroll
grave jolt
#

yeah that's what I gathered

#

I'm not concerned about autodoc, more with which way will be more conventional in the code itself

lusty scroll
grave jolt
#

yep

#

I tried this:

@dataclass(frozen=True)
class Animation(Generic[A]):
    duration: float
    "How long the animation should last for"

    projector: Projector[A]
    "Strategy to get a still frame from a progress in [0; 1]"

seems like VSCode picks up on these docstrings.

peak spoke
#

doesn't look like pycharm can fwiw

paper echo
#

Incidentally you could probably abuse annotations for some kind of macro system

paper echo
grave jolt
#

resurrecting pep 224?

#

!pep 224

fallen slateBOT
#
**PEP 224 - Attribute Docstrings**
Status

Rejected

Python-Version

2.1

Created

23-Aug-2000

Type

Standards Track

grave jolt
#

jesus, that was 21 years ago

paper echo
#

Yeah wow TIL

deft pagoda
#

you were 21 years ago

grave jolt
#

I wasn't 21 years ago (literally)

paper echo
#

And yes exactly but also for arbitrary top level variable assignments

paper echo
deft pagoda
#

i still say that

peak spoke
#

I don't really like the proposed __doc_name__ attrs but a dict like annotations could work

deft pagoda
#

would __annotations__ be a dict of dicts

paper echo
peak spoke
#

__doc__ where?

paper echo
#

Why should i have the documentation for the class on an instance thereof?

grave jolt
#

pydantic uses Annotated for this

class User(BaseModel):
    id: Snowflake
    username: str
    discriminator: Annotated[str, Field(description="4-digit tag")]
#

or assignment with Field

deft pagoda
#

if they add a __doc__ you have to also add a __marty__

paper echo
#

They already have to add it

#

The problem with __doc__ is that it's attached to the object itself, not the binding

#

So i see the value in __doc_<foo>__

#

But it could be a module-level dict of "binding docs"

deft pagoda
#

maybe __attr_docs__

paper echo
#

At the module level yeah

deft pagoda
#

never mind, i hate it

peak spoke
paper echo
#

Maybe we need a way to append to annotations

peak spoke
#

It's nice to parse with tools, but not by humans

paper echo
#

Another use for typing.Concatenate perhaps

grave jolt
deft pagoda
#

i think it belongs in __annotations__ somewhere

paper echo
#

What does __annotations__ look like when using Annotated?

grave jolt
#

it just stores the Annotated

paper echo
#

Ugly

grave jolt
#

why?..

peak spoke
#

it has to store it

grave jolt
#

!e

from typing import Annotated

def f(x: Annotated[int, "quack"]) -> str:
    ...

print(f.__annotations__)
paper echo
#

It'd be nice if you could annotate with a dict like salt is maybe suggesting

fallen slateBOT
#

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

{'x': typing.Annotated[int, 'quack'], 'return': <class 'str'>}
paper echo
#

Or at least name the Annotated elements

#

Maybe kwargs are allowed in class getitem?

peak spoke
#

get_type_hints won't include it if you only care about typehints

paper echo
#

Yeah im more spitballing ideas about what could be more ergonomic and less ugly

deft pagoda
#

typing in python is ugly almost by design

#

i swear it

paper echo
#

Python is actually pushing limits in this area. Type declarations are ugly in a lot of languages

paper echo
#

Like lambdas

peak spoke
#

most languages also don't try to pack as much into typing

paper echo
#

Or don't have to because they're statically typed

deft pagoda
#

lambdas ugly too, no doubt --- should have used generator expressions instead

paper echo
grave jolt
#

TypeScript has pretty cool annotation syntax

deft pagoda
#

f: x + y for x, y

#

or use from instead

paper echo
#

Interesting

#

That's a great proposal

deft pagoda
#

f = x + y from x, y

#

something like that

paper echo
#

That still doesn't support multi expression functions though

deft pagoda
#

no

paper echo
#

Could alias lambda to def at least

deft pagoda
#

simple, just use brackets and semicolons

grave jolt
#

cursed suggestions, volume 2

@document
def distance(
    x: "horizontal coordinate",
    y: "horizontal coordinate",
) -> "distance from origin to (x, y)":
    ...

def distance(x: float, y: float) -> float:
    return (x**2 + y**2) ** 0.5
paper echo
#

Honestly not cursed at all

#

Match up the names and append the docstring

deft pagoda
#

i would like this if they separated the typing as well

grave jolt
#
@d
def ocument(
    x: "horizontal coordinate",
    y: "horizontal coordinate",
) -> "distance from origin to (x, y)":
    ...

@d.ocument.ed
def distance(x: float, y: float) -> float:
    return (x**2 + y**2) ** 0.5
deft pagoda
#

oh hi there attr.s

paper echo
#

@typedef and @document

deft pagoda
#

thi.s i.s tota.lly read.able

paper echo
#

They do have the aliases with more normal names

#

Also the point is that the namespace attr is distinctive, this isn't some test framework expect(x).to.equal(y) shit

deft pagoda
#

i haven't actually used a dataclass in some long amount of time

grave jolt
#
class distance(metaclass=ActuallyThisIsAFunction):
    x: float
    "horizontal coordinate"

    y: float
    "vertical coordinate"

    returns(float)
    "distance from origin to (x, y)"

    def __call__(self):
        return (self.x**2 + self.y**2) ** 0.5
deft pagoda
#

cursed

#

obviously the better distance function is abs(complex(x, y))

grave jolt
#

You could mark *args as _*"args" and kwargs as _**"kwargs"

verbal escarp
#

"I "kinda" like the idea of having attribute docstrings (meaning it's not of great importance to me) but there are two things I don't like in your current proposal:

The syntax you propose is too ambiguous: as you say, stand-alone string literal are used for other purposes and could suddenly become attribute docstrings.
I don't like the access method either (doc<attrname>_)." - GvR

#

in the PEP

#

maybe you could give ... additional meaning?

#
class Foo:
  x: float
  ... "bla x blurp"
paper echo
#

i like salt's suggestion:

x: int = 123
"The special number"

becomes

x: int = 123

__attr_doc__['x'] = "The special number"
#

although i do also like the idea of attaching a docstring directly to that specific instance of 123, but it would probably be impossible and/or break a lot of internal optimizations

verbal escarp
#

i mean ... has no meaning on its own, it should be a fairly inambiguous token

paper echo
#

no, the old pep had a different implementation

native flame
#

Why not # comments
like x: int # description of x

peak spoke
#

because you want it to be available at runtime

paper echo
#

that, and because comments are comments, and especially line comments are already overused for things like pragmas

#

it's already unclear how to combine flake8, mypy, and coverage pragmas

#

(yes, i have wanted all 3 on a line before)

native flame
#

hmm

paper echo
#

and also because the subsequent-string-literal-as-docstring thing is used in sphinx and epydoc

elder blade
#

I thought it was specified you can do ```py
my_var = 0
"""A string for it."""

paper echo
#

and it's a very natural extension of function and class docstrings

elder blade
#

Like, through convention

paper echo
#

@elder blade yeah that was the 21 year old pep 224

elder blade
#

Ah, there you go. Has anything happened to it?!

paper echo
#

it was rejected because the proposed implementation of __doc_<name>__ is ugly, and i agree with that rejection reason

#

salt-die suggested a module-level dict __attr_docs__, which i find more appealing for a variety of reasons

verbal escarp
paper echo
#

now that sphinx and epydoc have more or less standardized it, people might be open to it

#

that, and/or standardizing some usage of Annotated

#
x: Annotated[
    type=int,
    doc="This is the special parameter."
]
x = 123

supporting kwargs in Annotated.__class_getitem__ might be nice too... is that even valid syntax?

#

!e ```python
class Foo:
@classmethod
def class_getitem(cls, *args, **kwargs):
print(args, kwargs)

Foo[99, x=1, y=2]

fallen slateBOT
#

@paper echo :x: Your eval job has completed with return code 1.

001 |   File "<string>", line 6
002 |     Foo[99, x=1, y=2]
003 |              ^
004 | SyntaxError: invalid syntax
paper echo
#

nope

#

too bad

verbal escarp
#

hmm.. rarely i liked to comment code out by putting them in """ for convenience, but i guess i could make do without

paper echo
#

i suppose it doesn't make sense because , in __getitem__ is really a tuple, right?

#

!e ```python
class Foo:
@classmethod
def class_getitem(cls, *args):
print(type(args), args)

Foo[1,2,3]

fallen slateBOT
#

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

<class 'tuple'> ((1, 2, 3),)
paper echo
#

yeah

#

i was also proposing the ability to append to an existing annotation

verbal escarp
#

having attr-docstrings would be consistent with the class docstrings

paper echo
#

right

#

and again, sphinx and epydoc already use them for that purpose

verbal escarp
#

oh, i didn't know

#

well then 🙂

#

i wonder how doctests work with those

elder blade
#

Oh I see now

#

You tried to use kwargs

#

!e ```py
from typing import Annotated

x: Annotated[
int,
"This is the special parameter."
] = 123

fallen slateBOT
#

@elder blade :warning: Your eval job has completed with return code 0.

[No output]
paper echo
#

the problem is if 2 different libraries start claiming uses for the 2nd-n'th positional args there will be trouble

#

libraries will have to start "guarding" their annotations with something like

x: Annotated[
    int,
    Doc["This is the special parameter"],
]
verbal escarp
#

maybe those docstrings might be a better place for guards (aka pre/post-conditions) instead of annotations?

paper echo
#

what do you mean by that?

lusty scroll
#

If you go with readonly properties, those can have runtime docstrings

paper echo
#
def atan2(
    y: float, x: float
) -> Annotated[
    Returns[float],
    Assert[
        not math.isclose(x, 0) or not math.isclose(y, 0),
        "X and Y both cannot be 0."
    ],
    Assert[
        (
            0 <= __return__ <= math.pi
            if y >= 0
            else -math.pi < __return__ < 0
        ),
        "y in [0, pi] if nonnegative, else in (-pi, 0) -- the overall range is (-pi, pi]."
    ],
]:
    ...
#

kind of cursed

#
@signature
def atan2(y: float, x: float) -> float:
    """This is where the docstring goes."""

@assertion
def atan2(y, x) -> Annotated[
    not math.isclose(x, 0) or not math.isclose(y, 0),
    "X and Y both cannot be 0."
]: ...

@assertion
def atan2(y, x) -> Annotated[
    (
        0 <= __return__ <= math.pi
        if y >= 0
        else -math.pi < __return__ < 0
    ),
    "y in [0, pi] if nonnegative, else in (-pi, 0) -- the overall range is (-pi, pi]."
]: ...

def atan2(y, x):
    # Implementation
    ...
lusty scroll
paper echo
#

at least, as of 3.10, or if using from __future__ import annotations in a prior version

#

heck maybe you can just combine multiple @signature annotations and they'll be "union"-ed together

#
@signature
def atan2(y: float, x: float) -> float:
    """This is where the docstring goes."""

@signature
def atan2(y, x) -> Assertion[
    not math.isclose(x, 0) or not math.isclose(y, 0),
    "X and Y both cannot be 0."
]: ...

@signature
def atan2(y, x) -> Assertion[
    (
        0 <= __return__ <= math.pi
        if y >= 0
        else -math.pi < __return__ < 0
    ),
    "y in [0, pi] if nonnegative, else in (-pi, 0) -- the overall range is (-pi, pi]."
]: ...

def atan2(y, x):
    # Implementation
    ...
lusty scroll
#

so they get parsed, but not evalled

paper echo
#

i think so

#

i'm not sure, e.g. syntax errors are still syntax errors afaik

#

but i think they're also stored literally as a string in __annotations__

lusty scroll
paper echo
#

i genuinely dont want to know 😛

#

i think my @signature idea could work

#

multiple Assertions in the return type hint would be concatenated, otherwise it would raise an error if you specified 2 different types for the same parameter or the return value

#

and you'd be able to annotate parameters with Assertion too

#

as well as Documentation perhaps?

#

and same with docstrings, error at function definition time if you specify more than one

#
@signature
def atan2(y: float, x: float) -> float:
    """Efficiently compute arctan(y/x), with adjustments for the signs of x and y."""

@signature
def atan2(
    y: Documentation[
        """The vertical coordinate (the 'opposite' side of the right triangle)."""
    ],
    x: Documentation[
        """The horizontal coordinate (the 'adjacent' side of the right triangle)."""
    ]
): ...
#

sigh, another project for the backlog

vast cloud
#

So Im using numba's JIT compiler to speed up and parallelize parts of my python code.
What is the actual difference between JIT compiling and the python interpreter (except of caching)?

paper echo
#

a lot less overhead

#

most things in python are represented as PyObject* and there's a lot of stuff that needs to happen to actually do things with such objects

#

numba can represent the data a lot more efficiently, as well as eliminating things like the large overhead of calling python functions, and probably even optimize certain algorithms to be more efficient

vast cloud
#

Ah I understand, is there some major difference if looking at the level of bytecode/machinecode, too?

paper echo
#

i assume so, but i have no idea how numba works internally

flat gazelle
#

well, cpython compiles to bytecode and then interprets that, numba goes all the way to native machine code using llvm and then attaches some converters to the function to wrap it back into pyobjects once done and unwrap the arguments from pyobjects

paper echo
#

so that's how it works

#

kinda magic

flat gazelle
#

the converters can sometimes be slow enough that the numba function ends up slower than the regular python one

vast cloud
#

Great, thanks for helping me understand @paper echo & @flat gazelle

lusty scroll
paper echo
#

yeah there are a few others @lusty scroll , such as icontract and deal

#

!pypi icontract

fallen slateBOT
paper echo
#

!pypi deal

fallen slateBOT
#

**Deal** is a Python library for [design by contract][wiki] (DbC) programming.

paper echo
lusty scroll
#

icontract is impressive

#

it would be nice if the actual type hints were checked at runtime as well; I guess that could be done with decorators somehow

main lynx
#

a few words about deal and icontract, when i tested them i found that icontract is much faster in -O (basically zero overhead) but about half as fast as deal in debug mode

#

because deal does a thing where you can turn assertions on and off at runtime (which i really don't care about) it can't just evaluate __debug__ at import time and eliminate that branch

#

both are much more expensive in debug mode than doing the same thing with assert

#

so fwiw i would always choose icontract because it has the switch for near-complete transparency in production

main lynx
#

crosshair looks great, practically no runtime cost ever

verbal escarp
#

wow, icontract looks really nice

#

i wonder what's the background of it

#

university or industry project

#

what's Parquery?

paper echo
#

ty for the field reports @main lynx

main lynx
verbal escarp
#

ohhh

#

that makes sense

#

@lusty scroll let's use icontract 🙂

paper echo
fallen slateBOT
#

Modules/clinic/posixmodule.c.h lines 1001 to 1011

#define OS_GETCWD_METHODDEF    \
    {"getcwd", (PyCFunction)os_getcwd, METH_NOARGS, os_getcwd__doc__},

static PyObject *
os_getcwd_impl(PyObject *module);

static PyObject *
os_getcwd(PyObject *module, PyObject *Py_UNUSED(ignored))
{
    return os_getcwd_impl(module);
}```
verbal escarp
#

that's c, that*s not python :p

paper echo
#

right, but where is the C implementation?

#

i only see that name used in this header file

#

also what's with .c.h

main lynx
#

the indecision extension

verbal escarp
#

it's been a while.. isn't there an argument clinic where they insert code?

paper echo
#

hmmmmm

#

i never knew what argument clinic was

fallen slateBOT
#

Modules/posixmodule.c lines 3810 to 3815

static PyObject *
os_getcwd_impl(PyObject *module)
/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
{
    return posix_getcwd(0);
}```
paper echo
#

huh, github code search failed me?

#

or is that generated per platform?

verbal escarp
#

i tried to wrap my brain around it for the import mechanic, there was a similar thing at the bootstrap if i'm not mistaken

paper echo
#

@lusty wing ☝️

lusty wing
#

got it

lusty scroll
paper echo
#

seems like 1 to inject the platform-specific version, 1 to make a top-level C function, 1 to expose it to the python API

#

so 3-4 depending on how you count

#

5 if you use Pathlib i think

lusty scroll
nimble garden
#

Hi, what program should i download to program in python ? (any other than visual studio)

ashen lance
#

PyCharm works well

nimble garden
#

thx

verbal escarp
#

vscode, juypter lab

#

sublime text

paper echo
#

IDLE or Thonny for simple tasks @nimble garden

#

in addition to the other options above

static bluff
#

Is os.getgid() supported on all major platforms?

unkempt rock
#

The documentation says it's available on Unix, so you won't be seeing it on Windows

surreal sun
#

Anyone worked with rebuilding CPython?

#

So I edited my local CPython, and I did build.bat --regen, but how do I run it with the updated version in the REPL?

static bluff
#

I guess the more salient question is, does windows use process groups?

unkempt rock
#

AttributeError: module 'os' has no attribute 'getgid' I got this on Windows

static bluff
#

Damn

#

Thanks @unkempt rock

surreal sun
#

!e

from forbiddenfruit import curse

function_class = type(lambda: None)


def __matmul__(self, other):
    return lambda item: self(other(item))


curse(function_class, '__matmul__', __matmul__)
curse(type, '__matmul__', __matmul__)

# same as result = lambda x: list(str(x))
result = (list @ str)
# converts integer to a list of strings by digit
print(result(225))
fallen slateBOT
#

@surreal sun :white_check_mark: Your eval job has completed with return code 0.

['2', '2', '5']
surreal sun
#

function concatenation i suppose, since i don't know a better word for it

#

I wonder if there's been a PEP on such a feature

boreal umbra
#

@surreal sun it's function composition and it has been shot down before

#

However I want it and I want it right now.

boreal umbra
#

For one thing, there'd be no way to optimize chains with more than two functions. A new intermediate function would need to be created between each two

#

Unless there was a grammar change.

#

You'd also have to decide if the composite function gets a name and what it would be.

surreal sun
#

ah

boreal umbra
#

Also why didn't you do (print @ list @ str)(256)

surreal sun
#

oh wait true lol

boreal umbra
#

Do it do it do it

#

I'm on mobile

surreal sun
#

!e

from forbiddenfruit import curse

function_class = type(lambda: None)


def __matmul__(self, other):
    return lambda item: self(other(item))


curse(function_class, '__matmul__', __matmul__)
curse(type, '__matmul__', __matmul__)

# same as result = lambda x: list(str(x))
(print @ list @ str)(256)
fallen slateBOT
#

@surreal sun :white_check_mark: Your eval job has completed with return code 0.

['2', '5', '6']
boreal umbra
#

Yessssssss

surreal sun
#

if a library doesn't exist for this i might create a library that just adds function composition (using forbiddenfruit) but i don't wanna break something and forbiddenfruit as a dependency from a library is cursed apparently

paper echo
#

@boreal umbra one issue is ambiguity in meaning of the @ operator

brave badger
#

<< for function composition

paper echo
#

What if you want to make a callable array? Sorry you can't

boreal umbra
paper echo
#

Yeah but two completely different meanings baked into the language

#

Like str %

surreal sun
#

!e

from forbiddenfruit import curse

function_class = type(lambda: None)


def __lshift__(self, other):
    return lambda item: self(other(item))


curse(function_class, '__lshift__', __lshift__)
curse(type, '__lshift__', __lshift__)


# same as result = lambda x: list(str(x))
(print << list << str)(256)
paper echo
#

I guess it's unlikely to collide

surreal sun
#

oh, reverse lshift doesn't exist?

paper echo
#

I think making it "matmul" was a terrible mistake

brave badger
#

__rshift__ ?

surreal sun
#

oh wait lmao

surreal sun
paper echo
fallen slateBOT
#

@surreal sun :white_check_mark: Your eval job has completed with return code 0.

['2', '5', '6']
boreal umbra
brave badger
#

__matmul__ is pratically free outside of numpy

paper echo
#

Right

#

Btw rrshift and rlshift should be valid @surreal sun

boreal umbra
#

And @ is already used for decorators, which is basically exactly fiction composition

surreal sun
#

so i likely messed something up

boreal umbra
#

Function*

paper echo
#

@boreal umbra also it's better to have it than to worry about some edge case

surreal sun
#

But why limit to only numeric types?

__add__ doesn't necessarily have to mean add this integer to another within custom implementations

paper echo
#

It's not

surreal sun
#

ohh is that talking about the actual usage of matmul, not custom implementations

paper echo
#

It's just that "addition" or whatever is usually numerical

surreal sun
#

It looks like more of a guideline tbh, __add__ within some implementations of it in built-in types != numerical

#

I'm missing your point I think

#

I'm pretty sure you already know that lol

boreal umbra
#

Add is also used for concatenation

surreal sun
#

Yeah

#

And as @boreal umbra, @ is already used to decorate a function, so it seems like it would fit in the genre of functions

boreal umbra
#

And at that point, the distinction between it and | starts to get fuzzy

surreal sun
#

What's funny is that me creating that function composition or whatever was actually used from a conversation long ago in the beginnings of this chat by you yourself

boreal umbra
#

Yes, I've wanted function composition right now for a long time.

#

And I still do.

#

At this very moment, in fact. I don't want patma btw.

#

(I mean I don't really mind it, I would just rather have function composition.)

surreal sun
#

patma is cool and all, but it's just such an intricate feature that i probably wouldn't find a usage for it until one random day

#

that's how metaclasses came to me

#

i was like "what are the purpose of these lmao i'm just using inheritance"

then, i finally found usages

boreal umbra
#

I'm vaguely aware of meta classes

surreal sun
#

the main thing i've used them for is changing namespaces

boreal umbra
#

And yeah, I don't think I'm the one patma is intended to help.

surreal sun
#

one thing i see it useful for is RPGs

#

One feature that would be quite cool is a none aware operator, but that PEP has already been denied for reasons

boreal umbra
#

I must disappear all of the sudden

#

Good talk

surreal sun
#

Cya

grave jolt
#

function composition is not very useful without haskell-style partial application IMO

#

or whatever elixir has

#

Nobody is stopping you from making a compose function

#

but py compose(partial(foo, 1), partial(bar, baz=42)) partial(foo, 1) @ partial(bar, baz=42) vs ```py
lambda x: foo(1, bar(x, baz=42))

sacred yew
#

alright time to patch __call__ to implement Haskell style currying

cerulean anchor
#

is function composition something like this: func_end(func_1(func_2(func3(args))))

#

In computer science, function composition is an act or mechanism to combine simple functions to build more complicated ones. Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole

surreal sun
#

What is partial again/

sacred yew
#

yeah

surreal sun
#

does it fill in one argument for a function?

sacred yew
#

any number of args, but yes

grave jolt
paper echo
#

In mathematics, function composition is an operation that takes two functions f and g and produces a function h such that h(x) = g(f(x)). In this operation, the function g is applied to the result of applying the function f to x. That is, the functions f : X → Y and g : Y → Z are composed to yield a function that maps x in X to g(f(x)) in Z.
Int...

cerulean anchor
grave jolt
cerulean anchor
#

oh I see, that's cool

#

it remembers to call sqrt first then invert when passed in x? function invsqrt that is

grave jolt
#

yes

cerulean anchor
#

and python is against this idea of what compose is suppose to accomplish in the above example?

#

since this is functional related, does lisp support this ?

sacred yew
#

I mean, you could write your own compose function

#

but it's kinda useless in python

grave jolt
#

Nobody is stopping you from making your own compose function. The discussion above was about giving functions an infix operator (like * or @) that would mean composition

cerulean anchor
grave jolt
#

but yeah, I don't think I've had a case where I needed it to be honest

sacred yew
#

bc there's no way to provide partial arguments without a lot of verbosity

prime estuary
#

And the issue there is that functions are implemented by lots of types, including arbitrary classes, and so there'd need to be new syntax or something...l

cerulean anchor
#

oh kk sorry for jumping in, just didn't know what was being talked about 😄

grave jolt
#

it will end up more verbose than a normal function or a lambda

#

whereas in functional languages composition is sometimes more clear and concise

paper echo
#

* might be interesting for composition

#

Some analogy to algebra as well as to convolution

surreal sun
#

Ok wait - what is functools.partial again?

native flame
#

func(a, b) == partial(func, a)(b) == partial(func, a, b)()

surreal sun
#

oh, what would be the use of that?

static bluff
#

I fail to see how this is difficult to implement

#

If you wanted to be fancy you could even write your own function class with bitwise composition without too much trouble, I'd guess 😐

static bluff
#

How would you apply forbiddenfruit? Just to modify the built in function type?

#

XD Bending Python to one's will — what a pass time

surreal sun
static bluff
#

XD

#

Is forbiddenfruit really forbidden?

surreal sun
#

Haha no, it's just the name

#

there's also fishook

#

!pypi fishhook

fallen slateBOT
static bluff
surreal sun
static bluff
#

Syntax abuse, used sparingly and with great care, can make all the difference

#

And sometimes that means fudging the basic physics of the language

native flame
#

The way I understood this behaviour is that strings cannot be interned at compile-time because it can't know if the str used here is the builtin str in the first place:

>>> a = str(1)
>>> b = str(1)
>>> a is b
False

But how come this happens, then?

>>> a = str("1")
>>> b = str("1")
>>> a is b
True
static bluff
#

Huh

#

If you figure it out, let us know

#

That's kinda curious

surreal sun
fallen slateBOT
#

@surreal sun :white_check_mark: Your eval job has completed with return code 0.

001 | 139669728400176 139669728400176
002 | 139669728370480 139669728370480 139669728370480
surreal sun
#

the ids are the same??

static bluff
#

See that's what I thought would be the case, and the reason I was surprised to the the False in the equality before

native flame
#

if it happens in the same line things work differently

static bluff
#

Strings are cached are they not?

native flame
#

yes but at compile time

static bluff
#

Ohhhhhh so creating two identical strings at runtime creates two different but equal objects?

native flame
#

yeah

static bluff
#

Huh

native flame
#

or, well, so i thought

#

the second snippet i posted seems to say otherwise

surreal sun
#

!e

print(id(str(1))
print(id(str(1))
fallen slateBOT
#

@surreal sun :x: Your eval job has completed with return code 1.

001 |   File "<string>", line 2
002 |     print(id(str(1))
003 |     ^
004 | SyntaxError: invalid syntax
native flame
#

closing paren 😔

surreal sun
#

!e

print(id(str(1)))
print(id(str(1)))
fallen slateBOT
#

@surreal sun :white_check_mark: Your eval job has completed with return code 0.

001 | 139734089790064
002 | 139734089790064
surreal sun
#

bruh

native flame
#

bruh

static bluff
#

bruh

surreal sun
#

!e

a = str(1)
b = str(1)
print(id(a))
print(id(b))
fallen slateBOT
#

@surreal sun :white_check_mark: Your eval job has completed with return code 0.

001 | 139718506726064
002 | 139718506726128
surreal sun
#

there we go

native flame
#

oh right

surreal sun
#

the ids will be different because originally they're integers and only changed to string when it's interpreting i'm guessing?

native flame
surreal sun
#

!e

a = 1
b = 1
print(id(a))
print(id(b))
fallen slateBOT
#

@surreal sun :white_check_mark: Your eval job has completed with return code 0.

001 | 140637506857264
002 | 140637506857264
surreal sun
#

interesting, they're the same as integers

#

it's just when converting to something else that they change ids

native flame
#

ints from -5 to 255 are also cached yeah

brave badger
#

@native flame The repl compiles the lines separately

native flame
#

oh

#

why does it happen in snekbox too then 🤔
snekbox is essentially py -c "code" isn't it

brave badger
#

It's largely inconsistent

139972435808560
139972435808624
#

This is from running

print(id(str(1)))
print(id(str(1)))

in a file

#

Actually, I'm not sure

keen pebble
#

how can I use 2captcha in python

unkempt rock
#

why is python3 not working? help me

surreal sun
#

It looks like it is erroring out on the encoding file which handles utf encoding iirc

fallen slateBOT
random spruce
#

hi just joined, gotta say love the server,

fave lang: mathematica
2nd fave lang: haskell

#

any one knows which channel talks about features to include in next python version ???

#

python should really have go like concurrency

#

though mypy linter and typechecker is cool though

gleaming rover
#

if backwards compatibility wasn't a concern

#

how hard would it be to graft a typechecker onto CPython?

flat gazelle
#

There is a certain subset of python you could maybe statically type. It would probably make the most sense to do it in a style similar to crystal

gleaming rover
#

not in the sense of "how hard is it to typecheck Python", but "how complicated would the addition of a typechecker to the compilation pipeline be"

#

i.e. would it be just like "add a stage after AST is generated and before the rest"?

flat gazelle
#

Generally, type checking runs on the AST, so you could probably just put it there with few issues. It also depends on how much you want to do with the types, e.g. do you want to use them to optimise code, or just as a check.

gleaming rover
#

It also depends on how much you want to do with the types, e.g. do you want to use them to optimise code, or just as a check.
if it were the former, would that be a significantly more involved process?

#

because you'd still perform those operations on the AST, right

flat gazelle
#

Ye, but now the types also need to be moved into the byte code compiler and it needs to be able to use them, and the byte code needs changes to be able to express types

gleaming rover
#

can you elaborate on this part

#

I don't see the necessity

flat gazelle
#

Well, the byte code insn BINARY_ADD (is that the name) runs the full try .add, then .radd. but if yiu knew you had 2 ints due to the type system, you would want a special insn that would only do the 2 ints

gleaming rover
#

ah

#

okay

#

that kinda stuff

#

I get it

#

okay, thanks! 🙇‍♂️

wise comet
#

help

#

how to add more than 1 txt file input to the chatbot i just made

wise comet
#

how to add more than 1 txt file input

verbal escarp
#

much nicer

#

also, in retrospection, i would have proposed a clean function-composition operator like |> in elixir and replace @ for decorators with that

verbal escarp
#

there are so many WTFs around is

visual shadow
#

Not really. Is is always going to be True if two things are the same object. The so called "wtf" are only happening because you're also gaining a lens into the optimisations behind the curtain so to speak when you invoke is with what are supposedly unrelated objects

elder blade
main lynx
#

mypyc-compiled Python checks types at runtime, raises TypeError

verbal escarp
peak spoke
#

the interpreter does try to raise warnings for such uses

#

and well, using is for them isn't recommended anywhere

grave jolt
verbal escarp
#

i never compare numpy arrays with is

#

never even occurred to me

#

what's there to gain that couldn't be done otherwise?

flat gazelle
#

well, a potentially None numpy array would be harder to find

#

but like... id(None) == id(arr)

#

though the difficulty of implementing id also makes it an annoying feature to have

peak spoke
#

Both sides have benefits and tradeoffs but I guess for libs like numpy being able to use is can be a lot more convenient

visual shadow
#

this isn't the fault of is if you try to use it for situations where it shouldn't be used, and then complain about optimizations making it inconsistent, when in fact those optimizations are otherwise fantastic "behind the scenes" stuff that should never be the concern of the average programmer.

#

to phrase it differently, forget is for a second. does caching small ints give a benefit? absolutely. should two independent small ints be compared via is? absolutely not. now, is it fair to blame is for telling us that the small ints were indeed cached? in my opinion, blaming is in this situation for doing exactly what it should do is unfair.

gleaming rover
verbal escarp
visual shadow
deft pagoda
#

is is great for sentinel checks

peak spoke
#

well, like with None, == would work the same in almost all cases

deft pagoda
#

i like the explicitness of is

visual shadow
#

more importantly, it's giving us a glimpse behind the curtain only in those situations where it shouldn't be used in the first place. to be more explicit, is will never say False when two references are genuinely the same thing.

visual shadow
#

oh?

verbal escarp
#

think of deserializing objects across processes

#

to compare those, uuid is a much better fit

visual shadow
#

i must admit i dont exactly know the context here, but perhaps i can take a guess. processes dont share memory right? the "loose definition" of two "same things" won't apply across processes. for a computer, they are absolutely two different objects, even if they're equal.

verbal escarp
#

and i know that you meant reference as a memory address thing, i mean in the abstract sense - whatever you mean by "this object" across time and space

visual shadow
#

when it's the same thing, it's a very specific same memory address. just like 5 instances of the same equal integer may not be the same thing, even though in terms of loose conversation they're the same.

verbal escarp
#

yeah, but i think you're assuming too much for the sake of optimisation here

visual shadow
#

hm. i dont know. I'd feel "betrayed" if is started telling me those two objects were the same while having different memory addresses for example.

#

as is, is is a very narrow, but very consistent thing.

#

perhaps to approach it from a different angle, if is tells me two things are same, and i mutate one of them, it's absolutely guaranteed the other references shows the change too

#

ultimately this is the one true contract that is is guaranteed to demonstrate.

verbal escarp
#

yes, okay

visual shadow
#

it's genuinely a very strict definition of "the same thing"

verbal escarp
#

let's have that contract

#

but let's assume a scenario with decentralised computing

#

a super computer maybe

#

or microservices

#

RPC

#

you know what i mean?

visual shadow
#

yeah. i just think it's outside the responsibility of is. is is consistent even in these scenarios.

verbal escarp
#

as you, i'd like to be able to modify an object and be sure it's consistent across the system

visual shadow
#

and as you mentioned, uuid and coordination would be better suited to manage expectations and coordinate things here. but that shouldnt' be is fault

verbal escarp
#

no, but maybe is could be a protocol instead of hard-coded?

visual shadow
#

hm. im ... conflicted.

#

on one hand, "something like that" is super useful.

#

my specific question is this, should is be the one doing it? because again, it would break it's current contract. it's extremely consistent right now

#

and this would throw a wrench in it's consistency for the sake of convenience, when perhaps something else entirely should be doing this.

verbal escarp
#

for the machine it's executed on, not for the whole system

#

dunno. just musing 🙂

visual shadow
#

aye, im also genuinely not sure where you give up consistency to make one function take on multiple responsibilities, or where you decide something should be separated even if it makes two very narrow things.

#

to me, there's a certain clear charm of things with single responsibilities. once you really get what something fundamentally does, and if it never deviates from it, you can always reason about it.

#

so even in scenarios where it doesnt do what you want, you can always reason why it wont do what you want

verbal escarp
#

to me the elegance lies in protocols and simplifying high-level code, but it's two sides of the same coin

visual shadow
#

does it make it less useful? quite possibly. is that a tradeoff that's worth it? i have absolutely no idea 😄

#

yeah, i see what you mean. i understand what you "want" is to do now, definitely interesting to think about.

verbal escarp
#

nodnod

quartz garnet
#

Hello guys is the yahoo api down?

#

for stock market?

visual shadow
#

afaik yahoo doesnt actually have an official API. also this feels like more suited for offtopic perhaps

past galleon
quartz garnet
#

ohk

#

yes i was using yfinance

past galleon
#

what are you trying to pull ?

quartz garnet
#

stock prices

#

closing and opening

past galleon
#

ye no issues with that here

quartz garnet
#

good

#

ok

#

was i down for some period before?

#

like in july

past galleon
#

eh , I remember people having issues with it awhile ago but not sure if it was July

quartz garnet
#

i am using this

#

syntax

#

is it fine?

#

@past galleon ?

past galleon
#

uh if you wanna use datareader with yfinance package documentation has it setup like this

granite steppe
#

Heya everyone

#

I am Sorrex, new to Python and i wish to learn more

past galleon
#

sidenote : if you plan on pulling in data for a lot of tickers it's better to grab it all at once instead of individually like this tickers = ['AAPL' , 'AMD' , 'GOOG' , 'SPY'] df = yf.download(tickers , interval = '1d' , period = '5y')

#

it'll take like an hour to pull snp500 if you are grabbing individually

elder blade
#

This is not the right place guys

#

Read the topic, you can move to a help channel

quartz garnet
#

@elder blade i used Help server still not got answer but here its instant

elder blade
#

That's merely a coincidence

quartz garnet
#

oh yes now yahoo is back

#

thats great

lost nexus
#

Is there a good/clean alternative to using hasattr? I'm making an implementation of getattribute and using hasattr will crash the stack.

paper echo
#

You can inspect __dict__ in many cases, but not all objects have a dict attribute

#

Could look at the output of __dir__

lost nexus
#

Can't do anything like if name in object.__getattribute__(self, "__dir__") either because dir is a builtin and in implies it would instead be an iterable

#

Yeah prob just not gonna use getattribute

#

Oh well

elder blade
native flame
#

same as dir, i checked

surreal sun
#

Not sure why you're using getattribute @lost nexus getattr works perfectly fine

elder blade
#

Well usually you do super().__getattribute__(...) assuming you don't have any parent class who override __getattribute__

elder blade
lost nexus
#

Nah yeah I switched to getattr a minute ago

elder blade
#

Ah cool

surreal sun
worldly ridge
elder blade
#

Move to a help channel

past galleon
# worldly ridge Do you know if there is any option to just get data from a specific industry (te...

(kinda getting off topic here for this channel) but are you asking for a way to filter for stocks within a given industry or just a benchmark for the industry? There is no built in way for yfinance to grab stocks by industry (you can still code it up if you want obv.) however if you're looking for a benchmark you can just pull data for a ETF or index that tracks what you're looking for RYT is one that tracks tech.

surreal sun
#

urgh, I'm trying to edit Cpython by adding a new operator to the AST as part of the book I'm reading (Cpython internals), I've added the token and added it to the types and all, but for some reason i can't find the function in the ast.c which handles comp operators

#

the book says ast_for_comp_op yet it's nowhere to be found on my end

#

@charred pilot You said you were reading CPython internals right? Did you get to the almost equal operator part yet for generating the AST?

surreal sun
#

Whoops, nevermind, solved it

#

turns out it got moved to compile.c

white nexus
#

aight

#

what is a stable way as of today to dynamically install modules to an environment?

#

this seems like its advanced enough to put here

grave jolt
#

@white nexus wdym by dynamically install modules?

white nexus
#

I want to download and install a package from pypi if the user tells my program to

grave jolt
#

I think amogorkon was doing something that 🙂

white nexus
#

yeah, justuse, hence why I said stable way as of today 😛

grave jolt
#

stable is probably don't

peak spoke
#

pip subprocess? Anyway that seems like it'd be more suited to some configuration file that's installed from before the program runs

grave jolt
#

I mean, there's no standard way

#

yeah, like poetry or something

#

well, poetry already exists

#

why do you want that? @white nexus

white nexus
white nexus
grave jolt
#

run pip like you would run any other program

white nexus
#

oh, with subprocess?

grave jolt
#

yes

white nexus
#

hmmm

#

moves to help channel

peak spoke
#

does poetry allow a custom dependency category?

white nexus
#

1.2 does

#

or... will

#

it exists on main/master and the 1.2 alpha 2 release

surreal sun
#

Maybe I'm misunderstanding but why can't you just do

import os

package_to_install = input("package: ")
os.system(f"pip install {package_to_install}")

i'm guessing that can be subject to malicious intent since pip will execute the __init__ file or whatever file at the beginning which could contain some dangerous stuff hence you don't want to leave it up to the user?

white nexus
#

nah, Idc what package the user installs. I think I have a solution in #help-burrito tho

#

random thing:

#

thing = var or raise Exception

#

That feels like that should be a thing

elder blade
white nexus
#

!d sys.executable

fallen slateBOT
#

sys.executable```
A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable, [`sys.executable`](https://docs.python.org/3.10/library/sys.html#sys.executable "sys.executable") will be an empty string or `None`.
elder blade
#

Also ngl I find it pretty shady of you to install libraries like that. Any way to get your library installed would also resolve dependencies

white nexus
#

wdym Any way?

elder blade
#

Unless this is like the final product, so the modmail you're working on?

white nexus
#

yea

#

and the command is plugin install so

elder blade
# white nexus wdym Any way?

Like- any way possible to get your module installed would bring with it your dependencies, unless you're shooting yourself in the foot and downloading the archive and putting it in the venv/site-packages.

elder blade
white nexus
#

discord bot

elder blade
#

Ah, so someone will download a plugin which you will load. Then you want to detect ImportErrors and install those dependencies until you don't get the ImportErrors anymore?

white nexus
#

nah, I'm supporting a way for plugins to declare dependencies 😛

#

I just need to install them in an asyncio subprocess wrapping pip

#

but I want to do it atomically without the risk of messing up installed packages

lusty scroll
#

justuse should do it but install it from source (the released/publisged version is ancient)

white nexus
#

I know justuse can do it but I want it a little more stable than that

unkempt rock
#

i have a question, Im coding a game and I was wondering how to use blender to import the libraries of the def function to make a virtual sun and have burning graphics and make it burn irl

lusty scroll
white nexus
#

yeah lol

#

it seems if I install them with pip and then instantly follow with poetry install --no-dev I can ensure that no dependencies that the bot relies on are affected

lusty scroll
white nexus
#

tldr easy to use extendable discord bot

lusty scroll
#

are you doing venvs or no

white nexus
#

also self hosted PepeHands

grave jolt
#

If you want to install plugins, just tell people to use normal dependency management (requirements.txt/pipenv/poetry/etc.). They're not special.

white nexus
white nexus
elder blade
lusty scroll
white nexus
lusty scroll
#

it might not be a total disaster if all the dependencies can be imported directly with zipinporter

#

by far most aren't

lusty scroll
#
grave jolt
surreal sun
#

So essentially if I'm understanding correctly, frames are essentially symbol tables?

And the assembler for Python which is the next step after the compiler will use Depth-First Search which goes to the lowest depth of the frame, and work backwards, then a code object is created?

that code object is essentially the frame?

#

Python frame objects that is

pliant tusk
#

when a code object is executed, a new frame object is allocated to store its runtime information. if another code object is executed inside that frame, a new frame is nested inside and so on

surreal sun
#

ty

unkempt rock
#

Are there any instances where Python 3 and Python 2 will have different syntax highlighting? pithink

#

indeed lemon_glass

#

though guess the issue in an editor anyway is python 2 files don't have a different extension

worldly ridge
#

In terms of performance in Python, what's a better approach (EDIT: it's a JSON file):

  1. Writing to a file every time if there is some data (there might be 100+ actions like this)
  2. Storing the data in a data structure and then writing to a file once?
boreal umbra
#

However having fewer disk IO operations is often the faster option.

verbal escarp
#

"often"?

#

when is writing to disk faster than operating on an in-memory DS?

boreal umbra
charred pilot
#

but those aren't equivalent..

worldly ridge
boreal umbra
charred pilot
#

you're either writing all the results as soon as you get them, or you cache them and write the results later. you're still writing the results to the file, one way or another

boreal umbra
worldly ridge
# boreal umbra why have the JSON step at all?

You are running the tool with arguments that will be taken from the user (from the front-end basically), for ex:

python3 some_program.py -k (some_user_input)

Then it returns data to the terminal:

foo
bar
fubar
etc.

So I need a way to be able to use the returned data and that’s why I saved it to a JSON file.

#

something like -o file_name_to_save_data.json

unkempt rock
#

How can I create a .egg from a library?

worldly ridge
soft lion
#

hi room

elder blade
surreal sun
#

is locals() just the current frame the assembler is on?

#

as a dictionary?

jovial fulcrum
#

Hey guys I'm new in this server I have a question about making an Instagram bot with python I really need help plz DM me if it's possible

astral gazelle
#

This isnt on topic for this channel and possibly against the terms of service for instagram

paper echo
surreal sun
#

Ah ok, thanks

#

Yeah I'm pretty sure it's the equivalent of the stack frame

paper echo
surreal sun
#

I was going to mess around with changing and to && and or to || but I have to make a lot of changes for that lmao

#

currently logical operators short circuit once found and the only time it'll check for another one is something like &= or |=

paper echo
#

I don't think you can do that by modifying local bindings

surreal sun
#

I'm working from cpython itself (a fork o fit)

paper echo
#

Ah

surreal sun
#

It's a lot of changes though

#

I did follow a tutorial from the RealPYthon book CPython internals and I got to add an almost equal opeartor

peak spoke
#

the stack frame holds a reference to the locals, but it does a bit more than that

surreal sun
#

something with assemblers and depth-first search

surreal sun
#

Agh,
I'm lost in CPython lmao

#

So I'm trying to redefine and and or to && and || but I can't find where to redefine keyword literals outside of python.gram

And c_generator.py raises an assertion error that the literal isn't known

#

Ah, nevermind, I can't do that

#

Because of how keywords work

naive saddle
#

Alec might have some knowledge on this works as they implemented Yeeython

#

Ok clearly it's not spelled that way (sorry Alec) since searching for that returns nothing ...

#

Ah, Yeethon.

surreal sun
naive saddle
#

I have literally no idea, that's why I linked the repo, the first three commits seem useful?

surreal sun
#

Ah

#

Ohh, it just adds yeet as a separate keyword along with del lol

#

How would I get the timezone of someone who is running a function?

I'm messing around with datetime on my CPython local repo and when someone calls datetime.now I want to get the timezone of where their function is running from if they don't specify

#

antigravity looks like it had something interesting

prime estuary
#

That's an easter egg implemen of an algorithm in that comic for getting a random location.

fast swift
#

Anyone here familiar with using beautiful soup library?

paper echo
fallen slateBOT
#

Lib/types.py line 15

MappingProxyType = type(type.__dict__)```
prime estuary
unkempt rock
#

What are some example of algorithm with step function complexity ?

#

For example an array algorithm that runs in k^2 steps on arrays of length n=4k.

#

Does that mean algorithm doesn't run on number that are not multiple of 4?

elder blade
dusky gull
#

guys whats more complex to learn pythong or django?

#

total noob here

elder blade
verbal escarp
terse crown
#

এক চর দিবো

#

না

gloomy rain
verbal escarp
#

hi

gusty marsh
#

anddd add it to Lib/keyword.py

grim fractal
#

Hi,
I want to find distance between different object, from an arial view (captured by drone). Can anyone tell me where I can start my research from?

junior osprey
#

Anyone know how to build a Python lexser from scratch. I need this to get all the functions available in any given py file.

sacred yew
#

why not use existing libraries

fleet ice
#

How do you "code"?
Yes I am in the correct channel

#

Writing code != Code

verbal escarp
#

that'd be slightly less work

junior osprey
surreal sun
#

!e

from forbiddenfruit import curse 
from random import randrange

builtin_method = type(print)

def give_me_a_number(start, end, step=1):
    return randrange(start, end, step)

curse(builtin_method, "give_me_a_number", give_me_a_number)

print(give_me_a_number(1, 3))
print(type(give_me_a_number))
fallen slateBOT
#

@surreal sun :white_check_mark: Your eval job has completed with return code 0.

001 | 2
002 | <class 'function'>
surreal sun
#

hm, does it not become a builtin after the curse?

#

oh well

gleaming rover
fleet ice
#

How do I code?

#

People around me keep saying I should learn how to code even tho they know i can write code

gleaming rover
fleet ice
#

Not about software design they just keep saying that to me

gleaming rover
modern gale
verbal escarp
#

pycon talks are pretty good

#

on youtube

junior osprey
paper echo
unkempt rock
#

my python AI is not taking my voice input
what shall i do

white nexus
#

where is the list of dict dunder methods

#

You know you're doing something cursed when you subclass dict and patch with this:
getattr(key, "id", key)

paper echo
#

!d collections.abc

fallen slateBOT
#

New in version 3.3: Formerly, this module was part of the collections module.

Source code: Lib/_collections_abc.py

This module provides abstract base classes that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whether it is a mapping.

An issubclass() or isinstance() test for an interface works in one of three ways.

  1. A newly written class can inherit directly from one of the abstract base classes. The class must supply the required abstract methods. The remaining mixin methods come from inheritance and can be overridden if desired. Other methods may be added as needed:
hazy plume
#

i was redirected here from my help channel #help-potato , could anyone help me with that question? i can paste it here i just dont want to flood. its pretty weird

paper echo
#

@hazy plume are you asking why print.__class__ = type is allowed if it's not actually allowed?

#

!e ```python
print.class = type

fallen slateBOT
#

@paper echo :x: Your eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "<string>", line 1, in <module>
003 | TypeError: __class__ assignment only supported for heap types or ModuleType subclasses
hazy plume
#

more the entire nature of the error

paper echo
#

i didn't know you could overwrite __class__ at all!

hazy plume
#

thats what im saying!

paper echo
#

i guess it was easier to just check at runtime than to allow it as a special case for those types

#

i have no idea what a "heap type" is

hazy plume
#

thats what im saying!!

paper echo
#

or what setting __class__ on a ModuleType would do

#

https://docs.python.org/3/reference/datamodel.html#customizing-module-attribute-access

For a more fine grained customization of the module behavior (setting attributes, properties, etc.), one can set the __class__ attribute of a module object to a subclass of types.ModuleType.

import sys
from types import ModuleType

class VerboseModule(ModuleType):
    def __repr__(self):
        return f'Verbose {self.__name__}'

    def __setattr__(self, attr, value):
        print(f'Setting {attr}...')
        super().__setattr__(attr, value)

sys.modules[__name__].__class__ = VerboseModule

interesting

#

hey @verbal escarp do you want to abuse this feature in Justuse? ☝️

#

when you access an attribute on the module for the first time, print an obnoxious message asking for donations 😆

#

you'd probably have to go read the C source to figure out what a "heap type" is, unless someone here knows

deft pagoda
#

i was abusing __class__ once as a proxy to changing the mro --- since you can't actually re-write the mro

#

i forgot what awful sorcery i was attempting though

surreal sun
#

I have a question:

How good would you all consider this explanation of print("Hello, world!")'s bytecode explanation to be?

`print("Hello World")`
What does this do you may ask? It infact, talks to the Python console to output something.

Let's take a look behind the bytecode of this. The bytecode is the instructions that a Python program uses that then is interpreted.
We can see the first bytecode `LOAD_NAME`, this will get the pointer of `print`, which is stored in the `frame`'s `f_builtins` portion.
This will push the function object of `print` to the top of the stack (the value stack is crucial to the execution loop)

Next, we use `LOAD_CONST`, this pushes the arguments to the already pushed `print` in the value stack, in this case, a literal of `Hello, world!`

Next, we have the instruction of `CALL_FUNCTION`, whose argument, *argc*, contains the number of positional arguments that the function has,.
This will pop the value stack however many times the positional arguments exist, in this case, it pops off one value at the top, `"Hello, world!"`, and is now the argument for said function

Next, it will pop the function itself off the stack, which is `print`

Lastly, `print` itself is executed and the `RETURN_VALUE` instruction is reached.
`RETURN_VALUE` is used for the return value of a function, as the name suggests, however `print` has no return value therefore it is `None`
#

I wrote this down since I'm trying to understand python bytecode, and started out with a simple print("Hello, world!") in byteocode

verbal escarp
#

but actually, we do something like that with the ProxyModule, but i think that's more elegant :p

#

because a proxy allows transparent handling of other trickery

#

waitasecond

#

you don't need to replace functions with decorated versions, you could decorate them on the fly on attribute access

#

hmmm.. curious

#

then we could add or remove decorators on the fly and never actually replace the function but only trigger then on access

unkempt rock
#

I was looking into vscodes syntax highlighting and noticed some of Python's built in functions show up as the turquoise type color instead of yellow function color:

#

I wonder if this is because they actually are separate types or just that they kinda tend to return unusual types pithink

peak spoke
#

They are their own types, zip/map have to produce iterators etc.

unkempt rock
#

!e wait, I guess they are types, makes sense but I've never really checked py print(type(zip('a', 'b')))

fallen slateBOT
#

@unkempt rock :white_check_mark: Your eval job has completed with return code 0.

<class 'zip'>
unkempt rock
#

I knew about the iterator types they give but never realized zip itself was a type

peak spoke
#

I think all builtin iterators will have their own type as it needs to manage the state

unkempt rock
pliant tusk
#

!e ```py
class a:
def set_b(self, val):
self.b = val

class b:
def get_b(self):
return self.b

A = a()
A.set_b(10)
A.class = b
print(A.get_b())```

fallen slateBOT
#

@pliant tusk :white_check_mark: Your eval job has completed with return code 0.

10
elder blade
cerulean anchor
#

Ask for permission if you would like, I am trying to create a spreadsheet that categorizes all the dunders along with showing the fallback dunders wherever there are fallbacks

surreal sun
#

Hm, I'm not on my google acct rn but:

__lte__
__gte__ for comparison

#

also maybe add a section for async?
__aiter__
__anext__
__aenter__
__aexit__

raven ridge
#

so, basically, all regular Python classes, and a new kind of extension class, but not the old school kind of built-in type used by the interpreter and most extension modules.

grave jolt
#

If I'm making a tutorial for a library that uses functions, should I use type annotations?

raven ridge
#

how advanced is your audience?

#

if they're beginners, annotations are likely to confuse them.
If they're experts, annotations might be enlightening.

grave jolt
#

I wish there was a way to show annotations on hover, like in an IDE but better

raven ridge
#

I mean, if you're writing it in HTML, CSS can totally do that.

#

but, there's no such thing as a "general audience", in any event. You'll just wind up writing something that's wrong for both experts and beginners.

grave jolt
raven ridge
#

"hard" 🙂

grave jolt
#

Yeah, I did that before... it was some ugly JS and painful even without an existing layer

grave jolt
#

so I guess I'll go with the abscence of type hints

#

Maybe I'll include a separate tutorial for intermediate people on how things are typed in the library

raven ridge
#

that sounds like a better strategy to me.

grave jolt
#

on the other hand, the beginner who doesn't know about type hints will probably have a heart attack if they press "jump to definition" on any of my functions lemon_pleased

raven ridge
#

I don't think that's particularly unusual, honestly. Libraries are written using tons of idioms that beginners don't know.

grave jolt
#

the body of this function is 4 short lines (without the docstring)

    acc = initial
    for item in items:
        acc = then(step(acc, item))
    return acc
raven ridge
#

Right - beginners aren't going to be able to wrap their heads around that.

#

Frankly, beginner beginners will probably be lost at "Callable"

grave jolt
raven ridge
#

I mean, conceptually, the idea of functions as first class objects is a jump for people. It's actually a hurdle to get past.

grave jolt
#

yeah

#

I'm actually having a hard time imagining how I'm going to write beginner-friendly tutorials for my thing

#

is it okay if I ping you later and show what I came up with?

raven ridge
#

sure.

grave jolt
#

👍 thx

grave jolt
#

it's non-trivial

raven ridge
#

It's fine if you can't figure out how to write beginner friendly docs for it - maybe the lib just isn't beginner friendly. That's definitely the case for some libraries - you might be able to show some examples of how to use the library that they can copy, paste, and modify, but hoping for them to understand the library is more than you're likely to get for some libraries.

grave jolt
#

do you have any articles/talks on writing tutorials?

raven ridge
#

I don't, no - beyond general writing advice like "know your audience"

paper echo
#

@grave jolt use very simple language, and keep the examples as simple as possible. look at any GNU manual for an example of what not to do in this regard

grave jolt
#

I liked the GNU Smalltalk tutorial!

visual shadow
#

If it's like a webpage perhaps you could put a toggle on code snippets

#

Show annotations, hide annotations

grave jolt
#

yeah, I thought about that

#

with mkdocs material I can make two tabs or somethng

#

but then I'll need to synchronize them, and it's a bit confusing

visual shadow
#

I don't really know what mkdocs is or what it does, but for an example cython tutorial page uses toggles. If mkdocs supports something that's as low friction as that then perhaps it's worth considering.

#

If it adds friction to the flow of the tutorial as a whole in mkdocs then maybe it's not worth it

grave jolt
#

eehhh

#

I think I'll just go with "without typehints"

raven ridge
#

The Cython docs are generated using Sphinx, as are the CPython docs. Might not be a bad strategy.

granite heath
#

How long have you got left of your test?

unkempt rock
#

30 minutes

granite heath
#

!mute 776093455561523210 45M We don't help with on-going tests or exams here. Feel free to come back when your test is over.

fallen slateBOT
#

:incoming_envelope: :ok_hand: applied mute to @unkempt rock until <t:1631613968:f> (44 minutes and 59 seconds).

proven dune
#

Ethics Q ~ Working on a new branch of a 2 year old abandoned repo, no license. Is it okay for me to give it an MIT license?

flat gazelle
#

if it is your repo, yes, you reserve all rights by default, including changing the license.

#

also, this is more of an off topic question

paper echo
proven dune
paper echo
#

indeed, that's what you should do

raven ridge
# proven dune Thanks, I suppose it's at least worth asking the OP

If you are not the OP, it is most definitely not legal for you to relicense their code. I don't believe there's any jurisdiction where you can say "I found this code, and no one gave me permission to use or distribute it, but I'm telling everyone else in the world that they can use it and distribute it" and have that stand up in court.

gloomy rain
#

From what I know, they retain the copyright.

#

Licenses are meant to grant permissions, not restrict them. The default is that the author retains all rights.

raven ridge
# gloomy rain From what I know, they retain the copyright.

Right. The original author both retains the copyright and reserves all rights to the code - no rights are licensed to anyone else by default. Which means Scarcer, who is not the original author of the code, has no right to use it, or to modify it, or to license it to others under different terms.

paper echo
#

the flipside is that if someone really did upload something to github and then abandon it, they probably don't care and won't sue you

raven ridge
#

Probably not, no.

paper echo
#

also iirc there's some agreement in the github ToS about letting other people use your code in some capacity

raven ridge
#

Yeah, D.5 "License Grant to Other Users".

paper echo
#

you agree to allow others to view and "fork" your repositories (this means that others may make their own copies of Content from your repositories in repositories they control).

raven ridge
#

It makes you grant others the right to read the code and make (unmodified) copies of it.

paper echo
#

If you set your pages and repositories to be viewed publicly, you grant each User of GitHub a nonexclusive, worldwide license to use, display, and perform Your Content through the GitHub Service and to reproduce Your Content solely on GitHub as permitted through GitHub's functionality (for example, through forking).

#

that seems almost like an oversight

#

you can fork it but can't modify it?

raven ridge
#

It's definitely not an oversight.

paper echo
#

it makes sense, but it seems like you could get in trouble with that easily

#

fork is fine, but push 1 commit and all of a sudden you've made a derivative work and have violated the original author's copyrights

raven ridge
#

Yep, that's right.

paper echo
#

i'd almost want a warning upon forking, "this repo has no license, if you make changes you might be in violation of laws"

#

then again it might also be useful to let you upload a user-account-wide default license that gets applied to each repo and can't be removed afterwards

raven ridge
#

Note also that nothing in the GitHub ToS allows others to use the code. They can read it, but can't link against it or execute it.

paper echo
#

it says "perform", what does that mean in the context of source code?

#

in the GPL it seems to imply at least compiling the software

#

solely on GitHub as permitted through GitHub's functionality
it sounds like you can do things like run github actions that entail compiling, linking against, and/or running artifacts derived from the source code

#

but you can't do it on your machine

#

it depends on what "perform" means

raven ridge
#

I suspect it refers to things like bringing up your code on a projector in front of an audience at a conference, or something like that - though IANAL and it's likely a term of art that I don't know.

paper echo
#

i do know AL in the copyright space 😛 i'll ask them

#

they've done software licensing work before

#

i do know that some of this stuff has not been subject to serious "testing" in the courts

#

for all i know, it's ambiguous and one day someone will sue someone and then a US federal court will decide

raven ridge
#

Yep. My layman's reading of the GitHub ToS is that they only grant others read-only access to the code, and then only through the GitHub UI. If it grants more than that it's not clear to me, and I'd be reluctant to put my career or company on the line over it.

candid pelican
#

hey people

#

i saw a video some days ago

#

and the dude said to use an enum instead strings to store fixed data

from enum import Enum, auto
from dataclasses import dataclass

class PostTypes(Enum):
    VIDEO = auto()
    IMAGE = auto()

@dataclass
class Post:
    file: str
    post_time: str
    post_type: PostTypes
    caption: str

post = Post('image.png', '12:30', PostTypes.IMAGE, 'Hello, world.')

is it nice??

paper echo
#

yeah, that's a good strategy

#

you can also do this, so the members are actually strings

class PostTypes(str, Enum):
    VIDEO = 'video'
    IMAGE = 'image'
#

!e ```python
from enum import Enum

class PostTypes(str, Enum):
VIDEO = 'video'
IMAGE = 'image'

print( PostTypes.VIDEO == 'video' )

fallen slateBOT
#

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

True
candid pelican
#

ic

#

i made this class:

class InstagramHandler:
    """handle client's instagram account."""
    def __init__(self, client: Client) -> None:
        self.bot = Bot()
        self.account = client.account
        self.password = client.password
        self.bot.login(username=self.account, password=self.password)

    def post_feed(self, post: Post) -> None:
        """post image on instagram."""
        try:
            if post.post_type is PostTypes.IMAGE:
                self.bot.upload_photo(post.file, caption=post.caption)

            elif post.post_type is PostTypes.VIDEO:
                self.bot.upload_video(post.file, caption=post.caption) 

            sleep(60)
        
        except FileNotFoundError:
            print('file not found.')

but the method post_feed do not looks good bc there are these if statements, i would like pass a function as an argument...

#

or the class Post shoud inherit some class with the post_feed method

sand goblet
#

Where do you post a suggestion for a minor change in a standard library .py file? I want to suggest for capwords in string.py to use the map function instead of a generator expression

surreal sun
#

!e

from enum import Enum


class Test(Enum):
  VALUE = "value"

print(type(Test.VALUE))
fallen slateBOT
#

@surreal sun :white_check_mark: Your eval job has completed with return code 0.

<enum 'Test'>
sand goblet
#

Thank you

molten kayak
livid dove
#

instagram has two apis: a read only one for normal accounts and a separate one for business and creator accounts where you can post, etc.

#

but yeah this might break tos, but it doesnt have to

sand goblet
boreal umbra
# sand goblet https://github.com/python/cpython/pull/28342 Does this look ok? Also I signed th...

I find the time measuring code difficult to read. Did you consider doing it in ipython?

In [12]: def comp_capwords(word, sep=None):
    ...:     return (sep or ' ').join(x.capitalize() for x in word.split(sep))
    ...: 

In [13]: def map_capwords(word, sep=None):
    ...:     return (sep or ' ').join(map(str.capitalize, word.split(sep)))
    ...: 

In [14]: %timeit comp_capwords('a ' * 10_000)
1.24 ms ± 11.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [15]: %timeit map_capwords('a ' * 10_000)
947 µs ± 13.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [16]: %timeit comp_capwords('a ' * 10_000_000)
1.67 s ± 17.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [17]: %timeit map_capwords('a ' * 10_000_000)
1.31 s ± 26 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
sand goblet
#

I’ll try to learn how to use it if you think it’s important

#

It always does at least 1 timing, right?

#

Because the 2nd to last test took 5 minutes 50 seconds

#

Also does it have a way to do setup? Because some of the strings are large

#

That’s still ipython?

#

Oh, right

#

Duh, or it wouldn’t know what the function was. Sorry

quasi hound
#

what does #3 mean

#

i saw that thing in a video a while ago but forgot what it does

languid yoke
#

If you create two iterators of a collection, they can both function independently. That's why it makes two different objects. If you make two iterators from an iterator, they both have to advance the same internal iterator. That's why they're the same object as each other.

paper echo
#

that's a good trick, i didn't know it

sand goblet
boreal umbra
# languid yoke If you create two iterators of a collection, they can both function independentl...

If you make two iterators from an iterator, they both have to advance the same internal iterator
rather, an iterator returns self for iter(...), per the iterator protocol. There is no secret internal iterator--you're just getting another reference to the same object.
they both have to advance the same internal iterator. That's why they're the same object as each other.
I think you have the cause and effect backwards here, but your larger point is correct.

languid yoke
surreal sun
#

Is the difference between LOAD_FAST and LOAD_NAME is that LOAD_FAST is used for locals?

#

But when does python know to use LOAD_FAST vs LOAD_NAME

peak spoke
#

I believe load fast is used when it knows it can grab the var from the locals array while load name does it conventionally with grabbing the relevant dicts and getting the items out of them (e.g. used on the global scope or with some dynamic code)

surreal sun
#

Ohh like if you do

def func():
    load = None
    load

it would call LOAD_FAST for load

grave jolt
#

or LOAD_DEREF if it's in the closure

royal stone
#

Hello Everyone i am a beginner in python and i want to learn it well so im looking for a modern python course can you help me ?

unkempt rock
#

!resources

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.

unkempt rock
#

i already asked, but... does exist a program choose alone the good python --version for interpreter in my IDE in fonction of module using inside the *.py