#internals-and-peps
1 messages Β· Page 52 of 1
I'm not sure I see a huge benefit
to using atomics?
But I personally haven't using much shared memory in python
I'm familiar with them and the use in other languages, but it's never a tool I've wanted and not had in python. Guess I write different programs in it
on windows you'd be using one of these for example http://timgolden.me.uk/pywin32-docs/win32event__CreateMutex_meth.html
on linux/osx you'd be using SYSV/POSIX primitives
the one benefit of atomics is that they don't (usually) block
which is ideal within an event loop
this problem keeps evolving
there isn't really a defined problem, i want atomics in general π€·
so it's atomics for IPC inside an event loop?
that's a use case yes
another is if i want a process safe variable in shared memory, if i put a lock there, how would c++ access said lock?
you use one of the synchronization primitives provided by the kernel
which have their own way of being ID'd system wide
well for windows you can use pywin32 to access the windows api
through ctypes?
and for linux/osx im sure there's a SYSV or POSIX api somewhere
pywin32 is a module that exposes the windows api to python
ah huh
hmm... ok i guess the uses cases are for shared memory if you don't want the overhead of locks, and for process safe non blocking locks (complicated but doable)
(non blocking as in for use with asyncio)
to have interprocess atomics like youre suggesting im pretty sure you'd need some backing from the kernel
and idk of any kernel that provides this
It seems like a use case almost entirely done at the cpython level
all lock free atomics are interprocess safe
the second one yes, the first one no
and if they're going to be used for the second one, why not provide that freedom to everyone π€·
are you sure youre not thinking of thread safety?
Describe a program that you would use this hypothetical library in
I'm not against it, I just want to understand
beyond the event loop thing?
Atomic operations are pretty limited. Simple maths and pointer swapping. What would you be doing in asyncio that was best solved by having two corouines accessing the same variable
so people in asyncio rn can't use thread/process locks
they block
there are OS primitives that let you have non blocking locks, and they almost always have a file descriptor (int) as a handle
so now you can use this handle with poll/select/epoll whatever, to implement an asyncio friendly lock π
great for mixing threads, and event loops, since in python int is already thread safe
but it's not interprocess safe
and you cant put the file descriptor in shared memory and have it be safe as is
so make it an atomic int π
It sounds to me like a coroutines aware lock primitive is what's required there. Maybe it can be achieved with atomic support, implemented in C using the atomics that already exist
this is what i'm working towards (other people helping), just realised i might need atomics for this, and thought why not propose it as part of the library, so other people can use it for their really niche use cases?
imo the more available directly rather than as an implementation detail, the better
also x86_64 has 128bit atomics which is nice π (fits a uuid)
maybe you want to make some sort of in memory interprocess database π€· idk people do crazy things (definitely not me)
Well. I'm interested but not convinced. Lucky you don't have to convince me :p if it's going on GitHub would you care to drop a link
i'm currently rethinking the implementation π writing an atomics library based on <stdatomic.h> is easy, but if you want 128bit atomics and stuff, you need intrinsics :/
i can link you to a partially complete old implementation?
intersting SO post. one question
The mechanism hardware exposes for atomic read-modify-write operations is based on MESI cache coherency which only cares about physical addresses. x86 lock cmpxchg / lock add / etc. makes a core hang on to a cache line in Modified state so no other core can read/write it in the middle of the atomic operation.
what happens if it's a single core running multiple processes accessing the same address
does that violate the atomicity?
no, but i'm trying to think why
on x86 specifically that's not an issue, since all int operations are atomic (but not in the sense that the cache will be updated for other cores)
but if there's no other cores to update, then it's just always atomic π€·
Do I interpret the above uses a memory guard in the cache? That seems like a non starter doesn't it
thats what im understanding
why is it a non starter?
But it seems like in C++ the operation is explicitly supported
yes it does use a lock in the simplest sense (hence the lock prefix on the instruction), however it's a hardware lock at the lowest level, and it's process safe (it applies to all cores on a machine)
(all cores that share that cache)
I think I'm more likely misunderstanding the cache line part. Research suggests it can work
I wonder if multiple cpu machines are the edge case
to be clear, this can still block, just realistically almost never, and if it does, it's for a tiny amount of time)
edge case in what? cache coherency is a multi core thing
It must be a solved problem for atomics to work at all, so disregard me. It's interesting and not something I know about
Is there a way to use functools lru cache for large memory requirements (e.g, graph isomorphism checking and path detection) without crashing your computer?
Swap, specifically
Could you give an example?
See how ya go. It's generally not that fast, but a cache for objects larger than memory than only be optimised so much. The critical point is probably to keep the keys in memory and swap out the value
That helps a lot, thanks!
How do you guys specify your version of installable packages that you create? I want to define it one place and then being able to get the version from within my code (to set FastAPI version)?
I ended up doing the following:
.
βββ alembic.ini
βββ docker
βΒ Β βββ Dockerfile
βΒ Β βββ Dockerfile.worker
βββ docs
βββ Makefile
βββ MANIFEST.in
βββ migrations
βΒ Β βββ env.py
βΒ Β βββ helper
βΒ Β βΒ Β βββ __init__.py
βΒ Β βββ README
βΒ Β βββ script.py.mako
βΒ Β βββ versions
βΒ Β βββ 2d1946f92088_added_users_table.py
βΒ Β βββ added_users
βββ README.md
βββ setup.cfg
βββ setup.py
βββ src
βΒ Β βββ wert_api
βΒ Β βββ asgi.py
βΒ Β βββ config.py
βΒ Β βββ __init__.py
βΒ Β βββ models
βΒ Β βββ routes
βΒ Β βββ version.py
βββ tests
βββ conftest.py
βββ test_users.py
Where version.py simply contained __version__ = '0.1.0'
And setup.py:
# -*- coding: utf-8 -*-
"""Builds boilerplate as a package
"""
import setuptools
from distutils import util
version = dict()
path = util.convert_path("src/wert_api/version.py")
with open(path) as file:
exec(file.read(), version)
setuptools.setup(version=version["__version__"])
why not import version from version?
how can i test if 2 types are equal?
for example, if i take a type as a function input, how do i equate that input type with some other type?
if type(obj1) is type(obj2):
You're asking in the wrong place btw @minor sinew. Use the help channels for help (see #βο½how-to-get-help)
@open trout that's not what im asking. in that, you're getting the type from some object
the type im getting is a function input, so there's no object to even compare
@minor sinew As I said, move to a help channel please
I feel like there should be a flatten function in the stdlib for flattening 2d lists
it's such a common problem with no universal solution.
values = [[1, 2, 3], [4, 5], [6]]
flattened = [e for row in values for e in row]
Though I agree. a flatten() function that flattens any list or maybe a flatten(axis) would be really conventient
for lists you can do sum(values, []), but we could absolutely use a flatten function
wow that's actually quite clever. Never thought about that
@unkempt rock that would make it uninstallable for new users since src is not a module.
finally finished testing this function i made to build a big matrix to represent connections in a big network lol the worst part was handwriting the expected test case matrices
Looking for advice in implementing Domain Driven Design, Event sourcing using python.
Does anyone here have experience dealing with neural networks in python?
Thanks
With cpython wouldn't each element in a list be different sizes as they don't have to be the same type?
(Same size in memory)
i was thinking that they're pointers, but i haven't looked so its just a guess/assumption from me.
So it's not an actual c array then?
It is an array of pointers.
an array of point... yesh. thats. π
Ok cheers
But different objects on their own, of course, can occupy different amount of space.
Yea that makes sense
They're just somewhere in the heap aren't they
π
I got a question about compiling an exe and making a gui in python. I wrote a function that takes in an excel sheet and for each row in the sheet, it uses selenium to fill out an online webform. Now I want to distribute this among the company I work for, so others can use it. If u compile this into an exe, how do I package it so the chrome driver.exe that's necessary for selenium to control chrome, is inside that .exe
I know nothing about doing this in python so like, idk if what I want us possible
you can try PyInstaller
wait, just read about the need of adding chrome driver.exe too
I think you could try to build it as a folder and wrangle the building step in PyInstaller in order to ship that .exe
Ok, thank you
Yeah you can do it with pyinstaller, but it has a lot of "gotyas". Might take some time to understand hidden imports and data packages for it
hey is anyone here familiar with multithreading multiprocessing* and database connections ?
i am trying to use multiple cores to make simulatenous inserts into a db and im getting a crash thats very odd
def createRandomOLD(mean, standardDeviation, numberOfUniques, SensorType,sensorID):
for k in range(numberOfUniques):
randomG = ('%.2f'%(random.gauss(mean,standardDeviation)))
print("Creating Entry with value " + randomG + SensorType)
cursor = dataBaseConnection.cursor()
cursor.execute(
'insert into SensorData(SensorUniqueID,SensorValue,SensorInput,SensorType,IsMalware) values(?,?,?,?,?);',
(sensorID, float(randomG), 'Virtual',SensorType,0)
)
dataBaseConnection.commit()
so im making two threads that call this function with different input parameters.
The error that i get seems to be an issue with syncronization?
What database?
Whatβs the error?
let me get it for u!
is it ok if i get a screenshot?
im like remoted into
and sometimes i get that error with no changes to the code
worth mentioning that if i run this on my desktop i get no errors
and i tested it with 16 processes and it worked fine
yeah its odd
where is MSSQL server running?
this also freezes up the sql server
its running on a dedicated server at a university
so they are hosting it with what i assume to be good hardware and all that
you have to restart MSSQL?
i dont
they do?
are you properly committing?
i beleive using try: and finally: has stopped that error
So im not too sure if i am
you could read back
im committing every time a value is generated by the funciton
with it being RaspPi, I wonder if it can't handle the load
idk if thats helpful
hmmmm
yeah im wondering
its a 4 core model with 4gb of ram so i expected it to
and malloc error is memory allocation errors
but it's not freeing up memory properly
yeah the hated word from my c/c++ experiences
I'm not familiar with multithreaded library either
thats fine
if this were your program to troubleshoot what steps would u take?
single threads work fine* idk if i mentioned that
I can tell you that MSSQL can handle numerous applications dumping garbage in it
we use it at work all the time
yeah the server is set to unlimited connnections
instead of multithreaded library
run it with just two SSH windows and python app (no multithread)
maybe third SSH windows to watch top to make sure you don't swap and explode
yeah so thats where we run into an issue
we have been told that it should all run from one script
and on a rpi
but that does work
we have tried that with like 4 seperate terminals
so 1 script that launches all the others?
i think we tried that and it finishes the scripts in order and not simulataneosly
I'm not familiar with Python launching stuff
I use Powershell for my OS level work
ah ok
thats fine i appreciate the help
i think we may have to do that and move on
I wonder if Multiprocessing thread is keeping track or something and in middle of keeping track, runs out of memory and crashes
are you spawning or forking?
hmm im not sure what spawning and forking is
i believe im spawning if youre referencing the creation of the thread
ok
yea, I mean multithreading this may or may not speed things up
probably will because SQL IO is long
our intention is to be able to change the rate of inserts per thread,
for example thread 1 inserts every 5 seconds while thread 2 inserts every second
we are emulating sensors with varying poll rates
but im starting to think more and more just use seperate scripts and say its easier this way
you should make time between inserts an argument for the script
python3 insertdata.py 5
Python - Command Line Arguments - Python provides a getopt module that helps you parse command-line options and arguments.
so you have a script that is python3 insertdata.py 5;python3 insertdata.py 2;so on
in general, I think argparse is preferred to getopt
If I'm not wrong, getopt is depreciated in favour of argparse
Isn't None fundamentally just a sentinel with a falsey bool value?
Should be pretty much just a singleton with the false bool value and some additional small things like the repr @boreal umbra here's the relatively short implementation https://github.com/python/cpython/blob/master/Objects/object.c#L1511-L1632
anybody use any of the libraries here for data validation or derivatives of them for dictionaries from external sources (for example, fetched from an http request, etc)
https://github.com/mahmoudimus/awesome-validation-python
kind of just looking for a simple validation library that i can write my own custom rules for and isn't too complicated but would be a lot of libs to test out by hand lol
rip advanced-python
it's only a name change
@boreal mantle i use marshmallow and jsonschema for this
I actually really would love a library that automatically generates a marshmallow schema from a jsonschema
Or vice versa although that will entail some restrictions on the marshmallow schema
does python have a built in code generator? i need to generator some repeated code
no, there are no convenient macros. Your best bet is making it in a loop or possibly eval
hmm, when you say eval, can i generate code that can then be used in the project it self
@narrow kettle would it not work simply as a function?
well its generating functions
if thats what your asking
in C# id use a source generator
to get an idea what i want
can i see what your use case is?
what is the actual goal here. It should be possible by setting module attributes or similar depending on what you are doing
i had an idea to generate decorators for all the events in my messaging system
ive been working on this for some time, with several different ideas but none have panned out
so you have a bunch of event classes and want decorators for them?
i have a bunch of events
the events are just strings
i need to generate decorators for each one
you can make a function inside a function
yee thats how decorators work
like ```py
def x(num):
for i in range(num):
def f():return i
yield f
so you could gen all your decorators like that?
can i then use them in the project
for event_string in events:
def decorator(): ...
globals()[f'on_{event_string}'] = decorator
``` do be careful that you cannot use event_string inside the decorator function
would be the shitty metaprogrammy way
or you could create a dict then add that dict into your modules scope, or put them as properties on a class
i want the freedom to have the event name and the function name differ
ye, this would be better as class attributes.
You can assign new attributes to objects after they've been defined and even instantiated.
ya thats what ive been working on
Python can generate source from AST no?
class events:
...
setattr(events, "on_event_2", func)```
i need to apply the decorator at instance creation
you can
Not possible with the way classes are defined
i did it yesterday, the problem was it doesnt work with decorator args
one sec ill show you
i mean you can hook class creation
Can't you do it with __new__ or something?
technically
hmm
oh, do you have the
class A:
@self.dec
def fun(self):
...
```problem
Yes exactly
The point is it's not built into the language and you have to use your own introspection logic to get it to work
ye, there are a few ways to get around that
There are several ways to implement it
But none of them are built into the language
Personally I think overriding attribute access is the wrong way to go about it
You should do what the attrs lib does imo
"Mark" a method with a decorator
that sounds like the thing that dpy uses for cog listeners, maybe you can take a look at it
Then in __new__ replace the marked methods with the decorated version
@final whale that too
one sec let me sort back through my git history to find what im talking about lol
from functools import partial
class Decorator(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
print(f'__call__ {args}')
return self.func(*args, **kwargs)
def __get__(self, instance, owner):
print(f'__get__ {self}')
return partial(self.__call__, instance)
class ham:
@Decorator
def eggs(self):
print('hello from eggs')
this allows you to get the instance ref
which you can then use to assign the decorated method reference
originally i wanted to have it so i could do @Set('the_event_name')
but for the life of me i cant figure out how to get the acutal method reference and not just the object instance
but if i can generate those for every event then i can just have a non parameterized decorator for each event
@messenger.some_event
instead of @Messenger.Set('some_event')
whoop
@narrow kettle just make Set return Decortator
The class has to know about the decorator at object instantiation time
def set(name):
class Dec:
...
return Dec
like that code up there works for what i need, i just would need to have a decorator per event
how would that work chil?
could do that with __getattr__
@narrow kettle you can return a non-initialized class from the function so customize the class by event name
and just return the class
basically if i make it a general decorator for all events then i need a way to get the acutal method reference from the decorator
and do whatever instance hacks inside that class
wait so what result do you want?
ya it would be easy if i could just make it so the name of the event has to match the method name
@narrow kettle look at how its done in attrs and discordpy
what file is that in
discord/ext/commands/cog.py
wait so what result do you want?
i want the flexibilty to do something like this ```py
class SomeService:
@messenger.Set('some_event_name')
def some_random_name(self):
pass
i can do this rn
class SomeService:
@messenger.some_event_name
def some_random_name(self):
pass
but if i did that id want to generator all the event decorators instead of typing them
hence the generator question
ahh ok, this d.py thing might be what i want
hmm
maybe with composition
but out of curisoity IS there a good way to generate python code?
that can then be referenced later
i mean you can generate AST and compile it/convert it to source code
and you can use eval
but there isnt really any good way
does eval add those types to the current running assembly
exec would
so far, here's what I could come with
import functools
import typing
Result = typing.Union[typing.Any, Exception]
class EventHandler:
def event_handler(self, name: str = None, /):
def wrapper(func: typing.Callable):
handler_name = name or func.__name__
if curr_handlers := getattr(self, handler_name, None):
curr_handlers.append(func)
else:
setattr(self, handler_name, [func])
@functools.wraps(func)
def wrapped(*args, **kwargs):
return func(*args, **kwargs)
return wrapped
return wrapper
@staticmethod
def _call_handlers(handlers: list, *args, **kwargs) -> typing.Generator[Result, None, None]:
for handler in handlers:
try:
yield handler(*args, **kwargs)
except Exception as error:
yield error
def dispatch(self, event_name: str, *args, **kwargs):
handlers = getattr(self, event_name) # might raise
return tuple(self._call_handlers(handlers, *args, **kwargs))
just saw this @final whale i appreciate it ill take a look
@paper socket This is not a help channel, please read the channel description. If you want hep with a specific question, check out #βο½how-to-get-help
Personally, I don't see why a chatbot is better than a wiki-like page
Python is often used for chatbots, so if you know Python, use it.
There is no single framework for bots since they work very differently on different platforms. You can search for a Python library for your specific platform.
@final whale I would rather do name: str = "", or name: Optional[str] = None. I'm not very fond of type annotations everywhere (it's obvious that name is a string, and func is a callable), but if you are, make them accurate.
Also -- what's the point of the wrapped function? You can just return func.
Another possible issue: name clash. handler_name could clash with some other attribute, so maybe put the handlers in a _event_handlers attribute that would be a dict.
I am still with None-aware operators in my mind 
oh we changed the channel name
lol did you guys see what went down in python-ideas mailing list?
If "explicit is better than implicit", why there is no @instancemethod, for example?
because it is default behavior I guess
explicit is better than implicit if implicit could be ambiguous
which in this case it's not
there should be one β and preferable only one β obvious way to do it
macro_def ignore_error($line, $errors):
try:
return $line
except $errors:
return
x = ignore_error!(1 / 0, ZeroDivisionError) # will be None``` my brain is throwing weird ideas now 
are you rusting?
Oxidising :D
rusting very
def _ignore_error_fn():
try:
return 1 / 0
except ZeroDivisionError:
return
x = _ignore_error_fn()``` can get translated to this I guess
rusting in #internals-and-peps 
Thank you for the suggestions, will fix @grave jolt
you know how _ prefix makes variables private in the sense that you shouldn't access them and if you do and break something it's your fault...
does the same apply to dunder methods?
if you mean manually calling dunders, it should be avoided, but is needed for things like __debug__, __file__, __name__, ...
ok but someone manually calling __new___ and __init__?
generally a bad idea unless you are doing super().__init__. Sometimes needed when doing more complex with classes
ok, so no one should be calling them in a way that's semantically different than using standard syntax like super().__init__ and Class()?
well, shouldn't is a strong word. Abusing the object system in python is pretty common for various libraries, but if you are just working with normal, not horribly mangled python objects, you should just use Class()
super().__init__ and Class() both do different things so I wouldn't try equate them
Class() calls new as well right?
they are entirely different
Darn, that new is supposed to be dunder*
You should use builtins to call the dunder methods where available
lst = [1, 2, 3]
s = str(lst) # good
s = lst.__str__() # bad```
Class() calls __call__ of the Class type and then __new__ and then __init__ and possibly others (for example dataclasses have a __post_init__)
ohhh that's what __call__ does
was very confused, cuz in c++, you're not allowed to have static operator()(...)
You can define your own __call__ for instances
class A:
def __call__(self, x, y):
return x + y
>>> a = A()
>>> a(1, 2)
3```
but not as a classmethod?
no. You need a metaclass for that afaik
ah ok so it is like c++
I think this is more of an opinion question so I don't want to post it in one of the help channels.
What I'm doing atm is: I start from a list of keys, these are also the keys in my database. I have a data object in which I store information, and that contain some helper methods like representaion and serialization.
Then I have a lookup objects. The reason I want to maintain state is that it can be called several times during the overall process and I can often resuse the results. Also yeah, just felt cleaner to me.
(below you find a minimal example)
However I like being explicit. And for me that also means I like to return things. In gerneral none of the methods would have to return, since I'm always mutating objects. But I could come up with one in each case.What do you think?
class MyDataClass:
def __init__(self, key, datum01=None, datum02=None):
self.key = key
self.datum01 = datum01
self.datum02 = datum02
def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True,
indent=4)
def get_data_from_db(keys):
samp = [['001', 'foo', 666], ['002', '#googleMurrayBookchin', 42], ['003', 'barfooo', 2]]
return samp
class MyLookupClass:
def __init__(self, config):
self.lookup_dict = {}
self.config = config
def prepare_lookup(self, data_objects: List[MyDataClass]):
keys = (obj.key for obj in data_objects)
for key in keys:
self.lookup_dict[key] = ''
def populate(self):
db_response = get_data_from_db(self.lookup_dict.keys())
for row in db_response:
self.lookup_dict[row[0]] = row[1:]
def update_objects(self, data_objects: List[MyDataClass]):
for obj in data_objects:
data = self.lookup_dict[obj.key]
obj.datum01, obj.datum02 = data[1], data[2]
from look_up import MyLookupClass, MyDataClass
def this_happens_in_flask(keys: List[str]):
config = {}
look_up = MyLookupClass(config)
data_obj = [MyDataClass(key) for key in keys]
look_up.prepare_lookup(data_obj)
look_up.populate()
look_up.update_objects(data_obj)
that seems quite overcomplicated. Why does one need to call 4 functions to get the data. I would just do
class LookUp:
def __init__(self, keys):
self._lookup = {}
self.keys = keys
def get_object(self, key):
if key not in self.keys:
raise KeyError
return self.lookup[key]
@property
def lookup(self):
if not self._lookup:
self._lookup = {key: MyDataClass(key, *data) for key, *data in get_data_from_db(self.keys)}
return self._lookup
def this_happens_in_flask(keys: List[str]):
lookup = MyLookupClass(keys)
results = lookup.lookup
```unless there are other requirements which mandate having so many functions for fetching data
So yes there is a bit more to it of course. Populate for example also does some postprocessing on the data. And the update process is more complicated. And it's more about the general question not the application here. Imagine the complexity is warranted. Would you explicitly return or mutate?
Also I've recently watched Brandon Rhodes on Clean Architecture. And I kinda tried to follow the priciples by not burrying my I/O opperation. And the person I talk most with about coding is a reactive kotlin programmer that follows that shit pretty zealously lol
@raven ridge (or anyone else who does c stuff), you think 3 pointers worth of indirection is too much for a python function call? (like atomic_compare_exchange_strong)
@thick topaz This channel is for indepth discussion about the Python language. If you want help or feedback, check out #βο½how-to-get-help
oh ok
@gloomy rain since you are here, would you say my question if in depth enogh or shoudl I also rather ask in a help channel.
I think at face value it seems more like a help channel situation.
If you need help in general, I think it's better to use a help channel.
If you just want to discuss something related to the Python language, this channel is more suitable.
But it's a bit of a gray area.
You're asking for a code review here basically.
Hi. I want to count the possible multiples of 3 in range 50 to 500. How can I do that?(I'm a beginner so the simplest way possible please )
@unkempt rock This channel is for indepth discussion about the Python language. If you want help or feedback, check out #βο½how-to-get-help
how did Guido created python???
and is it really very easy creating a language as such ?
He presumably wrote the first draft of an implementation in C. No, implementing a practically useful programming language is not easy.
ooo
For the compiling part, Guido mostly followed the parser and lexical analyser implementation outlined in the dragon book, if you are interested
how did he even have the effort to do it
Same way anyone has the effort to write a language
how to have effort
Same way as anything else you work hard at π€·ββοΈ
I doubt the first python code was executed the first day of its developent
Cresting a programming language (the syntax and rules) is easy, just have imagination and done
But creating the interpreter/compiler is hard work
You need to scan if the syntax is okay, and then parse it to convert it to machine code or bytecode
Ehh, tbh the hard part is the syntax and the idea lol
Basic Parsers and lexers arenβt THAT hard
And thereβs a million resources on how to do them
The hard part is having a consistent idea for what you want
and btw to convert it to machine code you need assembly knowledge
how are you supposed to write a C compiler from scratch without assembly knowledge
welp, with python is different
the result is bytecode
theres many different ways to approach such things
some more difficult then others
but the actual coherent idea/spec is the hard part
the other stuff is tedious but can be learned
python is at the same way powerful but sometimes inconsistent
its not perfect no, no language is. especially one as old as python
things change, ideas evolve
pep8: classes must start with uppercase and follow camel case
str, int, or bool go brr 
or float
builtins go brr
but that differentiates them as primitives persay
same in C#
int is just a alias for Int32
I think lowercase in built-in class names is fine, the only gripe I have is some inconsistencies in methods/dunders
In case of str, most if not all method names are just joined, while other classes tend to have underscores separating the words
Similarly, some dunders just join the words, like __getattribute__, while newer ones seem to use underscores, like __set_name__ and __class_getitem__
That's mostly because they came before the style guides or depend on something that doesn't follow python's naming
I understand why it's like that, and I don't mind it that much, but it's still a little annoying at times
It would probably also be confusing to beginners which builtins are capitalized and which are not
isn't python's variable naming snake_case?
ye, but str, int, type, bool, ... is a class, which follow PascalCase
everything but constants and classes is snake_case
functions, modules, attributes, methods, variables, packages are all snake_case
logging module flashbacks 
why don't multiprocessing.Process objects support the context manager protocol
@paper echo i don;'t have an answer, but: contextlib.closing() could help
yep, thats a handy one
class multiprocessing.managers.SyncManager```
A subclass of [`BaseManager`](#multiprocessing.managers.BaseManager "multiprocessing.managers.BaseManager") which can be used for the synchronization of processes. Objects of this type are returned by `multiprocessing.Manager()`.
Its methods create and return [Proxy Objects](#multiprocessing-proxy-objects) for a number of commonly used data types to be synchronized across processes. This notably includes shared lists and dictionaries.
`Barrier`(*parties*[, *action*[, *timeout*]])[](#multiprocessing.managers.SyncManager.Barrier "Permalink to this definition") Create a shared [`threading.Barrier`](threading.html#threading.Barrier "threading.Barrier") object and return a proxy for it.
New in version 3.3.
`BoundedSemaphore`([*value*])[](#multiprocessing.managers.SyncManager.BoundedSemaphore "Permalink to this definition") Create a shared [`threading.BoundedSemaphore`](threading.html#threading.BoundedSemaphore "threading.BoundedSemaphore") object and return a proxy for it.... [read more](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.managers.SyncManager)
huh
this looks like it's meant more for e.g. wrapping non-multiprocessing-aware things, no?
e.g. sharing a value or array or threading.Lock across processes
Does close actually do anything tho?
evidently its not necessary
Is there a reason you can't join?
@languid dagger they wanted to run 2 processes then kill both after one of them finished
Maybe you want concurrent.futures.ProcessPoolExecutor so you can wait for futures
tbf if its not cpu or io bound async might be better also
@languid dagger can you wait for "first completed" with the executor?
i know you can with asyncio
ummmm
Hey, I got a problem with a small double for loop problem. Im getting a "float" object not iterable error. However, I have no idea what is wrong. Here is what I have ``` for i in range (n):
avg += x[i]
avg*=(1/n)
m=[2,3,4]
new_m=[]
for j in m:
for i in range(11):
m+=(x[i]-avg)**j
m*=(1/n)
new_m= new_m.append(m)```
with the following error.
this being the eq I want to perform
If I could get some help that would be much appreciated. Thank you in advance
you're trying to iterate through m (for j in m)
but you're adding to m
so is it an iterable or not
maybe try naming your variables something less confusing?
brackets didnt change anything in regards to the error :/
@remote lantern it seems like I understood what u mean but It came up with a different error later down
hmm
that sounds... concerning
strange
do u have any idea?
i think it's the line new_m = new_m.append(m)
append() returns None
so when you try to give that value to new_m itself i think new_m turns into a None object?
that's my best guess
just do new_m.append(m) and it should work
also may i recommend learning about list comprehensions
very useful and may compact your code
Python comprehensions are a very natural and easy way to create lists, dicts, and sets. They are also a great alternative to using maps and filters within python. If you are using maps, filters, or for loops to create your lists, then most likely you could and should be using ...
just do new_m.append(m) and it should work
@remote lantern same error came up :/
nvm i got it
indentation error
@clever schooner @remote lantern For future reference, such conversations are better placed in the help channels, rather than #python-language. As per the description, this channel is intended for discussion of advanced language features, PEPs, etc; not for providing troubleshooting assistance.
That's okay. If it occurs again, you can redirect them to a help channel, then have them @ mention you if you are willing and able to assist with the problem.
alright
Okay so i need some help regarding scheduling my tasks
so at first i have one ,
my_list = ['94230', '40737', '06959', '62168']
now i want to print the first value of my_list....which is '94230'
the i want to keep the same code for 2 days/48hrs then i want to delete
the first item of the list which is '94230' then print the second item
of list which is '40737' and keep on following the same pattern....
so how should i do that?
a loop with time.sleep()?
but also not the right channel (i feel the channel name might be misleading)
@red solar its regarding python stuff right?
Discussion about these concepts. I think your question may be suited better for one of the topical channels or a help channel.
@brave badger Sorry didnt read the pins
I want to make a OS with Python, this will be a simple OS, like ms-DOS (Maybe more simple lol). Can I do it with Python?
@quick elbow with cpython definitely not
also if you want to make an os like msdos you must learn x86 assembly
please visit these two websites
https://osdev.wiki/
https://wiki.osdev.org/
for me nope
I just want to make a ultra simple os
https://github.com/pritamzope/os simple oses
TempleOS πΌ
filesystem, keyboard input, screen output, api?
btw you can't create an os with cpython
@quick elbow *x86 assembly
there are many assemblies
powerpc, arm, x86, mips, 6502, jvm
AVR, RISC-V...
m68k...
w8 this is offtopic 
my project was:
it has a simple mail system that can send mail to gmail/outlook . blah blah...
and
thats not simple lol
it dont have a ui
thats not simple lol
@true hollow I did it with python in vscode π€¦ββοΈ
you need to implement:
β’ network driver
β’ some keyboard driver and screen driver
β’ and then create the mail client
oh lol
"just a simple OS" famous last words
ok x86 isnt so easy to learn..
if I can create a msdos like OS I will buy a soooo old computer lol
is x86 is byte?
what?
May we move to DMs and let moderators purge this offtopic conversation?
gogo dm
Hey, can anyone tell how to keep bot connected forever?
Cuz whenever close the application in which python I running, bot goes offline
@unkempt rock This channel is for indepth discussion of the Python language. If you need help, you should check out #βο½how-to-get-help.
@quick elbow sure you can, you will need to:
- implement python yourself in C or assembly
- implement a PE/ELF loader in C or assembly
- implement your own executable format then write a compiler mod to compile the exiting CPython source into it (you will need to account for expected platform resources like threading)
the issue is that python, itself, isn't granular enough and relies on a lot of resources behind the scenes
for example, python heavily relies on a garbage collector which requires a heap, which requires a memory manager, etc
@true hollow assembly itself isn't hard but to implement an actual OS is
there is a reason why each instruction listing in the intel manuals has barely a couple sentences describing it but has entire volumes dedicated to memory management or execution modes
@unkempt rock ?
It would be much easier to lean a language more suited to OS dev (C, go, possibly rust, zig, odin) than to make an OS in python.
@subtle pike thanks for info
@true hollow why are you trying to convince people x86 is easy π
because it is
modern programmers portrait is as a hell language only for people with 200 iq
getting things done is ASM is more tedious than difficult
because you do not have cheap abstraction that other languages give you
Doing stuff in asm is tedious, and you need to know a fair amount and canβt make any mistakes (python is significantly easier in that regard) - doing stuff well in asm takes a whole lot of in depth knowledge of your target architecture
Maybe you could say some RISC ISAs are simple....
But I wouldnβt say that about x86
welp for me x86 assembly is easy
gimme docs and what i need to do and done
The docs are the huge book that is the intel developer manual
Right of the bat thatβs significantly harder to read than python docs
That and agnerβs stuff
read x86-64 amd docs instead
Yeah doesnβt look significantly simpler
what are people's thought on the overrides library, or explicit overrides in general?
Seems pretty cool
Someone indicates a free Python OOPs concepts course, please? Thanks
Sorry for my english
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
not the correct channel
@royal cliff This is not a help channel (see the channel description). Please check out #βο½how-to-get-help
Why are the types returned by keys, values, and items for dict not lists?
Making them lists would result in slower code because of the extra copying
Plus they have a feature lists don't, they update as the underlying container does.
I don't understand the added utility of them updating along with the dict itself, since you can call those methods at any time
Unless they're just a window into the data structure and don't actually cause anything new to be created
Yes, that's what they are afaik
And the fact that they have the external behavior of self-updating is a side effect
In that case, why are they methods rather than at the ivy?
Attributes?
Or maybe those methods have arguments that I don't know about.
But they didnΓ€t exist back then
Did the code underlying properties exist though?
I think I remember reading that properties are a utilization of some language feature where you have untouchable objects that implicitly affect what the dot operator does for classes that use it.
Not sure, I suppose that is a good point. They're implemented in C so they wouldn't need to depend on the public properties API
But I think I misunderstood your question
Yeah, I don't really see why the view would need to be re-created each time if it's dynamically going to update. It could just be created once and stored as an attribute
In that case, why are they methods rather than at the ivy?
The methods predate the "view" behavior. Previously they did return lists. https://docs.python.org/2/library/stdtypes.html#dict.keys etc
I didn't get that impression based on your answer
Makes sense.
I don't know that it's plain and simple
I think I remember reading that properties are a utilization of some language feature where you have untouchable objects that implicitly affect what the dot operator does for classes that use it.
@boreal umbra You're describing "descriptors"
Since lists are the primary structure for ordered data
yes, but copying is slow. It is that simple.
They could cache the view objects but presumably they don't since it's not worth it.
Right, but most python users don't know how the language is implemented, so it wouldn't be immediately obvious that using lists would be problematic in this case.
What's faster in C, passing an entire array or passing a pointer to that array and an int indicating the length?
https://docs.python.org/3.0/whatsnew/3.0.html#views-and-iterators-instead-of-lists describes the change in terms of efficiency.
It's really that simple
https://www.python.org/dev/peps/pep-3106/#introduction also says that the motivation was to return a lighter weight object. Just performance.
It would be okay if multiple calls to d.keys() (etc.) returned the same object, since the object's only state is the dict to which it refers. Is this worth having extra slots in the dict object for? Should that be a weak reference or should the d_keys (etc.) object live forever once created? Strawman: probably not worth the extra slots in every dict.
Neat thanks
Well, I'm only objecting to characterizing this as plain and simple from the perspective of those who don't know about or aren't interested in low level implementation details.
yeah - I agree that it might be non-obvious, but it is "plain and simple" in that the one word answer "efficiency" is correct.
But yes, descriptors are what I was thinking of. I've been meaning to learn about those, so thanks for reminding me of the name
Wow, 2006
nedbat pointed to https://nedbatchelder.com/blog/201306/explaining_descriptors.html the other day as an excellent article for learning descriptors.
I was wondering if this is correct, about python
Probably not, but I can't quite tell what they claim, so it is hard to tell. Python definitely does not do anything other than tokenization key by key though
Got some "clear" explaination
Python does have an ast which it does use (though linters afaik use a modified one due to comments and other things the default one does not keep). Hell, python gets compiled to bytecode before running.
And that compilation is cached for future use to speed up loading
the _pycache_?
Ye
Wrong from what I can tell. I mean, the translation from tokens (you can see how those get made with the tokenize module) to ast obviously goes over every token. In regards to parsing/compilation python is not all that different from java and some C impls
Python does have a grammar, it is somewhere in the docs
I'm not quite sure what they mean by "scan the script by each keyword"
ye, he clearly has no clue what he is talking about. The python tokenizer is quite normal
and Python's grammar is fully listed here: https://docs.python.org/3/reference/grammar.html
well, the python lexer is a bit irregular due to handling indentation
I mean, other than that
Indents dont really matter
he is like comprehensions suck and idk what not
At some point, it's just better to step away from the conversation when you realize that it's not going anywhere productive.
@pseudo patio If you require help, please refer to #βο½how-to-get-help as this is not exactly the proper channel for that
I have an idea
suppose this syntax were legal
from typing import Dict, List, * as t
Dict and List are brought directly into that namespace, and then the whole module is imported as t
Hi
what language should i learn after learning python
if i don't want any front end website
None. You have won programming by learning Python.
C++ is interesting, but learning C++ takes about a lifetime
ooo i see
also, go to off topic probably
This particular channel is for talking about the Python language itself
Also I still crave validation for my proposed import syntax, but feel free to trash talk it as well.
it would be nice sugar, but I see pretty much no advantage over
from typing import Dict, List
import typing as t
``` Though python could use a bit more terseness, it is quite verbose
have you used Java?
yes
In that light I'm not sure I agree with that assessment about Python's verbosity.
I'm not sure if introducing the new syntax would be worth it as it's not as explicit, and importing like that is rarely done
I guess it's not explicit in the sense that a * isn't self-explanatory, so you'd have to look up what it's doing in that context.
though it's not importing anything that isn't explicitly stated.
I mean, there are more verbose languages, but we do not call java terse just because COBOL is more verbose. Compared to haskell, ruby, raku, some LISPs, C#, D and JS in some cases even, python is quite inexpressive.
from typing import Dict, List; import typing as t

I'd also expect a * to do something different at first, with it being for unpacking or as the star wildcard on imports
do you find yourself importing the module and then something from the module, separately, often?
ye, it is quite inconsistent in that, though it is quite intuitive
often I want to import the items I plan to use most frequently by name
when I see it I usually just assumed it was 2 people working on the module together and they didnt notice one or the other was already done lol
and then I still want the rest of the module
I would include from typing import Dict, List, * in the same proposal, which in this case would be the same as from typing import Dict, list; import typing
Perl is more terse than Python, but you also have to spend eleven years figuring out what all the shorthands mean π
I'm not sure how I'd feel about the star behaving differently from just from typing import *
since if you do that you'll already have the Dict and List available right
in the syntax I'm thinking of, if you import something explicitly, followed by a star, the star doesn't implicitly import everything
it imports the module itself by its own name.
which would be confusing, yes
maybe we should just remove star imports and replace them with this behaviour lol
honestly, in 99% of the cases, I either use qualified imports or unqulified imports, not both
That gets support from me, but we'd have to wait until python 4
I don't recall mixing them intentionally either
unless we decide that people who use wildcard imports don't deserve to have their code work
which I also support.
Anyway I guess I can go to python-ideas but I imagine unless I include lots of caveats, they'll assume I have no design sense and I'll be eaten alive.
In 3.9 you won't have (in most cases) to import typing anyway 
ooh π
(I'm referring to https://docs.python.org/3.9/whatsnew/3.9.html#pep-585-builtin-generic-types)
Nope, there only are release candidates betas for now
oh ok thank god i have time for 3.10 then
if c++ can do it, python can do it π€·
Well, if C can do it CPython can do it 
It's not that you couldn't do it before, it just wasn't part of the stdlib
@undone hare so basically the builtins are getting __class_getitem__?
Here is the related PEP with the implementation details 
Hey. Just a heads-up. HumbleBundle is having nice sales on Python courses
https://www.humblebundle.com/software/python-programming-software?partner=limitedtimeoffer
In 3.9 you won't have (in most cases) to import
typinganyway
@undone hare is 3.9 released?????
@grand field That would be better in #python-discussion or an off topic channel, not this one
@wary plover Please don't advertise your channel, just wait for help.
@true hollow not yet, only the betas are available right now
I'll consider building one of them
All released and un-released version have a what's new page
Even 3.10 has one
I'd be surprised if they aren't published binaries for 3.9
If we assign a attribute to a class
say:
class FooBar:
def __init__(self):
pass
def foo(self):
pass
def bar(self):
pass
How come we cant then remove these attributes from a instance of it using
delattr
I don't see attributes? Only methods?
mhmm
yet we can assign the same methods with setattr and remove them like that
but then we loose the ability to reference self which annoying
It would be nice if we had the ability to modify methods like attributes instead of say doing it with a decorator
Oh interesting, hmm I'm not sure so you'll have to wait for someone more experienced
yet we can assign the same methods with setattr and remove them like that
these will not be bound methods
yeah, which im assuming is also why we cannot reference self which makes sense
You can bind it when assigning to the instance
You can't delete the methods because they don't exist in instances
the "nicer" way to bind them when you already have an instance
In [11]: from types import MethodType
In [12]: class MyClass: ...
In [13]: def func(self): return self
In [14]: inst = MyClass()
In [15]: inst.func = MethodType(func, inst)
In [16]: inst
Out[16]: <__main__.MyClass at 0x67ec148>
In [17]: inst.func()
Out[17]: <__main__.MyClass at 0x67ec148>
@grand field is the content good though?
Yoo
How make a thumbnail in the embed
does List become obsolete then?
It will stay for backwards compatability, but I think yes
type hints are a wild ride
Wait, what's changing for type hints?
soon you can do list[int] instead of needing List[int]
Ah
That probably simplifies things
It's going to break current typehints on leetcode yaaaaaaaay
It won't break anything I would think.
oh so you just don't have to import from Typing
The old typing.List are not removed, though they may be depracated soon
The PEP mentioned them being deprecated after builtin generics are implemented, but won't raise DeprecationWarning since it's just typing
having it supported on builtins would be great, yeah
If you do keep using them from typing for whatever reason then your code will break in the future
Good
Because having to do from Typing include * gets annoying as fuck
I don't even use mypy but I still like adding type hints for completeness
You can put in whatever u want really
The builtins are great but I don't think it's that much of a bother as most large projects will have a considerable amount of imports anyway which can be skipped over
Like if I want an arg to be a dataframe I just do pd.DataFrame
def func(df:pd.DataFrame)->pd.DataFrame:
Clear and concise
hello is anyone experienced with django
@wraith brook #web-development π
this channel is to discuss PEPs, language design, the future of this language, etc
yes i already was talking there sorry
nah don't worry
If I were to turn a website's API into a library, would it be better to use requests or aiohttp?
depends whether you want it to be asynchronous or not
using requests, your library will be easier to use for synchronous applications, whereas using aiohttp will make your library appropriate for async applications
it'd probably be very difficult to create something that will "just work" for both
usually you have libraries that are either strictly sync, or strictly async
Fair enough, I get what you mean. Thank you!
yeah no worries - it may be worth looking into whether there already exists a wrapper for the API, and then you can do something that the existing wrapper doesn't do
e.g. "while library A already wraps this API, it uses requests and blocks, my implementation is asynchronous and therefore appropriate for async apps" would be a good selling point
Makes sense, thanks!
I have an odd question, when doing a loop with an iterator that is the element and not a numerical incrementor, are you able to change what is in the existing array using that element iterator? Also is there any way to check to see which location that the element iterator is inside of the data structure that you are looping through?
Element:
for i in list:
// code
Regular:
for i in range(len(list)):
// code
no it's not possible, if I understand your question correctly
enumerate() is one option
>>> nums = [1, 2, 3]
>>>
>>> for n in nums:
... n = 0
...
>>> nums
[1, 2, 3]
but only for the second part, knowing where the element came from
the loop binds the value to a new name n
iterting will create a new reference, with which youΒ΄can't access the reference inside what you're iterating over without some provided methods
you can change the value of n, but it doesn't write to the list
similarly:
>>> def func(n):
... n = 0
...
>>> number = 1
>>> func(number)
>>> number
1
Ok, I was wondering how much more magic python had with loops. The majority of my coding experience is with C and Java so coming to a language where looping can be done is different ways was very new. It also still blows my mind that there is no variable scope in methods
Thank you
Java also has a foreach loop
Java has different ways to return an enumerable too
@rigid tundra what do you mean, "no variable scope in methods"? Methods have local variables.
!e
def scope():
for i in range(1):
x = 3
return x
print(scope())
You are not allowed to use that command here. Please use the #bot-commands channel instead.
@spark magnet are you actually like a teacher?
@flint fractal not formally π
alright
those are not methods, but yes, control flow does not have its own scope
Blocks don't create a new scope, i guess you meant.
@spark magnet Blocks?
the statements inside the "for" loop
if 5:
"this is a block"
i'm not sure they are actually called blocks. sometimes they are called clauses.
those are not methods, but yes, control flow does not have its own scope
A VERY confusing thing when i started
some people will tell you, once you've learned one or two languages, other languages are easy because you just need to learn the new syntax. those people are wrong π
it is called body in ast it seems
So, humor me on what exactly I said incorrectly
I mean once you know one Lang itβs relatively easy to transfer concepts
ah, I can tell what you meant. Within a single method, there is no variable scope
which is not quite true either
A method is a function defined within a class. They have their own scope, local variables ,like any function.
A bit harder going from paradigm to paradigm
def fun():
a = 5
def inner():
a = 8
inner()
return a
print(fun()) # 5
One think that could help is realizing that a "method" defined within the namespace of a class is just a regular function
Basically, this does two things:
def foo():
pass
Python will make sure you've got a function object in memory and you assign a name to that function object
While variables work a bit differently in R (copy on assignment), R does show nicely what happens:
name <- function(a) {
print(a)
}
hello. im learning python since few days and i couldnt understand how to use global variables. what am i doing wrong here?
def func1(a):
global b
b = a * 2
return b
def func2():
global b
c = b * 2
print(c)
You didnβt declare a variable
So,
def foo():
pass
and
name = "hello"
work very similarly
If you then move that same logic to a class definition, you see what happens:
class MyClass:
name = "hello"
def foo():
pass
You're creating objects as usual (a str object and a function object); it's just that the names here are class attributes
So, MyClass.foo will be assigned to the function object in memory and MyClass.name to the str object
The function object is just a normal function object
That's why you could also do:
!e
def my_odd_method(self):
print(self)
class MyClass:
method = my_odd_method
obj = MyClass()
obj.method()
@wide shuttle :white_check_mark: Your eval job has completed with return code 0.
<__main__.MyClass object at 0x7fa03da3f9d0>
There's nothing special about the function objects you define within a class namespace
The "magic" of inserting the instance (for self) happens when you access the class attribute on the instance of the class
So, again humor me please. I would like to go back to what I said initially. I said "no variable scope in methods", wherein I meant that you can create a variable where ever you want inside of a method and it exists. As @flat gazelle pointed out, this becomes invalid when I created another method within a method which directly proves my statement incorrect. How can I tailor my statement to be correct? Would it be only that there is not scope within "blocks" as @spark magnet said?
ye, I would say blocks do not create scope. Also method != function in python
as per Ves
@wide shuttle Also thank you for the little lesson on objects
"methods" are just regular function objects, so local names will go out of scope
However, we typically mutate the object the method is "acting on"
Like self.attribute = something
You need to use nonlocal same as with global for nested functions
I learned something today π
@rigid tundra i see, it was the ambiguity of "in" that tripped us up.
"There are no new scopes created within methods other than the method scope itself"
@spark magnet Yea, I thought that I had said in it in a correct way and I was confused when you guys said it was wrong, but I understand how I phrased it incorrectly
it wasn't incorrect it turns out. it's just hard to be super-precise.
phrasing is hard, but you had the right idea, and that is what matters
Wait wait wait.... so even though it was ambiguous I'm technically right with my phrase? I thought that because of the ambiguity of the use of "in" that it was incorrect because you can declare another method inside of the method and still have scope, and the way that I phrased it I said "no variable scope in methods" is false with that example mentioned.
that is kind of the sole exception. But it is clear that the statement does not consider that as the claims it makes cannot be true in the case of nested methods
cant you add variables to input texts?
@flat gazelle Thank you for the clarification, my biggest fear is always looking like an idiot because I didn't get the proper diction or phrasing correctly π
if someone calls you dumb for a slightly incorrect statement, they are the dumb one. Now knowing something is not shameful
So unfortunately I was never able to take a course in python directly, I had many projects through courses especially in my last year that got me to pick up python, and I feel decent at programming in it. However, I definitely lack the understanding of the language complexities such as scope, and immediately before that loop indexing. Does anyone have any recommendations as to where I may be able to find a good overview of very language specific things that python has that other languages do not?
There are a couple of great videos on YouTube that could help you with getting familiar with some of the core mechanisms in Python
In fact, nedbat gave a couple of really good talks on PyCon
They explain things like how assignment works in Python (the relationship between names and values) and looping (that talk uses Python 2, but it's still great)
Ned Batchelder - Facts and Myths about Python names and values https://www.youtube.com/watch?v=_AEJHKGk9ns
Ned Batchelder - Loop like a native https://www.youtube.com/watch?v=EnSu9hHGq5o
There are also a lot of other great talks, by Raymond Hettinger (e.g., Beyond PEP-8 https://www.youtube.com/watch?v=wf-BqAjZb8M) and others
Other than that, there are great books like Fluent Python (the second edition will be released on Feb 9, 2021)
@wide shuttle Awesome! I will be taking a look at these. @wide shuttle @flat gazelle @spark magnet @sacred tinsel Thank you for your time
guys, i am learning python as my first coding language, and really have a hard time remembering all the
different commands and so on
any advice ? did you have this problem aswell?
I suggest just writing them down in one place for easy lookup. With practice, they stick
that is what I do when I am using a language I am not all that used to
generally, you want to google something python cheat sheet
@smoky torrent btw, it's better not to start the same discussion in two channels at once
alright, wont do it again:)
how do i normalize number between 0-20
>>> xs = [*range(200)]
>>> min_x = min(xs)
>>> max_x = max(xs)
>>> xs_norm = [20 * (x - min_x) / (max_x - min_x) for x in xs]
>>> xs_norm[:3]
[0.0, 0.10050251256281408, 0.20100502512562815]
>>> xs_norm[-3:]
[19.798994974874372, 19.899497487437184, 20.0]
@unkempt rock
that is a statistics question, not a python language related question
preferably, use the appropriate channel
Anyone aware of the speed of len()? Is it true that it's just O(1)?
There's no single time complexity for the len function, as it depends on how it's implemented for the type
For most built-in types, the len just gets read off of a field in the C struct, which does make it fast
Well, the most common implementation is Cpython, right?
but, since you can implement support for len using the __len__ dunder in your own user-defined classes, it could be anything
From what I've read here it's supposedly O(1) for lists https://wiki.python.org/moin/TimeComplexity
Well I'll need to run it on strings, so I'm guessing I'm fine
I think that page only lists it for list, @brave badger
Yep
strings should keep that info since they're constant size
but, for custom user-defined classes, you could write your own crappy implementation
I believe most, if not all, builtin sequences should keep it around since it's a lot cheaper to change on changes than counting when accessed
They should probably do that anyway, to keep track of the memory allocation
You could always cheat, of course
from collections import UserList
class MyList(UserList):
def __len__(self):
count = 0
for element in self:
count += 1
return count
linked list have O(n) length query. And are one of the few data structures where n can actually be infinite
Can someone explain to me what objects are
I already understand what classes are but I still don't understand objects are
Objects are the "realisation" of classes
If "human" is a class, then "you" are one object of the human class.
A class is like a blueprint. An object is like an actual item made from that blueprint.
@livid geyser Could you show me an example of what you mean?
Because it would work if you do this:
!e
def my_strange_method(self):
print(self)
class Foo:
method = my_strange_method
f = Foo()
f.method()
@wide shuttle :white_check_mark: Your eval job has completed with return code 0.
<__main__.Foo object at 0x7fba609bf9d0>
If I
class Test:
pass
def myfunc(self, *args):
pass
Test.myfunc = myfunc```
Test.myfunc doesn't get self
Wait what
I swear I've literally done that and it doesn't bind
Wait no it's bound in the class
Right, when you access the class attribute on the class object directly, you get the function object back as-is
!e
class Test:
pass
def myfunc(self, *args):
print(self)
Test.myfunc = myfunc
Test().myfunc()
You are not allowed to use that command here. Please use the #bot-commands channel instead.
but that holds for "normal" methods as well
oh
!e
class Test:
pass
def myfunc(self, *args):
print(self)
Test.myfunc = myfunc
Test().myfunc()
@wide shuttle :white_check_mark: Your eval job has completed with return code 0.
<__main__.Test object at 0x7ff72d6e69d0>
That works fine
Wait what
However, if you were to do Test.myfunc(), it wouldn't work
I have literally done that in my code before and it behaved weirdly
I'm so confused lol
!e
class Test:
def name(self):
pass
print(Test.name)
@wide shuttle :white_check_mark: Your eval job has completed with return code 0.
<function Test.name at 0x7f462243de50>
If you access the class attribute on the class object (instead of on an instance), you get the function itself back
Did that get changed at some point or am I going mad lol
It won't "bind" an instance, since there is no instance to bind
However, if you access the class attribute on an instance, you do get the binding
!e
class Test:
def name(self):
pass
t = Test()
print(t.name)
@wide shuttle :white_check_mark: Your eval job has completed with return code 0.
<bound method Test.name of <__main__.Test object at 0x7f11e95f19d0>>
As you can see, we're no longer getting a function object back, but a bound method
Yeah I know about that much
Maybe when I did it I messed up and bound it to the instance instead of the class
Did you assign an instance attribute to the function object?
(N.B. by bound I mean set attr)
I probably did tbth
My bad lmao sorry for the wasted time
Hope your dinner was nice lol
Still busy cooking
Oh ok lol
although I don't really have to do anything atm
Can I export python file with "pygal" package to browser without html and etc
i cannot use pyaudio on my pc its showing error download microsoft visual studio but i have downloaded new version
- This channel isnt really for help, 2) its not VS you need its c++ build tools if you read the error carefully
Oooh c++ stuff π
Hello


