#internals-and-peps
1 messages · Page 122 of 1
Right
I assumed that maybe it could be by default an int and then if exceeded to go to unbounded integer
nope. not in python
I see
Like how it's stored in the underlying runtime
Are ints stored as bigints as in other languages?
you can always mix python with lower lvl languages like C for certain portions, with lower level semantics as you preferred
however, from the perspective of pure python, there's no concept of memory management*, it's abstracted away
Yeah, I know, I was just curious about what we get as the core thing of python
Thanks
You still need to care about it when interop
Pig, why are you trying to lose friends again (jk)
What friends
So floats are also stored individually as reference types, right?
Even despite being bounded
I was just thinking... that with big caches and stuff today, we could theoretically afford 16-byte long primitives, with 8 byte for data and 8 byte for type
So that <= 8 byte long primitve data structures could be stored as value types
And everything else would go as reference type
Do you think it would be much more efficient that what we have in python now?
less pointer chasing would help, but would it help enough to be a measurable difference, idk.
It would give much less garbage and much more direct computations (=> caches and stuff work)
Although the 1st statement comes from my own thoughts, I have no idea how exactly memory management works in python
at the same time, you still have an indirect call with accessing the type and reading its slots to know what operations to do
so I am not sure just copying around less would matter all that much
I store the tpye info in the value itself
you can't fit a python type in 8 bytes
... why?
types in python have so called slots, which define what happens on various operators, such as +-*/.
But doesn't pyython runtime treat built-in primitives like floats its own way?
no
Hm
they are still just objects, with an extra float field and the type is responsible for using the field effectively
But each float stores a reference to some type meta data, right? Including the slots
some things do optimise by checking if the type is the native type and skipping the indirect call, but all objects in python have a common interface which is what most of python uses
a python object is a refcount, the type, and then whatever values are needed for the object
some things do optimise by checking if the type is the native type and skipping the indirect call,
Well that's what I thought to use my unions for
but you don't get the not specialised option then, right?
Yeah, i don't
Hello! I am creating a raw tcp socket and sending a crafted ip+tcp packet (it works, i can see it in wireshark), but when I try to recv on the socket, I get: [errno 11: resource temporarily unavailable]. Pseudocode in python: http://pastebin.com/khgefa0Z am I missing some syscall or socket option?
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
are you familiar with java? Because let me tell you, having to special case your code for every single primitive type is not fun
So generally, (a + b) would be 2-3 indirected call?
especially in python, where you optimise specifically floats and absolutely nothing else
I came from the C# land
yeah, C# has a more sensible implementation of primitives which lets you be generic over them
C# is statically typed, so we don't have anything like that
(technically there's dynamic, and it doesn't work on unions either)
But yeah, with int being unbounded, I believe I would only optimize floats
well, a + b is
load a (possibly from the local scope, which are static)
load b (possibly from the local scope, which are static)
call the add opcode
```the add opcode then checks the add slot on a, if it gets `NotImplemented`, it check the radd slot b, and then call that
so it is 1-2 indirect calls and a lot of pointer chasing
load a is one indirect call, load b is another one, and checking for a's add is a third one, right?
So 3-4 indirect calls
loading from the local scope is an array access, but a global scope would indeed be an indirect call
Oh, that's fun. So it's some kind of indirect stack?
"local scope is an array access"
local scopes are static, so the frame can just keep an array of objects and the compiler converts local accesses into indicies (though the names are still kept so that eval and exec work)
but you can't add a new local with exec
So, do I understand it right:
b = ... # local var
c = ... # another local var
a = b + c
<=>
*a.ref = add(*b.ref, *c.ref) # pseudo code showing how we load the values and try to add then
<=>
arr[2] = add(add[0], add[1]) # "dereferencing" replaced by a local static array
right?
@flat gazelle :white_check_mark: Your eval job has completed with return code 0.
001 | 2 0 LOAD_FAST 0 (a)
002 | 2 LOAD_FAST 1 (b)
003 | 4 BINARY_ADD
004 | 6 RETURN_VALUE
Oh I've seen that API, is it JIT for python, right?
yes
Great. Thanks
why does this work the same?```py
raising an error class
raise type('TestError', (Exception,), {})
raising an error instance
raise type('TestError', (Exception,), {})()
if you raise the class, it will be instantiated without args implicitly
so raise Exception and raise Exception() should be functionally equal
o
i was just bumbling around some code, right clicking go to defn to go to the source etc, and realised that I was actually reading code for the inspect module, not the package i was intending to read.
I'm aware (i think?) that some core python modules are written in C, some are in pure python etc... But i'm not sure if there's any logic to what's written in which and why. For example, why is the inspect module not written in C? (Or cython, or whatever).
Given a random module from the standard library, can one say what the source would be written in given the module and it's purpose? Or is it just whatever the author felt like / was familiar with?
Can someone help me refactor my app using Classes? The app works fine, but I just wish to re-write it with Classes to grasp the concept of Classes better.
https://github.com/zubin13/Ton-Miles-Manager-for-Oil-Rigs
If anyone wants to code together on it please get in touch
@cobalt agate i don't think this is the right channel for that, if you had a specific question about classes / their use it might be more suitable, going through your code and refactoring is probably off topic though
imo classes should not be used just for the sake of using classes.
i think one should use classes for two reasons - bundling same things together (data or functions) or to deal with state
making APIs nicer maybe can be third reason, but that's more advanced
more often than not turning nicely defined global functions into methods leads to the temptation of introducing side effects via self
only making testing more complicated without offering much of a benefit
the most straight forward way to hoist functions into a class is by using @staticmethod for simply grouping functions together
@verbal escarp
Thanks! I was only planning to use it to practice classes. Right now the way my code is, each operation has its own tkinter UI in a function, which then calls a nested function to do the math calculations. I will take your suggestion into account. Is @staticmethod different from decorators? Coz the syntax looks like decorators.
I will look that up. Thanks for the tip. If you get a chance to look at tonmiles.py I'd greatly appreciate any more pointers you have.
Yes. 2 files in it. One functions.py which has some calculation functions. The calculations are related to oil rig stuff. But the tonmiles.py is where all the tkinter is.
btw, this doesn't look like a toy program, is that actually deployed?
Deployed as in? I haven't bundled it in an exe yet but it's good to go
Works perfectly
on an oil rig?
Calculates tonmiles for the day and stores data in a csv file
Fwiw in python it's pretty rare to use @staticmethod
Not never, but it's not usually the first choice
true, but it's a bit underrated imo
Why's that?
because, as i said, it avoids the temptation of using self for side-effects
while keeping the benefits of bundling
Yes. This brief thread will explain what the program does. If you're interested.
https://twitter.com/zubin_madon/status/1413821733732175872?s=19
The rig floor on an oil rig is a dangerous place to be. 100's of tons of steel goes up and down the length of the mast at breakneck speed. Roughnecks work standing directly under this, tripping pipes in and out of the hole, in 12 hour shifts. #100DaysOfCode #thread. https://t.co/IJgErB5Ybv
very cool 🙂
That's their job
Classes are for managing state
Idiomatically it's somewhat common to see class or static method for "named constructors" but outside of that I rarely see them
Just use a free function in the same file as the class
Java made these mistakes so that we could avoid them 😃
@verbal escarp I used a lil decorators to clear the frame of widgets when toggling from one functionality to another. But I haven't updated that bit on git. Right now I just call the clear widgets function normally within the other functions.
i respectfully disagree. it's not their sole purpose, but i'm not really in the mood to get in a fight over that 😉
Now I'm super confused if I should use hem for this program or not
In a decent size program there's going to be state to manage
So probably there will be good opportunities to use classes
there are different approaches and different people will advice you to do different things, take the advice and make up your own mind 🙂
But honestly don't use them just to "group" stuff
Well, you know, or start with what's consensus and idiomatic for a particular language :-)
You could look in the standard library or in major python libraries and count static methods
Compared to free and member functions
And those numbers will help you arrive at conclusions 😉
it's debatable whether it's idiomatic, i've seen all each approach in various cases
there are arguments for and against each
Like I said, I would simply suggest counting
counting doesn't help for specific cases
No, but it will give you a sense how high the threshold is for it to make sense
So this is what the program is. Each tab drilling, tripling, casing displays a whole new set of widgets on the center frame
really cool, i like it
As you toggle the previous widgets get destroyed and new ones appear. Then you add parameters and click calculate button
looks very oil rig ;D
So it calls a calculation function which is nested within the drilling function which had all the UI widgets
If in every library you look at, static functions are only a few percent of functions, and in your library they're 50 percent, unless you really know what you're doing, you're probably headed down the wrong path
Then the calculations are done and thrown into the bottom frame where the totals for the day are.
Then you click commit days data to csv which triggers another fucntion that does the pandas work
Is that for me?
if you're just using the decorator to namespace a majority of your methods, you can always stick them in another file and achieve the same result
A general comment, I havent looked at your code so I'm not saying you're using static method that often
Well my aim to refactor was to just practice classes. I assumed that a program which has so many different fucnctions and yet uses the same tkinter UI could use classes. Like tkinter could be a parent class where all buttons and widgets exist
Mostly I'm just saying,functions are the simplest and most common abstraxtion in python. Classes are great too but they are more complex, it's fine to use them of course but you should know why you are using it, and have a good reason
It's tricky because when you approach it as "I wanna use classes" it'll be easy to overuse them :-)
I commanded an oil rig till feb. No job now so decided to learn coding.
I do disagree with this argument tho, i'd suggest looking at common libs used as well as the std is a proportionally small codebase
For classes I would probably start by considering related state, rather than related functionality
for comparison, django has 61 static methods and 1600 free functions
Like, thesrntwo variables are intimately connected, when I modify one I want to also modify the other
@cobalt agate btw, try to avoid * imports, they mess with IDEs and your head down the road
Related state... Can you look at the UI screenshot and explain
there's a time and a place for them, just not for namespacing ¯_(ツ)_/¯
actually isnt there a namespace metaclass somewhere?
where
I thought typing had something similar but I cant find it
types.SimpleNamespace?
typing/types same difference 😄
Whatcha mean by this? 😂
at the same time, UIs are generally where classes make sense
esepcially considering your program starts with 6 globals that would work great in a class
i meant for people to use, not just as a toy, but you answered that question already
@flat gazelle agree that members are better than globals
But then you would need instance methods
Yes those globals are coz functions are tossing them around. All the functions drilling, tripping, jarring etc use some common values.
Not static methods
can you help me check the code and see if its open source and no one can steal my informations when I use this
https://github.com/snipesmarter/smart-sniper
I once made a horrible namespace hack that allowed you to do ```py
@namespace
def foo():
def bar(x, y):
...
baz = ...
class spam:
...
```, and it would make foo into a namespace with names bar, baz and spam. Can't find the source now, but you can imagine how hard it messed with editors
yeah, using static methods is definitely wrong here
half of these functions use shared state
So you suggest instance methods and classes...
@halcyon trail i can easily think of classes that have no state at all but also don't make use of staticmethods 😉
@halcyon trail for instance abstractions via properties
One question would be why the heck not using a class, and perhaps automatically make methods static
yes,
drilling_miles_total = 0
tripping_miles_total = 0
casing_miles_total = 0
liner_miles_total = 0
jarring_miles_total = 0
total_miles_result = 0
```all of these would make sense as instance attributes
that would not be as fun!
Haha
and yes, that is cursed and #esoteric-python
whether you want to move all the UI elements into a class is up to you
Yes, there are all kinds of uses if classes without state
Metaclasses too!
But that is more advanced
by state you mean mutable state, or just storing some parameters?
The starting point should be that classes are for managing state, that's their original and most common purpose
Thanks. Can I just make button, textbox, label in one UI Class and then call it in child classes drilling, tripping, jarring and keep changing their wordings using .config?
@grave jolt mutable or not
I would have one class that handles all the math related stuff, and then optionally a second one that handles the UI by calling into the first class. Whether it is viable to refractor this into that state I don't know, but interleaving logic with UI is generally considered poor form
This is going to be a very big learning curve.
yeah, going from passing around globals to full separation of concerns takes a while
like does it send the email and password I put in the cmd will be sent to someone?
@fast smeltthis project is in violation of MS and mojang TOS I am pretty sure, so we can't help with it
So one calculation class with various functions to calculate drilling, tripping, jarring miles. And then each drilling, tripping, jarring class calls whatever function it needs from parent
oh ok where should I ask that?
in game development?
not on this server, see rule 5
Thanks @flat gazelle very helpful pointers. Now the herculean task of putting all this in practice.
I would invert the control here. The GUI should ask the computation class for the information it calculates
the other way is also possible, but keeping the computation class clean of UI allows you to write tests
The tkinter button has a command kwarg which triggers a function. One of the calculation functions. However the calculated result has to go in a tkinter widget again to be displayed. So the calculator function will either have to have a label.config call in it to change the widget, or it must return a value which some other function uses to change the result widget.
So it is hard to completely separate both
let it return. that's how it's separated.
essentially the function should act like it doesn't even know about tkinter.
I don't think a return will work here alone
how about making a callback that will run the calculation class, get the result from it and display it where it is meant to be seen
this callback is part of the UI
This is a lot to chew on. Lol.
basically, think of it like an onion. your topmost function/callback whatever is responsible for being invoked from the UI. then, it talks to your class/functions inside to do some work. anything inside doesn't know about the stuff outside. so, the UI becomes the topmost layer, the function being called from the UI is just sitting underneath, and your core business logic is inside.
I guess it will need 3 steps.
Button(command= function1)
Function1() :
Calls variable = calculator()
Puts variable in label.config(answer is 'variable')
so, the outer layers use the inner layer, sure. but the inner layers don't need to know about who called them or why.
And calculator function returns result.
Thanks a lot everyone. You've given me a lot to think about.
Appreciate it v much. If anyone is bored enough to work on this together plz msg me and I'll be glad to work around your free time :)
you're welcome, don't hesitate to stop by if you need more advice/opinions 😉
What dunder do you need?
__getitem__
there's a __class_getitem__ coincidentally
if you don't mind me asking, what will you use it for?
if this is for typehinting you should probably inherit from typing.GenericAlias(?)
its for a codewars kata, i dont do crazy stuff like this for my personal projects 😅
is there a way to get the falsy value given an instance?
eg:
f([1, 'a', (1,2,3)]) -> []
f(3) -> 0
f('hello') -> ""
For those specific types, calling the constructor with no arguments will do it. list(), int(), etc
@unkempt rock yeah i was wondering if there was a way to get the falsy value from something though, i guess you just have to check a bunch of isinstance ? and return appropriate from that
got bitten hard by assuming empty instances always default to falsy
i was just curious really - well - i made something that would split lists into n chunks, then i wanted to pad the last sub-list (which would have fewer elements typically unless len(list) was divisible by n). I thought maybe I'd pad with the falsy value of the list def f(input_list : List[T], n : int) -> List[T] was what i'd typehinted with...
then i realised i wasn't sure how to do it
using type( ... )() makes sense tho : )
That isn't obeyed by any arbitary object though
class Foo:
def __init__(self, name: str):
self.name = name
``` for example
yeah - think i'll just use "NA" and handle from there, i was only really messing about anyway and just bumped into it 🙂
padding with Nones might make sense
yeah that'd be fine
Just out of curiosity, what's the reason to pad? Will later code assume that the lists have fixed length?
wanted to be a table
The downstream code will either have to watch out for shorter lists, or for None-padded lists
I see
Personally I would probably have the chunking function not pad the list, and handle that issue further downstream
use case is pretty basic - it's mainly just for quickly looking at what columns are in some data
But maybe the opposite makes more sense
I see
Usually use a lot of pandas stuff for that sort of thing
yeah if you have a df with a lot of columns i'm not sure what the most straightforward way to see them all in a table is
Pandas data frames have a bunch of methods to help with this
like what?
i'm aware pandas dataframes have a bunch of methods, but i'm not aware of a method that will enable you to see all 200 columns in a table easily
I mean just doing print(df) will give you a reasonable representation if it's not too big
I'm confused, the table is just the visual representation of a data frame?
no - it's only the columns, just to have a quick gander at what the columns actually are
sorry - only the column names (that was confusing)
But just the columns are a list, not a table, right?
Do the column names have some additional organization
yes, well a pandas index, but a list yeah
right - so i make them into a table so that i can see them all quickly
So basically you want to print a long list in a readable way by breaking it up into sub lists?
yeah that's all, i mean - i have something that does this, and use it - i was just messing about when i thought of the type thing previously
def ltt(*, lst, cols=7):
ar = np.array(sorted(lst))
rows = int(np.ceil(len(ar) / cols))
ar.resize((rows, cols), refcheck=False)
return pd.DataFrame(ar).replace({0: ""})
this is what i usually use - maybe there's a more obvious approach idk
Could look at pprint as well
pprint doesn't do it really
Because they're not aligned?
just doesn't give an output that's nice to read like the above
what does ltt stand for?
list to table
why not spell it out?
why spell it out when i know what it means?
presumably you posted it for others who won't know what it means
I guess I was confused for a while because table usually implies some relationship between rows and columns
oh sorry - i thought it was obvious from context, didn't think about others than quick reading it there
Whereas this is an aligned list
that's just a copy paste, didn't type it out
I think I see now though
oh, right yeah, that makes sense sos
In [10]: lst = ["".join(np.random.choice(list(string.ascii_letters),3)) for _ in range(50)]
In [11]: ltt(lst=lst)
Out[11]:
0 1 2 3 4 5 6
0 BTU BoO Bsx Cta Cut DHT EGR
1 Eah HAn Hiy JDG JJz KVg LDY
2 MuF MwN NYG PrB Qna Tdp URO
3 VLa VyN XeB ZHk aCL dCH eCO
4 eNj gkZ hSl hYx hcb iWU jAA
5 ldK mNf nkT oIU pMO qUc qzv
6 rPZ rdD ruz utZ wJm yaz zAL
7 zlK
in case it was unclear, not important though
i find it helpful sometimes, but only use it during eda
But why have the numbering of rows columns out of curiosity
I can understand the alignment for readability
In context of our discussion a few hours earlier, Composition Vs Inheritance: https://youtu.be/wfMtDGfHWpA
@verbal escarp @flat gazelle @halcyon trail @visual shadow the person in the video makes a compelling case to NEVER use inheritance. Would you agree/disagree with his rationale?
💖 Support the show by becoming a Patreon
https://www.patreon.com/funfunfunction
This is a weekly show where we try to become more confident and excited about programming by learning intriguing things that we didn’t know before. Today, we are are going to talk about composition over inheritance. Inheritance is when you design your types after wh...
disagree.
never is a strong word.
however, there is benefits to preferring composition. haven't seen the video, but GOF recommends composition too, if that's any benefit
i'd prefer composition in most cases, but inheritance has its values
In the video he seems to conclude by saying that anything you do with inheritance, you can do with comp and with a lot less future complications.... Honestly I'm not advanced enough to take sides here but I thought his take was interesting nonetheless.
hm.. it's not that simple. in python syntax and functionality revolve around protocols, often one or more __method__that is accessed to implement stuff
so, if you want to check if an object can be multiplied, you could check for __mul__
but that quickly becomes cumbersome the more methods are involved to implement a protocol
there you can use classes to signal "yeah, you could check my methods or just believe me when i say i'm a context manager"
so instead of checking what an object can do you'd check what an object is
of course, you could still add or remove functions in python independently, but let's try to stay civil ^^
so yeah, if the example is as simple is "can bark, can poop, can drive" it's easy to check for those abilities
but if you look at GUI frameworks especially like PyQT, there can be hundreds of methods
well, you could argue that an interface that consists of hundreds of methods isn't a good idea in the first place
it's not a "good idea" but it's the reality
not saying it isn't 🙂
inheritance works out nicer if you need a subclass to only provide one or two methods, whereas with composition you can't really have a model of "A is B, but C". You can still express the same things, but composition can end up messy as relationship between traits grow in hard to document ways, where inheritance tends to be a simple tree (even if python will let you have other shapes in it).
Entity Component Systems are a good example of where composition thrives tho, in a sense you need the messy relationship that data-driven systems provide because any sort of rigidity can end up meaning more complexity/redundancy
yes, I would argue that composition is overall a nicer way of modelling data and behaviours, but general truths lose to specific circumstances
@cobalt agate it really depends, it depends on circumstance
It is true that in general, implementation inheritance is far more discouraged now
Even OO languages are discouraging implementation inheritance
And it's also true that python doesn't really need interface inheritance the way that statically typed languages do
So, overall I do use inheritance in python very sparingly
yeah, most of my code is composition or even just full on dynamic polymorphism without a common protocol
When I use polymorphism I usually have a common interface
But you can do that with protocols, you can also register classes into ABCs so it's not quite the same as inheritance
Etc
pretty much
def selected_tile(self, selection):
if isinstance(selection, int):
return self.sides[selection]
elif isinstance(selection, tuple):
return self.board[selection]
```is what I do most often
I do still use ABCs and inheritance of them in python but they aren't required
This might be useful in my situation.
Compared to Java, C++ etc where using interfaces is basically mmandatory
Yeah, it might be
Honestly I maintain a good amount of python code and it's mostly extremely simple
i'm looking forward to using python 3.10 by default, i think match/case would work nicely with this pattern
Mostly functions and data classes, some normal classes, even fewer inherited classes, etc
I've probably used reflection in python about as much as inheritance 😃
Reflection actually solves real problems that you can't easily solve another way without a lot of boilerplate
yup, it certainly allows taking advantage of unions
same
or well, I have used inheritance to use certain libraries that require it, but for actually modelling my own code, very rarely
It will be cool if more languages have delegation as a first class feature
Achieves the things you often want to do achieve with implementation inheritance
Without the pitfalls
I also really recommend googling and reading about the fragile base problem
Its one of the main reasons for the fall from grace of implementation inheritance
yep. Right after I figure reflection which Im doing rn.
prototypes are another approach
If you want to reflect over data I recommend starting with dataclasses
That's been one of my main usages. Having code that can serialize any data class the way I need it serialized, using reflection, it's very nice
And much more organized, just poking into raw dict members can be a bit hazardous
there are also libraries like dataclass-factory that already implement the serialization
sometimes inheritance makes a lot of sense, especially where there's an abstract base class, you can build some functionality off of, and where the objects are expected to conform to a given interface imo
I deeply disagree, but you are completely justified in having your own opinions and preferences
as is everyone
what do you think the alternative is?
You'll have to forgive my lack of familiarity with Python specifically, but here is my personal opinion there: I generally find composition much more expressive and less brittle than inheritance
so...interface inheritance?
what I mean by this is: rather than breaking your domain into a rigid hierarchy, where often you can only inherit from one ancestor directly, it's often more intuitive to define relationships in terms of what that particular class does.
In languages like Java, this is done using Interfaces
yeah
because I believe that's what they were suggesting
there is no distinction between abstract classes and interfaces in Python
apart from actual usage
I would take a somewhat extreme stance on that, if Python abstract classes are similar to Java classes
Specifically, Java abstract classes allow you to share instance state with implementers
they can be
there are no guards on internal state in Python
or perhaps it is more accurate to say
there is no internal state except by convention
and because of dynamic typing, abstract classes are not really necessary, even
however, there is definite use of abstract classes to do what "interfaces" or "traits" would in other languages
My ideal system wrt interfaces is one which doesn't rely on any internal state whatsoever, and one which doesn't have the option of inheriting any state as in Java abstract classes
yes, that's the idea
but you can't enforce that in Python
no final keyword? too bad haha
well
I apply that very liberally in my java code hehe
you can kind of fake it with sufficient metaclass power
but it's not really part of the language's paradigm
I always flinch at the metaclass stuff haha...
I kind of look @ it this way
either you go hard on the static typing
or hard on the dynamic typing
Python is the latter
yep
makes sense, I personally find it not so comfortable, and don't program much Python as a result hehe
yeah, that's fair
I do a lot of Rust, which feels like a very static Python
mhm
still some pain points, particularly with async for me, but it's pretty nice
that may be a little simple for #internals-and-peps, but is there some standard Python way to print out thorough string representations of objects?
Not so much str(), but like an "everything and the kitchen sink"
possibly some kind of __blursed__ function which enumerates all the properties of a variable?
dir(x)?
it's called #internals-and-peps because it's for talking about the language itself, not because the questions here are particularly "difficult" (though they can be)
and, yes, dir(x) would be good
you might also want __dict__
dir? seems odd that would do what I'm looking for, but I could try.
Wouldn't be the first time something wasn't named what I expected
this will, of course, not work for dynamic properties
I kinda figured hehe
but I believe that would be equivalent to deciding function equality
someone with more theory can correct me
right, that makes 100% sense to me
Yes. It's fun to mess with people who try and debug your code.
lol
wow, what does it mean when uvicorn is saying source code string cannot contain null bytes?
I don't remember adding any
well, there's the pprint module
that's a good idea as well
if you want to see everything though, you unfortunately have to write something yourself, probably using inspect.getmembers(object)
least, if there is something out there, I'd like to know what people use - help(object) is also handy
thought I'd ask if there was some kind of python library already which does this
ideally repr() does this but it's inconsistent
but for example dataclasses have great reprs
also to quickly access a __dict__ in a prettier way use vars()
also fun fact about repr you can get a repr in an f string with !r like f"{thing_to_repr!r}"
yes, you can override __dir__, but that's not the same as enumerating all possible dynamic properties
I'm thinking about making a class which can't have instances made of itself.
do you think i should install hackintosh
sounds related to uninhabited types
why do you want to do that?
that wouldn't accomplish anything though
what does this line of code do?
currentNode.right.remove(currentNode.value, currentNode)
is it equivalent to below?
self.remove(currentNode, currentNode.val)
or this?
currentNode.right = self.remove(currentNode, currentNode.val)
do you have the implementation
yea
Hey @unkempt rock!
Uh-oh! It looks like your message got zapped by our spam filter. We currently don't allow .txt attachments, so here are some tips to help you travel safely:
• If you attempted to send a message longer than 2000 characters, try shortening your message to fit within the character limit or use a pasting service (see below)
• If you tried to show someone your code, you can use codeblocks
(run !code-blocks in #bot-commands for more information) or use a pasting service like:
it's 2000 lines long
your binary tree is 2000 lines long?
also, this isn't a help channel. try #algos-and-data-structs
my question isn't about binary trees
it's about how OOP aspects of the Python language work
a.b.c() is equivalent to tmp = a.b; tmp.c(), so IG the former
I see thanks
but how do I call a function/method like a.c() if it's inside a class
so I can only call it with self.c()
you can call methods on any instances of a class, even outside the class itself
I mean I'm trying to use a method inside another a method
like
class Classy:
def __init__(self):
self.val = val
class B:
def hello(self, d -> Classy):
myobj = d
d.val.goodbye(g) # except this doesnt work
def goodbye(self, g):
code
Lol, I've seen people say that it's an antipattern and you should use inheritance instead.
attempting to model this with OOP would create a cyclical dependency, so it is not the way to go
well, if d is an instance of Classy, then it doesn't have a goodbye method, other than that, it is fine
yes, I'm trying to use a method in class B inside another method that is inside class B but I want to call it on an object created from a different class
what about self.goodbye(d, g)?
!e
class Classy:
def __init__(self, val):
self.val = val
class B:
def hello(self, d: Classy):
d.val.goodbye(10)
def goodbye(self, g):
print(self, g)
b = B()
classy = Classy(b)
c = B()
c.hello(classy)
@flat gazelle :white_check_mark: Your eval job has completed with return code 0.
<__main__.B object at 0x7f45d995cfd0> 10
oh yea I'm already doing that in my code, still doesnt work
I've tried
self.goodbye(d, g)
Also, you can probably take the underlying function from a method and bind it to another object, and then call it...
But I wouldn't consider that a good idea.
What effect do you want to archieve?
oh, you want to call a method with self not being an instance of its own class
that seems like a bad idea
I'm not allowed to modify the original class
well, the way to do that is
B.goodbye(d, 123)
if you access the function on the class itself, it will return the original function without binding it to an instance
yea that's the same as doing self.goodbye(d, 10) I believe
which still doesnt work for resolving my error
I'll just post the full code #algos-and-data-structs
thanks for the OOP info tho
dumb question, what obje.att2.goodbye() actually do?
class Classy:
def __init__(self, att):
self.att = att
self.att2 = None
def hello(self):
obje = self
obje.att2.goodbye()
def goodbye(self):
obje = self
return obje.att
I thought what it does is just call self.goodbye()
but I guess I'm wrong and it actually does self.goodbye(objce.att2) but it's not that either since when I run it doesnt work
nvm it does actually work when I changed the code to be
class Classy:
def __init__(self, att):
self.att = att
self.att2 = None
def hello(self):
obje = self
self.goodbye(obje.att)
def goodbye(self, a):
obje = a
return obje.att
yea but when I instantiate the object, att2 is not None
obje.att2.goodbye() in that example seems to be self.goodbye(obje.att) (after changing the goodbye method a little)
sorry didnt mean instantiate, I meant after I change it
like
obj1 = Classy(10) # where 10 is att (self.att)
obj1.att2 = Classy(5)
I see
I'm having issues on anaconda I'm trying to install netcdftime but It keeps saying failed with initial frozen solve and errors with pip too any fix
What is the best gui module for Python?
@raven oar this is on-topic for #user-interfaces, but there is rarely ever a "best" of anything in software
you are more likely to get a useful answer if you state your use case and needs
I want to code multiple applications for not only windows
ive got a question in machine learning
so the sigmoid function compresses the values of the sum into the range 0 to 1
but you can use other activation functions, such as ReLU or tanh, whos ranges are (0, inf), and (-1,1) respectively
how do you factor for this different range?
do you normalise for example in ReLU by dividing by the largest activation in each layer
and like (A+1)/2 for tanh, to bring it into the (0,1) range
just realised theres an ai chat putting it in ther
@unkempt rock i see that you found #data-science-and-ml . but fyi this channel is for discussion of the python language itself
yep, just got a bit confused
but yeh ill keep that in mind for next time
and ty for the help!
docker,kubernetes are considered as?
Deployment? DevOps? Containerization?
idk these terms just seem to be popping more and more and they're kinda get me frustrated ahahhah
.c
c is pain to read
especially with that fun™️ wall of #includes
help()?
sys.modules?
i need this yesterday pls
what would you use it for?
something terrible i made
Can anyone help me code a advanced discord bot please
@mint sable This channel is for discussing the Python language itself. If you have a general question, ask in a help channel (see #❓|how-to-get-help) or in a specialized channel (like #discord-bots).
sorry :(
Pursuant to yesterday's discussion,
I refactored the app here: https://github.com/zubin13/Ton_Miles_Calculator_Refactored
I have only used one class. When I tried to make the Computation class, I was having a lot of difficulty calling functions/instances from buttons and getting data into widgets -- all of which reside in the UI class. Perhaps my understanding of Classes is still very premature to do it successfully. However I have gotten rid of the global variables. And all the calculation functions are static in one file. It is slightly better than having a whole program full of static functions calling each other.
If anyone would like to take a quick glance through main_ui.py and give feedback on the class structure, and critical comments will be most appreciated.
@visual shadow @verbal escarp @halcyon trail @flat gazelle many thanks. My eyes are burning from two straight days of staring at the screen in vain, trying to work classes into impossible tkinter functionalities, but I will love criticism.
On the bright side, I came up with some very good UX improvements. This version toggles frames instead of destroying widgets and creating new ones. So user inputs are preserved.
I mean, tkinter is pretty good for applications of this shape
i'd say pyqt would've saved a lot of pain with those ui details
What do you suggest... Kivy mobile app or flask?
drilling_miles_total = 0
tripping_miles_total = 0
casing_miles_total = 0
liner_miles_total = 0
jarring_miles_total = 0
total_miles_result = 0
formatted_date = 0
comments = ''
```why not make these in `__init__`?
Thanks. Will check pyqt
So the total keeps changing as each function keeps appending it. I thought being class variables would help, so that every time some function appends total, it remains same for all those accessing it
kivy, pyqt, gtk.. depends a lot on where you want to deploy your app
flask is something else entirely
you aren't making multiple instances of it anyway, right?
a web gui is not a bad idea, but different technology stack
Phones are banned offshore all over the world. At least camera smart phones are. So most people would use this app on a windows desktop. So which would serve best?
i didn't know that, why's that?
I'm calling on total and using block weight in multiple methods
Silly security concerns that make no sense
what operating system do you want to deploy it on?
self.a is shared per instance, all methods get the same self and thus the same self.a
Also the widgets are being updated with the total. I was worried it will change in one place and not others if I declare them as instance variables.
unless you make multiple instances
personally, i think tkinter is nice to have for very simple programs like installers etc.
wouldn't recommend it for anything more advanced
I don't really think there is a better option, if you want to relicense to GPL, pyqt is an option, sure
Correct. I didn't think of it. I am not making multiple instances so this should be worked
pyqt has weird dual/triple licenses
Is this a better approach than the static functions, even if not perfect?
it's not just GPL
This is an easy change I'll do it. Any disadvantage of having it the way I do right now? Right now I'm calling each one as MainScreen.total
it's just more verbose, but it doesn't really matter
it would matter if you were making more than one instance, but that doesn't make sense to do anyway
it might be educational
it's just mildly confusing
@verbal escarp if I bundle it as exe using pyinstaller, any windows desktop can load it perfectly right? Without having python installed?
yeah, pyinstaller should work just fine for this
yes
given it's the same OS
if you build it on windows 10 and want to deploy it on win 3.11, it might not 🙂
I didn't understand this q.
essentially, I don't think there is a meaningful reason to redo the entire user interface in a different framework considering that it already works in tkinter
So then it's better than another lib where users have to install qt right?
uh..?
Oh! Yes just edu. Perhaps kivy just to learn kivy.
pyinstaller bundles pyqt etc. so users don't have to install anything extra
as far as I can tell, that violates lgpl. You must keep the dependency under lgpl separate from the non gpl application so that it can be updated indepentently
Correct. But as @flat gazelle pointed out, one of the others requires users to install qt searately?
they wanted to change the license
qt itself is under lgpl, so they can't do a whole lot
qt has different licenses
Ok
Qt Open source Licensing |
• Commercial license
• LGPL3 open source license
• GPL2 or GPLv3 open source license
@flat gazelle this class implementation is better than the previous static functions? Does it provide any advantage you think or not much?
yup, and none of those allow "just bundle the dep into a big box"
unless you pay the several thousand dollars for the commerical license
I find it much nicer just because it is obvious what variables are part of the math
i should warn you though, making pyinstaller work with pyqt isn't as straight forward as one might wish, although, once it works, it works.
I've just built this to make my colleagues' and contemporaries' life easier as compared to using excel sheets. No such program exists anywhere.
if you want to try that approach and you feel you hit a road block, you may poke me 🙂
I have successfully bundled tkinter apps. I built one to browse NFTs on Ethereum by searching contract addresses
tkinter is trivial compared
I would suggest against lgpl deps unless you are on a *nix systems. Those were built to be run on mainframes, so they already support a single shared dynamically linked lgpl library that can be changed out for a modified version
doing this on windows is not quite simple
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
There are 2 main hurdles I encountered in the past. Even though I have installed a certain lib in pycharm, while bundling using pyinstaller they disappear. The other one is sometimes images have to be changed to base64
Will this affect me while bundling my tkinter app using pyinstaller?
While we're at it, what is your favorite method for storing passwords and keys as env when using an IDE? In a way that it can be easily transferred when you bundle the program as an exe or deploy it on the web? I've seen 4 different methods so far. The method whereby one enters the env by right clicking on the . Py file in pycharm obviously won't work once file is bundled and sent.
Another one I've seen is adding it to the bat files. And yet another one by creating env file
I generally use a .env file
And that file will be hidden/protected when you deloy the program somewhere?
it is more or less impossible to store data on a users system and hide it from them with only your program being able to read it
I cannot thank you enough for your feedback. Although I'm nowhere near getting a full grasp of classes combined with tkinter, at least there's improvement in the app and 20+ hours of refactoring aren't a total waste.
Cheers!
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
I can't imagine this step being too simple with pyinstaller. But IG just providing the python source and a requirements.txt would in theory do.
@cobalt agate the thing is you said "I didn't use globals" but then you have a bunch of class-level variables
class-level variables are just globals scoped to the class
As lakmatiol suggests, I'd probably just make them instance variables
I'll do that and retest
instance variables of a class that cannot logically have more than one instance of it are more or less globals too, but well, the project needs globals since it has global state, and I think keeping them on the instance makes the most sense
Yeah, it does
You kind of want to "postpone" admitting that anything is a global. You want to write everything as though it were normal classes, as much as possible, and then at the last second "gotcha!" there's only one global instance of class AppMain or whatever
Namespacing is important
hey guys is anyone fimilar with telegram's api?
!pban 839670371896262686 spam
:x: User is already permanently banned (#42761).
When does CPython check if a string is only ASCII letters and numbers and underscores and under 20 characters to decide to intern it? It doesn’t seem to do it for runtime strings because this prints False:py x = "my_test" y = "_string" a = x + y b = x + y print(a is b) # it prints False
And it seems like it interns all string literals whether they meet those qualifications or not because this prints True:```py
def f():
x = "my test- string ãbç over 20 characters"
return x
def g():
y = "my test- string ãbç over 20 characters"
return y
x = f()
y = g()
print(x is y) # it prints True```
When does it do those checks?
x = "my_test_string"
y = input()
print(x is y) # it prints False if you input my_test_string```
For the string literals, those might not be interned, just compiled to the same object in the bytecode
What’s the difference, is interning only when it does it during runtime?
i don't know many of the details. interning implies adding the string to a global table of strings. Your f,g example doesn't require doing that
compiled bytecode has constants in it.
Alright, that makes sense. But why are the strings separate objects in memory in the 1st and 3rd example?
(though they might be each in their own compiled bytecode function?)
Even though they meet the requirements
getting the interned string for every string could be an unexpected runtime cost, you can try running it through sys.intern and seeing if that returns the same or a different string
!e ```py
import sys
x = "my_test"
y = "_string"
a = x + y
sys.intern(a)
b = x + y # same if it’s b = "my_test_string"
sys.intern(b)
print(a is b) # it still prints False```
@sand goblet :white_check_mark: Your eval job has completed with return code 0.
False
I don’t get it
@sand goblet calling intern() wouldn't change a or b
You'd need to use the result from sys.intern, it'll return the interned string instead of the passed in string (if it was already interned)
!e ```py
import sys
x = "my_test"
y = "_string"
a = x + y
a = sys.intern(a)
b = x + y
b = sys.intern(b)
print(a is b) # it prints True now```
@sand goblet :white_check_mark: Your eval job has completed with return code 0.
True
right, but that will work for any equal strings
!e ```py
import sys
x = "my_test"
y = "_string ãbç over twenty characters"
a = x + y
a = sys.intern(a)
b = x + y
b = sys.intern(b)
print(a is b) # it still prints True```
@sand goblet :white_check_mark: Your eval job has completed with return code 0.
True
Yeah
afaik python will intern names (global names, and the keys for modules, classes etc.), there's not much reason to do it for others automatically.
I used intern once to avoid making my own dict when I wanted to prevent some unnecessary memory usage from many duplicate strings
https://stackabuse.com/guide-to-string-interning-in-python
This says strings with non-ASCII letters aren’t generally interned. But the strings in this example get interned even though they have non-ASCII letters
But I guess that might technically not be interning
But that article seems like it’s saying that it is
I’m just trying to figure out when it does the ASCII check
I can’t figure out when it checks for that
@sand goblet keep in mind, this can change version to version also
```py
# importing the library
import numpy as np
import matplotlib.pyplot as plt
import math
def diff(x,y,cx,cy,txt,list,dict):
for i in range(len(x)):
diffrence=math.sqrt((x[i]-cx)**2+(y[i]-cy)**2)
dict[x[i],y[i]] = diffrence
# 2.23606797749979
#
# data to be plotted
y = np.array([-2,2,4,0])
x = np.array([-2,4,2,0])
k=[-2,1]
k1=[3,1]
print(k)
l1=[]
l2=[]
dictionary2={}
dictionary1={}
# plotting
plt.title("K Means")
plt.xlabel("X axis")
plt.ylabel("Y axis")
c1=plt.scatter(k[0],k[1], color="black")
c2=plt.scatter(k1[0],k1[1], color="black")
plt.scatter(x, y, color='b')
diff(x,y,k[0],k[1],txt="1 ",list=l1,dict=dictionary1)
diff(x,y,k1[0],k1[1],txt="2 ",list=l2,dict=dictionary2)
plt.show()
Do you think that’s what’s happening with this?
@sonic dagger don't paste a screenful of code in multiple channels
Alright I am sorry for the disturbance
It looks like (some?) constants are interned with the constraints you mentioned, don't think it applies in other places https://github.com/python/cpython/blob/366fcbac18e3adc41e3901580dbedb6a91e41a10/Objects/codeobject.c#L64-L72
Objects/codeobject.c lines 64 to 72
if (all_name_chars(v)) {
PyObject *w = v;
PyUnicode_InternInPlace(&v);
if (w != v) {
PyTuple_SET_ITEM(tuple, i, v);
if (modified) {
*modified = 1;
}
}```
What's a good use case for weakrefs?
The main usecase is caches, and things like that - you might want to store a cached computation for an object, but if the object is deleted you don't need that cached value - and you don't want the cache to keep it alive.
A WeakKeyDict can act like an attribute on an object, but it's external which is useful if it's implemented by another module.
They're also useful as a tool to prevent reference cycles, by making one direction weak.
Python doesn't have separate notions of weak references and non-owning references, I suppose?
It does, via the weakref module.
You make a ref object, then can call that to retrieve a strong reference to the object.
Okay, so that's a weak reference? Where's the non owning reference?
Well Python's GC knows about the weakrefs, and clears them if the object is destroyed. Is that what you mean?
no, I mean that weak references as I'm used to thinking about them can be promoted to owning references
a purely non-owning reference would not
that's why I suspected that python does not make this distinction
Yeah, you can't do anything with the ref itself.
that said in a GC language this idea of a purely non-owning reference probably makes no sense
the object you want to refer to could be destroyed at any moment potentially
so you need to upgrade your weak reference into a real reference first, which then ensures the object won't suddenly die on you in the middle of something
Yep.
it's not upgrading a weak reference, it's getting a new real reference
sure
How do I get awips cave installed on CentOS I tried it before but I keep getting errors
they exist in the CPython implementation, but aren't exposed to Python. Code in the CPython C API has the notion of a "borrowed reference", which is an object that you hold a pointer to, but have not incremented the reference count of. They're mostly used for passing things down the stack - arguments are passed as borrowed references, because you know that the object can't be destroyed out from under you, because you know that the stack frame that calls you owns a reference to those arguments and can't drop it until after its call to your function returns.
they're also used for performance purposes in some places with the most fundamental types, like tuples. PyTuple_GetItem returns a PyObject* pointer to you for the item you've accessed without incrementing its reference count; it's the caller's responsibility to increment the reference count or forget the pointer before anything could happen that might destroy the tuple.
that's what the "Return value: Borrowed reference" at https://docs.python.org/3/c-api/tuple.html#c.PyTuple_GetItem refers to.
In addition to using them to prevent reference cycles by making one direction weak, as TeamSpen210 said, the other place I've used them with some regularity is to perform cleanup. Sometimes you need to add a resource under some existing code, like adding a persistent TCP session underneath something that used to make a new TCP connection for each request it sent. Since you need to support accessing it without a context manager for backwards compatibility, you can register an atexit handler to destroy the session. Since you don't want the existence of that atexit handler to prolong the lifetime of the object if the user did explicitly close it, you can make the atexit handler hold a weak reference to the object, such that its job is to clean it up if it still exist when the app is exiting.
or something in that direction, anyway. "If object X still exists, do Y" comes in handy every once in a while.
When is EAFP applied?
there are times when it needs to be used, because checking for an error condition in advance would result in a TOCTOU bug. If you want to remove a file if it exists, you can't check if it exists and then remove it if so, because something else could remove it in between you finding out that it does exist and you trying to remove it.
so the only safe way to handle "remove file X if it exists" is to try to remove the file, and handle the "it doesn't exist" error if removing it fails.
@raven ridge thanks for all the info!
Your point about using purely non owning references for function arguments is very good
I was mostly curious because as a C++ dev of course non owning references are far more common than weak references, so I was just thinking a bit about it
def on_release(
key,
lock_for_dict_=lock_for_dict,
held_or_released_=held_or_released,
shown_keys_=shown_keys,
hasattr_=hasattr,
str_lower=str.lower
):```
Out of curiosity, does this make the function faster? Because I saw that it makes it have `LOAD_FAST` instructions instead of `LOAD_GLOBAL` instructions
Very slightly faster, yeah.
Looking up a local variable is cheaper than looking up a global variable.
You mean making a local reference to str.lower?
yeah through default arguments
What if you only use it a single time in the function, is it still faster?
Yeah, I believe it can be slightly faster. Although unless you need to change it, why make it a parameter? Edit: nvm I was confused 😄
Erm, I'd have thought it would depend how often the function is called? It's probably one of those optimisations that isn't worth doing unless you are profiling your code, and it's a part of the code that is used very often.
I'm imagining it putting it in the array it uses for local values, so it ends up not needing to look it up in the global scope when you call it. Because it's already there.
Yes. Unless you're saying you only ever call the function once.
Remember that default values are evaluated when the function is defined, not when it's called.
But the usual consensus is that you shouldn't do this. It's a micro optimization that, if code is important enough for, it's probably important enough to write in a C extension module instead
alright. It sounds like it's a goofy thing to do, thanks guys.
I've gained significant speedups from doing it in functions with a long-running loop or that otherwise make repeated calls
Apparently global lookup can be significantly slower than local. Not sure how that compares to a single object attribute or dict lookup
(or weakref lookup as per above)
I remember a while ago someone posted an activestate script that resolved all global constants statically and then generated a new version of a function's source code with a bunch of these _local_foo = foo bindings at the top
Supposedly it was a big speed improvement on whatever their workload was
It's not that it doesn't improve the performance, so much as that if it makes a huge difference to your performance you're probably using the wrong language - or at least the wrong interpreter.
https://code.activestate.com/recipes/277940-decorator-for-bindingconstants-at-compile-time/ hah, the author was raymond hettinger
@raven ridge https://paste.pythondiscord.com/jugefayona.py is this a good use of try and except?
fair enough, but often people find themselves stuck using cpython for one reason or another. a localized performance hack is often easier than rewriting in another language. and pypy being stuck on 3.7 might be a problem if your code uses 3.8-isms
True enough. I'm not saying "never", so much as "don't make a habit of it"
A to-3.7 code generator could be useful, with polyfills/backports and some kind of "un-walrus" heuristics for common cases
That or ask your employer to donate to pypy
If you profile and find that some inner loop benefits from it, and you don't have time to move that inner loop to numpy or numba or Cython, then perhaps it's worth doing the hack
This looks reasonable, but you have a logic bug i think. You already attempt the lookup earlier in the code, before the try
Oh nvm these are 2 versions
yeh idea 1 was the original which had so many checks
but if I try except it , it would have to check so much things anymore
I would question the value of checking if something is a piece or not
That's what type annotations are for 😉
try:
loc = board[new_y][new_x]
except IndexError:
#if not inside the grid
continue
try:
loc_color = loc.color:
except AttributeError:
#if obj not piece
continue
if loc_color == piece.color:
moves.append((new_x, new_y))
Could do it this way too
I really like keeping try/except checks as "small" as possible
guys can any1 help ??
It makes it easier to see if you did it right
wdym ??
from pytube import YouTube
link = input("enter your link here : ")
yt = YouTube(link)
videos = yt.streams.all()
i = 1
for stream in videos:
print(str(i) + "-" + str(stream))
i += 1
stream_number = int(input("select a number :"))
video = videos[stream_number-1]
video.download()
print("download done :)")
@vital nymph i was responding to another user. Please see #❓|how-to-get-help
oh thanks
why would you have multiple try and except tho?
@dusty ledge But I think your "idea 2" is arguably better because it's more concise. However maybe you meant AttributeError instead of TypeError
I would have multiple try/except if it wasn't very very obvious what the except's are catching
Overly broad excepts make for a really bad time debugging and testing
If a and new_a are lists with them same length then these behave identically, right? py a[:] = new_a #VS for i in range(len(a)): a[i] = new_a[i]
Yep
Thanks 
Am I reading the docs incorrectly? https://docs.python.org/3/reference/compound_stmts.html#function-definitions that's the described syntax for a function definition, and it mentions that decorators must be separated by NEWLINE tokens although
@print@print
def foo(): pass
``` is equivalent to
```py
@print
@print
def foo(): pass
are they equivalent? I would assume in the first one it'd try to do a matrix multiplication and use the result as the decorator
I was wrong
I could've sworn I noticed that behaviour the other day
@unkempt rock :x: Your eval job has completed with return code 1.
001 | File "<string>", line 1
002 | func: lambda = lambda x: x
003 | ^
004 | SyntaxError: invalid syntax
Callable
can someone pls give me a basic python exersise
just ast.AST then
yeah, then it's just ast: ast.AST
hy
!resources
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
Thx
Hey guys! Excited to see so much going on in the Python Community. I am part of a project called Fortior Blockchain, which develops blockchain applications using Python! We recently released a paper focusing on smart contracts on the Algorand Blockchain, which has its own SDK for blockchain development. If you want to learn more, read our paper here: https://vixra.org/abs/2107.0097. That being said, I will be happy to answer any questions anyone may have about blockchain development in Python!
Oh gotcha! I was hoping to put educational material on here, and not neccesciarily advertise. I can edit the message.
FWIW "use cases" of python is on topic
And i have to ask - why python? Wouldn't you want type safety guarantees, domain modeling with types, etc, in a blockchain application?
Looking for discussion on design by contract after PEP 316 from 2003. Here's a summary of my research on the topic and a proposal.
Algorand, the blockchain platform we are developing on, has its own SDK for Python. Additionally, we thought it was best to use because of its scalability with front ends, various packages(we used the cryptography rsa package for encyrption), and general effectiveness. One thing to clarify is that we were specifically focused on smart contracts here, not building a DApp.
How does it compare to viper?
It'd be nice to have these capabilities in python just like other ML derivatives. I'm interested primarily to transpile python ton ML derivatives. But there could be other motivations too.
In the end you have to leverage python to the extent you can and innovate where necessary
"other ML derivatives" sounds like its implying that python is an ML derivative
That looks cool! We used Algorand because we received a grant to develop on their blockchain, so our specific smart contract is a legal validation on the Algorand Blockchain. Viper looks cool though. It’ll be something to look into for interoperability between Algorand and Ethereum, which could potentially be written in Python as well.
I actually meant adding capabilities to python that are similar to other ML derivatives (rust, fstar etc) with a "pythonic" syntax.
Ohh
does any one know how to convert h5 file generated to tflite Im trying to detect objects using maskrcnn tensorflow please help
Hey I’m new to python and I wanted to ask can someone help me code a discord bot which tracks u if u login to a Minecraft server like hypixel
This might be a good question in #python-discussion, or the topical channel #discord-bots (where discord bots are made). You can also claim a help channel (see #❓|how-to-get-help).
You most likely want to use discord.py, check the pins in #discord-bots for a good guide. When you got your discord bot set up you want to look into how to use REST APIs, do some googling for that. Important note: Don't use requests with your Discord bot, it will block it and not allow other commands to happen while you are making that request
Hypixel should have an API for you to use, other Minecraft servers may not have that. Without an API there is no way of asking for the data you want.
oh k
I installed miniconda but I keep getting traceback error no module name python38.dll
Is there a way for a base class to enforce that a subclass call 'super' for a specific method override?
No, Python is not good at forcing programmers to do things.
Facts.. I like the ABC (abstractmethod) for interface classes, but I couldn't find anything like that for what I need
I suppose I could write a decorator for it.
why not let the programmer do what they do?
every class needs subclassers to call super(), really
This is a case where the child class should union a set with the parent class... I think to get around it.. I might have the parent class just have a private set.. and then have a separate abstract method for the child class, then in the parent just union them
so. specifically, a set of attributes that are to be excluded from serialization
where can I ask questions about the process of learning python?
parent class implements the method excluded_attrs
ok thnaks
child class has a few attributes to exclude.. so it overrides the method excluded_attrs and should union it's list with the parent
parent's .serialize() will just call self.excluded_attrs() and it just works. I have issues when child classes don't union with the call to super and the parent attribute does not get excluded
I believe some linter will warn about that
PyCharm is happy to let me not call super in that case :).. I feel like there's a pattern here I'm missing... I'll have to give it some thought
like aggregation
Composite..
you could do something along the lines of
class Base:
_exclude = {'b'}
@property
def exclude(self):
res = set()
for cls in type(self).mro():
res |= getattr(cls, '_exclude', set())
return res
class SomeChild(Base):
_exclude = {'c'}
print(SomeChild().exclude)
```but it may be too much metaprogramming for something that could be far simpler
Honestly, that's very much what I think I need. That way I write it once, child implementations (which are many will be simplified.
well, if you think this is the solution, go for it
It's along the lines of the solution I'm looking for, so definitely.. thanks
This is the way to do it. You force children to implement certain methods which you then call in the public method they inherit
Couldn't you just have the serializer call the parent and child exclude itself
I don't understand the language you're using
The way I do it is like the parent has some ABC method _foo that the children must implement, and is used within foo method that the parent provides and the child inherits
pretty much what lakmatiol wrote. He put it into another exclude function, I was just thinking of it being inline in the serialize function, but it's the same idea
that code iterates over the base and child and retrieves what to exclude itself
also btw what you're suggesting with foo and _foo is a well known pattern as well from other languages, or at least in C++, it's called the non-public virtual idiom
yeah, there probably isn't much point in having that be a property, you can just inline to loop there
non-public virtual interface idiom 🙂
and I mean, that's what the _exclude is, a non-public virtual "interface"
or better, a non-public protocol, since this is python
yeah, this is just fancier because it works for multiple levels of derived that each provide their own exclude
Any problem in CS can be solved with another layer of indirection (c)
with "normal" NVI that's been discussed since ages ago in C++, you just call _exclude, which has to be provided by the derived class, you can't do anything like loop over the entire hierarchy calling each exclude separately
I know, I was just explaining the difference between this and NVI
if you wanted distinct names for them, you would use __exclude, but I don't really like that
exclude should also be a class method or even a static method, not that it really matters
if you need it to have logic, sure
oh nvm I see
yeah, it could pretty easily be a classmethod as well
sorry, yeah, classmethod, it cannot be a staticmethod
Is there a book on these patterns but written in python
I tried to read gang of four but I don't know either of those languages well enough
be careful about trying to apply patterns from one language to another.
people treat the gang of four as a bible, but it doesn't all apply.
yeah, I definitely agree with that
like half of that stuff is basically "shit, we can't pass around functions"
but half of it is still really relevant
Someone said to me
not always easy to know which half
Patterns exist to make up for deficits in the language
But here we are talking about them in some abstract way
yeah, this gets repeat a lot in the young, proggit/HN crowd, IME
I think this concept is basically a lisp invention
Proggit/HNE?
because lisp macros have pretty closed to unlimited power (within lisp), so the point is absolutely any repetition in concept (almost), can be written into a library using macros
instead of being an "idiom"
so the point is "if your language was more expressive, your idiom could be turned into a library, but it can't, so it stays an idiom"
I think it's worth knowing about this line of reasoning, but it's pretty overblown IMHO
i think it's a fair statement that patterns are how you put provided primitives together to do things they couldn't do already. in that sense, they make up for limitations of the language.
In theory it sounds nice and there are definitely times where it's applicable but IMHO it's a bit ridiculous to make such a strong statement about it
sometimes the idea of a pattern could just be a trade-off between two different approaches. As an example that came up on this discord a while ago, and came up again in the cpp slack, the idea of parsing something and "pushing" vs "pulling"
"pushing" meaning that you pass the parser an object with callbacks and the parser calls the callbacks for you
"pulling" means that you ask for the next piece of data, which in python you'd typically write with yield, so typically i.e. a generator/iterator
In the C++ slack someone brought this up but under the names SAX vs StAX
and then of course a third option is just returning the data structure to the user, which this person called DOM
so, I mean these 3 things are "design patterns" in some sense, they are very language independent, even though their manifestation could be pretty language dependent. But, you can't really factor this nicely into a library, IMHO, the whole point of the 3 approaches is likely to take advantage of specific relevant things in each case, which it's hard to imagine working well in any generic library (even w/ lisp macros)
@halcyon trail but as an example, all that debate over the parsing pattern is because these are not parsing languages. If we were working in yacc, there would be no patterns to discuss, because the language handles it natively.
Well, yacc (as I understand it), isn't really a "parsing language" if you want to be technical about it. It doesn't do any parsing. You declare the grammar of the language you want to parse in it.
And then you have to compile yacc into your language of choice, and with your parsing strategy of choice
but if I'm misunderstanding please correct me, I haven't actually used yacc
it's between a language and a framework. You write a .y file, in its own grammar, so it's a language in that sense. The point is, it has the dynamics of parsing built in to it, so there's no need to discuss "patterns" of parsing.
It's a framework in that you write chunks of C code in your .y file, and they are invoked as the parsing progresses.
in a language with no function calls, there would be patterns of how to do something like function calls.
I think "patterns make up for deficiencies in languages" is strongly worded to get attention ("deficiency" is a loaded term), but I think it makes an accurate technical point.
Maybe I'm misunderstanding but it sounds like yacc is basically using the push paradigm
it calls certain functions as certain things are processed
Anyone know any good tv show/movie script generator
@halcyon trail yes, maybe it is, but my point is that when using yacc, you don't have to think about which paradigm to use, or construct one. you don't have to have a pattern, because the language has it covered.
Well, the language has picked one for you. So here it's kind of the opposite; the design pattern doesn't exist because the language doesn't give you the option
right, you might not like the choice the language has made. but the point remains: if the language has already handled a need, then you don't need a pattern for it. Patterns are for things the language doesn't already do.
I see what you're saying, although (maybe just my reading of it), it still tends to make it sound like a "bad thing" the language doesn't already do it for you
but yeah, I more or less agree
yes, the popular statement has been crafted to get attention 🙂
that is very definitely true
from typing import Any
class Command(dict):
def __init__(self, **data: None | bool | int | float | str | Widget | Element):
dict.__init__(self, **data)
# commands are just packets of data, with a few extra behaviours attached
class Widget():
# other stuff ...
def dispatchCommand(self, command: 'Command'):
...
Is this type of circular dependence something I need to avoid?
is it just because of the typing?
Yeah
No, just stick the imports under if TYPE_CHECKING with forward refs and you can ignore it
And if it isn't something I should avoid — whats the difference between this and the type I should avoid?
No, that is okay. The static type checker is kind of a compiler. It has several strategies to figuring out those kinds of typings.
You should avoid circular imports
If it had some actual functionality you usually wouldn't be able to go around that without postponing something (although the import machinery can deal with simpler cases), so that's when you may want to restructure the project a bit
In this case, I simply don't think it can be restructured 😐 Widgets are command targets and commands by definition carry widgets inside them. I guess I could just have commands carry the ID of the object. I'm already having to keep them in a registry elsewhere for other reasons
Though I do hear what you're saying
as long as it's typing, you'll be fine with what you had as long as the import is not executed at runtime
It's usually better for classes not to have a cyclical relationship but sometimes it's the lesser evil
hi
i had a question
How many elements are in m = [[x, y] for x in range(0, 3) for y in range(0, 2)] ?
6 I think
i see
You could run the code and see?
i did but its showing a error
and i typed it out and its showing a error
and my sheet says that it should not show one
python is weird
Can you show the error
Like screenshot is?
Better to copy and paste the error probably
Okay bet
Along with the exact code
Well this really clears things up. Between this and cefpython3 for OSX upgrading to OSX and Linux this week, things are starting to come togther
XD Its been silent on the matter for months, and right when I needed it the guy who maintains it made a post saying it'll be out this week 😄 😄 😄
That said LexPlexed,.you may want to ask in a help channel, I don't think your question is really suitable for this channel
It happens to all of us :-)
Oh my bad haha
👍
I've got another one for you
Some helpful person on here explained to me that big composite classes are usually something to avoid. One class, one function. And its been a really helpful rule to live by
by "one function" you mean "one purpose" and not "one method", right?
Yeah, one purpose
Anyway, my element class is meant to replicate most of the functionality of a Javascript element. To do this I usually spread this out over three base classes (to keep the element class from getting massive)
One responsible for writing the element out as markup (very much non-trivial); one to handle 'node' related stuff i.e. adding/removing/rearranging children and handling the parent-child relationship between elements; one to handle the dispatching of events
I want to know if this sort of arrangement is something you'd all avoid
hard to say without more details. I'd probably expect that to be two classes, rather than three - one handling "node" related stuff and events, one that knows how to take a node and serialize it as markup.
the first two seem to be fundamentally part of what it means for something to be a node (exists in a tree with parents and children, supports some types of events) and the other seems fundamentally different (given a node, do something with it)
In javascript itself, node and eventTarget are separate — but I can do it however I like, really
I think in this case its fairly cut and dry but I wonder if you could offer some general wisdom on the topic of composing a class from multiple bases. When is it good, when bad, what are the pitfalls
writing out an element as markup seems like it could be a function rather than a class; at least without more details; it's not really a stateful thing but just a transformation
I'm imagining it being a stateful visitor
It could be implemented that way, but probably the stateful visitor could just be an implementation detail of a function, unless there's some reason to be able to inject different behavior during the visitation
hard to answer about pitfalls of composing a class from multiple bases without knowing why it's being done that way to start; the more common approach is to compose a class using, well, composition
If I'm following along, you're discussing the merits of having the serialization be a method of the class itself. For reference, I've never had it alter the state of an element before so it'd just be an observer
well, godly actually suggested it being a separate class
so, that's 3 different approaches I guess; separate class, separate free function, or member function
I prefer separate free function sall other things being equal because they're just the simplest. And as a general rule of thumb, things like serialization ought to be able to be done using the public API of a class
+1 on it needing to be implementable using the public API.
If ti can be done using the public API that removes one of the strongest reasons for it to be a method and not a free function
things like indenting HTML output as you produce it are just easier with a stateful visitor than a stateless one, though - you can track the current indent level with an instance attribute, instead of needing to pass every detail down the call stack.
Yeah, I agree withat, all I'm saying though is that speaks to implementation, rather than the public API
yep. I'm with you that it definitely shouldn't be part of the node class.
class _StatefulVisitor:
...
def serialize(...):
s = _StatefulVisitor()
...
something like that is what I would imagine
sure, that'd work.
but it kinda depends what public API the node itself offers; whether it offers a callback (push) API or a generator/yield (pull) API
Gotta admit, you two have kinda lost me
Though I thank you both for the help 🙂
And this if TYPE_CHECKING thing is a big relief. I'm still working really hard to avoid these kinds of relationships, but when push comes to shove its good to know theres an accepted solution
This idea of a visitor pattern is interesting. I won't pretend to know what you're talking about — but the notion of an external object traversing the hierarchy has interesting potential
I've got another one for you guys if its not too much trouble
This framework I'm working on, it replicates the DOM inside a python runtime and transmits changes to Javascript, I think I've probably got through this before so I hate to bore
Anyway, in the past I've rigged up a JSON encoder/decoder that would automatically encoding or decode elements passed as data in messages to/from javascript
Not too complicated — just have elements encode to {'__element__': True, 'id': element.elementID}
But to decode it, I need to have a registry of all instances of element, by ID. I'm wondering what the best way to implement this would be and, if this is a poor approach to begin with, what the alternative would be
A dict?
Well yeah, a dict 😛
But like, would it be best to store it globally inside the module that defines the base element class, or as a dict inside the base element class, or as a global registry somewhere else entirely like in the programs config file
class Element():
INSTANCES = {}
def __init__(self):
self.INSTANCES[id(self)] = weakref.ref(self) # still not sure about using a weakref :|
I've got you guys' voices in my head as I work, always pushing me to do things the most pythonically. I want to get professional grade
@static bluff btw, re our discussion long ago about a parsing API, and taking an approach where you yield back data vs a callback based approach. Something very similar to this came up in the cpp slack when we were discussing approaches to parsing, and someone was talking about DOM vs SAX vs StAX
There's not really my domain so I wasn't too familiar with the terms in that context, but it pretty much maps directly to the discussion
DOM -> return a data structure with all the parsed information, all at once
SAX -> A "push", event-callback driven approach
StAX -> A "pull", generator-iterator driven approach
if you're interested
Thank you! 😮
well, ok, StAX is a little more sophisticated than a generator, a StAX is a generalized, lazy approach, whereas generator is a fairly specific (albeit useful) lazy iterator
I eventually ironed out most of the kinks in my lexer and I'm just letting it simmer for now
But I intend to have a compiler already waiting for when I get into compiler design in a few years 😄
Do any of you use bootstrap along with Python/Flask? Would it offer a significant advantage if my html and css skills are very rudimentary?
Would using bootstrap be a shortcut to avoid going deep into html and css?
Well, HTML and CSS are among the more forgiving of languages for sure, and I'd highly recommend knowing at least the basics if you're going to do any web work at all
That said, my preferred tool for web design is Webflow. Think photoshop but for web design. Awesome drag-and-drop interface that really takes the sting out of the front end stuff. Not 100% how exactly you'd set up the server on the back end but I'm certain its possible
People who are learning flask are typically concerned with the backend and not the front end, especially while learning.
You can definitely start with bootstrap.
but you still need to know the basics of html and css... Boostrap kind of just lets you skip mastering css.
Put it this way
You need to know enough html/css to implement bootstrap, but you don't need to be so good at CSS that you can design something as sophisticated as bootstrap.
I know enough to understand the tags and I know enough to work selenium and beautifulsoup to sift through them. Would that be enough? @crimson niche
My personal take is that there is not such thing as enough, that you'll always be thirsting for more and so you should
As to whether or not it'll be enough for decent-sized project, if I'm being honest, the answer is probably not. I've been programming for years and I'm only just starting to put out 'professional-grade' software, though I didn't get a formal education so that slowed things down
But thats no reason not to do it anyway. Trial by fire, baby! Once you sit down and start doing it you'll start figuring out pretty quick what you need to know, and then you'll learn it 😄
Its amazing to watch a program transform over time, eh?
!e why is this the behavior for formatting bools? ```py
print(format(True,''))
print(format(True,'5'))
@sand python :white_check_mark: Your eval job has completed with return code 0.
001 | True
002 | 1
Bool is a subclass of int, and it doesn't override format, but format '' delegates to str
these are equivalent to '{}'.format(True) and '{:5}'.format(True).
so an empty format string would call bool's str, and a nonempty one would call int's __format__. hmm
surprising behavior, is there any reason to not add an override for bool's __format__?
My guess is it slipped under the radar
!e format
@acoustic crater :warning: Your eval job has completed with return code 0.
[No output]
wtf format is a global function
never parsed that before somehow
other built in constants don't format their own name when given '5' as a format spec
why should True and False? that behavior isn't supported for constants, so use the superclass's behavior
Yup, it calls the format of int, which will pad it up to a length
is there any difference between .format and a formatted string (f" ")?
They have slight differences - str.format has its own syntax for indexing/getting attributes of the value first, while f-strings can just use regular code.
And str.format can be applied to strings you construct at runtime, since it's not a literal.
But they both delegate to __format__ in the same way.
!e format
@unkempt rock :warning: Your eval job has completed with return code 0.
[No output]
Pycharm is really making me tear my head out. Not loading my changes to index.html in Flask no matter what I try. Can someone help in #help-pear ? I've tried saving, reloading, restarting debugger every possible solution on stackoverflow.
is it considered bad practice to have multiple logging.basicConfig ? If i setup logging for each module, what am I missing?
Doesnt logging.basicConfig only configure the root logger
This function does nothing if the root logger already has handlers configured, unless the keyword argument force is set to True.
I would say yes, it's pretty bad form
That function is usually only called in main
only called in main of what, the module where there's if __name__ == '__main__' ? (not sure if main is overloaded).
I've no idea what logging.basicConfig configures really 🤔
looking through some projects it seems as though they don't use it at all
and just do logger = logging.getLogger(__name__) then use from there
maybe i'll just not config it 😅
You don't have to use it
yeah i can config it so that the printing output is nicer, but maybe that's missing the point of logging a bit
It's just a very convenient way to quickly configure the root logger which is what most simple python programs want
Well, it lets you set the filename, level, format, and a few other things at once
yeh i'm in a monorepo here so basically everything is in it, rather than a single package
Most of my scripts, I have something like if name == main: main()
In main() I parse arguments first
The log file and log level is often based on the command line arguments
So after doing the argparse stuff I call basicConfig
You don't have to use basic config but with rare exceptions it's mostly the end user who should be configuring loggers
kind of the end user and the developer here so it gets blurred sometimes
Most packages should just be doing logger = logging.getLogger(name) and then logger.info or whatever
Well the idea still applies to the code
right - i think i might stick with that then for now, then when something is settled change basicConfig somewhere that actually makes sense to use it
At any rate for 95 percent of my code the only config I do is basicConfig in main
Sometimes I've attached handlers to write to a specific file deeper in the code
yeah i remember doing something where it inherited from a logger defined elsewhere, it was ages ago tho and i probably did it wrong then.
Feels like no basicConfig is the safer option for now at least 🙂
It's not less safe just more convenient
Instead you'll just go through the multiple calls to set level, create and add a handler, etc
hm, i'll just do
import logging
logger = logging.getLogger(__name__)
logger.info( ... )
in a module, where are the multiple calls for level and whatnot going to be?
i meant safer in the sense of there being less that needed undoing later on really
This is good, many do it like this and you should as well.
This means that you will have a nice path relative to the main file of where the logging originated.
cool thanks, once i've moved print -> log i'll consider whether there's much for basicConfig stuff 🙂
You can checkout this server's repositories, they do exactly this (I know for a fact that Pixels does at least).
You can also take a look at libraries like discord.py, you should see that line at the top of almost every file.
thanks 🙂 I had a nose at the dvc source code... if it's good enough for them it's good enough for me 😅
When should I use isinstance vs type 
isinstance matches subclasses
@magic python in a module/package, yes, that's all you should be doing
that code, and basicConfig serve two totally different purposes. That code is to actually do the logging, basicConfig is to determine what actually gets visibly logged, what it looks like, and where it goes (stderr, a file, etc)
The alternative to basicConfig isn't that code, it's more like
logging.getLogger().setLevel(logging.INFO)
logging.getLogger().addHandler(logging.Handler(logging.FileHandler(...), format=....))
code like this, or like basicConfig usually only appears at the "top" of the application, i.e. in main or near it, because that's the point in the code that's best qualified to make these decisions. The whole point of logging design is that the packages can just log without thinking about any of these things, and yet the whole system is flexible enough to give top of the application detailed control over what gets logged
right sorry, when i said i was going to leave basicConfig for now i meant that i'd just use whatever it spits out as default rather than configuring it by other means.
Hello guys is there is way i can make live user input i m using rich library so that it update when i enter the password
@delicate ice #❓|how-to-get-help
Is the logic here good?
len_ = 0
for x in somelist:
if len_ == 10:
break / return False
if instance(x, class):
do smth
len_ += 1```
Specifically len_ part
So I won't have to do 2 loops?
1 for len And 1 for checking each?
Why are you checking it every iteration, if it’s constant?