#internals-and-peps

1 messages · Page 103 of 1

raven ridge
#

it could be compiled to machine code, but doing so wouldn't necessarily be better. Because it would still need to support things like ```py
dear = 5
dead = 10
print(globals()["dea" + import("random").choice("rd")])

#

The language's user-facing behavior is that names are looked up dynamically at runtime. In an environment where names are looked up at runtime, compilation to machine code just doesn't add very much - that machine code would need to do the same slow searches to look up names and attributes that the CPython VM does.

#

Consider also code like ```py
x = 10
exec(input())
print(x)

After running the first line, `x` is an integer with value 10. After running the second line, literally anything at all could happen. It's possible the `print` will print 10, or that it won't be reached at all because the control flow changed, or that `x` no longer exists, or is no longer an integer
#

or consider code like ```py
x = 10
import main
main.x = 20
print(x)

If that's imported as a module, it will print `10`. If it's run as a script, it will print `20`. It executes the same code either way, but the code has different effects.
unkempt rock
#

how to make a conscious AI

shrewd talon
#

Hi guys, looking for the best and easy UI maker for python.
I will create exe from my python and I want to make UI to my app. What is the best drag and drop UI builder, or the easiest way to build a UI?

hexed island
hexed island
midnight hornet
#

can some1 tell me what number i should put in number of processors and no. of cores per processor in vm setting?
vm is a bit laggy. both vm and the os inside vm are installed in ssd as well
I have 3700x 16 gb ram

visual shadow
#

This is definitely the wrong room for this

unkempt rock
#

why does collections.Counter use self.__class__ to make the repr but not self.__class__ for methods like +?

class Foo(Counter):
    pass

repr(Foo()) # is "Foo()"
Foo() + Foo() # is Counter()``` in fact most classes not only in the standard library but also ones from popular libraries do this thing
midnight hornet
#

where should i go @visual shadow ?

visual shadow
#

Hm, I'd say try offtopic

#

!ot

midnight hornet
#

ty

flat gazelle
sand goblet
#

How does locals() get the names of all the variables if the local values are stored in an array and not a hash table? Does it store the names associated with the values in the array just for the sake of being able to create/update the locals() dictionary?

#

Also, why would someone ever need to use locals()?

snow kettle
#

it might be used in debugging tools

pliant tusk
#

@sand goblet the names are stored in the currentframe -> f_code -> co_varnames

sand goblet
#

And at the same memory offset as the array with the values?

pliant tusk
#

i believe so

sand goblet
#

And it does that just so that locals() will work? Or it’s used for other reasons too?

pliant tusk
#

co_varnames has other uses in other places

sand goblet
#

Alright

#

Where else is it used?

pliant tusk
#

they are used for error messages

#

and for dis and inspect

sand goblet
#

Alright

azure fiber
#

What is the correct way to upgrade all packages without having conflicting issues?

odd summit
#

hi how can i install pyttsx3

#

cause i cant use pip install pyttsx3

flat gazelle
azure fiber
#

What is interfaces, filter_fields and relay-node in graphene-permissions?

odd summit
#

There are several ways you may want to limit access to data when working with Graphene and Django: limiting which fields are accessible via GraphQL and limiting which objects a user can access.

#

Let’s use a simple example model.

#

from django.db import models

class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
published = models.BooleanField(default=False)
owner = models.ForeignKey('auth.User')

#

use it on vs code

unkempt rock
#

I think u got your answer already , ☺️

azure fiber
#

I also want to define permission based access.

lost sequoia
#

Hi guys,
How would go about writing some code that would instantiate different subclass depending on an init argument ?
I am thinking something like:

class Person:
   def __init__(gender):
      if gender == 'm':
         return Male()
      else gender == 'f':
         return Female()

class Male(Person):
   pass

class Female(Person):
   pass

Is that pythonist ?

graceful wedge
eager trail
#

Had a weird error today on our production server related to a code change someone made, so wondering if anyone can help me with this

Did python gain the ability to set OS.ENVIRON variables as types other than string sometime in 3.8.0 onwards

#

Someone set environment variables as None and then was trying to get that back with a check, and on some of our machines the var would set and on others it wouldn't, creating a logic error

limpid forum
eager trail
#

Hope I'm making sense

flat gazelle
#

os.environ enforces enforces its type, but .get() returns None if the mapping doesn't have the key

eager trail
#

I see, I wonder if that behavior changed in a recent version, since some of us are using 3.7.x and earlier and the rest are on newer versions . Apparently its been sitting around in the lambda layer forcing people to do workarounds for months until I noticed it today and got someone to revert it

flat gazelle
#

it did the same in python 2

eager trail
#

How odd. In any case it seems like bad practise so changing the logic was the simplest solution

raven surge
#
In [5]: class D(dict):
   ...:     def __del__(self, *args, **kwargs):
   ...:         super().__del__(*args, **kwargs)
   ...:         print("was called...")
   ...:         print("args", args)
   ...:         print("kwargs", kwargs)
   ...: 

In [6]: d = D()

In [7]: d["h"] = 1

In [8]: del d["h"]

In [9]: d
Out[9]: {}

How does del work on dict? Seems like del is not invoked.

radiant fulcrum
#

sadly __del__ isnt linked with del as a keyword, __del__ is invoked when any object is about to be destroyed hence why any object can implement __del__ but is only done rarely as it's not guaranteed to be called at a given time. iirc

flat gazelle
#

it invokes __delitem__

undone hare
#

__del__ isn't guaranteed to be called but it often is. But in situations such a segfault it won't really be possible.

flat gazelle
#

it gets called as long as python exits properly, that is without C level errors/os._exit

#

and it always gets called when the refcount reaches 0 in cpython

peak spoke
flat gazelle
#

I think that was changed in some pep.

raven surge
#
def abstract_property(
    name: str, type, doc: str = "", mutable: bool = False
) -> property:
    """Abstract property."""

    @property
    @abstractmethod
    def fn(self) -> type:
        """Get."""
        ...

    if mutable:
        @fn.setter
        @abstractmethod
        def fn(self, value: type) -> None:
            """Set"""
            ...

    fn.__name__ = name
    fn.__doc__ = doc
    return fn

# usage
class Iterfacex(ABC):
   field_x = abstract_property(name="field_x", type=str, doc="mutable propery c", mutable=True)
   field_y = abstract_property(name="field_y", type=int, doc="immutable propery y", mutable=False)

Doesn't work 😓 , is such a thing possible? Why, to reduce boilerplate code for setting up abstract properties.

raven ridge
flat gazelle
#

huh, I wonder what change I am confusing it with then

raven ridge
#

Python 2 wouldn't call __del__ methods for objects collected by the cycle collecting GC, but Python 3 will. Perhaps that?

grave jolt
sand goblet
#

Why is there nothing like this with .split, even though it does something similar for whitespace if you don’t give it any arguments?
"abc1321323xyz".split("123", any_combination=True) # ["abc", "xyz"]

#

It forces you to use re.split

#

"abc \t\n \n\t xyz".split()
There’s nothing like that for different collections of characters

raven ridge
#

Why would you expect it to give ["abc", "xyz"] instead of ["abc", "", "", "", "", "", "", "xyz"] if it existed?

sand goblet
#

Because splitting by whitespace gives ["abc", "xyz"]

raven ridge
#

!e Only splitting with no arguments does.

print("a   b".split(" "))
fallen slateBOT
#

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

['a', '', '', 'b']
sand goblet
#

Yeah

raven ridge
#

So why should your proposed addition behave more like splitting without specifying a delimiter than like splitting with a given delimiter? Is that how everyone would expect it to behave?

sand goblet
#

I think so

#

Since it would be like what it does by default with whitespace, just with a different collection of characters

limpid forum
#

it's marked in docs as special behaviour

raven ridge
#

I'm betting the only reason that split() with no args exists is that it predates the re module, and it's a relatively common case that people frequently need. Splitting on runs of arbitrary characters is a less common need, and it's easy enough to do using re, and it can be done faster with re because the pattern can be compiled in advance and reused

#

And doing it through re lets the user choose whether they want to collapse adjacent delimiters or not.

limpid forum
#

I'd assume so as well. Python doesn't often have special cases like that. it's not just a default argument thing, it's literally a different approach:
"If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace."

sand goblet
#

So it would be slower if they let you do it for any collection of characters?

raven ridge
#

Yes, definitely. There's a highly optimized version of the split on whitespace implementation.

peak spoke
charred wagon
#

Which is more idiomatic for storing a metaclass argument?

class M(type):
    def __new__(cls, name, bases, dict_, custom):
        instance = super().__new__(cls, name, bases, dict_)
        instance.__custom = custom
        return instance

    def __init__(cls, name, bases, dict_, custom):
        pass

