#internals-and-peps
1 messages · Page 46 of 1
Ah
my settings for textgenrnn textgen.train_from_file(r'C:\Users\wow\textgenrnn\datasets\quotes.txt', num_epochs=250,line_delimited=True,new_model=True,gen_epochs=1,train_size=0.8,batch_size=3848,word_level=False,rnn_layers=4,rnn_size=256,max_length=30)
isnt gpt3 out @rich wharf
Python is a great language, but it lacks a lot of features that can be really important:
Good scoping. Why I can access elements from the global scope inside of a class but not inside of a function? This is really weird.
Static keyword. Sometimes, when creating a module, we need like private variables that are only available for the current file, like for example, internal code statuses.
Const keyword. Sometimes, we want to save data that we don't want to be modified and python lacks a feature for this.
Example with the static keyword:
static x = 10
If you try to access this variable from outside of its file an error will be raised.
Example with the const keyword:
const y = 10
If you try to modify y, you will get an error.
In [7]: a = 10
...: def f():
...: print(a)
In [8]: f()
10
i can access global variables in a function
you don't recall correctly then
you can always freeze classes by overloading __setattr__
Wdym freeze classes
In [11]: class Immutable:
...: def __setattr__(self, attr, value):
...: raise AttributeError(f"cannot assign {attr}")
In [12]: Immutable().x = 10
Traceback (most recent call last):
File "<ipython-input-12-fa2e8e6f6113>", line 1, in <module>
Immutable().x = 10
File "<ipython-input-11-b2dc29740f91>", line 3, in __setattr__
raise AttributeError(f"cannot assign {attr}")
AttributeError: cannot assign x
accessing globals inside a function is about the only part of python scoping that works normally :p
where normally == the particular c like languages i used in the past
scoping in python is a bit weird, but overall fine
i first learned with block scoping, its largely how i think. ¯(°_o)/¯. its not so bad with nice editor warnings these days.
yeah but why complicate yourself creating classes when they can just add a const keyword
Doesn't really make sense
because the convention in python is that private variables start with _ and that largely the responsibility is on the developers to not break contracts
const dict = {};
dict.lmao = "why? because js.";```
_var underscored variables also have special meaning
That is, if you write a module like this:
# mod.py
_something = 13``` and then ```py
# some.py
from mod import *
print(_something)``` you should get an error.
unless you define __all__ in mod
I believe single underscore is a naming convention (aka use at your own risk), and double underscore actually limits access (at least in classes, that is, not sure about modules)
double underscore will name mangle
You don’t need to name mangle often tbh
i just single underscore things that aren't important to class api
i accept the limitations of my experience (no xbox huge python systems, more a microservices guy) but obsessive hiding and consting of things often seems like a solution in search of a problem
I don’t like single underscoring too much and I do so if I am very unsure if something should be accessed and used outside of my API
i use it pretty often
lemme grab an example
like uhh, say you store the hash somewhere like my_class._hash probably you shouldn't change this
or when properties provide the api you want
class PlistImageSheet:
def __init__(self, sheet_location: PathLike) -> None:
self.sheet_path = Path(sheet_location)
self.plist_path = self.sheet_path.with_suffix(".plist")
for path in (self.sheet_path, self.plist_path):
if not path.exists():
raise FileNotFoundError(f"{path} was not found.")
self.cache: List[str] = []
self.name = self.sheet_path.stem # last part, without suffix
self.image = Image.open(self.sheet_path)``` here is an example of a class I have written around a day ago. what I want to say is that, well, why wouldn't user be able to access sheet_path or image?
though cache could've been underscored yeah
they could access it either way, it's simply a way to demarcate intention
yeah
This used to be python language right?
So I’m going to pretend it still is
What’s the point in having Boolean variables
I always use an int at 1/0 because when I did scratch as a kid they only had int variables
So is there any gain to using Boolean over any other data type
Well, True and False internally are 1 and 0 respectively
bool is subclassed from int
I guess so
I'd say the main benefit is probably readability, it strongly implies that you're going to use it as a boolean - either true or false, no in-between
While 1 and 0 can make it unclear whether it's supposed to be a flag or a number
True
i dont see what advantage there is to using an int.
might as well use a specific type that does what you want. forcing true/false into other types always seemed like a hangover from typed single return value languages. not applicable here.
How do we manage to create a python script that will go to a website ang login ang then click some button ? is that something really hard?
selenium webdriver does that , you can check the docs out.
Coming from C# i don't get what the Called means in properties:
´´´class person:
def init(self):
self.__name=''
def setname(self, name):
print('setname() called')
self.__name=name
def getname(self):
print('getname() called')
return self.__name
name=property(getname, setname)´´´
def __init__(self):
self.__name=''
def setname(self, name):
print('setname() called')
self.__name=name
def getname(self):
print('getname() called')
return self.__name
name=property(getname, setname)```
It just means you called the getter/setter method
Alright. And that's what I understand that @property Decorator is used to avoid calling the Property()?
This allows you to use normal attribute-like access, but call a getter or setter underneath
You can have a uniform API from the user perspective, but still use getter/setter logic
If you now access a person instance's name attribute, it will call the getname method to get the value
Likewise, if you try to assign something to an person instance's name attribute, the setname setter will be called
The print calls in there are just to demonstrate that accessing/setting the instance's name attribute will actually call those methods under the hood
to demonstrate the effect
!e
class Person:
def __init__(self, name):
self.name = name
@property
def name(self):
print("Using the getter to get the `.name` attribute")
return self._name
@name.setter
def name(self, name):
print("Using the setter to set the `.name` attribute")
self._name = name
ves = Person(name="Ves")
print(ves.name)
ves.name = "Ves Zappa"
@wide shuttle :white_check_mark: Your eval job has completed with return code 0.
001 | Using the setter to set the `.name` attribute
002 | Using the getter to get the `.name` attribute
003 | Ves
004 | Using the setter to set the `.name` attribute
From my outside perspective, I'm just interacting with the ves.name attribute. Abstracted away, in the internal API of the instance, it actually makes use of getters and setters
This means that you don't need to start out with getters/setters: You can always add them later without changing the public API
Thanks for the explanation
It also means you don't have to use "custom" getter/setter names you have to memorize for each class (was it get_name?, getName?, and so on)
C# seems way easier
{
private string name; // field
public string Name // property
{
get { return name; } // get method
set { name = value; } // set method
}
}```
In Python, you typically don't use getters/settes unless you really need custom getter/setter logic
Don't understand why Pythonians have to call variables attributes
There's no need to always create a getter/setter just so you're able to change the implementation later without changing the API
"attributes" is not a Python-specific term
It just comes from general OOP terminology
In Python, you typically don't use getters/settes unless you really need custom getter/setter logic
@wide shuttle I understand, but still important to know how to encapsulate, just trying to learn. Different thinking here :p
Also what I understand in Python, setting the attributes to pivate you are still able to assign values outside the class
Yes, Python's philosophy is different in that sense
What the hell is that good for then?
Sorry, just don't get the logic of encapsulation in python, you can still "break it", no compiler will yell at you
Python's philosophy is that we're all consenting developers. If you want to reach into a class and mess with private attributes, you can. Don't be surprised if you break things, though
The _ signals to you "This is not part of the public API, maybe it's better to not use it"
but if you want, you can
imo people that overuse private in languages that enforce it are doing more damage than people that underuse it
imo people that overuse private in languages that enforce it are doing more damage than people that underuse it
@gray mirage How so?
We've all been in a situation where we needed to touch the private parts of someone's code
It makes room for more robust applications
is that your experience or just your guess?
The idea in Python is you mark off the things that might break stuff - then if someone does touch it, and it breaks, it's their problem
Since you're just starting out as a Python dev
But if they thought of something you didn't think of, then it's super useful that they can touch it
Monkey patching and duck typing are both massive parts of python's utility
No, it is the words of my professor at university which probably have more experience in programming than our age combined. Not being rude, just saying
Everyone has an opinion, haha
Not trying to create a C# vs Python war :p It is just interesting to see the perspective of both languages
You're right, it totally is
I do a lot of java so I see where you're coming from
But I've also been in a situation where some Java library has made something private that I needed to use
At least in Java you can get around it with reflection, but I prefer the way python does it: "Touch this and if it breaks, it's on you"
But it just hit me that if Python doesn't truly encapsulate the data, protection of data from accidental corruption is not present at all
python is a language for consenting adults.
Also, lower coupling between objects and hence improvement in code maintainability
you can touch someone private parts as long as they consent to it
You generally don't need to worry about data corruption in a high level language like python
Probably not but bad designing is the result of none proper encapsulation
I don't think python really has issues with encapsulation either
I think that's too generalized a statement to make
I will say that stricter languages make it easier to write code that works the first time around, but you can still encapsulate things pretty well in Python
since python is 100% object oriented you are probably right here gdude
But on the other hand, Python is scripting, C# is used for big applications mostly, Python is seen more for AI, Data Science, etc.
Actually, Python does power a lot of bigger projects too
Absolutely, gdude
It's pretty much a driving force in the web development world at the moment
Standout examples being YouTube, Instagram, parts of Facebook and Dropbox
But you don't create desktop applications really with Python
Some people do
sure you do
We do have toolkits for it
Qt is pretty popular
Although we have nothing as nice as winforms but that's just .NET for you
Although I guess you could make use of that with ironpython
yeah, you have access to some of the .net binding with ironpython, but desktop applications is not the same as .net/windows appcliactions
Well, I haven't really heard of industry professionals use Python for creating desktop apps but probably is more common than I think. I'm just a cyber security student
I mean, you're using Discord, it has some python components
Even if it is majority Electron
but most desktop users are on windows, hence some languages fall naturaly into that category by design
but not as a concept
java and python both make robust desktop applications
They gave C# for comparison earlier so that's why I mentioned winforms :>
the age of desktop applications are not at its peak anymore imho.
Then again, they're killing winforms soon anyway
Yeah, true enough
It's all web these days
with screens with higher pixel density less then one, windows applications just break
and web tech is just, thats no problem, we have done this for years
i moved all my desktop applications over to the web many years ago, and i have only one regret, that i did not port our company main application over to the web
Yeah
Python is excellent for web work, as I mentioned earlier
That's probably why it's seeing such an uptick recently
having backend python developers and fronend whatever developers makes for a good match
I hate that Python can’t distinguish between declaration and usage of a variable. You don’t need static typing to make that happen. It would just be nice to have a way to say “this is a variable that I deliberately declare, and I intend to introduce a new name, this is not a typo”.
Furthermore, I usually use Python variables in a write-once style, that is, I treat variables as being immutable and don’t modify them after their first assignment. Thanks to features such as list comprehension, this is actually incredibly easy and makes the code flow more easy to follow.
However, I can’t document that fact. Nothing in Python prevents me form overwriting or reusing variables.
In summary, I’d like to have two keywords in the language: var and let. If I write to a variable not declared by either of those, Python should raise an error. Furthermore, let declares variables as read-only, while var variables are “normal”.
You can declare a variable without assigning anything to it in some contexts
I assume that's what you mean
Consider this example:
```x = 42 # Error: Variable x undeclared
var x = 1 # OK: Declares x and assigns a value.
x = 42 # OK: x is declared and mutable.
var x = 2 # Error: Redeclaration of existing variable x
let y # Error: Declaration of read-only variable y without value
let y = 5 # OK: Declares y as read-only and assigns a value.
y = 23 # Error: Variable y is read-only```
Notice that the types are still implicit (but let variables are for all intents and purposes statically typed since they cannot be rebound to a new value, while var variables may still be dynamically typed).
Finally, all method arguments should automatically be let, i.e. they should be read-only. There’s in general no good reason to modify a parameter, except for the following idiom:
def foo(bar = None):
if bar == None: bar = [1, 2, 3]
This could be replaced by a slightly different idiom:
def foo(bar = None):
let mybar = bar or [1, 2, 3]
wait are you jsing python
I don't think those things really go with the spirit of Python
this assumes that variables in python are something they are not
But you could always ask the mailing lists
It's worth mentioning that you don't "create" a variable in Python
i really dislike the use of variables as a concept in python
It's more like you're just giving a name to a piece of data
because variables have other features in other languages
Yeah but hard to find bugs
hence, i use the more correct term, name and value binding
python binds a name to a value, and the name is just a reference to the value in memory
in no point is the "value" inside the "variable"
a "variable" in python is not a box where you put the value inside, its more a sign, that points to where the value is located
!names
Naming and Binding
A name is a piece of text that is bound to an object. They are a reference to an object. Examples are function names, class names, module names, variables, etc.
Note: Names cannot reference other names, and assignment never creates a copy.
x = 1 # x is bound to 1
y = x # y is bound to VALUE of x
x = 2 # x is bound to 2
print(x, y) # 2 1
When doing y = x, the name y is being bound to the value of x which is 1. Neither x nor y are the 'real' name. The object 1 simply has multiple names. They are the exact same object.
>>> x = 1
x ━━ 1
>>> y = x
x ━━ 1
y ━━━┛
>>> x = 2
x ━━ 2
y ━━ 1
Names are created in multiple ways
You might think that the only way to bind a name to an object is by using assignment, but that isn't the case. All of the following work exactly the same as assignment:
• import statements
• class and def
• for loop headers
• as keyword when used with except, import, and with
• formal parameters in function headers
There is also del which has the purpose of unbinding a name.
More info
• Please watch Ned Batchelder's talk on names in python for a detailed explanation with examples
• Official documentation
At low level, python uses dicts to represent namespaces, so you are really binding a name to a reference to the value
Well yes, like most languages :)
anyone knows pymc3?
@lost dragon If you want to get help with a specific problem or question, please use #❓|how-to-get-help
@undone hare Well, dicts are used in global namespaces.
!e
import dis
def f(x, y):
x += y
return x
dis.dis(f)
@grave jolt :white_check_mark: Your eval job has completed with return code 0.
001 | 4 0 LOAD_FAST 0 (x)
002 | 2 LOAD_FAST 1 (y)
003 | 4 INPLACE_ADD
004 | 6 STORE_FAST 0 (x)
005 |
006 | 5 8 LOAD_FAST 0 (x)
007 | 10 RETURN_VALUE
Local variables are represented more efficiently
But if I wrote x += z, then it would change the instruction to LOAD_GLOBAL("z")
What's dis module? First time I see it
That's interesting
hey there guys
I recently had a problem with making exe file of a python program and a ui file
i made the .ui file using Qt creator and had a python related script too ... but couldn't find a way to pack it all in a exe or at least installable file
do you have any idea how to do it ?
thanks
@cunning crest have you tried pyinstaller
Or alternatively, auto-py-to-exe https://pypi.org/project/auto-py-to-exe/
old conversation I know but Microsoft has announced a new GUI framework for building GUI and Web similar to Electron, C# also has great web framework
Oh interesting, I didn't know that
I've actually been really really impressed with big M's push towards open source
They are trying
is textgenrnn even still revelant? i may try this https://github.com/minimaxir/aitextgen
also whats the best voice cloner that is open sourced
Is contextlib.suppress more elegant than a try except ?
@final whale That's totally depends. When you want to suppress, then contextlib is better, but when you want to do something when exception raised, try-except is better.
suppress it is, the use case would be to ignore an indexerror
Yes, then this is better
Hey, I have question.
is there any advantage using vars(obj) over obj.__dict__?
alright so - im working with pygame and i want to take the load off myself when creating levels
so i thought it would be a neat idea to create classes for each type of surface (i.e. walls, platforms) and have the code read from an image and assign classes by the color that they appear
for example, it would see all the white in this image and assign it as a solid object
that is the intended goal, but im having trouble finding out the correct module or steps to "reading" the image and grabbing the coordinates of the coloured areas
can anyone send me down the right lane for this?
I don't think this really belongs here, you should rather claim a help channel (#❓|how-to-get-help)
but I would recommend looking into PIL.
Reading the level from an image comes with a catch -- it's very inflexible.
If you'll want to change some attributes of enemies or items in the game, you won't be able to do that with an image.
I would suggest storing the level as JSON or something similar
@karmic storm
No difference in result, since the documentation of vars says "With an argument, equivalent to object.__dict__."
It might be a bit more readable though
that is the intended goal, but im having trouble finding out the correct module or steps to "reading" the image and grabbing the coordinates of the coloured areas
@jaunty tiger More complicated "Computer Vision” are done using neural networks
For what you are doing... I think lots of 2d for loops?
You could use that to traces out the x,y of the shape
And use that information you create your objects
There might be modules that do it, but IDK any
Is there a library or tool that can convert dis output (python disassembled bytecode) back to python source code?
did some googling, perhaps this? https://github.com/rocky/python-uncompyle6
@cloud crypt I was trying that, I have it installed. There is a switch option --asm that I think is what I'm after, but it isn't working. Asking for only a pyc or py file which I believe my dis bytecode output is neither
https://chriswarrick.com/blog/2017/08/03/gynvaels-mission-11-en-python-bytecode-reverse-engineering/
I solve a mission, reverse-engineer Python bytecode, and write code in Paint.
hmm just found this, will dig into it =.= maybe this is a difficult task
hmm so i was sent here, how would one go about making a feature req for something like ruby-style if/unless
if c: a = b => a = b if c
if not c: a = b => a = b unless c
cuz i feel like that'd add to python's elegance and make code read a little better
a = b if c is sorta already possible with a = b if c else a
but the latter is a little long-winded
well you can sort of achieve this with something like a = b if c else None and a = b if not c else d
yeah
I don't really like your suggestion because it's not obvious what happens when the condition fails
does the assignment then not happen?
yup
for instance you'd say
a = "three" if b == 3
or
a = "not three" unless b == 3
it's a construct used in Ruby
a lot of times these things a = b if c else None can be better written as a = b or c depending on what you're doing anyways
I'm not familiar with how Ruby works but having a variable be undefined after such a statement is a bit hard to reason with
well it wouldnt be undefined, really, and a = b or c isnt immediately clear
and a = b if c else None doesnt work if a is defined, so you'd end up having to use a = b if c else a
Assuming that foobar isn't defined yet, most linters would go the safe way and assume that it will not be defined most of the time
if condition:
foobar = 42
in english, you'd say "give me the ball if you have it", if they dont have it, you dont get the ball
and yea, the same could happen with foobar = 42 if condition, right?
x = 0
x += 1 if x.zero?
print(x)
x = 0
x += 1 unless x.zero?
print(x)
Personally, not a fan of having a name not be put to scope after such a clear statement, and it's not like handling for None is an issue anyways
The structure of the language would have to be changed or some weirdly parsed statement for that to work
how so? you'd just make the else clause optional, wouldnt you?
a = b if c I assume that by itself, can mean either a = b if c else a or a = b if c else None
I mean its not quite the syntax you are looking for but going back to your earlier example there are 1 line if statements, it just has a colon in the middle of the line ```python
if b == 3: a = "three"
a = b if cI assume that by itself, can mean eithera = b if c else aora = b if c else None
@brave badgera = b if cis clear when you read it out tho
all of these are far uglier than just using more than one line for it
It would require context if it's on its own
oneliners are a code smell
what about a = b if c else d tho?
Context whether a is defined or a is not defined
oh we talkin about syntax stuff, nice
you'd need to know if b is defined beforehand anyway
But a can be undefined, and if b is false, a is never put to scope
just like if you
if b: a = c
@unkempt rock doesnt the same apply here?
most editors tend to freak out if you attempt to use a after something like this
^
if it were to use a bare = then that clashes with normal assignment statement, that statement requires an expression (list) on the right side of it and the above can't be an expression since there is a case where there is no output
It's a pitfall depending on the context
i think that x += 1 if/unless conditional is a pretty neat, but eh
personally i'd love if python had a bunch of ruby things stuffed inside
but i can sense that a lot of people would not
i mean one of python's great points is that it reads very easily
i mean i'm not suggesting adding ! or ? to functions (tho that'd be neat)
optionals?
if it were to use a bare
=then that clashes with normal assignment statement, that statement requires an expression (list) on the right side of it and the above can't be an expression since there is a case where there is no output
@peak spoke couldn't it just be processed differently if the else clause isnt there?
uh no
no wait, it's different in ruby
in ruby ! and ? are valud function characters
right
One statement assuming two things on its own would generally be a bad idea
well i mean it wouldnt really assume 2 things, either the else is there or it isnt
a = b if cI assume that by itself, can mean eithera = b if c else aora = b if c else None
Was referring to 😅
ohhh
a = b if cis clear when you read it out tho
"set a to b if c"
in english, it's implied that if c is false, the assigment doesnt happen
But yeah, eliminating the else clause would mean that Python would look for a previous definition of a
i mean it would do that anyway if you had a = b if c else a which is the exact same outcome of my proposed statement, disregarding the a undefined case
conditional assignment in an expression is a bit weird
since i see = and immediately assume something's happening
a = b if c -> a is b if c, but if not c then look for previous definitions of a and if a is not defined, set to None
but that would be a = b if c else None
ruby has some uh, idiosyncrasies
but it's almost like engilsh, and as such, very easy to pick up
Setting to None would be more rational than not bringing the name to scope at all
honestly i dont really parse the english "readings" of syntax anymore tbh
since i see
=and immediately assume something's happening
true, but it wouldnt be apparent
Setting to
Nonewould be more rational than not bringing the name to scope at all
just like if you
if b: a = c
@unkempt rock
this doesnt bring to scope
and can you imagine conditional assignment if the variable doesn't have to exist yet
the linter would have a fit
i mean it already does in the instance i just quoted
If that's the case, then the one-liner won't bring to scope?
oh ye
and my idea isnt so much for if the variable is undefined, but if it IS defined
That would be quite vague
Question, what would be the behavior if a is undefined?
same as
if b:
a = c
linter freaks out
i mean it's literally the same statement
exhum, a = c or b
I honestly think that's a pitfall that most beginners find hard to debug
i dont immediately know that or construct and im not sure beginners would either
Also based on personal experience when I was starting out
exhum,
a = c or b
@unkempt rock also that doesnt work if c (or b) isnt a bool
does it?
I think it implies truthiness
and my second point - unless would be a handy keyword
unless a == 2:
b = "not two"
>>> True or False
True
>>> 1 or False
1
>>> 'a' or 'b'
'a'
>>> 'b' or 'a'
'b'
truthiness
now would that be more apparent to a beginner tho?
idk, personally not fond of the idea of not bringing names to scope
i mean it'd have to be used with care, just like if c: a = b
A beginner would be quick to assume that Python defines that name either way
if you're translating
if b: a = c
then shouldn't that be a = b and c or a
well you're doing the same thing when you do
if a: c = b the if statement evalutes to true or false
it'd have the exact same rules
if you're translating
if b: a = c
then shouldn't that bea = b and c or a
@merry pollen and would beginners know that?

Especially if you put it in one line
a = b if c would read a is only put to scope if c
technically it's basic syntax
just somewhat obscure
it's just "truthiness" plus "short circuiting"
kinda
It really depends on the situation imo, but i don't like the syntax of a = b if b else c sort of thing, becasue this is just a = b or c
but how many beginners would see that tho?
Technically, this snippet would teach beginners how to reason with how Python runs code (top to bottom, left to right) and how variable names are defined
if c:
a = 21
else:
a = 42
i mean the syntax is kind of ugly
but a = b or c doesn't give you the right value
i think?
maybe im jsut dumb
a = b or c => a = b if c else c?
it's situational, multiline if/else is probably the most readable and beginner friendly like what @brave badger wrote
!e *
You are not allowed to use that command here. Please use the #bot-commands channel instead.
shush, bot
A beginner would assume that in a = b if c, a would be defined either way, and not everyone has a linter
readability and intuition are pretty personal things
hard to really say equivocally which one is more intuitive
if c:
a = 21
else:
a = 42
Technically, this snippet would teach beginners how to reason with how Python runs code (top to bottom, left to right) and how variable names are defined
i think the breakdown of assignment operators always assign is like, maybe not so hot
from a like, brief glance -over-code thing
Teaching them the semantics of a = b if c assumes they have previous knowledge of the implications of ... to the namespace
if c:
a = b
i mean it could just be noted it's a one-liner that does the exact same thing
just like list comps, lambdas, and a if b else c
Except that the if statement has the advantage of being indented
Making it clear that this assignment is only done when c is True
i think generally speaking python devs are hesitant to introduce new keywords that are mostly just space-savers
unless as a statement would be syntactic sugar for if not wouldn't it?
Unless (heh) it assumes all statements are false
unless foo and bar:
pass # foo and bar is both False
depressed = True
os.system('rm -rf /') unless not depressed
usecase
unlessas a statement would be syntactic sugar forif notwouldn't it?
@brave badger exactly
unless foo is the same as if not foo, and unless foo and bar would be if not foo and not bar
That could work
For decorators, what's the difference between
@decorator vs @decorator()? Why do some decorators use the function call while others dont?
The latter is actually a decorator factory, which is basically a function that creates a decorator. You can use that to supply arguments e.g.
def xy_maker(x=0, y=0):
def xy_decorator(func):
def wrapped(*args, **kwargs):
print(f"{x} and {y}")
return func(*args, **kwargs)
return wrapped
return xy_decorator
@unkempt rock This channel is not for providing help or troubleshooting problems, please read the description of the channel. If you'd like help with your problem, see #❓|how-to-get-help.
Are there any differences between First Class Functions and High Order Functions?
I googled the terms and i still get the same definition:
A function is an instance of the Object type.
You can store the function in a variable.
You can pass the function as a parameter to another function.
You can return the function from a function.
You can store them in data structures such as hash tables, lists, … ```
@unkempt rock This would be a better fit for #algos-and-data-structs, but: "higher order function" is a mathematics term for a function that either takes a function as an argument, or returns a function as a result. It is not specific to programming.
"First class functions" is a feature of some programming languages that means functions are objects that can be bound to variables, and therefore passed as parameters. First class functions allow for the implementation of higher order functions.
Higher order functions can be created in languages that don't have first order functions, but doing so requires metaprogramming, because functions aren't integrated into the language's normal data model.
And conversely, it's possible to leverage first class functions for purposes other than composing higher order functions. For instance:
x = print
x(42)
This requires first class functions, because it assigns a function to a variable, but it does not contain any higher order functions - there are no functions in this program that must accept a function as an argument or return a function.
Hello, @warped hatch, I don't have such a resource handy, but this is not the right channel for such a question
Try #python-discussion or otherwise ask your question in a help channel
This channel is specificially for the topics mentioned in the channel's topic
Ok sure
Where can I find the channel's topic ?
Does someone know how to use a decorator which count the number of times all functions with that decorator are used ?
I would like to not use a global variable
The channel's topic is in Discord's channel topic feature. On desktop, it's at the top of the channel; on mobile, you need to swipe from the right to reveal the member list. The channel topic will be at the top.
Here is the code with a global variable
numero = 0
def dec(f):
def wrapper(*arg, **karg):
global numero
numero += 1
print(numero)
return(f(*arg, **karg))
return(wrapper)
@dec
def x():
pass
@dec
def y():
pass
x() #1
x() #2
y() #3
x() #4
y() #5
Thanks
The typical solution for that would be to use a nonlocal variable
!e
def decorator(func):
count = 0
def wrapper(*args, **kwargs):
nonlocal count
count += 1
print(count)
return func(*args, **kwargs)
return wrapper
@decorator
def function():
print("function")
function()
function()
@wide shuttle :white_check_mark: Your eval job has completed with return code 0.
001 | 1
002 | function
003 | 2
004 | function
One of the difficulties here would be "how do I get the value out?"
I've never heard about nonlocal variable
Thank you for your help
I think the value may be store in dir(decorator)
Whenever a class is created, it uses type(...) in the background, and metaclasses can be used to change what actually happens when the function is defined
Is there any way to do the similar with functions?
Like, "metaclasses" for functions? Or how are even functions made, when the def ... is called?
Does anyone know?
Classes (as instances of type) are fundamentally different from Functions (as instances of the respective function type)
Yeah, I'm more refering to the actual concept of having some function called to construct the object
There's types.FunctionType that would allow you to create a function object much like how type can be used to create class objects
If you want something similar to metaclasses for functions, I guess you can make a class that overloads __call__ and wrap functions in it
Okay, perfect. Is that something that's always called in order to construct functions or are the functions constructed in some other way?
I'm thinking more of if there's something that always happens when a function is defined
Not quite sure about the specifics but internally, Python analyzes the function definition, creates a code object, then uses that to construct an instance of the function type
If you decide to look into wrapper classes, you can use the decorator syntax:
>>> class MyFunc:
... def __init__(self, func):
... self.func = func
... def __call__(self, *args, **kwargs):
... result = self.func(*args, **kwargs)
... print(f'Called "{self.func.__name__}" with {args=} and {kwargs=}')
... return result
...
>>> @MyFunc
... def add(a, b):
... return a + b
...
>>> add(10, 5)
Called "add" with args=(10, 5) and kwargs={}
15```
Do note that this decorator will not work the way you'd expect it to on methods
It does not have a __get__ method to bind the instance the method is used on to the function
my brain...
!e
#Code from Ves Zappa
def decorator(func):
count = 0
def wrapper(*args, **kwargs):
nonlocal count
count += 1
print(count)
return func(*args, **kwargs)
return wrapper
@decorator
def function():
print("function")
@decorator
def function2():
print("function2")
function()
function()
function2()
function2()
You are not allowed to use that command here. Please use the #bot-commands channel instead.
Ow
Anyway the output is supposed to be 1, 2, 3, 4 but it's 1, 2, 1, 2
It's seems nonlocal variable don't help...
I can still use global variable or class atribute
you reset the count for each decoration, but you can modify it
Yes but I don't want to reset @deft pagoda
The only way I found it's either use global variable or class attribute
from itertools import count
keep_track = count()
def decorator(func):
def wrapper(*args, **kwargs):
print(next(keep_track))
return func(*args, **kwargs)
return wrapper
@decorator
def function():
print("function")
@decorator
def function2():
print("function2")
function()
function()
function2()
function2()
Yes, it can work. But isn't there an other way without global variable ?
You could wrap the decorator in a class
def decorator(func, cache={}):
cache[func.__name__] = 0
def wrapper(*args, **kwargs):
cache[func.__name__] += 1
print(sum(cache.values()))
return func(*args, **kwargs)
return wrapper
@decorator
def function():
print("function")
@decorator
def function2():
print("function2")
function()
function()
function2()
function2()
Isn't the cache reset ?
nope
No, it's a mutable object
It's usually recommended to replace mutable defaults with None and set them in the function body, but this is an interesting use case for a mutable default
Things like these continue to amaze me
So that's how cache works lol
Both salt's example and my example will set independent counters for the different decorated functions
well, mine does, but it will print the total of all
The counter did not reset with the nonlocal version; you just have one counter per decorated function
yeah
You could also set that as an attribute of the decorator function to make it accessible
I have never found an use case for a mutable default argument that was properly justified
that's true
i've used mutables only for caches, but of course you could use @lru_cache instead
i mean you could just keep a list with a single int in it
def decorator(func, counter=[0]):
def wrapper(*args, **kwargs):
counter[0] += 1
print(counter[0])
return func(*args, **kwargs)
return wrapper
You could also set that as an attribute of the decorator function to make it accessible
@wide shuttle
Oh it works !
def decorator(func):
decorator.cache = 0
def wrapper(*args, **kwargs):
decorator.cache += 1
print(decorator.cache)
return func(*args, **kwargs)
return wrapper
Do note that if you try to access the attribute before the decorator is used for the first time, you'll get an attribute error
this will also reset it each time
i was about to say that
You'll need a check (if not hasattr) to prevent that
you could just set it once directly after the definition
You can obviously also solve it with a decorator
!e
def add_counter(func):
func.counter = 0
return func
@add_counter
def decorator(func):
def wrapper(*args, **kwargs):
decorator.counter += 1
print(decorator.counter)
return func(*args, **kwargs)
return wrapper
@decorator
def function():
print("function")
@decorator
def function_two():
print("function two")
function()
function()
function_two()
function()
@wide shuttle :white_check_mark: Your eval job has completed with return code 0.
001 | 1
002 | function
003 | 2
004 | function
005 | 3
006 | function two
007 | 4
008 | function
this will also reset it each time
@wide shuttle How to reset ?
The only way to reset is to create the decorator again
no, each time you decorate, the .cache attribute is being set to 0 again
The latter is actually a decorator factory, which is basically a function that creates a decorator. You can use that to supply arguments e.g.
def xy_maker(x=0, y=0): def xy_decorator(func): def wrapped(*args, **kwargs): print(f"{x} and {y}") return func(*args, **kwargs) return wrapped return xy_decorator
@brave badger
Maybe a bit meta then, but why do we prefer a factory over something like a partial? Wouldn't they achieve the same thing?
Yeah, a partial function is the other approach
@unkempt rock This would be a better fit for #algos-and-data-structs, but: "higher order function" is a mathematics term for a function that either takes a function as an argument, or returns a function as a result. It is not specific to programming.
"First class functions" is a feature of some programming languages that means functions are objects that can be bound to variables, and therefore passed as parameters. First class functions allow for the implementation of higher order functions.Higher order functions can be created in languages that don't have first order functions, but doing so requires metaprogramming, because functions aren't integrated into the language's normal data model.
@raven ridge Thank you for your input on this.
How can I add typehint to the result of an awaitable?
Seems to be typing.Awaitable[ReturnType]. But vscode does not detect that type hint
what linter are you using?
Maybe you could try Coroutine?
If you're typehinting the coroutine itself, don't use Awaitable, it will wrap the result in an Awaitable anyway.
Awaitable works for me, if that's your intended usage
@tacit hawk
Also, I'm not sure if this is the right channel for this.
This is for a more higher-level discussion of Python.
That is vscode?
I am using pylint
If you still have this problem, let's move to a help channel
just stopping by here to see extra crazy shit
anyone familiar with pyinstaller and spec files?
isnt pyinstaller pip? im not sure
nope, pyinstaller is for packaging python programs as executable files
You'll probably want to grab a free help channel or #tools-and-devops jac0b
@peak spoke I'm in a help channel but no replies for 3/4 hr
I'm getting kinda desperate
@silent arrow what channe;?
@spark magnet manganese
@silent arrow im good with py installer
just look it up on youtube pretty good tutorials, check for the one in within the last 2 years
@fickle minnow I'm having issues with imports, If you wanna help have a look in help-manganese
Hmm... i’m reading the documentation, and maybe it’s cuz i’m on my phone, but I can’t tell if the adapter sits on top of some bottom transport layer, or replaces the bottom transport layer? Like does the adaptor need to implement socket stuff and whatnot?
How large does a application need to get before flask slows down?
Depends what you’re doing with it
(And how you’re running it - single thread? Multiple? Is there a db choke point?)
Could also have async stuff
What kind of game?
(Big difference between chess and something that needs real-time updates)
(Although not sure why you’d do the latter over http)
If it’s something like chess, you’re probably fine with flask
(Although ig if you have thousands of simultaneous players might want something more efficient than http?)
Im tryna make something like Politics & War with updates every 30 minutes or so
And HTTPS is a must
Like an RTS?
Ig
Let’s say 100k players, updates every 30m, 55 requests a second on average :/ idk if a basic flask app could handle that
Maybe 10k players
Bruh we are never gonna pass 5k wydm
Not with that attitude, you're not!
I swear, Crystalmaster, you WILL make THE BEST RTS this world has ever been graced with!
Gamers will want to be with you, and programmers will want to BE you!
People will see you walking around and be like, "Oh my God, Sid Meier, Chris Metzen and Josh Mosquiera just collaborated together and fused into one person, and that person is Crystalmaster420!"
CrystalMaster, you will have to scale at certain point, it becomes #tools-and-devops task at that point
Hello, I was learning OOP and thought about inbuilt objects such as lists and it is methods I was wondering how can I access the internal code if I can
The main implementation of Python, CPython, is written in C. You can read the C code if you want: https://github.com/python/cpython
Some standard library things are implemented in Python, but most performance-critical and basic objects are written in C.
For example, this is the implementation of the list object: https://github.com/python/cpython/blob/master/Objects/listobject.c
@magic junco
It's kind of daunting because it's more than 3000 lines of C, but if you need, you can pick any of the functions apart.
There's also a giant essay on the sorting algorithm in there: https://github.com/python/cpython/blob/master/Objects/listsort.txt
https://github.com/python/cpython/blob/master/Objects/boolobject.c
bool is probably the most trivial object there
if you don't count None
Thank you very much for guiding me, do you know how they implement a c program into python
If you don't, do you know any resources that explain that
?
What do you mean?
It's not easy to describe how to implement a full programming language (and I am no expert in that)
Python docs have an entire section dedicated to the CPython API
@grave jolt I see
You can also connect C extensions to Python, but that's a different topic.
Python interpreter is written in C, so the C code related to lists is just a part of Python's implementation.
Is that why they call python C-like language?
No, it's because Python is very similar to C (compared to, say, Haskell)
Python's implementation is not the same as the Python language. The language is defined by its specification. You can implement a Python interpreter in C, C++, Java, Brainfuck, JS, Python or some other language.
@grave jolt I see
@grave jolt Thank you again for the great resources that you provided
Happy to help 👍
CPython is written very well, so you could learn a lot from it if you decide to learn C.
though, I would recommend reading the source code of Lua to learn C & programming langauge implementation
very well written source code with comments
is there any one using News-Api ?
So I'm pondering a fairly advanced application - a MUD server engine. I've been studying lots and lots of existing ones and trying dozens of things. In studying Evennia (which is implemented atop of Twisted and Django), I learned that having 'hot reload' for code changes without disconnecting people is... difficult, because reloading 'changed' modules can be very tricky. So... I'm pondering an application written in C++ that handles the networking and some other things, which then loads up CPython in embedded mode. If you wanna reload, the application achieves this by basically deleting and re-creating the embedded Python section.
Will this have the desired effect of being able to reload/update the Python code without killing the process and disconnecting users from the networking?
well i mean if you do the networking at a low enough level there need not be a concept of 'disconnecting'
if you store the connections in such a way that they're preserved, then the server could effectively go down and come back up and all the players would see is a slight temporary increase in ping
^ this
if you design the server to be able to carry on its previous state after a reboot
Are you referring to pure Python there, or something else?
well it shouldn't really matter, my point is more that you'll have that issue of disconnecting if you use a preexisting multiplayer network stack, since the server will 'go offline'
but if you build it from sockets yourself or whatever then you should be able to make it survive the server not accepting requests for a bit
Yeah that's the intention.
I'm just kind squinty at the direction that Evennia took. Its answer to 'reloading is tricky' is 'split the program into two different processes linked via localhost TCP/IP'. the Portal accepts and holds onto connections and then communicates to the Server. If you want to update game code on the Server, then you just reload the Server process. This allows a complete reboot without disconnecting users
that's what i was going to suggest actually
i've never built one of these before though so take what i say with a pinch of salt
@upper spoke I think that approach sounds sensible.
Probably way easier than some kind of language interop hack, even if it does work.
dementati: Which approach? the embedded, or the split process?
There was an Evennia presentation at Pycon Sweden last year
that was probably its current maintainer, Griatch
Since Gri was talking about doing that earlier. 🙂
Could be. I tried it out, it's pretty cool.
I'm trying to figure out how to make it even more awesome and also explore different ways of doing it. But to answer Seagull's question...
I got into MUDs/MUSHes/etc back when I was in late middle school and have never really gotten out of them. Eventually I got into coding - though, mostly for PennMUSH, using the "softcode" language that's common to the TinyMUX/TinyMUSH/Rhost/PennMUSH https://github.com/volundmush/mushcode
Which, if you'll notice by looking at this file here...
https://raw.githubusercontent.com/volundmush/mushcode/master/Bulletin Board System - BBS.txt
Yeah, hard to tolerate that language once you've been exposed to anything more sensible.
Jeez louise
yeah that's the reaction I usually get
so yeah I MASTERED THAT LANGUAGE.
over like ten years of tinkering and tinkering and studying the documentation.
With minimal actual programming experience.
That's a regex, right?
yeah
but it needs to be further escaped to work with MUSHcode 😛
so anyways then I discovered Evennia...
You're like a wildman living on your own in the bush for 10 years, finally discovering civilization.
yeah basically.
Or, in the mush, as it were.
i was getting ready to start building a really nice python mush library lol :(
or mud
or whatever the word is
oh mud is the genre
It's usually referred to as MU* by the community I hang on with since it encompases MUD, MUX, MOO, MUSH, MUCK, etc.
anyhoo
And Evennia is -really- cool but it has what, in my opinion, are some critical holes/failings which I'm having a hard time figuring out how to express/explain let alone solve. But it does soooooo darned much right, too. So I'm like... "what do"
To explain what I mean
The program is designed so that you install it via pip and this gives you the 'evennia' command, which can create a 'game template' folder. this folder contains your settings, custom code, etc. you launch evennia by being inside that folder and using 'evennia start'. It seems sensible, and it's a good approach. The problem is... the server is SUPER BAREBONES. There's no real great way to make portable/reusable/shareable/plugin'y Python code. A ton of its internal architecture is like 95% of the way there to having one, but it just -doesn't-, and the project's resistant to changes that would break its API for obvious reasons.
So I'm like... "what do?"
You could always just fork it.
so how do you actually make an evennia game
@hollow crane
EXACTLY
@gloomy rain yeah it's feeling more and more like I'll have to eventually do that. 😦
looks like it's more of a game engine
i.e. the 'creation' isn't in python
yeah
like these are all just 'commands'
nothing python-y
well you're supposed to extend it by adding more python code to your 'game template' folder. That will make more behavior available. but content can also be created within the game experience such as digging new rooms or defining object prototypes.
it's easy peasy to add new database tables, new commands, etc, using Python.
The problem isn't that. The problem is if you want to create a 'library of cool stuff designed for Evennia' that other people can use a foundation.
if that's your goal, the best you're gonna get is https://github.com/volundmush/athanor
this is a python library that you tell your 'game template' to import settings from instead of Evennia. It hijacks the Evennia load process and adds plugin loading, allowing plugins to further alter the settings before Django finishes initializing them. But it can only do so much, and it adds a ton more complexity to the setup/install and doesn't really feel maintainable.
btw
@hollow crane when you say 'build it from sockets myself' you mean that a pure CPython application can fork to reload its .py code, and it will inherit the socket-descriptors, network users will NOT be disconnected?
i think possibly below socket level then
well
So a lot of old MUD servers, the way that they work is this.
When you want to HOTBOOT/COPYOVER/HOT RELOAD (whatever you call it), this is implemented through some clever shenanigans. The game server saves its networking state (who's connected to what character on what file descriptor, etc) to disk. then it fork()'s up a new process of its own binary - using the recompiled binary, though. the forked process inherits the file descriptors/sockets of the parent process and thus, it just needs to read the file that was quickly saved to reconstruct the previous state.
this works because C and C++ is pretty low level
goes to research
yeah i think the issue is python sock will close automatically
if the context gets destroyed
i think you could inherit the socket object and remove that behaviour
but obviously you have to be careful to make that not cause problems
i wonder if there's a way in python to destroy an object without invoking its destroy dunders
I suppose it's easy enough to -test- this though
If you have two terminals open that are using the same venv, and you run two separate scripts, does the GIL mean that only one command for either can be run at a time?
Those should run in separate processes
So the GIL only applies within a process?
Yes
Ah
Possibly per interpreter soon
welp
how do I create a build system in sublimeREPL which links the venv interpreter
Wouldn't having an interpreter share a GIL for all its processes be worse for concurrency?
Per one running instance of the interpreter in a process
I assume that's better but I lack the knowledge to appreciate how.
subinterpreters may come in python 3.10 to allow multiple interpreters through python, alongside that a way to only have the GIL on a single interpreter instead of a process is being developed but that'll probably come later
Also, for some applications, spinning up more processes is fine
like HTTP applications
Doesn't that tool in joblib for parallelization spin up however many processes?
Sure, but that's more for data analysis
Yep but currently the only way to get through a CPU limitation is with a process (or a C lib), if subinterpreters don't share a GIL they're significantly cheaper so start up and communicate with
for Flask/Django, just set up HTTP Load Balancer and have multiple instances
Is there a way to get CPython for DOS, preferably MS-DOS? I heard about it but I can't find anything useful
If it works I'm interested also to port it to the DOS I'm making
But it might be kinda hard because I'll need to reimplement the whole C standard library and doing all this on real mode is pain
I come from C# and am pretty new to Python. One thing I'd like to understand is why would I use Iterator instead of just a normal for loop?
This doesn't really look efficient to me
myit = iter(mytuple)
print(next(myit))
print(next(myit))
print(next(myit))````
Compared to this ```mytuple = ("apple", "banana", "cherry")
for x in mytuple:
print(x) ```
Also __iter__() method. Don't wanna bash on Python, I love it! I just don't undestand these "underscore" methods at all. When I'm looking at other peoples code, I rarely see anyone use those. Seems more like useless bloat to me.
the dunders are mostly used when making your own classes
If iterator is used because I don't wanna print all components, I could just specify the index anyway, so I don't see the use case of it at all
A normal for loop uses the iterator under the hood.
a bit of a convoluted but commonly used example of using iter is the grouper from itertools recipes
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
But if you have a sequence anyway, you won't be using the builtin iter that much
so, a couple things to explain here. creating an object with iter() actually happens automatically when you use the for x in y: syntax. Iterators can only be used once, and then they are exhausted, which usually isn't something that is relevant. The main reason to call next() manually is when you want to advance an iterator in response to some other event, but it can also be handy when you want to pull values from a generator one by one.
As for the methods with leading double underscores, there are two different uses. __my_method__ is the most common format, and these are used primarily in custom classes. They are called "magic methods" in python because they allow your custom class objects to work with the normal operators: ```python
class Foo:
def add(self, other):
return str.add(self, other) # String concatenation
x = Foo()
y = Foo()
z = x + y # will call the str.add() method on these objects
the other leading double underscore format looks like __my_attribute and is used for name mangling. I'd read up on that one on your own, but it is not nearly as common as the dunder/ magic method useages
How does _len_() works for builtins ?
It's a function in C
is it doing an equivalent of sum(1 for _ in iterable) or does it use an internal counter that tracks objects being added or removed ?
Most likely an internal counter, not just for efficiency reasons but due to the way lists are handled
Yes, it would be O(n) if it did sum(1 for _ in iterable)
And in C you cannot not know the size of the list (unless it's a linked list)
So I think I have a design, after doing a bunch of research
My project is to create a MUD server/server-engine. not a specific game at this point - more the framework for making awesome games. And after a bunch of research and thinking about it, I think this approach would be good:
at the heart of the system is a 'gateway/portal' process/server. This has telnet, websocket, etc ports open up for clients. It has its own small database for accounts/authentication/login purposes. It can run commands users provide after they connect.
This would -probably- run via asyncio or something.
and then....
it would open another port for the actual GAMES to connect to - multiple games. they would be linked using some kind of RPC or other message setup that's much more robust and standardized than bloody telnet thank you.
Then a user could just specify a game to join for that given connection and bam, it connects to that game via the RPC
@pale lake please see #❓|how-to-get-help . This is not a help channel
and the beauty is that the Games wouldn't necessarily need to be written in Python. could be anything.
Alright. Thanks
i was using selenium in python and ran it to check for an element in chrome in headless mode and this error appeared, it didn't occur when i did not use headless chrome |"Uncaught SyntaxError: Unexpected token '{'"
can someone help me pls
I haven't used selenium before but it says that you put a '{' to somewhere you shouldn't, I cannot say anything else without examining the code
And this channel isn't for support. See #❓|how-to-get-help
is it alright to import a package module using from package import module, from within a module that's in the package 🤔
feels a bit daft
What's wrong with that?
no idea, just felt a bit odd, like if there's a module mod.py in package Pack, then doing from Pack import blah within mod.py, for some reason seemed odd
that's a done thing though ?
could do import Pack.blah or from . import blah but the normal from import looks the best imo
the normal from import looks the best imo
ok cool - i was just unsure about importing the package from within the package
import Pack.blah
this is usingPackas a directory presumably, whereas i havePack/thing/mod.pyand need to usePath/other/something.py, but i'll just import as though it was a script anywhere else
does anyone know if there's an example of this pattern somewhere?
ok , so i just cloned pandas and ran grep -rni . -e "from pandas import, seems there's a bunch of this pattern:
./core/indexes/base.py:1083: from pandas import Series
./core/indexes/base.py:1143: from pandas import DataFrame
./core/indexes/base.py:2367: from pandas import Series
./core/indexes/base.py:2372: from pandas import Series
for some reason i thought this might be an issue, perhaps one that would cause problems i wouldn't pick up, so all good 😄 thanks
there might be some issues with recurrent dependencies tho i guess
i think i might have been concerned about this
sorry to break in but do any of you know of any way to use machine learning to remove texture seams? kind of how an image upscaler would work or do you know where should i ask
@magic python there's nothing wrong with importing sub modules
You can also do import package.module as bob but call it what you want instead of Bob.
I never do relative imports because pycharm can't refractor those automatically
user will enter one input 1d2h3m4s - (this is time format) i want the program to split this and convert it into seconds i.e - 93,784
the user can also enter 2m1h, how to do it?
this isnt the channel @unkempt rock i'd ask in a help channel
Discussion on the use cases, implementation and future of the Python programming language including PEPs, advanced language concepts, new releases, the standard library, and the overall design of the language.
Oh ok
#❓|how-to-get-help @unkempt rock
@flint hornet I believe there are already some tools to do that, if that's actually your end goal, if not I don't know I'm afraid
Yeah i did find one that is related to the other program ive been using which is esrgan image enhancer but it seems to be really demanding on my pc since it rarely works. I think blender and 3ds max also have stuff like that but i would rather not touch any 3d modelling app
since i dont know how to use them and the textures im working on are from a very old game that has no in engine way to remove seams by itself
Make a seamless texture from photo online. Optionally you can choose the way to create a seamless texture, level of brightness transitions and to crop don't needed edges for best results.
i've had some success with this before
btw this probably shouldn't be in #internals-and-peps anymore :)
It totally did, thanks
@boreal umbra ok cool - thanks. To be clear, when one says "importing sub modules", this refers to importing modules of a package from within the package itself, using the syntax from <package> import <module> (or something similar).
I wasn't familiar with the phrase being that specific.
Anyone know how to run a python script using Automator for Mac?
I want to schedule the program at specific times too...
Can someone help me with integrating functions in my own language?
Does shared memory improves much the performance of multiprocessing?
It can - the alternative to shared memory is pickling every object that needs to be shared with the parent and communicating it back through a pipe, or something like that. That's much slower than shared memory as the amount of data to be communicated grows.
Is anyone aware of when Apache Arrow is going to be replaced with the current BlockManager in pandas?
Or is the Apache Arrow interface supposed to be like a complete rework..
Hey Agent Mimos.
Maybe I can help you.
I am getting a error reset not defined
This isn't a help channel, you should use the help system or a topical help channel. See #❓|how-to-get-help
It explains it in #❓|how-to-get-help
okay i started one i think
I own a server for helping in Python. If it matters.
can I add custom voice models in text to speech in Python? Someone pls tell me
@boreal umbra right, but that's the context that i was speaking within - i'm aware this pattern is fine in general, this specific context was what my question was related to though
i'm just going to assume it's fine at this point lol
i searched the pandas lib and they had it
I'll pay somebody $10 get on call with me and help me speed up my script by parallelizing my machine learning functions to run in another thread
!rule 6 @stone marten
6. No spamming or unapproved advertising, including requests for paid work. Open-source projects can be showcased in #show-your-projects.
Is it good practice to move helper functions that aren't meant to be accessible / used outside of a class ?
Your budget is also insultingly low
Doesn't reddit have higher quality questions due to the sorting algo
It's not really a job. It's mostly incentive to get my questions answered. My question gets drowned in low quality messages in #python-discussion
why not ask in an actual help channel
@stone marten removing those to clear this channel up, as @hollow crane says asking in a help channel is best bet for answers, not #python-discussion
Is it good practice to move helper functions that aren't meant to be accessible / used outside of a class ?
@final whale
What do you mean?
If you have a helper method that doesn't operate on an instance but you think it makes more sense to have it as part of the class, you can make it a static method.
(whoa, I really worded that horribly, sorry for giving you a stroke)
I didn't have a stroke.
figuratively i believe
Though it is likely that you wanted to communicate more than what you said.
Its mostly personal preference I believe
I put internal helpers into module namespace
Staticmethods only if they are part of the classes interface, which they rarely are in my experience
But I know that lemon for examples disagrees strongly, and uses staticmethods a lot
We actually had that chat recently
Ooo what did Lemon say?
Does putting helper functions in the module's namespace prevent the creation of unneccessary new methods when creating new instances ?
Lemon lives in Sweden or Norway, not sure
Instances don't get individual copies of the whole method
no, theres no duplication even with bound methods
Methods are part of the class. So there's only one copy of each method.
Even if you make a subclass, if you don't reimplement that method, it will go to the original class to define that method to find it.
I think part of why I tend to just put functions in modules is that I dislike the visual noise of the extra decorator + the optional leading underscore
if someone looks at the class they probably want to see the interface, not the internals
I like to keep things flat also
And since python doesn't require all functions to be methods, you have that option
that was interesting, need to look more on how internals work
and I'll definively use that
thank you very much for your help :D
I'm wondering about breakpoint(), are there any other uses beyond just calling it as breakpoint() ? I get that, and make use of it, but was curious whether there's something else i was unaware of
seems that it takes arguments, i tried throwing a couple of things in (strings or whatever) and it didn't like it
This function drops you into the debugger at the call site. Specifically, it calls sys.breakpointhook(), passing args and kws straight through. By default, sys.breakpointhook() calls pdb.set_trace() expecting no arguments. In this case, it is purely a convenience function so you don’t have to explicitly import pdb or type as much code to enter the debugger. However, sys.breakpointhook() can be set to some other function and breakpoint() will automatically call that, allowing you to drop into the debugger of choice.
from https://docs.python.org/3/library/functions.html#breakpoint
hrm
seems that perhaps you can select the debugger from the arguments or something https://docs.python.org/3/library/sys.html#sys.breakpointhook
oh man this is awesome. I've never had a project come together so quickly. Python rocks
@upper spoke seems off topic
What's the main thing that people use inspect for? I've only really used it for inspect.getsource, and that was only because i was too lazy to bother going into the editor to look at it... I'm curious what the main uses are for others though
I used it once to check if something was a coroutine
Cause I had to cancel it but the variable wasn't always a coroutine. I don't remember the exact situation
inspect.signature is useful when writing a decorator. Used that to do a dependency injection thing with the type hints.
How bad it is to keep python program running when unhandled exceptions are caught?
Depends on the exception, some (RuntimeError, MemoryError) indicate you should be shutting down since it's really broken.
But exceptions are normal and the interpreter would be fine. But it's probably a bad idea because you have no idea what state your program is in - you could have invalid broken objects still around because code didn't finish properly, so continuing would give wrong results.
What are you meaning by "unhandled" exceptions.
"Errors should never pass silently.Errors should never pass silently. Unless explicitly silenced." - import this
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
more implicit is more than implicit
!shhhhhhhhhhhhhhhhhhhhhhhh
✅ silenced current channel for 15 minute(s).
If you received a ping, that was because of a user thinking they would be cool for spamming pings by automating their Discord account.
They've been dealt with and are no longer a member of this community.
✅ unsilenced current channel.
This is not the channel for such jokes, please keep the channel on topic
whats [::x] used for
That is for slice notation, but such questions would be better off in #python-discussion or a help channel. See #❓|how-to-get-help
Is there any performance difference between dataclass and default class where you add all members in __init__?
dataclass is known to be slightly faster
"DataClass is 8.18% faster to create objects than NamedTuple to create and store objects."
"DataClass performs 13.21% faster than NamedTuple and 0.97% faster then Object to execute functions."
if everything is in __init__ and it's not doing much but data storage, it's likely negligible
Python's multiprocessing pool.map(<function>, <list of function_arguments>) seems to be a magical method to speed up a function. What are some rules of thumb to follow to ensure that a function can be used in python's multiprocessing pool.map?
If you dis a __init__ for both classes you get the same byte code. So I don't actually expect there to be any difference between __init__ calls
The comparison linked was mainly for comparing a dataclass with a namedtuple-based alternative, I think
As for pool.map the rule of thumb is to check the docs, as that outlines the expected function signature
It also compares standard objects
I just made tests and result is:
Normal class with __init__ and instance creation:
0.013281786814332008
Dataclass and instance creation:
0.43129867501556873
Tests is in #bot-commands
Keep in mind that is also timing defining the class not only creating an instance
Yes, I try now instance creation only
who did ping everyone
No one pinged everyone, someone used a selfbot to ping a lot of people.
yes?
are bots allowed to ping?
@cyan meteor
This was not an official bot, just someone abusing their own user account by automating it in a way that goes against Discord's Terms of Service. Quite often, the associated account gets terminated by Discord after pulling such a stunt.
Anyway, I think every answer has been answered at this point. Let's all try to keep this channel on topic.
Cool
that's surprising - why are data classes faster than namedtuples
@golden imp they are not faster all the time
namedtuples are faster to be created
while dataclasses are faster when you read and execute a function
they are also smaller in size compared to named tuples
56 bytes vs 80 bytes
Breh
A troll using a ping selfbot. They've been dealt with.
aha
Interesting, So in concurrent pings with Quart vs Sanic, Sanic still crushes Quart
even with Quart running on PyPy which was quicker than the benchmarks for standard cPython
!shhhhhhhhhhhhhhhhhhhhhhhh
that's a huge shhh

what if you miss a h
the length of the silencing of the channel depends on the amount of h's used... I think the minimum amount of h's is 3?
I get pinged, but disnt find the message
!shhhhhhhhhhhhhhhhhhhhhhhh
!tempmute 376364581523816461 8H Why you thought that would be the correct thing to do directly after a message from staff about staying on topic is beyond me. Try thinking before you type.
:incoming_envelope: :ok_hand: applied mute to @crimson dragon until 2020-06-08 23:19 (7 hours and 59 minutes).
Why does everyone have to type "who pinged"
when there's a clear message right above that says it was a troll
We get it
It's been dealt with
Keep this channel on topic
I am not able to follow this solution. Can someone help me
Connecting to gsheets: the usual way would be to run gc = pygsheets.authorize() with the creds.json file in the same root folder as the script gsheets.py (folder structure below)
├── creds.json
├──
@fleet abyss This is not the right channel for specific help questions. Check out #❓|how-to-get-help .
Sorry @grave jolt ....I'll make sure this doesn't happen again
If anyone has some time, could use help in #help-cake Cheers
@slim yew If someone wants to help you, they will help you. Don't post off-topic messages here.
Hi guys, is searching through a Binary Search Tree ALWAYS faster than searching through a list in Python?
It seems to be so
That's for #algos-and-data-structs
ah ty
Searching in a sorted array with binary search or searching in a BST is faster that searching by checking every element of a list, be it Python, Java, Haskell or Brainfuck (or a physical structure, like a sorted bookshelf)
On average, that is
...and given a big enough element count
For 3 elements, a list might be faster
i asked on general and no one knew about it , so im asking here
how do i link to a function or method in another sub-module in google docstrings?
snap , wrong channel
Well depends on how many times you are searching/is the array sorted, though
yeah that's what I mean
for 3, isnt it still faster or equal speed for a BST?
[1, 2, 3] vs [2,[1,3]]
worst case for list is O(3) while worst case for BST is O(2)
and best case for both is O(1)
Big-O notation doesn’t work like that
It basically shows how much does time/space increase as input increases
So for O(n^2) sorting algorithm, if you double the data, time quadruples
List lookup is O(n), binary tree is O(log(n))
so on average tree is faster
but if you have unordered data at the beginning, sorting and looking something up will take O(n*log(n)) + O(log(n)) vs O(n) list lookup, so on inputs with not so many lookups, using list lookup is better.
bruh lol
And the actual execution time function can have other factors. 3n + 27 log(n) + sqrt(n) is still O(n)
wouldnt it be O(2) when n = 2 and same with 3?
Assuming no external factors
If its O(n)
O(n) describes the behavior of time or space taken up by a computation depending on the input size.
As nekit said,
Big-O notation doesn’t work like that; It basically shows how much does time/space increase as input increases
Still, this is for #algos-and-data-structs
^^ you can interpret it more as a ratio or proportion i guess
if something is O(n^2) complexity and it takes 7 nanoseconds to evaluate n=1. then at n=2, you could guess that it'd take 30 nanoseconds or so to evaluate
What exactly is the query here?
I need to do some code introspection, e.g. find all usages of a given function. The kind of stuff an IDE like PyCharm does, but I need to do it in code. Can anyone help me out with some pointers or even good search terms? "Code introspection" is giving me a lot of stuff about getting object types and attributes, but it's not really what I'm after.
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))```
So this may be nitpicking or not, but I would avoid thinking about small n. Big O notation is an asymptotic thing, and there may be constant or lower-degree terms that dominate for sufficiently small input sizes.
usages of given functions can be obtained by using dir and vars.
Source code: Lib/ast.py
The ast module helps Python applications to process trees of the Python abstract syntax grammar. The abstract syntax itself might change with each Python release; this module helps to find out programmatically what the current grammar looks like.
An abstract syntax tree can be generated by passing ast.PyCF_ONLY_AST as a flag to the compile() built-in function, or using the parse() helper provided in this module. The result will be a tree of objects whose classes all inherit from ast.AST. An abstract syntax tree can be compiled into a Python code object using the built-in compile() function.
!e ```python
import ast
print( ast.parse("3 + 4") )
@paper echo :white_check_mark: Your eval job has completed with return code 0.
<_ast.Module object at 0x7f11c816b9d0>
hm.
Warning It is possible to crash the Python interpreter with a sufficiently large/complex string due to stack depth limitations in Python’s AST compiler.
so maybe not good for arbitrarily long python source code files. maybe you can use/repurpose something like jedi or rope for this
@grave jolt :x: Your eval job has completed with return code 1.
001 | s_push: parser stack overflow
002 | Traceback (most recent call last):
003 | File "<string>", line 2, in <module>
004 | File "/usr/local/lib/python3.8/ast.py", line 47, in parse
005 | return compile(source, filename, mode, flags,
006 | MemoryError
?
i have pretty printer for ast if it helps:
In [281]: past("3 + 4")
Expr
╰──BinOp
├──Constant
│ ╰──3
├──Add
╰──Constant
╰──4
thats fantastic.
i haven't changed it i don't think
i think i just added the curvy L character
versus the boxy one
nice
!e
import ast
e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
e.operand = e
compile(ast.Expression(e), "<test>", "eval")
@true ridge :warning: Your eval job has completed with return code 139 (SIGSEGV).
[No output]
Python is converted to bytecode, so that means... you can create a PYTHON CPU!!! :D
Just like Java CPUs exist you can create a Python CPU
Is there a way to import from .pyi files? Like if a Protocol or type is defined there, and I want to refer to it?
pyi files are just stubs, they contain only definitions and no actual functionality
they're basically there for your editor
you can import as normal, assuming your editor supports them
How?
They contain no functionality but they contain symbols. I need those symbols for type-checking
Basically I'm doing w: _writer = csv.writer(fp) and that will only work if I can get the _writer type from csv.pyi
The type is _csv.writer
and it's imported in csv.pyi: https://github.com/python/typeshed/blob/72c8907760286f275d6e9314f09f0739007ff469/stdlib/2and3/csv.pyi#L11
Problem is the type name is the same as the function name
If you create your object and output it you get
<_csv.writer object at 0x000001F565851680>
import _csv; w: '_csv._writer' = csv.writer(fp) works 🤔
I think csv._writer should also work
It doesn't which is why I was confused
would anyone know if something significant changed in python's logging module in python version 3.7 or 3.8?
I have a script which works as I expect in 3.6.6 but doesn't anymore in 3.8.3
In what way does it not work anymore?
I have an instance of logging.Logger
when I change it's logging level, logging calls above that level go through in 3.6, but not anymore in 3.8
(I'll brb)
yeah no I don't think this should have changed
most likely you have a different problem
Ok new problem: I'm making a proxy file object, which can operate on either TextIO or BinaryIO. So it needs a typevar that's either TextIO or BinaryIO, but it also needs to implement whichever protocol
T = TypeVar('T', TextIO, BinaryIO) ; class MyAdapter(Generic[T], T): does not work ('T' is a type variable and only valid in type context)
there's no @overload for classes either
most likely you have a different problem
by setting the level of the logger's handler, it seems to work againthat's not it
oh I see
in python 3.6, you could retro-actively change the logging level after you sent a logging message
why can't you do that in 3.8?????
typing.IO is not a protocol and I don't see why not
I mean a typing.Protocol
Ah okay, I getcha
This is a huge pain
Hello, @stoic iris, if you want to get help, claim a help channel (see #❓|how-to-get-help) or try #discord-bots. This channel is for discussing Python itself:
if I am implementing a UDP server, would it be faster to use multithreading or asyncio?
curio
Where can I find info on what kind of code optimisations CPython does at compile or runtime?
I couldn't really find much documented when searching myself but know of some if you're interested, or comments in here https://github.com/python/cpython/blob/master/Python/peephole.c/ compile.c
Thanks, I'll take a look^^


