#internals-and-peps

1 messages · Page 46 of 1

karmic storm
#

@boreal umbra I read that in PEP 3107 annotations may have been used to store arbitrary help messages for arguments, like.

def func(num: 'first number to add', num2: 'second number to add') -> 'the added numbers':
  ...
boreal umbra
#

Ah

cedar latch
#

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)

cedar latch
#

isnt gpt3 out @rich wharf

true hollow
#

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.

deft pagoda
#
In [7]: a = 10
   ...: def f():
   ...:     print(a)

In [8]: f()
10

i can access global variables in a function

true hollow
#

?

#

I can't IIRC

deft pagoda
#

you don't recall correctly then

#

you can always freeze classes by overloading __setattr__

true hollow
#

Wdym freeze classes

deft pagoda
#
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
hazy anchor
#

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

cloud crypt
#

scoping in python is a bit weird, but overall fine

hazy anchor
#

i first learned with block scoping, its largely how i think. ¯(°_o)/¯. its not so bad with nice editor warnings these days.

true hollow
#

yeah but why complicate yourself creating classes when they can just add a const keyword

#

Doesn't really make sense

deft pagoda
#

because the convention in python is that private variables start with _ and that largely the responsibility is on the developers to not break contracts

cloud crypt
#
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

spice pecan
#

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)

deft pagoda
#

double underscore will name mangle

cloud crypt
#

You don’t need to name mangle often tbh

deft pagoda
#

i just single underscore things that aren't important to class api

hazy anchor
#

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

cloud crypt
#

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

deft pagoda
#

i use it pretty often

cloud crypt
#

lemme grab an example

deft pagoda
#

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

cloud crypt
#
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

deft pagoda
#

they could access it either way, it's simply a way to demarcate intention

cloud crypt
#

yeah

topaz gyro
#

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

spice pecan
#

Well, True and False internally are 1 and 0 respectively

#

bool is subclassed from int

topaz gyro
#

I guess so

spice pecan
#

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

topaz gyro
#

True

hazy anchor
#

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.

nova aspen
#

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?

vestal hedge
#

selenium webdriver does that , you can check the docs out.

unkempt rock
#

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)```
spice pecan
#

It just means you called the getter/setter method

unkempt rock
#

Alright. And that's what I understand that @property Decorator is used to avoid calling the Property()?

wide shuttle
#

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"
fallen slateBOT
#

@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
wide shuttle
#

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

unkempt rock
#

Thanks for the explanation

wide shuttle
#

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)

unkempt rock
#

C# seems way easier

#
{
  private string name; // field

  public string Name   // property
  {
    get { return name; }   // get method
    set { name = value; }  // set method
  }
}```
wide shuttle
#

In Python, you typically don't use getters/settes unless you really need custom getter/setter logic

unkempt rock
#

Don't understand why Pythonians have to call variables attributes

wide shuttle
#

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

unkempt rock
#

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

wide shuttle
#

Yes, Python's philosophy is different in that sense

unkempt rock
#

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

wide shuttle
#

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

gray mirage
#

imo people that overuse private in languages that enforce it are doing more damage than people that underuse it

unkempt rock
#

imo people that overuse private in languages that enforce it are doing more damage than people that underuse it
@gray mirage How so?

gray mirage
#

We've all been in a situation where we needed to touch the private parts of someone's code

unkempt rock
#

It makes room for more robust applications

wide shuttle
#

is that your experience or just your guess?

gray mirage
#

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

wide shuttle
#

Since you're just starting out as a Python dev

gray mirage
#

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

unkempt rock
#

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

gray mirage
#

Everyone has an opinion, haha

unkempt rock
#

Not trying to create a C# vs Python war :p It is just interesting to see the perspective of both languages

gray mirage
#

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"

unkempt rock
#

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

gray mirage
#

Well, that's not true

#

Python is very good for memory safety for example

stable grail
#

python is a language for consenting adults.

unkempt rock
#

Also, lower coupling between objects and hence improvement in code maintainability

stable grail
#

you can touch someone private parts as long as they consent to it

gray mirage
#

You generally don't need to worry about data corruption in a high level language like python

unkempt rock
#

Probably not but bad designing is the result of none proper encapsulation

gray mirage
#

I don't think python really has issues with encapsulation either

wide shuttle
#

I think that's too generalized a statement to make

gray mirage
#

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

stable grail
#

since python is 100% object oriented you are probably right here gdude

unkempt rock
#