or ```py
class M(type):
def new(cls, name, bases, dict_, custom):
return super().new(cls, name, bases, dict_)

def __init__(cls, name, bases, dict_, custom):
    cls.__custom = custom
prime estuary
#

Well, if this is all you need the metaclass for, use __init_subclass__() as of 3.6.

#

I've usually seen it done in just __new__ though, the usual distinction isn't as relevant for metaclasses since you'd not call them yourself.

charred wagon
#

There's more to the metaclass, though as far as __new__ goes, it's only needed to get the custom kwarg.

#

What's even the semantic difference between doing something in __new__ and doing something in __init__ for a metaclass?

deft pagoda
#

The first argument of __init__ is different than the first argument of __new__. To me, this demarcates the main difference -- use init when you need an instance of the metaclass -- registries are probably the most common use -- though one can use __init_subclass__ for this now.

#
In [13]: class MyMeta(type):
    ...:     def __new__(meta, name, bases, methods, **kwargs):
    ...:         print(meta)
    ...:         return super().__new__(meta, name, bases, methods, **kwargs)
    ...:
    ...:     def __init__(cls, name, bases, methods, **kwargs):
    ...:         print(cls)
    ...:         super().__init__(cls)
    ...:

In [14]: class MyClass(metaclass=MyMeta):
    ...:     ...
    ...:
<class '__main__.MyMeta'>
<class '__main__.MyClass'>
#

I typically use __new__ when I want to modify the dictionary before I create the class.

charred wagon
#

Why would you need to modify the dict before it's created? Can't you achieve the same thing in __init__?

deft pagoda
#

modifying methods above in __init__ wouldn't actually modify the class at this point, you'd have to reach into cls.dict

In [7]: class MyMeta(type):
   ...:     def __new__(meta, name, bases, methods, **kwargs):
   ...:         print(methods)
   ...:         return super().__new__(meta, name, bases, methods, **kwargs)
   ...:
   ...:     def __init__(cls, name, bases, methods, **kwargs):
   ...:         del methods['__module__']
   ...:         super().__init__(cls)
   ...:

In [8]: class MyClass(metaclass=MyMeta):
   ...:     ...
   ...:
{'__module__': '__main__', '__qualname__': 'MyClass'}

In [9]: MyClass.__module__
Out[9]: '__main__'
charred wagon
#

But you can do cls.__module__ = whatever in __init__ or delete it from cls.__dict__

deft pagoda
#

But note, that methods isn't always a dict

#
In [12]: from collections import ChainMap
    ...:
    ...: class MyMeta(type):
    ...:     def __prepare__(*args):
    ...:         return ChainMap({'hidden_method': None}, {})
    ...:
    ...:     def __new__(meta, name, bases, methods, **kwargs):
    ...:         print(type(methods))
    ...:         return super().__new__(meta, name, bases, methods, **kwargs)
    ...:

In [13]: class MyClass(metaclass=MyMeta):
    ...:     ...
    ...:
<class 'collections.ChainMap'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-4db0ebe2f46b> in <module>
----> 1 class MyClass(metaclass=MyMeta):
      2     ...
      3

<ipython-input-12-56eb236938cb> in __new__(meta, name, bases, methods, **kwargs)
      7     def __new__(meta, name, bases, methods, **kwargs):
      8         print(type(methods))
----> 9         return super().__new__(meta, name, bases, methods, **kwargs)
     10

TypeError: type.__new__() argument 3 must be dict, not ChainMap
#

you can pass any mapping with __prepare__

#

it needs to be converted to a dict though

#

and needs to be done before __init__

charred wagon
#

It seems that as far as modifying the dict goes, it's just a semantic difference rather than a technical one since the same thing can be accomplished in either new or init 🤔

deft pagoda
#

That's not really true

#
In [21]: class MyProp:
    ...:     def __set_name__(*args):
    ...:         print("set name called")
    ...:

In [22]: class MyMeta(type):
    ...:     def __new__(meta, name, bases, methods, **kwargs):
    ...:         methods['my_prop'] = MyProp()
    ...:         return super().__new__(meta, name, bases, methods, **kwargs)
    ...:

In [23]: class MyClass(metaclass=MyMeta):
    ...:     ...
    ...:
set name called

In [24]: class MyMeta(type):
    ...:     def __new__(meta, name, bases, methods, **kwargs):
    ...:         return super().__new__(meta, name, bases, methods, **kwargs)
    ...:     def __init__(cls, *args):
    ...:         cls.my_prop = MyProp()
    ...:         super().__init__(cls)
    ...:

In [25]: class MyClass(metaclass=MyMeta):
    ...:     ...
    ...:
#

if you add properties to the methods dictionary, things like __set_name__ will be called for you

#

this doesn't happen in __init__

#

also, once again, methods might not even be a dictionary and you need to turn it into one -- you can't do this in __init__

#

you can call __set_name__ manually of course, but probably you should just be using __new__ instead

charred wagon
#

Okay, good point on __set_name__

#

Yeah I'm not trying to go down that rabbit hole. If it's more convenient to do it in __new__ then that's a win for __new__

#

By the way, is it safe to use *args for __init__ like you did? Doesn't that assume those args will always be passed as positional?

#

Is that a reasonable assumption to make?

#

(when considering someone extending the metaclass with some arbitrary behaviour)

deft pagoda
#

typically i wouldn't use *args here, but keyword arguments are pretty rare for class creating -- still if you're doing something funky with metaclasses probably should just use a more explicit signature, i was just being lazy

charred wagon
#

Yeah I wanna be lazy too but I guess I can't be

swift imp
#

type.__new__ has explicit argument for methods?

#

Why don't I remember that

charred wagon
#

It's not just for methods. It's for attributes too.

#

It's basically the namespace.

deft pagoda
#

yeah

swift imp
#

Yeah

#

That's how I remember it

#

But you were using method and I was confused by it

deft pagoda
#

i call it methods because that's what it was called when i learned about metaclasses

#

but probably namespace is a better name

swift imp
#

Yeah and get rid of kwargs

deft pagoda
#

i wouldn't get rid of kwargs

#

well, i guess it depends on your metaclass

#

but you can definitely pass kwargs to it

swift imp
#

Doesn't __init_subclass__ args appear there

#

Wait so type.__new__ is really meta, name, bases, namespace, kwargs

charred wagon
#

Yes

deft pagoda
#

oh, you can remove kwargs from the super call

#

that's probably a smart thing to do

#

but this is ok:

In [32]: class MyMeta(type):
    ...:     def __new__(meta, name, bases, methods, **kwargs):
    ...:         print(kwargs)
    ...:         return super().__new__(meta, name, bases, methods)  # <- no kwargs
    ...:

In [33]: class MyClass(metaclass=MyMeta, a=1, b=2):
    ...:       ...
    ...:
{'a': 1, 'b': 2}
charred wagon
#

It's annoying that __new__ has to be defined with kwargs just to be able to accept them even if __new__ itself won't do anything with them.

deft pagoda
#

you don't have to have **kwargs in the signature, especially if you don't plan on doing anything with keyword arguments

swift imp
#

It's also confusing

deft pagoda
#

it's really rare to see them

swift imp
#

It's also confusing that type.__call__ begins the class instantiation

charred wagon
#

I don't think so.

#

call is what the () is, after all.

swift imp
#

But you don't even need it

charred wagon
#

Isn't __call__ the entrypoint of anything that's callable?

swift imp
#

class Foo: pass

deft pagoda
#

i've used __call__ quite a few times with metaclasses

swift imp
#

Perfectly valid

#

Ok let me back up

#

It's confusing that type.__call__ gets used for both class creation and instantiation

#

For the example I gave above

#

You don't need ()

charred wagon
#

What do you mean, it's called for creation?

#

Cause as far as I can see, it isn't.

deft pagoda
#

probably like:

In [41]: type('hi', (), {})
Out[41]: __main__.hi

In [42]: type.__call__(type, 'hi', (), {})
Out[42]: __main__.hi

is what he means

swift imp
#

Yes

#

If you shove print statements in all the dunders you'll see

deft pagoda
#

i mean __call__ is weird anyway

#
In [43]: def f():
    ...:     ...
    ...:

In [44]: f.__call__.__call__.__call__
Out[44]: <method-wrapper '__call__' of method-wrapper object at 0x0000022A0A0D6790>
#

what am i supposed to do with this

charred wagon
#

Is a class's definition not complete until metaclass.__init__ is called?

#

Cause I'm unable to access a specific instance of the metaclass from metaclass.__init__

tranquil trout
#

Hi can anyone explain how the class become subscriptable and callable while inheriting enum to the class ?

tidal marten
tranquil trout
#

I didn't say I can't use I'm asking how a class object becomes subscriptable and callable by just inheriting enum
import enum

class demo(enum):
ONE = 1
TWO = 2

demo['ONE']
demo(1)

#

in above code both work I'm asking how the class object can become both subscriptable and callable at same time

tidal marten
#

You can definitely do demo['ONE'] using some magic methods. But for demo(1), I think you will need to override the __init__ class?

raven ridge
#

not when you inherit from enum.Enum - but that's because enum.Enum itself has a custom metaclass, which adds that subscripting support to classes it creates.

tranquil trout
#

metaclass ?

raven ridge
#

the class of a class is its metaclass. When you're creating a class (as opposed to an instance of that class), you're constructing an instance of it's metaclass. That metaclass has nearly complete control over the created class.

tidal marten
#

This works fine though

from enum import Enum

class Test(Enum):
    ONE = 1
    TWO = 2

print(repr(Test(1)))
print(repr(Test['ONE'])) 
#

#bot-commands

raven ridge
tidal marten
#

Ohhhh, I misread that lol

tranquil trout
raven ridge
tidal marten
raven ridge
#

"overhead" in what sense? If you were to implement the same features from scratch, it'd take the same number of lines of code, give or take.

#

and if you don't import enum, you don't pay anything for it.

tidal marten
#

I mean at runtime, calling the metaclasses when doing Enum and such.

raven ridge
#

well, you can profile to get an answer to that.

tranquil trout
tidal marten
#

Yeah, it's like all that just to provide static typing 😂

raven ridge
#
In [54]: %%timeit from enum import Enum
    ...: class MyClass(Enum):
    ...:     x = 1
    ...:     y = 2
    ...: 
289 µs ± 27.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [55]: %%timeit
    ...: class MyClass:
    ...:     x = 1
    ...:     y = 2
    ...: 
32.1 µs ± 3.4 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
tidal marten
#

Not much ehh?

raven ridge
#

Looks like defining an enum class is around 10x slower than defining a regular class.

#

but still pretty cheap in absolute terms.

tidal marten
#

Hmm, I guess you won't have hundreds of thousands of classes in a single project anyway

raven ridge
#

and I'm on pretty slow hardware - old chromebook with a Celeron processor.

#

how many classes you have in a project depends how many libraries you're using, of course, but - yeah, hundreds of thousands sounds like a lot. I'd expect most non-trivial programs to have in the hundreds to thousands, give or take.

#

still, though - most of the time, your choice will be between using Enum and living with a fraction of a millisecond of overhead, or implementing your own crippled version of Enum that's good enough for your needs. The maintenance cost of the latter is probably higher than the performance cost of the former. Probably.

tidal marten
#

Kinda wish that these are optimized in the parser though. So that the parser just abstract away the Enum or typing or whatever without loading/executing the metaclass.

raven ridge
#

That's would require changing the semantics of the language. It would make it impossible to mock or monkeypatch the enum.Enum class if it were special cased at parse time

tidal marten
#

You can probably check for trivial case and optimize for that while still having the unoptimized codes available for lazy instantiation for when it's necessary. That would complicate things for the parser/interpreter though.

upper spire
#

can someone tell me how I can identify if a resource group in azure is unused/useless , im writing a script to delete them
1 is if its size is 0 but that is based on assumption

tidal marten
#

You should probably consult the Azure docs or the relevant server/channel for that

peak spoke
#

Pretty much any analysis is going to be broken when exec, eval etc. is used so you'd have to disable all the optimizations if there's one in the code

tidal marten
#

Yeah. Probably only useful for static analyzer, not much for everything else.

raven ridge
tidal marten
#

Can't you hypothetically add new opcode for e.g. optimized enum and do checks if it's being monkeypatched at runtime?

raven ridge
#

I can't imagine how. Even if you could, it would be extremely expensive to check every class and attribute and helper function to see if it has been patched or not at runtime

#

enum.Enum could have been set to something unexpected, or enum.EnumType, or any attribute of either of those classes, or any function of built-in used by either of those classes

tidal marten
#

I was thinking of maybe optimizing only certain functionalities that are trivial. Ideally only classes that directly subclasses only Enum for example will benefit from this. This also depends for the implementation of Enum to be standard and unchanged, so it won't play nice with runtime monkeypatching.

#

So yeah, it's probably only good for static analyzing.

raven ridge
#

Yeah - detecting that something has been monkeypatched is itself non-trivial - and it needs to be applied recursively

tidal marten
#

Optimizing something that takes submilliseconds is just wasteful anyway, lol

visual shadow
#

The cost of running the check itself would kill any gains you would have gotten from the scenario of optimized code on the very small subset in your program that would benefit from the check

#

The power of monkey patching comes at the cost of not knowing the program state till runtime. Especially if, say, you wrote your code, but someone else could always import your code and patch it later. Allowing that capability means the program simply cannot take a guarantee on just about any part of the code.

raven ridge
#

And that's basically the intended case for monkey patching - it allows code that does one thing when run normally do a different, simpler thing when run from tests

#

Without changing the Python code in the module itself at all

snow kettle
#

I'm not sure if this is the right channel for this. Please let me know if it isn't.

I'm confused by the interpreter flags available to show additional debugging information, and need suggestions to help debugging strangely blank output:

Error in sys.excepthook:

Original exception was:

The process reports as exiting with 0 status despite the exception report being printed. Are there any flags or suggestions people have for where I could go in debugging this?

Simple test files run from the project directory seem to work fine, so I don't think it's pycharm's configuration. If there's a memory allocation issue or somethings odd going on with bound C libraries somewhere in the framework I'm using, could that be causing this?

snow kettle
visual shadow
#

Just so I'm on the same page with this... Process exited with exit code 0, the code 0 is usually used as "everything went fine". So, did you know that? If yes, why are you expecting this to give an exception?

snow kettle
#

I'm not expecting it to give an exception, but it gives what looks like one except the information is blank.

#

That's why I'm confused. The interpreter says there was an error, and prints a blank line for the original exception.

#

Obviously I shouldn't put null bytes in places they shouldn't be, but the question of how to get better information out of python in cases like these is still relevant for the future.

snow kettle
#

Ok, it might not be nulls in names.

snow kettle
#

I'm still stuck on how to go about debugging it.

gleaming rover
tropic gale
#

Ooh, sorry.

#

Just read the desc.

unkempt rock
#

Is there a way to check the text of a button with webdriver? Because the class is generic and there is multple ones with the same thing

boreal umbra
brave badger
#

!e What's up with:

print([0xfor x])
```?
fallen slateBOT
#

