#internals-and-peps
1 messages Ā· Page 137 of 1
sure but without it, if it were only as fast as regular CPython, it would still be a way better choice because of the possibility to have that just in time compilation
also, i don't see a realistic scenario where appending and popping from a list is the bottleneck
I do, I'm using a stack datastructure for certain things and I've used a list there, I will try collections.deque it may be better, I assume it's written as a C extension, but still, the performance benefit of that from PyPy is huge and it's a difference between running my program for 5 minutes vs running it for 40 seconds
this isn't just from the list operations, but it's a big part of it
well, run it with deque and let's see the numbers
yea, but I would need to copy it in every project. I do not like so
with deque, it's a lot better, but it's still faster with PyPy, even with a list there
numbers?
let me try and make a coherent example to test things rather then running it for my project only, give me a sec
I'd say that this is a fairly significant difference
this is it with lists, just for comparison
pretty big difference, i agree
for some reason, pypy is actually faster with a list here than a collections.deque, but I'd say that's a very insignificant difference and is within standard deviations
well, it's not the list or deque that is slow, it's the for-loop, i think
PyPy is well known for optimizing loops and other repeated calls, so that's not really surprising
if the algorithm didn't require a stack but rather an array, i'd propose to try to use numpy with numba
numba also is known to optimize loops very well
I'm not saying there aren't ways to improve the speeds with CPython, I'm just wondering why are we still using it if we could use something so much faster
because of limitations in PyPy?
what exactly are it's limitations?
aka the places where you usually want to go fast
it's sad and i would love to agree with you
Wait now I want to see the Cython comparison
i mean the whole notion "why can't we just replace CPython with PyPy"
it's beautiful
it probably wouldn't be all that faster than pypy, I think this is actually faster than C++
(With no optimizations though)
But again, Cython when popping and happening to lists won't speed up jack-shit because it used the Python API
So you're still as slow as CPython
That's the thing, it's not a fair comparison
If you have loads of computations then you can use Cython to bring that to pure C speed, but if you use the Python API a lot then you're still as slow as Python.
Does this stack structure have a maximum size?
this is interesting, but wouldn't just rewriting those modules (not that it would be an easy thing to do) fix this? I mean if we have something that's so much better in this sense, it may be worth to rewrite those things because after it's all done and ready, we could achieve much higher speeds with python and actually make python somewhat competing with other compiled languages to some extent
yes, it will never grow higher than 100 elements, but there's a lot of pushing and popping happening there
You say you used Cython and lists, why not use a 100 item C array? That will be fast afaik
i'd say it's doable, but it would probably require a company to step up and actually pay a couple of devs full-time to do it and be able to catch up to the level of CPython is..
I never mentioned Cython, I was only talking about CPython vs PyPy
I agree, but if the PSF supported that change, I think it could be done
and I think it would be a very welcome change it may even bring many developers who left python simply because it was way too slow
I just wanted to know if there was any particular reason it's not currently happening, I'd think we would always choose to go for the better option, even if it meant spending some time by rewriting some things, if it brings such a huge improvements
There is the adaptive interpreter pep
well, there is a project at microsoft to make python faster using bytecode level specialisation. It is not quite a JIT, since maintaining a JIT is a nightmare
especially with how many platforms python runs on
link?
CPython targeting a lot of platforms is definitely a concern for switching up the implementation
it's not really an issue with most things since C is mostly portable. But a JIT requires generating machine code per platform
we also have the "leaky internals" issue
I thought PyPy had a fairly good amount of platforms supported
yeah, stack access being a documented and supported feature does have its issues
many projects rely on specific behaviour exposed by inspect etc
is there some branch or a fork where this is already being worked on?
if PyPy can provide a similar API, it might be feasible, i'm not an expert though
Yeah I've read some documentation on that, sounds really cool and I am quite interested in how that will turn out
maybe the idea of having sub-interpreters might help
if we had an API shared between interpreters, different parts could be executed by different interpreters
shrug
doesn't seem so
Wait is that what subinterpreters mean?
I really don't understand the point, they sound like they suck
Because they don't yet bypass the GIL, you can't launch them unless you're using the C API and.. huh?!
I've read the PEP but I still left knowing as little as I did after starting reading
don't ask me, i just read over the PEP a while ago and dreamed up the rest :p
at least that would make sense to me, kinda
like, using subprocess, but specifically only for python processes
then you could mix and match interpreters that are best optimized for different scenarios
To me they just sounded like a weird mix of threads or something..
probably i'm way off and it's nothing like i imagine ^^
theoretically you can have a separate GIL for subinterp as long as the different interpreters don't touch the same objects
and have separate contexts e.g. imports of builtins etc.
sub-interpreters are such a cool idea but the API leaves something to be desired
they added a pure python module to use them but it's as rudimentary as possible
well I thouhht they did
But what does a "subinterpreter" mean?
Not added yet, but I read about it in the PEP
yeah guess we use
import _xxsubinterpreters``` for now
it works like a different process, than can do eval and send/receive strings
but thats pretty much the limit afaik
>>> import _xxsubinterpreters as sub >>> interp = sub.create() >>> interp
InterpreterID(1)
>>> sub.run_string(interp, "import _xxsubinterpreters as sub; ch = sub.channel_create(); sub.channel_send(ch, str(7 + 7))")
>>> sub.channel_recv(sub.channel_list_all()[0]) '14'
if there's a better way to do 7 + 7 i'd love to hear it
So that code now runs on another process/subprocess and can take care of another core?
in the sense of having an entirely isolated object space
the "almost" is what throws me off
same process, but you can't access objects in one from the other. like each interpreter would have its own True, False, None
Hey I just started learning python any tips ???
Why would I want that?
Well- there is one use-case I can think of with like asyncio
You can isolate different parts of a user's code
So that a user can't accidentally block the main important event loop logic doing something special
fixed
But even then that seems niche? What's so weird to me is how it feels like I am supposed to find it exciting but I just truly don't get the buzz y'know
how is this different from multiprocessing then?
maybe it would be useful for running tests
lower startup overhead
there's more issues.... not every module (even in the stdlib) is compatible
C modules have to use "multi-phase initialization" to work afaik
multiprocessing has to copy IPC, sub-interps can transfer ownership of objects, zero-copy
theoretically
and it gets hairy when objects are in containers and you're transferring them
does it transfer children or copy?
Sending strings back and worth go pew pew
the dream of sub-interps is truly parallel Python threads
which is orders of magnitude more efficient than multiprocessing
and not having to eliminate GIL from the Python interp
but still having to carefully separate contexts
either diligently locking access to shared resources or transferring ownership of objects in the heap
basically "Python: Hard Mode"
though tbh i haven't looked at the proposed interfaces yet so hopefully it will be made easy..
looks like they are keeping it pretty simple https://www.python.org/dev/peps/pep-0554/#the-interpreters-module
as they mention in the PEP the goal of sub-interps is parallelization of tasks you would otherwise have to throw expensive multiprocessing at
consider something like this benchmark https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/spectralnorm-python3-8.html
it is 304x slower than the fastest C version and much of the cost is wrapped up in multiprocessing overhead
for a task that is in parallel doing some arithmetic on numbers
this is where sub-interps should have a lot of value
without having to ship or write extensions for parallelization (see: numpy)
Question - What are some of the best talks regarding how import works in Python and the nits and grits of it? Was Beazley's talk in Pycon 2015 good, or are there any other good talks on it?
imagine a world where float is replaced with Decimal, just like how the default integer's size in Python grows arbitrarily at need.
would that be a net positive or negative?
positive
Hi, here from #esoteric-python . I'm curious to see how the ast module works and how I can use it for unusual stuff.
The reductionist explanation would likely be that it lexes and parses into an AST (abstract syntax tree) with whatever code you gave, and doesnāt go past that
Essentially it only goes through
Reader -> Lexer -> Parser (gives back an AST)
This might be wrong however
Thanks
Usually the entire process would be
Reader -> Lexer (Tokenizes) -> Parser (AST) -> Compiler (context-free grammar) -> Assembler (Bytecode) -> Interpreter
You can, and that's what it's exposed to let you do.
The AST is a big tree of objects you can mess with, and then pass it back to compile() to get a code object.
Yep
For that, you could also use import hooks. Iāve seen someone change lambda to a function keyword and just imported it
But yeah, as long as it wonāt raise a syntax error as soon as it runs
Damn syntaxerrors
Do you have a minimal example?
If youāre getting syntax errors, you should work with import hooks
Yes one second
Though itās in a package so
Itās a bit abstracted
okay
!pypi ideas
āā
Usage
Suppose that you want to use function as a keyword in Python, to mean the same thing as lambda, enabling you to write
my_program.py
square = function x: x**2
print(f"{square(4)} is the square of 4.")
You can do this using an import hook. The simplest (but flawed) way to create such an import hook with ideas would be as follows:
from ideas import import_hook
def transform(source, **kwargs):
return source.replace("function", "lambda")
import_hook.create_hook(transform_source=transform)
Then, you'd need to use it. Since there is already an example import hook that does this, we'll use it instead. All you have to do is instruct Python to add your import hook and it will be used from that point on. Something like the following.
Lets's call this 'loader.py'
from ideas.examples import function_keyword
function_keyword.add_hook()
import my_program
and then run
python loader.py
So, my_program.py , and any other module that could be loaded by it would recognize that function is a valid alternative to lambda.
Many more examples can be found in the documentation, including a better way to create such an import hook and information about a console (REPL) that supports code transformations.
āā
This uses an import hook, however itās within the explanation of a package so itās abstracted
Anyone API Developer Is Here?
Try asking in #user-interfaces
It's off topic for this channel, but it's what that channel is for
I am not sure, I think it might be for the better?
Negative. Floats are fasttttt
It's a very acceptable tradeoff when trying to number crunch
I would like fractions by default for rationals and floats as a manual "gotta go fast" switch like lisp/raku
Fractions are probably a lot safer than decimals
I'm imagining 1/3 kind of scenarios with decimals
That would be...not fun
yeah, decimal is quite specialised for currency etc
but numpy?
okay, maybe not decimals, but just a non-native real number type
my PoV is more likeā¦with something as high level as Py you want to hide the fact that ints and floats are imperfect concretions of mathematical abstractions, right
and we already do that with int; why not with the real number equivalent?
ye, I agree with the sentiment. It is one of the things that makes lisps feel really nice to use
can you elaborate on this please?
or this?
like functionally what is it about decimals that you think is less ergonomic than fractions
basically if you want precise calculations instead of current float implementation, then any division that would have led to 1/3 type of floats now instead lead to a ballooned up decimal that is going to impact every calculation it's contributing to from that point on
side note, i can see your POV and it makes sense to me. i suppose "expecting" something like numpy to deal with when you want calculations to be fast feels a bit like chicken and egg. numpy couldnt have existed without python first right, and then that means you essentially had to ship a language that was incapable of representing floats at all. in this, something about it all feels...wrong, im not quite sure what. Like, ints are still a great abstraction over machine ints, because as long as they align, they align just fine.
but maybe it's fine, im not sure yet
Like, ints are still a great abstraction over machine ints, because as long as they align, they align just fine.
true
I didnāt account for that
Iām writing my parser now and I was just thinking
do I really need floats�
decimals also do incorrect rational math, whereas fractions dont
fair
hm.
Just have any division result in fractals, and hard-coded 0.3 uses decimals
now, one thing I do want is the default repr of a rational to be a decimal number rather than a fraction
so what happens if you take like square roots
Hey guys can I use whatsapp and ADB modules at the same time to automatically send a message on android on whatsapp
- probably 2) what's ADB? 3) how is that related to python 4) how is that on-topic for this channel?
you can implement >> pipe syntax with that, yes https://github.com/robinhilliard/pipes
nevermind CUDA and other float-only infrastructure that python can also take advantage of indirectly
this is just op overloading
the python workflow strongly tends to be "prototype quickly to get a working proof-of-concept, then optimize and restrict" - removing float would make it much harder to convert from python code to numpy etc
I made something like that before
i made something "like that" before, too, but the ast transform makes it so much more powerful
std,cout=type('',(),{'__getitem__':lambda self,name:type('',(),{'__lshift__':lambda self,data:(__import__('sys').stdout.write(str(data)),self)[1],'__repr__':lambda self:''})()})(),0
#include <iostream>
a=2
b=502
std[::cout] << a << b;
will read
@pipes
def pretty_pipe():
print (
range(-5, 0)
<< map(lambda x: x + 1)
<< map(abs)
<< map(str)
>> tuple
) # prints ('4', '3', '2', '1', '0')
that's something you can't do with that approach
however this only works for stuff which is syntactically valid
yeah, it needs to survive the parsing step
you can't change the syntax via the AST, but you can change any behaviour after
afaik pytest also uses AST transform
you get a float IMO. While going full sympy would be nice, it is a large burden and not super useful except for specialised usecases
floats for irrational numbers, fractions for rational numbers
floats for speed š
hm.
interop might be complicated?
floats should be opt in if you need perf, not the default
is it simple to statically determine if an operation will produce a rational or irrational number?
I'm thinking of like 4 ** 0.5-equivalent?
that's an interesting notion
depending on the language, but for Py I would agree
ye, I feel like we are too quick to tolerate defaulting to incorrect math for rationals
you can kind of do it, but that is indeed an issue
at that point i'm thinking bytes vs. str
so if I want to chance the syntax that's more complex
say I want to add print_keyword
(hypothetically)
what if fractions are used for transmission and floats internally, just like bytes and str
transmission = ?
like serialisation?
why not strings
too much overhead?
okay, I guess it depends on whether what you have tends to be rational?
str doesnt use bytes internally to my knowledge (or well, no more than literally any other type does)
but also strings have the advantage of being primitive
Yeah, but not if you receive user input
in the sense that str = bytes + encoding?
i'm not saying str uses bytes, i'm saying systems/users use strs "internally" and bytes "externally"
oh, you mean like you serialise str into bytes. But how is that related to float vs fraction
yes
well, if you think about it, fractions are better suited for serialisation because, ideally, it's precise, while floats are system-dependent
but that doesnt solve the important issue, which is that floats have incorrect results
regardless of system
no, but then we would default to fractions for serialisation and the user could decide whether or not to use floats for speed or not
(and, in case of numpy, which kind of floats, without losing any information)
my guess: no
honestly, floats are portable in practice thanks to IEEE, so I dont think it matters all that much
Yes, because you need to support it at the runtime.
i'm not sure. defaults matter - and considering that float-speeds are only relevant when you actually specify the details like in numpy, cython etc. we could actually do away with floats in python entirely and delegate to more specific implementations where it matters
You do want to keep floats as they are a better representation of reals than fractions or decimals
complex?
using complex for reals would be odd IMO
yes
but ye, IG you could replace all floats with complex without much effort
People overestimate the impact floating point precision, it's just a non issue in the vast majority of use cases.
even in areas that people think precision is important, like finance or engineering.
i'll have to meditate over fractions for serialisation, it's an interesting idea i think
and then people underestimate the impact of floating point imprecision and wonder how you could siphon off money without anyone noticing or how rockets crash or..
It's such a myth that finance people need to use fixed precision reals.
no, it's not
They'll either use ints or round floats around.
afaik in finance it is so that you get correct rounding as you would get should a human do the math, not to be more precise
the more transactions, the higher the impact of slight imprecisions - and then look at high frequency trading
and engineers don't need more than a doubles worth of precision due to just how much the 14 or so digits actually are IRL
rounding is a huge topic on itself
you can do just about all math with floats, but you have to be mindful that the computer isn't doing rational math, but taking a pretty good guess at rational math
which I don't like, since I just want to do correct rational math and use == between 2 rationals without having to think about the domains of my valaues
Earlier this week, we received this question from a fan on Facebook who wondered how many decimals of the mathematical constant pi (Ļ) NASA-JPL scientists and engineers use when making calculations:Does JPL only use 3.14 for its pi calculations? Or do you use more decimals like say: 3.1415926535897932384626433832795028841971693993751058209749445...
40 decimal places
I work with a multinational bank and we use BigDecimal (Kotlin) everywhere
40 decimal places is afaik overkill, for rocketry you need 7 afaik.
40 is for radius of the universe to the precision of a hydrogen atom
yeah, that's upper bound (maybe relevant for simulations?)
it's more a matter of float errors being a low level detail that only makes programming harder
(unless you need performance)
and you do have to write your code in a way that accounts for these errors
even if the math itself is good enough for just about anything
which sucks majorly, yup
hypothesis taught me that lesson
but yeah
An hft bot will churn a bajillion single-precision floating point operations in a tpu just to round it to the nearest tick at the end of the process. The accounting side will keep track of inventory integers, which i guess is a form of fixed precision real if you're being pedantic.
Everything has low level details, it's about what's worth to abstract and what's not.
and I argue that it is worthwhile to use fractions instead of floats by default
as it leads to more predictable code that lies closer to how an abstract logical machine would handle the math
and the performance benefits of floats are not meaningful in enough cases to be worth making it the default
CPU architecture has been optimized to handle floating point operations for the last what? 40 years? I'd say using fractions as default is bad enough even if just for the amount of greenhouse gases their wide use over floats would entail.
I'm being hyperbolic but you get where i'm going
CPUs aren't very optimized for floats, that's what GPUs are for
isn't the point correctness over speed
no the point is the compromise between correctness and speed
of course if correcness is you one and only criterion then fractions is the logical choice
my point is that it's a bad criterion
in an algorithm where math forms a meaningful portion of CPU time, I would agree. But most code (at least the kind I write) is IO bound or CPU bound on string manipulation or searches. Not on non-whole number math.
correctness and ease of use is the thing I care about
whether adding 2 numbers is 10x slower doesnt matter when I add 2 numbers per web request
and when you need to manipulate lots of floats, you're probably also using numpy, which requires you to specify the exact variant of floats you want
Python is not commonly used for performance-critical number crunching, and in places where it is, it's done via an extension like numpy over python itself. I think the point int already abstracts away the range limitations, float should also abstract away imprecision is very fair
obviously it can't be the only criterion, since no machine is fully abstract
therefore implying that, in general, correctness >>>> speed
but maybe not call it float
I was thinking Real (since I wanna use capitalised words for type identifiers)
Well yeah, given that such a change does occur, it shouldn't be called float anymore
unified number type or what
no, Num is specifically floats
real/fraction/rational/etc would be more than fine
oh, so Num is float
A unified number type would also be convenient
ye
did someone say...JavaScript? š„“
That's one thing I don't hate about it tbh, and I feel like it would suit the whole number is number rhetoric
you cant always do that since you do indeed sometimes want floats for perf, even in python
i hate to be "that" guy but practicality beats purity.
NO functional programming is monarch š”
but raku for example lets you use any numeric type as a number in any place due to implicit conversions
hehe. the problem is, ultimately if you truly want a system that's capable of handling all scenarios nicely, you have to compromise with the physical limitations of the hardware we have
I don't propose getting rid of floats, but I could definitely get behind making Fraction either the default, or at least easily accessible
guess that's what I'll be doing then
would love to hear what kind of performance implications that would have. it seems like the most decent compromise if you wish to avoid floats
I still see no good argument for fractions as defaults, you keep saying "correctness and ease of use" but fractions offer nothing more than floats already offer in that regard, except in the most specialized fields. While floats offer significantly better performance. So far it sounds like it's a purely aesthetic desire.
why would fractions not offer more than what floats already offer? floats are imprecise
you can use equality on fractions and get correct results
you can also hash fractions and get correct results, therefore you can put them in a set or as dict keys
0.1 + 0.2 would genuinely be equal to 0.3 if it was all backed by fractions
0.1 + 0.2 is equal to 0.3, most implementers just compromise for ease of implementation and performance and accept the wrong result
Can I use ADB and whatsapp toolkit together for android
I don't think it is a sensible tradeoff
youre on a python server and this is not a help channel, perhaps ask in offtopic. although im not sure what whatsapp toolkit is, is that something against TOS?
does any other language do Pythonās arbitrary width int thing
probably, lets find out
No wait it is used for automatically send a message on whatsapp using python
Some programming languages such as Lisp, Python, Perl, Haskell and Ruby use, or have an option to use, arbitrary-precision numbers for all integer arithmetic. - wikipedia. im not sure which ones out of those have it as default
most lisps, ruby, perl, raku, haskell default to it
JS, java, kotlin, C# have it as an opt in
js has ints? i thought it was still doing that random "everything is a float" thing
huh. more than I thought
BigInt
or BigInteger, canāt remember which
i see, never heard of it before.
lisps and raku also use fractions by default
itās relatively new
@elder blade :white_check_mark: Your eval job has completed with return code 0.
False
Damn, how are you adding all this new stuff? Iām guessing you know how to edit C code
was that an ast manipulation? š
but 0.1 + 0.2 == 0.3 => False really should be a good reason why we should default to fractions
it's so in your face that any school child will say "wtf, python is cool, but this is all wrong!"
and it's really hard to explain to a beginner
i wonder how those are implemented in calculators
I don't know if it's particularly difficult, but it's definitely a learning moment where the student realizes computers are not magic
And then they realize how pretty much everything in the world is built on hacks that are built on kudges that are built on design decisions that made sense 45 years ago but probably don't make much sense today
well, imagine a teacher in 4th grade maybe who wants to teach their kids about decimals and thinks it might be a good opportunity for a first contact with python to experiment
the kids just learned about fractions and now nothing works as expected
that's probably not a good learning experience
that's on the teacher then š
python 4: / on ints returns a Fraction
I just saw code like this for the first time:
with NpyScpWriter(
output_dir / "norm",
output_dir / "norm/feats.scp",
) as norm_writer, NpyScpWriter(
output_dir / "denorm", output_dir / "denorm/feats.scp"
) as denorm_writer, open(
output_dir / "speech_shape/speech_shape", "w"
) as shape_writer, open(
output_dir / "durations/durations", "w"
) as duration_writer, open(
output_dir / "focus_rates/focus_rates", "w"
) as focus_rate_writer:
I guess there's an audience for the 3.10 changes to with statements after all.
nah, it's a matter of PR, really. if we advertise with ease of use and learning, a claim like "even a child could learn and use it" should hold
i kinda love it
if only i could use 3.10 at work, i'd get rid of so many ExitStacks in my tests
meh fair enough. but honestly python is kind of an "everything at once" language - imo the best language for newbies would be something like python syntax that compiles to racket
plus, the reality is that computers use floats and floats are imprecise
you missed the whole debate about fractions as default? š
no, i use lisp so you know i would love it
but i'm saying it's not really an indictment against python as a beginner-friendly language
>>> from fishhook import *
>>> @hook(int)
... def __truediv__(self, other):
... return Fraction(self, other)
...
>>> 1/2
Fraction(1, 2)
>>> print(1/3)
1/3
>>> ```
but yeah sure it'd be a nice feature for newbies, until they try to do real-number stuff with rationals and can't
fishhooks and forbidden fruits.. not sure whether to love or hate those š
we're back on with the fractions i see
there's got to be a hundred packages out there that do fractions
oh, i missed that it's being added with a hook, that's actually pretty cool
what would be a reason to override the built-in getattr -- I noticed its looked up many times in the interpreter
wonder if it's used to look up simple names in source or only when getattr(...) is literally being used
hi i need a little help im new to python and im experimenting around for fun, this is my code so far:
import itertools
import string
import datetime
characters = string.ascii_lowercase + string.digits
for i in range(len(characters)):
for j in itertools.permutations(characters, i):
x = datetime.datetime.now()
date = "[%s-%s-%s/%s:%s:%s]" %(x.year, x.month, x.day, x.hour, x.minute, x.second)
print(date,"[COMBINATION] :","".join(j))
and i've been noticing that this code actually doesnt do a full combination betwen all the characters, so it does go through all them like abd ab3 a3s 9as etc.... but, it never uses the same character in the same combination. like aba, or acc, it skeps to the next character instead so it goes abb, but that doesnt work either so it does abc, so it skipped from aba to abc. how do i fix this? itertools.combination doesnt work either since .combination skips all the numbers after the 2 digit combinations.
That's a good call. I asked in the UI channel but I'll ask there
It'd be a 4x style strategy, mostly a 2d interface overlaid on a 2d hex grid
But I want to implement a 3d geodesic for a certain dimension of it
https://docs.python.org/3/library/itertools.html explicitely states "r-length tuples, all possible orderings, no repeated elements"
you might want to use combinations_with_replacement
has there been any discussion about built-in syntax for named tuples?
i really liked how julia does it:
julia> tup = (a = "aa", b = 2, c = 3.4)
(a = "aa", b = 2, c = 3.4)
julia> tup[2]
2
julia> tup.a
"aa"
I... i wouldn't hate ('a': 'aa', 'b': 2, 'c': 3.4) I guess
hm
named tuple still allows indexing
also immutable and hashable
i also feel like named tuples represent heterogenous records more naturally
Code:
import itertools
import string
import datetime
characters = string.ascii_lowercase + string.digits
for i in range(len(characters)):
for j in itertools.product(characters, repeat=i):
x = datetime.datetime.now()
date = "[%s-%s-%s/%s:%s:%s]" %(x.year, x.month, x.day, x.hour, x.minute, x.second)
print(date,"[COMBINATION] :","".join(j))
Atm its printing all different possible combinations between the 36 letters and numbers ive provided, but i want to compare the combinations until it matches a variable, for example if i have a variable x = "kasd8" and i want to check each of every combinations made until it matches, how can i do this?
Im going offline now so either tag me or dm me with the info i need so i can read it later on, ty.
is there a tidy way to unload extension modules? looking at https://bugs.python.org/issue34309 i'm thinking of a __del__ hook for the ProxyModule so that you can do del(mod) and safely unload a use()d module in order to re-use it
since we talked about it the other day, maybe subinterpreters could work?
i'm imagining loading the package into a subinterpreter and simply kill the whole thing on del
or maybe run the whole package in its own actual python interpreter as subprocess and then communicate via stdin/out
having trouble imagining either approach being worth the cost
afaict to send anything more complex than a handful of primitive types to/from a subinterp you have to pickle to a string
subprocess same story, except have to pickle (or otherwise encode) everything to byte stream
i should say the thing i'm having trouble imagining is the use case
Wait that's true, subinterpreters could in some way be used for your "same modules different versions" thingy
That seems extremely hard though
that's interesting, not familiar enough with import mechanics to know if there's an easier way than subinterps
naive thought is import module in subinterp, pickle all the globals and send them back on a channel š
but i would be very surprised if that worked against any extension (if at all)
!e
import _xxsubinterpreters as interpreters
import pickle
code = """
import _xxsubinterpreters as interpreters
import math
import pickle
interpreters.channel_send(channel, pickle.dumps(math.__dict__))
"""
interp = interpreters.create()
channel = interpreters.channel_create()
interpreters.run_string(interp, code, {"channel": channel})
math = pickle.loads(interpreters.channel_recv(channel))
print(math)
print(math["sqrt"](2))
@main lynx :white_check_mark: Your eval job has completed with return code 0.
001 | {'__name__': 'math', '__doc__': 'This module provides access to the mathematical functions\ndefined by the C standard.', '__package__': '', '__loader__': <_frozen_importlib_external.ExtensionFileLoader object at 0x7f8d9fe11e20>, '__spec__': ModuleSpec(name='math', loader=<_frozen_importlib_external.ExtensionFileLoader object at 0x7f8d9fe11e20>, origin='/usr/local/lib/python3.9/lib-dynload/math.cpython-39-x86_64-linux-gnu.so'), 'acos': <built-in function acos>, 'acosh': <built-in function acosh>, 'asin': <built-in function asin>, 'asinh': <built-in function asinh>, 'atan': <built-in function atan>, 'atan2': <built-in function atan2>, 'atanh': <built-in function atanh>, 'ceil': <built-in function ceil>, 'copysign': <built-in function copysign>, 'cos': <built-in function cos>, 'cosh': <built-in function cosh>, 'degrees': <built-in function degrees>, 'dist': <built-in function dist>, 'erf': <built-in function erf>, 'erfc': <built-in function erfc>, 'exp': <built-in function exp>, 'ex
... (truncated - too long)
Full output: https://paste.pythondiscord.com/uxefoqodev.txt?noredirect
you can't marshal or pickle a module, but you can its __dict__
can't pickle numpy tho
<string>:3: UserWarning: NumPy was imported from a Python sub-interpreter but NumPy does not properly support sub-interpreters. This will likely work for most users but might cause hard to track down issues or subtle bugs. A common user of the rare sub-interpreter feature is wsgi which also allows single-interpreter mode.
Improvements in the case of bugs are welcome, but is not on the NumPy roadmap, and full support may require significant effort to achieve.
Traceback (most recent call last):
File "/home/matt/src/spectral-norm/subinterp_import.py", line 13, in <module>
interpreters.run_string(interp, code, {"channel": channel})
_xxsubinterpreters.RunFailedError: <class 'AttributeError'>: Can't pickle local object 'setquit.<locals>.Quitter'
even for modules that do support sub-interpreter, exporting all of the functions seems complicated
potentially you could break down the AST and ship that back through the channel, and now we're getting into some evil magick
keeping the module imported in the sub-interpreter and abstracting the interface through channels would be prohibitively expensive
hmm.. that depends
taking my idea from the other day and truely use a subprocess with an optimized python implementation for this purpose..
we could use a totally optimized interpreter for the numeric processing and ship the input/output back and forth to the generic cpython
basically use cpython as master
might come with some nice performance benefits and allow real and clean unloading/reloading
i don't understand, competing with numpy running everything through IPC?
not competing, delegating
numpy already has multiple backends for parallelizing
out of the box it will use all your cores
yeah, but for instance i've had a case where numba trumped numpy performance-wise, JIT-ing a nice and clean for-loop vs. numpy vectorization
so, maybe there's a python interpreter that could do that out of the box but isn't as nice for general purpose usage like cpython?
mypyc would seem to qualify
the purpose of this is being able to load and unload specific versions of modules?
extension packages, yes
not single modules
single modules i got pretty much covered, but isolating whole package is much more difficult
if sub-interps/subprocs really are the hammer for this nail, figuring out how to serialize the entire import seems more ideal than serializing every call-in
is really like working with any data stream, "can i turn this into a bytestream, and decode it on the other end?"
yes that dill š
š
on the module unloading issue, why is the id() the same even after deleting and recreating the _sqlite3 module? for example _sqlite3.Error
even manually calling dlclose in between and verifying the .so gets unloaded, the static types are still at the same address when importing a second time
Could be an optimization, python often places things in recently freed memory
I don't think it's that unreasonable to assume that it just happened to repopulate the same blocks again
is there a way to check that they're actually freshly initialized blocks?
Doesn't the GC allow you to see who has a reference to an object?
Well, I can suggest a way to check if it actually reloads the module
Extremely unorthodox, but still
Use ctypes to write garbage and see if something breaks
what could go wrong
Exaaaaaactly
i love it when a plan comes together snicker
how to find referrers without holding a reference? š
loop through gc.get_objects?
then call get_referrers on each and call id on each obj and see if it equals the held address
Or that, yeah
can i ask why tho?
They're reloading the sqlite3 module, but the memory addresses seem to persist
So they're wondering if it does get re-initialized in the same space or if it doesn't get reinitialized
At least that's my understanding
wanting to see if it's possible to cleanly unload an extension module
unlikely
i wonder if sqlite3 is a good test for that after all
it may be built in too deeply
Unload built-ins 
nah python is bad at fully freeing any thing
cause all classes keep a reference to their subclasses
that's right
!e py for i in range(10): class a():pass del a print([cls for cls in object.__subclasses__() if cls.__name__ == 'a'])
@pliant tusk :white_check_mark: Your eval job has completed with return code 0.
[<class '__main__.a'>, <class '__main__.a'>, <class '__main__.a'>, <class '__main__.a'>, <class '__main__.a'>, <class '__main__.a'>, <class '__main__.a'>, <class '__main__.a'>, <class '__main__.a'>, <class '__main__.a'>]
That's not very free
after doing import _sqlite3 there are already 15 things keeping references to it?
i can think of 2...
I was... th- that was a suggestion to see what is keeping the module alive for debugging purposes.
yea python has a lot of cross caching for speed
and a lot of it is low cost enough that its left till program close
Tl;Dr Python says Fuck you and your use-case haha
theoretically one could loop through get_objects() setting attributes to None until there are no more references?
I do want to see this attempted
well, unloading/reloading a builtin never was the actual use case
there's not really a point to that imo
extensions like numpy on the other hand..
_del = delattr
_imp = __import__
import builtins
for i in dir(builtins):
_del(builtins, i)
sys = _imp('sys')
del _del, _imp
sys.meta_path.clear()
sys.modules.clear()
# everything here is broke```
Will multi-phase-setup mess with that?
>>> len(gc.get_referrers(_sqlite3))
15
>>> del sys.modules["_sqlite3"]
>>> len(gc.get_referrers(_sqlite3))
14
``` 1 down
hrhr
its the stuff in its module
and some unknown list
so this clearly indicates nothing was reloaded, correct
Allegedly, yes
So does it have circular references?
i knew functions have __globals__, but where are they storing the module itself?
What if you force the GC to run?
still 14 š„
Yeah, it remains alive after getting removed from sys.modules and its name deleted
What does gc.get_referrers() return?
It gives me a list of 15 referrers, actually
Most of which are from sqlite3?
Interesting, considering _sqlite3 was imported
But what is that? The objects or IDs of them?
Objects
[[None, <module 'sys' (built-in)>, <module 'builtins' (built-in)>, None, None, None, None, <module 'io' (built-in)>, None, None, <module 'winreg' (built-in)>, None, None, None, None, None, None, None, None, None, None, <module '_datetime' (built-in)>, None, <module '_sqlite3' from 'C:\\Users\\FarmArt.DESKTOP\\AppData\\Local\\Programs\\Python\\Python310\\DLLs\\_sqlite3.pyd'>], <built-in function adapt>, <built-in function complete_statement>, <built-in function enable_callback_tracebacks>, <built-in function enable_shared_cache>, <built-in function register_adapter>, <built-in function register_converter>, <class 'sqlite3.Row'>, <class 'sqlite3.Cursor'>, <class 'sqlite3.Connection'>, <class 'sqlite3.Node'>, <class 'sqlite3.Cache'>, <class 'sqlite3.Statement'>, <class 'sqlite3.PrepareProtocol'>, <built-in function connect>]```
[
[None, <module 'sys' (built-in)>, <module 'builtins' (built-in)>, None, None, None, None, <module '_io' (built-in)>, None, None, None, None, None, None, None, <module 'readline' from '/data/media/0/src/python3/build/build/lib.linux-aarch64-3.11/readline.cpython-311-aarch64-linux-gnu.so'>, None, None, None, None, None, None, None, None, None, None, None, <module '_sqlite3' from '/data/media/0/src/python3/build/build/lib.linux-aarch64-3.11/_sqlite3.cpython-311-aarch64-linux-gnu.so'>]
<built-in function complete_statement>,
<built-in function connect>,
<built-in function enable_callback_tracebacks>,
<built-in function enable_shared_cache>,
<built-in function register_adapter>,
<built-in function register_converter>,
<class 'sqlite3.Row'>,
<class 'sqlite3.Cursor'>,
<class 'sqlite3.Connection'>,
<class 'sqlite3.Statement'>,
<class 'sqlite3.PrepareProtocol'>,
<built-in function adapt>,
# the __main__ dict im using
]
yeah
The first referrer seems to be a list
That also contains sys, builtins and a bunch of Nones
Why does None hold a reference to _sqlite3 š
No, no
None just happens to be in the same list
That holds a reference to _sqlite3
Let me remove it from that list
13!
š
It's all functions
And a few classes from sqlite3
Time to run dir a bunch of times
wonders what is gonna randomly crash now ..
Runs nicely so far
so far š
where is the module stored in a function, though, I'm genuinely baffled
It's not stored as an attribute on classes either
certainly soundes like a circular reference
perhaps the functions are used as closures, and that's where the dangling reference comes from?
But does sqlite3 exist in sys.modules?
Well, all I can say is, adapt only has _sqlite3 as a referent
but when you such functions, its not passed as an argument ?
just needs more ctypes
Running get_referrers on the function seems to give _sqlite3 as one of the results
Actually
How about we give one name to it
Overwrite the refcount to 1
And remove that name?
That should trigger destruction
overwrite the refcount? o.O
It's stored as an int on the object, so yeah
?!
I did that at some point, just gotta remember the offset
Or look it up in the sourcecode, but that's not fun
We're in for a ride here ladies, gentlemen and non-binary..
its before the object i think
refcount is offset 0
so c_ssize_t.from_address(id(obj)).value = 1 sets refcount to 1
that most certainly is resulting in a demon entering this plane of existence..
i thought ob_type was at offset 0?
ob_type is the second 8 bytes
ohh
what have i done >.>
that is the #, wow
but get_objects still returns 13 things?
Oh
*get_referrers
Wsg bru
Try accessing an attribute
it's traversing?
Fun things are happening
Abc xyz?
@worldly venture wtf your tag š
I'll try again in a new session
bros badges are fire asf
I am strapped to my seat with duct-tape
I just tried to access _sqlite3 after manipulating it into getting finalized
fail, that was my __main__ dict
At first I got the repr for a method-wrapper of io.Bytes.write, and a couple seconds later a segfault
disable gc
The refcounting is a different mechanism IIRC
gc is specifically for circular references, refcount finalization bypasses it
i think it's from allocation calling some GC "get memory" function, at least, thats what it looked like
mine crashed on tab completion
well that was fun, i guess i got my answer
We've brutally assaulted the interpreter
Not as brutally as #esoteric-python does sometimes, but still
sad but satisfied š„² š„²
true :p
so why <built-in function> has a __self__ ??
it does
So they were circular references
well if we're trying to unload, I don't see how it would hurt to clear that __self__ attribute
if I can figure out how
so gc.get_objects / gc.get_referrers doesn't use refcounts too
seems like
The gc and the refcounts are two separate mechanisms, yeah
Oh
There's a recursive tuple there for some reason
>>> r[-6]
<built-in method register_converter of tuple object at 0x000001B270219D10>
>>> r[-6].__self__
(<NULL>, 'r', True, None)
>>> r[-6].__self__[0]
((...), 'r', True, None)```
Internals are kinda scary
Yikes
even dill can't pickle a PyCapsule object, apparently
hello, Ive a question but not too sure where to ask, so: how can I display what is printed in the command line inside a gui (kivy in my case ) ?
sys.stdout.read?
not sure what that is buy I'll research it
you can replace sys.stdout to redirect output
or that ^
If you don't specify a file argument for print, it reaches for the object currently stored under sys.stdout and calls its write method. You can replace it with your own that will write to a label in your GUI either bypassing stdout or duplicating the output there
Or, if it's a one-off operation, you can specify the instance as the file argument
I understand, I am not familiar with sys, but with your explanation, it will definitely be a step forward to make it work
!e ```py
from io import StringIO
import sys
class CachedIO(StringIO):
out = sys.stdout
def write(self, arg):
self.out.write(arg)
super().write(arg)
text = StringIO()
print("I'm printed to console!")
print("And I'm saved internally", file=text)
print(text.getvalue()[2]) # Accessing the stored values
text = CachedIO()
sys.stdout = text
print("I'm stored AND redirected implicitly")
print(text.getvalue())
@spice pecan :white_check_mark: Your eval job has completed with return code 0.
001 | I'm printed to console!
002 | d
003 | I'm stored AND redirected implicitly
004 | I'm stored AND redirected implicitly
You can make your own, which would write the text to your GUI
Of course, that's based on the assumption that the output comes from your program. If you're calling a subprocess, you'd need to pipe things differently
OMG thanks so much, I'll tweak the code and add it
hu, that looks useful
I found out about the io module recently
Well, not that recently at this point, about a year ago actually. But it was a gamechanger
it's like tee(1)
I really appreciate the unified interface print brings, as well as the fact that it's not tied specifically to stdout and instead actually looks up sys.stdout
even more awesome would be if it were also a contextmanager
with CachedIO() as text:
print("hello")
output = text.getvalue()
``` or something?
!e ```py
from io import StringIO
from contextlib import contextmanager
@contextmanager
def redirect_output(to):
import sys
saved, sys.stdout = sys.stdout, to
yield to
sys.stdout = saved
with redirect_output(to=StringIO()) as cache:
print('Yo')
print(cache.getvalue() * 10)
@spice pecan :white_check_mark: Your eval job has completed with return code 0.
001 | Yo
002 | Yo
003 | Yo
004 | Yo
005 | Yo
006 | Yo
007 | Yo
008 | Yo
009 | Yo
010 | Yo
With some slight tweaks you can specify whether you want to redirect stdin, stdout or stderr as an optional argument
Would that be useful?
that's cool, I guess one could combine it with CachedIO() ?
Yep, that's the intended beauty of it
The actual redirection target is whatever you choose it to be
i do like these io interfaces, they're nicely designed
I guess a subprocess could be told to use sys.stdout and then it too would be captured
@contextlib.contextmanager
def redirect(to, stream='stdout'):
import sys
assert stream in ('stdout', 'stdin', 'stderr')
opened = False
if isinstance(to, str):
opened = True
to = open(to, mode='r+')
stored = getattr(sys, stream)
setattr(sys, stream, to)
try:
yield to
finally:
setattr(sys, stream, stored)
if opened:
to.close()
Unified version š
ahh perfect š„³
what does the yield to do, returm the stream you passed in? so you can do getvalue etc
Yeah, so you can do redirect(StringIO()) as cache and get the newly created object
(I'm just going to ignore the fact that contextlib.redirect_stdout and contextlib.redirect_stdin exist, shush)
yield on its own is part of the contextmanager decorator's protocol, yielding a value is the equivalent of returning it from __enter__, and the rest of the function body is the equivalent of __exit__
Agh, I'm kind of confused regarding import hooks, I tested out a Stack Overflow answer on my end but I'm not getting any results
but nothing happens whenever I import the file name the import hook was created in
But.. isn't there a context manager for this already?
Shhhhhhhhhhh
That's going to be our little secret
Are import hooks used when the file where the import hook exists is imported, or vice versa?
Cause I'm trying to use import hooks to modify the source code of a file when the package with the import hook in it is imported
after that
if you havent imported the thing you're wantimg to transform yet, its not in sys.modules or anythimg, it should be elligible for your hook
what mechanism are you using to set up the hook
MetaPathFinder/Loader
hu, that looks like it should work
does data get printed?
Nope
Only when I import the file I want to import in the file where the import hook is itself
so only when it's already in sys.modules
Yep
what if you reimport some other module
or do importlib.reload
oh, i missed that you're not trying to do this for every module
python tries to use the same loader that imported the top level, is why
- pretty sure
Ah, so it would only work for how Iām doing it when I import the file that is supposed to be importing the import hook in the file with the import hook itself
well, i think it doesn't matter which file is doing it, as much as if it's a subpackage where the parent package has been loaded
by file i mean if we're talking about pkg.a and pkg is in sys.modules, it doesn't matter if theres an import pkg.a in pkg/__init__.py or foo.py
you could check by overwriting __spec__.loader maybe with your loader
also look at sys.path_importer_cache
Hm
Is there just a way for a loader that acts like a normal one but will get any files where the file with the import hook is being imported and changes a certain thing
Figured the folks in here might enjoy this. A really good read on the GIL and some of the effects of the GIL in CPython: https://tenthousandmeters.com/blog/python-behind-the-scenes-13-the-gil-and-its-effects-on-python-multithreading/
As you probably know, the GIL stands for the Global Interpreter Lock, and its job is to make the CPython interpreter thread-safe. The GIL allows...
so say your loader is defined in myloader.py
what is an example of an imported file you want your loader to get
hey, sorry for the question, but how does one use an import hook?
I'm essentially aiming to rewrite ideas
There's a bunch of lists in sys that you add your instance to, and then the import system will check each in order.
Specifically path_hooks and meta_path depending on what you want.
thanks?
and it's run when something is imported, I guess? I've been looking through the import machinery
When you try to import, Python goes through the meta path, and calls the methods to see if each finder can handle it.
Meta finders allow you to define handling for modules directly by their name, ignoring filenames etc entirely. The path hooks are a subcategory of that, there's a path based meta hook which goes through sys.path and then calls hooks on each matching file it finds. That way it's easier to say implement a different file format without having to redo that logic.
Either way look in importlib.abc for the classes you need to implement.
A lot is optional for additional features.
The goal is to be able to use very unpythonic syntax natively
I asked in #esoteric-python , but this relates to the internals of Python.
I've done research already, but quite a bit of it is somewhat over my head.
Basically there's two classes you need, a finder and a loader. The finder takes a either a full dotted module name or a file path, and returns a module spec containing metadata and a loader if it knows where the module is. Then, the loader takes care of reading the actual code from the file or wherever.
I was recommended the ideas module, which uses "import hooks" - which is presumably a function (or something akin to that) that's called on imports. How to implement this, I do not know.
From this, I'll somehow get the source, then tokenize it, or alternatively get the tokenized source.
What's a module spec?
This class, which basically holds info for a module that can be imported:
https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec
ModuleSpec(name='sys', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='built-in')
for sys
You'll need a finder and a loader, since you want to change execution.
Can this be done in one script? I doubt it
Yep, as you can see there's one of the loaders, which is used to load modules builtin to the interpreter itself.
There's another which loads .pyd files, and a third for .py files.
Okay. Does this need to return a module object?
The ABCs you inherit have a bunch of default behaviour, so you can hook in at different levels.
I mean, what does the loader return?
I'm not familiar with abstract base classes, I'm afraid.
Shall we take this to a help channel? I feel like it might be spam here.
Sure. They're pretty straightforward, they're basically incomplete classes, with methods missing you need to define, but others with default implementations.
Okay. I'll register a help channel.
If it makes you feel better, this stuff is surprisingly complicated and I have tried and failed several times to understand it by reading the documentation
this almost works
lmao
on my python it prints
find_spec <abc.MyFinder object at 0x7ff7a00e80> sysconfig (None, None) {}
get_filename <abc.MyLoader object at 0x7ff7a00fd0> sysconfig () {}
get_data <abc.MyLoader object at 0x7ff7a00fd0> ('<sysconfig>',) {}
Hello world from transformed sysconfigmodule
followed by an error
no clue how to get subpackages to work
has anyone else noticed copying these text blocks inserts a garbage character at the end
import sysconfigM-bM-^@M-
\xe2 \x80 \x8a for what it's worth
U+200A 'ā' 0xe2 0x80 0x8a HAIR SPACE
how annoying >_<
If someone imports myloader.py
weird, that worked on my python 3.9
you want to transform the file that actually defines the loader?
Ah no, I wanna transform the file that imports the import hook
I was trying to run a sh file on my windows and it shows this error. Is there any way i can make this work on windows ?
ModuleNotFoundError: No module named 'fcntl'
i'm afraid not, you might look into win32api instead (or find a cross-platform alternative for what you're using fcntl for)
Should I try and debug by printing sys.modules?
it's hard to debug >_<
the best I can suggest is putting a print at the top of find_spec, get_filename, get_data
to make sure all of those are called
alright, i'll try that
yeah it seems like it only runs when i import the file into the file that has the import hooks itself
normally i would have
test.py import import_hook.py
however it only runs the import hook when i
import test.py into import_hook.py
can you send me a DM
Yep
Alright I turned my DMs on again from this server
need help building a bot/scalper
Do you think they would add a keyword argument to str.find/str.rfind to find the nth occurrence?
!d str.find
str.find(sub[, start[, end]])```
Return the lowest index in the string where substring *sub* is found within the slice `s[start:end]`. Optional arguments *start* and *end* are interpreted as in slice notation. Return `-1` if *sub* is not found.
Note
The [`find()`](https://docs.python.org/3.10/library/stdtypes.html#str.find "str.find") method should be used only if you need to know the position of *sub*. To check if *sub* is a substring or not, use the [`in`](https://docs.python.org/3.10/reference/expressions.html#in) operator:
```py
>>> 'Py' in 'Python'
True
!rule 5
5. Do not provide or request help on projects that may break laws, breach terms of services, or are malicious or inappropriate.
so many rules for smh annoying
i have a conceptual issue with find and index, speaking of
as we know a str is a sequence. and a list is also a sequence. why is it you can find a sub-sequence of characters, but not a sub-sequence of anything else
I'm assuming that's because lists are generally treated as a collection of self-sufficient elements, while strings put a lot more emphasis on the characters being a coherent text
Indexing a sub-sequence in a list is a less common operation than indexing a substring
many (most?) other programming languages have distinct "character" and "string" types, the latter being some kind of sequence of the former
i kind of wish python did too, but it would break way too much backward compatibility to make that change now
unless they added some kind of type coercion behavior for comparisons, additions, etc
I think I like python's approach better tbh
The fact that all string operations are available on single characters as well as vice-versa is extremely convenient
(coerce "hello" 'list)
; (#\h #\e #\l #\l #\o)
(coerce "š¦āė°ģ„" 'list)
; (#\U1F987 #\RIGHTWARDS_ARROW #\HANGUL_SYLLABLE_BAG #\HANGUL_SYLLABLE_JWI)
and the real kicker
(coerce "eĢ" 'list)
; (#\e #\COMBINING_ACUTE_ACCENT)
(coerce "Ć©" 'list)
; (#\LATIN_SMALL_LETTER_E_WITH_ACUTE)
I would argue that the different unicode forms of identical text are an argument for using just strings
eh, it's the same problems but now hidden
you still need something like str.graphemes() for proper text comparison
or unicodedata.normalize
yeah
yeah, maybe attempting to pave over unicode is just a bad idea
but i agree that you'd need to have a lot of conversion rules, like + should work on characters and strings interchangeably
at the same time, how often do you actually need to operate on a single unicode scalar value and can't just use the languages number type
str() of a character should be a length-1 string, etc.
I really like how convenient python's strings are in most common use cases
Pythons string handling is really nice
am advanced
the former is like a big named enum interface to the latter š
After using Rust, I am grateful for how nice Python is to us
I guess I just like the idea of characters being 1-len strings more on an intuitive level
rust string handling is very correct, but it does have its inconveniences
:incoming_envelope: :ok_hand: applied mute to @livid whale until <t:1633032218:f> (9 minutes and 59 seconds) (reason: mentions rule: sent 84 mentions in 10s).
I think I like that python has random string access, even if it does mean 4 byte characters
Spam pings?
two missing features in the python stdlib string handling:
- unicode character classes in the regex engine
- ability to iterate over extended graphemes
Ghost ping? lol
š¤·āāļø
whaT's ghost pingh?
^
84 or more people got pinged it seems
84 mentions absolute alpha
memory is cheap š
š
and yes i greatly appreciate array-like string lookups
yeah I found it very tedious to work with them after python
what does rust do?
guys
utf8 strings with all the flaws that has
do strings have an explicit encoding or something?
including me lol
Anyways as I was saying. many (most?) other programming languages have distinct "character" and "string" types, the latter being some kind of sequence of the former
i kind of wish python did too, but it would break way too much backward compatibility to make that change now
unless they added some kind of type coercion behavior for comparisons, additions, etc
guys
š¤
guys
thoughts?
Yes?
anyway, please keep this channel on topic and free of noise, despite the ping spam
i think spam belongs in an off-topic channel
k
Yeah I agree
it is 100% correct and forces you to deal with all the complexities that come from using unicode and utf8
Oh yeah lol even bytes is a sequence of... bytes......
(except locale aware to lower)
yeah, I misspoke there
ohhh thanks, that's actually what I had thought at first
the issue is that most older languages such as java, C# and even C don't have a useful character type
true
since it was designed for 2 byte codepoints which was assumed to be large enough for all characters, but unicode needs more
Yeah it's pretty funny how that works. It's the same as for strings.. strings are a sequence of characters.. characters in Python being represented as strings
utf-32 strings, but encoding/decoding by default to/from utf-8, is probably the most "practical" option

yeah
What extra characters can you represent in utf-32 again?
haha, funny emoji
@elder bladeyou can represent all characters in all unicode encodings, but only in utf32 can you find the nth character by index rather than by scanning from start to end
utf encodings cover all of unicode, but multi byte chars require more handling
agreed
Because it aligns perfectly yeah?
@edgy tinsel@lunar heronif you are going to contribute nothing to the discussion, please don't just create extra noise
I don't think you meant to paste that
how am i making noise in discord (ok ill stop)
no i think they did
lol
!mute @edgy tinsel 1d I already told you not to bring random noise
:incoming_envelope: :ok_hand: applied mute to @edgy tinsel until <t:1633118423:f> (23 hours and 59 minutes).
smh
ok ill stop
sorry
yup, currently afaik a unicode codepoint always fits into 21 bits
but you want a power of two so that it maps into a native type
C++ has u32string for this afaik
Why would you ever want to eat the cost of 4 times as large string just to get it to align?
so that you can do
string[10]
``` to get the 10th character without scanning the whole string
to ensure O(1) access without some other fuckery like having to keep a 2nd parallel array that marks codepoint break points
Oh is utf-8 variable length?
I thought it always was X amount of bits?
Yes, it's also one of its strengths as its one byte characters intersect with ascii
well, they encode a sequence of unicode scalar values

Then it makes more sense to think of utf-8 as compression?
a unicode scalar value is a codepoint that maps to a character, rather than being a surrogate
overall, the point is that looping over characters in a string in java, JS and C# is generally the incorrect, even if convenient, operation
Ah
oh nvm, JS seems to do things correctly
thats a first
some languages go even further and strings are sequences of grapheme clusters
i think swift is the only one that does it by default though
lmao that emoji
It's so perfect
How are combining chars handled for indexing?
generally just separate character
in the end, accessing separate characters has multiple possible definitions
this is the crux of the issue with strings being sequences of unicode code points, and why we have things like unicodedata.normalize
and why some people think it should be either sequences of graphemes or sequences of raw bytes, with codepoints being the worst of both worlds
i don't know if i agree, but it's an interesting opinion
Yeah I'm mostly wondering about python when like in your example it regards Ć© as a single indexable char even when it's a combined accent and a plain e
the nuclear option is just not having iterable strings and forcing people to be explicit
In [1]: list("eĢ")
Out[1]: ['e', 'Ģ']
In [2]: list("Ć©")
Out[2]: ['Ć©']
oh right so it doesn't
In [16]: unicodedata.normalize("NFC", "Ć©")[0]
Out[16]: 'Ć©'
In [17]: unicodedata.normalize("NFD", "Ć©")[0]
Out[17]: 'e'
s1 = ''.join(map(chr, [101, 769]))
s2 = ''.join(map(chr, [233]))
list(map(ord, unicodedata.normalize('NFD', s1)))
# [101, 769]
list(map(ord, unicodedata.normalize('NFD', s2)))
# [101, 769]
list(map(ord, unicodedata.normalize('NFC', s1)))
# [233]
list(map(ord, unicodedata.normalize('NFC', s2)))
# [233]
would the only choice here if you wanted to get the whole Ć© be to look around for a combining char?
that's the grapheme cluster
Unicode Standard tokenization routines and orthography profile segmentation - GitHub - cldf/segments: Unicode Standard tokenization routines and orthography profile segmentation
this is pretty neat, not sure it's useful for anything
% pipx install segments
installed package segments 2.2.0, Python 3.9.7
These apps are now globally available
- segments
done! ⨠š āØ
% segments profile <<< "aäaaöaaüaa"
Grapheme frequency mapping
a 7 a
Ƥ 1 Ƥ
ƶ 1 ƶ
ü 1 ü
yes
huh so i just discovered that it seems c-implemented modules take precedence and don't give circular dependency errors when naming the file the same as the imported module, but can't find information on why that is 
for example:
# threading.py, python3 threading.py
import threading # circular dependency error
# _thread.py, python3 _thread.py
import _thread # works fine
tested both on WSL ubuntu and windows, odd
wonder if anyone else knows this obscurity
That'll be because of the order in which the different importer finders are checked.
!e
import sys
print('Meta:', sys.meta_path)
print('Path:', sys.path_hooks)
@prime estuary :white_check_mark: Your eval job has completed with return code 0.
001 | Meta: [<class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>]
002 | Path: [<class 'zipimport.zipimporter'>, <function FileFinder.path_hook.<locals>.path_hook_for_FileFinder at 0x7f323789aca0>]
To import, each of the meta importers are checked, builtin (built into the Python executable) and frozen (importlib) come before the file-path finder so they always run first.
hi guys
Try _decimal, which isn't builtin to the interpreter itself but an external pyd (dll/so), that should have the regular dependency order.
Hello, what's your conundrum?
I created a code that automatically creates a perfect 3d cube in paint, to use it you need to install the pyautogui library (pip install pyautogui) and then just copy and paste!
Hey @scenic atlas!
Uh-oh! It looks like your message got zapped by our spam filter. We currently don't allow .txt attachments, so here are some tips to help you travel safely:
⢠If you attempted to send a message longer than 2000 characters, try shortening your message to fit within the character limit or use a pasting service (see below)
⢠If you tried to show someone your code, you can use codeblocks
(run !code-blocks in #bot-commands for more information) or use a pasting service like:
Hey @scenic atlas!
Uh-oh! It looks like your message got zapped by our spam filter. We currently don't allow .txt attachments, so here are some tips to help you travel safely:
⢠If you attempted to send a message longer than 2000 characters, try shortening your message to fit within the character limit or use a pasting service (see below)
⢠If you tried to show someone your code, you can use codeblocks
(run !code-blocks in #bot-commands for more information) or use a pasting service like:
import pyautogui
from time import sleep
pyautogui.press("win")
pyautogui.write("paint")
sleep(0.5)
pyautogui.press("enter")
sleep(2)
pyautogui.moveTo(x=874, y=381)
def cubo():
pyautogui.drag(0, 250, duration = 0.1)
sleep(0.1)
pyautogui.drag(250, 0, duration = 0.1)
sleep(0.1)
pyautogui.drag(0, -250, duration = 0.1)
sleep(0.1)
pyautogui.drag(-250, 0, duration = 0.1)
sleep(0.1)
pyautogui.moveTo(x=874, y=381)
sleep(0.1)
pyautogui.drag(100, -100, duration = 0.1)
pyautogui.moveTo(x=1124, y=382)
pyautogui.drag(100, -100, duration = 0.1)
pyautogui.moveTo(x=974, y=281)
sleep(0.1)
pyautogui.dragTo(x=1225, y=282, duration = 0.1)
sleep(0.1)
pyautogui.moveTo(x=1125, y=631)
pyautogui.drag(100, -100, duration = 0.1)
pyautogui.moveTo(x=1225, y=530)
sleep(0.1)
pyautogui.dragTo(x=1224, y=283)
sleep(0.1)
pyautogui.moveTo(x=974, y=281)
for i in range(13):
pyautogui.drag(0, 10, duration = 0.1)
pyautogui.move(0, 10, duration = 0.1)
pyautogui.moveTo(x = 874, y = 630)
for c in range(5):
pyautogui.drag(10, -10, duration = 0.1)
pyautogui.move(10, -10, duration = 0.1)
for cont in range(12):
pyautogui.move(10, 0, duration = 0.1)
pyautogui.drag(10, 0, duration = 0.1)
pyautogui.move(6, 0, duration = 0.1)
pyautogui.drag(4, 0, duration = 0.1)
cubo()
done!
good job, but this is a channel for discussion of the language itself
I must regretfully inform you that you are off topic, and also that you should enclose code in ```
anyways...I was thinking about GC
what is Gc?
so the GC is only needed for circular references, right
are there any stats on how much memory/etc. would have leaked due to circular references vs non-circular ones?
right
https://docs.python.org/3/library/gc.html#gc.get_stats tells you the number of objects that were caught in reference cycles and collected by the GC
I mean, in the general case
more like...way back then, was it a big priority? because naively I would think that users wouldn't create that many reference cycles intentionally when writing idiomatic code...?
but I have no idea whether that's true or not
that's very untrue. reference cycles are extremely common.
for example, there's one in:
def foo():
try:
1/0
except Exception as exc:
return exc
because the exception holds a reference to the frame it was raised from, and the frame holds a reference to the local variables of that stack frame, one of which is the exception.
is this something that is specific to Python, or in general?
Well, it's definitely true for Python - I'm not sure I can speak authoritatively about how common it is in other languages.
that talks about some common ways for reference cycles to occur in Python
it seems to me that at least the 2nd and 3rd cases, at least, are specific to Python (owing to its dynamic nature)
but thanks for the link! will look into the issue further
most languages that have garbage collection are very dynamic
or at least, many are š
Can I get you guy's opinion on this?
I'm getting really, really close to a basic working prototype
I figured I'd sorta sketch out how I want starting up one's own application to look like
I'd say Java is pretty dynamic
you can do a lot of things through reflection
from Application.Application import Application
from Template.Template import Template
from Window.Window import Window
that's beginning to look like Java
not to mention all the capitalized module names
eivl 19 hours on pycharm bruh you just leave it on or something??
Take it to Off-Topic
Is it possible to call VB6 function from python program?
if you can execute the function via command line, you can execute it from python via subprocess
that's the simplest way i think
integrating code from other languages can be quite challenging otherwise
Actually i am trying to convert the VB6 code to python in my project and there is this in built vb6 function i need to call
def connectMain():
AppPath = INI_IO("GLOBAL", "AppPath", App.Path, True)
if AppPath == "":
AppPath=""
JobTyp = [Command if Command.isnumeric() else 0]
AktDate = Now
server = ''
Driver = ''
Database = ''
uid= ''
pwd = ''
connection = pyodbc.connect("Driver={ };"
"Server = ;"
"Database = ;"
"uid = ;"
"pwd = ;")
print("S", "Start", "JobTyp = " + JobTyp)
In above INIO_IO is the function i am trying to call
what does it do?
not really sure..dont have access to that till now
i thought it was built in?
i mean i have been provided this whole VB6 code to convert
But it doesnt include that function part
i was thinking if there is anything just like we make python usable in shell script
maybe try to run the script in a debugger and see where it leads you
Sure man will try
on the other hand, maybe you can deduce what the function is supposed to do and then simply take a guess - shouldn't be hard with a variable name as "AppPath" and a check against "" below
but then again, i really don't get what if AppPath == "": AppPath="" is supposed to do..
Thats right..i can simply try it dummy data but the question is whether il be able to call VB function from somewhere else
Anyway il try it out
Thanks for the help man and please let me know if you come across any library or something that does that
VB6 code.. my condolences to you
i assume you know about configparser.ConfigParser ?
INI_IO seems like it's for parsing an ini file from some quick searching around
Seems like you are right...i did find one ini file in vb
il do some search around about configparser.ConfigParser you mentioned
This was helpful
if i go from windows 7 to 10 will I lose my files?
Definitely out of topic here
Go to offtopic channels
!ot
Off-topic channels
There are three off-topic channels:
⢠#ot0-psvmās-eternal-disapproval
⢠#ot1-perplexing-regexing
⢠#ot2-never-nesterās-nightmare
Their names change randomly every 24 hours, but you can always find them under the OFF-TOPIC/GENERAL category in the channel list.
Please read our off-topic etiquette before participating in conversations.
oh didnt know ot means of topic
Each channel has description at the top of the window
can anyone help me implement my sins in python? i'm looking for a functionality where subclasses can be instantiated directly, but the parent class which would otherwise be abstract can find & "change types" if called with a magic _type_ parameter:
import sys
from typing import Any, ClassVar, Optional, Union
import attr
class DynBase:
def __new__(cls, *args: Any, **kwargs: Any):
print(f"dynbase new (cls = {cls})")
if cls is DynBase:
# DynBase used directly; find real type from `_type_`
_type_: Optional[str] = kwargs.pop("_type_", None)
if isinstance(_type_, str):
target = getattr(sys.modules[__name__], _type_) # resolve str to class
else:
raise TypeError("can't instantiate a `DynBase` without `_type_` kwarg")
x = target(*args, **kwargs)
return x
else:
# a (real) subclass is being instantiated; continue up __new__ mro
s = super()
if s.__new__ is object.__new__:
x = s.__new__(cls)
return x
else:
# ??
x = s.__new__(cls, *args, **kwargs)
return x
def __init__(self):
print("dyn base init")
pass
is_dyn: ClassVar[bool] = True
@attr.s(auto_attribs=True, slots=True)
class DynA(DynBase):
arg0: int
arg1_a: bool
def __new__(cls, *args, **kwargs):
print("dyna new")
return super().__new__(cls, *args, **kwargs)
@attr.s(auto_attribs=True, slots=True)
class DynB(DynBase):
arg0: float
arg1_b: int
if __name__ == "__main__":
a = DynBase(arg0=5, arg1_a=True, _type_="DynA")
print(a)
which prints:
dynbase new (cls = <class '__main__.DynBase'>)
dyna new
dynbase new (cls = <class '__main__.DynA'>)
Traceback (most recent call last):
(...)
a = DynBase(arg0=5, arg1_a=True, _type_="DynA")
TypeError: __init__() got an unexpected keyword argument '_type_'
it seems like everything works except that there's the second, automatic __init__ call going to DynA.__init__, and, crucially, the interpreter is passing all the args and kwargs from the original DynBase call, which errors since DynA.__init__ isn't expecting a _type_ arg
as far as i can tell, there's no way to prevent the second init except to return an object of the wrong type, and no way to muddle with the args going in like the pop in DynBase.__new__ does. can subclasshook and friends help me here to "fake" it, or am i out of rope to hang myself with?
I know it looks strange. I've never built a framework of my own with this level of infrastructure (what you're seeing is only a small subset of it).
I'm trying to keep with the best practice. The capital letter module names are intentional, as is the camel case throughout the library. There's a good reason I swear xD
Beyond that, what's good, what's bad?
didn't ask me, but i'd consider "bubbling up" some of those names to higher import levels or adjusting the names because from X.X import X isn't very pleasant and possibly a source of confusion (don't beginners similarly screw up datetime.datetime)? also not a fan of the the capital module names, but as long as you have a good reason "oh well"
Yes! I plan on migrating all the publicly facing content of the API to the main module, so that no one will see my horrible uppercase module names
I'd like to maybe transition people's comments away from the cases though. Its relatively minor. I'm more concerned with the presentation of the startup procedure, particularly with the use of the dictionaries as arguments
Most of the classes one would launch in their opening script take a huge number of argumentsāmultiple "sets" of arguments in the case of the Application class