But on the other hand, Python is scripting, C# is used for big applications mostly, Python is seen more for AI, Data Science, etc.

gray mirage
#

Actually, Python does power a lot of bigger projects too

unkempt rock
#

Absolutely, gdude

gray mirage
#

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

unkempt rock
#

But you don't create desktop applications really with Python

gray mirage
#

Some people do

stable grail
#

sure you do

gray mirage
#

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

stable grail
#

yeah, you have access to some of the .net binding with ironpython, but desktop applications is not the same as .net/windows appcliactions

unkempt rock
#

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

gray mirage
#

I mean, you're using Discord, it has some python components

#

Even if it is majority Electron

stable grail
#

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

gray mirage
#

They gave C# for comparison earlier so that's why I mentioned winforms :>

stable grail
#

the age of desktop applications are not at its peak anymore imho.

gray mirage
#

Then again, they're killing winforms soon anyway

#

Yeah, true enough

#

It's all web these days

stable grail
#

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

unkempt rock
#

Yeah

gray mirage
#

Python is excellent for web work, as I mentioned earlier

#

That's probably why it's seeing such an uptick recently

stable grail
#

having backend python developers and fronend whatever developers makes for a good match

unkempt rock
#

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”.

gray mirage
#

You can declare a variable without assigning anything to it in some contexts

#

I assume that's what you mean

unkempt rock
#

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]

true hollow
#

wait are you jsing python

gray mirage
#

I don't think those things really go with the spirit of Python

stable grail
#

this assumes that variables in python are something they are not

gray mirage
#

But you could always ask the mailing lists

#

It's worth mentioning that you don't "create" a variable in Python

stable grail
#

i really dislike the use of variables as a concept in python

gray mirage
#

It's more like you're just giving a name to a piece of data

true hollow
#

piton is oop

#

*python

stable grail
#

because variables have other features in other languages

unkempt rock
#

Yeah but hard to find bugs

stable grail
#

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

fallen slateBOT
#

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

undone hare
#

At low level, python uses dicts to represent namespaces, so you are really binding a name to a reference to the value

true hollow
#

Basically variables are pointers to an object

undone hare
#

Well yes, like most languages :)

lost dragon
#

anyone knows pymc3?

grave jolt
#

@undone hare Well, dicts are used in global namespaces.

#

!e

import dis

def f(x, y):
  x += y
  return x

dis.dis(f)
fallen slateBOT
#

@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
grave jolt
#

Local variables are represented more efficiently

#

But if I wrote x += z, then it would change the instruction to LOAD_GLOBAL("z")

true hollow
#

What's dis module? First time I see it

grave jolt
#

It's for disassembly

undone hare
#

That's interesting

cunning crest
#

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

hollow crane
#

@cunning crest have you tried pyinstaller

full jay
spiral willow
#

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

full jay
#

Oh interesting, I didn't know that

#

I've actually been really really impressed with big M's push towards open source

spiral willow
#

They are trying

cedar latch
#

also whats the best voice cloner that is open sourced

final whale
#

Is contextlib.suppress more elegant than a try except ?

ruby spruce
#

@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.

final whale
#

suppress it is, the use case would be to ignore an indexerror

ruby spruce
#

Yes, then this is better

dire night
#

Hey, I have question.

karmic storm
#

is there any advantage using vars(obj) over obj.__dict__?

jaunty tiger
#

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?

grave jolt
#

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

hasty thistle
#

@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

neon palm
#

Hello guys

#

Anyone with skills about type hint?

unkempt rock
#

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

unkempt rock
#

Is there a library or tool that can convert dis output (python disassembled bytecode) back to python source code?

cloud crypt
unkempt rock
#

@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

cloud crypt
#

ahhh

#

well dis operates on the bytecode

#

and produces a more readable version of it

unkempt rock
#

hmm just found this, will dig into it =.= maybe this is a difficult task

unkempt rock
#

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

fossil pumice
#

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?

unkempt rock
#

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

brave badger
#

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

unkempt rock
#

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

brave badger
#

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
unkempt rock
#

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)
brave badger
#

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

unkempt rock
#

the ruby implementation

#

well it's more for the a being already defined case

peak spoke
#

The structure of the language would have to be changed or some weirdly parsed statement for that to work

unkempt rock
#

how so? you'd just make the else clause optional, wouldnt you?

brave badger
#

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

modern night
#

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"

unkempt rock
#

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
@brave badger a = b if c is clear when you read it out tho