@brave badger :white_check_mark: Your eval job has completed with return code 0.

[15]
brave badger
#

Not exactly sure what 0xfor stands for

feral cedar
#

it's a hex literal, 0xf

#

since that's truthy, the x isn't evaluated

brave badger
#

oh wow

sand python
#

why is that allowed is the real question

raven ridge
#

it doesn't work in some Python interpreters. micropython doesn't allow or to be used without a space before it, I know

#

works in pypy, though. I think it's just a grammar quirk, though - some early version of the interpreter allowed it accidentally, and by the time anyone noticed it wasn't worth changing it because they didn't know if it would break anything.

grave jolt
quiet crane
#

Why would Counter.subtract keep zeroes while subtraction with operator clears them?

>>> c = Counter(potatoes=10)
>>> c - c
Counter()
>>> c.subtract(c)
>>> c
Counter({'potatoes': 0})

What's the rationale behind this behavior? (Cross-posting from python-general)

peak spoke
#

The method can also go below zero while the operator will filter out non positive vals. On the why I'd say it's to keep track of what was encountered, the subtract modifies the existing object, while the operator creates a new one which doesn't "need" any knowledge of the items that didn't exist because of the 0/neg vals

quiet crane
#

oh wow, I would have thought it kept negatives! good to know. I think your "why" could be the reason.

#

Since it can handle negatives:

>>> c.subtract(c + c); c
Counter({'potatoes': -10})
boreal umbra
#

@frozen abyss This is strictly a discussion channel about the language.

frozen abyss
#

Ok sorry about that

red solar
#

does python have a feature like c/c++ compilers where you can give it a path to search for imports in?

#

kinda like gcc/clang's -I flag

radiant fulcrum
#

mmm probably the closest thing to it is virtual environments and yh

#

other than that not really

red solar
#

how would i do that with venv?

radiant fulcrum
#

well you'd basically just make a venv and install any deps there (or use something like pipenv) and let that store the deps etc...

#

things like pipenv allow you to customise the deps a bit more depending on setting etc...

red solar
#

hmm that doesn't look like it's as advanced :/ might just copy paste the folder into my project and use the copy

boreal umbra
#

!e

import sys
print(sys.path)
fallen slateBOT
#

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

['', '/snekbox/user_base/lib/python3.9/site-packages', '/usr/local/lib/python39.zip', '/usr/local/lib/python3.9', '/usr/local/lib/python3.9/lib-dynload']
boreal umbra
#

Though I don't believe that's recommended 😛

raven ridge
raven ridge
errant apex
#

Im gonna scream

#

I am doing excercises for my programming finals

#

and we have to do it all in syntax-strict pseudocode

#

I could do them in any language from C++ over Python to Java

#

but that Assembly-looking 50 year old pseudocode, for which we need to know that syntax

#

is so infuriating

red solar
#

oh cool, thanks @boreal umbra and @raven ridge 🙂

thick stump
feral cedar
#

you'll need to be able to evaluate what code does without actually running it

pseudo cradle
#

Wait, how do you determine what code does without running it?

#

Even my testing framework still runs code

feral cedar
#

reading it?

pseudo cradle
#

Oh

#

I thought there was some magic way for a computer to test code without running it

#

I wanted to learn this black magic

#

Hey maybe one day

pliant tusk
#

probably not for a long time due to the Halting Problem

#

In computability theory, the halting problem is the problem of determining, from a description of an arbitrary computer program and an input, whether the program will finish running, or continue to run forever. Alan Turing proved in 1936 that a general algorithm to solve the halting problem for all possible program-input pairs cannot exist.
For ...

novel vector
#

Silly question for today : When I open binary file in a 'rb' mode, and then use readlines on that object, what is deciding on when the actual line is ending?

feral cedar
pliant tusk
#

how would you define your subset?

pseudo cradle
#

Isn't that just like... the halting problem explained though?

pliant tusk
#

also that ^

pseudo cradle
#

It isn't that beneficial to have a halting problem solution that.... halts before it determines if a problem halts or not

feral cedar
#

maybe i'm grossly misunderstanding >w<

pseudo cradle
#

It's okay, that's what I do with life before I've had coffee

pliant tusk
#

you could have the following code py while True:pass

#

its clear that it is an infinite loop right

#

the only way for a program to tell if any code infinite loops is to run it

pseudo cradle
#

Well, one isssue is convergence. Maybe a loop will end given enough interations, maybe it won't. How do you solve for it?

flat gazelle
#

you can pick a subset of turing machines for which halting problem is solvable

#

for example, a program consisting of a single integer number will not halt

pseudo cradle
#

while num != 0:
num /= 2

pliant tusk
#
import random
while random.random() < 0.5:
  pass
pseudo cradle
#

Yeah and that doesn't even have convergence to help you

flat gazelle
#

I don't think the halting problem considers non determinism

pliant tusk
#

it does i believe

#

In computability theory, the halting problem is the problem of determining, from a description of an arbitrary computer program and an input, whether the program will finish running, or continue to run forever. Alan Turing proved in 1936 that a general algorithm to solve the halting problem for all possible program-input pairs cannot exist.
For ...

#

looks like it considers it by looking at all possible states

spark magnet
#

i think the point is that even only considering deterministic programs, you can't solve the halting problem.

pseudo cradle
#

If anyone can solve it, it's @spark magnet and @raven ridge

#

Get on it, you two

raven ridge
fallen slateBOT
#

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

001 | b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n'
002 | b'\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
barren fiber
#

Hello here. Does anyone know if pyinstaller or py2exe works in the windows linux subsystem, to create windows executables ?

pliant tusk
#
import gc

class magic:
    def __length_hint__(self):
        return 1
    def __iter__(self):
        for obj in gc.get_objects():
            if type(obj) == tuple and len(obj) == 1:
                try:1 in obj
                except SystemError:
                    yield obj
                    break

weird = tuple(magic()) # weird now contains itself``` would this be considered a bug?
#

because i assume that gc.get_objects() isnt supposed to return objects that are still being constructed

radiant fulcrum
#

Right so

#

I have a interesting situation that's certainly going to be a learning experience

#

but basically, What do you think is going to be the best way to implement Dynamic typing in a strictly typed ByteCode setup? The bytecode being web assembly.

#

the TL;DR of what i've been messing with is a Runtime/Transpiler which runs Python bytecode as web assembly

#

while the transpileing part is surprisingly easy logic wise because Python and wasm run on very similar stack styles

#

dynamic typing is something which im a bit torn about whats the best way to achieve it

flat gazelle
#

cpython uses consistent layout of the first few bytes of object structs, which allows unchecked casts into PyObject* without segfaults

radiant fulcrum
#

do you have a example of how that works logically?

#

this is essentially what it produces bytecode wise:

