#internals-and-peps
1 messages Β· Page 70 of 1
and if it's inside a function, it's hopefully easy to see than a variable is not mutated π
and if it's inside a function, it's hopefully easy to see than a variable is not mutated π
@grave jolt yeah, that's what I was thinking
because IIRC Final doesn't work for local variables?
and it would be nice to have something that a 3rd-party checker could pick up
inside a function could allow more optimizations too
inside a function could allow more optimizations too
@feral bane it could, but it would also be significantly more work for the core team, I guess
I mean like:
const x = 1 x = 2 # SyntaxError
@gleaming rover how's it interact with nonlocal? With globals()? With exec?
nonlocal shouldn't be a problem
lookup for nonlocal isn't dynamic, it knows when it's compiled what function the variable is from
i do think it's untenable to try to do this for scopes that aren't functions
though you could conceivably make a descriptor that does it for instance variables
class InitOnlyVar:
def __init__(self):
self.default = default
def __get__(self, obj, objtype=None):
return obj.__dict__[self.name]
def __set__(self, obj, val):
if self.name in obj.__dict__:
raise AttributeError("readonly attribute")
obj.__dict__[self.name] = val
def __set_name__(self):
self.name = value
class InitOnlySlotVar:
def __init__(self, name, descriptor):
self.descriptor = descriptor
self.name = name
def __get__(self, obj, objtype=None):
return self.descriptor.__get__(self, obj, objtype)
def __set__(self, obj, val):
try:
self.descriptor.__get__(self, obj)
except AttributeError: #good
self.descriptor.__set__(obj, val)
else:
raise AttributeError("readonly attribute")
def setup_initonly_slots(cls):
# called after class definition
slots = cls.__initonly_slots__
if isinstance(slots, str): slots = (slots,)
for name in slots:
desc = InitOnlySlotVar(name, getattr(cls, name))
setattr(cls, name, desc)
to use the regular one just use varname = InitOnlyVar(), and it creates a descriptor that can only be assigned once,
to use the slot one, define __slots__ as normal, __initonly_slots__ as the ones you want to have this behavior, and then call setup_initonly_slots(Class) after the class is defined
@gleaming rover how's it interact with
nonlocal? Withglobals()? Withexec?
@raven ridge you mean at runtime?
honestly I forgot about Final
with how breakable python is, having static analysis tools send a nice f-u message to people abusing its """"features"""", or straight up pretending they don't exist is the best we can do
Sometimes I wonder what dynamic typing really brings to the language
I can't really think of anything
Other than saving you time from typing things out
Other than saving you time from typing things out
@pseudo cradle type inference helps with that
because there isn't anything, programs generally aren't even designed in a way that takes advantage of dynamic typing
it looks nice
I'd say the main advantage of dynamic typing is letting you write programs that "almost work" quickly
i guess if people are fine with that 
It's also the main disadvantage, tbh
i guess if people are fine with that :shrug:
@teal yacht throwaway scripts + small programs I guess
i think "almost working" is worse than "never working" in a crushing majority of case tho
Like, even if you just really really need dynamic typing that's what unions are for?
Like, even if you just really really need dynamic typing that's what unions are for?
@pseudo cradle that's not what unions are for IMO
you mean like sum types?
Like, if you're trying to declare the return type of a function and you don't know if it's going to be a list or a bool
(Was an example I saw on SO)
i just wish global type inference was standard and we'd just never write types anymore
i swear python code has more types written than f#
i just wish global type inference was standard and we'd just never write types anymore
@teal yacht isn't it undecidable in the general case
depends on the type system, but yeah in common oop-like type systems, it's not decidable
@raven ridge you mean at runtime?
@gleaming rover yeah.
@gleaming rover yeah.
@raven ridge I was thinking a compile-time check, and then, yes, I rememberedFinal
although on that topic the equivalent of descriptors for modules would be nice too
my point is that a compile time check isn't sufficient - Python is such a dynamic language that there's no way a compile-time check will do the trick.
it can't possibly prevent something like:
def func(stmt):
const x = 0
eval(stmt)
print(x)
func("x = 5")```
no
I was envisioning something like type hints
i.e. no runtime enforcement by design
which is why I mentioned Final
ah. π€·
I mean, it'd give linting one extra mistake it could catch. But if we're talking about language changes that would make linting more effective, I don't think that's the most effective one...
I mean, it'd give linting one extra mistake it could catch. But if we're talking about language changes that would make linting more effective, I don't think that's the most effective one...
@raven ridge probably not? but it's something that's personally relevant to me because I more or less never reassign
some of the suggestions above would basically just be a different language. "Python, except with static typing", even with perfect type inference, would just be an entirely different language than Python.
some of the suggestions above would basically just be a different language. "Python, except with static typing", even with perfect type inference, would just be an entirely different language than Python.
@raven ridge did anyone suggest Python but statically typed?
Sometimes I wonder what dynamic typing really brings to the language
@pseudo cradle I mean, pretty much π
fair enough
mypy with --disallow-untyped-functions would be statically typed python
I'm going through and type hinting everything anyway
mypy is... not good.
Or at least making a half-hearted attempt to and giving up
I literally can't use mypy in a medium sized project without finding a bug, or limitation, that makes me need to go through ridiculous contrivances to work around it.
I literally can't use
mypyin a medium sized project without finding a bug, or limitation, that makes me need to go through ridiculous contrivances to work around it.
@raven ridge indeed.
i wouldn't call it good either, but it's orders of magnitude better than nothing imo
also having to use untyped libraries is just π¦
and there are common Python idioms that mypy just throws up it's hands about.
honestly i find it unusable atm because nobody uses it, it's a terrible vicious circle
_NOT_SET = object()
def some_func(param=_NOT_SET):
...
Try typing this. Whoops.
I actually do use mypy.
my team had a vote, I lost π
i don't think every python code is worth existing tbh
I don't like it, and find it ridiculously frustrating. More than once I've had to rewrite code to get it into a different form that mypy could adequately describe, heh
type systems by their very definition make some programs invalid
i don't think every python code is worth existing tbh
@teal yacht fair enough, but that was a common Python idiom for a very long time.
there is tons of existing code, part of public interfaces, that cannot be described by mypy. That's not ideal.
I just find typehints get unwieldy very fast when dealing with highly variable data
Unless you just say fuck it and pass Any half the time
the goal is to find a way to express logic through the type system, minimizing the amount of buggy program that gets validated, while maximizing the amount of program that does get validated
Because constructing the actual type hint for the allowed types gets really long and convoluted
look up protocols
and learn about the standard abstract classes
these are so underused it makes me sad
the goal is to find a way to express logic through the type system, minimizing the amount of buggy program that gets validated, while maximizing the amount of program that does get validated
@teal yacht if you're writing brand new code, perhaps. But when you're trying to add annotations to existing code (that was idiomatic beforemypyexisted!) it's nothing but disappointment.
especially in a language like python, structural subtyping is a blessing
it's just so uncommon in programming in general that it's hard to make it take off
the structural subtyping stuff in mypy actually is pretty cool. π
Like this is grim imo: Dict[Union[str, int], Dict[Union[str, int], Union[str, int, None, Dict[Union[str, int], str]]]
Sphinx now reads type hints and uses it to infer types for parameters. It almost works.
I have an API that I haven't yet figured out any meaningful way to type, actually. I've just thrown up my hands and given up on it.
I've got a DB-API 2.0 compliant database API that allows the user to set a row_factory property on a Connection to control the type of result rows. So you can do conn.row_factory = namedtuple_row_factory to get rows as namedtuple instances, or conn.row_factory = dict_row_factory to get rows as keys, etc.
so the method that fetches a row from the database returns - well, any type of object at all. It's a type of object that the user configured in advance, but there's no way to describe that to mypy
That's just Any no?
I mean, maybe, but then the user is forced to cast to a type they've already configured.
I mean really what you've got there is a limitation of static-type checking when you have a dynamic type
If you are allowing the user to dynamically define the return type how can you possibly expect mypy to check that
conn.row_factory = dict_row_factory
for row in conn:
print(row.keys())
Typing that as Any won't make mypy happy. Typing it as Dict is a lie in the general case. The user needs to do something ridiculous like:
conn.row_factory = dict_row_factory
for row in cast(Dict[str, Any], conn.execute(statement)):
...
If you are allowing the user to dynamically define the return type how can you possibly expect mypy to check that
@safe hedge I mean, I agree, but I've got users that are all like "why doesn't this library have type hints blah blah blah"
I mean really what you've got there is a limitation of static-type checking when you have a dynamic type
@safe hedge I'm pretty sure there's a way to type this in a sufficiently powerful static type system
and the answer is that, given the constraints of mypy, it cannot.
because each factory knows its own type
But isn't the user providing the factory?
Not just picking from a few available ones?
as long as the factory is typed, it shouldn't matter
like I'm not saying that mypy can do it
but the equivalent pattern in a statically typed language should be doable
Not just picking from a few available ones?
@safe hedge technically, yes, though the library provides several, and in practice I've never seen anyone provide one that didn't come from the library.
How much of statically typed language do you want to incorporate before you just say fuck it and make python fully statically typed
At some point you accept the trade-offs no?
I mean, I don't have a problem with dynamic typing. I find mypy annoying π
I use typehints in my own code but mainly so I remember how I intended to use the functions tbh
I don't even use a static type checker
I don't even use a static type checker
@safe hedge do PyCharm's checks count
Well I use an editor not an ide..
I generally don't write application code, I generally write library code. So, users ask for type hints because they want to type their applications, and - well, sometimes I can provide them,, I guess.
not when they want to mypy --strict their code.
Yeah but that's on them
Sometimes I'm so glad the majority of code I write I am also the user
this is actually open sourced code I'm complaining about - it's https://bloomberg.github.io/python-comdb2/dbapi2.html#comdb2.dbapi2.Cursor.fetchone
huh
@raven ridge Hmm, that's basically like DictCursor from pymysql right?
Does that have typehints?
don't know pymysql, but that sounds like it's probably similar.
@fast hamlet this isn't relevant to anything here.
yep. The idea of making the row type configurable is pretty common across DB-API interfaces.
Unless you're pyodbc which just leaves it up to you
and I expect none of them have figured out any way to type this, because as far as I've been able to tell mypy is insufficiently expressive to handle it. π
Although I presume that in reality all implementations are just wrappers around the standard return type of a list
Like the pyodbc recommended method to get dicts is:
in some sense - the database returns one particular type of thing, at the level of the wire protocol, and then the DB-API interface is responsible for massaging that into Python types for the user to consume.
columns = [c[0] for c in cursor.description]
res = [dict(zip(columns, r)) for r in cursor]
Also it's not strictly part of the DBAPI spec: https://www.python.org/dev/peps/pep-0249/#frequently-asked-questions
yep, very true. The spec only requires a tuplesequence of column values.
I think psycopg2 is my favourite python db api
I'm always frustrated using others that don't seem as well featured
I haven't used it. Any particular extensions that stand out?
psycopg2.sql is really useful for constructing arbitrary statements with escaped values
Effectively allows you to safely parameterise table names for example
I think it handles bulk inserts reasonably well too
It doesn't do much to any filtering of formatting
It only does very very basic escapes for table names because at the end of the day it's still string formatting
Not to mention the confusing margin it adds when trying to tell people to not format queries like that as they go βbut psycopg2 formats it like this"
As for bulk inserts than that's a meme, with executemany being slower than just a for loop of executions which is 
the emphasis on portability doesn't make much sense to me, SQL portability is kind of a joke anyway. not to mention that DBAPI implementations are also different that, even if the queries were the same, you would still often have to rewrite code if for some reason you switched databases in your application
The row factory approach is pretty reasonable though
Not hard to type annotate either now that we have protocols. But obviously you can't annotate the result from a dynamic query that's generated at runtime
How do other typed languages handle that?
I know "how does it work in Haskell" is a meme here but how does "arbitrarily typed IO" work in haskell?
JSON, SQL, etc. which can return data of somewhat arbitrary types
you just expect a certain inferred type and error/return Nothing if it does not fit
haskell read works like that
hs>(read "4.0"):: Int
*** Exception: Prelude.read: no parse
hs>(read "4.0"):: Float
4.0
I see
So, something like that isn't really possible with static type checking
Right?
Elm has a slightly different system, where you essentially describe what structure you expect and it either fits and you get Some t, and Nothing otherwise. This pretty much allows you to do everything dynamic typing can. In haskell, you can afaik use optics for similar results.
if you mean statically typed SQL queries, there is postgres-typed, which can check whether all the types match based on schema, but IDK if that is what you mean
row: Tuple[int, float, float] = cursor.fetchone()
There's basically no way to check this
SQL is not turing complete, so it should be possible in at least some cases
postgres-typed can for queries known at compile time
once you get to dependent typing though, it gets somewhat messier
Right, but in general you need to annotate the results of a query not for checking the results of the query but for making sure that the rest of the program knows what results you expect
Whereas in one of these typed languages the types you expect from the query actually determine runtime behavior
So even in Haskell you can't really check the types of an expression like that until runtime
(Unless you parse the SQL code itself)
ye, haskell cannot leave typechecking until runtime, as there is no runtime representation of types
so you have to describe your schema somewhere
what's happening here:
In [4]: def test(cls):
...: def something(cls):
...: print(dir(cls))
...: cls.__init_subclass__ = something
...: return cls
...:
In [5]: @test
...: class Test: ...
In [6]: class Test2(Test): ...
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-14348d06b900> in <module>
----> 1 class Test2(Test): ...
TypeError: something() missing 1 required positional argument: 'cls'
I can't figure out why __init_subclass__ isn't being passed any arguments
because something is a function, you need to call the protocol with __get__ to bind cls
You need to bind it to the class object
cls.__init_subclass__ = types.MethodType(something, cls) ought to work
I did not know MethodType, I personnaly use something like:
cls.__init_subclass__ = something.__get__(cls, cls.__class__)
MethodType seems way cleaner π
both of these work
pick the one you prefer then π
or not...:
In [28]: def test_deco(cls):
...: def __init_subclass__(cls):
...: print(cls)
...:
...: cls.__init_subclass__ = __init_subclass__.__get__(cls, cls.__class__)
...: return cls
...:
...: @test_deco
...: class Test:
...: ...
...:
...: class Test2(Test):
...: ...
...:
...: class Test3(Test2):
...: ...
...:
<class '__main__.Test'>
<class '__main__.Test'>
__init_subclass__ isn't getting the correct argument passed in
well, you bind __init_subclass within the test_deco you did at the begining
it needs to be bound again with Test2 and Test3
I don't think decorator are suited for what you do
I've wrapped it with classmethod decorator:
def test_deco(cls):
def __init_subclass__(cls):
print(cls)
cls.__init_subclass__ = classmethod(__init_subclass__)
return cls
@test_deco
class Test:
...
class Test2(Test):
...
class Test3(Test2):
...
this works
ho nice
i'm not sure what magic it's doing, but i'll dig into it later
I was going to suggest using a metaclass if you needed to add a function to the most derived impl of an inheritance
well, i have a metaclass solution to this hacky thing I'm doing
but the decorator solution is simpler
if I can get the methods to bind correctly
this is hwo it is implemented, I've not thought of using the descriptor this way
class ClassMethod(object):
"Emulate PyClassMethod_Type() in Objects/funcobject.c"
def __init__(self, f):
self.f = f
def __get__(self, obj, klass=None):
if klass is None:
klass = type(obj)
def newfunc(*args):
return self.f(klass, *args)
return newfunc
they give this as the descriptor of classmethod
yep
here's the thing:
from inspect import signature
def parse_signature(signature):
return str(signature)[1:-1].split(', ', 1)[1]
def additive_init(cls):
def __init_subclass__(cls):
def add_parent_init(init):
self_args = parse_signature(signature(init))
parent_args = parse_signature(signature(cls.__base__.__init__))
globals_ = {'init': init, 'cls':cls}
locals_ = {}
wrapper = f"""
def wrapper(self, {parent_args}, {self_args}):
super(cls, self).__init__({parent_args})
init(self, {self_args})
"""
exec(dedent(wrapper), globals_, locals_)
return locals_['wrapper']
cls.__init__ = add_parent_init(cls.__init__)
cls.__init_subclass__ = classmethod(__init_subclass__)
return cls
@additive_init
class Person:
def __init__(self, name): self.name = name
class Cyborg(Person):
def __init__(self, version): self.version = version
class Android(Cyborg):
def __init__(self, model): self.model = model
print(signature(Android)) # (name, version, model)
Just a way to add inits from subclasses so you don't have to keep re-typing it all
well, i have a metaclass solution to this hacky thing I'm doing
When you said hacky, I was actually far from the reality of what you were trying to achive π
when you end up doing exec, IMO you're the level above hacky π
I think at some level of meta-programming exec is very useful and nearly necessary
(TBH I don't have better to offer so not saying that you should NOT do it)
dataclasses use exec as well -- most code generators will
actually for parameter forwarding I prefer the django approach: they define only named args, and forward **kw to parent
the base class itself might not take **kw, so you get errors if some args are not known
namedtuples are the ones I know about using exec; when I had a problem with them the code was quite confusing to navigate while looking for the cause
anyone know any java disord server i can join?
Does anyone know how costly it is to add new fields to a numpy.struct_array?
Π·Π΄ΡΠ°ΡΡΠ΅
import random
def random_key(w):
a ,= random.choices(*zip(*w.items()))
return a
```should about do it
forgot to unpack w.items()
oops
In [49]: Counter(random_key(w) for _ in range(1000))
Out[49]: Counter({'B': 761, 'A': 239})
In [50]: w
Out[50]: {'A': 1, 'B': 3}
```with that it seems to work
How does machine learning work
@lone wagon better to ask that in #data-science-and-ml
k
looks like a class def to me:
attr_class_template = [
"class {}(tuple):".format(attr_class_name),
" __slots__ = ()",
]
!unsilence
β unsilenced current channel.
Is there any reason to use eval(compile(...)) over a direct exec? (from snippet linked above)
As for bulk inserts than that's a meme, with executemany being slower than just a for loop of executions which is
@radiant fulcrum Use the correct function then:execute_values. And the point is that even that basic SQL formatting is more than other libs seem to offer
Hey,please help me in #help-kiwi .
@ivory warren don't post in multiple channels for the same question, and this is not a help channel
Yay π₯³
What does it mean **kwargs?
@teal stone post in #python-discussion , this is the wrong channel
kwargs is short for keyword arguments
Has anyone been following the infinity constant proposal and can bullet point it for me? The thread wonβt load for me for some reason π¦
I haven't looked at the thread, but what about float('inf')?
I think they want a more standalone constant
To be honest, even defining INFINITY = float('inf') isn't too bad
@red solar The proposal makes the following points:
- It is also a fundamental constant (similar to True, False, and None), and should be representable as such in the language
- Requiring a cast from float to string is messy, and also obviously less efficient (but this performance difference is likely insignificant)
- Further, having a function call for something that should be a constant is a code-smell; in general str -> float conversion may throw an error or anything else and I'd rather not worry about that.
- It would make the useful property that
eval(repr(x)) == xfor floating point numbers (currently, NameError: name 'inf' is not defined)
The last point is further explained on the example of trying to serialize a list of floats by storing its repr and then evaling it
currently, it will throw the NameError
Wait, which PEP is this?
No PEP, it appears on the ideas mailing list: https://discordapp.com/channels/267624335836053506/704372456592506880/751487221374255115
Ah ty π
I agree with all of that, and hope it gets adopted (wonder how much code it would break tho)
Although ig if they added async as a keyword, they can do something for infinity
I am guessing they would keep float("inf") and give a deprication warning for a few versions
float("inf"), not float(some_variable_that_resolves_to_inf). In a similar vein 4(6) raises a syntax warning
There'd be no reason to ever deprecate float("inf"), nor would this be a new keyword. It'd be a new builtin. Though assignments to inf would probably need to be banned, like they are for None and True and False, and that's backwards incompatible.
I don't think it should be deprecated tbh
anyone here a git genius?
Like NotImplemented
At the cost of incongruity with the other builtin singletons.
Except NotImplemented
TIL.
Also Ellipsis
If it's allowed to be reassigned, I don't see any backwards compatibility concerns.
is there a way to connect to the web via python (not like with sockets) to check for example if an URL exists?
What if I based my code on there being a possible nameerror on inf
what?
Whatβs your goal?
to check if an URL exists
like try (function to connect to an URL) except print("URL doesn't exist")
Yea, use requests and catch the ConnectionError
def check_url(url):
try:
requests.get(url)
return True
except:
return False
is there a library to import?
@noble birch yes, it's calledrequestsπ
you'd also need to install it - requests is not a standard library.
Also, now that I notice, this is #internals-and-peps , not a help channel. Might want to move to one.
is there a library to import?
@noble birch
oops
i meant to say
that
you should go to your command promt and type ' pip install requests '
So basically my friend says he knows the least coding out of my Friend group is really hesitant to coding in general. Btw weβre all cs majors so how should I persuade him to code without being too preachy ??
if you're all cs majors you probably shouldn't need motivation to code
if you're all cs majors you probably shouldn't need motivation to code
@feral cedar hmmm how so Iβm just wondering
I guess itβs embedded into our cs instruction in college anyway
Well my friend Is the kind to get perfect scores on ap computer tests yet says he βforgotβ all the code techniques when it comes to working on a project
shrug he'll probably wind up dropping out or changing majors. Or focusing entirely on the math theory behind CS and never learn to code well. If he doesn't want to learn to code, you're not going to be able to talk him into it.
That's all off topic for this channel, though. #career-advice is probably the best channel for it.
shrug he'll probably wind up dropping out or changing majors. Or focusing entirely on the math theory behind CS and never learn to code well. If he doesn't want to learn to code, you're not going to be able to talk him into it.
@raven ridge no he says he wants to pick up on python and some js with discord.js but thru working with him in a casual group project , he has a really laissez faire approach and hardly ever intervenes unless the product looks βoffβ in his mind
@radiant fulcrum it errors for invalid urls (based on the dns response afaik)
@unkempt rock Sounds like management material
Ye what if I should suggest my pap to go into business idk....he says he hasnβt really extrapolated too much into cs and/or programming in general. My friend actually put his work in progress cs50 certification on LinkedIn. He didnβt even finish it yet. Like he just does it for the resume so how should I tell him nicely? Sorry if Iβm too concerned about his activity @pseudo cradle
Anyone around? I have a question about Python's hash() function and whether I should use an alternative.
what are you trying to do?
i'd not rely on the hash function for anything outside of implementing your own __hash__ methods.
How do I uninstall python 32 bit
just... uninstall it
it also has an change option can I use that to make it into 64 bit
What is the meaning of backtracking and how is it done?
@Beginner(slow-witted)
Hey Developers I am a 2nd year student and i want to build a system like jarvis of iron man. So i need your help I don't know where to start and what to do.
This channel is usually a bit more focused and specific than that. Maybe an off topic channel would be better?
@graceful saddle so first you must revolutionize AI research :) Maybe scale down your ambition a bit.
can anyone send me a pygame code to make a space invaders game using pygame
Guido has this great hot take from 2002: "filter and map should die and be subsumed into list comprehensions"
@cunning scaffold this channel isn't the place for that
honestly, map(f,*a) is cleaner than (f(*v) for v in zip(*a)
@flat gazelle it's more concise but I think the second one is a bit more obvious
granted both are entirely non-obvious if you're not familiar with that usage of the star operator.
I guess I wouldn't reject the first one if it was in a PR
I also use filter occasionally, but I feel like yield from filter(z, y) is more elegant than yield from (x for x in y if z)
strong disagree on that comprehension being more obvious than the map, but we all have different tastes i guess
Is there a way to block child class from inheriting parent docstring?
At work we subclass DataFrames in a few cases and inheriting the docstring just makes the help so verbose
Lol u reminded me of a case where someone was annoyed that docstring didnβt get inherited - iβll ping u if I find it
(Although thereβs probably easier ways)
If you control the parent class, override __init_subclass__ and set the child's __doc__ attribute to "". I've never tried it, but I think it could work.
>>> class T:
... """This is a docstring"""
... @classmethod
... def __init_subclass__(cls, **kwargs):
... cls.__doc__ = ""
... super().__init_subclass__(**kwargs)
...
>>> class M(T):
... pass
class T(builtins.object)
| This is a docstring
class M(T)
``` It seems to work here. The docstring doesn't appear for `M`.
Nice, thanks!
hello !
can anyone help me i dont know how this code works i dont know the benefit of it
for i in range(len(supplies)):
print('index'+ str(i) +'in supplies is :'+supplies[i])```
I'm assuming you got it from automate the boring stuff?
it's teaching you about loops, strings and indexing
Hello
Pls answer these both questions
How do I store the mid value of my string into a new matrix starting from 0,0 then 0,1 So on...
And
How do I store the values of a matrix into a list
Can I say x=[ [1][2],[3][4]]
Will x be having the 2 elements of my matrix?
@dapper yoke take a look at #βο½how-to-get-help. This channel is specifically for discussion
Thankyou
anyone have any experience with boto3
Hi, I just started learning about linked lists. I am printing what I stored in the linked list and the element at 0th index shows "None".
@trail ridge this sounds like a better question for #algos-and-data-structs . however you should at least show your code so that other people can help. nobody can guess what is wrong with your code
Is the following an acceptable pattern:
class MyClass():
def __init__(self, arg1, arg2, arg3, **kwargs):
self.arg1 = arg1
self.arg2 = arg2
self.arg3 = arg3
self.other_class = OtherClass(**kwargs)
def __enter__(self):
self.other_class.__enter__()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.cleanup_func()
self.other_class.__exit__()
why wouldnt you subclass OtherClass
Though this probably should be in help #βο½how-to-get-help
Because this is just a really simplified version
Imagine if OtherClass was open(filename)
we've been kind of 50/50 on whether "is this pythonic" questions are allowed
as long as they're sophisticated and not "how do i write function"
Yeah I felt this was more conceptual than a directed help
that said no i dont think there's anything wrong with it
there are lots of reasons why you wouldnt want to subclassOtherClass, e.g. maybe you have 3 other_class-like things
(the big problem i see here is lack of attrs usage π)
but for real yes its fine, however you should pass all those args in exit other_class.__exit__(exc_type, exc_value, traceback)
im also not a fan of the **kwargs here
class MyClass():
def __init__(self, arg1, arg2, arg3, other_thing: OtherClass):
self.arg1 = arg1
self.arg2 = arg2
self.arg3 = arg3
self.other_thing = other_thing
def __enter__(self):
self.other_thing.__enter__()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.cleanup_func()
self.other_thing.__exit__()
actually attrs isnt that useful unless you want type annotations, in which case it saves you a lot of duplicated typing
I wouldn't normally use kwargs. Again I was trying to create a really generic example
@attr.s
class MyClass():
arg1: int = attr.ib()
arg2: float = attr.ib()
arg3: bool = attr.ib()
other_thing: OtherClass = attr.ib()
def __enter__(self):
self.other_thing.__enter__()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.cleanup_func()
self.other_thing.__exit__()
I'm unsure on whether it's better to allow someone to pass the composite class or create it within the init
i definitely prefer the former. you can always create a default one if they pass None
For example in a situation where the composite class is something you wouldn't expect the end-user to ever directly interact with
fair enough. if the wrapped class is never meant to be created on its own then it's fine
plus creating classes to pass as parameters to other classes feels very clunky and Java-esque so i don't blame you for wanting to avoid it
but sometimes it really is the best way to do things
i'll take clunkiness if it means avoiding wasteful code duplication
or if it means avoiding a kwarg dumpster
i really dont like **kwargs in user-facing functions unless it's very obvious what said kwargs are being passed to
I have one case where there is a sort of 1-1 mapping of inner class to wider class, but which inner class you want is dependent on the outer class. Does that make sense at all haha
hm
why
maybe something like this?
@attr.s
class MyHttpxClient:
sansio_client: MyClient = attr.ib()
httpx_client: httpx.Client = attr.ib(factory=httpx.Client)
Like, I have a generic outer and generic inner. But then I have a specialised outer which uses a subclassed inner with one method overidden
can't the outer just wrap the one special method
instead of making a whole customized version of the inner
The outer class would then override a method on a class it's being passed?
no of course not
Like:
class InnerA():
...
class InnerB(InnerA):
def overridden(self):
#stuff
class OuterA():
def __init__(...):
self.inner = InnerA()
class OuterB(OuterA):
def __init__()
self.inner = InnerB()
Does that explain what I'm talking about?
@attr.s()
class OuterThing:
inner_thing: InnerThing = attr.ib()
def frobulate(self, x: float) -> float:
return self.inner_thing.frobulate(x / 100.0)
versus
class SpecialInnerThing(InnerThing):
def frobulate(self, x: float) -> float:
return super().frobulate(x / 100.0)
class OuterThing:
inner_thing: SpecialInnerThing
def __init__(self):
self.inner_thing = SpecialInnerThing()
def frobulate(self, x: float) -> float:
return self.inner_thing.frobulate(x)
I think what I probably want is for the outer class to define an inner class factory?
maybe. which in python of course is just a class
it really depends on how your application is set up
consider what your public interfaces are
what your data contracts are
hi everyone
Hello Nurken
does anyone know, how to create paint program at Python?
That's not really a question for this channel; we try to reserve this channel for discussing the Python programming language itself, from a higher-level and more abstract perspective.
We do have a lot of other channels where you can ask your question, like #python-discussion, or help channels
plus creating classes to pass as parameters to other classes feels very clunky and Java-esque so i don't blame you for wanting to avoid it
@paper echo why do you think so?
oh sorry
@gleaming rover it's just not something you see often in python
and classes like BeanFactoryFactory are a meme
@gleaming rover it's just not something you see often in python
@paper echo wait, let me clarify
did you mean classes, as in type objects (as opposed to instances)?
no sorry i mean instances, but things like this OuterThing(InnerThing(a, b), c)
yes
I actually don't have much Java experience so I'm not sure about that part of your statement, but why do you think it's clunky
i dont have much java experience either so i might be treating it unfairly
its clunky because you just dont see it that often in libraries
but i think its a perfectly valid, maybe even underused, pattern
hm.
now that you mention it
why is dependency injection so uncommon in Python
or is it just in the stuff I use
(fwiw i dont like dependency injection, id rather just wrap stuff)
you see things like factory= very occasionally
but again why do that when you can just write a wrapper function
its a good question though
Whats future module?
Whats future module?
@unkempt rock basically, a way to apply changes that will be mandatory in a future version of Python, but optional in the one you're running
Oh
future, or __future__ ?
__future__ is part of the Python language, a way to opt into backwards incompatible language changes before they become mandatory. future is a 3rd party module that tries to help you write code compatible with both Python 2 and Python 3.
and a Future is...
entirely different, but that's in futures, not future or __future__, heh
I want to override gettattr but my mul does a deepcopy which causes gettattr to throw an error. I don't want to override deepcopy as I don't feel I can reliably duplicate what that does. Anyone know if there's another solution?
anyone here use buildozer???
@unkempt rock Does your __getattr__ raise an AttributeError for nonexisting attributes? If not, that's probably what's causing the issue you have.
Normally, when you access a non-existing attribute, you'll get an AttributeError. However, if you overwrite __getattr__, that method gets called and it returns something. If the function does not hit an explicit return, it will return None implicitly. Take a look at this:
!e
class Foo:
def __getattr__(self, attr):
if attr == "bar":
return "some result"
f = Foo()
print(f"This will print 'some result' as expected: {f.bar!r}")
print(f"This will print `None`: {f.something_else!r}")
@wide shuttle :white_check_mark: Your eval job has completed with return code 0.
001 | This will print 'some result' as expected: 'some result'
002 | This will print `None`: None
I think this is what happening in your case
If you raise an AttributeError, copy.deepcopy will not complain:
!e
import copy
class Foo:
def __getattr__(self, attr):
if attr == "bar":
return "some result"
raise AttributeError
f = Foo()
copy.deepcopy(f)
@wide shuttle :warning: Your eval job has completed with return code 0.
[No output]
hmm. now i get max recurision.
def getattr(self, key):
if key in self.kwargs.keys():
return self.kwargs[key]
else:
# return None
raise AttributeError
but the deepcopy error is gone. however now it wont deep copy
You have to use the attribute access from a parent class that doesn't modify it or it'll call itself every time
self.kwargs <- if kwargs isn't an attribute you'll be calling getattr again
sorry . yes probably not best name. but it is defined on init
and just takes the kwargs that are passed
In this case, the object won't be initialized using teh __init__, so that attribute won't exist (yet)
So, everytime it's trying to check if an attribute exists before that kwargs becomes available, it will get in a __getattr__ loop
Maybe if you do it like this (or handle the KeyError), it will work:
def __getattr__(self, attr):
kwargs = super().__getattribute__('kwargs')
if attr in kwargs:
return kwargs[attr]
raise AttributeError
class MyClass(object):
def init(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
def __mul__(self, other):
reproducer = []
for i in range(other):
reproducer.append(copy.deepcopy(self))
return reproducer
def __getattr__(self, key):
if key in self.kwargs.keys():
return self.kwargs[key]
else:
# return None
raise AttributeError
ive deleted some stuff
but essentiall i want my cake and eat it
i feel if i wrote a deepcopy that did what deep copy does. it may work. but i dont trust i can do that
essentially if prop not exist. look for it on the nest array. gonna try your tip now. 5 mins
woah!
5 hours last night π¦ then i gave up
thank you so much
incredible
thank you so much . i've read the code and completely understand now what you are doing. I don't know why that wasn't obvious to me. thanks
I'm pretty sure I've burned myself with getattr a few times
For me, it's partly because None is just a valid, regular return value that's returned implicitly. My first instinct is to write a __getattr__ that just handles the dynamic attributes that I want to handle, but, if you're not careful, now every attribute exists dynamically but most of them just return None.
You don't notice this a lot until you hit something that relies on that AttributeError signal to be raised.
And, in this case, deepcopy throws a curveball as well, as you'd expect kwargs to exist from the start but it only exists after it's been deepcopied.
I don't really have anything to link to. Most of the free time I have for Python I spend on this community.
And I don't particularly need to get credit, as it were just a few lines and I integrated @peak spoke idea as well
big thanks to all that helped. I'll link the discord group. very grateful
I cant now get dot notation on extra props. which is so cool
*can
how do i call a function given as a parameter
I don't think anyone in communities like this, SO etc. really expects you to credit them even while technically it's all rights reserved unless stated otherwise
@stuck barn wdym
like a callback ?
func()
ok
u are passing the func by name (Func("func")) or the object itself (Func(func))
yes
i mean how are u passing it
like Func("func") or Func(func)
ok
Python has functions as first order objects. You can pass a function just like passing any other variable. Any variable that holds a function can just be called like a function.
^^
wait so if i do h = func then i can do h() ?
h = func
!e ```python
x = print
x("hello")
def say_hi(printer):
printer("hi")
say_hi(print)
@raven ridge :white_check_mark: Your eval job has completed with return code 0.
001 | hello
002 | hi
@peak spoke SO does list a license for user contributions, but some examples and snippets are fairly generic. I doubt you could really claim "copyright" on them. Anyway, we're getting a bit off-topic, but you're right about people probably not expecting credit.
Although you could do h = func() if func() returned another function
!e
You are not allowed to use that command here. Please use the #bot-commands channel instead.
ohh
here is a very simple example
def a(func):
if callable(func):
func()
else:
pass
def b():
print("it works")
a(b)
#this wont
a("hi")
it's a staff superpower to use !e in discussion channels.
i shall wield it someday
@stuck barn for completeness's sake: a function is an object. That object is able to be called. The name of the variable that refers to that object is irrelevant. The statement def foo(...): creates a new function object and assigns it to the name foo. It's syntactic sugar for a regular assignment. Once it has been assigned, it can be reassigned to a new name, or passed as an argument, or whatever.
I wish there was a std library of common decorators
It's the function object itself that has something magical about it - callability - not the original name that it was defined with. The only thing special about the name that it was defined with is that it's stored and introspectable, but that's really only for debugging purposes and to give a nice repr
The nice repr is hugely important though. I really wish lambdas had reasonable reprs.
It would be cool if there was a bitset/bitarray in the standard library
class array.array(typecode[, initializer])```
A new array whose items are restricted by *typecode*, and initialized from the optional *initializer* value, which must be a list, a [bytes-like object](../glossary.html#term-bytes-like-object), or iterable over elements of the appropriate type.
If given a list or string, the initializer is passed to the new arrayβs [`fromlist()`](#array.array.fromlist "array.array.fromlist"), [`frombytes()`](#array.array.frombytes "array.array.frombytes"), or [`fromunicode()`](#array.array.fromunicode "array.array.fromunicode") method (see below) to add initial items to the array. Otherwise, the iterable initializer is passed to the [`extend()`](#array.array.extend "array.array.extend") method.
Raises an [auditing event](sys.html#auditing) `array.__new__` with arguments `typecode`, `initializer`.
bytearray is also a thing
Yeah, the use case where I could have used something like this I ended up using bytearray which was much better then a list
Imo it seems like something that would fit nicely in collections but I'm not sure if it's too niche of a data type
@mighty viper I would ask in #web-development
WHy am i getting this error?
if i shutdown and restart then it works
but am not understanding why am i getting this error
its not an error
the cells are running
also, this isnt the proper channel to ask about this
Maybe, if you first move to the right channel. see #βο½how-to-get-help
Ayo anyone knows if there any way to implement a key of some sort to only be able to run ur python script on one computer ? Let's say for example i put a program for sale and i dont want it to just be passed on so easily is there a way i can add something to my program to only run on that particular computer ?
There are many ways. The best way is of course to not ship your software at all and instead have it on your own server. After that you should use a compiled language, then if you finally decide to use python then it's super easy to bypass and you should learn to live with that.
Can you even do that in a compiled language
@timber bobcat you can use PyArmor, but it's not likely that anyone will want to claim credit for your exact code.
I think their concern is piracy
Ah
yeah i already considered PyArmor so they cant read the files , but my concern is that one guy buys it and he just spreads it like covid-19
i was wondering if there was a way to make the program only run on one computer
No matter what language you use, basically the only way to stop that is to use a one time registration key
Consider that even professional and very expensive software like Adobe creative suite and Microsoft word can be cracked
or use the Software As A Service model
I'm actually not entirely sure how license keys work for software like that. I suppose the license key is sent to a server for approval, and then some kind of local confirmation of approval is stored
Or you can have the program connect to the Internet to check the key upon start up
At which point yes, it becomes much easier to hack apart a python program than a compiled C++ program in order to get it to run without a valid key..
I also think that kind of start up check can result in being user hostile as it now requires an Internet connection in order to use the software
You could cache the check
my program needs internet anyways to run
Maybe there are algorithms that let you use some kind of asymmetric encryption
so it wont be a problem really
but tbh i dont think someone will go out of their way to crack my program if i implement a one time registration key
its just a bot for a phone game
that sounds ToS-breaking π€
shhhh
Yeah, selling an automation bot for a game definitely sounds ToS-breaking
!rules 5
5. Do not provide or request help on projects that may break laws, breach terms of services, be considered malicious/inappropriate or be for graded coursework/exams.
oh shiiiπ
Seems a bit ironic to worry about piracy when you're botting someone else's application
True
hi
sup
the one above this one
ok thanks but i think ill stay here
naahh thanks im coo
i think we should move to the general chat till the mods dont throw the mighty ban hammer uppon us
kk
@vast dome thanks for reading them, a lot of people dont even bother
(Not throwing shade)
π
ayy man i read em
And yes @timber bobcat thank you as well
just got a bit carried away
Wasnt meant to be a jab at you
oh
Lots of people just ignore the rules and sometimes get upset when they are directed to the rules
You didnt do that so its all good
Hey my RTX 2060 is sitting at 60% usage while training a tensorflow bot. Why is tensorflow not maxing my card?
Latest version*
because you're training AI and thats inherently load intensive
TF is doing what its supposed todo by making use of your GPU
Oh. But surely it should use all of it?
Hmm interesting. π€
but equally you should remember the system GPU's like that are designed for are not the same as those used for AI
CPU and system RAM is cool.
True. Very true.
VRam aswell can be a limit
try:
from scipy.stats import binom_test
except ImportError:
print >>sys.stderr, 'INFO: Could not import scipy (www.scipy.org): signtest is not available.'
this was apparently written in 2013
has that syntax for print ever been supported or does this person just think they're writing C++?
so this is python 2?
that line at least
I have a few good things to say about this file
for example, the author uses random.choice
that's some weird syntax π
I've made a config system for my app where values are accessed and set with class attribute access on classes directly at the module level so with the class
class Category:
value: ... = ...
I can access the shared value from every module in my app with an import and all value gets synced to a file when set unless a method is used to delay it.
This works great, but I'm not sure about the design with classes and attributes, do you think it's fine or since the values are being modified at runtime some kind of an instance that gets passed around would be more fitting?
how do i extract the most dominant color from any type of image in any format?
somewhat like spotify
This isn't the place for questions, but https://stackoverflow.com/questions/3241929/python-find-dominant-most-common-color-in-an-image here
for example, the author uses
random.choice
@boreal umbra israndom.choicebad?
thanks @unkempt rock!
@unkempt rock no, random.choice is very good
The alternative is to use randint for the length of your list
ah ok
@unkempt rock (my responses are intermittent because I'm at work) random.choice is also better because it works for any container type. I think.
Let's see.
No worries. I'll read your responses as they come.
!e
bob = {1, 2, 3, 4, 5}
from random import choice
print(choice(bob))
@boreal umbra :x: Your eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "<string>", line 3, in <module>
003 | File "/usr/local/lib/python3.8/random.py", line 291, in choice
004 | return seq[i]
005 | TypeError: 'set' object is not subscriptable
Hey guys, I'm using the Youtube API Data
and i came to this error
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=ItsAleMazing&key=AIzaSyAM3YG7QAGmwiz36vKksbalOwKFKGisxVk%0A&alt=json returned "The request cannot be completed because you have exceeded your <a href="/youtube/v3/getting-started#quota">quota</a>.">
my code:
addLog('(type=id) Getting a request of Channel ID of "' + username + '" ...')
time.sleep(5)
request = youtube.channels().list(
part="id",
forUsername=username
)
addLog('(type=id) Got a request of Channel ID of "' + username + '" ...')
addLog('(type=id) Executing the Request of "youtube.activities" of [' + username +']...')
time.sleep(5)
response = request.execute()
time.sleep(5)```
May someone help me ?
the error come from this line `response = request.execute()`
@burnt obsidian this channel is specifically for discussions. Take a look at #βο½how-to-get-help
steler it works on sequences
hey so for matplotlib.pyplot why do i have to call plt.show()? can't it just pop up automatically for me?
hey so for matplotlib.pyplot why do i have to call plt.show()? can't it just pop up automatically for me?
@solemn jewel depends on where you're running it
some environments do, automatically
e.g. if you're running in terminal
it doesn't
so that you can apply all your changes before you show
not really the right channel btw, try #data-science-and-ml
Its weird that random.choice doesnt work on sets
not really
well, if the container is unordered, you don't really have a way to get an element that you can prove is random
Makes sense
it needs a sequence
A set is really more like a dictionary with just keys I guess
I imagine random.choice gets a random integer in the range of the sequence length and uses that as the index to get a random element. Don't know if that's actually true
That's always been my explanation to myself for why things likes sets will not work
probably, sounds about right
not sure if this is the right channel but, how do you write a function that can clear frames in tkinter?
not sure if this is the right channel but, how do you write a function that can clear frames in tkinter?
@ebon jasper #user-interfaces
thanks
I imagine
random.choicegets a random integer in the range of the sequence length and uses that as the index to get a random element. Don't know if that's actually true
@charred wagon this is true
steler it works on sequences
@feral cedar I can understand based on the error message why the implementation doesn't work for sets, but conceptually getting a random item from a set isn't hugely different from getting one from a list.
@feral cedar I can understand based on the error message why the implementation doesn't work for sets, but conceptually getting a random item from a set isn't hugely different from getting one from a list.
@boreal umbra is it not...?
The very concept of getting a random item from an ordered sequence is agnostic to the fact that that sequence is ordered.
in particular, for a set, there is no way to refer to any specific element
Right
it's not about it being ordered, really, I think
Sets have a method that will return a random element and remove it from the set. I don't remember the name for sure but it might be pop.
Sets have a method that will return a random element and remove it from the set. I don't remember the name for sure but it might be pop.
@boreal umbra it ispop, but it doesn't return a random element
it returns an arbitrary element
Point taken.
Thinking a bit more about the discussions I had in here yesterday with __enter__ and __exit__, is there some guidance for how an object with those defined is meant to behave. Like it seems that there is not consistency since with can be used without as i.e. with threading.Lock():. Is it really just a case of good documentation to ensure the correct usage of your class with a context manager?
usually it should be somewhat obvious when you need as
since that's when you actually want to use the object
@safe hedge the only semantic is that __enter__ should will be called on entering, and __exit__ on exiting. it can be used for resource acquisition, or exception management, or other things.
@spark magnet So effectively yes any specific usage/behaviour is only really covered by documenting?
e.g. Like this from stdlib https://docs.python.org/3/library/threading.html#using-locks-conditions-and-semaphores-in-the-with-statement
yes @safe hedge
hey guys
sup
why hasattr func even exists ?
can't we just use getattr and compare if its something or not ?
@unkempt rock because then you have to always decide on a sentinel value
@unkempt rock you could do getattr(obj, attr_name, None) but if the object does have that attribute and its value is just None you will get false positives you need a default for your method and there is always a chance that that object has that value as its atribute
hasattr is also implemented directly in C
https://github.com/python/cpython/blob/c96d00e88ead8f99bb6aa1357928ac4545d9287c/Python/bltinmodule.c#L1131-L1150 this is a lot more efficient than, say getattr(obj, attr_name, SENTINEL) is SENTINEL
and a lot less ugly
@paper echo only thing with hasattr compared todoing it with getattr is that it wont get methods
from what ive found
class Foo:
def bar(self):
...
hasattr(Foo(), 'bar')
True```works for me
hmm
what do you mean it wont get methods?
idk now lol, ive had issues in the paste where it wont get methods without doing say bar()
!e ```python
class Foo:
def bar(self):
...
foo = Foo()
print(type(getattr(Foo, 'bar')))
print(type(getattr(foo, 'bar')))
@paper echo :white_check_mark: Your eval job has completed with return code 0.
001 | <class 'function'>
002 | <class 'method'>
hasattr also lets you use strings, and you can't use variables with the dot operator
So it opens up a lot of functionality that's cleaner to write than having lots of try-catch AttributeError
What exactly is Ellipsis/... in Python?
its a shortcut for slicing
say you had an 10 dimensional ndarray called x if you wanted to slice say 2,5,10 and everything inbetween 5 and 10 instead of doing x[2,5,6,7,8,9,10] you could do x[2,5,...,10]
or something like that, I don't fully understand it myself but its for slicing high dimensional stuff
numpy arrays use it as a filler for multidimensional slices, yeah
IIRC the only use it has in the stdlib are a couple of (relatively) recent cases in typing
wait... what
i actually didnt know you could slice a numpy array with ellipses
that's also a 3rd party library co-opting it
as i understand it, the ellipsis is just a no-op expression
It's a mystery, really
i wonder when it was added
It has existed long before the ... literal apparently, and I don't really see how that would be useful?
yeah, we found that out one of the last times Ellipsis came up in this channel
!e ```python
x = list('abc')
print(x[...])
@paper echo :x: Your eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "<string>", line 2, in <module>
003 | TypeError: list indices must be integers or slices, not ellipsis
i dont think Ellipsis itself is used anywhere in the CPython source
It's a nice placeholder for code in stubs or when you're planning to implement something, apart from that I think only typing uses it
maybe ... is
The stdlib doesn't support Ellipsis anywhere outside of typing
yeah but ... and Ellipsis pre-date those uses @peak spoke e.g. with Protocol
is it like @/__matmul__ where it was added just for 3rd party libs to use?
Pretty much, yeah
!e ```python
print(dir(Ellipsis))
@paper echo :white_check_mark: Your eval job has completed with return code 0.
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
Don't think there's much to be found about it when the commit that added the object is from 96
PSF Mysteries Unsolved
Looks like it was added in 1.4, there's a comment with 11-Oct-1996 GvR renamed Py_Ellipses to Py_Ellipsis :-( in a modsupport.h file but not really sure what that refers to (probably something during development as 1.3 doesn't have either of them in the source)
python is kind of a weird language
fascinating development history
i loved this talk https://www.youtube.com/watch?v=qCGofLIzX6g
the PEPs and issue tracker are fairly easy to go through for features etc. but there's not much to be found for python 1
it's always fun looking through the git repository back in early 2000 though (or whenever they put it on github)
there are git commits going back into the 90s
github has the whole commit history
or a lot of it
https://github.com/python/cpython/commit/85a5fbbdfea617f6cc8fae82c9e8c2b5c424436d seems to be the first
how can i make like "wait for some event" in python... ?
do i need to use async and await ?
or what..?
@paper echo Thanks for that Talk :P Probably wouldnt have found it without it but damn thats pretty nice
Classes are really hacked in at the last moment.
It should be possible to use other object types as base classes,
but currently it isn't. We'll see if we can fix that later, sigh...
lol
So ive learn this from the talk: Kill Pickle, Fix imports
wait whats wrong with pickle
Arbitrary code execution stuff
hmm, i suppose pickle can serialize callables
id never reallly thought about that, thats intriguing
no, it doesn't pickle code
but it will run whatever code you tell it to that it can find in the process.
I never read an explanation of how pickle is insecure, just knew that it was.
Typically I will deconstruct properties fromk objects and put them into json. Then the program that loads the json, will construct objects again using that data. There are situations where that isnt realistic I think, but I do that pretty often.
Right ^ state in methods/classes is a bad idea but for 3rd party objects pickles r fine
note; some modules don't work well with pickle; like XGBoost doesn't return the same object you stored
@solar delta what do you mean, "not the same object"?
it's in a new process, isnt it? so it can't literally be the same object
if you pickle an XGBoost model that has been trained the results won't come out the same if you have the same seed.
It works fine with Gradient boosting from sklearn though..
Can I post a slightly more advanced question here?
@leaden sphinx There are dedicated channels for Python help. In fact, it's our entire help system #βο½how-to-get-help. Your question could also go in a topical channel, specifically #databases. This channel is not appropriate because it is a discussion channel, not a help channel.
Thanks
In fairness to pickle, it is noted that you should only use it on trusted data
k
why does asyncio's loop.run_in_executor leave a thread open permanently and prevent the program from ever exiting?
continued in #async-and-concurrency
Iβm looking at the first CPython commits, thatβs pretty interesting https://github.com/python/cpython/commits/master?after=3ff6975e2c0af0399467f234b2e307cc76efcfa9+108000
It seems like GvR commited python libraries before all the C code
class CheckAppearance() = LabelAppearance():
class BaseButton() = NoReactivity(), LabelAppearance(), Define():
I'm glad we have different inheritance syntax...
me too
are we writing cursed code at this point 
i wonder if py class Foo(Base1 and Base2): could be hacked together using metaclasses
You'd probably have to mess with the mro
basically make and return a new class that has both Base1 and Base2 in its mro
dont know if thats possible tho
Don't think you can, since the and will evaluate them separately
python programmers often ask themselves if they can, not enough times if they should 
oh 100%
You can't override for and but you could use + instead. However is there a reason to since Foo would already be that new class with Base1 andBase2 in the mro
see the reason is why not
could use &
see but now i really want to try and use and
& can work, and is not overloadable, it just returns one of the operands depending on their truth values, short-circuiting if the first one is False
Although I guess you could hack something together with some inspect magic
You could let the first class trickle through, then go another pass by just reading the AST and redeclare the class as if and worked like that probably
Not sure where you'd hook that second pass in.
I guess you could put that in a decorator or a metaclass/parent class
So you'd hook in on __init_subclass__, for example, use some magic to get the declaration line and manually parse the parent list
At that point, can you still modify the MRO?
My guess is that it's already read-only at that point
You can modify __bases__ at any point with a few hacks
Also for a class that does not have other bases than object?
from inspect import getsource
class Base: pass
class OtherBase: pass
class A(Base and OtherBase):
pass
text = getsource(A).split('\n')
text[0] = text[0].replace(' and ', ', ')
exec('\n'.join(text))
print(A.__mro__)
That's the dirtiest thing I've done in a while
PS C:\Users\FarmArt.DESKTOP\Desktop> py test.py
(<class '__main__.A'>, <class '__main__.Base'>, <class '__main__.OtherBase'>, <class 'object'>)```
It evaluates the bool values of the class values and returns the last evaluated as usual
Oh right
@boreal umbra :white_check_mark: Your eval job has completed with return code 0.
3
They follow the same logic
Right
Return the left operand if it's enough to determine the truthiness of the entire expression, else the right operand
For or, it's enough if the left one is True, but not enough if it's False
For and it's the other way round
I believe it is for optimization reasons at first, and then made into a feature, binary comparisons returns the last compared value
I can't think of any obvious uses for this functionality of and
I've used it a few times
I don't think I've used that in serious code, but you can do some fun stuff with it
def func(thing, a=None):
a = a or []
a.append(thing)
return a
Not a perfect example
The chained comparisons are internally joined by and, so you can abuse that to hack in "custom" operators
Not a perfect example
the thing is that you must compare the types, in addition to comparing the values
For example here https://github.com/Numerlor/bot/blob/truncate-internal-eval/bot/utils/services.py#L22 for a use, or and and not returning bools but the last value they evaluated doesn't change much logic (and where it does they can be wrapped in a bool call), but does come in handy at times
#esoteric-python should have some obscure examples as well
https://mypy-play.net/?mypy=latest&python=3.8&flags=show-error-context&gist=0dfe390f4f5a111429efffb7b83a94e3 is there something wrong with the reduce signature annotation? or is there something wrong with me?
Is there any cmd in python to find biggest substring in alphabetical order.

Or do I need to search it through index?
Wrong channel. Try #python-discussion. Also sounds like a max should work for you
π
Actually
I was toying around with metaclasses, and now I'm wondering if you can unpack a sequence into a class definition
Yes, yes you can
>>> class A: pass
...
>>> class B: pass
...
>>> class C: pass
...
>>> bases = A, B, C
>>> class Derived(*bases): pass
...
>>> Derived.__mro__
(<class '__main__.Derived'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>)
how to start weather prediction, I mean use of numeric models (numerical methods)?
Heyy Guys! Im new here.. Nice to meet you guys
hello, im new here as well
Idk if here's the right place to ask that.. If its not, i'm sorry ...
Someone here worked with uwsgi?
I'm facing some issues here.. :(
Trying to deploy a project with multiple threads and Flask application. But I can't get it to work...
I want the threads that i'm creating only in main process (right now it is beeing replied on every process), and I want flask (and just flask) in multiple processes... Is this possible at all?
@steep turret you should ask in #web-development
@charred wind hi there!
#web-development might be the right place
This room is reserved for discussions about the python language itself. See the room description for more info.
Welcome @charred wind! #python-discussion is the best place for introductions and such.
Or one of the off topic channels if you want to socialize.
thanks!
imagine writing ray marching code and making a very crude console 3d graphics system
is there a way to modify text colors via RGB with the standard console on Windows 10?
@boreal umbra
def func(thing, a=None):
a = a or []
a.append(thing)
return a
Well, actually, there's a bug --
x = 42
things = []
func(x, things)
print(things) # []
Would need to be a if a is not None else []
is it a bug though? It returns the list that was appended to
would invert that condition to avoid having to use not
@peak spoke But it suddenly behaves differently when you provide an empty list.
@grave jolt what are you expecting things to be?
the function mutates a in all other cases, so it would make sense that it would mutate an empty list as well
So a test case like this would work?
x = 42
things = [24]
func(x, things)
print(things) # [24, 42]
thereβs a "bug" in the code
because it does not mutate anything that evaluates to False, not only None
Ah, exactly the feature I needed. 
To expand on @nekit's point, an empty list [] is False-y. So, a in your case would be assigned the second empty list which is a different object than an empty list passed in as a. Hence you're not mutating the initial empty list passed in.
def append_thing(thing: T, some_list: Optional[List[T]] = None) -> List[T]:
actual_list: List[T] = some_list ?? []
actual_list.append(thing)
return actual_list``` looks kinda neat
@normal field I know why it works like that. The problem is that it might not be the intention.
For example, if the second argument is reserved for some shared cache, it will not work properly when the cache is empty.
i wish i knew programming π
We all went from the ground up, just keep up the hard work and don't give up! 
im not sure but i think no
import module as md
obj1 = md
obj1.a =10```
so why do we not use parenthesis for instantiating through a module?
Given : the code in module is implemented such as to use @properties for functions
I don't understand the question. The only objects being instantiated there are a module object and an int, both of which don't need parentheses to instantiate because the language has special syntax for instantiating objects of those types (the import statement and integer literals, respectively)
Does Python have an exclusive OR operator that is not bitwise? If it doesn't... why not???
It doesn't seem to unless I'm missing something (which I may be)
It is not all that common, and bool(a)^bool(b) is not horrible
I guess
bool(a) != bool(b) maybe?
i do like lakmatiols version better though
def logical_xor(string_self, string_other):
return bool(string_self) ^ bool(string_other)
@pseudo cradle
if you want it to look more fancy then it is, take a look in the operator module
!d operator.xor
operator.xor(a, b)``````py
operator.__xor__(a, b)```
Return the bitwise exclusive or of *a* and *b*.
you have to do a bool() on a and b here as well
Yeah
or implement your own __xor__
It just seems like it would be something fairly basic that I'm surprised isn't in the language. We have and and or after all, why not xor that behaves the same way?
because edge cases are normally never a good enough reason to implement something into the standard library
!d numpy.logical_xor
numpy.logical_xor(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'logical_xor'>```
Compute the truth value of x1 XOR x2, element-wise.
Parameters **x1, x2**array\_likeLogical XOR is applied to the elements of *x1* and *x2*. If `x1.shape != x2.shape`, they must be broadcastable to a common shape (which becomes the shape of the output).
**out**ndarray, None, or tuple of ndarray and None, optionalA location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.
**where**array\_like, optionalThis condition is broadcast over the input. At locations where the condition is True, the *out* array will be set to the ufunc result. Elsewhere, the *out* array will retain its original value. Note that if an uninitialized *out* array is created via the default `out=None`, locations within it where the condition is False will remain uninitialized.... [read more](https://docs.scipy.org/doc/numpy/reference/generated/numpy.logical_xor.html#numpy.logical_xor)
nice
you could also implement your own version of this using lambda calculus
but that would be way overkill
@grave jolt its not meant to be a great example, the point is just that if one wanted to have an optional argument that filters exactly any falsy object, that pattern would work.
There's xor bc xor is another name for equivalence
A scalar xor is by definition ==
Scratch that
I'm thinking xnor which is equivalence
I mean look
a xor b == (a and not b) or (not a and b)
But on that note
~= is logically equivalent to xor
If the two things are not equal do this
So there's really no point
Bitwise xor is nice bc it's useful for sets and it's used for parity
it's interesting because I just had a reason to use the equivalent yesterday
my values were already bool so I just used !=
Hi can someone help me with numpy
@grizzled marlin this is the wrong channel #βο½how-to-get-help
How does inheritance work with dataclasses?
Do you need both the base and subclass to be decorated?
If you need to generate new methods for the subclass yes
Are you suggesting if the subclass only adds attributes then you wouldn't need to decorate it?
You'd still need to decorate the subclass if you're adding __init__ attributes + generating dunder methods
Yeah I read that, I was just kind of intrigued by what happens if you make a subclass a dataclass but the parent wasn't
Like a mixin situation
I had this:
class CoerceIntMixin():
def __post_init__(self):
for field in dataclasses.fields(self):
if field.type is int:
curval = getattr(self, field.name)
if curval is None:
continue
setattr(self, field.name, int(curval))
And I was using it as the base of some dataclasses but then I realised I didn't make it a dataclass itself
And I couldn't work out if I was headed for some weird bugs
I want to add L_0.5 loss in my model while training
i have written :: loss = tf.reduce_sum(tf.pow(tf.abs(self.Coef),0.5))
But its giving NaN Error!
How its working perfectly with L1 loss and L2 loss
loss = tf.reduce_sum(tf.square(self.Coef))
loss = tf.reduce_sum(tf.abs(self.Coef))
the above 2 lines are working perfectly, but i want to use L_0.5 loss....How to do that?
https://github.com/PyCQA/pylint/issues/3060
why would abstract-class-instantiated ever matter? I don't even understand the error that this is supposed to catch
A class with abstract methods will raise when initialized because those methods should be overriden, the warning is probably there so the error is discovered even when your code doesn't take those paths during testing
@peak spoke would this be something to read to understand better : https://stackoverflow.com/questions/4382945/abstract-methods-in-python ?
i've not seen abstract methods before, have just heard the name
abcs allow you to create interfaces, if you subclass an abc you either get an error when trying to initialize it without the methods implemented or you get the whole interface the abc defines. For example python provides some in the collections.abc for common data structures in python https://docs.python.org/3/library/collections.abc.html
You have to write an efficient and pluggable API application. Describe how you would choose which framework to use and which framework you think would be best for the job and why.
My question is: What's a pluggable API in the first place?
@keen anchor does this help:
REST service. Also, because the platform is pluggable, it means that you can expose any of your custom code as an API call or REST service as well. This means the days of difficult integration, and siloed functionality are over. Welcome to a RESTful development life!
does they trying to say, writing handler for certain services?
@keen anchorhttps://docs.mendix.com/apidocs-mxsdk/apidocs/pluggable-widgets
A guide for understanding pluggable widgets.
thanks
np
Hi there, anyone has experience of pyinstaller?
@proven dirge better to ask more specific questions. Someone having experiece with pyinstaller might not know what you are looking for.
#include <iostream>
#include <stdio.h>
#define PVAR(x) printf("var" #x " + 1 = %d\n", var##x + 1)
int main(void) {
auto [var1, var2, var3] = (int[3]) {1, 2, 3};
PVAR(1); PVAR(2); PVAR(3);
}
Is it possible to implement something like this in python?
Only thing that comes to my mind is ast but I've only used it once so I'm not so sure about it
@unkempt rock the new debug feature in f strings?
Is that the {x=} thing @half wolf ?
What was the alternative people use over MyPy again?
dont get me wrong i love MyPy's system but holy fuck is it slow
it is kinda stupid sometimes
because of static analysis and other things
pyre, pyright and pytype
@radiant fulcrum pyre, pyright, pytype
pyre is by facebook, pyright is by microsoft, pytype is one of those "google but not actually google" projects
im curious to know how many people actually use Pycharm's profiling tools
I look at the graph sometimes, it's nice
Excuse me i have a question friends
I am Ozan i am studying master in data science and i enrolled dataquest datacamp udacity courses
what should i do as next after finishing
for the future issues , NLP , machine learning engineer like this
what are your suggestions thanks for everything . Best regards
what should i do as next after finishing
@waxen kindle this is for discussion of the language; why not try #career-advice? you might get better help there
Oh okey sorry about this someone in data science channel suggested that channel i enrolled as new
Oh okey sorry about this someone in data science channel suggested that channel i enrolled as new
@waxen kindle yup we can talk in #data-science-and-ml, I pinged you
anyone know how i can play audio from a URL without using vlc because im having problems with vlc. How can i play audio from an audio url without using vlc
@unkempt rock take a look at #βο½how-to-get-help; this channel is specifically for discussion
okay
is random.shuffle O(n)?
I believe so