fossil pumice
#

all of these are far uglier than just using more than one line for it

brave badger
#

It would require context if it's on its own

fossil pumice
#

oneliners are a code smell

unkempt rock
#

what about a = b if c else d tho?

brave badger
#

Context whether a is defined or a is not defined

unkempt rock
#

well that would be given in the code

#

just like if you

if b:
 a = c
merry pollen
#

oh we talkin about syntax stuff, nice

unkempt rock
#

you'd need to know if b is defined beforehand anyway

fossil pumice
#

yeah, you know, what this channel is actually for

#

it is nice.

brave badger
#

But a can be undefined, and if b is false, a is never put to scope

unkempt rock
#

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

brave badger
#

^

peak spoke
#

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

brave badger
#

It's a pitfall depending on the context

merry pollen
#

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

unkempt rock
#

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)

merry pollen
#

optionals?

unkempt rock
#

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

merry pollen
#

no wait, it's different in ruby

unkempt rock
#

in ruby ! and ? are valud function characters

merry pollen
#

right

brave badger
#

One statement assuming two things on its own would generally be a bad idea

merry pollen
#

hmn, it'd be neat

#

i'd like it, but syntax bloat is kinda maybe not the way to go

unkempt rock
#

well i mean it wouldnt really assume 2 things, either the else is there or it isnt

brave badger
#

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
Was referring to 😅

unkempt rock
#

ohhh

#

a = b if c is 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

brave badger
#

But yeah, eliminating the else clause would mean that Python would look for a previous definition of a

unkempt rock
#

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

hollow crane
#

conditional assignment in an expression is a bit weird

#

since i see = and immediately assume something's happening

brave badger
#

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

unkempt rock
#

but that would be a = b if c else None

merry pollen
#

ruby has some uh, idiosyncrasies

unkempt rock
#

but it's almost like engilsh, and as such, very easy to pick up

brave badger
#

Setting to None would be more rational than not bringing the name to scope at all

merry pollen
#

honestly i dont really parse the english "readings" of syntax anymore tbh

brave badger
#

since i see = and immediately assume something's happening

unkempt rock
#

true, but it wouldnt be apparent

#

Setting to None would 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

hollow crane
#

and can you imagine conditional assignment if the variable doesn't have to exist yet

#

the linter would have a fit

unkempt rock
#

i mean it already does in the instance i just quoted

brave badger
#

If that's the case, then the one-liner won't bring to scope?

merry pollen
#

oh ye

unkempt rock
#

and my idea isnt so much for if the variable is undefined, but if it IS defined

brave badger
#

That would be quite vague

merry pollen
#
c = False
a = b if c 

would be runtime fine but linter..weird

#

hmm

brave badger
#

Question, what would be the behavior if a is undefined?

unkempt rock
#

same as

if b:
  a = c
#

linter freaks out

#

i mean it's literally the same statement

#

exhum, a = c or b

brave badger
#

I honestly think that's a pitfall that most beginners find hard to debug

unkempt rock
#

i dont immediately know that or construct and im not sure beginners would either

brave badger
#

Also based on personal experience when I was starting out

unkempt rock
#

exhum, a = c or b
@unkempt rock also that doesnt work if c (or b) isnt a bool

#

does it?

brave badger
#

I think it implies truthiness

unkempt rock
#

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?

brave badger
#

idk, personally not fond of the idea of not bringing names to scope

unkempt rock
#

i mean it'd have to be used with care, just like if c: a = b

brave badger
#

A beginner would be quick to assume that Python defines that name either way

merry pollen
#

if you're translating
if b: a = c
then shouldn't that be a = b and c or a

unkempt rock
#

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 be a = b and c or a
@merry pollen and would beginners know that?

merry pollen
brave badger
#

Especially if you put it in one line
a = b if c would read a is only put to scope if c

merry pollen
#

technically it's basic syntax

#

just somewhat obscure

#

it's just "truthiness" plus "short circuiting"

#

kinda

unkempt rock
#

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?

brave badger
#

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
merry pollen
#

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

unkempt rock
#

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

merry pollen
#

e!

a = "a"
b = True
c = "c"
print(b or c)
#

oh

unkempt rock
#

!e *

fallen slateBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

unkempt rock
#

shush, bot

merry pollen
#

@unkempt rock see #bot-commands

brave badger
#

A beginner would assume that in a = b if c, a would be defined either way, and not everyone has a linter

unkempt rock
#