(module
 (memory $0 1)
 (data (i32.const 12) "\1c\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\08\00\00\00y\00e\00e\00t\00\00\00\00\00")
 (table $0 1 funcref)
 (export "foo" (func $module/foo))
 (export "memory" (memory $0))
 (func $module/bar
 (local $0 i32)
  i32.const 32
  local.set $0
 )
 (func $module/foo
  call $module/bar
 )
 (type $none_=>_none (func))
)```
#

the approach i considered taking is basically not compute anything until it is directly needed and then feed that in

#

but that naturally adds alot of extra overhead on the already fairly steap overhead as well as i dont think it's necessarily the optimum solution

flat gazelle
radiant fulcrum
#

yeah, thats sorta what im thinking

flat gazelle
#

how does wasm do polymorphism? Indexing into a large array?

radiant fulcrum
#

I believe that depends on the backend runtime it's using

flat gazelle
#

how does rust manage to compile dyn traits then? Those do runtime polymorphism, so there has to be some way to handle that.

radiant fulcrum
#

hmm true

flat gazelle
#

are there function pointers of sorts?

radiant fulcrum
#

yes sorta

runic kelp
#

i think wasi might help here

halcyon trail
#

I have a meta class question

#

I was messing around with a bit of code to help with auto registering some classes according to a certain pattern

#

Haven't done a ton with metaclasses before

#

I have something working seemingly how I want, but there's a bit of a hack in it, I was hoping I could do a little better

#
class MetaRegistrar(ABCMeta):
    def __new__(mcs, name, bases, class_dict):
        cls = ABCMeta.__new__(mcs, name, bases, class_dict)
        if name != "CheckBase":
            print(cls)
            _check_registry[cls.__name__] = cls
        return cls


class CheckBase(metaclass=MetaRegistrar):
    @staticmethod
    @abstractmethod
    def create_check(args: CheckArguments) -> Optional[CheckBase]:
        pass


@dataclass
class SomeCheck(CheckBase):
    check_info: int

    @staticmethod
    def create_check(arg: CheckArguments) -> Optional[CheckBase]:
         # implementation here
#

so this is the gist of it, basically, it allows adding these new checks (which hold onto heterogeneous information related to the check), and it automatically puts them in this dictionary global _check_registry to be used to run checks by name, run all checks, etc

#

I'm basically happy with this, but notice the weird hacky if name != "CheckBase"

#

I added this after I realized that the base class was also getting registered, of course, and since it's not a real check, I don't want it to be

#

is there a more elegant overall approach here?

#

i'm also not 100% convinced I understand why this works, really. Metaclasses are not inherited, so I'm not really sure why MetaRegistrar.__new__ gets called for every derived class... they are all inheriting from the same CheckBase, which is the only class using that metaclass, so why isn't new only called once?

radiant fulcrum
#

wasi adds some interfacing stuff for WASM to access but other than that

runic kelp
#

i dont think i quite clearly understand

radiant fulcrum
#

Well the wat produced is fed into wasmer which handles the runtime etc...

#

external imports arent anything im planning to deal with yet

#

I think how it's looking is probably building the wat as and when it's needed so the types are known before hand

modern night
halcyon trail
#

hmm ok

#

That was my initial thought and then I found an SO that contradicted that seemingly

#

I think it may only be true in python 2?

#

actually

#

I just misread it

#

alright, thanks for the clarification

#

As to the actual solution, it seems like a simpler and better approach is to use the init_subclass hook, which I had never heard of

supple geyser
#

is pep636 active? I have the 3.9 interpreter but not even the pep cases work

raven ridge
supple geyser
#

@raven ridge yes, I got it working now, it's great 🙂

swift imp
#

Can you do a mapping pattern match that matches on value type for certain and entries and then value literals on others?

undone hare
#

I think so

quiet crane
#

Is this a good place to discuss a PEP idea?

peak spoke
#

Not a bad place, but the mailing lists would probably lead to more discussion over time

quiet crane
#

Where can I find them?

peak spoke
quiet crane
#

Thanks 👍

gusty swallow
#

ye

spark magnet
#

@quiet crane this could be a good place to chat about the idea first. the mailing lists can be a rough place, and getting people's impressions first could create a better first message.

boreal umbra
#

@spark magnet in that case, what do you think of a boolean switch for collections.defaultdict where the missing key gets passed to the default factory as an argument, instead of the value returned by the default factory being static?

pliant tusk
boreal umbra
#

I'm the ta for a class that uses python at the moment. A lot of students are using python for the first time and they remark that the language comes with so many niceties out of the box.

visual shadow
#

Guidos time machine, as we say.

quiet crane
#

Time machine?

prime estuary
#

It's a joke about how people might think that they need to request a feature, only to find that it's been there all along.

quiet crane
#

Ok, my idea for convenience is a conflict_resolution argument to dictionary.updste
Today dict_a.update(dict_b) will overwrite items in a if they also exist in b.

More than once I have had dictionaries of lists where I would have liked to extend the lists instead of just getting the ones from dict_b.

Example:
a_dict.update(b_dict, operator.add)

#

With this you could also merge the dictionary values in other ways. Like keeping the items from a if they exist in both.

#

Other use cases could be:
a.update(b, max)
a.update(b, min)

visual shadow
#

I don't think this belongs in the update method, but sounds nifty as a way of merging dicts

#

If this was an external utility function, it could take an arbitrary iterable of dicts instead of being limited to just merging on a single update

#

The real question though is whether it's actually a common enough operation or not. And that requires verifying against popular code bases. Some real stats to back it up, so to speak

#

For what it's worth, I've written code where I wanted to retain the first occurance of keys while merging, and also the min/max have been handy

native flame
#

fwiw this exists in java's hashmaps so it couldn't be that uncommon, i guess

quiet crane
#

@Darr any ideas on what code to look through?

#

I thought about that, but Where would an external function be put?

prime estuary
#

That'd mean just defining it yourself as a regular function.

quiet crane
#

Java has the merge method for merging single item into a map:
hashmap.merge(key, value, remappingFunction)
The putAll method is like python's update method.

#

@prime estuary what do you mean? The proposal is to include the functionality in python so that you don't have to write a function for it.

prime estuary
#

One issue is the existence of duck typing in Python. In Java, big class hierarchies are expected, and any dict/mapping object must inherit from the base class. So adding all these utility methods is easy and applies to all of them. In Python instead it's all agreed interfaces, so it'd need to be separately added to every mapping type.

#

I was explaining what Darr meant by an "external utility function".

quiet crane
#

Right, and therefore a free function is less intrusive?

#

Sure I understood what it means :)

prime estuary
#

It would be yeah, same reason we have str.join not list.stringjoin.

quiet crane
#

The question was more: what should the free function be called and in what package does it belong?

#

Right

#

So dict.merge for example

prime estuary
#

That's a good question, since we don't have a mappingtools module :)

quiet crane
#

Do you know of other msppingtools that have been discussed? 👀

prime estuary
#

I don't no.

fallen slateBOT
#

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

{'a': 1, 'b': 2}
#
**PEP 584 - Add Union Operators To dict**
Status

Final

Python-Version

3.9

Created

01-Mar-2019

Type

Standards Track

native flame
#

kaddkaka is referring to what happens when the same keys exist in both dicts

quiet crane
#

Yes exactly. Like Counter for integers

native flame
#

currently, the value for that key in the first dict is overwritten by the value in the second

#

they propose that instead of overwriting, a function be passed which decides what happens

quiet crane
#

Whatever you want. You provide a binary function

native flame
#
dict1 = {'a':2}
dict2 = {'a':3}
a|b -> {'a':6}
quiet crane
#

Yes

#

Like normally calling a function with incompatible types would.

#

The original idea was to not treat nested dicts differently. But I guess that could be considered differently. Hmm, deep/shallow merge

#

I think it's difficult to do something generally correct (intuitive) for nested dicts. I guess if you assume the level is uniform it's OK I guess.
d1 = {0: 100}
d2 = {0 : {10: 10, 20: 20}}
merge(d1, d2, +) 🤯

#

But you could all distribute it, to end up with
{0: {10: 110, 20: 120}} but it's quite exotic!

visual shadow
#

I'd say as long as you can write a function for mapping, it's perfectly reasonable to expect that said function would manage dict merge logics. It shouldn't be a problem for the interface itself.

#

Just like we don't really care how sorted would work on lists that contained lists, if it errors out, then the key they passed didn't make sense for nested lists. That's on the programmer.

#

The interface should be as simple as possible, and I like the idea of a key for merging. As for where such a function would belong? Frankly I'm not sure.

#

I definitely dont like silentatly propagating operations though

#

That will probably break for people expecting diff behaviour in different ways.

quiet crane
#

I agree

grave jolt
quiet crane
#

@fix error The same goes for mappings

prime estuary
#

Indeed, and similarly you could implement the merging function without requiring the object to be a dict subclass only.

violet crest
#

help me out on this code . Error in line 8

#

fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
line= line.rstrip()
linelist = line.split()
for word in linelist:

    if (word in lst):
        continue
    else : lst = lst.append(word)

y= lst.sort()
print(y)

coarse ore
#

You assign an NoneType in the else part of your if statement. So after the first append, lst.append won't work anymore.

desert peak
#

is it possible to take self in a class and replace it with something else? I just tried, but my variable remained the same

#

something like

def transmute(self, other):
  self = other
charred wagon
#

That would update the local variable self inside the function successfully

desert peak
#

but isn't self a reference? like, shouldn't it replace the class instance?

charred wagon
#

No, variables don't work like that.

limpid forum
#

functions always get a copy of variable, so you only change the reference inside the function/method. whatever it was outside, it still is the same

charred wagon
#

It's a copy of a reference, so it only updates your local copy.

desert peak
#

that seems weird/unintuitive to me

#

like, I can mutate self.prop and it persists the change in the binding, but modifying self itself does not?

charred wagon
#

self is basically just a memory address and Python is passing around that address by copying it.

limpid forum
#

if you change self.prop, you don't change self but the address it contains

charred wagon
#

Then your function looks up the object at that address and you can mutate its properties.

desert peak
#

right, so it's not possible to get the address (without digging into some black magic) and change what's there

limpid forum
#

the best description of references is:
imagine a variable is a paper with address written on it
if you change any attribute, it means you go to the house under that address and change something there

now your function gets a copy of the variable - you get a paper with address written on it, pointing to the same house
doing self = something makes you basically erase/cross out the address on your paper and write a new one
but the original piece of paper is still there. you don't change it. it still has the original address

peak spoke
desert peak
#

Not because I'm actually trying to implement anything noteworthy

#

does python even allow you to change a binding without being explicit about it

limpid forum
charred wagon
#

I'm not sure exactly how to do it but for classes you can change what self is I believe because the binding mechanism relies on descriptors

peak spoke
#

What would you expect self = other to do? Change the object for all references to the old one?

raven ridge
#

that would require replacing a potentially infinite number of references - and Python doesn't know where they all are.

visual shadow
#

python variables are references to values. So, there's a very simple contract that happens on each new variable assignment. there's no "reverse linkage" of a value knowing all it's refernces

#

so, expecting values to propagate back on a re-assignment just doesn't fit with python's worldview of variables and values

#

the reasons mutations (such as self.prop )reflect across all references are because they're mutations. the object/value itself is fundamentally changing, the variable name -> value contract doesn't change there either

#

Hope that makes sense.

raven ridge
#

a better way to think about what's happening here is that both self = obj and self.prop = obj are just updating a name to refer to a new object, but the namespace in which the name is rebound is different.
self = obj binds a local (if in a function or class) or global (otherwise) variable to the given object. It updates a single variable to refer to a new object, and that single variable is either a local or global variable.
self.prop = obj goes into the self object, which itself holds a namespace of names mapped to values, and it asks self to update its prop name to refer to the new obj within that namespace. It's still only updating a single variable, but instead of updating a global or local variable, it's updating a variable within the namespace of the self object.

desert peak
#

hm, would it be possible to get the memory address with id and transmute it to your own type?

limpid forum
#

I think we could also use a wrapper - just have a class that contains self._real_object so you can change it
but to treat it like itself from the outside (my_object.something = 1, instead of doing my_object._real_object.something = 1 all the time), you'd need to overload all/most dunders

charred wagon
#

Or maybe I misunderstood what you're asking

desert peak
limpid forum
# desert peak are you sure?

that's "the address on the piece of paper" from the metaphor right now
if you do self = something inside, id(self) will change

charred wagon
#

Yeah sorry I was not thinking of it correctrly

desert peak
raven ridge
#

what do you expect the state of the world to be after doing this?

a = T()
b = a
c = T()
a.transmute(c)

At the start the call to transmute there are 3 references to the T instance that was created first (globals()["a"], globals()["b"], and the self local variable in the transmute call), and there's 2 references to the T instance that was created second (globals["c"] and the other local variable in the transmute call).

visual shadow
#

in the realm of sanity, python doesn't give you the mechanism to hijack an object's id and deliberately substitute it for something else entirely. However, python does offer ctypes for hacking through things. So i assume you may be able to change the bytes there yourself. suffice to say, this is literally one of the worst ideas you can implement in day-to-day code.

desert peak
desert peak
#

and whatever a was referring to to be cleaned up by GC

#

as it becomes unreachable

visual shadow
#

I think it's doable. im also quite sure it's beyond my capability to mess with something like this unless i commit a lot of time to it. Which means my claim about it being doable is worth the paper it's written on. So that's fun. but yes, that would require hacking past the usual stuff into the bytecode, since in "normal use syntax" this is not a thing.

desert peak
#

yeah, I imagine with ctypes anything is doable, even segfaults 😄

visual shadow
#

mhm

charred wagon
#

Seg faults are quite easy

#

But in any case the notion of trying to replace the object like that sounds scary and bound to fail

red dock
#

seg faults are easy?

charred wagon
#

To reproduce one yeah e.g. ctypes.string_at(0)

limpid forum
#

please mind that I don't normally deal with manual getattr, so it's just the most basic thing to show what I meant by a wrapper:

class Wrapper:
    def __init__(self, real_object):
        self._real_object = real_object
    def transmute(self, other_object):
        self._real_object = other_object
    def __getattr__(self, item):
        if item != "_real_object":
            return self._real_object.__dict__[item]

basic test:

>>> a
<__main__.Test object at 0x000002EB11B11F40>
>>> a.a
1
>>> b
<__main__.Wrapper object at 0x000002EB11B11670>
>>> b.a
1
>>> id(b.a)
3208634591536
>>> id(a.a)
3208634591536
>>>
#

https://wrapt.readthedocs.io/en/latest/wrappers.html omg, someone already did a wrapper like that (found it via https://stackoverflow.com/questions/57589110/python-getattr-doesnt-work-for-built-in-functions when searching "python getattr" for my example 😄 )

toxic garden
#

Hello can I talk with someone with experience in React.js or an experienced HR
I'm struggling in my job search and would appreciate a conversation

pliant tusk
#

@desert peak it is possible to do what you want without ctypes

raven ridge
# desert peak taking this literally, I'd expect `id(a) == id(c)`

Names don't have id's, objects do. A name refers to an object, and the object itself has some id. As a CPython implementation detail, these two are in fact the same thing: a name refers to an object by keeping a pointer to it, and the id of an object is its location in the interpreter's memory, represented as an integer.

And the id of an object can never be changed. At the start of the call to transmute, there's two objects, one called a and b and self with one id, and the other called c and other with a second id. If you're expecting that after executing self = other that a, b, self, c, and other all refer to the second object, then the interpreter would need to find and update 3 different names.

#

Which it doesn't do, and doesn't even have the information to do in an efficient way.

#

and the most efficient way, even if it did have the information to do it, would still be linear with the number of references that need to be updated

pliant tusk
#
import gc

class a:
  def transmute(self, other):
    referrers = gc.get_referrers(self)
    for referrer in referrers:
      # swap self for other in each referrer (if mutable)
      pass```
#

that will change every mutable reference (known to the garbage collector) to swap self and other

#

to do the switch at the name level (ie self = other is equivalent to self.transmute(other)) you can parse bytecode to get names and then traverse frames

raven ridge
#