again, what about

if c:
  a = b
#

exact same statement

merry pollen
#

readability and intuition are pretty personal things

#

hard to really say equivocally which one is more intuitive

brave badger
#
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

merry pollen
#

i think the breakdown of assignment operators always assign is like, maybe not so hot

#

from a like, brief glance -over-code thing

brave badger
#

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
unkempt rock
#

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

brave badger
#

Except that the if statement has the advantage of being indented

#

Making it clear that this assignment is only done when c is True

unkempt rock
#

hm

#

that aside, what about the second point i mentioned? unless?

merry pollen
#

i think generally speaking python devs are hesitant to introduce new keywords that are mostly just space-savers

unkempt rock
#

i mean it's just an inverted if?

#

but hm

brave badger
#

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
unkempt rock
#
depressed = True
os.system('rm -rf /') unless not depressed
#

usecase

#

unless as a statement would be syntactic sugar for if not wouldn't it?
@brave badger exactly

brave badger
#

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

unkempt rock
#

i would say unless a and b is if not(a and b)

#

but ye

fossil jetty
#

For decorators, what's the difference between

@decorator vs @decorator()? Why do some decorators use the function call while others dont?

brave badger
#

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
grizzled vigil
#

@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.

karmic karma
#

hi all masters of python

#

you can help me with my proyect

unkempt rock
#

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, … ```
raven ridge
#

@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.

wide shuttle
#

Hello, @warped hatch, I don't have such a resource handy, but this is not the right channel for such a question

#

This channel is specificially for the topics mentioned in the channel's topic

warped hatch
#

Ok sure

spiral obsidian
feral heath
#

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

wide shuttle
#

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.

feral heath
#

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

wide shuttle
#

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()
fallen slateBOT
#

@wide shuttle :white_check_mark: Your eval job has completed with return code 0.

001 | 1
002 | function
003 | 2
004 | function
wide shuttle
#

One of the difficulties here would be "how do I get the value out?"

feral heath
#

I've never heard about nonlocal variable

#

Thank you for your help

#

I think the value may be store in dir(decorator)

spark parcel
#

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?

brave badger
#

Classes (as instances of type) are fundamentally different from Functions (as instances of the respective function type)

spark parcel
#

Yeah, I'm more refering to the actual concept of having some function called to construct the object

brave badger
#

There's types.FunctionType that would allow you to create a function object much like how type can be used to create class objects

spice pecan
#

If you want something similar to metaclasses for functions, I guess you can make a class that overloads __call__ and wrap functions in it

spark parcel
#

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

brave badger
#

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

spark parcel
#

That's perfect. I'll look into those things even further then

#

Thanks for the help

spice pecan
#

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```
wide shuttle
#

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

rocky garden
#

my brain...

feral heath
#

!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()
fallen slateBOT
#

You are not allowed to use that command here. Please use the #bot-commands channel instead.

feral heath
#

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

deft pagoda
#

you reset the count for each decoration, but you can modify it

feral heath
#

Yes but I don't want to reset @deft pagoda

#

The only way I found it's either use global variable or class attribute

deft pagoda
#
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()
feral heath
#

Yes, it can work. But isn't there an other way without global variable ?

spice pecan
#

You could wrap the decorator in a class

deft pagoda
#
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()
feral heath
#

Isn't the cache reset ?

deft pagoda
#

nope

spice pecan
#

No, it's a mutable object

bronze garden
#

thats such a hack!

#

dont use mutable default args lol

spice pecan
#

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

deft pagoda
#

that's actually the use-case of mutable args

#

if you were wondering

spice pecan
#

Things like these continue to amaze me

dire hull
#

So that's how cache works lol

wide shuttle
#

Both salt's example and my example will set independent counters for the different decorated functions

deft pagoda
#

well, mine does, but it will print the total of all

wide shuttle
#

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

molten onyx
#

I have never found an use case for a mutable default argument that was properly justified

deft pagoda
#

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
feral heath
#

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
wide shuttle
#

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

deft pagoda
#

i was about to say that

wide shuttle
#

You'll need a check (if not hasattr) to prevent that

deft pagoda
#

you could just set it once directly after the definition

wide shuttle
#

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()
fallen slateBOT
#

@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
deft pagoda
#

oh yeah, the good ol' decorator decorator

#

add_counter needs a decorator

feral heath
#

this will also reset it each time
@wide shuttle How to reset ?

#

The only way to reset is to create the decorator again