I think that's actually a better example for why this isn't possible than what I was giving! If any of the references are in an immutable object, then even if you can find them, you still can't update them.

pliant tusk
#

you cant update them safely

#

you could in theory update whatever is containing the immutable

visual shadow
#

well, in the spirit of exploration, i created this (admittedly simpler) example on the ctypes hack i was thinking of.

pliant tusk
#

and go up recursively

visual shadow
#

!e

import ctypes as c
import sys

a = 999
b = a

size = sys.getsizeof(a)
new_var = 1001
size2 = sys.getsizeof(new_var)

assert size == size2

px=(c.c_char*size).from_address(id(a))

# px.raw

px2 = (c.c_char*size).from_address(id(new_var))
px.raw = px2.raw

print(a)
print(b)
print(new_var)
fallen slateBOT
#

@visual shadow :white_check_mark: Your eval job has completed with return code 0.

001 | 1001
002 | 1001
003 | 1001
raven ridge
#

you can't update them without breaking Python's data model and potentially leading to corruption of arbitrary data structures.

visual shadow
#

i was very conflicted on whether to post it or not though...it just..its all kinds of wrong

pliant tusk
visual shadow
#

ah, interesting. is it a fixed size that i can account for?

pliant tusk
#

you can use py def sizeof(obj): return type(obj).__sizeof__(obj) to get closer to the proper size but some types add more data then the base struct size to __sizeof__

#

for example sizeof(user_cls) is the sizeof the PyHeapTypeObject struct and a dict keys object if the class has cached keys

#

there isnt a perfect universal sizeof that works for all objects that only returns the size of that memory at id(object)

#

afaik sys.sizeof and __sizeof__ are really only supposed to be used for memory profiling

visual shadow
#

looks like i lucked out for ints at least, they seem to be in sync in this example for me

raven ridge
#

ints aren't tracked by the garbage collector.

pliant tusk
#

yea and int.__sizeof__ doesnt traverse anything else

visual shadow
#

wait, what does "tracked by garbage collector" mean? could you explain that part