deft pagoda
#

no, each time you decorate, the .cache attribute is being set to 0 again

feral heath
#

I didn't think about that

#

Thank you @deft pagoda

fossil jetty
#

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?

brave badger
#

Yeah, a partial function is the other approach

unkempt rock
#

@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.

tacit hawk
#

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

grave jolt
#

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.

#

@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.

tacit hawk
#

That is vscode?

grave jolt
#

yes, that's vscode.

#

Although I'm using Pyright as my linter, not flake8 or mypy

tacit hawk
#

I am using pylint

grave jolt
#

If you still have this problem, let's move to a help channel

fickle minnow
#

just stopping by here to see extra crazy shit

silent arrow
#

anyone familiar with pyinstaller and spec files?

unkempt rock
#

isnt pyinstaller pip? im not sure

hollow crane
#

nope, pyinstaller is for packaging python programs as executable files

peak spoke
silent arrow
#

@peak spoke I'm in a help channel but no replies for 3/4 hr

#

I'm getting kinda desperate

spark magnet
#

@silent arrow what channe;?

silent arrow
#

@spark magnet manganese

fickle minnow
#

@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

silent arrow
#

@fickle minnow I'm having issues with imports, If you wanna help have a look in help-manganese

sage scroll
#

Can anyone tell me the use of requests.adapters

#

🤦‍♂️

red solar
#

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?

unkempt rock
#

How large does a application need to get before flask slows down?

red solar
#

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

unkempt rock
#

Im making a multiplayer web browser game

#

so idk about cores

red solar
#

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?)

unkempt rock
#

Im tryna make something like Politics & War with updates every 30 minutes or so

#

And HTTPS is a must

#

Like an RTS?

#

Ig

red solar
#

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

unkempt rock
#

Bruh we are never gonna pass 5k wydm

unkempt rock
#

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!"

spiral willow
#

CrystalMaster, you will have to scale at certain point, it becomes #tools-and-devops task at that point

magic junco
#

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

grave jolt
#

Some standard library things are implemented in Python, but most performance-critical and basic objects are written in 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.

#

if you don't count None

magic junco
#

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

#

?

grave jolt
#

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

magic junco
#

@grave jolt I see

grave jolt
#

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.

magic junco
#

Is that why they call python C-like language?

grave jolt
#

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.

magic junco
#

@grave jolt I see

#

@grave jolt Thank you again for the great resources that you provided

grave jolt
#

Happy to help 👍

#

CPython is written very well, so you could learn a lot from it if you decide to learn C.

visual maple
#

though, I would recommend reading the source code of Lua to learn C & programming langauge implementation

#

very well written source code with comments

unique rampart
#

is there any one using News-Api ?

upper spoke
#

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?

hollow crane
#

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

rugged lake
#

^ this

hollow crane
#

if you design the server to be able to carry on its previous state after a reboot

upper spoke
#

Are you referring to pure Python there, or something else?

hollow crane
#

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

upper spoke
#

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

hollow crane
#

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

gloomy rain
#

@upper spoke I think that approach sounds sensible.

#

Probably way easier than some kind of language interop hack, even if it does work.

upper spoke
#

dementati: Which approach? the embedded, or the split process?

gloomy rain
#

The split one

#

You might want to do that anyway in order to improve scalability.

hollow crane
#

that sounds like a really fun project actually

#

what's the inspiration

upper spoke
#

waves an Evennia awareness flag in that case

#

well

gloomy rain
#

There was an Evennia presentation at Pycon Sweden last year

upper spoke
#

that was probably its current maintainer, Griatch

#

Since Gri was talking about doing that earlier. 🙂

gloomy rain
#

Could be. I tried it out, it's pretty cool.

upper spoke
#

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.

hollow crane
#

ahh

#

what the

gloomy rain
#

Jeez louise

upper spoke
#

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.

gloomy rain
#

That's a regex, right?

upper spoke
#

yeah

#

but it needs to be further escaped to work with MUSHcode 😛

#

so anyways then I discovered Evennia...

gloomy rain
#

You're like a wildman living on your own in the bush for 10 years, finally discovering civilization.

upper spoke
#

yeah basically.

gloomy rain
#

Or, in the mush, as it were.

hollow crane
#

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

upper spoke
#

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?"

gloomy rain
#

You could always just fork it.

hollow crane
#

so how do you actually make an evennia game

upper spoke
#

@hollow crane
EXACTLY

#

@gloomy rain yeah it's feeling more and more like I'll have to eventually do that. 😦

hollow crane
#

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

upper spoke
#

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.

#

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.

upper spoke
#

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?

hollow crane
#

i think possibly below socket level then

upper spoke
#

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

hollow crane
#

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

upper spoke
#

I suppose it's easy enough to -test- this though

boreal umbra
#

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?

peak spoke
#

Those should run in separate processes

boreal umbra
#

So the GIL only applies within a process?

peak spoke
#

Yes

boreal umbra
#

Ah

peak spoke
#

Possibly per interpreter soon

somber sage
#

welp
how do I create a build system in sublimeREPL which links the venv interpreter

boreal umbra
#

Wouldn't having an interpreter share a GIL for all its processes be worse for concurrency?

peak spoke
#

Per one running instance of the interpreter in a process

boreal umbra
#

I assume that's better but I lack the knowledge to appreciate how.

peak spoke
#

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

spiral willow
#

Also, for some applications, spinning up more processes is fine

#

like HTTP applications

boreal umbra
#

Doesn't that tool in joblib for parallelization spin up however many processes?

spiral willow
#

Sure, but that's more for data analysis

peak spoke
#

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

spiral willow
#

for Flask/Django, just set up HTTP Load Balancer and have multiple instances

true hollow
#

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

unkempt rock
#

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.

peak spoke
#

the dunders are mostly used when making your own classes

unkempt rock
#

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

zenith topaz
#

A normal for loop uses the iterator under the hood.

peak spoke
#

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

clever escarp
#

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

final whale
#

How does _len_() works for builtins ?

zenith topaz
#

It's a function in C

final whale
#

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 ?

spice pecan
#

Most likely an internal counter, not just for efficiency reasons but due to the way lists are handled

grave jolt
#

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)

upper spoke
#

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

shy vine
upper spoke
#

and the beauty is that the Games wouldn't necessarily need to be written in Python. could be anything.

pale lake
#

Alright. Thanks

balmy thunder
#

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

vestal jetty
#

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

raven ridge
magic python
#

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

peak spoke
#

What's wrong with that?

magic python
#

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 ?

peak spoke
#

could do import Pack.blah or from . import blah but the normal from import looks the best imo

magic python
#

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 using Pack as a directory presumably, whereas i have Pack/thing/mod.py and need to use Path/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

magic python
#

there might be some issues with recurrent dependencies tho i guess
i think i might have been concerned about this

flint hornet
#

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

boreal umbra
#

@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

unkempt rock
#

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

hollow crane
#

@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

flint hornet
#

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

hollow crane
#

i've had some success with this before

flint hornet
#

what its just that easy?!

#

OMG THANK YOU SOOOOO MUCH

hollow crane
#

did that work for you

#

glad to help

flint hornet
#

It totally did, thanks

magic python
#

@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).

boreal umbra
#

I wasn't familiar with the phrase being that specific.

dense pumice
#

Anyone know how to run a python script using Automator for Mac?

#

I want to schedule the program at specific times too...

empty prawn
#

Can someone help me with integrating functions in my own language?

tacit hawk
#

Does shared memory improves much the performance of multiprocessing?

raven ridge
#

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.

swift imp
#

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..

unkempt rock
#

Hello Guys

#

I need help with something

leaden ember
#

Hey Agent Mimos.
Maybe I can help you.

unkempt rock
#

I am getting a error reset not defined

sturdy timber
#

This isn't a help channel, you should use the help system or a topical help channel. See #❓|how-to-get-help

unkempt rock
#

😦

#

I don't know how to do it

sturdy timber
unkempt rock
#

okay i started one i think

leaden ember
#

I own a server for helping in Python. If it matters.

balmy thunder
#

can I add custom voice models in text to speech in Python? Someone pls tell me

magic python
#

@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

stone marten
#

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

low lagoon
#

!rule 6 @stone marten

fallen slateBOT
#

6. No spamming or unapproved advertising, including requests for paid work. Open-source projects can be showcased in #show-your-projects.

stone marten
#

Oh, sorry. Won't happen again

#

Is my message something you could approve?

kind bridge
#

No

#

This isn't reddit

final whale
#

Is it good practice to move helper functions that aren't meant to be accessible / used outside of a class ?

kind bridge
#

Your budget is also insultingly low

stone marten
#

Doesn't reddit have higher quality questions due to the sorting algo

kind bridge
#

¯_(ツ)_/¯