pliant tusk
#
static Py_ssize_t
int___sizeof___impl(PyObject *self)
/*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/
{
    Py_ssize_t res;

    res = offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(self))*sizeof(digit);
    return res;
}``` this is `int.__sizeof__`
raven ridge
#

and because it's impossible to set an arbitrary attribute on an int, and because an int doesn't itself refer to any other objects, an int doesn't need to register itself with the GC when it's created.

visual shadow
#

ah! got it

desert peak
#

this was an interesting conversation, thanks for indulging me

eager trail
#

Fascinating

#

This is good to know

oblique timber
#

does anyone know about this
Write down the mode 0 control words of 8255 for the following two cases:

(a) Port A = Input port, Port B = not used, Port CU = Input port and Port CL = Output port.

(b) Port A = Output port, Port B = Input port, Port C = Output port

quiet crane
#

@oblique timber what does this has to do with python?

oblique timber
#

this says advance discussion so i thought if anyone know about this can let me know the answer

quiet crane
#

This channel is for advanced python disc though

oblique timber
quiet crane
#

It describes all the bits d0-d7, how is it not enough?

visual shadow
#

Perhaps try our off topic rooms, this room is about discussion of the python language itself. Please see the room description for more information

quiet crane
#

Hi @visual shadow , about the dict-merge feature. Is the next step to discuss it more here, mailing list, or try to write a PEP?

quiet crane
#

I guess you talked about looking through popular code bases, I will try to find something. Got any pointers?

quiet crane
glacial pagoda
#

Anyone here use Scrapy? I don't quite get the concept behind pipelines and why I should use them.

waxen folio
#

Hey hacker's

glacial pagoda
quiet crane
#

@spark magnet It's my idea to provide mappingtools.merge() or a resolve methoed to dict.update() to be able to merge dicts more elegantly

#

a.update(b) updates dict a by replacing overlapping keys with values from b. Imaging being able to do:
a.update(b, resolve_function=max) to select the maximum value from a and b on overlapping keys
a.update(b, resolve_function=operators.add) to extend list-values (dict of type : Dict[KeyType, List[ValueType]])
etc...

charred wagon
#

It can't be added to update due to existing kwarg behaviour. It'd have to be a separate function I suppose.

quiet crane
#

ok thanks for the feedback

charred wagon
#

The idea was more appealing when it was part of update

quiet crane
#

whats the problematic behavior of kwargs that prevents it?

charred wagon
#

update will update the dictionary using the kwargs given

#

like dict.update(key1=value)

quiet crane
#

oh I should read the doc ...

#

aha ...

charred wagon
#

So if someone was already using the kwarg resolve_function it would break their code

quiet crane
#

yup

#

I never used it in any other way than d1.update(d2) so didn't think about that. Hmm

charred wagon
#

It would've been a nice API but unfortunately it's a breaking change.

quiet crane
#

d1.update_cool() is also out of the question I guess?

charred wagon
#

That's a bit awkward. It would feel redundant even though the underlying reason is to avoid breaking changes.

flat gazelle
#
d1.update(d2, max)
```isn't breaking
#

but it breaks any custom dict implementing their own update

charred wagon
#

I feel like this sort of thing is better left to a third party library akin to moreitertools.

grave jolt
#

yeah, it's not very big

#

but small utility libraries are sort of weird

#

if you take a new dependency for every small function-that-should've-been-in-the-stdlib-but-isn't, you get many dependencies

#

basically javascript hell

#

I would just implement a function like this from scratch

quiet crane
#

I've come across the need for this function a few times, and seen it in others' code as well

#

Therefore it would have been nice to have it ready

#

I didn't know about moreitertools, looks interesting

grave jolt
#

!pypi even-more-itertools

fallen slateBOT
grave jolt
#
def invert(predicate):
    return lambda v: not predicate(v)

this is the sort of javascript hell I'm talking about 🙂

flat gazelle
#
def merge_by(left, right, fun):
    for k, v in right.items():
        if k in left:
            left[k] = fun(left[k], v)
        else:
            left[k] = v
```is simple enough that I don't think it's too bad to just write it from scratch
grave jolt
#

ah

#

yes

flat gazelle
#

yeah, I may be wrong about the simple thing

raven ridge
#

Should probably be fun(left[k], v) instead

grave jolt
#

but lakmatiol

#

.randomcase you forgot the type hints

neon troutBOT
#

yOu forgOt THe TypE HiNts

quiet crane
#

Right. The invert() I have used, but function composition would have been really nice: not . predicate e.g. sort(..., key=not . predicate)

grave jolt
#

yeah

#

languages with things like that need fewer silly small functions

flat gazelle
#

I just do

lambda k: not predicate(k)
grave jolt
#

yeah, that seems simple enough

quiet crane
#

sure it works, but more wordy and ugly than not . predicate

flat gazelle
#

personally, I don't really mind these minor details. In the grand scheme of things, it doesn't really matter.

quiet crane
#

(the . looks a bit out of place in python code perhaps)

flat gazelle
#

there is a proposal for fun @ other as composition similar to decorators.

charred wagon
#

How would that work? One function would be bound as an argument to the other?

quiet crane
#

I guess it makes sense, the decorator syntax is already composing functions

flat gazelle
#
def __matmul__(self, other):
    return lambda *a: self(other(*a))
charred wagon
grave jolt
#

ah

#

well, I suppose some special object like partial

#

that would store the list of functions to apply

raven ridge
#

it's technically possible to make the proposed change as a backwards compatible change to update by making it a second positional-only argument. Though that's much less obvious/intuitive/self-describing than a new keyword argument would have been.

grave jolt
charred wagon
grave jolt
flat gazelle
#

you could also just put compose(*funs) into functools

#

but honestly, I don't think there is much reason to do functional programming for the few keyword arguments that take functions

quiet crane
#

@raven ridge right, and an underscore-argument doesn't make a different I guess? it just looks uglier: d1.update(d2, _res_func_=)

flat gazelle
#

yeah, update takes any kwarg, just like dict

grave jolt
fallen slateBOT
#

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

001 | False
002 | True
grave jolt
#

🙃

raven ridge
charred wagon
#

What's up with the syntax highlighting of Not there

grave jolt
fallen slateBOT
quiet crane
#

lol

white night
#

hello ^_^

charred wagon
#

Hi

quiet crane
#

@grave jolt Why cyrillic?

grave jolt
#

so I can't name a variable like that

#

and I named the class accordingly

quiet crane
#

@grave jolt and nоt . even parses as not.even?

#

Ah ok

grave jolt
#

yes

quiet crane
#

right

grave jolt
# quiet crane right

!e
the cursed syntax can be extended much further

def main():
    x <- get_line
    y <- get_line
    s = (nоt . even) (int(x) + int(y))
    print(s)
fallen slateBOT
#

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

[No output]
quiet crane
#

haskell monads incoming!

gleaming rover
#

honestly I was just thinking I would like Option in some Django code I'm writing

desert peak
pseudo cradle
#

I would love stronger typing in Python

#

Some days it feels like the only reason I'm using Python is for the ecosystem and because matlab licenses are too expensive

crimson sierra
#

Hi everyone! I saw a Python wrapper for Autohotkey, but I was wondering where I would go to learn how to make a Python Library to manipulate the Windows API directly? I found the Microsoft docs and it seems like it's mostly assembly, should I invest time in assembly or is there a higher level way to do this? Would be nice if I could just use Python to make the new library. I have done several Python courses but never have I seen a course on how to make new Python libraries and contribute. I'm very open to suggestions

deft pagoda
#

this isn't a help channel unfortunately, but a discussion channel about python, though i'm not sure the appropriate topical channel for your issue, there is a way to access windows api with i think it's https://github.com/mhammond/pywin32

sudden canopy
#

you can use ctypes with its windll

crimson sierra
#

@sudden canopy thanks!

eager trail
#

Guys is there a less resource intensive way to do the followint

data['test'] = [data['test']] if not isinstance(data['test'], list) else data['test']

#

I feel like there's definitely a way that uses less memory

prime estuary
#

Just put it in a variable?

test_data = data['test']
if not isinstance(test_data, list):
  data['test'] = [test_data]

Or with the walrus operator for a slight shrinkage:

if not isinstance(test_data := data['test'], list):
  data['test'] = [test_data]
#

The better solution would be to make data not have unwrapped lists, if possible.

eager trail
#

Not possible since its an api response in either a single or listed values

#

Tanks teamspen

#

Oh, our runtime is 3.6 and 3.7, no walruses here :(

sacred tinsel
#

Why is that too resource intensive?

#

You're just conditionally creating a single list

#

I don't see how you could use less memory there

unkempt rock
#

Hello family how are you?
I am a deginner in Python Langage. I am delighted to find among you in order to better equip myself

eager trail
quiet crane
#

@eager trail Why? That sounds like bad way of working. WIll you ever finish if you sneak in "any #optimisations'" you can? How do you even know if they are optimsations? And why would they be needed?

boreal umbra
#

@lunar panther Glad to hear that you found a solution. Keep in mind that this channel is for discussion about the language, not for advanced questions. Your question about dataframes would be great for #data-science-and-ml.

astral crystal
#

Hi

#

How do I make pictures with poython?

sacred yew
#

@unkempt rock @astral crystal both off topic for this channel

lunar panther
#

Ok, sorry, I've been misled by the title and forgot to read the channel description, my bad.

tawdry scroll
#

have anyone used pyenv library i need help

astral crystal
#

No

#

I havent

jolly dawn
#

heyall, I am a high school senior who spent the last year taking a python-centered class. We covered lists, functions, classes, strings, dictionaries, exceptions (this one very vaguely) and some other topics. What and where do I learn next?

boreal umbra
#

This would be on-topic in #data-science-and-ml and not in this channel. Please try to frame it more as a topic for discussion.

boreal umbra
jolly dawn
crimson sapphire
#

@eager trail use a dictionary

azure fiber
#

Can anyone tell me what is the best way to keep two version of python in my system?

flat gazelle
#

pyenv for linux, just the default installer for windows.

azure fiber
#

I use parrot, and by default it provides python 3.9 but I have to run rasa , so I need python3.8 , I have installed in by using update alternatives and after upgrade it removes that

azure fiber
crimson cipher
#

I need help can time.time() set to be 0 again ?

azure fiber
#

Tried deactivate doesn't work

#

Tried pyenv deactivate doesn't work

flat gazelle
#

you essentially switch to the environment "system", either with pyenv local system or pyenv shell system

azure fiber
#

Ok, get it.

sacred tinsel
#

you can try doing pyenv versions, it should show all installed versions and mark the active one with an asterisk

#

from there you can decide which one to switch to

warm elbow
#

hello guys do you know how to make decisions in python like by using two bottoms and once you press one it like chooses that decision?

sacred yew
#

wrong channel?

#

and you need to provide more context anyways

crimson niche
#

Do you think that structs liketyping.NamedTuple, collections.namedtuple, dataclasses.dataclass are becoming extremely numerous? Do you use all of them for different situations? Or have you picked the ones you like

charred wagon
#

I never use the un-typed namedtuple.

#

It's not really that many - only 2 of them then.

#

They aren't exactly the same either since NamedTuple is basically a tuple while a dataclass is more like a standard class.

#

So a NamedTuple has all the typical expected behaviours of a tuple

peak spoke
#

namedtuple really only goes to the things I want to keep simple. Otherwise I mostly use NamedTuple in places of tuples with their static content, and dataclasses when more functionality is needed which separates them nicely

charred wagon
#

In short, yes, they are used for different situations.

crimson niche
#

I usually dont know which one to pick. I like typed NamedTuples best of those I think. Although the original namedtuple is cool because you can slam it out. But I try to type everything these dayss.

#

Im not good enough of a a programmer to not do it.

#

I picked Fluent Python back up. I started it but didnt go back to reading all of it because of how dense it can be while jumping around and being something you don't necessarily read front to back.

#

But its worth going back to.

peak spoke
#

Was looking through something and noticed that typing creates "fake" modules to namespace some names which looked a bit weird to me. Is there any reason not to use ModuleType so they're more "proper" modules instead of normal classes?

class io:
    """Wrapper namespace for IO generic classes."""

    __all__ = ['IO', 'TextIO', 'BinaryIO']
    IO = IO
    TextIO = TextIO
    BinaryIO = BinaryIO

io.__name__ = __name__ + '.io'
sys.modules[io.__name__] = io
worldly monolith
#

Can anyone suggest a good source for Machine learning project. My final year project is on lips reading from a video data per frame ...

balmy spindle
#

hey, is it at all possible to override a builtin class's attribute

#

as a joke, i want to override the builtin str.__add__ method

#

im curious if theres a way to do it with some c magic

spice pecan
#

Check out forbiddenfruit

balmy spindle
#

yeah, thanks

sacred yew
novel vector
#

Where are the source code files for the builtins like hex() of byteorder.fromhex() ? I can't find it anywhere

sacred yew
#

those are written in C, let me find the github link

#

though you'll need to follow the function call chain

#

@novel vector

novel vector
#

Thank you, looks nice

unkempt rock
#

!e We say the difference between a method and function is that a method belongs to an object. Isn't everything a method in python then?

def foo():
    pass

like the output suggests, this actually belongs to the __main__ module which is an object

import __main__
__main__.foo()

if you were to say sum is a built-in function:

import builtins
builtins.sum

it actually belongs to the builtins object

fallen slateBOT
#

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

[No output]
peak spoke
#

Methods are functions bound to objects, not just namespaced

unkempt rock
#

so

class Foo:
    @staticmethod
    def foo(): pass

foo isn't a method?

peak spoke
#

Really depends on the context, but from the functional point of view it isn't

swift imp
#

Static methods are still methods

#

By the data model

odd summit
#

i cant pip command as pip install pyttsx3

undone hare
#

Hey @odd summit, this channel is for discussions related to the language itself. For Python questions, can you look at #❓|how-to-get-help ?

wide shuttle
#

I guess that, at that point, it's just a semantic discussion about what the word "method" means. The intention here is to use it as a method, but one for which the descriptor protocol doesn't insert the instance as the first argument if you access it as a instance attribute.

supple lagoon
#

And I guess you could say that all methods are functions, but not all functions are methods.

Sebastiaan, I didn't want to ping you on this because I'm sure mods+ get tons of pins, but do you have any leads/resources on where you learned to build your GitHub Actions, for example the Sir Lancebot workflows?

Your caching and pull request annotations are interesting, but I can't find a resource for learning such things.

flat gazelle
#

well, all methods are callable. A staticmethod isn't a function.

wide shuttle
supple lagoon
#

Ah, I see your branch for test-ci-linting now. Do you squash commits to keep your experiments cleaner, even in a feature branch? Or you use a local workflow runner (like below) to test without having to push every time?

Thanks for your time.

https://github.com/nektos/act

GitHub

Run your GitHub Actions locally 🚀. Contribute to nektos/act development by creating an account on GitHub.

unkempt rock
#

can i get early supporter badge now

boreal umbra
#

The way methods are in python is pretty similar to the idea of partials in functional languages, yes?

#

Speaking of, I don't really get the point of functools.partial. is there a reason that it's better than just wrapping one function call in another?

peak spoke
#

Nicer interface that you can use inline without a lambda mess, and I believe the calls are also a bit faster

flat gazelle
#

Also passes by value rather than by name, which is often desirable

unkempt rock
#
class X:
    @syayoce
    def Y(): pass
#

wait how do i test code on here

feral cedar
#

!e

fallen slateBOT
#
Command Help

!eval [code]
Can also use: e

*Run Python code and get the results.

This command supports multiple lines of code, including code wrapped inside a formatted code
block. Code can be re-evaluated by editing the original message within 10 seconds and
clicking the reaction that subsequently appears.

We've done our best to make this sandboxed, but do let us know if you manage to find an
issue with it!*

unkempt rock
#

thank you

fallen slateBOT
#

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

001 | Traceback (most recent call last):
002 |   File "<string>", line 1, in <module>
003 |   File "<string>", line 2, in X
004 | NameError: name 'syayoce' is not defined
#

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

[No output]
#

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

XD
pliant tusk
#

#bot-commands

unkempt rock
#

oh im sorry

pliant tusk
#

its all good

hardy apex
#

Anyone know how python obtains it’s hashed values in dictionary to mapped the key to the value

minor needle
#

iirc different types are hashed differently

gloomy rain
#

For custom types, the __hash__() method is called, which defaults to using the object ID, I think? But you can override it.

#

Like @minor needle said, different types use different hashing algorithms.

boreal umbra
gloomy rain
#

Yep

eager trail
#

Hey guys, I'm trying to get a qol change for the cloud trail detectives in my company into the code base.

Does anyone know of a way to get qualname in the base class to take on the name of the wrapper without modifying the wrapper file itself? Because either I change one or two files in the reference library or every single function we have (and there are 300 of them)

#

Changing the files is trivial, but I'll get a lot less pushback if I can do it the former way

neon palm
#

Guys, do you know if Parallel + delayed from joblib copy the objects I pass through the function?

sacred yew
#

@eager trail can you provide some sample code so we can visualize what you mean?

eager trail
#

Sure, will do when I'm home

eager trail
#

okay so, the functions are generally in this state

*\reference_lib\botohelper.py
import boto3
class Rule:

other methods

def sessioncredentials(role):
sessionclient=boto3.client('xxx')
assume_role_response = sessionclient.assume_role(role, RoleSessionName='ConfigLambdaExecution')

other methods

*\lambda\AWS-AAAEC2.py
from reference_lib import Rule
class AWS-AAAEC2(Rule):

methods drawn from Rule

What I would like to be able to do is somehow, when calling the sessioncredentials method of Rule via AWS-AAAEC2, pass qualname (or something similar) to RoleSessionName so that when my guys are doing stuff in cloudtrail, there isn't just one giant mass of ConfigLambdaExecution sessions.
For example, it could instead be ConfigLambdaExecution-AWS-AAAEC2 which would be much more descriptive

Changing each file automatically with a simple script would be trivial. The problem would be the amount of resistance I get to making a push that changes every single lambda function we have. If there was a way to change only botohelper to get the wrapper to somehow pass either the class name or the file name as a string to rolesessionname, it would be amazing

eager trail
#

Perhaps there's something in functools, but I'm not sure

fathom ginkgo
#

posting here because it might require specific bottle knowledge:
Why does this work? How does API.py know that there is an app from main.py ? magic?

main.py:
------------------- 
from bottle import default_app
import API


app = default_app()

API.py:
------------------- 
from bottle import route
@route("/", method=['GET'])

def index():
    return "hello world"
flat gazelle
#

bottle probably has a global variable internally

fathom ginkgo
#

to me it looks bad because it looks like im importing API but not using it.
isnt that un-pythonic or something?

#

linter also complains : (

flat gazelle
#

The import is probably just to make that module run. It is not idiomatic, but frameworks are allowed to try weird things like this. You can use #noqa to silence the linter

fathom ginkgo
#

i see. I'll try that. hmm just one more question though.
any idea on what's the idiomatic way to have that kind of separation of concerns in bottle? (by separation of concerns, I mean separating the routes in different files, per use-case)

flat gazelle
#

That is probably idiomatic in bottle, unless they state otherwise in some tutorial. Sometimes idiomatic frameworks aren't idiomatic python, and that's fine

fathom ginkgo
magic python
#
def f(s):
    # s is a string
    # _some_ name for the sake of this example
    f_name = 'module_' + len(s) + ".py"
    with open(f_name, 'w+') as fh:
        fh.writelines(s)
    return f_name

can this function return before the file is written ?

spark magnet
#

@magic python it depends what you mean by "written". The file will definitely get the lines written to it.

#

@magic python but I wonder about writing Python files

magic python
#

@spark magnet originally this is from wanting to test that a bunch of ipython notebooks run without exception - I want to convert the notebooks to python modules and run those (so that I don't have to worry about connecting to a kernel etc, I'm aware of papermill etc to run notebooks with though).

By written, i just meant that the file would be completely written prior to the function returning

#

so that

py_module = f(s)
do_something(py_module)

the module would certainly exist for do_something

spark magnet
magic python
#

@spark magnet hm ok - originally i was just running the notebooks, but this caused errors with Pytest (something something unix socket), so I'm looking at just using python modules instead. But whilst doing so I started getting paranoid about whether the io would definitely be completed before teh function was returned

spark magnet
#

I understand, but it seems like it's ok

magic python
#

@spark magnet ok thanks 🙂

magic python
#

@spark magnet sorry to ping, but i'm trying to run these modules (converted from notebooks), and although I've stripped magic I seem to be having difficulty with exec( python_source ), although when I save the source to a file with

with open('check.py', 'w') as fh: fh.write(python_source) # here python_source is extracted from nb

and run with

python check.py

it works, I'm wondering if you have any reading suggestions to get this working... or if there's something with exec that's known to throw this sort of thing? The error is usually module not found (though it's imported in the code).

sacred yew
eager trail
#

Okay, I'll give that a try, thanks

#

Oh wait nuts it's the former

unkempt rock
#

Good Evening to all member

#

I am really surprised by one behaviour in pycharm
I have installed some python package (GDAL) in Global env
now I want to use that package in pycharm too without reinstallation, so I have edited pyvenv.cfg in pycharm
include-system-site-packages = True
Now when I do for first time import gdal (its giving some weired error)
but at second time. It is able to import, when I do import gdal
any specific reason, for first time giving error and for second time no error

fickle osprey
#

Hello. After spending all day finding errors i realized I made a blunder by using different variables as inputs and labels for training the data which is why i keep getting errors for when im training the data. Now it would be a straightforward thing to debug it, however im really new to python. therefore its confusing me big time about how i should go about it. i was trying to use different approaches to it which other people suggested me

fickle osprey
sacred yew
# eager trail Okay, I'll give that a try, thanks

hmmm, so its basically a classmethod thats undecorated
what about

@classmethod
def sessioncredentials(cls, role):
    sessionclient=boto3.client('xxx')
    assume_role_response = sessionclient.assume_role(role, RoleSessionName='ConfigLambdaExecution-' + cls.__name__)
eager trail
eager trail
#

You boys ever join a new place, start digging around in the code base, and find lots of horrific things

delicate sage
#

in python 3.8

>>> def a(c:b, e:('f', "e")):
    pass

>>> a.__annotations__
{'c': <class '__main__.b'>, 'e': ('f', 'e')}

but in python 3.10 due to the postponed evaluation of annotations
it becomes

>>> def a(c:b, e:('f', "e")):
    pass

>>> a.__annotations__
{'c': 'b', 'e': "('f', 'e')"}

before b was a class in annotations, now its a string
how to achieve the same result without eval?

eager trail
#

Presumably you want the 3.10 result yeah

delicate sage
mortal wasp
#

can anyone help me with my svm model

peak spoke
#

get_type_hints will resolve them against the globals of the func/ the ones you pass it. But that will fail because they're not typehints so you'll need eval it yourself

prime estuary
#

For this reason inspect.get_annotations is currently being PR-ed in, to handle non-typing annotation uses.

prime estuary
prime estuary
magic python
peak spoke
#

A file like object is just something that implements the same interface as your regular file obj (read, write, etc.), from the name I'd guess _TemporaryFileWrapper does do that

magic python
#

hrm - i passed in named temp file and got a fail, whereas passing a file obj from with open(...) as file_obj worked, which made me wonder

#
            # does not work
            with tempfile.NamedTemporaryFile(prefix="something.log") as std_out:
            # does work
            # with open("process.txt", "w") as std_out:

basically , followed by with contextlib.redirect_stdout(std_out). So i wasn't sure what to inspect etc to determine why

eager trail
#

Anyone know of the top of their head whether set intersection or list comprehension is cheaper memory wise if checking whether two lists share any item, and returning True if so

spark magnet
eager trail
#

Yeah, that's most of what I got, but I'm trying to optimise for memory over speed and not in front of my computer rn

#

Not knowing is driving me crazy haha

spark magnet
#

the list will be smaller

eager trail
#

Thanks ned, legend

spark magnet
#

@eager trail if you don't mind me asking, why focus on memory? Are you running out?

peak spoke
#

Don't know about the ops but the set itself will be considerably larger

eager trail
peak spoke
#

But there is a good chance that even if you split it up into multiple smaller operations it will be quite a bit faster while limiting its memory use

spark magnet
#

how large are these lists?

eager trail
#

Less than 30 items but there are a heckuva lot of them

unkempt rock
#

Hey, if im doing a cli for a list program, where you add rows to a database, and the row in question has 9 columns what is the correct way to do a command for adding that? Should i use keyword arguments or just "add A B C D E ... etc "?

#

Or, another approach i could follow is to separate this add command in two, as some of the columns reflect state, i could simplify the command to 5 fields, and the other 4 could set to a default until set by other commands

ashen monolith
#

If there are sensible defaults for value then make these optional arguments

radiant fulcrum
#

Is there a method / a not esoteric method of removing a parameter from a function?

#

e.g.

#
def foo(user):
  ...

import inspect
inspect.signature(foo)
#

will give ('user',)

#

is there a way to hide this pepehmm

spark magnet
#

why do you want to hide it?

radiant fulcrum
#

Making a function decorator that covers alot of boiler plate for FastAPI

#

but fastAPI takes all args other than request as a parameter in a url query

undone hare
#

Sounds like you could have a wrapper taking no parameter in but what are you actually going to call the function with?

radiant fulcrum
#

atm im using wraps

#

so that way it doesnt break the router etc...

#

but the downside is it copies over the wrapped functions sig

#

current setup basically doesnt allows this:

class Foo(router.Blueprint):
  @router.endpoint(
        "/ahhh",
        endpoint_name="ahhh",
    )
    @enforce_auth(REQUIRE_USER_SESSION)
    async def ahhh(self, req: Request, user: User):
        ...
#

fastAPI will reject the request because it doesnt get given a url query called user

#
class Foo(router.Blueprint):
  @router.endpoint(
        "/ahhh",
        endpoint_name="ahhh",
    )
    @enforce_auth(REQUIRE_USER_SESSION)
    async def ahhh(self, req: Request):
        ...

would be valid

kind helm
#

Guys

#

Can somebody help me?

fallen slateBOT
#

Hey @kind helm! I noticed you posted a seemingly valid Discord API token in your message and have removed your message. This means that your token has been compromised. Please change your token immediately at: https://discordapp.com/developers/applications/me

Feel free to re-post it with the token removed. If you believe this was a mistake, please let us know!

#

Hey @kind helm! I noticed you posted a seemingly valid Discord API token in your message and have removed your message. This means that your token has been compromised. Please change your token immediately at: https://discordapp.com/developers/applications/me

Feel free to re-post it with the token removed. If you believe this was a mistake, please let us know!

sacred yew
timid briar
#

HI. I'm creating a python IDE and I want to know your feedback.

timid briar
raven ridge
timid briar
#

Thank you

unkempt rock
#

@clever escarp

#

wrong person

boreal umbra
#

@unkempt rock If you post any additional advertisements, you will be removed from the server.

grave jolt
#

Why do the Python docs sometimes refer to classes like float, str, range etc. as functions?

raven ridge
#

The only place I'm aware of where it really does that is in the https://docs.python.org/3/library/functions.html#built-in-functions section of the docs - which does say "Built-in Functions", but also says in the prose:

The Python interpreter has a number of functions and types built into it that are always available.
which acknowledges that some are types and some are functions.

#

I guess they probably do it to make that section more approachable to beginners.

eager trail
spark magnet
#

@eager trail i hope you don't need more memory than that

eager trail
#

Yeah, I won't, so I'm playing around with either sets or generators and seeing which is faster

undone hare
feral mist
#

Hi

prime estuary
#

In older Python 2 versions before new-style classes were a thing, they were actually just functions that constructed the types. It could be old language that wasn't fixed. Though for new users it's good to have them listed among functions since you can treat them that way if you don't care about classes.

On the other hand things like map and filter are deliberately defined as functions, CPython as an implementation detail happens to define them as classes but other interpreters may use generators or whatever they want.

iron perch
#

How does python store classes data? thinkmon

flat gazelle
#

user defined classes have a dict on them which stores their attributes, you can use __slots__ to make it not fully dynamic. Some builtin classes are just structs

native bridge
#

Can someone help me with python code for AI face recogniser

boreal umbra
delicate sage
#

Hmm shudn't it be a string?

#

3.10.0a7+ (heads/master:09aa6f914d, Apr 25 2021, 20:10:20) [MSC v.1916 64 bit (AMD64)]

#

oooh

#

is this new?

peak spoke
#

Yes, some lib maintainers asked for more time with the feature

quiet crane
#

@peak spoke Could you expand on that a bit? Will the annotations for a class like that b result in something different in the future?

peak spoke
# quiet crane <@!184351770636582913> Could you expand on that a bit? Will the annotations for ...

PEP 563 makes all annotations forward refs instead of being evaluated at definition time as is done currently they'll only store a string version of the expression which needs to be evaluated at a later time to use its value (you can currently enable this with from __future__ import annotations on a per module basis). But evaluating like that comes with some limitations and it broke libraries that use annotations for non typing purposes so their maintainers asked to delay the feature becoming a default https://mail.python.org/archives/list/python-dev@python.org/thread/CLVXXPQ2T2LQ5MP2Y53VVQFCXYWQJHKZ/.
There are some proposals for how to handle the deferred evaluation like PEP 649 that may make it easier to convert with the new behavior in mind

quiet crane
#

Thanks

bitter star
#

In python if you get a square root of an integer it will always give a float.
IF the integer is a perfect square the you will always get the float ending with .0
if we check if the output has .0 in its end then it is a perfect square else it's not
so basically you can cheat your way to find if a num is a perfect square or not, without math logic.

deft pagoda
radiant fulcrum
#

your logic would be fine, if it wasnt floating point as well

bitter star
#

ok i will just check the last two

#

digits

#

solution:

  1. last two char is '.0'
  2. len of chars after '.' if one
#
    def is_square(self, num):

        ans = num**.5
        str_ans = str(ans)[-2:]

        if str_ans  == '.0':
            return True, int(ans)
        else:
            return False
deft pagoda
#

i'm not sure why this is in this room either

bitter star
#

you see it sees the last two chars.
if they are .0

deft pagoda
#

did i miss an earlier discussion about floating point

spice pecan
#

!d float.is_integer

fallen slateBOT
#

float.is_integer()```
Return `True` if the float instance is finite with integral value, and `False` otherwise:

```py
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
flat gazelle
#

how about

(16 ** .5).is_integer()
#

oh, jinx

bitter star
#

32.01562118716424
this num does not end with .0

flat gazelle
#

yeah, this doesn't really fit here

bitter star
#

it ends with 24

bitter star
deft pagoda
#

Discussion on the use cases, implementation and future of the Python programming language including PEPs, advanced language concepts, new releases, the standard library, and the overall design of the language.

flat gazelle
radiant fulcrum
bitter star
#
if str(num**.5)[-2:] == '.0':
  return True
radiant fulcrum
#

doesnt work for 152342352536265664 sadly in my impl

modern night
radiant fulcrum
bitter star
magic python
#

The generated bytecode reflects this decision and will try to fetch b from the local environment

from Fluent Python, referring to why the following will error:

a = 4
b = 5
def f(a):
    print(a)
    print(b)
    b = 6

f(a)

How do i go about checking the bytecode though? Is this a dis thing?

unkempt rock
#

They show how they generate the bytecode in the comparing bytecode section

>>> from dis import dis 
>>> dis(f1)
magic python
#

the output of dis.dis(f) is:

  4           0 LOAD_GLOBAL              0 (print)
              2 LOAD_FAST                0 (a)
              4 CALL_FUNCTION            1
              6 POP_TOP

  5           8 LOAD_GLOBAL              0 (print)
             10 LOAD_FAST                1 (b)
             12 CALL_FUNCTION            1
             14 POP_TOP

  6          16 LOAD_CONST               1 (6)
             18 STORE_FAST               1 (b)
             20 LOAD_CONST               0 (None)
             22 RETURN_VALUE

🤔

magic python
peak spoke
#

It may, bytecode compatibility is not guaranteed between versions. With python 3.x are also more of major versions imo as 2/3 are mostly regarded as completely separate

eager trail
#

Should I be using yield instead of return when doing say api calls so each item being returned iteratively can be processed while waiting for the api cooldown (sorry for imprecise language)

brave badger
eager trail
#

Thanks! ☺️

prime estuary
# magic python Although their output is different: theirs https://i.imgur.com/D3X7unf.png mine ...

That bytecode is the same actually, looks like theirs has the wordcode update which changed the byte lengths to be the same for all instructions. The five columns dis outputs are in order:

  • the line number, for the first instruction on each line
  • the index of that opcode in the bytes object
  • the name of the instruction
  • the value of the "argument" byte
  • for some instructions, a decoding of the argument - the name it refers to, the constant, etc.

For CALL_FUNCTION, looks like their version had an update decoding the values it has. Here in the docs is a list of all the current instructions: https://docs.python.org/3/library/dis.html#python-bytecode-instructions

iron perch
#

Do you guys know ytbers who have low level python content?

visual shadow
#

You can use pycon talks for it perhaps. Eg https://youtu.be/cSSpnq362Bk

Speaker: James Bennett

At some point every Python programmer sees Python bytecode files -- they're those '.pyc' files Python likes to leave behind after it runs. But have you ever wondered what's really going on in those files? Well, wonder no more! In this talk you'll learn what Python bytecode is and how it's used to execute your code, as wel...

▶ Play video
unkempt rock
#

Hey, what language should I learn after Python? I thought about learning JavaScript because it's used for things like web dev. Also thought about C or C++ because it's a low-level language and to get a job in programming I would want/need to learn a low-level lang, and it's also used for making games which I would really want to do. Thanks for any answers 😊

lament sinew
#

haskell

visual shadow
#

Ultimately only you can pick for yourself. There's benefits to a lot of languages as you're yourself indicating

kind helm
#

Who like Java ?

#

Fuck Java all my homies hate Java

halcyon trail
#

C++ is a good practical choice, it's quite the mission to learn it well though.

#

I've learned some Kotlin in my spare time, and it's definitely made me want many things from it in python

#

Kotlin/Swift are probably the two languages that are aimed at the mainstream, while integrating a very broad spectrum of things we've learned are good ideas from a variety of sources, so I think they are great languages to learn.

feral cedar
#

swift is pretty much only used for ios dev.

boreal umbra
#

The study guide for my programming languages exam says that Python is pass-by-value, but I'm pretty sure that's only true for immutable types

#

Unless I'm misunderstanding what is meant by pass-by-{value, reference}

flat gazelle
#

neither pass by value nor pass by reference exactly fit python

#

I have seen pass by value reference used before

undone hare
halcyon trail
#

nice lambdas

#

extension functions

#

null awareness

#

more expression oriented rather than statement oriented

sacred yew
#

extension funcs seem unpythonic

halcyon trail
#

not sure why really

#

And I'm not sure if it makes sense to talk about a feature being pythonic really when we are comparing two languages; python's made its decisions about what's pythonic or not obviously, the question is how well those decisions hold up

#

braces are unpythonic as well but it seems to be one of the main reasons why python can't have a nice syntax for lambdas, like kotlin/swift do

#

Re swift, I agree it's only really used for ios and perhaps other apple targets, so I doubt I'll ever use it. That said the language itself is really quite nice. Kotlin is nice but marred significantly in a few places, they needed to make a couple of major compromises for Java interop. Swift didn't have that problem.

lament sinew
#

I'd prefer the uniform functional call syntax from D.

sacred yew
#

re: ext funcs
adding methods that are in separate files and subject to import order seems bug prone

lament sinew
#

i.e. every function can be called f(a, b) == a.f(b)

sacred yew
#

re: lambdas
you could just use something like js/java syntax, eg (a,b)=>a+b

lament sinew
#

but that'd fuck up python's name resolution

halcyon trail
#

UFCS was discussed a lot in C++ but didn't make headway. Having seen that discussion, and used Kotlin, I think extension functions are simpler and achieve the important things.

#

I'd rather have UFCS than nothing, but comparing UFCS and extensions head on, the delta in utility isn't worth the delta in complexity. Just my 0.02.

#

The problem for lambdas is if you want multi-line lambdas you need to know where it ends

lament sinew
#

I'd be very agains multiline lambdas in python.

halcyon trail
#

🤷‍♂️

lament sinew
#

It's like asking for callback hell.

halcyon trail
#

@sacred yew I'm not sure why it's more bug prone than anything else, you'd probably need to import it directly to use it. It's not really different than importing the exact same function twice

#

@lament sinew empirically that doesn't really happen though in languages with lambdas like that? callback hell is also something pretty specific where the nesting level increases

#

callback hell has more to do with whether you have a way for chaining asynchronous operations without increasing nesting

lament sinew
#

Happens in languages that more or less belong to the same niche as python, i.e. JS, Ruby (to a lesser extent but still).

halcyon trail
#

It's famous for happening in JS because of how async programming was handled in JS for a long time.

#

Ruby uses blocks extensively for how it handles collections (as opposed to python's comprehensions approach), and in that context, callback hell isn't really a thing AFAIK

lament sinew
#

I guess i misused the term callback hell, i meant defining-big-functions-as-function-arguments

#

adds nothing to the language but confusion

halcyon trail
#

it seems like an extremely arbitrary limitation that lambdas are ok, but only if they are exactly one expression

#

It's not the end of the world and I like python despite it but I have trouble understanding why it's a good thing

lament sinew
#

What is to gain with multiline lambdas? One liners are useful because a good chunk of the time all you need is a closure or some quick operation for sorting keys or whatever.

#

multiline would invariably result in complex anonymous functions being used

halcyon trail
#

when you say "what is to gain", you are saying, compared to defining a local function?

#

What exactly are you comparing to?

lament sinew
#

can't compare to anything else

sacred yew
#

can you give any instances where a multiline lambda would be useful compared to a normal function definition?

undone hare
#

The thing with multi-line lambdas is that they cause a huge semantic issue

#

There are two type of nodes you can have in Python, more or less

#

Expression and statements

#

Expression have a value, while statement do an operation

halcyon trail
#

@sacred yew i think in practice people just tend to not end up defining a local function in a lot of cases

#

like, they won't define a local function and do a list comprehension with that function

#

they'll instead create an empty list, a for loop, and call append repeatedly

undone hare
#

The whole syntax is built to make sure that you never try to use an expression where you should be having a statement and vise versa (like my_variable = def function(...))

halcyon trail
#

in most modern languages you would just call map and pass it whatever lambda you wanted do

undone hare
#

Lambdas are expression, that's it

#

But a function is a statement (because it has many lines)

halcyon trail
#

that has nothing to do with why a lambda can only be one expression

#

the lambda could contain many lines internally, but the lambda node in the AST is still an expression

undone hare
#

But a like in a function is a statement, right ?

halcyon trail
#

anyhow, more broadly, yes, mult-line lambdas may not play nice with other aspects of python, which is why I said it was "unpythonic", but that's why I didn't frame things that way. I framed it in terms of things I miss when I'm writing python.

#

a like?

undone hare
#

I'd give anything to swap patma for better null awareness tbh

halcyon trail
#

patma

#

?

sacred yew
#

pattern matching

undone hare
#

I know a lot of people don't like me anymore

halcyon trail
#

oh

#

yeah, pattern matching in python seems a bit odd to me, I'll suspend judgement until I use it

#

in statically typed languages pattern matching is a lot more than sytnactic sugar

#

because the types are also cast appropriately inside the blocks

#

in python, I dunno, it seems like it may not be that big of an update over if elif, in the vast majority of cases

#

but maybe I'm wrong

#

@sacred yew also btw re lambdas, even the standardization docs for walrus operator, show examples where they basically existing python solutions to things if they involve lambdas or locally defined functions

#

in particular, two argument iter solves one of the use cases for walrus operator, but almost nobody seems to use two argument iter in python

#

because in many/most cases you need to feed it a lambda/local function, and in practice this seems to be ugly enough that in most cases people actually write out the "loop and a half" repetition instead

lament sinew
#

I don't think null is fixable in python. it's just a pain you have to learn to deal with.

#

Something I miss from Rust/Haskell in python is algebraic data types. That's doable and would be play nicely with the new pattern matching.

halcyon trail
#

python's not statically typed, so what would that actually mean

#

You can already annotate things with Union[int, str] or what not and I'm sure in short order it will play nicely with pattern matching

#

I guess you could have a type annotation that also provides names to each of the states, as well as types

#

something like ADT[foo=int, bar=str]

boreal umbra
#

Any reason why slice is a type, but you can't make a slice literal outside of syntactically valid {(class_)?get,set,del}_item calls?

lament sinew
#

You can do Slice(1,10,2)

peak spoke
#

Not much reason for it to have a literal apart from that syntax

#

There are other builtins like bytearray which are probably used more overall but don't have literal syntax

fallen slateBOT
#

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

[2, 3]
halcyon trail
#

ah that's a cool trick

#

the !e i mean

visual shadow
# boreal umbra The study guide for my programming languages exam says that Python is pass-by-va...

If you want a confusion free explanation that fits the python universe, python is "pass by assignment". Ie. Function args follow the same semantics as if they were assigned. Simple. Elegant. Concepts that shouldn't be exposed in python stay at the lower levels of abstraction. (ie pass by value pass by reference should only be introduced when talking about languages that understand pointers vs non pointers.)

#

If you want to peel away the layer of abstraction and talk about it, well pass by value is correct. Python would be pass by value, with the caveat that all variables in python are 1st level pointers. However I would encourage not using this terminology at all, it just adds to confusion

#

I feel like deja vu almost, but could someone explain what better null awareness is/does?

flat gazelle
#

essentially, syntax level support for Optional[T], so you have operators like

obj?.attr
obj?[0]
obj ?: default
``` which do the regular thing if the obj is not None, and just result in None otherwise
`?:` is  like `or`, but only None is considered "Falsey"
#

personally, I would rather have some form of excepttry so that you can use exceptions without nesting things extremely far

visual shadow
#

Ah! I see. Thanks

quaint willow
#

Anyone here from canada ?

visual shadow
#

Please keep the posts in this channel on topic. Take a look at the channel description

halcyon trail
#

every once in a while I run across code that has to propagate optionals all over the place and current in python it's just so long winded

#

even those null aware operators aren't a full solution but they would be a start

#

Ideally i'd like something that allows you to call an arbitrary function on a python variable that might be null; if it's non-null give you the result of the function, otherwise just give you back the null

#

e.g. I have a Path, which was an optional argument to my function, to do some kind of check against a reference. So now I want to form reference_file = prefix_dir / something_else, but only if prefix_dir is non-null; otherwise I Just want reference_file to be null

#

the previous programmer actually factored out a lot of this logic into functions, and the function takes the prefix dir as an Optional[Path] and returns another Optional[Path]. But now the logic is embedded in there along with the optionality, so now even when you definitely have the prefix_dir, in some other context, you still get an optional back, and now you have to assert that it's not None, which is very silly

#

In Kotlin, because you have both extensions and nice lambdas, you can do val referenceFile = prefix_dir?.let { it / something_else }, which is quite nice, even though Kotlin still only has basically the same null-aware operators as are proposed for python. Not sure how you could make this work nicely in python, even with null aware operators.

#

I guess via ?.__div__ but eww 🙂

timber scroll
grave jolt
weak copper
#

how to filter date

eager trail
#

Pretty sure that's not the right datetime format

#

Take this to the data science channel

iron perch
#

How does assigning a dict's value to a variable better than accessing it everytime ? thinkmon

#

Low level stuff

digital igloo
raven ridge
#

There is some cost to looking up a value by key, so if you need something to be fast it makes sense to only look it to once and reuse it once you find it

pliant tusk
# iron perch How does assigning a dict's value to a variable better than accessing it everyt...

dict_object[key] involves an extra set of opcodes (it has to load the dict and the key onto the stack). the BINARY_SUBSCR opcode then calls PyObject_GetItem which is the generic __getitem__ C function which eventually calls PyDictType.tp_as_mapping.mp_item with the arguments obj and key. the alternative (loading a variable) is much faster. If the variable is a local, it is a simple C array index (fastest). If it is a global, a dict lookup still occurs, but without the Indirection of PyObject_GetItem