#

I'm not a redditor

stone marten
#

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

hollow crane
#

why not ask in an actual help channel

worldly venture
#

@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

boreal umbra
#

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.

final whale
#

(whoa, I really worded that horribly, sorry for giving you a stroke)

boreal umbra
#

I didn't have a stroke.

hollow crane
#

figuratively i believe

boreal umbra
#

Though it is likely that you wanted to communicate more than what you said.

sacred tinsel
#

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

boreal umbra
#

Ooo what did Lemon say?

sacred tinsel
#

We can pingu him, but he may be asleep

#

actually it's 1am for us so I wont

boreal umbra
#

He's online.

#

You're both in the Netherlands, yes?

hollow crane
#

moop moop

#

remember pingu

final whale
#

Does putting helper functions in the module's namespace prevent the creation of unneccessary new methods when creating new instances ?

sacred tinsel
#

Lemon lives in Sweden or Norway, not sure

boreal umbra
#

Instances don't get individual copies of the whole method

sacred tinsel
#

no, theres no duplication even with bound methods

boreal umbra
#

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.

sacred tinsel
#

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

boreal umbra
#

I like to keep things flat also

#

And since python doesn't require all functions to be methods, you have that option

final whale
#

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

magic python
#

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

upper spoke
#

oh man this is awesome. I've never had a project come together so quickly. Python rocks

magic python
#

@upper spoke seems off topic

magic python
#

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

charred wagon
#

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

prime estuary
#

inspect.signature is useful when writing a decorator. Used that to do a dependency injection thing with the type hints.

tacit hawk
#

How bad it is to keep python program running when unhandled exceptions are caught?

prime estuary
#

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.

unkempt rock
#

"Errors should never pass silently.Errors should never pass silently. Unless explicitly silenced." - import this

north root
#

yep, it's part of the zen

#

!zen

fallen slateBOT
#
The Zen of Python, by Tim Peters

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!

deft pagoda
#

more implicit is more than implicit

wide shuttle
#

!shhhhhhhhhhhhhhhhhhhhhhhh

fallen slateBOT
#

✅ silenced current channel for 15 minute(s).

wide shuttle
#

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.

fallen slateBOT
#

✅ unsilenced current channel.

cosmic cape
#

ai is taking over

#

automated accounts will be the future of social media

languid dagger
#

This is not the channel for such jokes, please keep the channel on topic

unkempt rock
#

whats [::x] used for

languid dagger
ruby spruce
#

Is there any performance difference between dataclass and default class where you add all members in __init__?

unkempt rock
#

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

forest flicker
#

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?

languid dagger
#

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

wide shuttle
#

The comparison linked was mainly for comparing a dataclass with a namedtuple-based alternative, I think

languid dagger
#

As for pool.map the rule of thumb is to check the docs, as that outlines the expected function signature

unkempt rock
#

It also compares standard objects

ruby spruce
#

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

languid dagger
#

Keep in mind that is also timing defining the class not only creating an instance

ruby spruce
#

Yes, I try now instance creation only

still river
#

who did ping everyone

wide shuttle
#

No one pinged everyone, someone used a selfbot to ping a lot of people.

cyan meteor
#

Huh

#

how tho?

#

are bots allowed to ping?

cloud crypt
#

yes?

wide shuttle
#

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.

cyan meteor
#

Cool

golden imp
#

that's surprising - why are data classes faster than namedtuples

stable grail
#

@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

golden imp
#

I see

#

I wonder why they're smaller

blissful dirge
#

Breh

wide shuttle
#

A troll using a ping selfbot. They've been dealt with.

void stirrup
#

They literally did it in 3 servers I was in

#

Oof

true hollow
#

aha

radiant fulcrum
#

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

cloud crypt
#

sanic is really what its name is huh

#

haha

radiant fulcrum
#

bearing in mind this was a single threaded instance however

#

but yeah

merry valley
#

Uh

#

Mass tag???

cloud crypt
#

!shhhhhhhhhhhhhhhhhhhhhhhh
that's a huge shhh lemon_pleased

true hollow
thin gull
#

what if you miss a h

hasty thistle
#

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?

stray plover
#

I get pinged, but disnt find the message

shy vine
#

Please keep this channel on topic

crimson dragon
#

!shhhhhhhhhhhhhhhhhhhhhhhh

shy vine
#

!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.

fallen slateBOT
#

:incoming_envelope: :ok_hand: applied mute to @crimson dragon until 2020-06-08 23:19 (7 hours and 59 minutes).

shy vine
#

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

fleet abyss
grave jolt
fleet abyss
#

Sorry @grave jolt ....I'll make sure this doesn't happen again

slim yew
#

If anyone has some time, could use help in #help-cake Cheers

grave jolt
#

@slim yew If someone wants to help you, they will help you. Don't post off-topic messages here.

quaint jolt
#

Hi guys, is searching through a Binary Search Tree ALWAYS faster than searching through a list in Python?

#

It seems to be so

grave jolt
quaint jolt
#

ah ty

grave jolt
#

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

weak owl
#

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

cloud crypt
#

Well depends on how many times you are searching/is the array sorted, though

grave jolt
#

True

#

Sorting the array just to use binary search once is pretty wasteful

cloud crypt
#

yeah that's what I mean

quaint jolt
#

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)

cloud crypt
#

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

quaint jolt
#

well still lol

#

isnt it 2 vs 3

cloud crypt
#

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.

grave jolt
#

O(2) and O(3) don't make any sense

#

O(n) is the asymptotic behavior

quaint jolt
#

bruh lol

grave jolt
#

And the actual execution time function can have other factors. 3n + 27 log(n) + sqrt(n) is still O(n)

quaint jolt
#

wouldnt it be O(2) when n = 2 and same with 3?

#

Assuming no external factors

#

If its O(n)

grave jolt
#

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

frozen finch
#

^^ 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

still otter
#

What exactly is the query here?

coral birch
#

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.

still otter
#
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))```
coral birch
#

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.

still otter
#

usages of given functions can be obtained by using dir and vars.

paper echo
#

@coral birch for introspection, maybe you want the ast module

#

!d g ast

fallen slateBOT
#

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.

paper echo
#

!e ```python
import ast
print( ast.parse("3 + 4") )

fallen slateBOT
#

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

<_ast.Module object at 0x7f11c816b9d0>
paper echo
#

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
#

Maybe they're talking about

#

!e

import ast
print(ast.parse("("*300 + ")"*300))
fallen slateBOT
#

@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
grave jolt
#

?

deft pagoda
#

i have pretty printer for ast if it helps:

In [281]: past("3 + 4")
Expr
╰──BinOp
   ├──Constant
   │  ╰──3
   ├──Add
   ╰──Constant
      ╰──4
hazy anchor
#

thats fantastic.

cloud crypt
#

past

#

haha

#

@deft pagoda is it updated? I recall worse looking characters haha

deft pagoda
#

i haven't changed it i don't think

#

i think i just added the curvy L character

#

versus the boxy one

minor venture
#

nice

true ridge
#

!e

import ast
e = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0)
e.operand = e
compile(ast.Expression(e), "<test>", "eval")
fallen slateBOT
#

@true ridge :warning: Your eval job has completed with return code 139 (SIGSEGV).

[No output]
true hollow
#

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

hollow zephyr
#

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?

gray mirage
#

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

hollow zephyr
#

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

languid dagger
#

The type is _csv.writer

languid dagger
#

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>
hollow zephyr
#

import _csv; w: '_csv._writer' = csv.writer(fp) works 🤔

languid dagger
#

I think csv._writer should also work

hollow zephyr
#

It doesn't which is why I was confused

hasty thistle
#

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

hollow zephyr
#

In what way does it not work anymore?

hasty thistle
#

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)

hollow zephyr
#

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

hasty thistle
#

most likely you have a different problem
by setting the level of the logger's handler, it seems to work again that'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?????

hollow zephyr
#

typing.IO is not a protocol and I don't see why not

full jay
#

As in like data type?

#

Or keyboard

hollow zephyr
#

I mean a typing.Protocol

full jay
#

Ah okay, I getcha

hollow zephyr
#

This is a huge pain

wide shuttle
warm sage
#

if I am implementing a UDP server, would it be faster to use multithreading or asyncio?

cloud crypt
deft pagoda
#

curio

gentle lodge
#

not Trio?

#

I thought Trio was the new hotness

unkempt rock
#

Where can I find info on what kind of code optimisations CPython does at compile or runtime?

peak spoke
unkempt rock
#

Thanks, I'll take a look^^

prime estuary
#

They're mainly quite basic, since Python is so dynamic most aren't safe.

#

Things like constant folding, converting for x in [1, 2, 3] to loop over a tuple instead, tidying up jumps to unconditional jumps, etc.